DBA Data[Home] [Help]

PACKAGE BODY: APPS.BEN_PROVIDER_POOLS

Source


1 package body ben_provider_pools as
2 /* $Header: benpstcr.pkb 120.25.12020000.2 2012/09/03 08:36:41 pvelvano ship $ */
3 --------------------------------------------------------------------------------
4 /*
5 +==============================================================================+
6 |                       Copyright (c) 1998 Oracle Corporation                  |
7 |                          Redwood Shores, California, USA                     |
8 |                               All rights reserved.                           |
9 +==============================================================================+
10 --
11 Name
12         Benefit provider pools
13 Purpose
14         This package is used to create benefit provided ledger entries as well
15         as flex credit enrolment results.
16 History
17   Date       Who       Version  What?
18   ----        ---      -------  -----
19   07 Jun 98  jcarpent  110.0    Created
20   18 jun 98  jcarpent  110.1    Added lots more functionality
21   22 jun 98  jcarpent  115.3    Added proper messages
22   30 jun 98  jcarpent  115.4    Restructured accumulate pools
23               Update cash entry not just ins
24   06 jul 98  jcarpent  115.5    When update result pass ovn
25   27 jul 98  jcarpent  115.6    Fix messages take out nocopy message.get
26   30 jul 98  jcarpent  115.7    removed use of the global g_credit_pool
27               for most stuff.
28   22 SEP 98  GPERRY    115.8    Corrected error messages
29   13 OCT 98  jcarpent  115.9    Added excess credit processing
30   16 OCT 98  jcarpent  115.10   Changed total_pools to allow non-flex
31               calls to pass through with no error.
32   02 NOV 98  jcarpent  115.11   Fixed accum pools for chc not providing
33                                 credits
34   10 NOV 98  jcarpent  115.12   Added bg_id to delete_bpl call
35   04 DEC 98  jcarpent  115.13   Check max elctn val on enrt_rt
36                                 Added code comments
37                                 Use pool_rlovr_rqmt_f.prtt_elig_rlovr_rl
38                                 Use pool.auto_alct_excs_flag
39   23 DEC 98  jcarpent  115.14   Subtract forfeitures from
40                                 total flex credits.
41   29 JAN 99  shdas     115.15   added codes for comp objs.
42   17 MAR 99  shdas     115.17   modified create_rollover_enrollment.
43   16 APR 99  shdas     115.18   changed ben_per_in_ler_f to ben_per_in_ler.
44   29 APR 99  shdas     115.19   added parameters to genutils.formula.
45   29 Apr 99  lmcdonal  115.20   prtt_enrt_rslt now has stat cd.
46   04 May 99  shdas     115.21   added jurisdiction code.
47   27 May 99  maagrawa  115.22   Added new procedures to re-calculate
48                     flex credits when choice may not be present.
49   25-Jun-99  jcarpent  115.23   Added per_in_ler_id to bpl.
50   09-Jul-99  jcarpent  115.24   Added checks for backed out nocopy pil
51   20-JUL-99  Gperry    115.25   genutils -> benutils package
52                                 rename.
53   12-AUG-99  maagrawa  115.26   Corrected cursor c_cash_abr
54                                 to fetch values into correct
55                                 variables.
56   07-Sep-99  shdas     115.27   Added codes for cmbn_ptip_opt.
57                                 fixed call to pay_mag_util (TGUY)
58                                 added bnft_val to election_information
59   21-Sep-99  lmcdonal  115.28   Added rlovr_val_rl and
60                                 dflt_excs_trtmt_rl calls.
61   23-Sep-99  cparmar   115.29   added item pil_flex.ler_id in total_pools
62                                 for the group by clause
63   21-Oct-99  lmcdonal  115.30   ledger did not always have
64                                 per-in-ler-id filled in
65   12-Nov-99  lmcdonal  115.31   Calls to create_Benefit_Prvdd_Ledger
66                                 must pass in person id and enrt_mthd_cd.
67                                 Added enrt_mthd_cd to create_debit_ledger_entry,
68                                 recompute_flex_credits
69 
70   17-Nov-99  pbodla    115.32   added acty_base_rt_id parameter to formula call
71                                 when  prtt_elig_rlovr_rl is evaluated.
72                                 Also added to run_rule procedure.
73   18-Nov-99  pbodla    115.33 - added elig_per_elctbl_chc_id parameter
74                                 to run_rule, formula calls.
75                               - elig_per_elctbl_chc_id selected in cursor
76                                 c_cash_abr
77                               - Added cursor c_epe
78   18-Jan-00 shdas      115.34 - changed c-ledger cursor in
79                                 cleanup_invalid_ledger_entries.
80   25-Jan-00 maagrawa   115.35 -Added parameter p_per_in_ler_id to procedures
81                                create_credit_ledger_entry,
82                                create_debit_ledger_entry,
83                                cleanup_invalid_ledger_entries,
84                                create_flex_credit_enrolment, total_pools,
85                                create_rollover_enrollment.
86                               -Fixed procedures to not look at 'STRTD'
87                                per_in_ler. (Bug 1148445)
88   29-Jan-00 shdas      115.36  modified remove_bnft_prvdd_ldgr(bug 4493)
89   28-Feb-00 maagrawa   115.37  Pass p_source to delete_enrollment.
90   28-Feb-00 shdas      115.38  Added get_dt_upd_mode(4785).
91   03-Mar-00 maagrawa   115.39  Get the acty_base_rt_id for the pool in
92                                procedure distribute_credits (c_cash_abr) using
93                                the ledger row.
94   09-Mar-00 lmcdonal   115.40  Support for oiplip flex credit rates.
95   05-Apr-00 mmogel     115.41  Added tokens to messages to make them more
96                                meaningful to the user
97   15-Aug-00 maagrawa   115.42  Removed the unions from the c_choice cursor in
98                                accumulate_pools procedure.
99   28-Sep-00 stee       115.43  UK Select changes. Net credits are processed
100                                through payroll.
101   09-Oct-00 stee       115.44  UK Select changes. When a person is found
102                                ineligible for a comp object, the net credits
103                                are re-calculated.
104   12-Oct-00 maagrawa   115.45  Added p_old_rlovr_amt to
105                                create_rollover_enrollment.
106                                Pass the bnft_id and bnft_val, if
107                                benefit record exists for rollover plan.
108   17-Oct-00 stee       115.46  Change c_prv2 to get acty_typ_cd from
109                                ben_prtt_rt_val. Fix total_credits in
110                                recompute_flex_credits to check for null
111                                forfeited amount.
112   18-Oct-00 stee       115.47  Added total pool restriction edit for rollover
113                                amount at the plan level.
114   16-Jan-01 mhoyes     115.48 - Added calculate only mode parameter to
115                                 create_debit_ledger_entry for EFC.
116   13-Feb-00 pbodla     115.50 - Put the version 115.48 with changes in 115.49
117   30-Mar-01 kmahendr   115.51 - Bug#1708166 - when comp.object is replaced in the
118                                 subsequent life event, the flex credit entry for
119                                 the deenrolled comp.object is not being deleted
120                                 from ben_bnft_prvdd_ldgr_f with the result the
121                                 used value is wrongly shown - added codes to
122                                 cleanup_invalid_ledger_entries
123   09-Apr-01 pbodla     115.52 - Bug 1711831 - While getting the activity base rate
124                                 for the provided credits choice row for the pool
125                                 we are working with cursor c_cash_abr is not
126                                 getting the row if the person was deenrolled from
127                                 comp object in the benmngle run. But the ledger
128                                 row is still sitting. To avoid activity base rate
129                                 row not found error use the cursor c_get_pool_abr.
130   12-Apr-1 ikasire     115.53   bug 1285336 changed the cursor
131                                 c_cmbn_ptip_opt_enrollment
132   01-May-01 kmahendr   115.54   Bug#1750825-Added edit to cleanup credit ledger entries
133                                 if the comp. object is deenrolled in the subsequent life
134                                 event.
135                                 Added Get_DT_Upd_Mode calls before update_ledger_api to
136                                 fix error 07211-future dated rows exist.
137   02-May-01 kmahendr   115.55   Leap frog version of 115.52 and changes in version
138                                 115.54 included
139   02-May-01 kmahendr   115.56   Version 115.54 brought in as version 115.55 was leap frog
140   17-May-01 maagrawa   115.57   Modified call to ben_global_enrt procedures.
141   10-Jul-01 mhoyes     115.58 - Converted compute_excess, create_credit_ledger_entry and
142                                 create_debit_ledger_entry for EFC.
143   27-aug-01 tilak      115.59   bug:1949361 jurisdiction code is
144                                               derived inside benutils.formula.
145   23-Oct-01 kmahendr   115.60   bug#2065445-cursor c_bnft_prvdd_ldgr and c_bnft_prvdd_ldgr_2
146                                 modified to select row based on p_effective_date
147   26-Oct-01 kmahendr   115.61   bug#2077743-in cursor c_prv order by rt_strt_dt desc added to
148                                 fetch the last rate row.
149   30-Oct-01 pbodla     115.62   bug#1931485-modified cusor c_rollovers to outerjoin
150                                 to ben_oipl_f table.
151   02-Nov-01 kmahendr   115.63   cursors in person_enrolled_in_choice modified to look for
152                                 effective_end_date = eot
153   09-Jan-02 kmahendr   115.64   Added a cursor c_enrt_rt to get the acty_base_rt_id for
154                                 getting the ledger entries thro cursor c_bnft_prvdd_ldgr_2
155                                 in cleanup_invalid ledger entries - Bug#2171014
156   25-Jan-02 pbodla     115.65   Bug 2185478 Added procedure to validate the
157                                 rollover value entered by the user on flex
158                                 enrollment form and show proper message immediately
159   25-Jan-02 stee       115.66   Check if a person is enrolled in the comp
160                                 object prior to deleting the ledger for a
161                                 roll over enrollment.  Bug 2119974.
162   28-Jan-02 pbodla     115.67   Added dbdrv lines for GSCC compliance.
163   07-Feb-02 ikasire    115.68   2199238 rollover records not appearing in the
164                                 flex enrollment form fixed.
165   11-Feb-02 kmahendr   115.69   Bug#2210322-Ledger rows for used_val need not be updated
166                                 in the same life event. Changed cursor in create_debit_ledger
167                                 procedure.
168   15-Feb-02 iaksire    115.70   Bug 2185478 added the union to the cursor in default_rollover
169                                 to handle the cases of enrt_bnft records
170   06-Mar-02 kmahendr   115.71   Bug#2254326 - Modified cursor c_flx_credit_plan to pick up
171                                 the one not deenrolled.
172   11-mar-02  tjesumic  115.72   bug#2251057 auto_alct_exces_flag control moved just for
173                                 Rollover
174   13 MAR 02  tjesumic  115.73   bug#2251057  automatic rollover is executed for default
175                                 enrollment cursor c_get_mthd added
176   15 May 02  ikasire   115.74   Bug 2200139 create_credit_ledger_entry always recomputes the
177                                 provided value from the enrt_rt record if the rate is changed
178                                 on bpp record. Now this will not happen if the enrollment
179                                 method code is 'O' (for Override Enrollment)
180   23 May 02  kmahendr  115.75   Added a parameter to ben_determine_acty_base_rt
181   39 May-02  ikasire   115.76   Bug 2386000 Added cursors to pass lee_rsn_id to rate_and
182                                 _coverage_dates call
183   08-Jun-02  pabodla   115.31   - Do not select the contingent worker
184                                   assignment when assignment data is
185                                   fetched.
186   08 Aug 02  kmahendr  115.78   -Bug#2382651 - added additional parameter to total_pools
187                                  and create_flex_credit_enrollment and made changes to
188                                  work for Multiple flex programs.
189   14 Aug 02  kmahendr  115.79  - Bug#2441871 - In Update_rate procedure the ler_id is
190                                  fetched from per_in_ler and create_prtt_rt api is called
191                                  only if enrt rt id is not null
192   11-Oct-02  vsethi    115.80    Rates Sequence no enhancements. Modified to cater
193                                  to new column ord_num on ben_acty_base_rt_f
194   06-Nov-02  ikasire   115.81    Bug 2645993 Fixed the case where Used amount is more than
195                                  the provided amount.
196   11-Nov-02  lakrish   115.82    Bug 2645624 Changed create_flex_credit_enrolment to allow
197                                  non-flex calls to pass through with no error.
198   02-dec-02  hnarayan  115.83    Bug fix 2689926 - fixed c_choice cursor in accumulate_pools
199   				 procedure to pick up the correct benefit pool row from EPE
200   30-Dec-02  mmudigon  115.84    NOCOPY
201   13-Feb-03  kmahendr  115.85    Added a parameter to call -acty_base_rt.main
202   01-May-03  pbodla    115.86    Removed where clause of c_choice as part of
203                                  2917128 as provided ledger entries are not
204                                  getting created properly.
205   13-May-03  rpgupta   115.87    Bug 2988218 - Even When the flex shell plan is invalid,
206   				 processing completes fine. If p_rt_val is null, we need
207   				 the error to be displayed. So changed the expression.
208   				 Also do this check only if it is called form a flex pgm,
209   				 so that a call from non flex does'nt error.
210   13-May-03  rpgupta   115.88    Added COBRAFLX to chk the program type
211   21-Aug-03  kmahendr  115.89    Bug#2736036 - for net credit method,deduction rate is updated to
212                                  zero if contribution is positive and vice versa.
213   19-Mar-04  ikasire   115.90    Added formula rate_periodization_rl
214   16-Apr-04  kmahendr  115.91    Added codes in total_pools and cleanup_invalid_ledger to
215                                  handle situations when benefit pool already enrolled
216                                  becomes inactive
217   02-Jun-04  nhunur    115.92    changed c_prv to prevent MJC
218   31-Aug-04  ikasire   115.93    Bug 3864152 fixed the nvl condition
219   22-Mar-05  abparekh  115.94    Bug 4251187 : Pass proper per_in_ler_id to update of BPL
220                                                in procedure distribute_credits
221   29-Apr-05  kmahendr  115.95    Bug#4340736 - ledger entry for forfeiture is written wrongly
222                                  Code added in total_pools
223   03-May-05  mmudigon  115.96    Bug 4320660. Order by clause for cursors
224                                  c_prv2 and c_prv_child
225   07-Jul-05  kmahendr  115.97    Bug#4473573 - added net_Credits_method condition
226                                  before resetting distributed value to 0 in
227                                  total_credits.
228   09-Aug-05  swjain    115.98    Bug#4538041 - Modified c_pgm_enrollment cursor in
229                                  procedure person_enrolled_in_choice to exclude enrollments in
230 				 Flex Credit Plans
231   26-Sep-05  swjain    115.100   Bug 4613270 - Modified cursor c_cash_abr in procedure
232                                  distribute_credits to fetch the acty_base_rt_id corresponding
233 				 to the provided(FLXCR) row only
234   04-Nov-05  swjain    115.101   Bug No 4714939 - Updated the cursor c_cash_rcvd to fetch any
235                                  cash row for the pool instead of a particular acty_base_rt_id
236   25-Dec-05  rbingi    115.102   Bug No 4964766 -  Modified cursor c_rlovr_chc to
237                                   rollover to the plan within the flex program
238                                   Modified c_oipl_enrolment to check pen's pgm_id also
239   06-Feb-05  rbingi    115.103   Contd: 4964766 - pgm_id condition added in
240                                   cursor c_plan_enrolment also.
241   13-Mar-05  rbingi    115.104   Contd: 4964766 - passing pen_id null to elinf when
242                                   created in prev LE and coverage starting in future.
243   06-Apr-06  kmahendr  115.105   Bug#5136668 - modified cursor c_prv in compute_rollover
244   27-Apr-06  rbingi    115.106   Bug 5185351: deleting ledger entries for suspended
245                                   enrollements in cleanup_invalid_ledger_entries
246   06-Jun-06  rbingi    115.107   Bug 5257226: Updating rate even when created in past pil
247                                   in case of updated ledger.
248   22-Aug-06  abparekh  115.108   Bug 5447507 : Fixed distribute_credits to query FLXCR ABR
249                                  from EPE, if not found through BPL
250   04-Sep-06  abparekh  115.109   Bug 5344961 : Fixed cursor C_BPL in procedure DELETE_ALL_LEDGERS
251   06-Sep-06  abparekh  115.110   Bug 5500864 : In procedure cleanup_invalid_ledger_entries delete BPL
252                                                in DELETE mode instead of ZAP mode to retain history
253                                                of ledger entries
254   02-NOV-06  ssarkar   115.111   bug 5608160 : calculation of l_new_prtt_rt_val is
255                                  modified based entr_val_enrt_flag
256   07-Nov-06  nhunur    115.112   cleanup_invalid_ledger_entries changed to pass correct dates for
257                                  delete BPL
258   22-Feb-08  rtagarra  115.113   Bug 6834215 : Closed the cursor c_choice.
259   02-Jul-08  sallumwa  115.114   Bug 7118730 : Re-calculate the ledger if used amt is more than
260                                  provided amount.
261   22-Oct-08  sallumwa  115.115   Bug 7363185 : Update the ledger even though there is no change
262                                  in the rollover amount.
263   25-Sep-09  sallumwa  115.116   Bug 8504085 : If the person is de-enrolled from the comp object,
264                                  check if there is any ledger entry corresponding to only that
265 				 comp object and delete the corresponding ledger entry.
266   05-Nov-09  sallumwa  115.117   Bug 8601352 : Modified the cursors in the procedure
267                                  recompute_flex_credits so that for the old prv records,the rate
268 				 end date doesn't change based on the new rate end date.
269   19-Feb-10  sallumwa  115.118   Bug 9388229 : If Provided value is more than Used value and
270                                  Cash received is still -ve,then reset it to zero.
271   17-May-10  sagnanas  115.119   Bug 9663838 : Modified the cursors in  procedure
272                                  recompute_flex_credits
273   27-May-10  sallumwa  115.120   Bug 9754353 : Cash Received value is set to zero only when
274                                  Provided value is more than Used value.
275   08-Dec-11  velvanop  115.121   Bug 13010726: When LE is processed and Automatic Enrollments are part of Flex pool,If enrollment and
276                                  deenrollment of automatic benefits happen, do not delete the new ledger record that is created for
277 				 the new per_in_ler_id
278   03-Sep-12  velvanop 115.122    Bug 14496319: Reverted the fix of Bug 13010726.
279 */
280 --------------------------------------------------------------------------------
281 g_package varchar2(80):='ben_provider_pools';
282 
283 --------------------------------------------------------------------------------
284 --                      run_rule
285 --------------------------------------------------------------------------------
286 procedure run_rule
287          (p_effective_date     in date
288          ,p_person_id          in number
289          ,p_dflt_excs_trtmt_rl in number default null
290          ,p_rlovr_val_rl       in number default null
291          ,p_business_group_id  in number
292          ,p_ler_id             in number
293          ,p_bnft_prvdr_pool_id in number
294          ,p_acty_base_rt_id    in number default null
295          ,p_elig_per_elctbl_chc_id    in number default null
296          ,p_dflt_excs_trtmt_cd out nocopy varchar2
297          ,p_mx_val             out nocopy number) is
298 
299   l_proc varchar2(72) := g_package||'.run_rule';
300 
301   cursor c_pool_rule_info is
302     select
303         bpp.pgm_id,
304         ptip.pl_typ_id,
305         nvl(plip.pl_id, plip2.pl_id) pl_id,
306         oipl.opt_id
307     from   ben_bnft_prvdr_pool_f bpp, ben_ptip_f ptip, ben_plip_f plip,
308            ben_oipl_f oipl, ben_oiplip_f oiplip, ben_plip_f plip2
309     where  p_effective_date between
310              bpp.effective_start_date and
311              bpp.effective_end_date and
312            bpp.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
313            bpp.business_group_id=p_business_group_id
314       and  bpp.ptip_id = ptip.ptip_id(+)
315       and  bpp.plip_id = plip.plip_id(+)
316       and  bpp.oiplip_id = oiplip.oiplip_id(+)
317       and  oiplip.oipl_id = oipl.oipl_id(+)
318       and  oiplip.plip_id = plip2.plip_id(+)
319       and  p_effective_date between
320              ptip.effective_start_date and
321              ptip.effective_end_date
322       and  p_effective_date between
323              plip.effective_start_date and
324              plip.effective_end_date
325       and  p_effective_date between
326              oiplip.effective_start_date and
327              oiplip.effective_end_date
328       and  p_effective_date between
329              oipl.effective_start_date and
330              oipl.effective_end_date
331       and  p_effective_date between
332              plip2.effective_start_date and
333              plip2.effective_end_date
334     ;
335     l_pool_rule_info c_pool_rule_info%rowtype;
336 
337   cursor c_person_rule_info is
338     select asg.assignment_id,
339         asg.organization_id,
340         loc.region_2
341     from  per_all_assignments_f asg,
342           hr_locations_all loc
343     where
344         asg.person_id = p_person_id and
345         asg.assignment_type <> 'C'and
346         asg.primary_flag='Y' and
347         asg.location_id = loc.location_id(+) and
348         p_effective_date between
349         asg.effective_start_date and asg.effective_end_date
350     ;
351     l_person_rule_info c_person_rule_info%rowtype;
352 
353   l_outputs           ff_exec.outputs_t;
354   l_jurisdiction_code varchar2(30);
355 
356 begin
357   hr_utility.set_location('Entering:'|| l_proc, 10);
358   open c_pool_rule_info;
359   fetch c_pool_rule_info into l_pool_rule_info;
360   if c_pool_rule_info%NOTFOUND or c_pool_rule_info%NOTFOUND is null then
361      close c_pool_rule_info;
362      fnd_message.set_name('BEN','BEN_91724_NO_FLX_CR_RT_FOUND');
363      fnd_message.set_token('PROC',l_proc);
364      fnd_message.set_token('PERSON_ID',to_char(p_person_id));
365      hr_utility.set_location(l_proc,82);
366      fnd_message.raise_error;
367   end if;
368   close c_pool_rule_info;
369 
370   open c_person_rule_info;
371   fetch c_person_rule_info into l_person_rule_info;
372   if c_person_rule_info%NOTFOUND or c_person_rule_info%NOTFOUND is null then
373      close c_person_rule_info;
374      fnd_message.set_name('BEN','BEN_91708_PERSON_NOT_FOUND');
375      fnd_message.set_token('PROC',l_proc);
376      fnd_message.set_token('ID', p_person_id);
377      hr_utility.set_location(l_proc,82);
378      fnd_message.raise_error;
379   end if;
380   close c_person_rule_info;
381 
382 --  if l_person_rule_info.region_2 is not null then
383 --    l_jurisdiction_code :=
384 --       pay_mag_utils.lookup_jurisdiction_code
385 --         (p_state => l_person_rule_info.region_2);
386 --  end if;
387 
388   if  p_dflt_excs_trtmt_rl is not null then
389       l_outputs := benutils.formula
390             (p_formula_id           => p_dflt_excs_trtmt_rl,
391              p_assignment_id        => l_person_rule_info.assignment_id,
392              p_organization_id      => l_person_rule_info.organization_id,
393              p_business_group_id    => p_business_group_id,
394              p_effective_date       => p_effective_date,
395              p_opt_id               => l_pool_rule_info.opt_id,
396              p_pl_id                => l_pool_rule_info.pl_id,
397              p_pgm_id               => l_pool_rule_info.pgm_id,
398              p_ler_id               => p_ler_id,
399              p_elig_per_elctbl_chc_id => p_elig_per_elctbl_chc_id,
400              p_pl_typ_id            => l_pool_rule_info.pl_typ_id,
401              p_jurisdiction_code    => l_jurisdiction_code);
402 
403         p_dflt_excs_trtmt_cd := l_outputs(l_outputs.first).value;
404         p_mx_val := null;
405   elsif p_rlovr_val_rl  is not null then
406         l_outputs := benutils.formula
407             (p_formula_id           => p_rlovr_val_rl,
408              p_assignment_id        => l_person_rule_info.assignment_id,
409              p_organization_id      => l_person_rule_info.organization_id,
410              p_business_group_id    => p_business_group_id,
411              p_effective_date       => p_effective_date,
412              p_opt_id               => l_pool_rule_info.opt_id,
413              p_pl_id                => l_pool_rule_info.pl_id,
414              p_pgm_id               => l_pool_rule_info.pgm_id,
415              p_ler_id               => p_ler_id,
416              p_elig_per_elctbl_chc_id => p_elig_per_elctbl_chc_id,
417              p_pl_typ_id            => l_pool_rule_info.pl_typ_id,
418              p_jurisdiction_code    => l_jurisdiction_code);
419         p_dflt_excs_trtmt_cd := null;
420         p_mx_val := l_outputs(l_outputs.first).value;
421   end if;
422   hr_utility.set_location('Leaving:'|| l_proc, 999);
423 
424 end run_rule;
425 --------------------------------------------------------------------------------
426 -- This procedure returns the update mode appropriate
427 -- for the given table and effective_date and key.
428 --
429 Procedure Get_DT_Upd_Mode
430           (p_effective_date        in  date,
431            p_base_table_name       in  varchar2,
432            p_base_key_column       in  varchar2,
433            p_base_key_value        in  number,
434            p_mode                  out nocopy varchar2
435            ) is
436   --
437   l_correction             boolean := TRUE;
438   l_update                 boolean := FALSE;
439   l_update_override        boolean := FALSE;
440   l_update_change_insert   boolean := FALSE;
441   --
442 begin
443 hr_utility.set_location(' Entering: Get_DT_Upd_Mode' , 10);
444   --
445   -- Get the appropriate update mode.
446   --
447   DT_Api.Find_DT_Upd_Modes(p_effective_date => p_effective_date,
448                     p_base_table_name       => p_base_table_name,
449                     p_base_key_column       => p_base_key_column,
450                     p_base_key_value        => p_base_key_value,
451                     p_correction            => l_correction,
452                     p_update                => l_update,
453                     p_update_override       => l_update_override,
454                     p_update_change_insert  => l_update_change_insert);
455   --
456   if l_update_override or l_update_change_insert then
457      p_mode := 'UPDATE_OVERRIDE';
458   elsif l_update then
459      p_mode := 'UPDATE';
460   elsif l_correction then
461      p_mode := 'CORRECTION';
462   end if;
463   --
464   hr_utility.set_location(' Leaving: Get_DT_Upd_Mode' , 10);
465 end;
466 
467 --------------------------------------------------------------------------------
468 --                            accumulate_pools
469 --------------------------------------------------------------------------------
470 procedure accumulate_pools(
471         p_validate                        in boolean default false,
472         p_person_id                       in number,
473         p_elig_per_elctbl_chc_id          in number,
474         p_enrt_mthd_cd                    in varchar2,
475         p_effective_date                  in date,
476         p_business_group_id               in number
477 ) is
478   --
479   l_proc varchar2(72) := g_package||'.accumulate_pools';
480   --
481   -- cursor c_choice
482   -- epe1 is the choice which is being enrolled in
483   -- epe is the choice which provides credits for epe1.
484   -- epe is at the level of epe1 (same row) or above.
485   --
486   -- do combinations using unions.
487   --
488 
489   -- bug fix 2689926 - The cursor c_choice can return multple choice rows which
490   -- have benefit pool id as not null. This can occur when multiple credits and benefit pools are
491   -- defined for comp objects. For eg: one at program level and one in plan or option in plan level.
492   -- then when the enrollment (epe row) is for Plan or option in plan the corresponding benefit pool
493   -- if present should be considered for creating the ledger entry as well as updating the prtt rate
494   -- value of the shell plan prtt_enrt_rslt row.
495   --
496 
497   cursor c_choice is
498     select      epe.bnft_prvdr_pool_id,
499                 epe.elig_per_elctbl_chc_id,
500                 epe.prtt_enrt_rslt_id,
501                 epe.pgm_id,
502                 epe.ptip_id,
503                 epe.plip_id,
504                 epe.pl_id,
505                 epe.oipl_id,
506                 epe.oiplip_id,
507                 epe.cmbn_plip_id,
508                 epe.cmbn_ptip_id,
509                 epe.cmbn_ptip_opt_id,
510                 epe.business_group_id,
511                 epe.per_in_ler_id
512     from        ben_elig_per_elctbl_chc epe1,
513                 ben_elig_per_elctbl_chc epe
514     where       epe1.elig_per_elctbl_chc_id=p_elig_per_elctbl_chc_id and
515                 epe1.business_group_id=p_business_group_id and
516                 epe1.pgm_id = epe.pgm_id and -- start fix 2689926
517 /* Removed where clause as part of 2917128
518 */
519                 epe1.per_in_ler_id = epe.per_in_ler_id and
520                 epe.bnft_prvdr_pool_id is not null and
521                 epe.business_group_id=p_business_group_id
522 	order by
523                 epe.pgm_id,
524                 epe.ptip_id,
525                 epe.plip_id,
526                 epe.pl_id,
527                 epe.oipl_id,
528                 epe.oiplip_id,
529                 epe.cmbn_plip_id,
530                 epe.cmbn_ptip_id,
531                 epe.cmbn_ptip_opt_id ;
532 
533 
534   l_epe                        ben_epe_shd.g_rec_type;
535   --
536   l_bnft_prvdd_ldgr_id         number;
537   l_prtt_enrt_rslt_id          number;
538   l_prtt_rt_val_id             number;
539   l_acty_ref_perd_cd           varchar2(30);
540   l_acty_base_rt_id            number;
541   l_rt_strt_dt                 date;
542   l_rt_val                     number;
543   l_element_type_id            number;
544   --
545 begin
546   --
547   hr_utility.set_location('Entering:'|| l_proc, 10);
548   hr_utility.set_location(' p_elig_per_elctbl_chc_id '||p_elig_per_elctbl_chc_id,12);
549   --
550   open c_choice;
551   loop
552     --
553     fetch c_choice into
554         l_epe.bnft_prvdr_pool_id,
555         l_epe.elig_per_elctbl_chc_id,
556         l_epe.prtt_enrt_rslt_id,
557         l_epe.pgm_id,
558         l_epe.ptip_id,
559         l_epe.plip_id,
560         l_epe.pl_id,
561         l_epe.oipl_id,
562         l_epe.oiplip_id,
563         l_epe.cmbn_plip_id,
564         l_epe.cmbn_ptip_id,
565         l_epe.cmbn_ptip_opt_id,
566         l_epe.business_group_id,
567         l_epe.per_in_ler_id
568     ;
569     exit when c_choice%notfound;
570     --
571     accumulate_pools_for_choice(
572         p_person_id                        => p_person_id,
573         p_epe_rec                          => l_epe,
574         p_enrt_mthd_cd                     => p_enrt_mthd_cd,
575         p_effective_date                   => p_effective_date
576     );
577     --
578   end loop;
579   close c_choice;
580   --
581   hr_utility.set_location(' Leaving:'||l_proc, 999);
582   --
583 end accumulate_pools;
584 --------------------------------------------------------------------------------
585 --                            accumulate_pools_for_choice
586 --------------------------------------------------------------------------------
587 procedure accumulate_pools_for_choice(
588         p_validate                      in boolean default false,
589         p_person_id                     in number,
590         p_epe_rec                       in ben_epe_shd.g_rec_type,
591         p_enrt_mthd_cd                  in varchar2,
592         p_effective_date                in date
593 ) is
594   --
595   l_proc                varchar2(72) := g_package||'.accumulate_pools_for_choice';
596   l_person_enrolled     boolean;
597   l_bnft_prvdd_ldgr_id  number;
598   l_enrt_rt_rec         ben_ecr_shd.g_rec_type;
599   --
600   l_dummy_number        number;
601   --
602 begin
603 
604   hr_utility.set_location('Entering:'|| l_proc, 10);
605   --
606   -- determine if the person is enrolled in the comp object or a component of it.
607   --
608   l_person_enrolled:=person_enrolled_in_choice(
609         p_person_id                        => p_person_id,
610         p_epe_rec                          => p_epe_rec,
611         p_effective_date                   => p_effective_date
612   );
613   --
614   -- If enrolled do the credit stuff
615   --
616   if (l_person_enrolled and
617         p_epe_rec.bnft_prvdr_pool_id is not null) then
618     hr_utility.set_location(l_proc, 20);
619     create_credit_ledger_entry
620       (p_person_id          => p_person_id
621       ,p_epe_rec            => p_epe_rec
622       ,p_bnft_prvdd_ldgr_id => l_bnft_prvdd_ldgr_id
623       ,p_enrt_mthd_cd       => p_enrt_mthd_cd
624       ,p_effective_date     => p_effective_date
625       --
626       ,p_bpl_prvdd_val      => l_dummy_number
627       );
628   end if;
629   --
630   hr_utility.set_location(' Leaving:'||l_proc, 999);
631   --
632 end accumulate_pools_for_choice;
633 --------------------------------------------------------------------------------
634 --                            person_enrolled_in_choice
635 --------------------------------------------------------------------------------
636 function person_enrolled_in_choice(
637         p_person_id                        number,
638         p_epe_rec                          ben_epe_shd.g_rec_type,
639         p_old_result_id                    number default hr_api.g_number,
640         p_effective_date                   date) return boolean is
641 
642   l_proc varchar2(72) := g_package||'.person_enrolled_in_choice';
643 
644   cursor c_pgm_enrolment is
645           select 'x'
646           from   ben_prtt_enrt_rslt_f per
647           where  per.person_id=p_person_id and
648                  per.business_group_id=p_epe_rec.business_group_id and
649                  p_effective_date between
650                    per.effective_start_date and per.effective_end_date and
651                  (enrt_cvg_thru_dt is null or
652                   enrt_cvg_thru_dt=hr_api.g_eot) and
653                  per.effective_end_date = hr_api.g_eot and
654                  per.pgm_id=p_epe_rec.pgm_id and
655                  per.prtt_enrt_rslt_id<>p_old_result_id and
656                  per.prtt_enrt_rslt_stat_cd is null and
657         	 -- Bug 4538041
658 		 per.COMP_LVL_CD <> 'PLANFC' and
659 		 -- End Bug 4538041
660                  per.sspndd_flag='N';
661 
662   cursor c_ptip_enrolment is
663           select 'x'
664           from   ben_prtt_enrt_rslt_f per,
665                  ben_pl_f pl,
666                  ben_ptip_f pt
667           where  per.person_id=p_person_id and
668                  per.business_group_id=p_epe_rec.business_group_id and
669                  p_effective_date between
670                    per.effective_start_date and per.effective_end_date and
671                  (enrt_cvg_thru_dt is null or
672                   enrt_cvg_thru_dt=hr_api.g_eot) and
673                  per.effective_end_date = hr_api.g_eot and
674                  per.pl_id=pl.pl_id and
675                  pl.business_group_id=p_epe_rec.business_group_id and
676                  per.prtt_enrt_rslt_id<>p_old_result_id and
677                  per.sspndd_flag='N' and
678                  per.prtt_enrt_rslt_stat_cd is null and
679                  pl.pl_typ_id=pt.pl_typ_id and
680                  pt.business_group_id=p_epe_rec.business_group_id and
681                  pt.pgm_id=p_epe_rec.pgm_id and
682                  p_epe_rec.ptip_id=pt.ptip_id;
683 
684   cursor c_cmbn_ptip_enrolment is
685           select 'x'
686           from   ben_prtt_enrt_rslt_f per,
687                  ben_pl_f pl,
688                  ben_ptip_f pt
689           where  per.person_id=p_person_id and
690                  per.business_group_id=p_epe_rec.business_group_id and
691                  p_effective_date between
692                    per.effective_start_date and per.effective_end_date and
693                  (enrt_cvg_thru_dt is null or
694                   enrt_cvg_thru_dt=hr_api.g_eot) and
695                  per.effective_end_date = hr_api.g_eot and
696                  per.pl_id=pl.pl_id and
697                  pl.business_group_id=p_epe_rec.business_group_id and
698                  per.prtt_enrt_rslt_id<>p_old_result_id and
699                  per.sspndd_flag='N' and
700                  per.prtt_enrt_rslt_stat_cd is null and
701                  pl.pl_typ_id=pt.pl_typ_id and
702                  pt.business_group_id=p_epe_rec.business_group_id and
703                  pt.pgm_id=p_epe_rec.pgm_id and
704                  p_epe_rec.cmbn_ptip_id=pt.cmbn_ptip_id;
705 
706   cursor c_cmbn_plip_enrolment is
707           select 'x'
708           from   ben_prtt_enrt_rslt_f per,
709                  ben_pl_f pl,
710                  ben_plip_f cpp
711           where  per.person_id=p_person_id and
712                  per.business_group_id=p_epe_rec.business_group_id and
713                  p_effective_date between
714                    per.effective_start_date and per.effective_end_date and
715                  (enrt_cvg_thru_dt is null or
716                   enrt_cvg_thru_dt=hr_api.g_eot) and
717                  per.effective_end_date = hr_api.g_eot and
718                  per.pl_id=pl.pl_id and
719                  pl.business_group_id=p_epe_rec.business_group_id and
720                  per.prtt_enrt_rslt_id<>p_old_result_id and
721                  per.sspndd_flag='N' and
722                  per.prtt_enrt_rslt_stat_cd is null and
723                  cpp.pl_id=pl.pl_id and
724                  cpp.business_group_id=p_epe_rec.business_group_id and
725                  cpp.pgm_id=p_epe_rec.pgm_id and
726                  p_epe_rec.cmbn_plip_id=cpp.cmbn_plip_id;
727 /* bug 1285336
728   cursor c_cmbn_ptip_opt_enrolment is
729           select 'x'
730           from   ben_prtt_enrt_rslt_f per,
731                  ben_pl_f pl,
732                  ben_ptip_f pt,
733                  ben_oipl_f oipl,
734                  ben_opt_f opt
735           where  per.person_id=p_person_id and
736                  per.business_group_id=p_epe_rec.business_group_id and
737                  p_effective_date between
738                    per.effective_start_date and per.effective_end_date and
739                  (enrt_cvg_thru_dt is null or
740                   enrt_cvg_thru_dt=hr_api.g_eot) and
741                  per.pl_id=pl.pl_id and
742                  pl.business_group_id=p_epe_rec.business_group_id and
743                  per.prtt_enrt_rslt_id<>p_old_result_id and
744                  per.sspndd_flag='N' and
745                  per.prtt_enrt_rslt_stat_cd is null and
746                  pl.pl_typ_id=pt.pl_typ_id and
747                  pt.business_group_id=p_epe_rec.business_group_id and
748                  pt.pgm_id=p_epe_rec.pgm_id and
749                  p_epe_rec.cmbn_ptip_opt_id=opt.cmbn_ptip_opt_id and
750                  p_effective_date between
751                    opt.effective_start_date and opt.effective_end_date and
752                  opt.business_group_id=p_epe_rec.business_group_id and
753                  oipl.opt_id=opt.opt_id and
754                  oipl.oipl_id=per.oipl_id and
755                  p_effective_date between
756                    oipl.effective_start_date and oipl.effective_end_date and
757                  oipl.business_group_id=p_epe_rec.business_group_id
758                 ;
759 */
760   cursor c_cmbn_ptip_opt_enrolment is
761           select 'x'
762           from   ben_prtt_enrt_rslt_f per,
763                  ben_pl_f pl,
764                  ben_optip_f otp,
765                  ben_oipl_f oipl
766           where  per.person_id=p_person_id and
767                  per.business_group_id=p_epe_rec.business_group_id and
768                  p_effective_date between
769                    per.effective_start_date and per.effective_end_date and
770                  (enrt_cvg_thru_dt is null or
771                   enrt_cvg_thru_dt=hr_api.g_eot) and
772                  per.effective_end_date = hr_api.g_eot and
773                  per.pl_id=pl.pl_id and
774                  pl.business_group_id=p_epe_rec.business_group_id and
775                  per.prtt_enrt_rslt_id<>p_old_result_id and
776                  per.sspndd_flag='N' and
777                  per.prtt_enrt_rslt_stat_cd is null and
778                  pl.pl_typ_id=otp.pl_typ_id and
779                  otp.business_group_id=p_epe_rec.business_group_id and
780                  otp.pgm_id=p_epe_rec.pgm_id and
781                  p_epe_rec.cmbn_ptip_opt_id=otp.cmbn_ptip_opt_id and
782                  p_effective_date between
783                    otp.effective_start_date and otp.effective_end_date and
784                  oipl.opt_id=otp.opt_id and
785                  oipl.oipl_id=per.oipl_id and
786                  p_effective_date between
787                    oipl.effective_start_date and oipl.effective_end_date and
788                  oipl.business_group_id=p_epe_rec.business_group_id
789                 ;
790 
791   cursor c_plan_enrolment is
792           select 'x'
793           from   ben_prtt_enrt_rslt_f per
794           where  per.person_id=p_person_id and
795                  per.business_group_id=p_epe_rec.business_group_id and
796                  per.prtt_enrt_rslt_id<>p_old_result_id and
797                  per.sspndd_flag='N' and
798                  per.prtt_enrt_rslt_stat_cd is null and
799                  p_effective_date between
800                    per.effective_start_date and per.effective_end_date and
801                  (enrt_cvg_thru_dt is null or
802                   enrt_cvg_thru_dt=hr_api.g_eot) and
803                  per.effective_end_date = hr_api.g_eot and
804                  p_epe_rec.pl_id=per.pl_id and
805                  p_epe_rec.pgm_id = per.pgm_id; -- Added : 4964766
806 
807   cursor c_oipl_enrolment is
808           select 'x'
809           from   ben_prtt_enrt_rslt_f per
810           where  per.person_id=p_person_id and
811                  per.business_group_id=p_epe_rec.business_group_id and
812                  per.prtt_enrt_rslt_id<>p_old_result_id and
813                  per.sspndd_flag='N' and
814                  per.prtt_enrt_rslt_stat_cd is null and
815                  p_effective_date between
816                    per.effective_start_date and per.effective_end_date and
817                  (enrt_cvg_thru_dt is null or
818                   enrt_cvg_thru_dt=hr_api.g_eot) and
819                  per.effective_end_date = hr_api.g_eot and
820                  p_epe_rec.oipl_id=per.oipl_id and
821                  p_epe_rec.pgm_id = per.pgm_id;  -- Added : 4964766
822 
823   cursor c_oiplip_enrolment is
824           select 'x'
825           from   ben_prtt_enrt_rslt_f per,ben_oiplip_f oiplip,ben_plip_f cpp
826           where  per.person_id=p_person_id and
827                  per.business_group_id=p_epe_rec.business_group_id and
828                  per.prtt_enrt_rslt_id<>p_old_result_id and
829                  per.sspndd_flag='N' and
830                  per.prtt_enrt_rslt_stat_cd is null and
831                  p_effective_date between
832                    per.effective_start_date and per.effective_end_date and
833                  (enrt_cvg_thru_dt is null or
834                   enrt_cvg_thru_dt=hr_api.g_eot) and
835                  per.effective_end_date = hr_api.g_eot and
836                  cpp.pgm_id = p_epe_rec.pgm_id and
837                  per.pl_id = cpp.pl_id and
838                  cpp.plip_id = oiplip.plip_id and
839                  per.oipl_id = oiplip.oipl_id and
840                  p_epe_rec.oiplip_id=oiplip.oiplip_id and
841                  p_effective_date between
842                    cpp.effective_start_date and cpp.effective_end_date and
843                  cpp.business_group_id=p_epe_rec.business_group_id and
844                  p_effective_date between
845                    oiplip.effective_start_date and oiplip.effective_end_date and
846                  oiplip.business_group_id=p_epe_rec.business_group_id;
847 
848   l_enrolled varchar2(1);
849 
850 begin
851 
852   hr_utility.set_location('Entering:'|| l_proc, 10);
853 
854   if p_epe_rec.cmbn_ptip_id is not null then
855     hr_utility.set_location(l_proc, 170);
856     open c_cmbn_ptip_enrolment;
857     fetch c_cmbn_ptip_enrolment into l_enrolled;
858     if c_cmbn_ptip_enrolment%notfound then
859       close c_cmbn_ptip_enrolment;
860       hr_utility.set_location(l_proc, 180);
861       return false;
862     else
863       close c_cmbn_ptip_enrolment;
864       hr_utility.set_location(l_proc, 190);
865       return true;
866     end if;
867 
868   elsif p_epe_rec.cmbn_plip_id is not null then
869     hr_utility.set_location(l_proc, 240);
870     open c_cmbn_plip_enrolment;
871     fetch c_cmbn_plip_enrolment into l_enrolled;
872     if c_cmbn_plip_enrolment%notfound then
873       close c_cmbn_plip_enrolment;
874       hr_utility.set_location(l_proc, 250);
875       return false;
876     else
877       close c_cmbn_plip_enrolment;
878       hr_utility.set_location(l_proc, 260);
879       return true;
880     end if;
881 
882   elsif p_epe_rec.cmbn_ptip_opt_id is not null then
883     hr_utility.set_location(l_proc, 200);
884     open c_cmbn_ptip_opt_enrolment;
885     fetch c_cmbn_ptip_opt_enrolment into l_enrolled;
886     if c_cmbn_ptip_opt_enrolment%notfound then
887       close c_cmbn_ptip_opt_enrolment;
888       hr_utility.set_location(l_proc, 210);
889       return false;
890     else
891       close c_cmbn_ptip_opt_enrolment;
892       hr_utility.set_location(l_proc, 220);
893       return true;
894     end if;
895     hr_utility.set_location(l_proc, 230);
896 
897   elsif p_epe_rec.oiplip_id is not null then
898     hr_utility.set_location(l_proc, 201);
899     open c_oiplip_enrolment;
900     fetch c_oiplip_enrolment into l_enrolled;
901     if c_oiplip_enrolment%notfound then
902       close c_oiplip_enrolment;
903       hr_utility.set_location(l_proc, 301);
904       return false;
905     else
906       close c_oiplip_enrolment;
907       hr_utility.set_location(l_proc, 401);
908       return true;
909     end if;
910     hr_utility.set_location(l_proc, 501);
911 
912   elsif p_epe_rec.oipl_id is not null then
913     hr_utility.set_location(l_proc, 20);
914     open c_oipl_enrolment;
915     fetch c_oipl_enrolment into l_enrolled;
916     if c_oipl_enrolment%notfound then
917       close c_oipl_enrolment;
918       hr_utility.set_location(l_proc, 30);
919       return false;
920     else
921       close c_oipl_enrolment;
922       hr_utility.set_location(l_proc, 40);
923       return true;
924     end if;
925     hr_utility.set_location(l_proc, 50);
926 
927   elsif p_epe_rec.pl_id is not null then
928     hr_utility.set_location(l_proc, 60);
929     open c_plan_enrolment;
930     fetch c_plan_enrolment into l_enrolled;
931     if c_plan_enrolment%notfound then
932       close c_plan_enrolment;
933       hr_utility.set_location(l_proc, 70);
934       return false;
935     else
936       close c_plan_enrolment;
937       return true;
938     end if;
939     hr_utility.set_location(l_proc, 80);
940 
941   elsif p_epe_rec.ptip_id is not null then
942     hr_utility.set_location(l_proc, 90);
943     open c_ptip_enrolment;
944     fetch c_ptip_enrolment into l_enrolled;
945     if c_ptip_enrolment%notfound then
946       close c_ptip_enrolment;
947       hr_utility.set_location(l_proc, 100);
948       return false;
949     else
950       close c_ptip_enrolment;
951       hr_utility.set_location(l_proc, 110);
952       return true;
953     end if;
954     hr_utility.set_location(l_proc, 120);
955 
956   elsif p_epe_rec.pgm_id is not null then
957     hr_utility.set_location(l_proc, 130);
958     open c_pgm_enrolment;
959     fetch c_pgm_enrolment into l_enrolled;
960     if c_pgm_enrolment%notfound then
961       close c_pgm_enrolment;
962       hr_utility.set_location(l_proc, 140);
963       return false;
964     else
965       close c_pgm_enrolment;
966       hr_utility.set_location(l_proc, 150);
967       return true;
968     end if;
969     hr_utility.set_location(l_proc, 160);
970 
971   else
972     hr_utility.set_location(l_proc, 270);
973     return false;
974   end if;
975   --
976   hr_utility.set_location(' Leaving:'||l_proc, 999);
977   --
978 end person_enrolled_in_choice;
979 --------------------------------------------------------------------------------
980 --                            create_credit_ledger_entry
981 --------------------------------------------------------------------------------
982 -- OVER LOADED PROCEDURE!!
983 --
984 procedure create_credit_ledger_entry(
985     p_validate          in boolean default false,
986     p_person_id         in number,
987     p_elig_per_elctbl_chc_id    in number,
988         p_per_in_ler_id                 in number,
989     p_business_group_id     in number,
990     p_bnft_prvdr_pool_id        in number,
991     p_enrt_mthd_cd          in varchar2,
992     p_effective_date        in date
993 )
994 is
995   l_proc varchar2(72) := g_package||'.create_credit_ledger_entry';
996   l_epe_rec ben_epe_shd.g_rec_type;
997   l_bnft_prvdd_ldgr_id number;
998   l_dummy_number       number;
999   l_pgm_id             number;
1000   --
1001   cursor c_bnft_pool is
1002     select bpp.pgm_id
1003     from   ben_bnft_prvdr_pool_f bpp
1004     where  bpp.bnft_prvdr_pool_id = p_bnft_prvdr_pool_id
1005     and    p_effective_date between bpp.effective_start_date
1006            and bpp.effective_end_date;
1007   --
1008 
1009 begin
1010 
1011   hr_utility.set_location('Entering:'|| l_proc, 10);
1012   hr_utility.set_location(' p_elig_per_elctbl_chc_id '||p_elig_per_elctbl_chc_id,13);
1013    --
1014    open c_bnft_pool;
1015    fetch c_bnft_pool into l_pgm_id;
1016    close c_bnft_pool;
1017    --
1018 
1019 
1020   l_epe_rec.per_in_ler_id          := p_per_in_ler_id;
1021   l_epe_rec.elig_per_elctbl_chc_id := p_elig_per_elctbl_chc_id;
1022   l_epe_rec.business_group_id      := p_business_group_id;
1023   l_epe_rec.bnft_prvdr_pool_id     := p_bnft_prvdr_pool_id;
1024   l_epe_rec.pgm_id                 := l_pgm_id;
1025 
1026   create_credit_ledger_entry
1027     (p_person_id          => p_person_id
1028 
1029     ,p_epe_rec            => l_epe_rec
1030     ,p_bnft_prvdd_ldgr_id => l_bnft_prvdd_ldgr_id
1031     ,p_enrt_mthd_cd       => p_enrt_mthd_cd
1032     ,p_effective_date     => p_effective_date
1033     --
1034     ,p_bpl_prvdd_val      => l_dummy_number
1035     );
1036   --
1037   hr_utility.set_location(' Leaving:'||l_proc, 999);
1038   --
1039 
1040 end create_credit_ledger_entry;
1041 --------------------------------------------------------------------------------
1042 --                            create_credit_ledger_entry
1043 --------------------------------------------------------------------------------
1044 -- OVER LOADED PROCEDURE!!
1045 --
1046 procedure create_credit_ledger_entry
1047   (p_validate            in     boolean default null
1048   ,p_calculate_only_mode in     boolean default false
1049   ,p_person_id           in     number
1050   ,p_epe_rec             in     ben_epe_shd.g_rec_type
1051   ,p_enrt_mthd_cd        in     varchar2
1052   ,p_effective_date  in     date
1053   --
1054   ,p_bnft_prvdd_ldgr_id     out nocopy number -- to pass back created id
1055   ,p_bpl_prvdd_val          out nocopy number
1056   )
1057 is
1058   l_proc varchar2(72) := g_package||'.create_credit_ledger_entry';
1059   l_enrt_rt_id            number;
1060   l_val                   number;
1061   l_effective_start_date  date;
1062   l_effective_end_date    date;
1063   l_object_version_number number;
1064   l_bnft_prvdd_ldgr_id    number;
1065   l_acty_base_rt_id       number;
1066   l_datetrack_mode        varchar2(30);
1067   l_prtt_enrt_rslt_id     number;
1068   l_prtt_rt_val_id        number;
1069   l_prvdd_val             number;
1070 
1071  /*
1072   cursor c_enrt_rt is
1073     select
1074           decode(enb.enrt_bnft_id,null,
1075            ecr2.enrt_rt_id,ecr1.enrt_rt_id) enrt_rt_id,
1076           decode(enb.enrt_bnft_id,null,
1077            ecr2.acty_base_rt_id,ecr1.acty_base_rt_id) acty_base_rt_id,
1078           decode(enb.enrt_bnft_id,null,
1079            nvl(ecr2.dflt_val,ecr2.val),nvl(ecr1.dflt_val,ecr1.val)) val
1080     from   ben_enrt_rt ecr1,
1081            ben_enrt_rt ecr2,
1082            ben_enrt_bnft enb
1083    where  ((ecr1.elig_per_elctbl_chc_id=p_epe_rec.elig_per_elctbl_chc_id
1084     and (ecr1.enrt_bnft_id = enb.enrt_bnft_id
1085     or ecr1.enrt_bnft_id is null)
1086     and ecr2.enrt_rt_id = ecr1.enrt_rt_id)
1087    or
1088      (ecr2.enrt_bnft_id = enb.enrt_bnft_id and
1089       ecr2.enrt_rt_id = ecr1.enrt_rt_id and
1090       enb.elig_per_elctbl_chc_id = p_epe_rec.elig_per_elctbl_chc_id))
1091    and
1092      (ecr1.business_group_id=p_epe_rec.business_group_id or
1093      ecr2.business_group_id=p_epe_rec.business_group_id)
1094    and
1095      --(ecr1.decr_bnft_prvdr_pool_id is null or
1096       --ecr2.decr_bnft_prvdr_pool_id is null)
1097      (ecr1.rt_usg_cd = 'FLXCR' or
1098       ecr2.rt_usg_cd = 'FLXCR')
1099     ;
1100  */
1101 
1102   cursor c_enrt_rt
1103     (c_epe_id in number
1104     )
1105   is
1106     select ecr.enrt_rt_id,
1107            ecr.acty_base_rt_id,
1108            nvl(ecr.dflt_val, ecr.val) val
1109     from   ben_enrt_rt ecr
1110     where  ecr.elig_per_elctbl_chc_id = c_epe_id
1111     and    ecr.rt_usg_cd = 'FLXCR';
1112 
1113   cursor c_old_ledger is
1114     select bpl.bnft_prvdd_ldgr_id,
1115            bpl.prvdd_val,
1116            bpl.object_version_number,
1117            bpl.effective_start_date
1118     from   ben_bnft_prvdd_ldgr_f bpl,
1119            ben_per_in_ler pil
1120     where  bpl.bnft_prvdr_pool_id=p_epe_rec.bnft_prvdr_pool_id
1121       and  bpl.business_group_id=p_epe_rec.business_group_id
1122       and  bpl.acty_base_rt_id = l_acty_base_rt_id
1123       and  bpl.prtt_enrt_rslt_id=g_credit_pool_result_id
1124       and  bpl.prvdd_val is not null
1125       and  p_effective_date between
1126              bpl.effective_start_date and bpl.effective_end_date
1127 and pil.per_in_ler_id=bpl.per_in_ler_id
1128 and pil.business_group_id=bpl.business_group_id
1129 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
1130     ;
1131 
1132 begin
1133   hr_utility.set_location('Entering:'|| l_proc, 15);
1134   --
1135   -- Get the flex credit result id and it's per-in-ler.   Both are needed
1136   -- for when we create ledger rows.
1137   --
1138   if not p_calculate_only_mode then
1139     --
1140     create_flex_credit_enrolment
1141       (p_person_id             => p_person_id
1142       ,p_enrt_mthd_cd          => p_enrt_mthd_cd
1143       ,p_business_group_id     => p_epe_rec.business_group_id
1144       ,p_effective_date        => p_effective_date
1145       ,p_prtt_enrt_rslt_id     => l_prtt_enrt_rslt_id
1146       ,p_prtt_rt_val_id        => l_prtt_rt_val_id
1147       ,p_per_in_ler_id         => p_epe_rec.per_in_ler_id
1148       ,p_rt_val                => null
1149       ,p_pgm_id                => p_epe_rec.pgm_id
1150       );
1151     --
1152   end if;
1153   hr_utility.set_location(l_proc, 40);
1154 
1155   hr_utility.set_location(l_proc, 50);
1156 
1157   hr_utility.set_location('starting create', 10);
1158   hr_utility.set_location('business_group_id='||to_char(p_epe_rec.business_group_id), 10);
1159   open c_enrt_rt
1160     (c_epe_id => p_epe_rec.elig_per_elctbl_chc_id
1161     );
1162   fetch c_enrt_rt into
1163     l_enrt_rt_id,
1164     l_acty_base_rt_id,
1165     l_val
1166   ;
1167   if c_enrt_rt%notfound
1168   then
1169     -- error
1170     hr_utility.set_location('BEN_91724_NO_FLX_CR_RT_FOUND', 51);
1171     fnd_message.set_name('BEN','BEN_91724_NO_FLX_CR_RT_FOUND');
1172     fnd_message.set_token('PROC',l_proc);
1173     fnd_message.set_token('PERSON_ID',to_char(p_person_id));
1174     hr_utility.set_location(' choice_id='''||to_char(p_epe_rec.elig_per_elctbl_chc_id),51);
1175     hr_utility.set_location(' rslt_id='''||to_char(g_credit_pool_result_id),51);
1176     close c_enrt_rt;
1177     fnd_message.raise_error;
1178   elsif not p_calculate_only_mode
1179   then
1180     hr_utility.set_location(l_proc, 20);
1181     if l_acty_base_rt_id is null then
1182       hr_utility.set_location('BEN_91725_NO_FLX_CR_ABR_FOUND', 52);
1183       fnd_message.set_name('BEN','BEN_91725_NO_FLX_CR_ABR_FOUND');
1184       fnd_message.set_token('PROC',l_proc);
1185       fnd_message.set_token('PERSON_ID',to_char(p_person_id));
1186       hr_utility.set_location(' enrt_rt_id='''||to_char(l_enrt_rt_id),52);
1187       close c_enrt_rt;
1188       fnd_message.raise_error;
1189     end if;
1190 
1191     hr_utility.set_location(' choice_id='||to_char(p_epe_rec.elig_per_elctbl_chc_id),51);
1192     hr_utility.set_location('looking for POOL p_epe_rec.bnft_prvdr_pool_id'||
1193              TO_CHAR(p_epe_rec.bnft_prvdr_pool_id),60);
1194     hr_utility.set_location(' l_acty_base_rt_id'||to_char(l_acty_base_rt_id),60);
1195     hr_utility.set_location(' g_credit_pool_result_id'||
1196           to_char(g_credit_pool_result_id), 60);
1197 
1198     -- if a row is already there update it
1199     open c_old_ledger;
1200     fetch c_old_ledger into
1201       l_bnft_prvdd_ldgr_id,
1202       l_prvdd_val,
1203       l_object_version_number,
1204       l_effective_start_date
1205     ;
1206     hr_utility.set_location(l_proc, 60);
1207     if c_old_ledger%notfound then
1208       hr_utility.set_location('val is'||to_char(l_val),12);
1209       ben_Benefit_Prvdd_Ledger_api.create_Benefit_Prvdd_Ledger (
1210            p_bnft_prvdd_ldgr_id            => l_bnft_prvdd_ldgr_id
1211           ,p_effective_start_date          => l_effective_start_date
1212           ,p_effective_end_date            => l_effective_end_date
1213           ,p_prtt_ro_of_unusd_amt_flag     => 'N'
1214           ,p_frftd_val                     => null
1215           ,p_prvdd_val                     => l_val
1216           ,p_used_val                      => null
1217           ,p_bnft_prvdr_pool_id            => p_epe_rec.bnft_prvdr_pool_id
1218           ,p_acty_base_rt_id               => l_acty_base_rt_id
1219           ,p_person_id                     => p_person_id
1220           ,p_enrt_mthd_cd                  => p_enrt_mthd_cd
1221           ,p_per_in_ler_id                 => p_epe_rec.per_in_ler_id
1222           ,p_prtt_enrt_rslt_id             => g_credit_pool_result_id
1223           ,p_business_group_id             => p_epe_rec.business_group_id
1224           ,p_object_version_number         => l_object_version_number
1225           ,p_cash_recd_val                 => null
1226           ,p_effective_date                => p_effective_date
1227        );
1228       hr_utility.set_location('CREATED LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),60);
1229       -- Bug 2200139 Override if user changes the provided value from the
1230       -- Override Enrollment form don't reset it again to the enrt_rt value
1231       --
1232     elsif l_val<>l_prvdd_val and nvl(p_enrt_mthd_cd,'E') <> 'O' then
1233       /*
1234       if l_effective_start_date=p_effective_date then
1235         l_datetrack_mode:=hr_api.g_correction;
1236       else
1237         l_datetrack_mode:=hr_api.g_update;
1238       end if;
1239       */
1240       Get_DT_Upd_Mode
1241          (p_effective_date        => p_effective_date,
1242           p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
1243           p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
1244           p_base_key_value        => l_bnft_prvdd_ldgr_id,
1245           p_mode                  => l_datetrack_mode);
1246       hr_utility.set_location('UPDATING LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),70);
1247       ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
1248              p_bnft_prvdd_ldgr_id         => l_bnft_prvdd_ldgr_id
1249           ,p_effective_start_date         => l_effective_start_date
1250           ,p_effective_end_date           => l_effective_end_date
1251           ,p_prtt_ro_of_unusd_amt_flag    => 'N'
1252           ,p_frftd_val                    => null
1253           ,p_prvdd_val                    => l_val
1254           ,p_used_val                     => null
1255           ,p_bnft_prvdr_pool_id           => p_epe_rec.bnft_prvdr_pool_id
1256           ,p_acty_base_rt_id              => l_acty_base_rt_id
1257           ,p_per_in_ler_id                => p_epe_rec.per_in_ler_id
1258           ,p_prtt_enrt_rslt_id            => g_credit_pool_result_id
1259           ,p_business_group_id            => p_epe_rec.business_group_id
1260           ,p_object_version_number        => l_object_version_number
1261           ,p_cash_recd_val                => null
1262           ,p_effective_date               => p_effective_date
1263         ,p_datetrack_mode                 => l_datetrack_mode
1264        );
1265       hr_utility.set_location('UPDATED LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),80);
1266     end if;
1267     p_bnft_prvdd_ldgr_id:=l_bnft_prvdd_ldgr_id;
1268     close c_old_ledger;
1269   end if;
1270   close c_enrt_rt;
1271   --
1272   p_bpl_prvdd_val := l_val;
1273   --
1274   hr_utility.set_location('p_bnft_prvdd_ldgr_id-' || p_bnft_prvdd_ldgr_id,123);
1275   hr_utility.set_location(' Leaving:'||l_proc, 998);
1276 
1277 end create_credit_ledger_entry;
1278 --------------------------------------------------------------------------------
1279 --                            create_debit_ledger_entry
1280 --------------------------------------------------------------------------------
1281 -- OVER LOADED PROCEDURE!!
1282 --
1283 procedure create_debit_ledger_entry
1284   (p_validate           in     boolean default false
1285   ,p_calculate_only_mode        in     boolean default false
1286   ,p_person_id          in     number
1287   ,p_per_in_ler_id              in     number
1288   ,p_elig_per_elctbl_chc_id in     number
1289   ,p_prtt_enrt_rslt_id      in     number
1290   ,p_decr_bnft_prvdr_pool_id    in     number
1291   ,p_acty_base_rt_id        in     number
1292   ,p_prtt_rt_val_id     in     number
1293   ,p_enrt_mthd_cd               in     varchar2
1294   ,p_val                        in     number
1295   ,p_bnft_prvdd_ldgr_id     in out nocopy number
1296   ,p_business_group_id      in     number
1297   ,p_effective_date     in     date
1298   --
1299   ,p_bpl_used_val                  out nocopy number
1300   )
1301 is
1302 
1303   l_proc                 varchar2(72) := g_package||'.create_debit_ledger_entry';
1304   l_epe_rec              ben_epe_shd.g_rec_type;
1305   l_ecr_rec              ben_ecr_shd.g_rec_type;
1306   l_prtt_enrt_rslt_id    number;
1307   l_prtt_rt_val_id       number;
1308   l_pgm_id               number;
1309   --
1310   --bug#2382651
1311   cursor c_epe is
1312     select pgm_id
1313     from ben_elig_per_elctbl_chc epe
1314     where epe.elig_per_elctbl_chc_id = p_elig_per_elctbl_chc_id;
1315  --
1316 
1317 begin
1318 
1319   hr_utility.set_location('Entering:'|| l_proc, 10);
1320   --
1321   open c_epe;
1322   fetch c_epe into l_pgm_id;
1323   close c_epe;
1324   -- Check for calculate only mode
1325   --
1326   if not p_calculate_only_mode then
1327     --
1328     -- Get the flex credit result id and it's per-in-ler.   Both are needed
1329     -- for when we create ledger rows.
1330     --
1331     create_flex_credit_enrolment
1332       (p_person_id                => p_person_id
1333       ,p_enrt_mthd_cd             => p_enrt_mthd_cd
1334       ,p_business_group_id        => p_business_group_id
1335       ,p_effective_date           => p_effective_date
1336       ,p_prtt_enrt_rslt_id        => l_prtt_enrt_rslt_id
1337       ,p_prtt_rt_val_id           => l_prtt_rt_val_id
1338       ,p_per_in_ler_id            => p_per_in_ler_id
1339       ,p_rt_val                   => null
1340       ,p_pgm_id                   => l_pgm_id
1341       );
1342     --
1343   end if;
1344   hr_utility.set_location(l_proc, 40);
1345   --
1346   l_epe_rec.elig_per_elctbl_chc_id:= p_elig_per_elctbl_chc_id;
1347   l_epe_rec.prtt_enrt_rslt_id     := p_prtt_enrt_rslt_id;
1348   l_epe_rec.business_group_id     := p_business_group_id;
1349   l_epe_rec.per_in_ler_id         := p_per_in_ler_id;
1350 
1351   l_ecr_rec.decr_bnft_prvdr_pool_id := p_decr_bnft_prvdr_pool_id;
1352   l_ecr_rec.acty_base_rt_id         := p_acty_base_rt_id;
1353   l_ecr_rec.prtt_rt_val_id          := p_prtt_rt_val_id;
1354   l_ecr_rec.val                     := p_val;
1355 
1356   create_debit_ledger_entry
1357     (p_validate             => p_validate
1358     ,p_calculate_only_mode  => p_calculate_only_mode
1359     ,p_person_id            => p_person_id
1360     ,p_enrt_mthd_cd         => p_enrt_mthd_cd
1361     ,p_epe_rec              => l_epe_rec
1362     ,p_enrt_rt_rec          => l_ecr_rec
1363     ,p_bnft_prvdd_ldgr_id   => p_bnft_prvdd_ldgr_id
1364     ,p_business_group_id    => p_business_group_id
1365     ,p_effective_date       => p_effective_date
1366     --
1367     ,p_bpl_used_val         => p_bpl_used_val
1368     );
1369   hr_utility.set_location('Leaving:'|| l_proc, 999);
1370 
1371 end create_debit_ledger_entry;
1372 --------------------------------------------------------------------------------
1373 --                            create_debit_ledger_entry
1374 --------------------------------------------------------------------------------
1375 -- OVER LOADED PROCEDURE!!
1376 --
1377 procedure create_debit_ledger_entry
1378   (p_validate            in     boolean default false
1379   ,p_calculate_only_mode in     boolean default false
1380   ,p_person_id           in     number
1381   ,p_enrt_mthd_cd        in     varchar2
1382   ,p_epe_rec             in     ben_epe_shd.g_rec_type
1383   ,p_enrt_rt_rec         in     ben_ecr_shd.g_rec_type
1384   ,p_bnft_prvdd_ldgr_id  in     out nocopy number  -- to pass back created id
1385   ,p_business_group_id   in     number
1386   ,p_effective_date      in     date
1387   --
1388   ,p_bpl_used_val           out nocopy number
1389   )
1390 is
1391 
1392   l_effective_start_date date;
1393   l_effective_end_date date;
1394   l_object_version_number number;
1395   l_val number;
1396   l_datetrack_mode varchar2(30);
1397 
1398   l_proc varchar2(72) := g_package||'.create_debit_ledger_entry';
1399   l_per_in_ler_id   number;
1400   l_used_val        number := 0;
1401 
1402   cursor c_old_ledger is
1403     select      bpl.bnft_prvdd_ldgr_id,
1404                 bpl.per_in_ler_id,
1405                 bpl.used_val,
1406                 bpl.object_version_number,
1407                 bpl.effective_start_date
1408     from        ben_bnft_prvdd_ldgr_f bpl,
1409                 ben_per_in_ler pil
1410     where       bpl.bnft_prvdr_pool_id=p_enrt_rt_rec.decr_bnft_prvdr_pool_id
1411         and     bpl.business_group_id=p_epe_rec.business_group_id
1412         and     bpl.acty_base_rt_id=p_enrt_rt_rec.acty_base_rt_id
1413         and     bpl.prtt_enrt_rslt_id=g_credit_pool_result_id
1414         and     bpl.used_val is not null
1415         and     p_effective_date between
1416                   bpl.effective_start_date and bpl.effective_end_date
1417         and pil.per_in_ler_id=bpl.per_in_ler_id
1418         and pil.business_group_id=bpl.business_group_id
1419         and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT') ;
1420 
1421   cursor c_prtt_rt_val
1422     (c_prtt_rt_val_id in number
1423     )
1424   is
1425     select rt_val
1426     from   ben_prtt_rt_val
1427     where  prtt_rt_val_id=p_enrt_rt_rec.prtt_rt_val_id;
1428 
1429 begin
1430 
1431   hr_utility.set_location('Entering:'|| l_proc, 15);
1432 
1433   -- see if a prtt_enrt_rt exists, is so use it's val
1434   if p_enrt_rt_rec.prtt_rt_val_id is null then
1435     l_val:=p_enrt_rt_rec.val;
1436   else
1437     -- get the value to use from prtt_rt_val
1438     open c_prtt_rt_val
1439       (c_prtt_rt_val_id => p_enrt_rt_rec.prtt_rt_val_id
1440       );
1441     fetch c_prtt_rt_val into l_val;
1442     if c_prtt_rt_val%notfound then
1443        l_val:=p_enrt_rt_rec.val;
1444     end if;
1445     close c_prtt_rt_val;
1446   end if;
1447   -- if a row is already there update it
1448   open c_old_ledger;
1449   fetch c_old_ledger into
1450     p_bnft_prvdd_ldgr_id,
1451     l_per_in_ler_id,
1452     l_used_val,
1453     l_object_version_number,
1454     l_effective_start_date;
1455   --
1456   -- Check for calculate only mode
1457   --
1458   if not p_calculate_only_mode then
1459     --
1460     if c_old_ledger%notfound then
1461        ben_Benefit_Prvdd_Ledger_api.create_Benefit_Prvdd_Ledger (
1462              p_bnft_prvdd_ldgr_id           => p_bnft_prvdd_ldgr_id
1463             ,p_effective_start_date         => l_effective_start_date
1464             ,p_effective_end_date           => l_effective_end_date
1465             ,p_prtt_ro_of_unusd_amt_flag    => 'N'
1466             ,p_frftd_val                    => null
1467             ,p_prvdd_val                    => null
1468             ,p_used_val                     => l_val
1469             ,p_bnft_prvdr_pool_id           => p_enrt_rt_rec.decr_bnft_prvdr_pool_id
1470             ,p_acty_base_rt_id              => p_enrt_rt_rec.acty_base_rt_id
1471             ,p_per_in_ler_id                => p_epe_rec.per_in_ler_id
1472             ,p_enrt_mthd_cd                 => p_enrt_mthd_cd
1473             ,p_person_id                    => p_person_id
1474             ,p_prtt_enrt_rslt_id            => g_credit_pool_result_id
1475             ,p_business_group_id            => p_epe_rec.business_group_id
1476             ,p_object_version_number        => l_object_version_number
1477             ,p_cash_recd_val                => null
1478             ,p_effective_date               => p_effective_date
1479         );
1480         hr_utility.set_location('CREATED LEDGER ID='||to_char(p_bnft_prvdd_ldgr_id),41);
1481     else
1482         /*if l_effective_start_date=p_effective_date then
1483           l_datetrack_mode:=hr_api.g_correction;
1484         else
1485           l_datetrack_mode:=hr_api.g_update;
1486         end if;*/
1487         Get_DT_Upd_Mode
1488          (p_effective_date        => p_effective_date,
1489           p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
1490           p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
1491           p_base_key_value        => p_bnft_prvdd_ldgr_id,
1492           p_mode                  => l_datetrack_mode);
1493        -- bug#2210322 - if there is no update on result row then no need to update ledger row
1494        -- or condition to take care of unrestricted life event
1495        if l_per_in_ler_id <> p_epe_rec.per_in_ler_id or
1496            l_used_val <> l_val then
1497 
1498            hr_utility.set_location('UPDATING LEDGER ID='||to_char(p_bnft_prvdd_ldgr_id),51);
1499 
1500            ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
1501                 p_bnft_prvdd_ldgr_id           => p_bnft_prvdd_ldgr_id
1502                ,p_effective_start_date         => l_effective_start_date
1503                ,p_effective_end_date           => l_effective_end_date
1504   --
1505   -- Bug 2199238 rollover plan not displayed because of this
1506   --             ,p_prtt_ro_of_unusd_amt_flag    => 'N'
1507                ,p_frftd_val                    => null
1508                ,p_prvdd_val                    => null
1509                ,p_used_val                     => l_val
1510                ,p_bnft_prvdr_pool_id           => p_enrt_rt_rec.decr_bnft_prvdr_pool_id
1511                ,p_acty_base_rt_id              => p_enrt_rt_rec.acty_base_rt_id
1512                ,p_per_in_ler_id                => p_epe_rec.per_in_ler_id
1513                ,p_prtt_enrt_rslt_id            => g_credit_pool_result_id
1514                ,p_business_group_id            => p_epe_rec.business_group_id
1515                ,p_object_version_number        => l_object_version_number
1516                ,p_cash_recd_val                => null
1517                ,p_effective_date               => p_effective_date
1518                ,p_datetrack_mode               => l_datetrack_mode
1519               );
1520              hr_utility.set_location('UPDATED LEDGER ID='||to_char(p_bnft_prvdd_ldgr_id),55);
1521         end if;
1522     end if;
1523     close c_old_ledger;
1524   end if;
1525   hr_utility.set_location(' Leaving:'||l_proc, 998);
1526   --
1527   -- Set OUT parameters
1528   --
1529   p_bpl_used_val := l_val;
1530   --
1531 end create_debit_ledger_entry;
1532 --------------------------------------------------------------------------------
1533 --                      cleanup_invalid_ledger_entries
1534 --------------------------------------------------------------------------------
1535 -- OVER LOADED PROCEDURE!!
1536 --
1537 procedure cleanup_invalid_ledger_entries(  -- so few args because uses global table
1538         p_validate              in boolean default false,
1539         p_person_id             in number,
1540         p_prtt_enrt_rslt_id     in number,
1541         p_effective_date        in date,
1542         p_business_group_id     in number
1543 ) is
1544   l_proc varchar2(72) := g_package||'.cleanup_invalid_ledger_entries';
1545   l_epe ben_epe_shd.g_rec_type;
1546   l_rt_usg_cd        varchar2(30);
1547   l_bnft_prvdd_ldgr_id number;
1548   l_ldgr_id number;
1549   l_per_in_ler_id number;
1550   l_person_enrolled boolean;
1551   l_effective_start_date date;
1552   l_effective_end_date date;
1553   l_object_version_number number;
1554   l_prtt_enrt_rslt_id  number;
1555   l_acty_base_rt_id  number;
1556   l_prtt_ro_of_unusd_amt_flag  ben_bnft_prvdd_ldgr_f.prtt_ro_of_unusd_amt_flag%type;
1557   l_delete_bpl boolean default true;
1558   l_exists varchar2(1);
1559   --
1560   -- this cursor needs some explaination
1561   --   It get the set of choices which have ledger entries - (for entire person)
1562   --   The first half of the query gets the choices and the abr to join to ledger
1563   --   The second half makes sure the ledger belongs to the correct person
1564   --   This is weird because the result id on the ledger is not the one which caused
1565   --   the ledger entry to be written.  jcarpent
1566 
1567   cursor c_ledger is
1568     select distinct
1569                 epe.bnft_prvdr_pool_id,
1570                 epe.elig_per_elctbl_chc_id,
1571                 epe.prtt_enrt_rslt_id,
1572                 epe.pgm_id,
1573                 epe.ptip_id,
1574                 epe.plip_id,
1575                 epe.pl_id,
1576                 epe.oipl_id,
1577                 epe.cmbn_ptip_id,
1578                 epe.cmbn_plip_id,
1579                 epe.cmbn_ptip_opt_id,
1580                 epe.business_group_id,
1581                 pil.per_in_ler_id,
1582                 bpl.bnft_prvdd_ldgr_id,
1583                 bpl.object_version_number,
1584                 decode(enb.enrt_bnft_id, null,
1585                 ecr2.rt_usg_cd,ecr1.rt_usg_cd) rt_usg_cd
1586     from        ben_elig_per_elctbl_chc epe,
1587                 ben_per_in_ler pil,
1588                 ben_enrt_rt ecr1,
1589                 ben_enrt_rt ecr2,
1590                 ben_enrt_bnft enb,
1591                 ben_bnft_prvdd_ldgr_f bpl,
1592                 ben_elig_per_elctbl_chc epe_flex,
1593                 ben_per_in_ler pil_flex
1594     where       pil.person_id=p_person_id and
1595                 pil.business_group_id=p_business_group_id and
1596                 pil.per_in_ler_stat_cd='STRTD' and
1597                 pil.per_in_ler_id=epe.per_in_ler_id and
1598                 bpl.per_in_ler_id = pil.per_in_ler_id and
1599                 epe.business_group_id=p_business_group_id and
1600                 --epe.bnft_prvdr_pool_id is not null and
1601                 epe.elig_per_elctbl_chc_id=ecr2.elig_per_elctbl_chc_id(+) and
1602                 (ecr1.acty_base_rt_id=bpl.acty_base_rt_id
1603                  or ecr2.acty_base_rt_id=bpl.acty_base_rt_id) and
1604                 epe.elig_per_elctbl_chc_id=enb.elig_per_elctbl_chc_id(+) and
1605                 enb.enrt_bnft_id = ecr1.enrt_bnft_id(+) and
1606                 bpl.business_group_id=p_business_group_id and
1607                 p_effective_date between
1608                   bpl.effective_start_date and bpl.effective_end_date
1609                 --and bpl.prvdd_val is null and
1610                 and bpl.prtt_enrt_rslt_id=epe_flex.prtt_enrt_rslt_id and
1611                 epe_flex.business_group_id=p_business_group_id and
1612                 epe_flex.per_in_ler_id=pil_flex.per_in_ler_id and
1613                 pil_flex.business_group_id=p_business_group_id and
1614                 pil_flex.person_id=p_person_id --and
1615     ;
1616 
1617     cursor c_rslt_exist is
1618     select pen.prtt_enrt_rslt_id
1619     from ben_prtt_enrt_rslt_f pen
1620     where pen.prtt_enrt_rslt_id <> p_prtt_enrt_rslt_id
1621     and pen.comp_lvl_cd <> 'PLANFC'
1622     and pen.person_id = p_person_id
1623     and p_effective_date between
1624     pen.effective_start_date and pen.effective_end_date;
1625 
1626     cursor c_ldgr_exist(l_ldgr_id number,l_per_in_ler_id number) is
1627     select 'x'
1628     from ben_bnft_prvdd_ldgr_f bpl,
1629          ben_per_in_ler        pil
1630     where bpl.bnft_prvdd_ldgr_id = l_ldgr_id
1631     and bpl.per_in_ler_id = l_per_in_ler_id
1632     -- UK change : Bug 1634870
1633     and bpl.per_in_ler_id = pil.per_in_ler_id
1634     and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
1635     and p_effective_date between
1636     bpl.effective_start_date and bpl.effective_end_date;
1637     --
1638     --Bug#1708166 - the cursor above deletes entries related to deenrolled
1639     -- comp.object only if the comp.object is deleted in the same life event.
1640     -- In other words, in the subsequent life event, if the comp.object
1641     -- is replaced , the related ledger entry is not being deleted.
1642     -- The following cursors accomplish the task
1643     --
1644     cursor c_rslt is
1645        select enrt_cvg_strt_dt,
1646               enrt_cvg_thru_dt,
1647               prtt_enrt_rslt_stat_cd,
1648               sspndd_flag
1649        from   ben_prtt_enrt_rslt_f
1650        where  prtt_enrt_rslt_id = p_prtt_enrt_rslt_id
1651        and    p_effective_date between effective_start_date
1652               and effective_end_date;
1653     --
1654     cursor c_acty_base_rt is
1655        select distinct acty_base_rt_id
1656        from ben_prtt_rt_val
1657        where prtt_enrt_rslt_id = p_prtt_enrt_rslt_id;
1658     --
1659     ---------------Bug 8504085
1660     ----------Get the flex credit epe ID against the comp object
1661     cursor c_get_credit_ledger_epe(p_pil_id number,p_oipl_id number) is
1662        SELECT *
1663          FROM ben_elig_per_elctbl_chc epe
1664         WHERE epe.per_in_ler_id = p_pil_id
1665           AND epe.business_group_id = p_business_group_id
1666           AND epe.bnft_prvdr_pool_id is not null
1667           AND epe.oipl_id = p_oipl_id
1668           AND comp_lvl_cd = 'OIPLIP';
1669 
1670     l_get_credit_ledger_epe   c_get_credit_ledger_epe%rowtype;
1671     cursor c_acty_base_rt1(p_epe_id number) is
1672        SELECT acty_base_rt_id
1673          FROM ben_enrt_rt
1674         WHERE rt_usg_cd = 'FLXCR'
1675           AND business_group_id = p_business_group_id
1676           AND elig_per_elctbl_chc_id = p_epe_id;
1677 
1678     l_acty_base_rt1   c_acty_base_rt1%rowtype;
1679     ---------------Bug 8504085
1680     cursor c_flx_credit_plan is
1681        select prtt_enrt_rslt_id
1682        from ben_prtt_enrt_rslt_f
1683        where person_id = p_person_id
1684        and   comp_lvl_cd = 'PLANFC'
1685        and   prtt_enrt_rslt_stat_cd is null
1686        -- added effective_end_date line Bug# 2254326
1687        and   effective_end_date = hr_api.g_eot
1688        and   p_effective_date between enrt_cvg_strt_dt
1689              and enrt_cvg_thru_dt;
1690     --
1691     cursor c_bnft_prvdd_ldgr(c_per_in_ler_id number) is
1692        select bpl.bnft_prvdd_ldgr_id,
1693               bpl.prtt_ro_of_unusd_amt_flag,
1694               bpl.object_version_number,
1695               bpl.effective_start_date
1696        from   ben_bnft_prvdd_ldgr_f bpl,
1697               ben_per_in_ler pil
1698        where  bpl.acty_base_rt_id = l_acty_base_rt_id
1699        and    bpl.prtt_enrt_rslt_id = l_prtt_enrt_rslt_id
1700     --   and    bpl.effective_end_date = hr_api.g_eot
1701        and    p_effective_date between bpl.effective_start_date
1702               and bpl.effective_end_date
1703        and    bpl.per_in_ler_id = pil.per_in_ler_id
1704        and    bpl.per_in_ler_id = nvl(c_per_in_ler_id,bpl.per_in_ler_id)
1705        and    pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT');
1706 
1707     /* Bug 13010726 */
1708     cursor c_flx_credit_plan_deenrt is
1709        select pen1.prtt_enrt_rslt_id,pen1.per_in_ler_id
1710        from ben_prtt_enrt_rslt_f pen1,
1711             ben_prtt_enrt_rslt_f pen2
1712        where pen1.person_id = p_person_id
1713        and   pen1.comp_lvl_cd = 'PLANFC'
1714        and   pen1.prtt_enrt_rslt_stat_cd is null
1715        and   pen1.effective_end_date = hr_api.g_eot
1716        and   p_effective_date between pen1.enrt_cvg_strt_dt
1717              and pen1.enrt_cvg_thru_dt
1718        and   pen2.prtt_enrt_rslt_id = p_prtt_enrt_rslt_id
1719        and   p_effective_date between pen2.effective_start_date
1720               and pen2.effective_end_date
1721        and   ( (pen2.enrt_cvg_thru_dt <> hr_api.g_eot and pen2.per_in_ler_id <> pen1.per_in_ler_id)
1722                or (pen2.per_in_ler_id = pen1.per_in_ler_id) ) ;
1723 
1724      l_per_in_ler_id_1 number;
1725     --
1726     -- Bug#1750825
1727     cursor c_choice is
1728     select      epe.bnft_prvdr_pool_id,
1729                 epe.elig_per_elctbl_chc_id,
1730                 epe.prtt_enrt_rslt_id,
1731                 epe.pgm_id,
1732                 epe.ptip_id,
1733                 epe.plip_id,
1734                 epe.pl_id,
1735                 epe.oipl_id,
1736                 epe.oiplip_id,
1737                 epe.cmbn_plip_id,
1738                 epe.cmbn_ptip_id,
1739                 epe.cmbn_ptip_opt_id,
1740                 epe.business_group_id,
1741                 epe.per_in_ler_id
1742     from        ben_elig_per_elctbl_chc epe1,
1743                 ben_elig_per_elctbl_chc epe,
1744                 ben_per_in_ler pil
1745     where       epe1.prtt_enrt_rslt_id=p_prtt_enrt_rslt_id and
1746                 epe1.business_group_id=p_business_group_id and
1747                 epe1.pgm_id = epe.pgm_id and
1748                 epe1.per_in_ler_id = epe.per_in_ler_id and
1749                 epe.bnft_prvdr_pool_id is not null and
1750                 epe.business_group_id=p_business_group_id and
1751                 epe1.per_in_ler_id = pil.per_in_ler_id and
1752                 pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT');
1753     --
1754      cursor c_bnft_prvdd_ldgr_2 (p_acty_base_rt_id number) is
1755        select bpl.bnft_prvdd_ldgr_id,
1756               bpl.object_version_number,
1757               effective_start_date
1758        from   ben_bnft_prvdd_ldgr_f bpl,
1759               ben_per_in_ler pil
1760        where  bpl.bnft_prvdr_pool_id = l_epe.bnft_prvdr_pool_id
1761        and    bpl.prtt_enrt_rslt_id = l_prtt_enrt_rslt_id
1762        and    bpl.acty_base_rt_id   = p_acty_base_rt_id
1763        --and    bpl.effective_end_date = hr_api.g_eot
1764        and    p_effective_date between bpl.effective_start_date
1765               and bpl.effective_end_date
1766        and    bpl.per_in_ler_id = pil.per_in_ler_id
1767        and    pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT');
1768 
1769     --
1770     --    Bug#2171014
1771     --
1772     cursor c_enrt_rt
1773     (c_epe_id in number
1774     )
1775       is
1776     select ecr.enrt_rt_id,
1777            ecr.acty_base_rt_id,
1778            nvl(ecr.dflt_val, ecr.val) val
1779     from   ben_enrt_rt ecr
1780     where  ecr.elig_per_elctbl_chc_id = c_epe_id
1781     and    ecr.rt_usg_cd = 'FLXCR';
1782     --
1783     cursor c_person_enrolled is
1784        select null
1785        from ben_elig_per_elctbl_chc epe
1786            ,ben_prtt_enrt_rslt_f pen
1787            ,ben_enrt_rt ecr
1788            ,ben_enrt_bnft enb
1789        where epe.prtt_enrt_rslt_id = pen.prtt_enrt_rslt_id
1790        and   pen.person_id = p_person_id
1791        and   pen.prtt_enrt_rslt_stat_cd is null
1792        and   epe.business_group_id = pen.business_group_id
1793        and   pen.sspndd_flag = 'N'
1794        and   pen.enrt_cvg_thru_dt = hr_api.g_eot
1795        and   pen.effective_end_date = hr_api.g_eot
1796        and   epe.elig_per_elctbl_chc_id = enb.elig_per_elctbl_chc_id
1797        and   enb.enrt_bnft_id = ecr.enrt_bnft_id
1798        and   ecr.acty_base_rt_id = l_acty_base_rt_id;
1799     --
1800     l_enrt_rt    c_enrt_rt%rowtype;
1801 
1802     l_ldgr_exist  varchar2(1);
1803     l_rslt_id number;
1804     l_rslt    c_rslt%rowtype;
1805     l_purge   varchar2(1) := 'N';
1806     -- Bug 5500864
1807     l_bpl_esd        date;
1808     l_datetrack_mode varchar2(30);
1809     l_effective_date date;
1810 --
1811 begin
1812   hr_utility.set_location('Entering:'|| l_proc, 10);
1813   open c_ledger;
1814 
1815   loop
1816     fetch c_ledger into
1817         l_epe.bnft_prvdr_pool_id,
1818         l_epe.elig_per_elctbl_chc_id,
1819         l_epe.prtt_enrt_rslt_id,
1820         l_epe.pgm_id,
1821         l_epe.ptip_id,
1822         l_epe.plip_id,
1823         l_epe.pl_id,
1824         l_epe.oipl_id,
1825         l_epe.cmbn_ptip_id,
1826         l_epe.cmbn_plip_id,
1827         l_epe.cmbn_ptip_opt_id,
1828         l_epe.business_group_id,
1829         l_per_in_ler_id,
1830         l_bnft_prvdd_ldgr_id,
1831         l_object_version_number,
1832         l_rt_usg_cd
1833     ;
1834     hr_utility.set_location(l_proc, 20);
1835 
1836     exit when c_ledger%notfound;
1837 
1838     hr_utility.set_location('ldgr is '||l_bnft_prvdd_ldgr_id, 30);
1839     hr_utility.set_location('Pool is '||l_epe.bnft_prvdr_pool_id, 30);
1840     hr_utility.set_location('PGM is '||l_epe.pgm_id, 30);
1841     hr_utility.set_location('PLIP is '||l_epe.plip_id, 30);
1842     hr_utility.set_location('PL is '||l_epe.pl_id, 30);
1843     hr_utility.set_location('OIPL is '||l_epe.oipl_id, 30);
1844     hr_utility.set_location('PTIP is '||l_epe.ptip_id, 30);
1845 
1846 
1847     l_person_enrolled:=person_enrolled_in_choice(
1848         p_person_id        => p_person_id,
1849         p_epe_rec          => l_epe,
1850         p_old_result_id    => p_prtt_enrt_rslt_id,
1851         p_effective_date   => p_effective_date    );
1852 
1853     hr_utility.set_location(l_proc, 40);
1854     --
1855     -- if the person is not enrolled or
1856     -- rt_usg_cd<>'FLXCR' and not directly enrolled
1857     -- then remove the ledger entry
1858     --
1859     hr_utility.set_location('Checking ledger='||to_char(l_bnft_prvdd_ldgr_id), 45);
1860     if (not l_person_enrolled) then
1861         open c_ldgr_exist(l_bnft_prvdd_ldgr_id,l_per_in_ler_id);
1862         fetch c_ldgr_exist into l_ldgr_exist;
1863         if c_ldgr_exist%notfound then
1864             close c_ldgr_exist;
1865         else
1866 
1867         --nvl(l_rt_usg_cd,hr_api.g_varchar2)<>'FLXCR') then
1868       hr_utility.set_location('Deleting ledger='||to_char(l_bnft_prvdd_ldgr_id), 50);
1869       ben_Benefit_Prvdd_Ledger_api.delete_Benefit_Prvdd_Ledger(
1870         p_bnft_prvdd_ldgr_id      => l_bnft_prvdd_ldgr_id,
1871         p_effective_start_date    => l_effective_start_date,
1872         p_effective_end_date      => l_effective_end_date,
1873         p_object_version_number   => l_object_version_number,
1874         p_effective_date          => p_effective_date,
1875         p_datetrack_mode          => hr_api.g_zap,
1876         p_business_group_id       => p_business_group_id
1877       );
1878       close c_ldgr_exist;
1879       end if;
1880       --check if there are any ledger entry only corresponding to this Activity base rt ID,Bug 8504085
1881       if l_epe.oipl_id is not null then
1882         hr_utility.set_location('oipl ID not null ,l_epe.oipl_id : '||l_epe.oipl_id, 50);
1883 	 open c_get_credit_ledger_epe(l_per_in_ler_id,l_epe.oipl_id);
1884 	 fetch c_get_credit_ledger_epe into l_get_credit_ledger_epe;
1885 	 if c_get_credit_ledger_epe%found then
1886 	 open c_acty_base_rt1(l_get_credit_ledger_epe.elig_per_elctbl_chc_id);
1887 	 fetch c_acty_base_rt1 into l_acty_base_rt_id;
1888 	 if c_acty_base_rt1%found then
1889 	     hr_utility.set_location('l_acty_base_rt_id1 : '|| l_acty_base_rt_id, 50);
1890 	    open c_flx_credit_plan;
1891 	    fetch c_flx_credit_plan into l_prtt_enrt_rslt_id;
1892 	    close c_flx_credit_plan;
1893            if l_prtt_enrt_rslt_id is not null then
1894 	    open c_bnft_prvdd_ldgr(NULL);
1895           fetch c_bnft_prvdd_ldgr into l_bnft_prvdd_ldgr_id,
1896                                        l_prtt_ro_of_unusd_amt_flag,
1897                                        l_object_version_number,
1898                                        l_bpl_esd;
1899           hr_utility.set_location('ldgr id is'||l_bnft_prvdd_ldgr_id, 51);
1900           if c_bnft_prvdd_ldgr%found then
1901               if p_effective_date = l_bpl_esd
1902               then
1903                 l_datetrack_mode := hr_api.g_zap;
1904 		l_effective_date := l_bpl_esd ;
1905               else
1906                 l_datetrack_mode := hr_api.g_delete;
1907 		l_effective_date := p_effective_date - 1 ;
1908               end if;
1909               --
1910               ben_Benefit_Prvdd_Ledger_api.delete_Benefit_Prvdd_Ledger(
1911                 p_bnft_prvdd_ldgr_id      => l_bnft_prvdd_ldgr_id,
1912                 p_effective_start_date    => l_effective_start_date,
1913                 p_effective_end_date      => l_effective_end_date,
1914                 p_object_version_number   => l_object_version_number,
1915                 p_effective_date          => l_effective_date,
1916                 p_datetrack_mode          => l_datetrack_mode,
1917                 p_business_group_id       => p_business_group_id
1918                );
1919             end if;
1920            close c_bnft_prvdd_ldgr;
1921 	   end if;
1922 	 end if;
1923 	 close c_acty_base_rt1;	 end if;
1924 	 close c_get_credit_ledger_epe;
1925       end if;
1926     end if;
1927     l_prtt_enrt_rslt_id := null;
1928     l_acty_base_rt_id := null;
1929     l_bnft_prvdd_ldgr_id := null;
1930     l_prtt_ro_of_unusd_amt_flag := null;
1931     l_object_version_number := null;
1932     l_bpl_esd := null;
1933     l_datetrack_mode := null;
1934     l_effective_date := null;
1935     hr_utility.set_location(l_proc, 70);
1936   end loop;
1937   --
1938 -- bug # 1708166
1939   --
1940     open c_rslt;
1941     fetch c_rslt into l_rslt;
1942     close c_rslt;
1943     hr_utility.set_location('Thru dt ' ||l_rslt.enrt_cvg_thru_dt, 70);
1944     hr_utility.set_location('stat Cd '||l_rslt.prtt_enrt_rslt_stat_cd, 70);
1945     hr_utility.set_location('sspndd  '||l_rslt.sspndd_flag, 70);
1946     if l_rslt.enrt_cvg_thru_dt <>hr_api.g_eot or
1947         l_rslt.prtt_enrt_rslt_stat_cd in ('VOIDD','BCKDT') or
1948         nvl(l_rslt.sspndd_flag,'N') = 'Y' then -- Bug 5185351
1949        l_purge := 'Y';
1950     end if;
1951     --
1952     --
1953     /*Bug 13010726 */
1954    /*if(l_rslt.enrt_cvg_thru_dt <> hr_api.g_eot) then
1955 
1956             open c_flx_credit_plan_deenrt;
1957 	    fetch c_flx_credit_plan_deenrt into l_prtt_enrt_rslt_id,l_per_in_ler_id_1;
1958 	    close c_flx_credit_plan_deenrt;
1959 	    hr_utility.set_location('p_prtt_enrt_rslt_id  '||p_prtt_enrt_rslt_id, 70);
1960 	    hr_utility.set_location('l_prtt_enrt_rslt_id  '||l_prtt_enrt_rslt_id, 70);
1961 	    hr_utility.set_location('l_per_in_ler_id_1  '||l_per_in_ler_id_1, 70);
1962    else*/
1963 	    open c_flx_credit_plan;
1964 	    fetch c_flx_credit_plan into l_prtt_enrt_rslt_id;
1965 	    close c_flx_credit_plan;
1966   --end if;
1967   /*Bug 13010726*/
1968     --
1969     if l_purge = 'Y' then
1970       if l_prtt_enrt_rslt_id is not null then
1971         open c_acty_base_rt;
1972         loop
1973           fetch c_acty_base_rt into l_acty_base_rt_id;
1974           exit when c_acty_base_rt%notfound;
1975           open c_bnft_prvdd_ldgr(l_per_in_ler_id_1);
1976           fetch c_bnft_prvdd_ldgr into l_bnft_prvdd_ldgr_id,
1977                                        l_prtt_ro_of_unusd_amt_flag,
1978                                        l_object_version_number,
1979                                        l_bpl_esd;
1980           hr_utility.set_location('ldgr id is'||l_bnft_prvdd_ldgr_id, 51);
1981           if c_bnft_prvdd_ldgr%found then
1982             --
1983             -- Delete only if there are no current enrollment in
1984             -- this comp object for the person.  For an FSA plan, when
1985             -- a person rolls over excess credits, it is a comp object
1986             -- change as the benefit amount changes.  The original enrollment
1987             -- is voided and a new enrollment result is created.  In
1988             -- this case, we do not want to delete the ledger as the
1989             -- person is still enrolled in the fsa plan. Bug 2119974.
1990             --
1991 	    /* Bug 13010726 : Added if..else condition*/
1992             /*if(l_rslt.enrt_cvg_thru_dt <> hr_api.g_eot) then
1993                     l_delete_bpl := true;
1994             else*/
1995 		    if l_prtt_ro_of_unusd_amt_flag = 'Y' then
1996 		      open c_person_enrolled;
1997 		      fetch c_person_enrolled into l_exists;
1998 		      if c_person_enrolled%found then
1999 			hr_utility.set_location('person_enrolled ', 51);
2000 			l_delete_bpl := false;
2001 		      else
2002 			l_delete_bpl := true;
2003 		      end if;
2004 		      close c_person_enrolled;
2005 		    end if;
2006            --end if;
2007             --
2008             if l_delete_bpl then
2009               --
2010               -- Bug 5500864
2011               -- We dont want to purge the BPL entries. This prevents the reinstatement in case
2012               -- the life event that deletes these entries is voided subsequently
2013               -- Following call is delete of USED_VAL BPL entries.
2014               --
2015               if p_effective_date = l_bpl_esd
2016               then
2017                     l_datetrack_mode := hr_api.g_zap;
2018 		                l_effective_date := l_bpl_esd ;
2019               else
2020                 l_datetrack_mode := hr_api.g_delete;
2021 		                l_effective_date := p_effective_date - 1 ;
2022               end if;
2023               --
2024               ben_Benefit_Prvdd_Ledger_api.delete_Benefit_Prvdd_Ledger(
2025                 p_bnft_prvdd_ldgr_id      => l_bnft_prvdd_ldgr_id,
2026                 p_effective_start_date    => l_effective_start_date,
2027                 p_effective_end_date      => l_effective_end_date,
2028                 p_object_version_number   => l_object_version_number,
2029                 p_effective_date          => l_effective_date,
2030                 p_datetrack_mode          => l_datetrack_mode,
2031                 p_business_group_id       => p_business_group_id
2032                );
2033             end if;
2034           end if;
2035           close c_bnft_prvdd_ldgr;
2036         end loop;
2037         close c_acty_base_rt;
2038        --
2039       end if;
2040     end if;
2041     -- Bug#1750825
2042 
2043     if l_prtt_enrt_rslt_id is not null then
2044         open c_choice;
2045         loop
2046         --
2047         fetch c_choice into
2048               l_epe.bnft_prvdr_pool_id,
2049               l_epe.elig_per_elctbl_chc_id,
2050               l_epe.prtt_enrt_rslt_id,
2051               l_epe.pgm_id,
2052               l_epe.ptip_id,
2053               l_epe.plip_id,
2054               l_epe.pl_id,
2055               l_epe.oipl_id,
2056               l_epe.oiplip_id,
2057               l_epe.cmbn_plip_id,
2058               l_epe.cmbn_ptip_id,
2059               l_epe.cmbn_ptip_opt_id,
2060               l_epe.business_group_id,
2061               l_epe.per_in_ler_id
2062           ;
2063         exit when c_choice%notfound;
2064         l_person_enrolled:=person_enrolled_in_choice(
2065          p_person_id                        => p_person_id,
2066          p_epe_rec                          => l_epe,
2067          p_old_result_id                    => p_prtt_enrt_rslt_id,
2068          p_effective_date                   => p_effective_date
2069           );
2070         if (not l_person_enrolled) then
2071            --
2072            open c_enrt_rt (l_epe.elig_per_elctbl_chc_id);
2073            fetch c_enrt_rt into l_enrt_rt;
2074            /*  bug#3365290
2075            if c_enrt_rt%notfound
2076                 then
2077     -- error
2078                hr_utility.set_location('BEN_91724_NO_FLX_CR_RT_FOUND', 51);
2079                fnd_message.set_name('BEN','BEN_91724_NO_FLX_CR_RT_FOUND');
2080                fnd_message.set_token('PROC',l_proc);
2081                close c_enrt_rt;
2082                fnd_message.raise_error;
2083            end if;
2084            close c_enrt_rt;
2085            if l_enrt_rt.acty_base_rt_id is null then
2086               hr_utility.set_location('BEN_91725_NO_FLX_CR_ABR_FOUND', 52);
2087               fnd_message.set_name('BEN','BEN_91725_NO_FLX_CR_ABR_FOUND');
2088               fnd_message.set_token('PROC',l_proc);
2089               fnd_message.raise_error;
2090            end if;
2091            --
2092            */
2093            close c_enrt_rt;
2094             --
2095 
2096            open c_bnft_prvdd_ldgr_2 (l_enrt_rt.acty_base_rt_id);
2097            fetch c_bnft_prvdd_ldgr_2 into l_bnft_prvdd_ldgr_id,l_object_version_number,l_bpl_esd;
2098            hr_utility.set_location('ldgr id provided is'||l_bnft_prvdd_ldgr_id, 52);
2099            if c_bnft_prvdd_ldgr_2%found then
2100               --
2101               -- Bug 5500864
2102               if p_effective_date = l_bpl_esd
2103               then
2104                 l_datetrack_mode := hr_api.g_zap;
2105 		l_effective_date := l_bpl_esd ;
2106               else
2107                 l_datetrack_mode := hr_api.g_delete;
2108 		l_effective_date := p_effective_date - 1 ;
2109               end if;
2110               --
2111               ben_Benefit_Prvdd_Ledger_api.delete_Benefit_Prvdd_Ledger(
2112                 p_bnft_prvdd_ldgr_id      => l_bnft_prvdd_ldgr_id,
2113                 p_effective_start_date    => l_effective_start_date,
2114                 p_effective_end_date      => l_effective_end_date,
2115                 p_object_version_number   => l_object_version_number,
2116                 p_effective_date          => l_effective_date,
2117                 p_datetrack_mode          => l_datetrack_mode,
2118                 p_business_group_id       => p_business_group_id
2119                );
2120            end if;
2121            close c_bnft_prvdd_ldgr_2;
2122         end if;
2123       end loop;
2124       --
2125       close c_choice; -- Bug 6834215
2126     end if;
2127   close c_ledger;
2128   --
2129 
2130   hr_utility.set_location(' Leaving:'||l_proc, 999);
2131   --
2132 end cleanup_invalid_ledger_entries;
2133 --------------------------------------------------------------------------------
2134 --                      cleanup_invalid_ledger_entries
2135 --------------------------------------------------------------------------------
2136 -- OVER LOADED PROCEDURE!!
2137 --
2138 procedure cleanup_invalid_ledger_entries(  -- so few args because uses global table
2139         p_validate             in boolean default false,
2140         p_person_id            in number,
2141         p_per_in_ler_id        in number,
2142         p_effective_date       in date,
2143         p_business_group_id    in number
2144 ) is
2145   l_proc varchar2(72) := g_package||'.cleanup_invalid_ledger_entries';
2146   l_epe ben_epe_shd.g_rec_type;
2147   l_rt_usg_cd        varchar2(30);
2148   l_bnft_prvdd_ldgr_id number;
2149   l_person_enrolled boolean;
2150   l_effective_start_date date;
2151   l_effective_end_date date;
2152   l_object_version_number number;
2153   --
2154   -- this cursor needs some explaination
2155   --   It get the set of choices which have ledger entries - (for entire person)
2156   --   The first half of the query gets the choices and the abr to join to ledger
2157   --   The second half makes sure the ledger belongs to the correct person
2158   --   This is weird because the result id on the ledger is not the one which caused
2159   --   the ledger entry to be written.  jcarpent
2160   --
2161   cursor c_ledger is
2162     select distinct
2163                 epe.bnft_prvdr_pool_id,
2164                 epe.elig_per_elctbl_chc_id,
2165                 epe.prtt_enrt_rslt_id,
2166                 epe.pgm_id,
2167                 epe.ptip_id,
2168                 epe.plip_id,
2169                 epe.pl_id,
2170                 epe.oipl_id,
2171                 epe.cmbn_ptip_id,
2172                 epe.cmbn_ptip_opt_id,
2173                 epe.business_group_id,
2174                 bpl.bnft_prvdd_ldgr_id,
2175                 bpl.object_version_number,
2176                 decode(enb.enrt_bnft_id,null,
2177                 ecr2.rt_usg_cd,ecr1.rt_usg_cd) rt_usg_cd
2178     from        ben_elig_per_elctbl_chc epe,
2179                 ben_per_in_ler pil,
2180                 ben_enrt_rt ecr1,
2181                 ben_enrt_rt ecr2,
2182                 ben_enrt_bnft enb,
2183                 ben_bnft_prvdd_ldgr_f bpl,
2184                 ben_elig_per_elctbl_chc epe_flex,
2185                 ben_per_in_ler pil_flex,
2186                 ben_per_in_ler pil_flex1
2187     where       pil.per_in_ler_id = p_per_in_ler_id and
2188                 pil.business_group_id=p_business_group_id and
2189                 epe.bnft_prvdr_pool_id is not null and
2190                 pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
2191                 pil.per_in_ler_id=epe.per_in_ler_id and
2192                 epe.business_group_id=p_business_group_id and
2193                 epe.elig_per_elctbl_chc_id=ecr2.elig_per_elctbl_chc_id(+) and
2194                 (ecr1.acty_base_rt_id=bpl.acty_base_rt_id
2195                  or ecr2.acty_base_rt_id=bpl.acty_base_rt_id) and
2196                 epe.elig_per_elctbl_chc_id=enb.elig_per_elctbl_chc_id(+) and
2197                 enb.enrt_bnft_id = ecr1.enrt_bnft_id(+) and
2198                 bpl.business_group_id=p_business_group_id and
2199                 p_effective_date between
2200                         bpl.effective_start_date and bpl.effective_end_date and
2201 --              bpl.cash_recd_val is null and
2202                 bpl.prtt_enrt_rslt_id=epe_flex.prtt_enrt_rslt_id and
2203                 epe_flex.business_group_id=p_business_group_id and
2204                 epe_flex.per_in_ler_id=pil_flex.per_in_ler_id and
2205                 pil_flex.business_group_id=p_business_group_id and
2206                 -- Bug 1634870
2207                 pil_flex1.per_in_ler_id=bpl.per_in_ler_id and
2208                 pil_flex1.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
2209                 pil_flex1.business_group_id=p_business_group_id and
2210                 pil_flex.person_id=p_person_id
2211     ;
2212 
2213 begin
2214 
2215   hr_utility.set_location('Entering:'|| l_proc, 15);
2216 
2217   open c_ledger;
2218 
2219   loop
2220     fetch c_ledger into
2221         l_epe.bnft_prvdr_pool_id,
2222         l_epe.elig_per_elctbl_chc_id,
2223         l_epe.prtt_enrt_rslt_id,
2224         l_epe.pgm_id,
2225         l_epe.ptip_id,
2226         l_epe.plip_id,
2227         l_epe.pl_id,
2228         l_epe.oipl_id,
2229         l_epe.cmbn_ptip_id,
2230         l_epe.cmbn_ptip_opt_id,
2231         l_epe.business_group_id,
2232         l_bnft_prvdd_ldgr_id,
2233         l_object_version_number,
2234         l_rt_usg_cd
2235     ;
2236     hr_utility.set_location(l_proc, 20);
2237 
2238     exit when c_ledger%notfound;
2239 
2240     hr_utility.set_location(l_proc, 30);
2241 
2242     l_person_enrolled:=person_enrolled_in_choice(
2243         p_person_id                     => p_person_id,
2244         p_epe_rec                       => l_epe,
2245         p_effective_date                => p_effective_date
2246     );
2247     hr_utility.set_location(l_proc, 40);
2248     --
2249     -- if the person is not enrolled or
2250     -- rt_usg_cd<>'FLXCR' and not directly enrolled
2251     -- then remove the ledger entry
2252     --
2253     if ((not l_person_enrolled) or
2254         (nvl(l_rt_usg_cd,hr_api.g_varchar2)<>'FLXCR' and
2255          l_epe.prtt_enrt_rslt_id is null)) then
2256       hr_utility.set_location('Deleting ledger='||to_char(l_bnft_prvdd_ldgr_id), 50);
2257       ben_Benefit_Prvdd_Ledger_api.delete_Benefit_Prvdd_Ledger(
2258         p_bnft_prvdd_ldgr_id            => l_bnft_prvdd_ldgr_id,
2259         p_effective_start_date          => l_effective_start_date,
2260         p_effective_end_date            => l_effective_end_date,
2261         p_object_version_number         => l_object_version_number,
2262         p_effective_date                => p_effective_date,
2263         p_datetrack_mode                => hr_api.g_zap,
2264         p_business_group_id             => p_business_group_id
2265       );
2266       hr_utility.set_location(l_proc, 60);
2267     end if;
2268     hr_utility.set_location(l_proc, 70);
2269   end loop;
2270   close c_ledger;
2271   --
2272   hr_utility.set_location(' Leaving:'||l_proc, 998);
2273   --
2274 end cleanup_invalid_ledger_entries;
2275 --------------------------------------------------------------------------------
2276 --                      create_flex_credit_enrolment
2277 --------------------------------------------------------------------------------
2278 procedure create_flex_credit_enrolment
2279   (p_validate           in     boolean default false
2280   ,p_person_id          in     number
2281   ,p_enrt_mthd_cd               in     varchar2
2282   ,p_business_group_id          in     number
2283   ,p_effective_date             in     date
2284   ,p_prtt_enrt_rslt_id             out nocopy number
2285   ,p_prtt_rt_val_id                out nocopy number
2286   ,p_per_in_ler_id              in     number
2287   ,p_rt_val                     in     number
2288   ,p_net_credit_val             in     number default null
2289   ,p_pgm_id                     in     number default null
2290   )
2291 is
2292   l_proc varchar2(72) := g_package||'.create_flex_credit_enrolment';
2293   l_acty_typ_cd            ben_acty_base_rt_f.acty_typ_cd%type;
2294   l_prnt_prtt_rt_val_id    ben_prtt_rt_val.prtt_rt_val_id%type;
2295   l_prnt_enrt_rt_id        ben_enrt_rt.enrt_rt_id%type;
2296   l_prnt_acty_base_rt_id   ben_acty_base_rt_f.acty_base_rt_id%type;
2297   l_child_prtt_rt_val_id   ben_prtt_rt_val.prtt_rt_val_id%type;
2298   l_child_enrt_rt_id       ben_enrt_rt.enrt_rt_id%type;
2299   l_child_acty_base_rt_id  ben_acty_base_rt_f.acty_base_rt_id%type;
2300   l_prnt_rt_val            number := 0;
2301   l_child_rt_val           number := 0;
2302   l_dummy_num              number;
2303   l_dummy_varchar2         varchar2(80);
2304   l_dummy_date             date;
2305   l_prtt_rt_val_id number;
2306   l_suspend_flag varchar2(30);
2307   l_prtt_enrt_interim_id number;
2308   l_dpnt_actn_warning boolean;
2309   l_bnf_actn_warning  boolean;
2310   l_ctfn_actn_warning boolean;
2311   l_effective_start_date date;
2312   l_effective_end_date date;
2313   l_good_prtt_rt_val_id number;
2314   l_enrt_rt_id number;
2315 
2316   cursor c_flex_credit_choice is
2317             select      elig_per_elctbl_chc_id,
2318                         epe.prtt_enrt_rslt_id,
2319                         per.object_version_number,
2320                         pil.per_in_ler_id,
2321                         epe.pgm_id,
2322                         epe.pl_id,
2323                         per.enrt_cvg_strt_dt,
2324                         per.per_in_ler_id chc_pen_id
2325             from        ben_elig_per_elctbl_chc epe,
2326                         ben_per_in_ler pil,
2327                         ben_prtt_enrt_rslt_f per
2328             where       epe.comp_lvl_cd = 'PLANFC' and
2329                         epe.per_in_ler_id = p_per_in_ler_id and
2330                         per.prtt_enrt_rslt_stat_cd is null and
2331                         epe.business_group_id=p_business_group_id and
2332                         epe.per_in_ler_id=pil.per_in_ler_id and
2333                         pil.business_group_id=p_business_group_id and
2334                         pil.person_id=p_person_id and
2335                         pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
2336                         per.prtt_enrt_rslt_id(+)=epe.prtt_enrt_rslt_id and
2337                         (p_pgm_id is null or p_pgm_id = epe.pgm_id) and
2338                         p_effective_date between per.effective_start_date(+) and
2339                                         per.effective_end_date(+) ;
2340   l_flex_credit_choice c_flex_credit_choice%rowtype;
2341   --
2342   cursor c_enrt_rt_flex is
2343     select       ecr.prtt_rt_val_id,
2344                  ecr.enrt_rt_id
2345     from         ben_enrt_rt ecr,
2346                  ben_acty_base_rt_f abr
2347     where        ecr.elig_per_elctbl_chc_id=
2348                            l_flex_credit_choice.elig_per_elctbl_chc_id
2349     and          ecr.business_group_id=p_business_group_id
2350     and          ecr.acty_typ_cd not in ('NCRDSTR','NCRUDED')
2351     and          abr.parnt_acty_base_rt_id is null
2352     and          ecr.acty_base_rt_id = abr.acty_base_rt_id
2353     and          ecr.business_group_id = abr.business_group_id
2354     and          p_effective_date between abr.effective_start_date
2355                  and abr.effective_end_date;
2356   --
2357   cursor c_net_credits_rate(p_acty_typ_cd in varchar2) is
2358     select  ecr.prtt_rt_val_id
2359            ,ecr.enrt_rt_id
2360            ,ecr.acty_base_rt_id
2361     from  ben_enrt_rt ecr
2362     where ecr.elig_per_elctbl_chc_id=
2363             l_flex_credit_choice.elig_per_elctbl_chc_id
2364     and   ecr.acty_typ_cd = p_acty_typ_cd
2365     and   ecr.business_group_id=p_business_group_id;
2366   --
2367   cursor c_net_credits_child(p_acty_base_rt_id in number) is
2368     select ecr.prtt_rt_val_id
2369           ,ecr.enrt_rt_id
2370           ,ecr.acty_base_rt_id
2371     from  ben_enrt_rt ecr
2372          ,ben_acty_base_rt_f abr
2373     where ecr.elig_per_elctbl_chc_id=
2374             l_flex_credit_choice.elig_per_elctbl_chc_id
2375     and   ecr.acty_base_rt_id = abr.acty_base_rt_id
2376     and   abr.parnt_acty_base_rt_id = p_acty_base_rt_id
2377     and   ecr.business_group_id=p_business_group_id
2378     and   abr.business_group_id = ecr.business_group_id
2379     and   p_effective_date between
2380           abr.effective_start_date and abr.effective_end_date;
2381   -- bug 2988218
2382   cursor c_pgm_type is
2383     select pgm_typ_cd
2384     from ben_pgm_f
2385     where pgm_id = p_pgm_id
2386     and business_group_id = p_business_group_id
2387     and p_effective_date between
2388         effective_start_date and effective_end_date;
2389 
2390     l_pgm_type  varchar2(30);
2391   -- end 2988218
2392   l_net_credits_rate       c_net_credits_rate%rowtype;
2393   l_net_credits_child      c_net_credits_child%rowtype;
2394   l_acty_typ_cd2          varchar2(300);
2395   --
2396 begin
2397 
2398   hr_utility.set_location('Entering:'|| l_proc, 10);
2399 
2400   -- 2988218 chk if its a flex pgm
2401   open c_pgm_type;
2402   fetch c_pgm_type into l_pgm_type;
2403   close c_pgm_type;
2404 
2405   if (l_pgm_type in ( 'FLEX', 'FPC', 'COBRAFLX') ) then
2406   -- end 2988218
2407 
2408     -- find the choice to use
2409     open c_flex_credit_choice;
2410     fetch c_flex_credit_choice into l_flex_credit_choice;
2411     --
2412     if (c_flex_credit_choice%notfound and
2413         nvl(p_rt_val,hr_api.g_number) <> hr_api.g_number ) then  --Bug 3864152
2414         hr_utility.set_location('BEN_91726_NO_FLX_CR_CHOICE', 20);
2415         fnd_message.set_name('BEN','BEN_91726_NO_FLX_CR_CHOICE');
2416         fnd_message.set_token('PROC',l_proc);
2417         fnd_message.set_token('PERSON_ID', to_char(p_person_id));
2418         fnd_message.set_token('PER_IN_LER_ID',to_char(p_per_in_ler_id));
2419         fnd_message.set_token('EFFECTIVE_DATE',to_char(p_effective_date));
2420         fnd_message.set_token('BG_ID',to_char(p_business_group_id));
2421         close c_flex_credit_choice;
2422         fnd_message.raise_error;
2423     else
2424     --
2425     if l_flex_credit_choice.enrt_cvg_strt_dt > p_effective_date
2426      and l_flex_credit_choice.chc_pen_id <> p_per_in_ler_id then
2427       -- Choice created by prevoius LE, coverage starting in future,
2428       -- hav to back it out and create new one. Bug 4964766
2429       l_flex_credit_choice.prtt_enrt_rslt_id := null;
2430       --
2431     end if;
2432     --
2433       p_prtt_enrt_rslt_id := l_flex_credit_choice.prtt_enrt_rslt_id;
2434     --
2435     end if;
2436     close c_flex_credit_choice;
2437     --
2438   end if;  -- pgm_type is flx or fpc
2439   -- if a rt_val is passed in then a prv will be created also
2440   if p_rt_val is not null then
2441      open c_enrt_rt_flex;
2442      fetch c_enrt_rt_flex into
2443         l_good_prtt_rt_val_id,
2444         l_enrt_rt_id
2445       ;
2446      close c_enrt_rt_flex;
2447   end if;
2448     --
2449   if p_net_credit_val is not null then
2450     --
2451     if p_net_credit_val > 0 then
2452       --
2453       --  Get the enrollment rate with an activity type code of net credit
2454       --  distribution(NCRDSTR) and associated child rate.
2455       --
2456       l_acty_typ_cd := 'NCRDSTR';
2457       l_acty_typ_cd2 := 'NCRUDED';
2458     else
2459       l_acty_typ_cd := 'NCRUDED';
2460       l_acty_typ_cd2 := 'NCRDSTR';
2461     end if;
2462     hr_utility.set_location('l_acty_typ_cd:'|| l_acty_typ_cd, 10);
2463     --
2464     open c_net_credits_rate(l_acty_typ_cd);
2465     fetch c_net_credits_rate into l_prnt_prtt_rt_val_id
2466                                  ,l_prnt_enrt_rt_id
2467                                  ,l_prnt_acty_base_rt_id;
2468     if c_net_credits_rate%notfound then
2469       hr_utility.set_location('not found:'|| l_acty_typ_cd, 10);
2470       close c_net_credits_rate;
2471       fnd_message.set_name('BEN','BEN_92622_NET_CRED_RT_NOT_FND');
2472       fnd_message.raise_error;
2473     else
2474       close c_net_credits_rate;
2475     end if;
2476     --
2477     --  Child rate.
2478     --
2479     open c_net_credits_child(l_prnt_acty_base_rt_id);
2480     fetch c_net_credits_child into l_child_prtt_rt_val_id
2481                                   ,l_child_enrt_rt_id
2482                                   ,l_child_acty_base_rt_id;
2483     --
2484     if c_net_credits_child%notfound then
2485       close c_net_credits_child;
2486       fnd_message.set_name('BEN','BEN_92623_NET_CRE_CHLD_NOT_FND');
2487       fnd_message.raise_error;
2488     else
2489       close c_net_credits_child;
2490     end if;
2491     --
2492     --bug#2736036 - to pick the other rates
2493     open c_net_credits_rate(l_acty_typ_cd2);
2494     fetch c_net_credits_rate into l_net_credits_rate;
2495     close c_net_credits_rate;
2496     --
2497     --  Child rate.
2498     --
2499     open c_net_credits_child(l_net_credits_rate.acty_base_rt_id);
2500     fetch c_net_credits_child into l_net_credits_child;
2501     close c_net_credits_child;
2502     --
2503     -- bug#2736036
2504     if p_net_credit_val <> 0 then
2505       --
2506       -- Get the calculated value for the child rate.
2507       --
2508       l_prnt_rt_val := abs(p_net_credit_val);
2509       --
2510       hr_utility.set_location('l_prnt_rt_val:'|| l_prnt_rt_val, 10);
2511       hr_utility.set_location('l_child_rt_val:'|| l_child_rt_val, 10);
2512       ben_determine_activity_base_rt.main
2513         (p_person_id                   => p_person_id
2514         ,p_elig_per_elctbl_chc_id      => l_flex_credit_choice.elig_per_elctbl_chc_id
2515         ,p_acty_base_rt_id             => l_child_acty_base_rt_id
2516         ,p_effective_date              => p_effective_date
2517         ,p_per_in_ler_id               => p_per_in_ler_id
2518         ,p_calc_only_rt_val_flag       => true
2519         ,p_pgm_id                      => l_flex_credit_choice.pgm_id
2520         ,p_pl_id                       => l_flex_credit_choice.pl_id
2521         ,p_business_group_id           => p_business_group_id
2522         ,p_cal_val                     => l_prnt_rt_val
2523         ,p_val                         => l_child_rt_val
2524         ,p_mn_elcn_val                 => l_dummy_num
2525         ,p_mx_elcn_val                 => l_dummy_num
2526         ,p_ann_val                     => l_dummy_num
2527         ,p_ann_mn_elcn_val             => l_dummy_num
2528         ,p_ann_mx_elcn_val             => l_dummy_num
2529         ,p_cmcd_val                    => l_dummy_num
2530         ,p_cmcd_mn_elcn_val            => l_dummy_num
2531         ,p_cmcd_mx_elcn_val            => l_dummy_num
2532         ,p_cmcd_acty_ref_perd_cd       => l_dummy_varchar2
2533         ,p_incrmt_elcn_val             => l_dummy_num
2534         ,p_dflt_val                    => l_dummy_num
2535         ,p_tx_typ_cd                   => l_dummy_varchar2
2536         ,p_acty_typ_cd                 => l_dummy_varchar2
2537         ,p_nnmntry_uom                 => l_dummy_varchar2
2538         ,p_entr_val_at_enrt_flag       => l_dummy_varchar2
2539         ,p_dsply_on_enrt_flag          => l_dummy_varchar2
2540         ,p_use_to_calc_net_flx_cr_flag => l_dummy_varchar2
2541         ,p_rt_usg_cd                   => l_dummy_varchar2
2542         ,p_bnft_prvdr_pool_id          => l_dummy_num
2543         ,p_actl_prem_id                => l_dummy_num
2544         ,p_cvg_calc_amt_mthd_id        => l_dummy_num
2545         ,p_bnft_rt_typ_cd              => l_dummy_varchar2
2546         ,p_rt_typ_cd                   => l_dummy_varchar2
2547         ,p_rt_mlt_cd                   => l_dummy_varchar2
2548         ,p_comp_lvl_fctr_id            => l_dummy_num
2549         ,p_entr_ann_val_flag           => l_dummy_varchar2
2550         ,p_ptd_comp_lvl_fctr_id        => l_dummy_num
2551         ,p_clm_comp_lvl_fctr_id        => l_dummy_num
2552         ,p_ann_dflt_val                => l_dummy_num
2553         ,p_rt_strt_dt                  => l_dummy_date
2554         ,p_rt_strt_dt_cd               => l_dummy_varchar2
2555         ,p_rt_strt_dt_rl               => l_dummy_num
2556         ,p_prtt_rt_val_id              => l_dummy_num
2557         ,p_dsply_mn_elcn_val           => l_dummy_num
2558         ,p_dsply_mx_elcn_val           => l_dummy_num
2559         ,p_pp_in_yr_used_num           => l_dummy_num
2560         ,p_ordr_num                    => l_dummy_num
2561         ,p_iss_val                     => l_dummy_num
2562         );
2563         hr_utility.set_location(l_proc, 70);
2564         hr_utility.set_location('l_child_rt_val:'|| l_child_rt_val, 10);
2565     end if;
2566   end if;
2567   --
2568   -- call election_information api to create prtt enrt result
2569   --
2570   -- do if
2571   -- new record
2572   --   rt_val<>0 or rt_val is null
2573   -- change
2574   --   rt_val specified
2575   --
2576   if (l_flex_credit_choice.prtt_enrt_rslt_id is null and
2577       (p_rt_val<>0 or
2578        (p_rt_val is null and
2579         p_net_credit_val is null))) then
2580   begin
2581     hr_utility.set_location(l_proc, 70);
2582     if l_flex_credit_choice.elig_per_elctbl_chc_id is not null then --Bug 2645624
2583       ben_election_information.election_information(
2584         p_elig_per_elctbl_chc_id   => l_flex_credit_choice.elig_per_elctbl_chc_id,
2585         p_prtt_enrt_rslt_id        => l_flex_credit_choice.prtt_enrt_rslt_id,
2586         p_effective_date           => p_effective_date,
2587         p_effective_start_date     => l_effective_start_date,
2588         p_effective_end_date       => l_effective_end_date,
2589         p_enrt_mthd_cd             => p_enrt_mthd_cd,
2590         p_enrt_bnft_id             => null,
2591         p_bnft_val                 => null,
2592         p_datetrack_mode           => hr_api.g_insert,
2593         p_suspend_flag             => l_suspend_flag,
2594         p_object_version_number    => l_flex_credit_choice.object_version_number,
2595         p_prtt_enrt_interim_id     => l_prtt_enrt_interim_id,
2596         p_rt_val1                  => p_rt_val,
2597         p_enrt_rt_id1              => l_enrt_rt_id,
2598         p_prtt_rt_val_id1          => l_good_prtt_rt_val_id,
2599         p_prtt_rt_val_id2          => l_prtt_rt_val_id,
2600         p_prtt_rt_val_id3          => l_prtt_rt_val_id,
2601         p_prtt_rt_val_id4          => l_prtt_rt_val_id,
2602         p_prtt_rt_val_id5          => l_prtt_rt_val_id,
2603         p_prtt_rt_val_id6          => l_prtt_rt_val_id,
2604         p_prtt_rt_val_id7          => l_prtt_rt_val_id,
2605         p_prtt_rt_val_id8          => l_prtt_rt_val_id,
2606         p_prtt_rt_val_id9          => l_prtt_rt_val_id,
2607         p_prtt_rt_val_id10         => l_prtt_rt_val_id,
2608         p_business_group_id        => p_business_group_id,
2609         p_dpnt_actn_warning        => l_dpnt_actn_warning,
2610         p_bnf_actn_warning         => l_bnf_actn_warning,
2611         p_ctfn_actn_warning        => l_ctfn_actn_warning
2612       );
2613     p_prtt_enrt_rslt_id:=l_flex_credit_choice.prtt_enrt_rslt_id;
2614     end if;
2615   end;
2616   elsif (l_flex_credit_choice.prtt_enrt_rslt_id is not null and
2617          (p_rt_val is not null
2618        or p_net_credit_val is not null)) then
2619     hr_utility.set_location(l_proc, 80);
2620     if p_rt_val is not null and l_flex_credit_choice.elig_per_elctbl_chc_id is not null then --Bug 2645624
2621       ben_election_information.election_information(
2622         p_elig_per_elctbl_chc_id   => l_flex_credit_choice.elig_per_elctbl_chc_id,
2623         p_prtt_enrt_rslt_id        => l_flex_credit_choice.prtt_enrt_rslt_id,
2624         p_effective_date           => p_effective_date,
2625         p_effective_start_date     => l_effective_start_date,
2626         p_effective_end_date       => l_effective_end_date,
2627         p_enrt_mthd_cd             => p_enrt_mthd_cd,
2628         p_enrt_bnft_id             => null,
2629         p_bnft_val                 => null,
2630         p_datetrack_mode           => hr_api.g_correction,
2631         p_suspend_flag             => l_suspend_flag,
2632         p_object_version_number    => l_flex_credit_choice.object_version_number,
2633         p_prtt_enrt_interim_id     => l_prtt_enrt_interim_id,
2634         p_rt_val1                  => p_rt_val,
2635         p_enrt_rt_id1              => l_enrt_rt_id,
2636         p_prtt_rt_val_id1          => l_good_prtt_rt_val_id,
2637         p_prtt_rt_val_id2          => l_prtt_rt_val_id,
2638         p_prtt_rt_val_id3          => l_prtt_rt_val_id,
2639         p_prtt_rt_val_id4          => l_prtt_rt_val_id,
2640         p_prtt_rt_val_id5          => l_prtt_rt_val_id,
2641         p_prtt_rt_val_id6          => l_prtt_rt_val_id,
2642         p_prtt_rt_val_id7          => l_prtt_rt_val_id,
2643         p_prtt_rt_val_id8          => l_prtt_rt_val_id,
2644         p_prtt_rt_val_id9          => l_prtt_rt_val_id,
2645         p_prtt_rt_val_id10         => l_prtt_rt_val_id,
2646         p_business_group_id        => p_business_group_id,
2647         p_dpnt_actn_warning        => l_dpnt_actn_warning,
2648         p_bnf_actn_warning         => l_bnf_actn_warning,
2649         p_ctfn_actn_warning        => l_ctfn_actn_warning
2650         );
2651     end if;
2652     --
2653     if (p_net_credit_val is not null and
2654          (p_net_credit_val <> 0 or
2655           (p_net_credit_val = 0
2656            and l_prnt_prtt_rt_val_id is not null))) then
2657       --
2658       if l_flex_credit_choice.elig_per_elctbl_chc_id is not null then --Bug 2645624
2659         ben_election_information.election_information(
2660           p_elig_per_elctbl_chc_id   => l_flex_credit_choice.elig_per_elctbl_chc_id,
2661           p_prtt_enrt_rslt_id        => l_flex_credit_choice.prtt_enrt_rslt_id,
2662           p_effective_date           => p_effective_date,
2663           p_effective_start_date     => l_effective_start_date,
2664           p_effective_end_date       => l_effective_end_date,
2665           p_enrt_mthd_cd             => p_enrt_mthd_cd,
2666           p_enrt_bnft_id             => null,
2667           p_bnft_val                 => null,
2668           p_datetrack_mode           => hr_api.g_correction,
2669           p_suspend_flag             => l_suspend_flag,
2670           p_object_version_number    => l_flex_credit_choice.object_version_number,
2671           p_prtt_enrt_interim_id     => l_prtt_enrt_interim_id,
2672           p_rt_val1                  => l_prnt_rt_val,
2673           p_enrt_rt_id1              => l_prnt_enrt_rt_id,
2674           p_prtt_rt_val_id1          => l_prnt_prtt_rt_val_id,
2675           p_rt_val2                  => l_child_rt_val,
2676           p_enrt_rt_id2              => l_child_enrt_rt_id,
2677           p_prtt_rt_val_id2          => l_child_prtt_rt_val_id,
2678           p_rt_val3                  => 0,
2679           p_enrt_rt_id3              => l_net_credits_rate.enrt_rt_id,
2680           p_prtt_rt_val_id3          => l_net_credits_rate.prtt_rt_val_id,
2681           p_rt_val4                  => 0,
2682           p_enrt_rt_id4              => l_net_credits_child.enrt_rt_id,
2683           p_prtt_rt_val_id4          => l_net_credits_child.prtt_rt_val_id,
2684           p_prtt_rt_val_id5          => l_prtt_rt_val_id,
2685           p_prtt_rt_val_id6          => l_prtt_rt_val_id,
2686           p_prtt_rt_val_id7          => l_prtt_rt_val_id,
2687           p_prtt_rt_val_id8          => l_prtt_rt_val_id,
2688           p_prtt_rt_val_id9          => l_prtt_rt_val_id,
2689           p_prtt_rt_val_id10         => l_prtt_rt_val_id,
2690           p_business_group_id        => p_business_group_id,
2691           p_dpnt_actn_warning        => l_dpnt_actn_warning,
2692           p_bnf_actn_warning         => l_bnf_actn_warning,
2693           p_ctfn_actn_warning        => l_ctfn_actn_warning
2694           );
2695         p_prtt_enrt_rslt_id:=l_flex_credit_choice.prtt_enrt_rslt_id;
2696       end if;
2697     end if;
2698    end if;
2699     -- store the created id in the global
2700     g_credit_pool_result_id:=l_flex_credit_choice.prtt_enrt_rslt_id;
2701     g_credit_pool_person_id:=p_person_id;
2702 
2703   hr_utility.set_location(' Leaving:'||l_proc, 999);
2704 
2705 end create_flex_credit_enrolment;
2706 --
2707 --------------------------------------------------------------------------------
2708 --                      remove_bnft_prvdd_ldgr
2709 --------------------------------------------------------------------------------
2710 procedure remove_bnft_prvdd_ldgr(
2711         p_prtt_enrt_rslt_id        in number,
2712         p_effective_date           in date,
2713         p_business_group_id        in number,
2714         p_validate                 in boolean,
2715         p_datetrack_mode           in varchar2
2716 ) is
2717 
2718   l_proc varchar2(72) := g_package||'.remove_bnft_prvdd_ldgr';
2719   l_person_id number;
2720 
2721   cursor c_result is
2722     select person_id
2723     from   ben_prtt_enrt_rslt_f res
2724     where  res.prtt_enrt_rslt_id=p_prtt_enrt_rslt_id and
2725            --res.prtt_enrt_rslt_stat_cd is null and
2726            p_effective_date between
2727              res.effective_start_date and res.effective_end_date and
2728            p_business_group_id=res.business_group_id ;
2729 
2730 begin
2731   --
2732   hr_utility.set_location('Entering:'|| l_proc, 10);
2733   --
2734   --  get the flex result from the ledger row(s)
2735   --  so don't have to do it first.
2736   --
2737   --  remove all debit ledger entries for this result
2738   --  remove all credit ledger entries for this result where
2739   --  the comp object is the last one which justifies the credit.
2740   --
2741   open c_result;
2742   fetch c_result into
2743         l_person_id
2744   ;
2745   close c_result;
2746 
2747   cleanup_invalid_ledger_entries(
2748         p_person_id                        => l_person_id,
2749         p_prtt_enrt_rslt_id                => p_prtt_enrt_rslt_id,
2750         p_effective_date                   => p_effective_date,
2751         p_business_group_id                => p_business_group_id
2752 
2753   );
2754   --
2755   --  Don't update the flex credit row if necessary done in total_pools
2756   --
2757   hr_utility.set_location('Leaving:'||l_proc, 999);
2758   --
2759 end remove_bnft_prvdd_ldgr;
2760 --------------------------------------------------------------------------------
2761 --                      forfeit_credits
2762 --------------------------------------------------------------------------------
2763 procedure forfeit_credits
2764   (p_calculate_only_mode in     boolean default false
2765   ,p_validate            in     boolean default false
2766   ,p_prtt_enrt_rslt_id   in     number
2767   ,p_bnft_prvdr_pool_id  in     number
2768   ,p_acty_base_rt_id     in     number
2769   ,p_person_id           in     number
2770   ,p_enrt_mthd_cd        in     varchar2
2771   ,p_per_in_ler_id       in     number
2772   ,p_prvdd_val           in     number
2773   ,p_rlld_up_val         in     number
2774   ,p_used_val            in     number
2775   ,p_rollover_val        in     number
2776   ,p_cash_val            in     number
2777   ,p_effective_date      in     date
2778   ,p_business_group_id   in     number
2779   ,p_frftd_val              out nocopy number
2780   )
2781 is
2782   l_proc varchar2(72) := g_package||'.forfeit_credits';
2783   l_balance number;
2784   l_bnft_prvdd_ldgr_id number;
2785   l_effective_start_date date;
2786   l_effective_end_date date;
2787   l_object_version_number number;
2788   l_datetrack_mode varchar2(30);
2789   l_frftd_val number;
2790   l_cash_recd_val number;
2791   l_balance_for_cr number;
2792   --
2793   cursor c_forfeit is
2794     select bpl.bnft_prvdd_ldgr_id,
2795            bpl.frftd_val,
2796            bpl.object_version_number,
2797            bpl.effective_start_date
2798     from   ben_bnft_prvdd_ldgr_f bpl,
2799            ben_per_in_ler pil
2800     where  bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id
2801       and  bpl.business_group_id=p_business_group_id
2802       and  bpl.acty_base_rt_id = p_acty_base_rt_id
2803       and  bpl.prtt_enrt_rslt_id=p_prtt_enrt_rslt_id
2804       and  bpl.frftd_val is not null
2805       and  p_effective_date between
2806              bpl.effective_start_date and bpl.effective_end_date
2807       and pil.per_in_ler_id=bpl.per_in_ler_id
2808       and pil.business_group_id=bpl.business_group_id
2809       and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT');
2810   --
2811   -- Bug 2645993
2812   --
2813   cursor c_cash_rcvd is
2814     select bpl.bnft_prvdd_ldgr_id,
2815            bpl.cash_recd_val,
2816            bpl.object_version_number,
2817            bpl.effective_start_date
2818     from   ben_bnft_prvdd_ldgr_f bpl,
2819            ben_per_in_ler pil
2820     where  bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id
2821       and  bpl.business_group_id=p_business_group_id
2822       -- and  bpl.acty_base_rt_id = p_acty_base_rt_id      /* Bug No 4714939 */
2823       and  bpl.prtt_enrt_rslt_id=p_prtt_enrt_rslt_id
2824       and  bpl.cash_recd_val is not null
2825       and  p_effective_date between
2826              bpl.effective_start_date and bpl.effective_end_date
2827       and pil.per_in_ler_id=bpl.per_in_ler_id
2828       and pil.business_group_id=bpl.business_group_id
2829       and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT') ;
2830   --
2831 begin
2832   hr_utility.set_location('Entering:'|| l_proc, 10);
2833   l_balance:=nvl(p_prvdd_val,0)
2834         +nvl(p_rlld_up_val,0)
2835         -nvl(p_used_val,0)
2836         -nvl(p_rollover_val,0)
2837         -nvl(p_cash_val,0);
2838   --
2839   l_balance_for_cr := l_balance ;
2840   --
2841   hr_utility.set_location('balance='||to_char(l_balance),15);
2842   open c_forfeit;
2843   fetch c_forfeit into
2844         l_bnft_prvdd_ldgr_id,
2845         l_frftd_val,
2846         l_object_version_number,
2847         l_effective_start_date;
2848   if c_forfeit%found then
2849     if l_balance<0 then
2850       hr_utility.set_location(l_proc, 16);
2851       l_balance:=0;
2852     end if;
2853   end if;
2854   --
2855   p_frftd_val:=l_balance;
2856   --
2857   hr_utility.set_location('frftd_val='||to_char(l_frftd_val),17);
2858   hr_utility.set_location('l_balance='||to_char(l_balance),17);
2859   --
2860   if l_frftd_val<>l_balance then
2861     hr_utility.set_location(l_proc, 18);
2862     --
2863     -- update ledger row
2864     --
2865     /*
2866     if l_effective_start_date=p_effective_date then
2867       l_datetrack_mode:=hr_api.g_correction;
2868     else
2869       l_datetrack_mode:=hr_api.g_update;
2870     end if;
2871     */
2872     Get_DT_Upd_Mode
2873        (p_effective_date        => p_effective_date,
2874         p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
2875         p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
2876         p_base_key_value        => l_bnft_prvdd_ldgr_id,
2877         p_mode                  => l_datetrack_mode);
2878     hr_utility.set_location('UPDATING LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),20);
2879     ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
2880                    p_bnft_prvdd_ldgr_id           => l_bnft_prvdd_ldgr_id
2881                   ,p_effective_start_date         => l_effective_start_date
2882                   ,p_effective_end_date           => l_effective_end_date
2883                   ,p_prtt_ro_of_unusd_amt_flag    => 'N'
2884                   ,p_frftd_val                    => l_balance
2885                   ,p_prvdd_val                    => null
2886                   ,p_used_val                     => null
2887                   ,p_bnft_prvdr_pool_id           => p_bnft_prvdr_pool_id
2888                   ,p_acty_base_rt_id              => p_acty_base_rt_id
2889                   ,p_per_in_ler_id                => p_per_in_ler_id
2890                   ,p_prtt_enrt_rslt_id            => p_prtt_enrt_rslt_id
2891                   ,p_business_group_id            => p_business_group_id
2892                   ,p_object_version_number        => l_object_version_number
2893                   ,p_cash_recd_val                => null
2894                   ,p_effective_date               => p_effective_date
2895                   ,p_datetrack_mode               => l_datetrack_mode
2896     );
2897     hr_utility.set_location('UPDATED LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),30);
2898   else
2899     if l_balance>0 and l_frftd_val is null then
2900       --
2901       -- create a forfeit row
2902       --
2903       hr_utility.set_location('result_id='||to_char(p_prtt_enrt_rslt_id),35);
2904       ben_Benefit_Prvdd_Ledger_api.create_Benefit_Prvdd_Ledger (
2905                    p_bnft_prvdd_ldgr_id          => l_bnft_prvdd_ldgr_id
2906                   ,p_effective_start_date        => l_effective_start_date
2907                   ,p_effective_end_date          => l_effective_end_date
2908                   ,p_prtt_ro_of_unusd_amt_flag   => 'N'
2909                   ,p_frftd_val                   => l_balance
2910                   ,p_prvdd_val                   => null
2911                   ,p_used_val                    => null
2912                   ,p_bnft_prvdr_pool_id          => p_bnft_prvdr_pool_id
2913                   ,p_acty_base_rt_id             => p_acty_base_rt_id
2914                   ,p_person_id                   => p_person_id
2915                   ,p_enrt_mthd_cd                  => p_enrt_mthd_cd
2916                   ,p_per_in_ler_id               => p_per_in_ler_id
2917                   ,p_prtt_enrt_rslt_id           => p_prtt_enrt_rslt_id
2918                   ,p_business_group_id           => p_business_group_id
2919                   ,p_object_version_number       => l_object_version_number
2920                   ,p_cash_recd_val               => null
2921                   ,p_effective_date              => p_effective_date
2922       );
2923     end if ;
2924   end if;
2925     --
2926   -- Bug 2645993
2927   hr_utility.set_location('CREATED LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),40);
2928   if l_balance_for_cr < 0 then
2929       --
2930       hr_utility.set_location('More Used than Provided case '||l_balance_for_cr ,30);
2931       open c_cash_rcvd;
2932       fetch c_cash_rcvd into
2933         l_bnft_prvdd_ldgr_id,
2934         l_cash_recd_val,
2935         l_object_version_number,
2936         l_effective_start_date;
2937       --
2938       -- When Used Value is more than the forefeited value
2939       hr_utility.set_location(' l_cash_recd_val '||l_cash_recd_val,31);
2940       if l_cash_recd_val is not null then
2941         --
2942         --if l_cash_recd_val > 0 then
2943         --  l_balance := l_cash_recd_val + l_balance ;
2944         --end if;
2945         --
2946         Get_DT_Upd_Mode
2947         (p_effective_date        => p_effective_date,
2948          p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
2949          p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
2950          p_base_key_value        => l_bnft_prvdd_ldgr_id,
2951          p_mode                  => l_datetrack_mode);
2952         hr_utility.set_location('l_cash_recd_val is not null '||to_char(l_bnft_prvdd_ldgr_id),20);
2953         ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
2954                    p_bnft_prvdd_ldgr_id           => l_bnft_prvdd_ldgr_id
2955                   ,p_effective_start_date         => l_effective_start_date
2956                   ,p_effective_end_date           => l_effective_end_date
2957                   ,p_prtt_ro_of_unusd_amt_flag    => 'N'
2958                   ,p_frftd_val                    => null -- l_balance
2959                   ,p_prvdd_val                    => null
2960                   ,p_used_val                     => null
2961                   ,p_bnft_prvdr_pool_id           => p_bnft_prvdr_pool_id
2962                   ,p_acty_base_rt_id              => p_acty_base_rt_id
2963                   ,p_per_in_ler_id                => p_per_in_ler_id
2964                   ,p_prtt_enrt_rslt_id            => p_prtt_enrt_rslt_id
2965                   ,p_business_group_id            => p_business_group_id
2966                   ,p_object_version_number        => l_object_version_number
2967                   ,p_cash_recd_val                => l_balance_for_cr
2968                   ,p_effective_date               => p_effective_date
2969                   ,p_datetrack_mode               => l_datetrack_mode
2970         );
2971       --
2972       else
2973         hr_utility.set_location('CR NULL ='||to_char(p_prtt_enrt_rslt_id),35);
2974         ben_Benefit_Prvdd_Ledger_api.create_Benefit_Prvdd_Ledger (
2975                    p_bnft_prvdd_ldgr_id          => l_bnft_prvdd_ldgr_id
2976                   ,p_effective_start_date        => l_effective_start_date
2977                   ,p_effective_end_date          => l_effective_end_date
2978                   ,p_prtt_ro_of_unusd_amt_flag   => 'N'
2979                   ,p_frftd_val                   => null -- l_balance
2980                   ,p_prvdd_val                   => null
2981                   ,p_used_val                    => null
2982                   ,p_bnft_prvdr_pool_id          => p_bnft_prvdr_pool_id
2983                   ,p_acty_base_rt_id             => p_acty_base_rt_id
2984                   ,p_person_id                   => p_person_id
2985                   ,p_enrt_mthd_cd                  => p_enrt_mthd_cd
2986                   ,p_per_in_ler_id               => p_per_in_ler_id
2987                   ,p_prtt_enrt_rslt_id           => p_prtt_enrt_rslt_id
2988                   ,p_business_group_id           => p_business_group_id
2989                   ,p_object_version_number       => l_object_version_number
2990                   ,p_cash_recd_val               => l_balance_for_cr -- null
2991                   ,p_effective_date              => p_effective_date
2992         );
2993         --
2994       end if;
2995       close c_cash_rcvd;
2996       p_frftd_val := 0 ;
2997       --
2998       --Bug 9388229
2999   ----If Provided is more than used value and cash received value is still -ve,then rest it to zero.
3000   else
3001     if l_balance_for_cr >= 0
3002        ----Bug 9754353
3003       and nvl(p_prvdd_val,0) >= nvl(p_used_val,0) then
3004       --Bug 9754353
3005        hr_utility.set_location('Provided more than used case' ,30.1);
3006       open c_cash_rcvd;
3007       fetch c_cash_rcvd into
3008         l_bnft_prvdd_ldgr_id,
3009         l_cash_recd_val,
3010         l_object_version_number,
3011         l_effective_start_date;
3012       --
3013       -- When provided is more than used and cash received value is found then reset it.
3014       hr_utility.set_location(' l_cash_recd_val '||l_cash_recd_val,31.1);
3015       if l_cash_recd_val is not null and l_cash_recd_val < 0 then
3016         --
3017         Get_DT_Upd_Mode
3018         (p_effective_date        => p_effective_date,
3019          p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
3020          p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
3021          p_base_key_value        => l_bnft_prvdd_ldgr_id,
3022          p_mode                  => l_datetrack_mode);
3023         hr_utility.set_location('l_cash_recd_val is not null '||to_char(l_bnft_prvdd_ldgr_id),20.1);
3024         ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
3025                    p_bnft_prvdd_ldgr_id           => l_bnft_prvdd_ldgr_id
3026                   ,p_effective_start_date         => l_effective_start_date
3027                   ,p_effective_end_date           => l_effective_end_date
3028                   ,p_prtt_ro_of_unusd_amt_flag    => 'N'
3029                   ,p_frftd_val                    => null -- l_balance
3030                   ,p_prvdd_val                    => null
3031                   ,p_used_val                     => null
3032                   ,p_bnft_prvdr_pool_id           => p_bnft_prvdr_pool_id
3033                   ,p_acty_base_rt_id              => p_acty_base_rt_id
3034                   ,p_per_in_ler_id                => p_per_in_ler_id
3035                   ,p_prtt_enrt_rslt_id            => p_prtt_enrt_rslt_id
3036                   ,p_business_group_id            => p_business_group_id
3037                   ,p_object_version_number        => l_object_version_number
3038                   ,p_cash_recd_val                => 0
3039                   ,p_effective_date               => p_effective_date
3040                   ,p_datetrack_mode               => l_datetrack_mode
3041         );
3042 	end if;
3043       close c_cash_rcvd;
3044      end if;
3045      --Bug 9388229
3046   end if;
3047   close c_forfeit;
3048   -- p_frftd_val:=l_balance;
3049   hr_utility.set_location('Leaving:'||l_proc||' p_frftd_val '||p_frftd_val , 999);
3050 
3051 end forfeit_credits;
3052 --------------------------------------------------------------------------------
3053 --                      distribute_credits
3054 --------------------------------------------------------------------------------
3055 procedure distribute_credits
3056   (p_calculate_only_mode  in     boolean default false
3057   ,p_validate             in     boolean default false
3058   ,p_prtt_enrt_rslt_id    in     number
3059   ,p_bnft_prvdr_pool_id   in     number
3060   ,p_acty_base_rt_id      in     number
3061   ,p_per_in_ler_id        in     number
3062   ,p_dflt_excs_trtmt_cd   in     varchar2
3063   ,p_prvdd_val            in     number
3064   ,p_rlld_up_val          in     number
3065   ,p_used_val             in     number
3066   ,p_rollover_val         in     number
3067   ,p_cash_recd_total      in     number
3068   ,p_val_rndg_cd          in     varchar2
3069   ,p_val_rndg_rl          in     number
3070   ,p_pct_rndg_cd          in     varchar2
3071   ,p_pct_rndg_rl          in     number
3072   ,p_mn_dstrbl_val        in     number
3073   ,p_mn_dstrbl_pct_num    in     number
3074   ,p_mx_dstrbl_val        in     number
3075   ,p_mx_pct               in     number
3076   ,p_person_id            in     number
3077   ,p_enrt_mthd_cd         in     varchar2
3078   ,p_effective_date       in     date
3079   ,p_business_group_id    in     number
3080   ,p_process_enrt_flag    in     varchar2 default 'Y'
3081   --
3082   ,p_dstrbtd_val             out nocopy number
3083   ,p_bpl_cash_recd_val       out nocopy number
3084   ,p_bnft_prvdd_ldgr_id      out nocopy number
3085   )
3086 is
3087 
3088   l_proc varchar2(72) := g_package||'.distribute_credits';
3089   l_balance               number;
3090   l_mn_dstrbl_val         number;
3091   l_mx_dstrbl_val         number;
3092   l_cash_val              number;
3093   L_ACTY_BASE_RT_ID       number;
3094   l_bnft_prvdd_ldgr_id    number;
3095   l_prtt_rt_val_id        number;
3096   l_old_cash_val          number;
3097   l_effective_start_date  date;
3098   l_effective_end_date    date;
3099   l_datetrack_mode        varchar2(30);
3100   l_prtt_enrt_rslt_id     number;
3101   l_object_version_number number;
3102   l_pgm_id                number;
3103 
3104   cursor c_cash_abr is
3105     select      bpl.acty_base_rt_id
3106     from        ben_bnft_prvdd_ldgr_f bpl,
3107                 ben_per_in_ler pil,
3108                 ben_acty_base_rt_f abr          -- Bug 4613270
3109     where       bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id
3110         and     bpl.business_group_id=p_business_group_id
3111         and     bpl.prtt_enrt_rslt_id=g_credit_pool_result_id
3112         and     p_effective_date between
3113                   bpl.effective_start_date and bpl.effective_end_date
3114         and pil.per_in_ler_id=bpl.per_in_ler_id
3115         and pil.business_group_id=bpl.business_group_id
3116         and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
3117         /* Bug 4613270 */
3118         and bpl.acty_base_rt_id = abr.acty_base_rt_id
3119         and abr.rt_usg_cd = 'FLXCR' and p_effective_date between
3120             abr.effective_start_date and abr.effective_end_date;
3121   --
3122   -- Bug 5447507
3123   CURSOR c_cash_abr_from_epe
3124   IS
3125      SELECT ecr.acty_base_rt_id
3126        FROM ben_per_in_ler pil,
3127             ben_elig_per_elctbl_chc epe,
3128             ben_enrt_rt ecr
3129       WHERE pil.person_id = p_person_id
3130         AND pil.business_group_id = p_business_group_id
3131         AND pil.per_in_ler_stat_cd NOT IN ('VOIDD', 'BCKDT')
3132         AND epe.per_in_ler_id = pil.per_in_ler_id
3133         AND epe.business_group_id = p_business_group_id
3134         AND epe.bnft_prvdr_pool_id = p_bnft_prvdr_pool_id
3135         AND ecr.elig_per_elctbl_chc_id = epe.elig_per_elctbl_chc_id
3136         AND ecr.rt_usg_cd = 'FLXCR'
3137         AND ecr.business_group_id = p_business_group_id
3138    ORDER BY pil.per_in_ler_stat_Cd desc;
3139   -- Bug 5447507
3140   --
3141   cursor c_old_ledger
3142     (c_bnft_prvdr_pool_id in number
3143     ,c_prtt_enrt_rslt_id  in number
3144     ,c_effective_date     in date
3145     )
3146   is
3147     select bpl.bnft_prvdd_ldgr_id,
3148            bpl.cash_recd_val,
3149            bpl.object_version_number,
3150            bpl.effective_start_date
3151     from   ben_bnft_prvdd_ldgr_f bpl,
3152            ben_per_in_ler pil
3153     where  bpl.bnft_prvdr_pool_id = c_bnft_prvdr_pool_id
3154       and  bpl.prtt_enrt_rslt_id  = c_prtt_enrt_rslt_id
3155       and  bpl.cash_recd_val is not null
3156       and  c_effective_date
3157         between bpl.effective_start_date and bpl.effective_end_date
3158       and  pil.per_in_ler_id = bpl.per_in_ler_id
3159       and  pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT');
3160   --
3161   cursor c_bnft_pool is
3162     select bpp.pgm_id
3163     from   ben_bnft_prvdr_pool_f bpp
3164     where  bpp.bnft_prvdr_pool_id = p_bnft_prvdr_pool_id
3165     and    p_effective_date between bpp.effective_start_date
3166            and bpp.effective_end_date;
3167 
3168 begin
3169   hr_utility.set_location('Entering:'|| l_proc, 10);
3170   hr_utility.set_location('p_prvdd_val:'||to_char(p_prvdd_val)||
3171      'p_rlld_up_val:'||to_char(p_rlld_up_val),11);
3172   hr_utility.set_location('p_used_val:'||to_char(p_used_val),12);
3173   hr_utility.set_location('p_rollover_val:'||to_char(p_rollover_val),13);
3174   --
3175   -- balance used depends on dflt_excs_trtmt_cd
3176   --
3177   if p_dflt_excs_trtmt_cd in ('DSTRBT_ALL','DSTRBT_RLOVR_FRFT') then
3178     hr_utility.set_location(l_proc,20 );
3179     l_balance:=p_prvdd_val+p_rlld_up_val-p_used_val;
3180   elsif p_dflt_excs_trtmt_cd = 'RLOVR_DSTRBT_FRFT' then
3181     hr_utility.set_location(l_proc,30 );
3182     l_balance:=p_prvdd_val+p_rlld_up_val-p_used_val-p_rollover_val;
3183   else
3184     hr_utility.set_location(l_proc,40 );
3185     fnd_message.set_name('BEN','BEN_DFLT_TRTMT_NOT_HNDLD');
3186     fnd_message.set_token('PROC',l_proc);
3187     fnd_message.set_token('PERSON_ID',to_char(p_person_id));
3188     fnd_message.set_token('DFLT_EXCS_TRTMT_CD',p_dflt_excs_trtmt_cd);
3189     fnd_message.raise_error;
3190   end if;
3191   hr_utility.set_location(l_proc,50 );
3192   --
3193   -- balance is rounded
3194   --
3195   l_acty_base_rt_id := p_acty_base_rt_id;
3196   --
3197   hr_utility.set_location('['||p_val_rndg_cd||']',55);
3198   if p_val_rndg_cd is not null then
3199     l_balance:=benutils.do_rounding(
3200         p_rounding_cd    => p_val_rndg_cd,
3201         p_rounding_rl    => p_val_rndg_rl,
3202         p_value          => l_balance,
3203         p_effective_date => p_effective_date);
3204   end if;
3205   hr_utility.set_location(l_proc,60 );
3206   --
3207   -- if the rounded balance is less than the minimum val or pct, then nothing to do
3208   --
3209   -- do min val first
3210   --
3211   if p_mn_dstrbl_val is not null then
3212      if p_mn_dstrbl_val > l_balance then
3213        hr_utility.set_location('Leaving with zero distributed:'||l_proc,97);
3214        p_dstrbtd_val:=0;
3215        return;
3216      end if;
3217   end if;
3218   hr_utility.set_location(l_proc,80 );
3219   --
3220   -- do min pct second
3221   --
3222   if p_mn_dstrbl_pct_num is not null then
3223     hr_utility.set_location(l_proc,90 );
3224     l_mn_dstrbl_val:=(p_mn_dstrbl_pct_num/100)*(p_prvdd_val+p_rlld_up_val-p_used_val);
3225     if p_pct_rndg_cd is not null then
3226       l_mn_dstrbl_val:=benutils.do_rounding(
3227         p_rounding_cd    => p_pct_rndg_cd,
3228         p_rounding_rl    => p_pct_rndg_rl,
3229         p_value          => l_mn_dstrbl_val,
3230         p_effective_date => p_effective_date);
3231     end if;
3232     if l_mn_dstrbl_val > l_balance then
3233       hr_utility.set_location(l_proc,100 );
3234       p_dstrbtd_val:=0;
3235       return;
3236     end if;
3237     hr_utility.set_location(l_proc,110 );
3238   end if;
3239   hr_utility.set_location(l_proc,120 );
3240   --
3241   -- if the rounded balance is more than the maximum val or pct, then reduce it to the maximum
3242   --
3243   l_cash_val:=l_balance;
3244   --
3245   -- do max val next
3246   --
3247   if p_mx_dstrbl_val is not null then
3248      if p_mx_dstrbl_val < l_cash_val then
3249        hr_utility.set_location(l_proc,130 );
3250        l_cash_val:=p_mx_dstrbl_val;
3251      end if;
3252   end if;
3253   hr_utility.set_location(l_proc,140 );
3254   --
3255   -- do max pct second
3256   --
3257   if p_mx_pct is not null then
3258     hr_utility.set_location(l_proc,150 );
3259     l_mx_dstrbl_val:=(p_mx_pct/100)*(p_prvdd_val+p_rlld_up_val-p_used_val);
3260     if p_pct_rndg_cd is not null then
3261       l_mx_dstrbl_val:=benutils.do_rounding(
3262         p_rounding_cd    => p_pct_rndg_cd,
3263         p_rounding_rl    => p_pct_rndg_rl,
3264         p_value          => l_mx_dstrbl_val,
3265         p_effective_date => p_effective_date);
3266     end if;
3267     if l_mx_dstrbl_val < l_cash_val then
3268       hr_utility.set_location(l_proc,160 );
3269       l_cash_val:=l_mx_dstrbl_val;
3270     end if;
3271     hr_utility.set_location(l_proc,170 );
3272   end if;
3273   hr_utility.set_location(l_proc,180 );
3274   --
3275   -- create the cash ledger entry
3276   --
3277   p_dstrbtd_val:=l_cash_val;
3278   --
3279   if l_cash_val<>p_cash_recd_total
3280     and not p_calculate_only_mode
3281   then
3282   hr_utility.set_location(l_proc,190 );
3283   hr_utility.set_location('ACE l_cash_val = ' || l_cash_val, 9999);
3284   hr_utility.set_location('ACE p_cash_recd_total = ' || p_cash_recd_total, 9999);
3285     --
3286     -- get cash abr
3287     --
3288     open c_cash_abr;
3289     fetch c_cash_abr into l_acty_base_rt_id;
3290     if c_cash_abr%notfound
3291     then
3292       close c_cash_abr;
3293       --
3294       -- Bug 5447507
3295       -- Case : User does not enrol into a plan that provides with flex credits. So we dont have
3296       -- BPL row for PRVDD_VAL. But user enrols into a plan which has deductible rates. So we have
3297       -- BPL row with USED_VAL. So if user does not enrol into plan with flex credits or decides
3298       -- to enrol later, then it should not stop us from creating CASH_VAL (which would be negative in
3299       -- such a case). So here ACTY_BASE_RT_ID from EPE->ECR table rather than BPL table.
3300       --
3301       open c_cash_abr_from_epe;
3302         fetch c_cash_abr_from_epe into l_acty_base_rt_id;
3303         if c_cash_abr_from_epe%notfound
3304         then
3305           close c_cash_abr_from_epe;
3306           -- Bug 5447507
3307           --
3308           hr_utility.set_location(l_proc,200 );
3309           fnd_message.set_name('BEN','BEN_91724_NO_FLX_CR_RT_FOUND');
3310           fnd_message.set_token('PROC',l_proc);
3311           fnd_message.set_token('PERSON_ID',to_char(p_person_id));
3312           hr_utility.set_location('BEN_91724_NO_FLX_CR_RT_FOUND', 220);
3313           hr_utility.set_location('pool_id='||to_char(p_bnft_prvdr_pool_id), 230);
3314           fnd_message.raise_error;
3315           --
3316         end if;
3317         --
3318       close c_cash_abr_from_epe;
3319       --
3320     else
3321       hr_utility.set_location(l_proc,205 );
3322       close c_cash_abr;
3323     end if;
3324     hr_utility.set_location('ACE l_acty_base_rt_id = ' || l_acty_base_rt_id, 99);
3325     hr_utility.set_location(l_proc,210 );
3326     --
3327     hr_utility.set_location(l_proc, 220);
3328     --
3329     if p_process_enrt_flag = 'Y'
3330       and not p_calculate_only_mode
3331     then
3332        --
3333        -- check if g_credit_pool_result_id is set
3334        --
3335        if (g_credit_pool_result_id is null or
3336            g_credit_pool_person_id is null or
3337            g_credit_pool_person_id<>p_person_id) then
3338          hr_utility.set_location(l_proc, 250);
3339          --
3340          open c_bnft_pool;
3341          fetch c_bnft_pool into l_pgm_id;
3342          close c_bnft_pool;
3343          --
3344          create_flex_credit_enrolment(
3345            p_person_id           => p_person_id,
3346            p_enrt_mthd_cd        => p_enrt_mthd_cd,
3347            p_business_group_id   => p_business_group_id,
3348            p_effective_date      => p_effective_date,
3349            p_prtt_enrt_rslt_id   => l_prtt_enrt_rslt_id,
3350            p_prtt_rt_val_id      => l_prtt_rt_val_id,
3351            p_per_in_ler_id       => p_per_in_ler_id,
3352            p_rt_val              => null,
3353            p_pgm_id              => l_pgm_id
3354          );
3355          hr_utility.set_location(l_proc, 260);
3356        end if;
3357        --
3358     end if;
3359     hr_utility.set_location(l_proc,270 );
3360     --
3361     open c_old_ledger
3362       (c_bnft_prvdr_pool_id => p_bnft_prvdr_pool_id
3363       ,c_prtt_enrt_rslt_id  => g_credit_pool_result_id
3364       ,c_effective_date     => p_effective_date
3365       );
3366     fetch c_old_ledger into l_bnft_prvdd_ldgr_id,
3367                             l_old_cash_val,
3368                             l_object_version_number,
3369                             l_effective_start_date;
3370     --
3371     -- insert cash row
3372     --
3373     hr_utility.set_location(l_proc, 280);
3374     if c_old_ledger%notfound
3375       and not p_calculate_only_mode
3376     then
3377       hr_utility.set_location('insert cash row', 290);
3378       ben_Benefit_Prvdd_Ledger_api.create_Benefit_Prvdd_Ledger (
3379                    p_bnft_prvdd_ldgr_id         => l_bnft_prvdd_ldgr_id
3380                   ,p_effective_start_date       => l_effective_start_date
3381                   ,p_effective_end_date         => l_effective_end_date
3382                   ,p_prtt_ro_of_unusd_amt_flag  => 'N'
3383                   ,p_frftd_val                  => null
3384                   ,p_prvdd_val                  => null
3385                   ,p_used_val                   => null
3386                   ,p_bnft_prvdr_pool_id         => p_bnft_prvdr_pool_id
3387                   ,p_acty_base_rt_id            => l_acty_base_rt_id
3388                   ,p_person_id                  => p_person_id
3389                   ,p_enrt_mthd_cd                  => p_enrt_mthd_cd
3390                   ,p_per_in_ler_id              => p_per_in_ler_id
3391                   ,p_prtt_enrt_rslt_id          => g_credit_pool_result_id
3392                   ,p_business_group_id          => p_business_group_id
3393                   ,p_object_version_number      => l_object_version_number
3394                   ,p_cash_recd_val              => l_cash_val
3395                   ,p_effective_date             => p_effective_date
3396       );
3397       hr_utility.set_location('CREATED CASH LDGR ID='||to_char(l_bnft_prvdd_ldgr_id),300);
3398     elsif l_old_cash_val<>l_cash_val
3399       and not p_calculate_only_mode
3400     then
3401       hr_utility.set_location(l_proc, 310);
3402      /* if l_effective_start_date=p_effective_date then
3403         hr_utility.set_location(l_proc,320 );
3404         l_datetrack_mode:=hr_api.g_correction;
3405       else
3406         hr_utility.set_location(l_proc,330 );
3407         l_datetrack_mode:=hr_api.g_update;
3408       end if; */
3409       Get_DT_Upd_Mode
3410          (p_effective_date        => p_effective_date,
3411           p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
3412           p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
3413           p_base_key_value        => l_bnft_prvdd_ldgr_id,
3414           p_mode                  => l_datetrack_mode);
3415       hr_utility.set_location('UPDATING LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),340);
3416       ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
3417                    p_bnft_prvdd_ldgr_id           => l_bnft_prvdd_ldgr_id
3418                   ,p_effective_start_date         => l_effective_start_date
3419                   ,p_effective_end_date           => l_effective_end_date
3420                   ,p_prtt_ro_of_unusd_amt_flag    => 'N'
3421                   ,p_frftd_val                    => null
3422                   ,p_prvdd_val                    => null
3423                   ,p_used_val                     => null
3424                   ,p_bnft_prvdr_pool_id           => p_bnft_prvdr_pool_id
3425                   ,p_acty_base_rt_id              => l_acty_base_rt_id
3426                   ,p_per_in_ler_id                => nvl(p_per_in_ler_id, hr_api.g_number)   /* Bug 4251187 */
3427                   ,p_prtt_enrt_rslt_id            => g_credit_pool_result_id
3428                   ,p_business_group_id            => p_business_group_id
3429                   ,p_object_version_number        => l_object_version_number
3430                   ,p_cash_recd_val                => l_cash_val
3431                   ,p_effective_date               => p_effective_date
3432                   ,p_datetrack_mode               => l_datetrack_mode
3433       );
3434       hr_utility.set_location('UPDATED LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),350);
3435     end if;
3436     hr_utility.set_location(l_proc,360 );
3437     close c_old_ledger;
3438     --
3439   end if;
3440   hr_utility.set_location('Leaving:'||l_proc, 999);
3441   --
3442   -- Set out param
3443   --
3444   p_bpl_cash_recd_val  := l_cash_val;
3445   p_bnft_prvdd_ldgr_id := l_bnft_prvdd_ldgr_id;
3446   --
3447 end distribute_credits;
3448 --------------------------------------------------------------------------------
3449 --                      default_rollovers
3450 --------------------------------------------------------------------------------
3451 procedure default_rollovers
3452   (p_calculate_only_mode  in     boolean default false
3453   ,p_bnft_prvdr_pool_id   in     number
3454   ,p_person_id            in     number
3455   ,p_enrt_mthd_cd         in     varchar2
3456   ,p_effective_date       in     date
3457   ,p_datetrack_mode       in     varchar2
3458   ,p_business_group_id    in     number
3459   ,p_pct_rndg_cd          in     varchar2
3460   ,p_pct_rndg_rl          in     number
3461   ,p_dflt_excs_trtmt_cd   in     varchar2
3462   ,p_rollover_val            out nocopy number
3463   ,p_per_in_ler_id        in     number
3464   -- Bug 2185478
3465   ,p_acty_base_rt_id      in     number default null
3466   )
3467 is
3468   l_proc varchar2(72) := g_package||'.default_rollovers';
3469 
3470   l_datetrack_mode        varchar2(30);
3471   l_effective_end_date    date;
3472   l_object_version_number number;
3473   l_effective_start_date  date;
3474   l_prvdd_val           number;
3475   l_recd_val            number;
3476   l_used_val            number;
3477   l_acty_base_rt_id     number;
3478   l_balance             number;
3479   l_mn_dstrbl_pct_num   number;
3480   l_mx_dstrbl_pct_num   number;
3481   l_old_rlovr_val       number;
3482   l_cash_val            number;
3483   l_bnft_prvdd_ldgr_id  number;
3484   l_rld_ovr_val         number;
3485   l_rollover_diff_total number;
3486   l_qualify_flag        varchar2(30);
3487   l_outputs             ff_exec.outputs_t;
3488   l_jurisdiction_code   varchar2(30);
3489   l_dummy               varchar2(80);
3490   --
3491   -- Query will get the rollover information and
3492   --   the elctbl_chc, enrollment rate
3493   --   If the person is enrolled will get the result and prv.
3494   --
3495   cursor c_rollovers is
3496     select
3497         prr.mn_rlovr_pct_num,
3498         prr.mn_rlovr_val,
3499         prr.mx_rchd_dflt_ordr_num,
3500         prr.mx_rlovr_pct_num,
3501         prr.mx_rlovr_val,
3502         prr.pct_rlovr_incrmt_num,
3503         prr.pct_rndg_cd,
3504         prr.pct_rndg_rl,
3505         prr.rlovr_val_incrmt_num,
3506         prr.rlovr_val_rl,
3507         prr.val_rndg_cd,
3508         prr.val_rndg_rl,
3509         prr.acty_base_rt_id,
3510         epe.elig_per_elctbl_chc_id,
3511         ecr.enrt_rt_id,
3512         ecr.mn_elcn_val,
3513         ecr.mx_elcn_val,
3514         prv.rt_val,
3515         prv.prtt_rt_val_id,
3516         rslt.prtt_enrt_rslt_id,
3517         prr.prtt_elig_rlovr_rl,
3518         asg.assignment_id,
3519         asg.organization_id,
3520         loc.region_2,
3521         oipl.opt_id,
3522         epe.pl_id,
3523         epe.pgm_id,
3524         pil.ler_id,
3525         epe.pl_typ_id,
3526         epe.per_in_ler_id
3527     from
3528         ben_bnft_pool_rlovr_rqmt_f prr,
3529         ben_per_in_ler pil,
3530         ben_elig_per_elctbl_chc epe,
3531         ben_enrt_rt ecr,
3532         ben_prtt_rt_val prv,
3533         ben_prtt_enrt_rslt_f rslt,
3534         per_all_assignments_f asg,
3535         hr_locations_all loc,
3536         ben_oipl_f oipl
3537     where
3538         prr.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
3539         prr.business_group_id=p_business_group_id and
3540         p_effective_date between
3541                 prr.effective_start_date and prr.effective_end_date and
3542         pil.per_in_ler_id=p_per_in_ler_id and
3543         pil.business_group_id=p_business_group_id and
3544         pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3545         epe.per_in_ler_id=pil.per_in_ler_id and
3546         epe.elctbl_flag='Y' and
3547         epe.business_group_id=p_business_group_id and
3548         ecr.elig_per_elctbl_chc_id=epe.elig_per_elctbl_chc_id and
3549         -- Added for bug 2185478
3550         (p_acty_base_rt_id is null or
3551          prr.acty_base_rt_id = p_acty_base_rt_id) and
3552         ecr.acty_base_rt_id=prr.acty_base_rt_id and
3553         ecr.business_group_id=p_business_group_id and
3554         prv.prtt_rt_val_id(+)=ecr.prtt_rt_val_id and
3555         prv.business_group_id(+)=p_business_group_id and
3556         rslt.prtt_enrt_rslt_id(+)=epe.prtt_enrt_rslt_id and
3557         p_effective_date between
3558                 rslt.effective_start_date(+) and rslt.effective_end_date(+) and
3559         rslt.business_group_id(+)=p_business_group_id and
3560         asg.person_id=pil.person_id and
3561         asg.assignment_type <> 'C'and
3562         asg.primary_flag='Y' and
3563         asg.location_id = loc.location_id(+) and
3564         p_effective_date between
3565           asg.effective_start_date and asg.effective_end_date and
3566         oipl.oipl_id(+)=epe.oipl_id and
3567         p_effective_date between
3568           oipl.effective_start_date(+) and oipl.effective_end_date(+) and
3569         oipl.business_group_id(+)=p_business_group_id
3570     --Bug 2185478 added the union to handle the cases of enrt_bnft
3571     union
3572     select
3573         prr.mn_rlovr_pct_num,
3574         prr.mn_rlovr_val,
3575         prr.mx_rchd_dflt_ordr_num,
3576         prr.mx_rlovr_pct_num,
3577         prr.mx_rlovr_val,
3578         prr.pct_rlovr_incrmt_num,
3579         prr.pct_rndg_cd,
3580         prr.pct_rndg_rl,
3581         prr.rlovr_val_incrmt_num,
3582         prr.rlovr_val_rl,
3583         prr.val_rndg_cd,
3584         prr.val_rndg_rl,
3585         prr.acty_base_rt_id,
3586         epe.elig_per_elctbl_chc_id,
3587         ecr.enrt_rt_id,
3588         ecr.mn_elcn_val,
3589         ecr.mx_elcn_val,
3590         prv.rt_val,
3591         prv.prtt_rt_val_id,
3592         rslt.prtt_enrt_rslt_id,
3593         prr.prtt_elig_rlovr_rl,
3594         asg.assignment_id,
3595         asg.organization_id,
3596         loc.region_2,
3597         oipl.opt_id,
3598         epe.pl_id,
3599         epe.pgm_id,
3600         pil.ler_id,
3601         epe.pl_typ_id,
3602         epe.per_in_ler_id
3603     from
3604         ben_bnft_pool_rlovr_rqmt_f prr,
3605         ben_per_in_ler pil,
3606         ben_elig_per_elctbl_chc epe,
3607         ben_enrt_bnft enb,
3608         ben_enrt_rt ecr,
3609         ben_prtt_rt_val prv,
3610         ben_prtt_enrt_rslt_f rslt,
3611         per_all_assignments_f asg,
3612         hr_locations_all loc,
3613         ben_oipl_f oipl
3614     where
3615         prr.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
3616         prr.business_group_id=p_business_group_id and
3617         p_effective_date between
3618                 prr.effective_start_date and prr.effective_end_date and
3619         pil.per_in_ler_id=p_per_in_ler_id and
3620         pil.business_group_id=p_business_group_id and
3621         pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3622         epe.per_in_ler_id=pil.per_in_ler_id and
3623         epe.elctbl_flag='Y' and
3624         epe.business_group_id=p_business_group_id and
3625         enb.elig_per_elctbl_chc_id=epe.elig_per_elctbl_chc_id and
3626         enb.enrt_bnft_id = ecr.enrt_bnft_id  and
3627         -- Added for bug 2185478
3628         (p_acty_base_rt_id is null or
3629          prr.acty_base_rt_id = p_acty_base_rt_id) and
3630         ecr.acty_base_rt_id=prr.acty_base_rt_id and
3631         ecr.business_group_id=p_business_group_id and
3632         prv.prtt_rt_val_id(+)=ecr.prtt_rt_val_id and
3633         prv.business_group_id(+)=p_business_group_id and
3634         rslt.prtt_enrt_rslt_id(+)=epe.prtt_enrt_rslt_id and
3635         p_effective_date between
3636                 rslt.effective_start_date(+) and rslt.effective_end_date(+) and
3637         rslt.business_group_id(+)=p_business_group_id and
3638         asg.person_id=pil.person_id and
3639         asg.assignment_type <> 'C'and
3640         asg.primary_flag='Y' and
3641         asg.location_id = loc.location_id(+) and
3642         p_effective_date between
3643           asg.effective_start_date and asg.effective_end_date and
3644         oipl.oipl_id(+)=epe.oipl_id and
3645         p_effective_date between
3646           oipl.effective_start_date(+) and oipl.effective_end_date(+) and
3647         oipl.business_group_id(+)=p_business_group_id
3648     order by mx_rchd_dflt_ordr_num
3649     ;
3650 
3651   cursor c_ledger_totals is
3652     select
3653                 nvl(sum(prvdd_val),0),
3654                 nvl(sum(decode(prtt_ro_of_unusd_amt_flag,'N',used_val,0)),0),-- non rollovers
3655                 nvl(sum(cash_recd_val),0),
3656                 nvl(sum(decode(prtt_ro_of_unusd_amt_flag,'Y',used_val,0)),0) -- rollovers
3657     from        ben_bnft_prvdd_ldgr_f bpl,
3658                 ben_elig_per_elctbl_chc epe_flex,
3659                 ben_per_in_ler pil_flex,
3660                 ben_per_in_ler pil_flex1
3661     where       p_effective_date between
3662                         bpl.effective_start_date and bpl.effective_end_date and
3663                 bpl.business_group_id=p_business_group_id and
3664                 bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
3665                 bpl.prtt_enrt_rslt_id=epe_flex.prtt_enrt_rslt_id and
3666                 -- exclude the rollover for this abr
3667                 (bpl.acty_base_rt_id<>l_acty_base_rt_id or
3668                 -- but include the used amounts for this abr
3669                 prtt_ro_of_unusd_amt_flag='N') and
3670                 epe_flex.business_group_id=p_business_group_id and
3671                 epe_flex.per_in_ler_id=pil_flex.per_in_ler_id and
3672                 pil_flex.business_group_id=p_business_group_id and
3673                 pil_flex.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3674                 pil_flex.per_in_ler_id=p_per_in_ler_id and
3675                 -- Bug 1634870
3676                 pil_flex1.per_in_ler_id=bpl.per_in_ler_id and
3677                 pil_flex1.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3678                 pil_flex1.business_group_id=p_business_group_id
3679     group by    bpl.bnft_prvdr_pool_id
3680     ;
3681 
3682   cursor c_ledger_totals_this_rollover is
3683     select
3684                 nvl(sum(prvdd_val),0),
3685                 nvl(sum(decode(prtt_ro_of_unusd_amt_flag,'N',used_val,0)),0),-- non rollovers
3686                 nvl(sum(cash_recd_val),0),
3687                 nvl(sum(decode(prtt_ro_of_unusd_amt_flag,'Y',used_val,0)),0) -- rollovers
3688     from        ben_bnft_prvdd_ldgr_f bpl,
3689                 ben_elig_per_elctbl_chc epe_flex,
3690                 ben_per_in_ler pil_flex,
3691                 ben_per_in_ler pil_flex1
3692     where       p_effective_date between
3693                         bpl.effective_start_date and bpl.effective_end_date and
3694                 bpl.business_group_id=p_business_group_id and
3695                 bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
3696                 bpl.prtt_enrt_rslt_id=epe_flex.prtt_enrt_rslt_id and
3697                 bpl.acty_base_rt_id=l_acty_base_rt_id and  -- this is the difference
3698 --              bpl.prtt_ro_of_unusd_amt_flag='Y' and -- and this is the rollover
3699                 epe_flex.business_group_id=p_business_group_id and
3700                 epe_flex.per_in_ler_id=pil_flex.per_in_ler_id and
3701                 pil_flex.business_group_id=p_business_group_id and
3702                 pil_flex.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3703                 pil_flex.per_in_ler_id=p_per_in_ler_id and
3704                 -- Bug 1634870
3705                 pil_flex1.per_in_ler_id=bpl.per_in_ler_id and
3706                 pil_flex1.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3707                 pil_flex1.business_group_id=p_business_group_id
3708     group by    bpl.bnft_prvdr_pool_id
3709     ;
3710 
3711   cursor c_old_ledger is
3712     select
3713                 bnft_prvdd_ldgr_id,
3714                 bpl.used_val,
3715                 bpl.object_version_number,
3716                 bpl.effective_start_date
3717     from        ben_bnft_prvdd_ldgr_f bpl,
3718                 ben_elig_per_elctbl_chc epe_flex,
3719                 ben_per_in_ler pil_flex,
3720                 ben_per_in_ler pil_flex1
3721     where       p_effective_date between
3722                         bpl.effective_start_date and bpl.effective_end_date and
3723                 bpl.business_group_id=p_business_group_id and
3724                 bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
3725                 bpl.prtt_enrt_rslt_id=epe_flex.prtt_enrt_rslt_id and
3726                 --bpl.used_val<>0 and   -----For the Bug 7118730
3727                 bpl.prtt_ro_of_unusd_amt_flag='Y' and
3728                 epe_flex.business_group_id=p_business_group_id and
3729                 epe_flex.per_in_ler_id=pil_flex.per_in_ler_id and
3730                 pil_flex.business_group_id=p_business_group_id and
3731                 pil_flex.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3732                 -- Bug 1634870
3733                 pil_flex1.per_in_ler_id=bpl.per_in_ler_id and
3734                 pil_flex1.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
3735                 pil_flex1.business_group_id=p_business_group_id and
3736                 pil_flex.per_in_ler_id=p_per_in_ler_id ;
3737 begin
3738   hr_utility.set_location('Entering:'|| l_proc, 10);
3739   l_rollover_diff_total:=0;
3740   for l_rollover in c_rollovers loop
3741     --
3742     -- If rule exists run if and see if qualifies for rollover
3743     --
3744     if l_rollover.prtt_elig_rlovr_rl is not null then
3745       --
3746       -- execute rule
3747       --
3748   --    if l_rollover.region_2 is not null then
3749 
3750   --      l_jurisdiction_code :=
3751   --          pay_mag_utils.lookup_jurisdiction_code
3752   --           (p_state => l_rollover.region_2);
3753 
3754   --  end if;
3755 
3756       l_outputs := benutils.formula
3757             (p_formula_id           => l_rollover.prtt_elig_rlovr_rl,
3758              p_assignment_id        => l_rollover.assignment_id,
3759              p_organization_id        => l_rollover.organization_id,
3760              p_business_group_id    => p_business_group_id,
3761              p_effective_date       => p_effective_date,
3762              p_opt_id               => l_rollover.opt_id,
3763              p_pl_id                => l_rollover.pl_id,
3764              p_pgm_id               => l_rollover.pgm_id,
3765              p_ler_id               => l_rollover.ler_id,
3766              p_pl_typ_id            => l_rollover.pl_typ_id,
3767              p_acty_base_rt_id      => l_rollover.acty_base_rt_id,
3768              p_elig_per_elctbl_chc_id      => l_rollover.elig_per_elctbl_chc_id,
3769              p_jurisdiction_code    => l_jurisdiction_code);
3770 
3771       l_qualify_flag := l_outputs(l_outputs.first).value;
3772     else
3773       l_qualify_flag:='Y';
3774     end if;
3775     if l_qualify_flag='Y' then
3776       --
3777       -- Process this rollover
3778       --
3779       hr_utility.set_location(l_proc, 20);
3780       l_acty_base_rt_id:=l_rollover.acty_base_rt_id;
3781       --
3782       -- Get the ledger totals with exclusions (see cursor)
3783       --
3784       open c_ledger_totals;
3785       fetch c_ledger_totals into
3786           l_prvdd_val,
3787           l_used_val,
3788           l_recd_val,
3789           l_rld_ovr_val;
3790       close c_ledger_totals;
3791       --
3792       -- depending on the excess treatment code compute the balance
3793       --
3794       if p_dflt_excs_trtmt_cd in ('DSTRBT_ALL','DSTRBT_RLOVR_FRFT') then
3795         hr_utility.set_location(l_proc, 30);
3796         l_balance:=l_prvdd_val-l_used_val-l_recd_val-l_rld_ovr_val;
3797       else
3798         hr_utility.set_location(l_proc, 40);
3799         l_balance:=l_prvdd_val-l_used_val-l_rld_ovr_val;
3800       end if;
3801       hr_utility.set_location(l_proc, 50);
3802       --
3803       -- round the balance
3804       --
3805       if l_rollover.val_rndg_cd is not null then
3806         l_balance:=benutils.do_rounding(
3807           p_rounding_cd    => l_rollover.val_rndg_cd,
3808           p_rounding_rl    => l_rollover.val_rndg_rl,
3809           p_value          => l_balance,
3810           p_effective_date => p_effective_date);
3811       end if;
3812       --
3813       -- Bug 2185478 : Globals used for validating the rollover amounts
3814       --
3815       if p_acty_base_rt_id is not null then
3816          --
3817          g_balance      := l_balance;
3818          g_mx_rlovr_val := l_rollover.mx_rlovr_val;
3819          g_mx_elcn_val  := l_rollover.mx_elcn_val;
3820          --
3821       end if;
3822       --
3823       -- set value to less than the maximums
3824       -- do this before minimums since if any max is less than any minimum should skip it.
3825       --
3826       -- set balance to max pct if defined and > balance
3827       --
3828       if l_rollover.mx_rlovr_pct_num is not null then
3829         l_mx_dstrbl_pct_num:=(l_prvdd_val-l_used_val)*l_rollover.mx_rlovr_pct_num/100;
3830         if p_pct_rndg_cd is not null then
3831           l_mx_dstrbl_pct_num:=benutils.do_rounding(
3832             p_rounding_cd    => p_pct_rndg_cd,
3833             p_rounding_rl    => p_pct_rndg_rl,
3834             p_value          => l_mx_dstrbl_pct_num,
3835             p_effective_date => p_effective_date);
3836         end if;
3837         if l_balance > l_mx_dstrbl_pct_num then
3838           --
3839           -- Bug 2185478
3840           --
3841           if p_acty_base_rt_id is not null then
3842              --
3843              g_mx_dstrbl_pct_num  := l_mx_dstrbl_pct_num;
3844              --
3845           end if;
3846           l_balance:=l_mx_dstrbl_pct_num;
3847           --
3848         end if;
3849       end if;
3850       --
3851       -- Set balance to max amount if defined and < balance
3852       --
3853       if l_rollover.mx_rlovr_val is not null and
3854          l_balance > l_rollover.mx_rlovr_val then
3855         l_balance:=l_rollover.mx_rlovr_val;
3856       end if;
3857       --
3858       -- Set balance to max elcn val if defined and < balance
3859       --
3860       if l_rollover.mx_elcn_val is not null and
3861          l_balance > l_rollover.mx_elcn_val then
3862         l_balance:=l_rollover.mx_elcn_val;
3863       end if;
3864 
3865       -- Check the rlover rule - this is a max val rule.
3866       if l_rollover.rlovr_val_rl is not null then
3867           run_rule
3868             (p_effective_date     => p_effective_date
3869             ,p_person_id          => p_person_id
3870             ,p_rlovr_val_rl       => l_rollover.rlovr_val_rl
3871             ,p_business_group_id  => p_business_group_id
3872             ,p_ler_id             => l_rollover.ler_id
3873             ,p_bnft_prvdr_pool_id => p_bnft_prvdr_pool_id
3874             ,p_dflt_excs_trtmt_cd => l_dummy  -- output
3875             ,p_acty_base_rt_id    => l_acty_base_rt_id
3876             ,p_elig_per_elctbl_chc_id      => l_rollover.elig_per_elctbl_chc_id
3877             ,p_mx_val             => l_rollover.mx_rlovr_val); -- output
3878          --
3879          -- Bug 2185478
3880          --
3881          if p_acty_base_rt_id is not null then
3882              --
3883              g_mx_rlovr_rl_val := l_rollover.mx_rlovr_val;
3884              --
3885          end if;
3886          --
3887          if l_rollover.mx_rlovr_val is not null and
3888             l_balance > l_rollover.mx_rlovr_val then
3889             l_balance:=l_rollover.mx_rlovr_val;
3890          end if;
3891       end if;
3892       --
3893       -- compute the min pct value
3894       --
3895       l_mn_dstrbl_pct_num:=(l_prvdd_val-l_used_val)*l_rollover.mn_rlovr_pct_num/100;
3896       if p_pct_rndg_cd is not null then
3897         l_mn_dstrbl_pct_num:=benutils.do_rounding(
3898           p_rounding_cd    => p_pct_rndg_cd,
3899           p_rounding_rl    => p_pct_rndg_rl,
3900           p_value          => l_mn_dstrbl_pct_num,
3901           p_effective_date => p_effective_date);
3902       end if;
3903       --
3904       -- Bug 2185478
3905       --
3906       if p_acty_base_rt_id is not null then
3907          --
3908          g_mn_dstrbl_pct_num := l_mn_dstrbl_pct_num;
3909          g_mn_elcn_val       := l_rollover.mn_elcn_val;
3910          g_mn_rlovr_val      := l_rollover.mn_rlovr_val;
3911          --
3912       end if;
3913       --
3914       --
3915       -- if less than mimimums cannot rollover, skip it.
3916       --
3917       if not p_calculate_only_mode then
3918         --
3919         if (l_balance < l_rollover.mn_elcn_val or -- should this include the current elections?
3920             l_balance < l_rollover.mn_rlovr_val or
3921             l_balance < l_mn_dstrbl_pct_num or
3922             l_balance <=0 )
3923         then
3924           hr_utility.set_location('Balance less than minimum or zero',60);
3925 	  hr_utility.set_location('l_balance : '||l_balance,60);
3926 
3927 	  --Bug 7118730
3928 	  open c_old_ledger;
3929           fetch c_old_ledger into
3930             l_bnft_prvdd_ldgr_id,
3931             l_old_rlovr_val,
3932             l_object_version_number,
3933             l_effective_start_date
3934           ;
3935           if c_old_ledger%found then
3936 	  Get_DT_Upd_Mode
3937                   (p_effective_date        => p_effective_date,
3938                    p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
3939                    p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
3940                    p_base_key_value        => l_bnft_prvdd_ldgr_id,
3941                    p_mode                  => l_datetrack_mode);
3942 	  l_balance := 0;
3943           ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
3944                        p_bnft_prvdd_ldgr_id         => l_bnft_prvdd_ldgr_id
3945                       ,p_effective_start_date       => l_effective_start_date
3946                       ,p_effective_end_date         => l_effective_end_date
3947                       ,p_prtt_ro_of_unusd_amt_flag  => 'Y'
3948                       ,p_frftd_val                  => null
3949                       ,p_prvdd_val                  => null
3950                       ,p_used_val                   => l_balance
3951                       ,p_bnft_prvdr_pool_id         => p_bnft_prvdr_pool_id
3952                       ,p_acty_base_rt_id            => l_acty_base_rt_id
3953                       ,p_per_in_ler_id              => p_per_in_ler_id
3954                       ,p_prtt_enrt_rslt_id          => g_credit_pool_result_id
3955                       ,p_business_group_id          => p_business_group_id
3956                       ,p_object_version_number      => l_object_version_number
3957                       ,p_cash_recd_val              => 0
3958                       ,p_effective_date             => p_effective_date
3959                       ,p_datetrack_mode             => l_datetrack_mode
3960             );
3961 	    end if;
3962 	    close c_old_ledger;
3963           --
3964           --
3965           -- nothing to do
3966           --
3967         else
3968           hr_utility.set_location(l_proc, 70);
3969           --
3970           -- Find out amount of rollover
3971           --
3972           open c_old_ledger;
3973           fetch c_old_ledger into
3974             l_bnft_prvdd_ldgr_id,
3975             l_old_rlovr_val,
3976             l_object_version_number,
3977             l_effective_start_date
3978           ;
3979           --
3980           -- perform rollover
3981           --
3982           hr_utility.set_location(l_proc, 80);
3983           if c_old_ledger%notfound then
3984             hr_utility.set_location(l_proc, 90);
3985             l_rollover_diff_total:=l_rollover_diff_total+l_balance;
3986             ben_Benefit_Prvdd_Ledger_api.create_Benefit_Prvdd_Ledger (
3987                        p_bnft_prvdd_ldgr_id           => l_bnft_prvdd_ldgr_id
3988                       ,p_effective_start_date         => l_effective_start_date
3989                       ,p_effective_end_date           => l_effective_end_date
3990                       ,p_prtt_ro_of_unusd_amt_flag    => 'Y'
3991                       ,p_frftd_val                    => null
3992                       ,p_prvdd_val                    => null
3993                       ,p_used_val                     => l_balance
3994                       ,p_bnft_prvdr_pool_id           => p_bnft_prvdr_pool_id
3995                       ,p_acty_base_rt_id              => l_acty_base_rt_id
3996                       ,p_per_in_ler_id                => p_per_in_ler_id
3997                       ,p_person_id                    => p_person_id
3998                       ,p_enrt_mthd_cd                 => p_enrt_mthd_cd
3999                       ,p_prtt_enrt_rslt_id            => g_credit_pool_result_id
4000                       ,p_business_group_id            => p_business_group_id
4001                       ,p_object_version_number        => l_object_version_number
4002                       ,p_cash_recd_val                => null
4003                       ,p_effective_date               => p_effective_date
4004             );
4005             hr_utility.set_location('CREATED LDGR ID='||to_char(l_bnft_prvdd_ldgr_id),100);
4006           else --if l_old_rlovr_val<>l_balance then-------Bug 	7363185
4007             hr_utility.set_location(l_proc, 110);
4008             /*
4009             if l_effective_start_date=p_effective_date then
4010               l_datetrack_mode:=hr_api.g_correction;
4011             else
4012               l_datetrack_mode:=hr_api.g_update;
4013             end if;
4014             */
4015             Get_DT_Upd_Mode
4016                   (p_effective_date        => p_effective_date,
4017                    p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
4018                    p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
4019                    p_base_key_value        => l_bnft_prvdd_ldgr_id,
4020                    p_mode                  => l_datetrack_mode);
4021             hr_utility.set_location('UPDATING LDGR ID='||to_char(l_bnft_prvdd_ldgr_id),120);
4022             l_rollover_diff_total:=l_rollover_diff_total+l_balance-l_old_rlovr_val;
4023             ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
4024                        p_bnft_prvdd_ldgr_id         => l_bnft_prvdd_ldgr_id
4025                       ,p_effective_start_date       => l_effective_start_date
4026                       ,p_effective_end_date         => l_effective_end_date
4027                       ,p_prtt_ro_of_unusd_amt_flag  => 'Y'
4028                       ,p_frftd_val                  => null
4029                       ,p_prvdd_val                  => null
4030                       ,p_used_val                   => l_balance
4031                       ,p_bnft_prvdr_pool_id         => p_bnft_prvdr_pool_id
4032                       ,p_acty_base_rt_id            => l_acty_base_rt_id
4033                       ,p_per_in_ler_id              => p_per_in_ler_id
4034                       ,p_prtt_enrt_rslt_id          => g_credit_pool_result_id
4035                       ,p_business_group_id          => p_business_group_id
4036                       ,p_object_version_number      => l_object_version_number
4037                       ,p_cash_recd_val              => 0
4038                       ,p_effective_date             => p_effective_date
4039                       ,p_datetrack_mode             => l_datetrack_mode
4040             );
4041             hr_utility.set_location('UPDATED LEDGER (ID='||to_char(l_bnft_prvdd_ldgr_id),130);
4042           end if;
4043           close c_old_ledger;
4044         end if;
4045       end if;
4046     end if; -- rule returned 'Y'
4047     hr_utility.set_location(l_proc, 140);
4048   end loop;
4049   if l_rollover_diff_total is null then
4050      p_rollover_val := 0;
4051   else   p_rollover_val:=l_rollover_diff_total;
4052   end if;
4053   hr_utility.set_location('We rolled over $'||to_char(l_rollover_diff_total), 998);
4054   hr_utility.set_location('Leaving:'||l_proc, 999);
4055 end default_rollovers;
4056 --
4057 -- Bug 2185478 Added procedure to validate the rollover value entered by the user on
4058 -- flex enrollment form and show proper message immediately
4059 --------------------------------------------------------------------------------
4060 --                      validate_rollover_val
4061 --------------------------------------------------------------------------------
4062 procedure validate_rollover_val
4063   (p_calculate_only_mode  in     boolean default false
4064   ,p_bnft_prvdr_pool_id   in     number
4065   ,p_person_id            in     number
4066   ,p_per_in_ler_id        in     number
4067   ,p_acty_base_rt_id      in     number default null
4068   ,p_enrt_mthd_cd         in     varchar2
4069   ,p_effective_date       in     date
4070   ,p_datetrack_mode       in     varchar2
4071   ,p_business_group_id    in     number
4072   ,p_pct_rndg_cd          in     varchar2
4073   ,p_pct_rndg_rl          in     number
4074   ,p_dflt_excs_trtmt_cd   in     varchar2
4075   ,p_new_rollover_val     in     number
4076   ,p_rollover_val            out nocopy number
4077   )
4078 is
4079   --
4080   l_proc varchar2(72) := g_package||'.validate_rollover_val';
4081   --
4082   cursor c_pool_info is
4083     select bpp.*
4084     from   ben_bnft_prvdr_pool_f bpp
4085     where  p_effective_date between
4086              bpp.effective_start_date and
4087              bpp.effective_end_date and
4088            bpp.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
4089            bpp.business_group_id=p_business_group_id
4090     ;
4091   l_pool_info    c_pool_info%rowtype;
4092   l_rollover_val number;
4093   --
4094 begin
4095   --
4096   hr_utility.set_location('Entering:'||l_proc, 10);
4097   --
4098   -- Initialise globals.
4099   --
4100   g_balance           := null;
4101   g_mx_dstrbl_pct_num := null;
4102   g_mx_rlovr_val      := null;
4103   g_mx_elcn_val       := null;
4104   g_mx_rlovr_rl_val   := null;
4105   g_mn_dstrbl_pct_num := null;
4106   g_mn_rlovr_val      := null;
4107   g_mn_elcn_val       := null;
4108   --
4109   open c_pool_info;
4110   fetch c_pool_info into l_pool_info;
4111   if c_pool_info%notfound then
4112       --
4113       -- error
4114       --
4115       close c_pool_info;
4116       fnd_message.set_name('BEN','BEN_92538_POOL_NOT_FOUND');
4117       fnd_message.set_token('PROC',l_proc);
4118       fnd_message.set_token('PERSON_ID',to_char(p_person_id));
4119       fnd_message.set_token('ACTY_BASE_RT_ID',to_char(p_acty_base_rt_id));
4120       fnd_message.set_token('BNFT_PRVDR_POOL_ID',
4121                           to_char(p_bnft_prvdr_pool_id));
4122       hr_utility.set_location(l_proc,20);
4123       fnd_message.raise_error;
4124   end if;
4125   hr_utility.set_location(l_proc,30 );
4126   if l_pool_info.dflt_excs_trtmt_cd in ('DSTRBT_RLOVR_FRFT','RLOVR_DSTRBT_FRFT') then
4127    --
4128    default_rollovers(
4129      p_calculate_only_mode     => true,
4130      p_bnft_prvdr_pool_id      => p_bnft_prvdr_pool_id,
4131      p_person_id               => p_person_id,
4132      p_acty_base_rt_id         => p_acty_base_rt_id,
4133      p_effective_date          => p_effective_date,
4134      p_datetrack_mode          => p_datetrack_mode,
4135      p_business_group_id       => p_business_group_id,
4136      p_pct_rndg_cd             => l_pool_info.pct_rndg_cd,
4137      p_pct_rndg_rl             => l_pool_info.pct_rndg_rl,
4138      p_dflt_excs_trtmt_cd      => l_pool_info.dflt_excs_trtmt_cd,
4139      p_rollover_val            => l_rollover_val, -- returns the rollover change
4140      p_per_in_ler_id           => p_per_in_ler_id,
4141      p_enrt_mthd_cd            => p_enrt_mthd_cd);
4142    --
4143   end if;
4144   --
4145   hr_utility.set_location( 'g_rollover_val = ' || g_balance, 30);
4146   hr_utility.set_location( 'g_mx_dstrbl_pct_num = ' || g_mx_dstrbl_pct_num, 30);
4147   hr_utility.set_location( 'g_mx_rlovr_val = ' || g_mx_rlovr_val, 30);
4148   hr_utility.set_location( 'g_mx_elcn_val = ' || g_mx_elcn_val, 30);
4149   hr_utility.set_location( 'g_mn_dstrbl_pct_num = ' || g_mn_dstrbl_pct_num, 30);
4150   hr_utility.set_location( 'g_mn_rlovr_val = ' || g_mn_rlovr_val, 30);
4151   hr_utility.set_location( 'g_mn_elcn_val = ' || g_mn_elcn_val, 30);
4152   hr_utility.set_location( 'g_mx_rlovr_rl_val = ' || g_mx_rlovr_rl_val, 30);
4153   hr_utility.set_location( 'l_rollover_val = ' || g_balance, 30);
4154   hr_utility.set_location( 'p_new_rollover_val = ' || p_new_rollover_val, 30);
4155   --
4156   -- Raise errors if entered rollover value exceeds any limits set
4157   --
4158   if p_new_rollover_val /* g_balance */ > g_mx_dstrbl_pct_num then
4159      --
4160      fnd_message.set_name('BEN','BEN_92960_RLOVR_VAL_GT_MXDPN');
4161      hr_utility.set_location(l_proc,40);
4162      fnd_message.raise_error;
4163      --
4164   end if;
4165   --
4166   if p_new_rollover_val /* g_balance */ > g_mx_rlovr_val then
4167      --
4168      fnd_message.set_name('BEN','BEN_92961_RLOVR_VAL_GT_MXRV');
4169      hr_utility.set_location(l_proc,41);
4170      fnd_message.raise_error;
4171      --
4172   end if;
4173   if p_new_rollover_val /* g_balance */ > g_mx_elcn_val then
4174      --
4175      fnd_message.set_name('BEN','BEN_92962_RLOVR_VAL_GT_MXEV');
4176      hr_utility.set_location(l_proc,42);
4177      fnd_message.raise_error;
4178      --
4179   end if;
4180   --
4181   if p_new_rollover_val /* g_balance */ > g_mx_rlovr_rl_val then
4182      --
4183      fnd_message.set_name('BEN','BEN_92963_RLOVR_VAL_GT_MXRRV');
4184      hr_utility.set_location(l_proc,43);
4185      fnd_message.raise_error;
4186      --
4187   end if;
4188   --
4189   --
4190   if p_new_rollover_val /* g_balance */ < g_mn_dstrbl_pct_num then
4191      --
4192      fnd_message.set_name('BEN','BEN_92964_RLOVR_VAL_GT_MNDPN');
4193      hr_utility.set_location(l_proc,44);
4194      fnd_message.raise_error;
4195      --
4196   end if;
4197   --
4198   if p_new_rollover_val /* g_balance */ < g_mn_rlovr_val then
4199      --
4200      fnd_message.set_name('BEN','BEN_92965_RLOVR_VAL_GT_MNRV');
4201      hr_utility.set_location(l_proc,45);
4202      fnd_message.raise_error;
4203      --
4204   end if;
4205   if p_new_rollover_val /* g_balance */ < g_mn_elcn_val then
4206      --
4207      fnd_message.set_name('BEN','BEN_92966_RLOVR_VAL_GT_MNEV');
4208      hr_utility.set_location(l_proc,46);
4209      fnd_message.raise_error;
4210      --
4211   end if;
4212   --
4213   -- Reset globals
4214   --
4215   g_balance           := null;
4216   g_mx_dstrbl_pct_num := null;
4217   g_mx_rlovr_val      := null;
4218   g_mx_rlovr_rl_val   := null;
4219   g_mx_elcn_val       := null;
4220   g_mn_dstrbl_pct_num := null;
4221   g_mn_rlovr_val      := null;
4222   g_mn_elcn_val       := null;
4223   --
4224   hr_utility.set_location('Leaving:'||l_proc, 40);
4225   --
4226 end validate_rollover_val;
4227 --------------------------------------------------------------------------------
4228 --                      create_rollover_enrollment
4229 --------------------------------------------------------------------------------
4230 procedure create_rollover_enrollment(
4231         p_bnft_prvdr_pool_id in number,
4232         p_person_id in number,
4233         p_per_in_ler_id in number,
4234         p_effective_date in date,
4235         p_datetrack_mode in varchar2,
4236         p_acty_base_rt_id in number,
4237         p_rlovr_amt in number,
4238         p_old_rlovr_amt in number,
4239         p_business_group_id in number,
4240         p_enrt_mthd_cd in varchar2) is
4241   l_proc varchar2(72) := g_package||'.create_rollover_enrollment';
4242 
4243 l_new_prtt_rt_val        number := 0;
4244 l_new_ann_val            number := 0;
4245 l_old_prtt_rt_val_amt    number := 0;
4246 l_good_prtt_rt_val_id    number := null;
4247 l_datetrack_mode         varchar2(30);
4248 l_effective_start_date   date;
4249 l_effective_end_date     date;
4250 l_suspend_flag           varchar2(30);
4251 l_object_version_number  number;
4252 l_prtt_enrt_interim_id   number;
4253 l_prtt_rt_val_id         number;
4254 l_dpnt_actn_warning      boolean;
4255 l_bnf_actn_warning       boolean;
4256 l_ctfn_actn_warning      boolean;
4257 l_global_asg_rec ben_global_enrt.g_global_asg_rec_type;
4258 
4259 
4260 -- query to get old prtt_rt_val amount
4261   cursor c_old_prtt_rt_val(v_enrt_rt_id number) is
4262     select
4263         prv.rt_val,
4264         prv.prtt_rt_val_id
4265     from ben_prtt_rt_val prv,
4266          ben_enrt_rt ecr
4267     where ecr.enrt_rt_id = v_enrt_rt_id and
4268           prv.prtt_rt_val_id=ecr.prtt_rt_val_id and
4269           prv.prtt_rt_val_stat_cd is null and
4270 	  prv.per_in_ler_id = p_per_in_ler_id and
4271           ecr.business_group_id=p_business_group_id;
4272 
4273   cursor c_rlovr_chc is
4274     select decode(enb.enrt_bnft_id,
4275                     null, ecr2.enrt_rt_id,
4276                           ecr1.enrt_rt_id) enrt_rt_id,
4277            decode(enb.enrt_bnft_id,
4278                     null, ecr2.rt_mlt_cd,
4279                           ecr1.rt_mlt_cd) rt_mlt_cd,
4280 	   decode(enb.enrt_bnft_id,
4281                     null, ecr2.entr_val_at_enrt_flag,
4282                           ecr1.entr_val_at_enrt_flag) entr_val_at_enrt_flag, --bug 5608160
4283            enb.enrt_bnft_id,
4284            nvl(enb.val, enb.dflt_val) bnft_val,
4285            epe.elig_per_elctbl_chc_id,
4286            pel.acty_ref_perd_cd,
4287            pen.prtt_enrt_rslt_id,
4288            pen.bnft_amt,
4289            pen.object_version_number
4290     from   ben_per_in_ler pil,
4291            ben_elig_per_elctbl_chc epe,
4292            ben_pil_elctbl_chc_popl pel,
4293            ben_enrt_rt ecr1,
4294            ben_enrt_rt ecr2,
4295            ben_enrt_bnft enb,
4296            ben_prtt_enrt_rslt_f pen,
4297            ben_bnft_prvdr_pool_f bpp -- join to get only current pgm_id - rgajula
4298     where  pil.per_in_ler_id=p_per_in_ler_id and
4299            pil.business_group_id=p_business_group_id and
4300            pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
4301            pil.per_in_ler_id=epe.per_in_ler_id and
4302            pil.per_in_ler_id = pel.per_in_ler_id and
4303            pel.pil_elctbl_chc_popl_id = epe.pil_elctbl_chc_popl_id and
4304            epe.business_group_id=p_business_group_id and
4305            epe.elig_per_elctbl_chc_id=ecr2.elig_per_elctbl_chc_id(+) and
4306 	       bpp.bnft_prvdr_pool_id = p_bnft_prvdr_pool_id and                                  --
4307 	       bpp.business_group_id = p_business_group_id and                                    --
4308 	       p_effective_date between bpp.effective_start_date and bpp.effective_end_date and   --
4309 	       bpp.pgm_id = epe.pgm_id and                                                        --
4310            (p_acty_base_rt_id is null or
4311             ecr1.acty_base_rt_id = p_acty_base_rt_id or
4312             ecr2.acty_base_rt_id = p_acty_base_rt_id) and
4313            pen.prtt_enrt_rslt_id(+)=epe.prtt_enrt_rslt_id and
4314            epe.elig_per_elctbl_chc_id=enb.elig_per_elctbl_chc_id(+) and
4315            enb.enrt_bnft_id = ecr1.enrt_bnft_id(+) and
4316            pen.prtt_enrt_rslt_stat_cd is null  and
4317            p_effective_date between
4318            pen.effective_start_date(+) and pen.effective_end_date(+) and
4319            pen.business_group_id(+)=p_business_group_id ;
4320  --
4321  l_rlovr_chc c_rlovr_chc%rowtype;
4322  --GEVITY
4323  cursor c_abr(cv_acty_base_rt_id number)
4324  is select rate_periodization_rl
4325       from ben_acty_base_rt_f abr
4326      where abr.acty_base_rt_id = cv_acty_base_rt_id
4327        and p_effective_date between abr.effective_start_date
4328                                 and abr.effective_end_date ;
4329  --
4330  l_rate_periodization_rl NUMBER;
4331  --
4332  l_dfnd_dummy number;
4333  l_ann_dummy  number;
4334  l_cmcd_dummy number;
4335  l_assignment_id                 per_all_assignments_f.assignment_id%type;
4336  l_payroll_id                    per_all_assignments_f.payroll_id%type;
4337  l_organization_id               per_all_assignments_f.organization_id%type;
4338  --END GEVITY
4339 begin
4340   hr_utility.set_location('Entering:'|| l_proc, 10);
4341   hr_utility.set_location('crdt_pool_rslt_id='||to_char(g_credit_pool_result_id),10);
4342   --
4343   -- Get choice id of what you are trying to roll into
4344   open c_rlovr_chc;
4345   fetch c_rlovr_chc into l_rlovr_chc;
4346   if c_rlovr_chc%notfound then
4347     hr_utility.set_location('BEN_91457_ELCTBL_CHC_NOT_FOUND ACTY_id:'||
4348        to_char(p_acty_base_rt_id), 20); hr_utility.set_location('p_person_id:'||to_char(p_person_id), 20);
4349     hr_utility.set_location('p_business_group_id:'||to_char(p_business_group_id), 20);
4350     close c_rlovr_chc;
4351     fnd_message.set_name('BEN','BEN_91457_ELCTBL_CHC_NOT_FOUND');
4352     fnd_message.set_token('ID', 'NA');
4353     fnd_message.set_token('PROC',l_proc);
4354     fnd_message.raise_error;
4355   end if;
4356   close c_rlovr_chc;
4357 
4358   --
4359   -- get old prtt_rt_val
4360   --
4361   if l_rlovr_chc.enrt_rt_id is not null then
4362     open c_old_prtt_rt_val(l_rlovr_chc.enrt_rt_id);
4363     fetch c_old_prtt_rt_val into
4364       l_old_prtt_rt_val_amt,
4365       l_good_prtt_rt_val_id;
4366 
4367     close c_old_prtt_rt_val;
4368   end if;
4369   --
4370   -- compute the new prtt_rt_val
4371   -- start bug 5608160
4372   hr_utility.set_location('Entering: SSARKAR ', 10);
4373 
4374 if nvl(l_rlovr_chc.entr_val_at_enrt_flag,'N') = 'N' then
4375      l_new_prtt_rt_val:=l_old_prtt_rt_val_amt+p_rlovr_amt;
4376 else
4377      l_new_prtt_rt_val:=l_old_prtt_rt_val_amt-p_old_rlovr_amt+p_rlovr_amt;
4378 end if ;
4379 
4380 -- end bug 5608160
4381 
4382  hr_utility.set_location('l_new_prtt_rt_val='||l_new_prtt_rt_val,10);
4383  hr_utility.set_location('l_old_prtt_rt_val_amt='||l_old_prtt_rt_val_amt,10);
4384  hr_utility.set_location('p_old_rlovr_amt='||p_old_rlovr_amt,10);
4385  hr_utility.set_location('p_rlovr_amt='||p_rlovr_amt,10);
4386   --
4387   -- create the result
4388   --
4389     hr_utility.set_location('enrt_rt_id='||to_char(l_rlovr_chc.enrt_rt_id),40);
4390     hr_utility.set_location('choice_id='||to_char(l_rlovr_chc.elig_per_elctbl_chc_id),50);
4391     hr_utility.set_location('result_id='||to_char(l_rlovr_chc.prtt_enrt_rslt_id),60);
4392     --
4393     -- Calculate the annual value.
4394     --
4395    if l_new_prtt_rt_val > 0 and l_rlovr_chc.enrt_rt_id is not null then
4396      --
4397      /*
4398      ben_global_enrt.get_asg  -- assignment
4399        (p_person_id              => p_person_id
4400        ,p_effective_date         => p_effective_date
4401        ,p_global_asg_rec         => l_global_asg_rec);
4402      */ --GEVITY
4403      ben_element_entry.get_abr_assignment
4404       (p_person_id       => p_person_id
4405       ,p_effective_date  => p_effective_date
4406       ,p_acty_base_rt_id => p_acty_base_rt_id
4407       ,p_organization_id => l_organization_id
4408       ,p_payroll_id      => l_payroll_id
4409       ,p_assignment_id   => l_assignment_id
4410       );
4411      --
4412      open c_abr(p_acty_base_rt_id) ;
4413        fetch c_abr into l_rate_periodization_rl ;
4414      close c_abr;
4415      --
4416      IF l_rate_periodization_rl IS NOT NULL THEN
4417        --
4418        l_dfnd_dummy := l_new_prtt_rt_val;
4419        --
4420        ben_distribute_rates.periodize_with_rule
4421                   (p_formula_id             => l_rate_periodization_rl
4422                   ,p_effective_date         => p_effective_date
4423                   ,p_assignment_id          => l_assignment_id
4424                   ,p_convert_from_val       => l_dfnd_dummy
4425                   ,p_convert_from           => 'DEFINED'
4426                   ,p_elig_per_elctbl_chc_id => l_rlovr_chc.elig_per_elctbl_chc_id
4427                   ,p_acty_base_rt_id        => p_acty_base_rt_id
4428                   ,p_business_group_id      => p_business_group_id
4429                   ,p_enrt_rt_id             => l_rlovr_chc.enrt_rt_id
4430                   ,p_ann_val                => l_new_ann_val
4431                   ,p_cmcd_val               => l_cmcd_dummy
4432                   ,p_val                    => l_cmcd_dummy
4433        );
4434        --
4435      ELSE
4436        --
4437        l_new_ann_val := ben_distribute_rates.period_to_annual
4438                     (p_amount                  => l_new_prtt_rt_val,
4439                      p_enrt_rt_id              => l_rlovr_chc.enrt_rt_id,
4440                      p_acty_ref_perd_cd        => l_rlovr_chc.acty_ref_perd_cd,
4441                      p_business_group_id       => p_business_group_id,
4442                      p_effective_date          => p_effective_date,
4443                      p_complete_year_flag      => 'Y',
4444                      p_payroll_id              => l_global_asg_rec.payroll_id);
4445      END IF; --GEVITY
4446      --
4447      if l_rlovr_chc.rt_mlt_cd = 'SAREC' then
4448        l_rlovr_chc.bnft_val := l_new_ann_val;
4449      end if;
4450      --
4451    end if;
4452     --
4453     -- call election_information api to create prtt enrt result
4454     --
4455       --
4456       if l_rlovr_chc.prtt_enrt_rslt_id is not null then
4457         l_datetrack_mode:=hr_api.g_correction;
4458       else
4459         l_datetrack_mode:=hr_api.g_insert;
4460       end if;
4461       --
4462       hr_utility.set_location(l_proc, 70);
4463       ben_election_information.election_information(
4464         p_elig_per_elctbl_chc_id   => l_rlovr_chc.elig_per_elctbl_chc_id,
4465         p_prtt_enrt_rslt_id        => l_rlovr_chc.prtt_enrt_rslt_id,
4466         p_effective_date           => p_effective_date,
4467         p_effective_start_date     => l_effective_start_date,
4468         p_effective_end_date       => l_effective_end_date,
4469         p_enrt_mthd_cd             => p_enrt_mthd_cd,
4470         p_enrt_bnft_id             => l_rlovr_chc.enrt_bnft_id,
4471         p_bnft_val                 => l_rlovr_chc.bnft_val,
4472         p_datetrack_mode           => l_datetrack_mode,
4473         p_suspend_flag             => l_suspend_flag,
4474         p_object_version_number    => l_rlovr_chc.object_version_number,
4475         p_prtt_enrt_interim_id     => l_prtt_enrt_interim_id,
4476         p_rt_val1                  => l_new_prtt_rt_val,
4477         p_ann_rt_val1              => l_new_ann_val,
4478         p_enrt_rt_id1              => l_rlovr_chc.enrt_rt_id,
4479         p_prtt_rt_val_id1          => l_good_prtt_rt_val_id,
4480         p_prtt_rt_val_id2          => l_prtt_rt_val_id,
4481         p_prtt_rt_val_id3          => l_prtt_rt_val_id,
4482         p_prtt_rt_val_id4          => l_prtt_rt_val_id,
4483         p_prtt_rt_val_id5          => l_prtt_rt_val_id,
4484         p_prtt_rt_val_id6          => l_prtt_rt_val_id,
4485         p_prtt_rt_val_id7          => l_prtt_rt_val_id,
4486         p_prtt_rt_val_id8          => l_prtt_rt_val_id,
4487         p_prtt_rt_val_id9          => l_prtt_rt_val_id,
4488         p_prtt_rt_val_id10         => l_prtt_rt_val_id,
4489         p_business_group_id        => p_business_group_id,
4490         p_dpnt_actn_warning        => l_dpnt_actn_warning,
4491         p_bnf_actn_warning         => l_bnf_actn_warning,
4492         p_ctfn_actn_warning        => l_ctfn_actn_warning
4493       );
4494   hr_utility.set_location('Leaving:'||l_proc, 999);
4495 end create_rollover_enrollment;
4496 --------------------------------------------------------------------------------
4497 --                      total_pools
4498 --------------------------------------------------------------------------------
4499 procedure total_pools(
4500         p_validate             in boolean default false,
4501         p_prtt_enrt_rslt_id    in out nocopy number,
4502         p_prtt_rt_val_id       in out nocopy number,
4503         p_acty_ref_perd_cd     out nocopy varchar2,
4504         p_acty_base_rt_id      out nocopy number,
4505         p_rt_strt_dt           out nocopy date,
4506         p_rt_val               out nocopy number,
4507         p_element_type_id      out nocopy number,
4508         p_person_id            in number,
4509         p_per_in_ler_id        in number,
4510         p_enrt_mthd_cd         in varchar2,
4511         p_effective_date       in date,
4512         p_business_group_id    in number,
4513         p_pgm_id               in number default null
4514 ) is
4515 
4516   l_proc varchar2(72) := g_package||'.total_pools';
4517   l_new_cash              number;
4518   l_bnft_prvdd_ldgr_id    number;
4519   l_elig_per_elctbl_chc_id    number;
4520   l_effective_start_date  date;
4521   l_effective_end_date    date;
4522   l_total_credits         number;
4523   l_def_exc_amount        number;
4524   l_def_exc_amt           number;
4525   l_deficit_limit         number;
4526   l_object_version_number number;
4527   l_acty_base_rt_id       number;
4528   l_cash_val              number;
4529   l_compensation_value    number;
4530   l_datetrack_mode        varchar2(30);
4531   l_rollover_val          number := 0 ;
4532   l_dstrbtd_val           number;
4533   l_dummy                 number;
4534   l_uses_net_crs_mthd     boolean := false;
4535   l_dummy_number          number;
4536   l_redis_credits         boolean := false;
4537 
4538   cursor c_ledger_totals is
4539     select        bpl.bnft_prvdr_pool_id,
4540                 nvl(sum(prvdd_val),0) prvdd_total,
4541                 nvl(sum(used_val),0) used_total, -- include rollovers
4542                 nvl(sum(frftd_val),0) frftd_total,
4543                 nvl(sum(cash_recd_val),0) cash_recd_total,
4544                 pil_flex.ler_id
4545     from        ben_bnft_prvdd_ldgr_f bpl,
4546                 ben_elig_per_elctbl_chc epe_flex,
4547                 ben_per_in_ler pil_flex,
4548                 ben_per_in_ler pil_flex1
4549     where       p_effective_date between
4550                         bpl.effective_start_date and bpl.effective_end_date and
4551                 bpl.business_group_id=p_business_group_id and
4552                 bpl.bnft_prvdr_pool_id is not null and
4553                 bpl.prtt_enrt_rslt_id=epe_flex.prtt_enrt_rslt_id and
4554                 epe_flex.business_group_id=p_business_group_id and
4555                 epe_flex.per_in_ler_id=pil_flex.per_in_ler_id and
4556                 pil_flex.business_group_id=p_business_group_id and
4557                 pil_flex.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
4558                 pil_flex.per_in_ler_id=p_per_in_ler_id and
4559                 -- Bug 1634870
4560                 pil_flex1.per_in_ler_id=bpl.per_in_ler_id and
4561                 pil_flex1.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
4562                 pil_flex1.business_group_id=p_business_group_id
4563                 and epe_flex.pgm_id = p_pgm_id
4564     group by    bpl.bnft_prvdr_pool_id, pil_flex.ler_id
4565     ;
4566   l_ledger_totals c_ledger_totals%rowtype;
4567 
4568   cursor c_pool_info is
4569     select bpp.*
4570     from   ben_bnft_prvdr_pool_f bpp
4571     where  p_effective_date between
4572              bpp.effective_start_date and
4573              bpp.effective_end_date and
4574            bpp.bnft_prvdr_pool_id=l_ledger_totals.bnft_prvdr_pool_id and
4575            bpp.business_group_id=p_business_group_id
4576     ;
4577   l_pool_info c_pool_info%rowtype;
4578 
4579   -- for the provided credits choice row for the pool we are working with,
4580   -- get the acty-base-rt-id from the enrt-rt FLXCR row.
4581   cursor c_cash_abr is
4582     select      ecr.acty_base_rt_id,
4583                 epe.elig_per_elctbl_chc_id
4584     from        ben_per_in_ler pil,
4585                 ben_elig_per_elctbl_chc epe,
4586                 ben_enrt_rt ecr
4587     where       pil.per_in_ler_id=p_per_in_ler_id and
4588                 pil.business_group_id=p_business_group_id and
4589                 pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT') and
4590                 epe.per_in_ler_id=pil.per_in_ler_id and
4591                 epe.business_group_id=p_business_group_id and
4592                 epe.bnft_prvdr_pool_id=l_ledger_totals.bnft_prvdr_pool_id and
4593                 ecr.elig_per_elctbl_chc_id=epe.elig_per_elctbl_chc_id and
4594                 ecr.rt_usg_cd='FLXCR' and
4595                 ecr.business_group_id=p_business_group_id;
4596   --
4597   -- Bug 1711831
4598   --
4599   cursor c_get_pool_abr is
4600     select abr.acty_base_rt_id
4601     from   ben_bnft_prvdd_ldgr_f bpl,
4602            ben_acty_base_rt_f abr
4603     where  bpl.bnft_prvdr_pool_id = l_ledger_totals.bnft_prvdr_pool_id and
4604            abr.rt_usg_cd = 'FLXCR' and
4605            bpl.business_group_id = abr.business_group_id and
4606            bpl.business_group_id = p_business_group_id and
4607            bpl.acty_base_rt_id   = abr.acty_base_rt_id and
4608            p_effective_date between abr.effective_start_date and
4609                                     abr.effective_end_date ;
4610   --
4611   cursor c_element_details is
4612     select      prv.acty_ref_perd_cd,
4613                 ecr.acty_base_rt_id,
4614                 prv.rt_strt_dt,
4615                 prv.rt_val,
4616                 abr.element_type_id
4617     from        ben_prtt_rt_val prv,
4618                 ben_enrt_rt ecr,
4619                 ben_acty_base_rt_f abr,
4620                 ben_prtt_enrt_rslt_f res
4621     where       prv.prtt_rt_val_id=p_prtt_rt_val_id and
4622                 prv.business_group_id=p_business_group_id and
4623                 ecr.prtt_rt_val_id=prv.prtt_rt_val_id and
4624                 ecr.business_group_id=p_business_group_id and
4625                 abr.acty_base_rt_id=ecr.acty_base_rt_id and
4626                 abr.business_group_id=p_business_group_id and
4627                 p_effective_date between
4628                         abr.effective_start_date and abr.effective_end_date and
4629                 res.prtt_enrt_rslt_id=p_prtt_enrt_rslt_id and
4630                 res.business_group_id=p_business_group_id and
4631                 p_effective_date between
4632                         res.effective_start_date and res.effective_end_date
4633     ;
4634   cursor c_old_ledger is
4635     select      bpl.bnft_prvdd_ldgr_id,
4636                 bpl.cash_recd_val,
4637                 bpl.object_version_number,
4638                 bpl.effective_start_date
4639     from        ben_bnft_prvdd_ldgr_f bpl,
4640                 ben_per_in_ler pil
4641     where       bpl.bnft_prvdr_pool_id=l_ledger_totals.bnft_prvdr_pool_id
4642         and     bpl.business_group_id=p_business_group_id
4643         and     bpl.acty_base_rt_id = l_acty_base_rt_id
4644         and     bpl.prtt_enrt_rslt_id=g_credit_pool_result_id
4645         and     bpl.cash_recd_val is not null
4646         and     p_effective_date between
4647                         bpl.effective_start_date and bpl.effective_end_date
4648         and pil.per_in_ler_id=bpl.per_in_ler_id
4649         and pil.business_group_id=bpl.business_group_id
4650         and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT') ;
4651   --
4652   cursor c_get_mx_pct_roll_num is
4653     select bpr.mx_pct_ttl_crs_cn_roll_num
4654           ,bpr.acty_base_rt_id
4655     from   ben_bnft_pool_rlovr_rqmt_f bpr
4656     where  p_effective_date between
4657              bpr.effective_start_date and
4658              bpr.effective_end_date
4659     and    bpr.bnft_prvdr_pool_id=l_ledger_totals.bnft_prvdr_pool_id
4660     and    bpr.business_group_id=p_business_group_id
4661     and    bpr.mx_pct_ttl_crs_cn_roll_num is not null
4662     ;
4663   --
4664   cursor c_get_ldgr(p_acty_base_rt_id in number) is
4665     select bpl.used_val
4666     from   ben_bnft_prvdd_ldgr_f bpl,
4667            ben_per_in_ler pil
4668     where  p_effective_date between
4669              bpl.effective_start_date and
4670              bpl.effective_end_date
4671     and    bpl.acty_base_rt_id = p_acty_base_rt_id
4672     -- UK change : Bug 1634870
4673     and    bpl.per_in_ler_id = pil.per_in_ler_id
4674     and    pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
4675     and    bpl.bnft_prvdr_pool_id=l_ledger_totals.bnft_prvdr_pool_id
4676     and    bpl.business_group_id=p_business_group_id
4677     and    bpl.prtt_ro_of_unusd_amt_flag = 'Y';
4678   -- cursor to fin is that an defult enrollemt
4679   -- chne any of the current result has default enrollment
4680   cursor c_get_mthd is
4681     select 'x' from
4682     ben_prtt_enrt_rslt_f pen
4683     where pen.per_in_ler_id = p_per_in_ler_id
4684       and pen.prtt_enrt_rslt_stat_cd is null
4685       and pen.enrt_mthd_cd   = 'D'
4686       and  p_effective_date between
4687            pen.enrt_cvg_strt_dt and pen.enrt_cvg_thru_dt
4688       and  pen.effective_end_date = hr_api.g_eot ;
4689 
4690    l_default_enrt_flag varchar2(1) := 'N' ;
4691    l_dummy_var         varchar2(1) ;
4692 begin
4693   hr_utility.set_location('Entering:'|| l_proc, 10);
4694 
4695   -- Get the flex credit result id and it's per-in-ler.   Both are needed
4696   -- for when we create ledger rows.
4697   create_flex_credit_enrolment(
4698         p_person_id             => p_person_id,
4699         p_enrt_mthd_cd          => p_enrt_mthd_cd,
4700         p_business_group_id     => p_business_group_id,
4701         p_effective_date        => p_effective_date,
4702         p_prtt_enrt_rslt_id     => p_prtt_enrt_rslt_id,
4703         p_prtt_rt_val_id        => p_prtt_rt_val_id,
4704         p_per_in_ler_id         => p_per_in_ler_id,
4705         p_rt_val                => null,
4706         p_pgm_id                => p_pgm_id
4707       );
4708   l_total_credits:=0;
4709   l_def_exc_amount := 0;
4710   l_def_exc_amt := 0;
4711 
4712   ---decide whether it is default enrollment
4713   open c_get_mthd ;
4714   fetch c_get_mthd into l_dummy_var ;
4715   if c_get_mthd%found then
4716     l_default_enrt_flag := 'Y' ;
4717   else
4718     l_default_enrt_flag := 'N' ;
4719   end if ;
4720   close c_get_mthd ;
4721 
4722   open c_ledger_totals;
4723   hr_utility.set_location(l_proc, 20);
4724 
4725   loop
4726     hr_utility.set_location(l_proc, 25);
4727     --
4728     fetch c_ledger_totals into l_ledger_totals;
4729     hr_utility.set_location(l_proc, 30);
4730     --
4731     exit when c_ledger_totals%notfound;
4732     hr_utility.set_location(l_proc, 55);
4733     hr_utility.set_location( 'prvdd_total = ' || l_ledger_totals.prvdd_total ||
4734                              'used_total = ' || l_ledger_totals.used_total ||
4735                              'frftd_total = ' || l_ledger_totals.frftd_total ||
4736                              'cash_recd_total = ' || l_ledger_totals.cash_recd_total, 55);
4737     --
4738     -- Total up credits provided
4739     --
4740     l_total_credits:=l_total_credits+l_ledger_totals.prvdd_total;
4741     hr_utility.set_location( 'l_total_credits = ' || l_total_credits, 55);
4742     --
4743     -- get the generic ABR to use for cash and forfeitures.
4744     --
4745     l_acty_base_rt_id := null;
4746     open c_cash_abr;
4747     fetch c_cash_abr into  l_acty_base_rt_id, l_elig_per_elctbl_chc_id;
4748     if c_cash_abr%notfound then
4749         -- bug#3365290
4750        if l_ledger_totals.prvdd_total = 0 and
4751           l_ledger_totals.used_total = 0   and
4752           l_ledger_totals.cash_recd_total = 0  and
4753           l_ledger_totals.frftd_total = 0 then
4754          --
4755          null;
4756        else
4757          -- Bug : 1711831
4758          open c_get_pool_abr;
4759          fetch c_get_pool_abr into l_acty_base_rt_id;
4760          if c_get_pool_abr%notfound then
4761             close c_cash_abr;
4762             close c_get_pool_abr;
4763             hr_utility.set_location(l_proc,15);
4764             fnd_message.set_name('BEN','BEN_91725_NO_FLX_CR_ABR_FOUND');
4765             fnd_message.set_token('PROC',l_proc);
4766             fnd_message.set_token('PERSON_ID',to_char(p_person_id));
4767             fnd_message.raise_error;
4768          end if;
4769          close c_get_pool_abr;
4770          -- Bug : 1711831
4771        end if;
4772        --bug#3365290
4773     end if;
4774     close c_cash_abr;
4775     -- bug#3365290
4776     l_pool_info := null;
4777     if l_ledger_totals.prvdd_total = 0 and
4778        l_ledger_totals.used_total = 0   and
4779        l_ledger_totals.cash_recd_total = 0  and
4780        l_ledger_totals.frftd_total = 0 then
4781        --
4782        null;
4783     else
4784       --
4785       open c_pool_info;
4786       hr_utility.set_location(l_proc, 60);
4787       fetch c_pool_info into l_pool_info;
4788       if c_pool_info%notfound then
4789         --
4790         -- error
4791         --
4792         close c_pool_info;
4793         fnd_message.set_name('BEN','BEN_92538_POOL_NOT_FOUND');
4794         fnd_message.set_token('PROC',l_proc);
4795         fnd_message.set_token('PERSON_ID',to_char(p_person_id));
4796         fnd_message.set_token('ACTY_BASE_RT_ID',to_char(l_acty_base_rt_id));
4797         fnd_message.set_token('BNFT_PRVDR_POOL_ID',
4798                           to_char(l_ledger_totals.bnft_prvdr_pool_id));
4799         hr_utility.set_location(l_proc,70);
4800         fnd_message.raise_error;
4801       end if;
4802       close c_pool_info;
4803     --
4804     end if;
4805     --
4806     --  If using the net credits method i.e. only the net credits are
4807     --  processed through payroll.
4808     --
4809     if l_pool_info.uses_net_crs_mthd_flag = 'Y' then
4810       --
4811       -- Check if there is a deficit amount.
4812       --
4813       l_def_exc_amt :=
4814         l_ledger_totals.prvdd_total - l_ledger_totals.used_total;
4815       hr_utility.set_location('l_def_exc_amt: '||l_def_exc_amt,70);
4816       --
4817       if l_def_exc_amt <> 0 then
4818         if l_def_exc_amt < 0 then
4819           --
4820           --  Check if the amounts does not exceed limits defined.
4821           --
4822           --  Cannot exceed the maximum percent of the total credits.
4823           --
4824           if l_pool_info.mx_dfcit_pct_pool_crs_num is not null then
4825             l_deficit_limit :=
4826               l_pool_info.mx_dfcit_pct_pool_crs_num/100 * l_ledger_totals.prvdd_total;
4827             hr_utility.set_location('l_deficit_limit: '||l_deficit_limit,70);
4828             if abs(l_def_exc_amt) > l_deficit_limit then
4829               fnd_message.set_name('BEN', 'BEN_92620_EXCEED_TOT_DFCIT_PCT');
4830               fnd_message.raise_error;
4831             end if;
4832           end if;
4833           --
4834           --  Cannot exceed the maximum percent of compensation.
4835           --
4836           if l_pool_info.mx_dfcit_pct_comp_num is not null then
4837             --
4838             --  Get the compensation factor.
4839             --
4840             ben_derive_factors.determine_compensation
4841               (p_comp_lvl_fctr_id     => l_pool_info.comp_lvl_fctr_id
4842               ,p_person_id            => p_person_id
4843               ,p_per_in_ler_id        => p_per_in_ler_id
4844               ,p_pgm_id               => l_pool_info.pgm_id
4845               ,p_business_group_id    => p_business_group_id
4846               ,p_perform_rounding_flg => true
4847               ,p_effective_date       => p_effective_date
4848               ,p_value                => l_compensation_value
4849               );
4850             --
4851             l_deficit_limit :=
4852               l_pool_info.mx_dfcit_pct_comp_num/100 * l_compensation_value;
4853             hr_utility.set_location('l_deficit_limit_c: '||l_deficit_limit,70);
4854             if abs(l_def_exc_amt) > l_deficit_limit then
4855               fnd_message.set_name('BEN', 'BEN_92621_DFCIT_EXC_COMP_PCT');
4856               fnd_message.raise_error;
4857             end if;
4858           end if;
4859         end if;
4860       end if;
4861     end if;
4862 
4863     hr_utility.set_location(' auto_alct_excs_flag ' || l_pool_info.auto_alct_excs_flag , 998 );
4864     --if l_pool_info.auto_alct_excs_flag='Y' then
4865       -- to avoid bleeding of the output parameter l_dstrbtd_val it is initialised
4866       l_dstrbtd_val := 0;
4867 
4868       hr_utility.set_location(l_proc,80 );
4869       --
4870       -- get the dflt trtmt cd by executing the rule if necessary
4871       if l_pool_info.dflt_excs_trtmt_rl is not null then
4872          run_rule
4873             (p_effective_date     => p_effective_date
4874             ,p_person_id          => p_person_id
4875             ,p_dflt_excs_trtmt_rl => l_pool_info.dflt_excs_trtmt_rl
4876             ,p_business_group_id  => p_business_group_id
4877             ,p_acty_base_rt_id    => l_acty_base_rt_id
4878             ,p_elig_per_elctbl_chc_id    => l_elig_per_elctbl_chc_id
4879             ,p_ler_id             => l_ledger_totals.ler_id
4880             ,p_bnft_prvdr_pool_id => l_ledger_totals.bnft_prvdr_pool_id
4881             ,p_dflt_excs_trtmt_cd => l_pool_info.dflt_excs_trtmt_cd  -- output
4882             ,p_mx_val             => l_dummy); -- output
4883       end if;
4884 
4885       -- execute the appropriate sequence based on the dflt_excs_trtmt_cd
4886       --
4887       if l_pool_info.dflt_excs_trtmt_cd in ('DSTRBT_ALL','DSTRBT_RLOVR_FRFT') then
4888         hr_utility.set_location(l_proc,90 );
4889         distribute_credits
4890           (p_validate            => p_validate
4891           ,p_prtt_enrt_rslt_id   => p_prtt_enrt_rslt_id
4892           ,p_bnft_prvdr_pool_id  => l_ledger_totals.bnft_prvdr_pool_id
4893           ,p_acty_base_rt_id     => l_acty_base_rt_id
4894           ,p_per_in_ler_id       => p_per_in_ler_id
4895           ,p_dflt_excs_trtmt_cd  => l_pool_info.dflt_excs_trtmt_cd
4896           ,p_prvdd_val           => l_ledger_totals.prvdd_total
4897           ,p_rlld_up_val         => 0
4898           ,p_used_val            => l_ledger_totals.used_total
4899           ,p_rollover_val        => 0
4900           ,p_cash_recd_total     => l_ledger_totals.cash_recd_total
4901           ,p_val_rndg_cd         => l_pool_info.val_rndg_cd
4902           ,p_val_rndg_rl         => l_pool_info.val_rndg_rl
4903           ,p_pct_rndg_cd         => l_pool_info.pct_rndg_cd
4904           ,p_pct_rndg_rl         => l_pool_info.pct_rndg_rl
4905           ,p_mn_dstrbl_val       => l_pool_info.mn_dstrbl_val
4906           ,p_mn_dstrbl_pct_num   => l_pool_info.mn_dstrbl_pct_num
4907           ,p_mx_dstrbl_val       => l_pool_info.mx_dstrbl_val
4908           ,p_mx_pct              => l_pool_info.mx_dstrbl_pct_num
4909           ,p_person_id           => p_person_id
4910           ,p_enrt_mthd_cd        => p_enrt_mthd_cd
4911           ,p_effective_date      => p_effective_date
4912           ,p_business_group_id   => p_business_group_id
4913           ,p_dstrbtd_val         => l_dstrbtd_val
4914           ,p_bpl_cash_recd_val   => l_dummy_number
4915           ,p_bnft_prvdd_ldgr_id  => l_dummy_number
4916           );
4917           hr_utility.set_location( 'l_dstrbtd_val = ' || l_dstrbtd_val, 90);
4918       end if;
4919 
4920       if l_pool_info.dflt_excs_trtmt_cd in ('DSTRBT_RLOVR_FRFT','RLOVR_DSTRBT_FRFT') then
4921         hr_utility.set_location(l_proc,100 );
4922         if l_pool_info.auto_alct_excs_flag='Y' or l_default_enrt_flag = 'Y'  then
4923            default_rollovers(
4924              p_bnft_prvdr_pool_id      => l_ledger_totals.bnft_prvdr_pool_id,
4925              p_person_id               => p_person_id,
4926              p_effective_date          => p_effective_date,
4927              p_datetrack_mode          => l_datetrack_mode,
4928              p_business_group_id       => p_business_group_id,
4929              p_pct_rndg_cd             => l_pool_info.pct_rndg_cd,
4930              p_pct_rndg_rl             => l_pool_info.pct_rndg_rl,
4931              p_dflt_excs_trtmt_cd      => l_pool_info.dflt_excs_trtmt_cd,
4932              p_rollover_val            => l_rollover_val, -- returns the rollover change
4933              p_per_in_ler_id           => p_per_in_ler_id,
4934              p_enrt_mthd_cd            => p_enrt_mthd_cd);
4935              hr_utility.set_location( 'l_rollover_val = ' || l_rollover_val, 100);
4936          end if;
4937       end if ;
4938 
4939       if l_pool_info.dflt_excs_trtmt_cd ='RLOVR_DSTRBT_FRFT' then
4940         hr_utility.set_location(l_proc,90 );
4941 	------Bug 7118730,set this flag to recalculate the ledger,
4942 	     -----if used amount is more than provided amt.
4943         if l_ledger_totals.used_total > l_ledger_totals.prvdd_total
4944 	        and l_rollover_val = 0 then
4945 	    l_redis_credits := true;
4946             hr_utility.set_location('re-distribute the credits',90 );
4947         end if;
4948 
4949 	------Bug 7118730
4950 
4951         distribute_credits
4952           (p_validate            => p_validate
4953           ,p_prtt_enrt_rslt_id   => p_prtt_enrt_rslt_id
4954           ,p_bnft_prvdr_pool_id  => l_ledger_totals.bnft_prvdr_pool_id
4955           ,p_acty_base_rt_id     => l_acty_base_rt_id
4956           ,p_dflt_excs_trtmt_cd  => l_pool_info.dflt_excs_trtmt_cd
4957           ,p_prvdd_val           => l_ledger_totals.prvdd_total
4958           ,p_rlld_up_val         => 0
4959           ,p_used_val            => l_ledger_totals.used_total
4960           ,p_rollover_val        => l_rollover_val
4961           ,p_cash_recd_total     => l_ledger_totals.cash_recd_total
4962           ,p_val_rndg_cd         => l_pool_info.val_rndg_cd
4963           ,p_val_rndg_rl         => l_pool_info.val_rndg_rl
4964           ,p_pct_rndg_cd         => l_pool_info.pct_rndg_cd
4965           ,p_pct_rndg_rl         => l_pool_info.pct_rndg_rl
4966           ,p_mn_dstrbl_val       => l_pool_info.mn_dstrbl_val
4967           ,p_mn_dstrbl_pct_num   => l_pool_info.mn_dstrbl_pct_num
4968           ,p_mx_dstrbl_val       => l_pool_info.mx_dstrbl_val
4969           ,p_mx_pct              => l_pool_info.mx_dstrbl_pct_num
4970           ,p_person_id           => p_person_id
4971           ,p_enrt_mthd_cd        => p_enrt_mthd_cd
4972           ,p_effective_date      => p_effective_date
4973           ,p_business_group_id   => p_business_group_id
4974           ,p_dstrbtd_val         => l_dstrbtd_val
4975           ,p_per_in_ler_id       => p_per_in_ler_id
4976           ,p_bpl_cash_recd_val   => l_dummy_number
4977           ,p_bnft_prvdd_ldgr_id  => l_dummy_number
4978           );
4979           hr_utility.set_location( 'l_dstrbtd_val = ' || l_dstrbtd_val, 101);
4980       end if;
4981       --
4982       if l_acty_base_rt_id is not null and l_pool_info.dflt_excs_trtmt_cd is not null then
4983         -- Bug#4473573 - added net credit method condition
4984         if l_dstrbtd_val < 0  and l_pool_info.uses_net_crs_mthd_flag <>'Y' then
4985             -- if it is negative, it gets added because of minus operator
4986            l_dstrbtd_val := 0;
4987         end if;
4988         forfeit_credits(
4989             p_validate            => p_validate,
4990             p_prtt_enrt_rslt_id   => p_prtt_enrt_rslt_id,
4991             p_bnft_prvdr_pool_id  => l_ledger_totals.bnft_prvdr_pool_id,
4992             p_acty_base_rt_id     => l_acty_base_rt_id,
4993             p_prvdd_val           => l_ledger_totals.prvdd_total,
4994             p_rlld_up_val         => 0,
4995             p_used_val            => l_ledger_totals.used_total,
4996             p_rollover_val        => l_rollover_val,
4997             p_cash_val            => l_dstrbtd_val,
4998             p_effective_date      => p_effective_date,
4999             p_business_group_id   => p_business_group_id,
5000             p_frftd_val           => l_ledger_totals.frftd_total,
5001             p_per_in_ler_id       => p_per_in_ler_id,
5002             p_person_id           => p_person_id,
5003             p_enrt_mthd_cd         => p_enrt_mthd_cd
5004           );
5005        end if;
5006       hr_utility.set_location( 'l_dstrbtd_val = ' || l_dstrbtd_val, 102);
5007       hr_utility.set_location( 'l_rollover_val = ' || l_rollover_val, 102);
5008       hr_utility.set_location( 'frftd_total = ' || l_ledger_totals.frftd_total, 102);
5009       --
5010       -- If some are forfeited subtract from credit total
5011       --
5012       l_total_credits:=l_total_credits-l_ledger_totals.frftd_total;
5013       --
5014       hr_utility.set_location(l_proc,110 );
5015     --end if; -- if auto_alct_excs_flag='Y'
5016     --close c_pool_info;
5017     --
5018     if l_pool_info.uses_net_crs_mthd_flag = 'Y' then
5019       --
5020       l_uses_net_crs_mthd := true;
5021       --
5022       -- Check if there is a deficit amount.
5023       --
5024       if l_pool_info.auto_alct_excs_flag = 'Y' then
5025         l_def_exc_amount := l_def_exc_amount + nvl(l_dstrbtd_val,0);
5026       else
5027         l_def_exc_amount := l_def_exc_amount +
5028           (l_ledger_totals.prvdd_total - l_ledger_totals.used_total);
5029       end if;
5030       hr_utility.set_location('l_def_exc_amount: '||l_def_exc_amount,110 );
5031     end if;
5032     --
5033     --  Check if there is a total pool restriction.
5034     --
5035     for l_bpr_rec in c_get_mx_pct_roll_num loop
5036       for l_bpl_rec in c_get_ldgr(l_bpr_rec.acty_base_rt_id) loop
5037          if l_bpl_rec.used_val > ((l_bpr_rec.mx_pct_ttl_crs_cn_roll_num/100)
5038                          * l_ledger_totals.prvdd_total) then
5039           fnd_message.set_name('BEN', 'BEN_92636_TOT_POOL_PCT_EXCD');
5040           fnd_message.raise_error;
5041         end if;
5042       end loop;
5043     end loop;
5044     --
5045   end loop;
5046   close c_ledger_totals;
5047   hr_utility.set_location(l_proc,120 );
5048 
5049   ------Bug 7118730,recalculate the ledger and distribute the credits.
5050   if l_redis_credits then
5051   open c_ledger_totals;
5052     --
5053     fetch c_ledger_totals into l_ledger_totals;
5054     if c_ledger_totals%found then
5055     hr_utility.set_location(l_proc, 30);
5056     hr_utility.set_location(l_proc, 55);
5057     hr_utility.set_location( 'prvdd_total = ' || l_ledger_totals.prvdd_total ||
5058                              'used_total = ' || l_ledger_totals.used_total ||
5059                              'frftd_total = ' || l_ledger_totals.frftd_total ||
5060                              'cash_recd_total = ' || l_ledger_totals.cash_recd_total, 55);
5061   if l_pool_info.dflt_excs_trtmt_cd ='RLOVR_DSTRBT_FRFT' then
5062         hr_utility.set_location(l_proc,90 );
5063         distribute_credits
5064           (p_validate            => p_validate
5065           ,p_prtt_enrt_rslt_id   => p_prtt_enrt_rslt_id
5066           ,p_bnft_prvdr_pool_id  => l_ledger_totals.bnft_prvdr_pool_id
5067           ,p_acty_base_rt_id     => l_acty_base_rt_id
5068           ,p_dflt_excs_trtmt_cd  => l_pool_info.dflt_excs_trtmt_cd
5069           ,p_prvdd_val           => l_ledger_totals.prvdd_total
5070           ,p_rlld_up_val         => 0
5071           ,p_used_val            => l_ledger_totals.used_total
5072           ,p_rollover_val        => l_rollover_val
5073           ,p_cash_recd_total     => l_ledger_totals.cash_recd_total
5074           ,p_val_rndg_cd         => l_pool_info.val_rndg_cd
5075           ,p_val_rndg_rl         => l_pool_info.val_rndg_rl
5076           ,p_pct_rndg_cd         => l_pool_info.pct_rndg_cd
5077           ,p_pct_rndg_rl         => l_pool_info.pct_rndg_rl
5078           ,p_mn_dstrbl_val       => l_pool_info.mn_dstrbl_val
5079           ,p_mn_dstrbl_pct_num   => l_pool_info.mn_dstrbl_pct_num
5080           ,p_mx_dstrbl_val       => l_pool_info.mx_dstrbl_val
5081           ,p_mx_pct              => l_pool_info.mx_dstrbl_pct_num
5082           ,p_person_id           => p_person_id
5083           ,p_enrt_mthd_cd        => p_enrt_mthd_cd
5084           ,p_effective_date      => p_effective_date
5085           ,p_business_group_id   => p_business_group_id
5086           ,p_dstrbtd_val         => l_dstrbtd_val
5087           ,p_per_in_ler_id       => p_per_in_ler_id
5088           ,p_bpl_cash_recd_val   => l_dummy_number
5089           ,p_bnft_prvdd_ldgr_id  => l_dummy_number
5090           );
5091           hr_utility.set_location( 'l_dstrbtd_val = ' || l_dstrbtd_val, 101);
5092       end if;
5093       --
5094       if l_acty_base_rt_id is not null and l_pool_info.dflt_excs_trtmt_cd is not null then
5095         -- Bug#4473573 - added net credit method condition
5096         if l_dstrbtd_val < 0  and l_pool_info.uses_net_crs_mthd_flag <>'Y' then
5097             -- if it is negative, it gets added because of minus operator
5098            l_dstrbtd_val := 0;
5099         end if;
5100         forfeit_credits(
5101             p_validate            => p_validate,
5102             p_prtt_enrt_rslt_id   => p_prtt_enrt_rslt_id,
5103             p_bnft_prvdr_pool_id  => l_ledger_totals.bnft_prvdr_pool_id,
5104             p_acty_base_rt_id     => l_acty_base_rt_id,
5105             p_prvdd_val           => l_ledger_totals.prvdd_total,
5106             p_rlld_up_val         => 0,
5107             p_used_val            => l_ledger_totals.used_total,
5108             p_rollover_val        => l_rollover_val,
5109             p_cash_val            => l_dstrbtd_val,
5110             p_effective_date      => p_effective_date,
5111             p_business_group_id   => p_business_group_id,
5112             p_frftd_val           => l_ledger_totals.frftd_total,
5113             p_per_in_ler_id       => p_per_in_ler_id,
5114             p_person_id           => p_person_id,
5115             p_enrt_mthd_cd         => p_enrt_mthd_cd
5116           );
5117        end if;
5118       hr_utility.set_location( 'l_dstrbtd_val = ' || l_dstrbtd_val, 102);
5119       hr_utility.set_location( 'l_rollover_val = ' || l_rollover_val, 102);
5120       hr_utility.set_location( 'frftd_total = ' || l_ledger_totals.frftd_total, 102);
5121       hr_utility.set_location( 'Re-distributed credits', 102);
5122   end if;
5123   close c_ledger_totals;
5124    end if;
5125   ------Bug 7118730
5126   --
5127   -- create/update the result row and create prv
5128   --
5129   if p_prtt_enrt_rslt_id is not null then
5130     --
5131     --  If one of the pools is using the net credit method.
5132     --
5133     if l_uses_net_crs_mthd then
5134       --
5135       --  Write an enrollment result for the excess/deficit amount.
5136       --
5137       hr_utility.set_location('l_def_exc_amount'||l_def_exc_amount,110 );
5138       hr_utility.set_location('prvdd_total'||l_ledger_totals.prvdd_total,110 );
5139       hr_utility.set_location('used_total'||l_ledger_totals.used_total,110 );
5140       create_flex_credit_enrolment
5141        (p_person_id             => p_person_id,
5142         p_enrt_mthd_cd          => p_enrt_mthd_cd,
5143         p_business_group_id     => p_business_group_id,
5144         p_effective_date        => p_effective_date,
5145         p_prtt_enrt_rslt_id     => p_prtt_enrt_rslt_id,
5146         p_prtt_rt_val_id        => p_prtt_rt_val_id,
5147         p_per_in_ler_id         => p_per_in_ler_id,
5148         p_rt_val                => null,
5149         p_net_credit_val        => l_def_exc_amount,
5150         p_pgm_id                => p_pgm_id
5151        );
5152     end if;
5153     --
5154     hr_utility.set_location('calling create_flex,total_credits='||to_char(l_total_credits),130);
5155     create_flex_credit_enrolment(
5156         p_person_id             => p_person_id,
5157         p_enrt_mthd_cd          => p_enrt_mthd_cd,
5158         p_business_group_id     => p_business_group_id,
5159         p_effective_date        => p_effective_date,
5160         p_prtt_enrt_rslt_id     => p_prtt_enrt_rslt_id,
5161         p_prtt_rt_val_id        => p_prtt_rt_val_id,
5162         p_per_in_ler_id         => p_per_in_ler_id,
5163         p_rt_val                => l_total_credits,
5164         p_pgm_id                => p_pgm_id
5165     );
5166     hr_utility.set_location(l_proc, 140);
5167     open c_element_details;
5168     fetch c_element_details into
5169         p_acty_ref_perd_cd,
5170         p_acty_base_rt_id,
5171         p_rt_strt_dt,
5172         p_rt_val,
5173         p_element_type_id
5174     ;
5175     close c_element_details;
5176   end if;
5177   --
5178   hr_utility.set_location('Leaving:'||l_proc, 999);
5179   --
5180 end total_pools;
5181 --
5182 --------------------------------------------------------------------------------
5183 --                      update_rate
5184 --------------------------------------------------------------------------------
5185 procedure update_rate(p_prtt_rt_val_id      in out nocopy number,
5186                       p_val                 in  number,
5187                       p_prtt_enrt_rslt_id   in  number,
5188                       p_ended_per_in_ler_id in  number,
5189                       p_effective_date      in  date,
5190                       p_business_group_id   in  number) is
5191    --
5192    cursor c_prv is
5193       select prv.*
5194       from   ben_prtt_rt_val prv
5195       where  prv.prtt_rt_val_id = p_prtt_rt_val_id
5196       and    prv.prtt_rt_val_stat_cd is null;
5197    --
5198    l_prv_rec     c_prv%rowtype;
5199    --
5200    cursor c_pen is
5201       select pen.pgm_id,
5202              pen.pl_id,
5203              pen.oipl_id,
5204              pen.person_id,
5205              pen.ler_id
5206       from   ben_prtt_enrt_rslt_f pen
5207       where  pen.prtt_enrt_rslt_id = p_prtt_enrt_rslt_id
5208       and    pen.business_group_id  = p_business_group_id
5209       and    pen.prtt_enrt_rslt_stat_cd is null
5210       and    p_effective_date between
5211              pen.effective_start_date and pen.effective_end_date;
5212    --
5213    l_pen_rec  c_pen%rowtype;
5214    --
5215    cursor c_abr is
5216       select abr.input_value_id,
5217              abr.element_type_id
5218       from   ben_acty_base_rt_f abr
5219       where  abr.acty_base_rt_id = l_prv_rec.acty_base_rt_id
5220       and    abr.business_group_id  = p_business_group_id
5221       and    p_effective_date between
5222              abr.effective_start_date and abr.effective_end_date;
5223    --
5224    l_abr_rec   c_abr%rowtype;
5225    --
5226    cursor c_ecr is
5227       select nvl(ecr1.enrt_rt_id, ecr2.enrt_rt_id) enrt_rt_id
5228       from   ben_enrt_rt ecr1,
5229              ben_enrt_rt ecr2,
5230              ben_elig_per_elctbl_chc epe,
5231              ben_enrt_bnft           enb
5232       where  epe.per_in_ler_id = p_ended_per_in_ler_id
5233       and    ecr1.prtt_rt_val_id(+) = p_prtt_rt_val_id
5234       and    ecr2.prtt_rt_val_id(+) = p_prtt_rt_val_id
5235       and    epe.elig_per_elctbl_chc_id = ecr1.elig_per_elctbl_chc_id(+)
5236       and    epe.elig_per_elctbl_chc_id = enb.elig_per_elctbl_chc_id (+)
5237       and    enb.enrt_bnft_id = ecr2.enrt_bnft_id(+);
5238    --
5239    -- Bug 2386000
5240    CURSOR c_lee_rsn_for_plan (c_ler_id number, c_pl_id number ) IS
5241       SELECT   leer.lee_rsn_id
5242       FROM     ben_lee_rsn_f leer,
5243                ben_popl_enrt_typ_cycl_f petc
5244       WHERE    leer.ler_id            = c_ler_id
5245       AND      leer.business_group_id = p_business_group_id
5246       AND      p_effective_date BETWEEN leer.effective_start_date
5247                    AND leer.effective_end_date
5248       AND      leer.popl_enrt_typ_cycl_id = petc.popl_enrt_typ_cycl_id
5249       AND      petc.pl_id                 = c_pl_id
5250       AND      petc.enrt_typ_cycl_cd = 'L'                        -- life event
5251       AND      petc.business_group_id = p_business_group_id
5252       AND      p_effective_date BETWEEN petc.effective_start_date
5253                    AND petc.effective_end_date;
5254    --
5255    CURSOR c_lee_rsn_for_program (c_ler_id number, c_pgm_id number )IS
5256       SELECT   leer.lee_rsn_id
5257       FROM     ben_lee_rsn_f leer,
5258                ben_popl_enrt_typ_cycl_f petc
5259       WHERE    leer.ler_id            = c_ler_id
5260       AND      leer.business_group_id = p_business_group_id
5261       AND      p_effective_date BETWEEN leer.effective_start_date
5262                    AND leer.effective_end_date
5263       AND      leer.popl_enrt_typ_cycl_id = petc.popl_enrt_typ_cycl_id
5264       AND      petc.pgm_id                = c_pgm_id
5265       AND      petc.enrt_typ_cycl_cd      = 'L'
5266       AND      petc.business_group_id     = p_business_group_id
5267       AND      p_effective_date BETWEEN petc.effective_start_date
5268                    AND petc.effective_end_date;
5269    --
5270    cursor c_pil is
5271      select ler_id
5272      from ben_per_in_ler pil
5273      where pil.per_in_ler_id = p_ended_per_in_ler_id;
5274    --
5275    l_lee_rsn_id                number := null ;
5276    --
5277    l_enrt_cvg_strt_dt          date;
5278    l_enrt_cvg_strt_dt_cd       varchar2(30);
5279    l_enrt_cvg_strt_dt_rl       number;
5280    l_rt_strt_dt                date;
5281    l_rt_strt_dt_cd             varchar2(30);
5282    l_rt_strt_dt_rl             number;
5283    l_enrt_cvg_end_dt           date;
5284    l_enrt_cvg_end_dt_cd        varchar2(30);
5285    l_enrt_cvg_end_dt_rl        number;
5286    l_rt_end_dt                 date;
5287    l_rt_end_dt_cd              varchar2(30);
5288    l_rt_end_dt_rl              number;
5289    l_enrt_rt_id                number;
5290    l_ler_id                    number;
5291    --
5292   l_proc varchar2(80) := g_package||'.update_rate';
5293    --
5294 begin
5295    --
5296    hr_utility.set_location('Entering '||l_proc,5);
5297    --
5298    open  c_prv;
5299    fetch c_prv into l_prv_rec;
5300    close c_prv;
5301    --
5302    open  c_pen;
5303    fetch c_pen into l_pen_rec;
5304    close c_pen;
5305    --
5306    open  c_abr;
5307    fetch c_abr into l_abr_rec;
5308    close c_abr;
5309    --
5310    open  c_ecr;
5311    fetch c_ecr into l_enrt_rt_id;
5312    close c_ecr;
5313    --
5314    --  during ineligible process the ler_id is not updated on Shell plan if the update
5315    --  rate is called before shell plan deenrollment and therefore it is
5316    --  better to take the ler_id from ending per_in_ler_id so that the rate end date
5317    --  is calculated correctly
5318    open c_pil;
5319    fetch c_pil into l_ler_id;
5320    close c_pil;
5321    --
5322    open c_lee_rsn_for_plan(l_ler_id, l_pen_rec.pl_id );
5323    fetch c_lee_rsn_for_plan into l_lee_rsn_id ;
5324    close c_lee_rsn_for_plan ;
5325    --
5326    if l_lee_rsn_id is null and l_pen_rec.pgm_id is not null then
5327      open c_lee_rsn_for_program(l_ler_id, l_pen_rec.pgm_id);
5328      fetch c_lee_rsn_for_program into l_lee_rsn_id ;
5329      close c_lee_rsn_for_program ;
5330    end if;
5331    --
5332    hr_utility.set_location(l_proc,10);
5333    --
5334    ben_determine_date.rate_and_coverage_dates(
5335          p_which_dates_cd          => 'R',
5336          p_pgm_id                  => l_pen_rec.pgm_id,
5337          p_pl_id                   => l_pen_rec.pl_id,
5338          p_oipl_id                 => l_pen_rec.oipl_id,
5339          p_per_in_ler_id           => p_ended_per_in_ler_id,
5340          p_lee_rsn_id              => l_lee_rsn_id,
5341          p_person_id               => l_pen_rec.person_id,
5342          p_business_group_id       => p_business_group_id,
5343          p_enrt_cvg_strt_dt        => l_enrt_cvg_strt_dt,
5344          p_enrt_cvg_strt_dt_cd     => l_enrt_cvg_strt_dt_cd,
5345          p_enrt_cvg_strt_dt_rl     => l_enrt_cvg_strt_dt_rl,
5346          p_rt_strt_dt              => l_rt_strt_dt,
5347          p_rt_strt_dt_cd           => l_rt_strt_dt_cd,
5348          p_rt_strt_dt_rl           => l_rt_strt_dt_rl,
5349          p_enrt_cvg_end_dt         => l_enrt_cvg_end_dt,
5350          p_enrt_cvg_end_dt_cd      => l_enrt_cvg_end_dt_cd,
5351          p_enrt_cvg_end_dt_rl      => l_enrt_cvg_end_dt_rl,
5352          p_rt_end_dt               => l_rt_end_dt,
5353          p_rt_end_dt_cd            => l_rt_end_dt_cd,
5354          p_rt_end_dt_rl            => l_rt_end_dt_rl,
5355          p_effective_date          => p_effective_date
5356           );
5357    --
5358    hr_utility.set_location(l_proc,15);
5359    --
5360    if l_prv_rec.rt_strt_dt >= l_rt_strt_dt then
5361       --
5362       ben_prtt_rt_val_api.delete_prtt_rt_val(
5363           p_prtt_rt_val_id                => p_prtt_rt_val_id
5364          ,p_enrt_rt_id                    => l_enrt_rt_id
5365          ,p_person_id                     => l_pen_rec.person_id
5366          ,p_business_group_id             => p_business_group_id
5367          ,p_object_version_number         => l_prv_rec.object_version_number
5368          ,p_effective_date                => p_effective_date
5369        );
5370       --
5371    else
5372       --
5373       ben_prtt_rt_val_api.update_prtt_rt_val(
5374           p_prtt_rt_val_id                => p_prtt_rt_val_id
5375          ,p_rt_end_dt                     => l_rt_end_dt
5376          ,p_ended_per_in_ler_id           => p_ended_per_in_ler_id
5377          ,p_acty_base_rt_id               => l_prv_rec.acty_base_rt_id
5378          ,p_input_value_id                => l_abr_rec.input_value_id
5379          ,p_element_type_id               => l_abr_rec.element_type_id
5380          ,p_person_id                     => l_pen_rec.person_id
5381          ,p_business_group_id             => p_business_group_id
5382          ,p_object_version_number         => l_prv_rec.object_version_number
5383          ,p_effective_date                => p_effective_date
5384    );
5385    end if;
5386    --
5387    hr_utility.set_location(l_proc,20);
5388    --
5389    -- Bug#2441871
5390    if l_enrt_rt_id is not null then
5391      --
5392        ben_prtt_rt_val_api.create_prtt_rt_val(
5393           p_prtt_rt_val_id                 => p_prtt_rt_val_id
5394          ,p_enrt_rt_id                     => l_enrt_rt_id
5395          ,p_per_in_ler_id                  => p_ended_per_in_ler_id
5396          ,p_rt_typ_cd                      => l_prv_rec.rt_typ_cd
5397          ,p_tx_typ_cd                      => l_prv_rec.tx_typ_cd
5398          ,p_acty_typ_cd                    => l_prv_rec.acty_typ_cd
5399          ,p_mlt_cd                         => l_prv_rec.mlt_cd
5400          ,p_acty_ref_perd_cd               => l_prv_rec.acty_ref_perd_cd
5401          ,p_rt_val                         => p_val
5402          ,p_rt_strt_dt                     => l_rt_strt_dt
5403          ,p_rt_end_dt                      => hr_api.g_eot
5404          ,p_ann_rt_val                     => null
5405          ,p_bnft_rt_typ_cd                 => l_prv_rec.bnft_rt_typ_cd
5406          ,p_cmcd_ref_perd_cd               => l_prv_rec.cmcd_ref_perd_cd
5407          ,p_cmcd_rt_val                    => p_val
5408          ,p_dsply_on_enrt_flag             => l_prv_rec.dsply_on_enrt_flag
5409          ,p_elctns_made_dt                 => p_effective_date
5410          ,p_cvg_amt_calc_mthd_id           => l_prv_rec.cvg_amt_calc_mthd_id
5411          ,p_actl_prem_id                   => l_prv_rec.actl_prem_id
5412          ,p_comp_lvl_fctr_id               => l_prv_rec.comp_lvl_fctr_id
5413          ,p_prtt_enrt_rslt_id              => p_prtt_enrt_rslt_id
5414          ,p_business_group_id              => p_business_group_id
5415          ,p_object_version_number          => l_prv_rec.object_version_number
5416          ,p_effective_date                 => p_effective_date
5417          ,p_acty_base_rt_id                => l_prv_rec.acty_base_rt_id
5418          ,p_input_value_id                 => l_abr_rec.input_value_id
5419          ,p_element_type_id                => l_abr_rec.element_type_id
5420          ,p_person_id                      => l_pen_rec.person_id
5421          ,p_ordr_num               => l_prv_rec.ordr_num
5422            );
5423       --
5424    End if;
5425    --
5426    hr_utility.set_location('Leaving '||l_proc,5);
5427    --
5428 end update_rate;
5429 --
5430 --------------------------------------------------------------------------------
5431 --                      delete_all_ledgers
5432 --------------------------------------------------------------------------------
5433 procedure delete_all_ledgers(p_bnft_prvdr_pool_id in number,
5434                              p_flex_rslt_id       in number,
5435                              p_person_id          in number,
5436                              p_per_in_ler_id      in number,
5437                              p_effective_date     in date,
5438                              p_business_group_id  in number) is
5439    --
5440    -- All ledgers for the pool.
5441    --
5442    cursor c_bpl is
5443       select bpl.bnft_prvdd_ldgr_id,
5444              bpl.object_version_number,
5445              bpl.effective_start_date,
5446              bpl.acty_base_rt_id,
5447              bpl.prtt_ro_of_unusd_amt_flag,
5448              bpl.used_val
5449       from   ben_bnft_prvdd_ldgr_f bpl,
5450              ben_per_in_ler pil
5451       where  bpl.bnft_prvdr_pool_id = p_bnft_prvdr_pool_id
5452       and    bpl.prtt_enrt_rslt_id = p_flex_rslt_id
5453       and    bpl.business_group_id  = p_business_group_id
5454       and    p_effective_date between
5455              bpl.effective_start_date and bpl.effective_end_date
5456 and pil.per_in_ler_id=bpl.per_in_ler_id
5457 and pil.business_group_id=bpl.business_group_id
5458 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
5459 order by prtt_ro_of_unusd_amt_flag
5460 /* Bug 5344961 - Added the above order by clause. This is because if a ROLLOVER (prtt_ro_of_unusd_amt_flag=Y)
5461 BPL row is picked up earlier, we will delete the corresponding enrollment. This in a typical scenario (see bug)
5462 will delete all provided BPL rows in that program. So if cursor C_BPL then tries to delete such BPL row
5463 we will get an error. This typical scenario is when the rollover plan is same as the plan that has
5464 used flex credits */
5465 ;
5466    --
5467    -- Result record for rollover ledgers.
5468    --
5469    cursor c_pen(v_acty_base_rt_id in number) is
5470       select pen.prtt_enrt_rslt_id,
5471              pen.object_version_number,
5472              pen.effective_start_date,
5473              abr.entr_val_at_enrt_flag,
5474              prv.prtt_rt_val_id,
5475              prv.rt_val
5476       from   ben_prtt_enrt_rslt_f pen,
5477              ben_prtt_rt_val      prv,
5478              ben_acty_base_rt_f   abr
5479       where  prv.acty_base_rt_id = v_acty_base_rt_id
5480       and    prv.business_group_id  = p_business_group_id
5481       and    prv.prtt_rt_val_stat_cd is null
5482       and    prv.prtt_enrt_rslt_id = pen.prtt_enrt_rslt_id
5483       and    pen.person_id = p_person_id
5484       and    pen.prtt_enrt_rslt_stat_cd is null
5485       and    pen.enrt_cvg_thru_dt = hr_api.g_eot
5486       and    p_effective_date between
5487              pen.effective_start_date and pen.effective_end_date
5488       and    prv.acty_base_rt_id = abr.acty_base_rt_id
5489       and    p_effective_date between
5490              abr.effective_start_date and abr.effective_end_date;
5491    --
5492    l_effective_start_date     date;
5493    l_effective_end_date       date;
5494    l_effective_date           date;
5495    l_datetrack_mode           varchar2(30);
5496    l_prtt_enrt_rslt_id        number;
5497    l_pen_object_version_number number;
5498    l_pen_effective_start_date  date;
5499    l_entr_val_at_enrt_flag     varchar2(30);
5500    l_prtt_rt_val_id            number;
5501    l_rt_val                    number;
5502    --
5503   l_proc varchar2(80) := g_package||'.delete_all_ledgers';
5504    --
5505 begin
5506    --
5507    hr_utility.set_location('Enetering '||l_proc, 5);
5508    for l_bpl_rec in c_bpl loop
5509       --
5510       hr_utility.set_location(l_proc,10);
5511       if p_effective_date = l_bpl_rec.effective_start_date then
5512          --
5513          l_datetrack_mode := hr_api.g_zap;
5514          l_effective_date := p_effective_date;
5515          --
5516       else
5517          --
5518          l_datetrack_mode := hr_api.g_delete;
5519          l_effective_date := p_effective_date -1;
5520          --
5521       end if;
5522       --
5523       ben_Benefit_Prvdd_Ledger_api.delete_Benefit_Prvdd_Ledger(
5524            p_validate              => false,
5525            p_bnft_prvdd_ldgr_id    => l_bpl_rec.bnft_prvdd_ldgr_id,
5526            p_effective_start_date  => l_effective_start_date,
5527            p_effective_end_date    => l_effective_end_date,
5528            p_object_version_number => l_bpl_rec.object_version_number,
5529            p_effective_date        => l_effective_date,
5530            p_datetrack_mode        => l_datetrack_mode,
5531            p_business_group_id     => p_business_group_id,
5532            p_process_enrt_flag => 'N');
5533       --
5534       -- Check whether rollovers are attached.
5535       --
5536       hr_utility.set_location(l_proc,15);
5537       if l_bpl_rec.prtt_ro_of_unusd_amt_flag = 'Y' then
5538          --
5539          hr_utility.set_location(l_proc,20);
5540          open  c_pen(l_bpl_rec.acty_base_rt_id);
5541          fetch c_pen into l_prtt_enrt_rslt_id,
5542                           l_pen_object_version_number,
5543                           l_pen_effective_start_date,
5544                           l_entr_val_at_enrt_flag,
5545                           l_prtt_rt_val_id,
5546                           l_rt_val;
5547          close c_pen;
5548          --
5549          -- If enter value at enrollment, we need to keep the
5550          -- enrollment. Just reduce the rate by the rollover amount.
5551          --
5552          if l_entr_val_at_enrt_flag = 'Y' then
5553             --
5554             update_rate(p_prtt_rt_val_id    => l_prtt_rt_val_id,
5555                         p_val        => (l_rt_val-l_bpl_rec.used_val),
5556                         p_prtt_enrt_rslt_id => l_prtt_enrt_rslt_id,
5557                         p_ended_per_in_ler_id => p_per_in_ler_id,
5558                         p_effective_date => p_effective_date,
5559                         p_business_group_id => p_business_group_id);
5560             --
5561          elsif l_prtt_enrt_rslt_id is  not null then
5562             --
5563             -- If enter value at enrollment flag is off, delete the
5564             -- enrollment.
5565             --
5566             if p_effective_date = l_pen_effective_start_date then
5567                --
5568                l_datetrack_mode := hr_api.g_zap;
5569                --
5570             else
5571                --
5572                l_datetrack_mode := hr_api.g_delete;
5573                --
5574             end if;
5575             --
5576             ben_prtt_enrt_result_api.delete_enrollment(
5577                               p_prtt_enrt_rslt_id  => l_prtt_enrt_rslt_id,
5578                               p_per_in_ler_id      => p_per_in_ler_id,
5579                               p_object_version_number => l_pen_object_version_number,
5580                               p_effective_start_date => l_effective_start_date,
5581                               p_effective_end_date   => l_effective_end_date,
5582                               p_effective_date       => p_effective_date,
5583                               p_business_group_id    => p_business_group_id,
5584                               p_datetrack_mode       => l_datetrack_mode,
5585                               p_source               => 'benpstcr');
5586             --
5587          end if;
5588          --
5589       end if;
5590       --
5591    end loop;
5592    --
5593    hr_utility.set_location('Leaving '||l_proc,5);
5594    --
5595 end delete_all_ledgers;
5596 --
5597 --------------------------------------------------------------------------------
5598 --                      compute_rollovers
5599 --------------------------------------------------------------------------------
5600 procedure compute_rollovers
5601   (p_calculate_only_mode in     boolean default false
5602   ,p_bnft_prvdr_pool_id  in     number
5603   ,p_flex_rslt_id        in     number
5604   ,p_person_id           in     number
5605   ,p_per_in_ler_id       in     number
5606   ,p_enrt_mthd_cd        in     varchar2
5607   ,p_effective_date      in     date
5608   ,p_business_group_id   in     number
5609   ,p_pct_rndg_cd         in     varchar2
5610   ,p_pct_rndg_rl         in     number
5611   ,p_dflt_excs_trtmt_cd  in     varchar2
5612   ,p_rollover_val           out nocopy number
5613   )
5614 is
5615   l_datetrack_mode varchar2(30);
5616   l_effective_end_date date;
5617   l_object_version_number number;
5618   l_effective_start_date date;
5619   l_prvdd_val number;
5620   l_recd_val number;
5621   l_used_val number;
5622   l_acty_base_rt_id number;
5623   l_balance number;
5624   l_mn_dstrbl_pct_num number;
5625   l_mx_dstrbl_pct_num number;
5626   l_old_rlovr_val number;
5627   l_cash_val number;
5628   l_bnft_prvdd_ldgr_id number;
5629   l_rld_ovr_val number;
5630   l_rollover_diff_total number;
5631   l_prtt_enrt_rslt_id   number;
5632   l_prtt_rt_val_id      number;
5633   l_rt_val              number;
5634   l_qualify_flag varchar2(30);
5635   l_outputs     ff_exec.outputs_t;
5636   --
5637   -- Query will get the rollover and enrollment information
5638   --
5639   cursor c_rollovers is
5640     select
5641         prr.mn_rlovr_pct_num,
5642         prr.mn_rlovr_val,
5643         prr.mx_rchd_dflt_ordr_num,
5644         prr.mx_rlovr_pct_num,
5645         prr.mx_rlovr_val,
5646         prr.pct_rlovr_incrmt_num,
5647         prr.pct_rndg_cd,
5648         prr.pct_rndg_rl,
5649         prr.rlovr_val_incrmt_num,
5650         prr.rlovr_val_rl,
5651         prr.val_rndg_cd,
5652         prr.val_rndg_rl,
5653         prr.acty_base_rt_id,
5654         rslt.prtt_enrt_rslt_id,
5655         prv.rt_val,
5656         prv.prtt_rt_val_id,
5657         prr.prtt_elig_rlovr_rl,
5658         asg.assignment_id,
5659         asg.organization_id,
5660         loc.region_2,
5661         oipl.opt_id,
5662         rslt.pl_id,
5663         rslt.pgm_id,
5664         rslt.ler_id,
5665         rslt.pl_typ_id
5666     from
5667         ben_bnft_pool_rlovr_rqmt_f prr,
5668         ben_prtt_rt_val prv,
5669         ben_prtt_enrt_rslt_f rslt,
5670         per_all_assignments_f asg,
5671         hr_locations_all loc,
5672         ben_oipl_f oipl
5673     where
5674         prr.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
5675         prr.business_group_id=p_business_group_id and
5676         p_effective_date between
5677         prr.effective_start_date and prr.effective_end_date and
5678         prv.acty_base_rt_id = prr.acty_base_rt_id and
5679         prv.prtt_enrt_rslt_id = rslt.prtt_enrt_rslt_id  and
5680         rslt.person_id=p_person_id and
5681         rslt.business_group_id=p_business_group_id and
5682         rslt.prtt_enrt_rslt_stat_cd is null and
5683         rslt.enrt_cvg_thru_dt = hr_api.g_eot and
5684         prv.business_group_id =p_business_group_id and
5685         prv.prtt_rt_val_stat_cd is null and
5686         p_effective_date between
5687         rslt.effective_start_date and rslt.effective_end_date and
5688         asg.person_id=rslt.person_id and
5689         asg.assignment_type <> 'C'and
5690         asg.primary_flag='Y' and
5691         asg.location_id = loc.location_id(+) and
5692         p_effective_date between
5693         asg.effective_start_date and asg.effective_end_date and
5694         oipl.oipl_id(+)=rslt.oipl_id and
5695         p_effective_date between
5696         oipl.effective_start_date(+) and oipl.effective_end_date(+) and
5697         oipl.business_group_id(+)=p_business_group_id
5698     order by prr.mx_rchd_dflt_ordr_num
5699     ;
5700   --
5701   -- Ledger totals for the pool.
5702   --
5703   cursor c_ledger_totals is
5704     select
5705                 nvl(sum(bpl.prvdd_val),0),
5706                 nvl(sum(decode(bpl.prtt_ro_of_unusd_amt_flag,
5707                                'N',bpl.used_val,
5708                                 0)),0),-- non rollovers
5709                 nvl(sum(bpl.cash_recd_val),0),
5710                 nvl(sum(decode(bpl.prtt_ro_of_unusd_amt_flag,
5711                                'Y',bpl.used_val,
5712                                 0)),0) -- rollovers
5713     from        ben_bnft_prvdd_ldgr_f bpl,
5714                 ben_per_in_ler pil
5715     where       p_effective_date between
5716                         bpl.effective_start_date and bpl.effective_end_date and
5717                 bpl.business_group_id=p_business_group_id and
5718                 bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
5719                 bpl.prtt_enrt_rslt_id = p_flex_rslt_id and
5720                 -- exclude the rollover for this abr
5721                 (bpl.acty_base_rt_id<>l_acty_base_rt_id or
5722                 -- but include the used amounts for this abr
5723                 bpl.prtt_ro_of_unusd_amt_flag='N')
5724 and pil.per_in_ler_id=bpl.per_in_ler_id
5725 and pil.business_group_id=bpl.business_group_id
5726 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
5727      group by bpl.bnft_prvdr_pool_id;
5728   --
5729   -- get the ledger details for rollover.
5730   --
5731   cursor c_old_ledger is
5732     select
5733                 bnft_prvdd_ldgr_id,
5734                 bpl.used_val,
5735                 bpl.object_version_number,
5736                 bpl.effective_start_date
5737     from        ben_bnft_prvdd_ldgr_f bpl,
5738                 ben_per_in_ler pil
5739     where       p_effective_date between
5740                 bpl.effective_start_date and bpl.effective_end_date and
5741                 bpl.business_group_id=p_business_group_id and
5742                 bpl.bnft_prvdr_pool_id=p_bnft_prvdr_pool_id and
5743                 bpl.prtt_enrt_rslt_id = p_flex_rslt_id and
5744                 bpl.acty_base_rt_id = l_acty_base_rt_id and
5745                 bpl.prtt_ro_of_unusd_amt_flag = 'Y'
5746 and pil.per_in_ler_id=bpl.per_in_ler_id
5747 and pil.business_group_id=bpl.business_group_id
5748 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
5749 ;
5750   --
5751   -- Rate record to update, if the rollover amount has changed.
5752   --
5753   cursor c_prv (c_per_in_ler_id number) is -- 5257226: added parameter
5754      select prv.prtt_rt_val_id,
5755             prv.rt_val,
5756             prv.rt_strt_dt
5757      from   ben_prtt_rt_val prv -- ,ben_prtt_enrt_rslt_f pen
5758      where  prv.prtt_enrt_rslt_id = l_prtt_enrt_rslt_id
5759      and    prv.acty_base_rt_id = l_acty_base_rt_id
5760      and    prv.prtt_rt_val_stat_cd is null
5761      and    prv.business_group_id  = p_business_group_id
5762      and    (prv.per_in_ler_id = c_per_in_ler_id  or c_per_in_ler_id = -1 ) -- bug#5136668 -only for that lf event
5763      order by prv.rt_strt_dt desc;
5764   --
5765   cursor c_epe is
5766      select elig_per_elctbl_chc_id
5767      from ben_elig_per_elctbl_chc
5768      where prtt_enrt_rslt_id = l_prtt_enrt_rslt_id;
5769   --
5770   l_elig_per_elctbl_chc_id number;
5771   l_proc                 varchar2(80) := g_package||'.compute_rollovers';
5772   l_jurisdiction_code    varchar2(30);
5773   l_dummy                varchar2(80);
5774   l_rt_strt_dt           date;
5775    --
5776 begin
5777   hr_utility.set_location('Entering:'|| l_proc, 5);
5778   --
5779   -- Running total for the rollover.
5780   --
5781   l_rollover_diff_total:=0;
5782   --
5783   for l_rollover in c_rollovers loop
5784     --
5785     l_prtt_enrt_rslt_id := l_rollover.prtt_enrt_rslt_id;
5786     --
5787     -- If rule exists run if and see if qualifies for rollover
5788     --
5789     if l_rollover.prtt_elig_rlovr_rl is not null then
5790       --
5791       -- execute rule
5792       --
5793       -- Get the l_elig_per_elctbl_chc_id associated with the result
5794       -- to be passed to formula as context.
5795       --
5796       open c_epe;
5797       fetch c_epe into l_elig_per_elctbl_chc_id;
5798       close c_epe;
5799 
5800   --    if l_rollover.region_2 is not null then
5801   --          pay_mag_utils.lookup_jurisdiction_code
5802   --           (p_state => l_rollover.region_2);
5803   --    end if;
5804 
5805       l_outputs := benutils.formula
5806             (p_formula_id           => l_rollover.prtt_elig_rlovr_rl,
5807              p_assignment_id        => l_rollover.assignment_id,
5808              p_organization_id      => l_rollover.organization_id,
5809              p_business_group_id    => p_business_group_id,
5810              p_effective_date       => p_effective_date,
5811              p_opt_id               => l_rollover.opt_id,
5812              p_pl_id                => l_rollover.pl_id,
5813              p_pgm_id               => l_rollover.pgm_id,
5814              p_ler_id               => l_rollover.ler_id,
5815              p_pl_typ_id            => l_rollover.pl_typ_id,
5816              p_elig_per_elctbl_chc_id            => l_elig_per_elctbl_chc_id,
5817              p_jurisdiction_code    => l_jurisdiction_code);
5818 
5819       l_qualify_flag := l_outputs(l_outputs.first).value;
5820     else
5821       l_qualify_flag:='Y';
5822     end if;
5823     --
5824     if l_qualify_flag='Y' then
5825       --
5826       -- Process this rollover
5827       --
5828       hr_utility.set_location(l_proc, 20);
5829       l_acty_base_rt_id:=l_rollover.acty_base_rt_id;
5830       --
5831       -- Get the ledger totals with exclusions (see cursor)
5832       --
5833       open c_ledger_totals;
5834       fetch c_ledger_totals into
5835           l_prvdd_val,
5836           l_used_val,
5837           l_recd_val,
5838           l_rld_ovr_val;
5839       close c_ledger_totals;
5840       --
5841       -- depending on the excess treatment code compute the balance
5842       --
5843       if p_dflt_excs_trtmt_cd in ('DSTRBT_ALL','DSTRBT_RLOVR_FRFT') then
5844         hr_utility.set_location(l_proc, 30);
5845         l_balance:=l_prvdd_val-l_used_val-l_recd_val-l_rld_ovr_val;
5846       else
5847         hr_utility.set_location(l_proc, 40);
5848         l_balance:=l_prvdd_val-l_used_val-l_rld_ovr_val;
5849       end if;
5850       hr_utility.set_location(l_proc, 50);
5851       --
5852       -- round the balance
5853       --
5854       if l_rollover.val_rndg_cd is not null then
5855         l_balance:=benutils.do_rounding(
5856           p_rounding_cd    => l_rollover.val_rndg_cd,
5857           p_rounding_rl    => l_rollover.val_rndg_rl,
5858           p_value          => l_balance,
5859           p_effective_date => p_effective_date);
5860       end if;
5861       --
5862       -- set value to less than the maximums
5863       -- do this before minimums since if any max is less than any minimum should skip it.
5864       --
5865       -- set balance to max pct if defined and < balance
5866       --
5867       if l_rollover.mx_rlovr_pct_num is not null then
5868         l_mx_dstrbl_pct_num:=(l_prvdd_val-l_used_val)*l_rollover.mx_rlovr_pct_num/100;
5869         if p_pct_rndg_cd is not null then
5870           l_mx_dstrbl_pct_num:=benutils.do_rounding(
5871             p_rounding_cd    => p_pct_rndg_cd,
5872             p_rounding_rl    => p_pct_rndg_rl,
5873             p_value          => l_mx_dstrbl_pct_num,
5874             p_effective_date => p_effective_date);
5875         end if;
5876         if l_balance > l_mx_dstrbl_pct_num then
5877           l_balance:=l_mx_dstrbl_pct_num;
5878         end if;
5879       end if;
5880       --
5881       -- Set balance to max amount if define and > balance
5882       --
5883       if l_rollover.mx_rlovr_val is not null and
5884          l_balance > l_rollover.mx_rlovr_val then
5885         l_balance:=l_rollover.mx_rlovr_val;
5886       end if;
5887             -- Check the rlover rule - this is a max val rule.
5888       if l_rollover.rlovr_val_rl is not null then
5889           run_rule
5890             (p_effective_date     => p_effective_date
5891             ,p_person_id          => p_person_id
5892             ,p_rlovr_val_rl       => l_rollover.rlovr_val_rl
5893             ,p_business_group_id  => p_business_group_id
5894             ,p_ler_id             => l_rollover.ler_id
5895             ,p_bnft_prvdr_pool_id => p_bnft_prvdr_pool_id
5896             ,p_dflt_excs_trtmt_cd => l_dummy  -- output
5897             ,p_elig_per_elctbl_chc_id            => l_elig_per_elctbl_chc_id
5898             ,p_mx_val             => l_rollover.mx_rlovr_val); -- output
5899 
5900          if l_rollover.mx_rlovr_val is not null and
5901             l_balance > l_rollover.mx_rlovr_val then
5902             l_balance:=l_rollover.mx_rlovr_val;
5903          end if;
5904       end if;
5905 
5906       --
5907       -- compute the min pct value
5908       --
5909       l_mn_dstrbl_pct_num:=(l_prvdd_val-l_used_val)*l_rollover.mn_rlovr_pct_num/100;
5910       if p_pct_rndg_cd is not null then
5911         l_mn_dstrbl_pct_num:=benutils.do_rounding(
5912           p_rounding_cd    => p_pct_rndg_cd,
5913           p_rounding_rl    => p_pct_rndg_rl,
5914           p_value          => l_mn_dstrbl_pct_num,
5915           p_effective_date => p_effective_date);
5916       end if;
5917       --
5918       -- if less than mimimums cannot rollover, skip it.
5919       --
5920       if (l_balance < l_rollover.mn_rlovr_val or
5921           l_balance < l_mn_dstrbl_pct_num or
5922           l_balance <=0 ) then
5923         hr_utility.set_location('Balance less than minimum or zero',60);
5924         --
5925         -- nothing to do
5926         --
5927         null;
5928         --
5929       else
5930         hr_utility.set_location(l_proc, 70);
5931         --
5932         open c_old_ledger;
5933         fetch c_old_ledger into
5934           l_bnft_prvdd_ldgr_id,
5935           l_old_rlovr_val,
5936           l_object_version_number,
5937           l_effective_start_date
5938         ;
5939         --
5940         -- perform rollover
5941         --
5942         hr_utility.set_location(l_proc, 80);
5943         if c_old_ledger%notfound
5944           and not p_calculate_only_mode
5945         then
5946           hr_utility.set_location(l_proc, 90);
5947           --
5948           -- Check the rollover plan is already enrolled.
5949           -- If not, no rollovers to be attached to this plan.
5950           --
5951           open  c_prv( p_per_in_ler_id );
5952           fetch c_prv into l_prtt_rt_val_id,
5953                            l_rt_val,l_rt_strt_dt;
5954           --
5955           if c_prv%found then
5956              --
5957              l_rollover_diff_total:=l_rollover_diff_total+l_balance;
5958              ben_Benefit_Prvdd_Ledger_api.create_Benefit_Prvdd_Ledger (
5959                         p_bnft_prvdd_ldgr_id           => l_bnft_prvdd_ldgr_id
5960                        ,p_effective_start_date         => l_effective_start_date
5961                        ,p_effective_end_date           => l_effective_end_date
5962                        ,p_prtt_ro_of_unusd_amt_flag    => 'Y'
5963                        ,p_frftd_val                    => null
5964                        ,p_prvdd_val                    => null
5965                        ,p_used_val                     => l_balance
5966                        ,p_bnft_prvdr_pool_id           => p_bnft_prvdr_pool_id
5967                        ,p_acty_base_rt_id              => l_acty_base_rt_id
5968                        ,p_per_in_ler_id                => p_per_in_ler_id
5969                        ,p_person_id                    => p_person_id
5970                        ,p_enrt_mthd_cd                 => p_enrt_mthd_cd
5971                        ,p_prtt_enrt_rslt_id            => p_flex_rslt_id
5972                        ,p_business_group_id            => p_business_group_id
5973                        ,p_object_version_number        => l_object_version_number
5974                        ,p_cash_recd_val                => null
5975                        ,p_effective_date               => p_effective_date
5976                        ,p_process_enrt_flag            => 'N'
5977              );
5978              hr_utility.set_location('CREATED LDGR ID='||to_char(l_bnft_prvdd_ldgr_id),100);
5979              --
5980              -- Update the rollover plan rate to reflect rollovers.
5981              --
5982              update_rate(p_prtt_rt_val_id   => l_prtt_rt_val_id,
5983                          p_val      => (l_rt_val + l_balance),
5984                          p_prtt_enrt_rslt_id => l_prtt_enrt_rslt_id,
5985                          p_ended_per_in_ler_id => p_per_in_ler_id,
5986                          p_effective_date  => p_effective_date,
5987                          p_business_group_id => p_business_group_id);
5988              --
5989           end if;
5990           --
5991           close c_prv;
5992           --
5993         elsif l_old_rlovr_val<>l_balance
5994           and not p_calculate_only_mode
5995         then
5996           --
5997           -- Update the ledger if the amount differs.
5998           --
5999           hr_utility.set_location(l_proc, 110);
6000           /*
6001           if l_effective_start_date=p_effective_date then
6002             l_datetrack_mode:=hr_api.g_correction;
6003           else
6004             l_datetrack_mode:=hr_api.g_update;
6005           end if;
6006           */
6007           Get_DT_Upd_Mode
6008                  (p_effective_date        => p_effective_date,
6009                   p_base_table_name       => 'BEN_BNFT_PRVDD_LDGR_F',
6010                   p_base_key_column       => 'BNFT_PRVDD_LDGR_ID',
6011                   p_base_key_value        => l_bnft_prvdd_ldgr_id,
6012                   p_mode                  => l_datetrack_mode);
6013           hr_utility.set_location('UPDATING LEDGER ID='||to_char(l_bnft_prvdd_ldgr_id),120);
6014           l_rollover_diff_total:=l_rollover_diff_total+l_balance-l_old_rlovr_val;
6015           ben_Benefit_Prvdd_Ledger_api.update_Benefit_Prvdd_Ledger (
6016                      p_bnft_prvdd_ldgr_id         => l_bnft_prvdd_ldgr_id
6017                     ,p_effective_start_date       => l_effective_start_date
6018                     ,p_effective_end_date         => l_effective_end_date
6019                     ,p_prtt_ro_of_unusd_amt_flag  => 'Y'
6020                     ,p_frftd_val                  => null
6021                     ,p_prvdd_val                  => null
6022                     ,p_used_val                   => l_balance
6023                     ,p_bnft_prvdr_pool_id         => p_bnft_prvdr_pool_id
6024                     ,p_acty_base_rt_id            => l_acty_base_rt_id
6025                     ,p_per_in_ler_id              => p_per_in_ler_id
6026                     ,p_prtt_enrt_rslt_id          => p_flex_rslt_id
6027                     ,p_business_group_id          => p_business_group_id
6028                     ,p_object_version_number      => l_object_version_number
6029                     ,p_cash_recd_val              => 0
6030                     ,p_effective_date             => p_effective_date
6031                     ,p_datetrack_mode             => l_datetrack_mode
6032                     ,p_process_enrt_flag          => 'N'
6033           );
6034           hr_utility.set_location('UPDATED LEDGER ID='||To_char(l_bnft_prvdd_ldgr_id),130);
6035           --
6036           open  c_prv(-1);
6037           fetch c_prv into l_prtt_rt_val_id,
6038                            l_rt_val, l_rt_strt_dt;
6039           close c_prv;
6040           --
6041           -- Update the rollover plan rate to reflect rollovers.
6042           --
6043           update_rate(p_prtt_rt_val_id => l_prtt_rt_val_id,
6044                       p_val     => (l_rt_val-l_old_rlovr_val+l_balance),
6045                       p_ended_per_in_ler_id => p_per_in_ler_id,
6046                       p_prtt_enrt_rslt_id => l_prtt_enrt_rslt_id,
6047                       p_business_group_id  => p_business_group_id,
6048                       p_effective_date    => p_effective_date);
6049           --
6050         end if;
6051         close c_old_ledger;
6052       end if;
6053     end if; -- rule returned 'Y'
6054     hr_utility.set_location(l_proc, 140);
6055   end loop;
6056   --
6057   p_rollover_val:=l_rollover_diff_total;
6058   hr_utility.set_location('Leaving:'||l_proc, 999);
6059   --
6060 end compute_rollovers;
6061 --
6062 --------------------------------------------------------------------------------
6063 --                      compute_excess
6064 --------------------------------------------------------------------------------
6065 procedure compute_excess
6066   (p_calculate_only_mode in     boolean default false
6067   ,p_bnft_prvdr_pool_id  in     number
6068   ,p_flex_rslt_id        in     number
6069   ,p_person_id           in     number
6070   ,p_per_in_ler_id       in     number
6071   ,p_enrt_mthd_cd        in     varchar2
6072   ,p_effective_date      in     date
6073   ,p_business_group_id   in     number
6074   --
6075   ,p_frftd_val              out nocopy number
6076   ,p_def_exc_amount         out nocopy number
6077   ,p_bpl_cash_recd_val      out nocopy number
6078   )
6079 is
6080    --
6081    -- Get totals for a pool.
6082    --
6083    cursor c_totals
6084      (c_bnft_prvdr_pool_id number
6085      ,c_prtt_enrt_rslt_id  number
6086      ,c_effective_date     date
6087      )
6088    is
6089       select nvl(sum(bpl.prvdd_val),0) tot_prvdd,
6090              nvl(sum(bpl.used_val),0) tot_used,
6091              nvl(sum(frftd_val),0) tot_frftd,
6092              nvl(sum(cash_recd_val),0) tot_cash,
6093              pil.ler_id
6094       from   ben_bnft_prvdd_ldgr_f bpl,
6095              ben_per_in_ler pil
6096       where  bpl.bnft_prvdr_pool_id = c_bnft_prvdr_pool_id
6097       and    bpl.prtt_enrt_rslt_id  = c_prtt_enrt_rslt_id
6098       and    c_effective_date
6099         between bpl.effective_start_date and bpl.effective_end_date
6100       and pil.per_in_ler_id=bpl.per_in_ler_id
6101       and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
6102       group by pil.ler_id;
6103    --
6104    l_totals c_totals%rowtype;
6105    --
6106    -- Get the acty_base_rt_id to be used when fetching/creating
6107    -- excess amounts ledger.(Credit Ledger)
6108    --
6109    cursor c_bpl is
6110       select bpl.acty_base_rt_id
6111       from   ben_bnft_prvdd_ldgr_f bpl,
6112              ben_per_in_ler pil
6113       where  bpl.bnft_prvdr_pool_id = p_bnft_prvdr_pool_id
6114       and    bpl.prtt_enrt_rslt_id  = p_flex_rslt_id
6115       and    bpl.prvdd_val is not null
6116       and    bpl.business_group_id  = p_business_group_id
6117       and    p_effective_date between
6118              bpl.effective_start_date and bpl.effective_end_date
6119 and pil.per_in_ler_id=bpl.per_in_ler_id
6120 and pil.business_group_id=bpl.business_group_id
6121 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
6122 ;
6123    --
6124    -- Get the pool (limits and rounding) details.
6125    --
6126    cursor c_pool is
6127       select bpp.pct_rndg_cd,
6128              bpp.pct_rndg_rl,
6129              bpp.dflt_excs_trtmt_cd,
6130              bpp.dflt_excs_trtmt_rl,
6131              bpp.mn_dstrbl_pct_num,
6132              bpp.mn_dstrbl_val,
6133              bpp.mx_dstrbl_pct_num,
6134              bpp.mx_dstrbl_val,
6135              bpp.rlovr_rstrcn_cd,
6136              bpp.val_rndg_cd,
6137              bpp.val_rndg_rl,
6138              bpp.auto_alct_excs_flag,
6139              bpp.uses_net_crs_mthd_flag
6140       from   ben_bnft_prvdr_pool_f bpp
6141       where  bpp.bnft_prvdr_pool_id = p_bnft_prvdr_pool_id
6142       and    bpp.business_group_id  = p_business_group_id
6143       and    p_effective_date between
6144              bpp.effective_start_date and bpp.effective_end_date;
6145    l_pool_rec    c_pool%rowtype;
6146    --
6147    l_rollover_val           number;
6148    l_dstrbtd_val            number;
6149    l_acty_base_rt_id        number;
6150    l_dummy                  number;
6151    l_def_exc_amount         number := 0;
6152    l_acty_base_rt_id2       number;
6153    l_rt_val                 number;
6154    --
6155    l_proc varchar2(80) := g_package||'.compute_excess';
6156    --
6157    l_dummy_number           number;
6158    --
6159 begin
6160    --
6161    hr_utility.set_location('Entering '||l_proc,5);
6162    --
6163    -- Get all the totals for the pool.
6164    --
6165    p_frftd_val := 0;
6166    --
6167    open c_totals
6168      (c_bnft_prvdr_pool_id => p_bnft_prvdr_pool_id
6169      ,c_prtt_enrt_rslt_id  => p_flex_rslt_id
6170      ,c_effective_date     => p_effective_date
6171      );
6172    fetch c_totals into l_totals;
6173    close c_totals;
6174    --
6175    -- Get the activiity base rt id to be used.
6176    --
6177    open c_bpl;
6178    fetch c_bpl into l_acty_base_rt_id;
6179    close c_bpl;
6180    --
6181    -- Get all the pool details.
6182    --
6183    open c_pool;
6184    fetch c_pool into l_pool_rec;
6185    close c_pool;
6186    --
6187    -- If auto allocate flag is ON, then do the allocation.
6188    --
6189    hr_utility.set_location(l_proc,10);
6190    --
6191    if l_pool_rec.auto_alct_excs_flag = 'Y' then
6192       --
6193       g_credit_pool_result_id := p_flex_rslt_id;
6194       --
6195       hr_utility.set_location(l_proc,15);
6196       -- get the dflt trtmt cd by executing the rule if necessary
6197       if l_pool_rec.dflt_excs_trtmt_rl is not null then
6198          run_rule
6199             (p_effective_date     => p_effective_date
6200             ,p_person_id          => p_person_id
6201             ,p_dflt_excs_trtmt_rl => l_pool_rec.dflt_excs_trtmt_rl
6202             ,p_business_group_id  => p_business_group_id
6203             ,p_ler_id             => l_totals.ler_id
6204             ,p_bnft_prvdr_pool_id => p_bnft_prvdr_pool_id
6205             ,p_dflt_excs_trtmt_cd => l_pool_rec.dflt_excs_trtmt_cd  -- output
6206             ,p_mx_val             => l_dummy); -- output
6207       end if;
6208       --
6209       if l_pool_rec.dflt_excs_trtmt_cd in ('DSTRBT_ALL','DSTRBT_RLOVR_FRFT') then
6210          --
6211          -- Distribute the cash.
6212          --
6213          hr_utility.set_location(l_proc,20);
6214          --
6215          distribute_credits
6216            (p_calculate_only_mode => p_calculate_only_mode
6217            ,p_per_in_ler_id       => p_per_in_ler_id
6218            ,p_prtt_enrt_rslt_id   => p_flex_rslt_id
6219            ,p_bnft_prvdr_pool_id  => p_bnft_prvdr_pool_id
6220            ,p_acty_base_rt_id     => l_acty_base_rt_id
6221            ,p_dflt_excs_trtmt_cd  => l_pool_rec.dflt_excs_trtmt_cd
6222            ,p_prvdd_val           => l_totals.tot_prvdd
6223            ,p_rlld_up_val         => 0
6224            ,p_used_val            => l_totals.tot_used
6225            ,p_rollover_val        => 0
6226            ,p_cash_recd_total     => l_totals.tot_cash
6227            ,p_val_rndg_cd         => l_pool_rec.val_rndg_cd
6228            ,p_val_rndg_rl         => l_pool_rec.val_rndg_rl
6229            ,p_pct_rndg_cd         => l_pool_rec.pct_rndg_cd
6230            ,p_pct_rndg_rl         => l_pool_rec.pct_rndg_rl
6231            ,p_mn_dstrbl_val       => l_pool_rec.mn_dstrbl_val
6232            ,p_mn_dstrbl_pct_num   => l_pool_rec.mn_dstrbl_pct_num
6233            ,p_mx_dstrbl_val       => l_pool_rec.mx_dstrbl_val
6234            ,p_mx_pct              => l_pool_rec.mx_dstrbl_pct_num
6235            ,p_person_id           => p_person_id
6236            ,p_enrt_mthd_cd        => null
6237            ,p_effective_date      => p_effective_date
6238            ,p_business_group_id   => p_business_group_id
6239            ,p_process_enrt_flag   => 'N'
6240            ,p_dstrbtd_val         => l_dstrbtd_val
6241            ,p_bpl_cash_recd_val   => p_bpl_cash_recd_val
6242            ,p_bnft_prvdd_ldgr_id  => l_dummy_number
6243            );
6244          --
6245       end if;
6246       --
6247       if l_pool_rec.dflt_excs_trtmt_cd in ('DSTRBT_RLOVR_FRFT',
6248                                            'RLOVR_DSTRBT_FRFT') then
6249          --
6250          -- Rollover the excess, if anything left.
6251          --
6252          hr_utility.set_location(l_proc,25);
6253          compute_rollovers
6254            (p_calculate_only_mode => p_calculate_only_mode
6255            ,p_bnft_prvdr_pool_id  => p_bnft_prvdr_pool_id
6256            ,p_person_id           => p_person_id
6257            ,p_enrt_mthd_cd        => p_enrt_mthd_cd
6258            ,p_flex_rslt_id        => p_flex_rslt_id
6259            ,p_per_in_ler_id       => p_per_in_ler_id
6260            ,p_effective_date      => p_effective_date
6261            ,p_business_group_id   => p_business_group_id
6262            ,p_pct_rndg_cd         => l_pool_rec.pct_rndg_cd
6263            ,p_pct_rndg_rl         => l_pool_rec.pct_rndg_rl
6264            ,p_dflt_excs_trtmt_cd  => l_pool_rec.dflt_excs_trtmt_cd
6265            ,p_rollover_val        => l_rollover_val
6266            );
6267          --
6268       end if;
6269       --
6270       if l_pool_rec.dflt_excs_trtmt_cd ='RLOVR_DSTRBT_FRFT' then
6271         hr_utility.set_location(l_proc,90 );
6272         --
6273         -- Distribute as cash, if anything still left.
6274         --
6275         hr_utility.set_location(l_proc,30);
6276         distribute_credits
6277           (p_calculate_only_mode => p_calculate_only_mode
6278           ,p_per_in_ler_id       => p_per_in_ler_id
6279           ,p_prtt_enrt_rslt_id   => p_flex_rslt_id
6280           ,p_bnft_prvdr_pool_id  => p_bnft_prvdr_pool_id
6281           ,p_acty_base_rt_id     => l_acty_base_rt_id
6282           ,p_dflt_excs_trtmt_cd  => l_pool_rec.dflt_excs_trtmt_cd
6283           ,p_prvdd_val           => l_totals.tot_prvdd
6284           ,p_rlld_up_val         => 0
6285           ,p_used_val            => l_totals.tot_used
6286           ,p_rollover_val        => l_rollover_val
6287           ,p_cash_recd_total     => l_totals.tot_cash
6288           ,p_val_rndg_cd         => l_pool_rec.val_rndg_cd
6289           ,p_val_rndg_rl         => l_pool_rec.val_rndg_rl
6290           ,p_pct_rndg_cd         => l_pool_rec.pct_rndg_cd
6291           ,p_pct_rndg_rl         => l_pool_rec.pct_rndg_rl
6292           ,p_mn_dstrbl_val       => l_pool_rec.mn_dstrbl_val
6293           ,p_mn_dstrbl_pct_num   => l_pool_rec.mn_dstrbl_pct_num
6294           ,p_mx_dstrbl_val       => l_pool_rec.mx_dstrbl_val
6295           ,p_mx_pct              => l_pool_rec.mx_dstrbl_pct_num
6296           ,p_person_id           => p_person_id
6297           ,p_enrt_mthd_cd        => null
6298           ,p_effective_date      => p_effective_date
6299           ,p_business_group_id   => p_business_group_id
6300           ,p_process_enrt_flag   => 'N'
6301           ,p_dstrbtd_val         => l_dstrbtd_val
6302           ,p_bpl_cash_recd_val   => p_bpl_cash_recd_val
6303           ,p_bnft_prvdd_ldgr_id  => l_dummy_number
6304           );
6305          --
6306       end if;
6307       --
6308       -- All possible rollovers and cash distribution done. If anything is
6309       -- still left, it has to be forfeited.
6310       --
6311       hr_utility.set_location(l_proc,35);
6312       forfeit_credits
6313         (p_calculate_only_mode => p_calculate_only_mode
6314         ,p_prtt_enrt_rslt_id   => p_flex_rslt_id
6315         ,p_bnft_prvdr_pool_id  => p_bnft_prvdr_pool_id
6316         ,p_acty_base_rt_id     => l_acty_base_rt_id
6317         ,p_prvdd_val           => l_totals.tot_prvdd
6318         ,p_rlld_up_val         => 0
6319         ,p_used_val            => l_totals.tot_used
6320         ,p_rollover_val        => l_rollover_val
6321         ,p_cash_val            => l_dstrbtd_val
6322         ,p_effective_date      => p_effective_date
6323         ,p_business_group_id   => p_business_group_id
6324         ,p_frftd_val           => p_frftd_val
6325         ,p_per_in_ler_id       => p_per_in_ler_id
6326         ,p_person_id           => p_person_id
6327         ,p_enrt_mthd_cd        => p_enrt_mthd_cd
6328         );
6329 
6330    end if;
6331    --
6332    if l_pool_rec.uses_net_crs_mthd_flag = 'Y' then
6333      --
6334      -- Check if there is a deficit amount.
6335      --
6336      if l_pool_rec.auto_alct_excs_flag = 'Y' then
6337        l_def_exc_amount := l_def_exc_amount + nvl(l_dstrbtd_val,0);
6338      else
6339        l_def_exc_amount := l_def_exc_amount +
6340                             (l_totals.tot_prvdd - l_totals.tot_used);
6341      end if;
6342    end if; -- end net credit method.
6343    --
6344    p_def_exc_amount := l_def_exc_amount;
6345    --
6346    hr_utility.set_location('Leaving '||l_proc,999);
6347 
6348 end compute_excess;
6349 --
6350 --------------------------------------------------------------------------------
6351 --                      update_net_credit_rate
6352 --------------------------------------------------------------------------------
6353 procedure update_net_credit_rate
6354             (p_prtt_rt_val_id       in number
6355             ,p_flex_rslt_id         in number
6356             ,p_acty_base_rt_id      in number
6357             ,p_per_in_ler_id        in number
6358             ,p_def_exc_amount       in number
6359             ,p_effective_date       in date
6360             ,p_business_group_id    in number
6361             ) is
6362    --
6363    cursor c_prv_child is
6364       select prv.prtt_rt_val_id,
6365              prv.rt_val,
6366              prv.rt_strt_dt,
6367              prv.acty_base_rt_id
6368       from   ben_prtt_rt_val prv
6369             ,ben_acty_base_rt_f abr
6370       where  prv.prtt_enrt_rslt_id = p_flex_rslt_id
6371       and    prv.business_group_id  = p_business_group_id
6372       and    prv.prtt_rt_val_stat_cd is null
6373       and    abr.acty_base_rt_id = prv.acty_base_rt_id
6374       and    p_effective_date
6375              between abr.effective_start_date and
6376              abr.effective_end_date
6377       and    abr.business_group_id = prv.business_group_id
6378       and    abr.parnt_acty_base_rt_id = p_acty_base_rt_id
6379    order by prv.rt_strt_dt desc;
6380    l_prv_child_rec    c_prv_child%rowtype;
6381    --
6382    cursor c_pen is
6383       select pen.*
6384       from   ben_prtt_enrt_rslt_f pen
6385       where  pen.prtt_enrt_rslt_id = p_flex_rslt_id
6386       and    pen.business_group_id  = p_business_group_id
6387       and    p_effective_date
6388              between pen.effective_start_date and
6389              pen.effective_end_date
6390       and    pen.prtt_enrt_rslt_stat_cd is null;
6391    l_pen_rec    c_pen%rowtype;
6392    --
6393    cursor c_abr is
6394       select abr.*
6395       from   ben_acty_base_rt_f abr
6396       where  abr.acty_base_rt_id = p_acty_base_rt_id
6397       and    abr.business_group_id  = p_business_group_id
6398       and    p_effective_date between
6399              abr.effective_start_date and abr.effective_end_date;
6400    --
6401    l_abr_rec    c_abr%rowtype;
6402    --
6403    cursor c_abr_child is
6404       select abr.*
6405       from   ben_acty_base_rt_f abr
6406       where  abr.parnt_acty_base_rt_id = p_acty_base_rt_id
6407       and    abr.business_group_id  = p_business_group_id
6408       and    p_effective_date between
6409              abr.effective_start_date and abr.effective_end_date;
6410    --
6411    l_abr_child_rec    c_abr_child%rowtype;
6412    --
6413    cursor c_get_acty_ref_perd_cd is
6414       select  acty_ref_perd_cd
6415              ,cmcd_ref_perd_cd
6416       from   ben_prtt_rt_val prv
6417       where  prv.prtt_enrt_rslt_id = p_flex_rslt_id
6418       and    prv.prtt_rt_val_stat_cd is null
6419       and    prv.business_group_id = p_business_group_id;
6420    --
6421    --Bug 2386000
6422    --
6423    CURSOR c_lee_rsn_for_plan (c_ler_id number, c_pl_id number ) IS
6424       SELECT   leer.lee_rsn_id
6425       FROM     ben_lee_rsn_f leer,
6426                ben_popl_enrt_typ_cycl_f petc
6427       WHERE    leer.ler_id            = c_ler_id
6428       AND      leer.business_group_id = p_business_group_id
6429       AND      p_effective_date BETWEEN leer.effective_start_date
6430                    AND leer.effective_end_date
6431       AND      leer.popl_enrt_typ_cycl_id = petc.popl_enrt_typ_cycl_id
6432       AND      petc.pl_id                 = c_pl_id
6433       AND      petc.enrt_typ_cycl_cd = 'L'                        -- life event
6434       AND      petc.business_group_id = p_business_group_id
6435       AND      p_effective_date BETWEEN petc.effective_start_date
6436                    AND petc.effective_end_date;
6437    --
6438    CURSOR c_lee_rsn_for_program (c_ler_id number, c_pgm_id number )IS
6439       SELECT   leer.lee_rsn_id
6440       FROM     ben_lee_rsn_f leer,
6441                ben_popl_enrt_typ_cycl_f petc
6442       WHERE    leer.ler_id            = c_ler_id
6443       AND      leer.business_group_id = p_business_group_id
6444       AND      p_effective_date BETWEEN leer.effective_start_date
6445                    AND leer.effective_end_date
6446       AND      leer.popl_enrt_typ_cycl_id = petc.popl_enrt_typ_cycl_id
6447       AND      petc.pgm_id                = c_pgm_id
6448       AND      petc.enrt_typ_cycl_cd      = 'L'
6449       AND      petc.business_group_id     = p_business_group_id
6450       AND      p_effective_date BETWEEN petc.effective_start_date
6451                    AND petc.effective_end_date;
6452    --
6453    l_lee_rsn_id                number := null ;
6454    --
6455    l_child_rt_val              ben_prtt_rt_val.rt_val%type;
6456    l_prtt_rt_val_id            ben_prtt_rt_val.prtt_rt_val_id%type;
6457    l_object_version_number     ben_prtt_rt_val.object_version_number%type;
6458    l_cmcd_ref_perd_cd          ben_prtt_rt_val.cmcd_ref_perd_cd%type;
6459    l_acty_ref_perd_cd          ben_prtt_rt_val.acty_ref_perd_cd%type;
6460    l_dummy_num                 number;
6461    l_dummy_varchar2            varchar2(80);
6462    l_dummy_date                date;
6463    l_enrt_cvg_strt_dt          date;
6464    l_enrt_cvg_strt_dt_cd       varchar2(30);
6465    l_enrt_cvg_strt_dt_rl       number;
6466    l_rt_strt_dt                date;
6467    l_rt_strt_dt_cd             varchar2(30);
6468    l_rt_strt_dt_rl             number;
6469    l_enrt_cvg_end_dt           date;
6470    l_enrt_cvg_end_dt_cd        varchar2(30);
6471    l_enrt_cvg_end_dt_rl        number;
6472    l_rt_end_dt                 date;
6473    l_rt_end_dt_cd              varchar2(30);
6474    l_rt_end_dt_rl              number;
6475    l_enrt_rt_id                number;
6476 
6477    --
6478    l_proc varchar2(80) := g_package||'.update_net_credit_rate';
6479    --
6480 begin
6481   --
6482   hr_utility.set_location('Entering '||l_proc,5);
6483   open c_pen;
6484   fetch c_pen into l_pen_rec;
6485   close c_pen;
6486   --
6487   if p_prtt_rt_val_id is not null then
6488     --
6489     l_prtt_rt_val_id := p_prtt_rt_val_id;
6490     --
6491     hr_utility.set_location('update_net_credit amt '||p_def_exc_amount,5);
6492     update_rate
6493       (p_prtt_rt_val_id       => l_prtt_rt_val_id
6494       ,p_val                  => p_def_exc_amount
6495       ,p_prtt_enrt_rslt_id    => p_flex_rslt_id
6496       ,p_ended_per_in_ler_id  => p_per_in_ler_id
6497       ,p_effective_date       => p_effective_date
6498       ,p_business_group_id    => p_business_group_id
6499       );
6500     --
6501     --  Update the associated child rate.
6502     --
6503     open c_prv_child;
6504     fetch c_prv_child into l_prv_child_rec;
6505     close c_prv_child;
6506     if p_def_exc_amount <> 0 then
6507       --
6508       --  Calculate the child rate.
6509       --
6510       ben_determine_activity_base_rt.main
6511         (p_person_id                   => l_pen_rec.person_id
6512         ,p_elig_per_elctbl_chc_id      => null
6513         ,p_acty_base_rt_id             => l_prv_child_rec.acty_base_rt_id
6514         ,p_effective_date              => p_effective_date
6515         ,p_per_in_ler_id               => p_per_in_ler_id
6516         ,p_calc_only_rt_val_flag       => true
6517         ,p_pgm_id                      => l_pen_rec.pgm_id
6518         ,p_pl_id                       => l_pen_rec.pl_id
6519         ,p_business_group_id           => p_business_group_id
6520         ,p_cal_val                     => p_def_exc_amount
6521         ,p_val                         => l_child_rt_val
6522         ,p_mn_elcn_val                 => l_dummy_num
6523         ,p_mx_elcn_val                 => l_dummy_num
6524         ,p_ann_val                     => l_dummy_num
6525         ,p_ann_mn_elcn_val             => l_dummy_num
6526         ,p_ann_mx_elcn_val             => l_dummy_num
6527         ,p_cmcd_val                    => l_dummy_num
6528         ,p_cmcd_mn_elcn_val            => l_dummy_num
6529         ,p_cmcd_mx_elcn_val            => l_dummy_num
6530         ,p_cmcd_acty_ref_perd_cd       => l_dummy_varchar2
6531         ,p_incrmt_elcn_val             => l_dummy_num
6532         ,p_dflt_val                    => l_dummy_num
6533         ,p_tx_typ_cd                   => l_dummy_varchar2
6534         ,p_acty_typ_cd                 => l_dummy_varchar2
6535         ,p_nnmntry_uom                 => l_dummy_varchar2
6536         ,p_entr_val_at_enrt_flag       => l_dummy_varchar2
6537         ,p_dsply_on_enrt_flag          => l_dummy_varchar2
6538         ,p_use_to_calc_net_flx_cr_flag => l_dummy_varchar2
6539         ,p_rt_usg_cd                   => l_dummy_varchar2
6540         ,p_bnft_prvdr_pool_id          => l_dummy_num
6541         ,p_actl_prem_id                => l_dummy_num
6542         ,p_cvg_calc_amt_mthd_id        => l_dummy_num
6543         ,p_bnft_rt_typ_cd              => l_dummy_varchar2
6544         ,p_rt_typ_cd                   => l_dummy_varchar2
6545         ,p_rt_mlt_cd                   => l_dummy_varchar2
6546         ,p_comp_lvl_fctr_id            => l_dummy_num
6547         ,p_entr_ann_val_flag           => l_dummy_varchar2
6548         ,p_ptd_comp_lvl_fctr_id        => l_dummy_num
6549         ,p_clm_comp_lvl_fctr_id        => l_dummy_num
6550         ,p_ann_dflt_val                => l_dummy_num
6551         ,p_rt_strt_dt                  => l_dummy_date
6552         ,p_rt_strt_dt_cd               => l_dummy_varchar2
6553         ,p_rt_strt_dt_rl               => l_dummy_num
6554         ,p_prtt_rt_val_id              => l_dummy_num
6555         ,p_dsply_mn_elcn_val           => l_dummy_num
6556         ,p_dsply_mx_elcn_val           => l_dummy_num
6557         ,p_pp_in_yr_used_num           => l_dummy_num
6558         ,p_ordr_num                    => l_dummy_num
6559         ,p_iss_val                     => l_dummy_num
6560         );
6561     else
6562       l_child_rt_val := 0;
6563     end if;
6564     --
6565     --  Update the child rate.
6566     --
6567     update_rate
6568      (p_prtt_rt_val_id       => l_prv_child_rec.prtt_rt_val_id
6569      ,p_val                  => l_child_rt_val
6570      ,p_prtt_enrt_rslt_id    => p_flex_rslt_id
6571      ,p_ended_per_in_ler_id  => p_per_in_ler_id
6572      ,p_effective_date       => p_effective_date
6573      ,p_business_group_id    => p_business_group_id
6574      );
6575   else
6576     --
6577     --  Create the prtt_rt_val.
6578     --Bug 2386000
6579     open c_lee_rsn_for_plan(l_pen_rec.ler_id, l_pen_rec.pl_id );
6580     fetch c_lee_rsn_for_plan into l_lee_rsn_id ;
6581     close c_lee_rsn_for_plan ;
6582     --
6583     if l_lee_rsn_id is null and l_pen_rec.pgm_id is not null then
6584       open c_lee_rsn_for_program(l_pen_rec.ler_id, l_pen_rec.pgm_id);
6585       fetch c_lee_rsn_for_program into l_lee_rsn_id ;
6586       close c_lee_rsn_for_program ;
6587     end if;
6588     --
6589     ben_determine_date.rate_and_coverage_dates(
6590          p_which_dates_cd          => 'R',
6591          p_pgm_id                  => l_pen_rec.pgm_id,
6592          p_pl_id                   => l_pen_rec.pl_id,
6593          p_oipl_id                 => l_pen_rec.oipl_id,
6594          p_lee_rsn_id              => l_lee_rsn_id,
6595          p_per_in_ler_id           => p_per_in_ler_id,
6596          p_person_id               => l_pen_rec.person_id,
6597          p_business_group_id       => p_business_group_id,
6598          p_enrt_cvg_strt_dt        => l_enrt_cvg_strt_dt,
6599          p_enrt_cvg_strt_dt_cd     => l_enrt_cvg_strt_dt_cd,
6600          p_enrt_cvg_strt_dt_rl     => l_enrt_cvg_strt_dt_rl,
6601          p_rt_strt_dt              => l_rt_strt_dt,
6602          p_rt_strt_dt_cd           => l_rt_strt_dt_cd,
6603          p_rt_strt_dt_rl           => l_rt_strt_dt_rl,
6604          p_enrt_cvg_end_dt         => l_enrt_cvg_end_dt,
6605          p_enrt_cvg_end_dt_cd      => l_enrt_cvg_end_dt_cd,
6606          p_enrt_cvg_end_dt_rl      => l_enrt_cvg_end_dt_rl,
6607          p_rt_end_dt               => l_rt_end_dt,
6608          p_rt_end_dt_cd            => l_rt_end_dt_cd,
6609          p_rt_end_dt_rl            => l_rt_end_dt_rl,
6610          p_effective_date          => p_effective_date
6611          );
6612     --
6613     open c_abr;
6614     fetch c_abr into l_abr_rec;
6615     close c_abr;
6616     --
6617     open c_get_acty_ref_perd_cd;
6618     fetch c_get_acty_ref_perd_cd into l_acty_ref_perd_cd
6619                                      ,l_cmcd_ref_perd_cd;
6620     close c_get_acty_ref_perd_cd;
6621 
6622     ben_prtt_rt_val_api.create_prtt_rt_val
6623       (p_prtt_rt_val_id         => l_prtt_rt_val_id
6624       ,p_per_in_ler_id          => p_per_in_ler_id
6625       ,p_rt_typ_cd              => l_abr_rec.rt_typ_cd
6626       ,p_tx_typ_cd              => l_abr_rec.tx_typ_cd
6627       ,p_acty_typ_cd            => l_abr_rec.acty_typ_cd
6628       ,p_mlt_cd                 => l_abr_rec.rt_mlt_cd
6629       ,p_acty_ref_perd_cd       => l_acty_ref_perd_cd
6630       ,p_rt_val                 => p_def_exc_amount
6631       ,p_rt_strt_dt             => l_rt_strt_dt
6632       ,p_rt_end_dt              => hr_api.g_eot
6633       ,p_ann_rt_val             => null
6634       ,p_bnft_rt_typ_cd         => l_abr_rec.bnft_rt_typ_cd
6635       ,p_cmcd_ref_perd_cd       => l_cmcd_ref_perd_cd
6636       ,p_cmcd_rt_val            => p_def_exc_amount
6637       ,p_dsply_on_enrt_flag     => l_abr_rec.dsply_on_enrt_flag
6638       ,p_elctns_made_dt         => p_effective_date
6639       ,p_cvg_amt_calc_mthd_id   => null
6640       ,p_actl_prem_id           => null
6641       ,p_comp_lvl_fctr_id       => l_abr_rec.comp_lvl_fctr_id
6642       ,p_prtt_enrt_rslt_id      => p_flex_rslt_id
6643       ,p_business_group_id      => p_business_group_id
6644       ,p_object_version_number  => l_object_version_number
6645       ,p_effective_date         => p_effective_date
6646       ,p_acty_base_rt_id        => p_acty_base_rt_id
6647       ,p_input_value_id         => l_abr_rec.input_value_id
6648       ,p_element_type_id        => l_abr_rec.element_type_id
6649       ,p_person_id              => l_pen_rec.person_id
6650       ,p_ordr_num       => l_abr_rec.ordr_num
6651       );
6652     --
6653     --   Create the child rate.
6654     --
6655     open c_abr_child;
6656     fetch c_abr_child into l_abr_child_rec;
6657     close c_abr_child;
6658     --
6659     --  Calculate the child rate.
6660     --
6661     ben_determine_activity_base_rt.main
6662       (p_person_id                   => l_pen_rec.person_id
6663       ,p_elig_per_elctbl_chc_id      => null
6664       ,p_acty_base_rt_id             => l_abr_child_rec.acty_base_rt_id
6665       ,p_effective_date              => p_effective_date
6666       ,p_per_in_ler_id               => p_per_in_ler_id
6667       ,p_calc_only_rt_val_flag       => true
6668       ,p_pgm_id                      => l_pen_rec.pgm_id
6669       ,p_pl_id                       => l_pen_rec.pl_id
6670       ,p_business_group_id           => p_business_group_id
6671       ,p_cal_val                     => p_def_exc_amount
6672       ,p_val                         => l_child_rt_val
6673       ,p_mn_elcn_val                 => l_dummy_num
6674       ,p_mx_elcn_val                 => l_dummy_num
6675       ,p_ann_val                     => l_dummy_num
6676       ,p_ann_mn_elcn_val             => l_dummy_num
6677       ,p_ann_mx_elcn_val             => l_dummy_num
6678       ,p_cmcd_val                    => l_dummy_num
6679       ,p_cmcd_mn_elcn_val            => l_dummy_num
6680       ,p_cmcd_mx_elcn_val            => l_dummy_num
6681       ,p_cmcd_acty_ref_perd_cd       => l_dummy_varchar2
6682       ,p_incrmt_elcn_val             => l_dummy_num
6683       ,p_dflt_val                    => l_dummy_num
6684       ,p_tx_typ_cd                   => l_dummy_varchar2
6685       ,p_acty_typ_cd                 => l_dummy_varchar2
6686       ,p_nnmntry_uom                 => l_dummy_varchar2
6687       ,p_entr_val_at_enrt_flag       => l_dummy_varchar2
6688       ,p_dsply_on_enrt_flag          => l_dummy_varchar2
6689       ,p_use_to_calc_net_flx_cr_flag => l_dummy_varchar2
6690       ,p_rt_usg_cd                   => l_dummy_varchar2
6691       ,p_bnft_prvdr_pool_id          => l_dummy_num
6692       ,p_actl_prem_id                => l_dummy_num
6693       ,p_cvg_calc_amt_mthd_id        => l_dummy_num
6694       ,p_bnft_rt_typ_cd              => l_dummy_varchar2
6695       ,p_rt_typ_cd                   => l_dummy_varchar2
6696       ,p_rt_mlt_cd                   => l_dummy_varchar2
6697       ,p_comp_lvl_fctr_id            => l_dummy_num
6698       ,p_entr_ann_val_flag           => l_dummy_varchar2
6699       ,p_ptd_comp_lvl_fctr_id        => l_dummy_num
6700       ,p_clm_comp_lvl_fctr_id        => l_dummy_num
6701       ,p_ann_dflt_val                => l_dummy_num
6702       ,p_rt_strt_dt                  => l_dummy_date
6703       ,p_rt_strt_dt_cd               => l_dummy_varchar2
6704       ,p_rt_strt_dt_rl               => l_dummy_num
6705       ,p_prtt_rt_val_id              => l_dummy_num
6706       ,p_dsply_mn_elcn_val           => l_dummy_num
6707       ,p_dsply_mx_elcn_val           => l_dummy_num
6708       ,p_pp_in_yr_used_num           => l_dummy_num
6709       ,p_ordr_num                    => l_dummy_num
6710         ,p_iss_val                     => l_dummy_num
6711       );
6712     --
6713     ben_prtt_rt_val_api.create_prtt_rt_val
6714       (p_prtt_rt_val_id         => l_prtt_rt_val_id
6715       ,p_per_in_ler_id          => p_per_in_ler_id
6716       ,p_rt_typ_cd              => l_abr_rec.rt_typ_cd
6717       ,p_tx_typ_cd              => l_abr_rec.tx_typ_cd
6718       ,p_acty_typ_cd            => l_abr_rec.acty_typ_cd
6719       ,p_mlt_cd                 => l_abr_rec.rt_mlt_cd
6720       ,p_acty_ref_perd_cd       => l_acty_ref_perd_cd
6721       ,p_rt_val                 => l_child_rt_val
6722       ,p_rt_strt_dt             => l_rt_strt_dt
6723       ,p_rt_end_dt              => hr_api.g_eot
6724       ,p_ann_rt_val             => null
6725       ,p_bnft_rt_typ_cd         => l_abr_rec.bnft_rt_typ_cd
6726       ,p_cmcd_ref_perd_cd       => l_cmcd_ref_perd_cd
6727       ,p_cmcd_rt_val            => l_child_rt_val
6728       ,p_dsply_on_enrt_flag     => l_abr_rec.dsply_on_enrt_flag
6729       ,p_elctns_made_dt         => p_effective_date
6730       ,p_cvg_amt_calc_mthd_id   => null
6731       ,p_actl_prem_id           => null
6732       ,p_comp_lvl_fctr_id       => l_abr_rec.comp_lvl_fctr_id
6733       ,p_prtt_enrt_rslt_id      => p_flex_rslt_id
6734       ,p_business_group_id      => p_business_group_id
6735       ,p_object_version_number  => l_object_version_number
6736       ,p_effective_date         => p_effective_date
6737       ,p_acty_base_rt_id        => p_acty_base_rt_id
6738       ,p_input_value_id         => l_abr_rec.input_value_id
6739       ,p_element_type_id        => l_abr_rec.element_type_id
6740       ,p_person_id              => l_pen_rec.person_id
6741       ,p_ordr_num       => l_abr_rec.ordr_num
6742       );
6743   end if;
6744   --
6745   hr_utility.set_location('Leaving '||l_proc,999);
6746   --
6747 end update_net_credit_rate;
6748 --------------------------------------------------------------------------------
6749 --                      recompute_flex_credits
6750 --------------------------------------------------------------------------------
6751  procedure recompute_flex_credits(
6752               p_person_id            in number,
6753               p_enrt_mthd_cd         in varchar2,
6754               p_prtt_enrt_rslt_id    in number,
6755               p_per_in_ler_id        in number,
6756               p_pgm_id               in number,
6757               p_business_group_id    in number,
6758               p_effective_date       in date) is
6759    --
6760    l_debit_ledger_deleted    boolean := false;
6761    l_credit_ledger_deleted   boolean := false;
6762    l_recompute_excess        boolean := false;
6763    l_person_enrolled         boolean := false;
6764    l_total_credits           number  := 0;
6765    l_debit_pool_id           number;
6766    l_flex_rslt_id            number  := null;
6767    l_effective_start_date    date;
6768    l_effective_end_date      date;
6769    l_effective_date          date;
6770    l_datetrack_mode          varchar2(30);
6771    l_flex_pgm                varchar2(30);
6772    l_frftd_val               number;
6773    l_prtt_rt_val_id          number;
6774    l_acty_base_rt_id         number;
6775    l_rt_val                  number;
6776    l_uses_net_crs_mthd       boolean := false;
6777    l_prv_exists              boolean := false;
6778    l_def_exc_amount          number := 0;
6779    l_epe_rec                 ben_epe_shd.g_rec_type;
6780    l_dummy_number            number;
6781    l_rt_strt_dt              date;
6782    --
6783    cursor c_pgm is
6784       select 'Y'
6785       from   ben_pgm_f pgm
6786       where  pgm.pgm_id = p_pgm_id
6787       and    pgm.business_group_id = p_business_group_id
6788       and    pgm.pgm_typ_cd in ('FLEX','FPC')
6789       and    p_effective_date between
6790              pgm.effective_start_date and pgm.effective_end_date;
6791    --
6792    -- Cursor to fetch the debit ledger.
6793    --
6794    cursor c_dbt_bpl is
6795       select bpl.bnft_prvdd_ldgr_id,
6796              bpl.object_version_number,
6797              bpl.effective_start_date,
6798              bpl.bnft_prvdr_pool_id,
6799              bpl.prtt_enrt_rslt_id
6800       from   ben_prtt_enrt_rslt_f   enrt_pen,
6801              ben_prtt_rt_val        enrt_prv,
6802              ben_bnft_prvdd_ldgr_f  bpl,
6803              ben_per_in_ler pil,
6804              ben_prtt_enrt_rslt_f   flex_pen
6805       where  enrt_pen.prtt_enrt_rslt_id = p_prtt_enrt_rslt_id
6806       and    enrt_pen.prtt_enrt_rslt_stat_cd is null
6807       and    enrt_pen.enrt_cvg_thru_dt = hr_api.g_eot
6808       and    p_effective_date between
6809              enrt_pen.effective_start_date and enrt_pen.effective_end_date
6810       and    enrt_pen.prtt_enrt_rslt_id = enrt_prv.prtt_enrt_rslt_id
6811       and    enrt_prv.prtt_rt_val_stat_cd is null
6812       and    enrt_prv.acty_base_rt_id = bpl.acty_base_rt_id
6813       and    bpl.used_val is not null
6814       and    p_effective_date between
6815              bpl.effective_start_date and bpl.effective_end_date
6816       and    bpl.prtt_enrt_rslt_id = flex_pen.prtt_enrt_rslt_id
6817       and    flex_pen.person_id = p_person_id
6818       and    flex_pen.prtt_enrt_rslt_stat_cd is null
6819       and    flex_pen.enrt_cvg_thru_dt = hr_api.g_eot
6820       and    p_effective_date between
6821              flex_pen.effective_start_date and flex_pen.effective_end_date
6822 and pil.per_in_ler_id=bpl.per_in_ler_id
6823 and pil.business_group_id=bpl.business_group_id
6824 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
6825 order by bpl.bnft_prvdd_ldgr_id, bpl.effective_start_date -- rajkiran
6826 ;
6827    --
6828    l_dbt_bpl_rec    c_dbt_bpl%rowtype;
6829    --
6830    -- Cursor to fetch all pools for the person and program
6831    --
6832    cursor c_crdt_bpp is
6833       select bpp.pgm_id,
6834              bpp.plip_id,
6835              bpp.ptip_id,
6836              bpp.cmbn_ptip_id,
6837              bpp.cmbn_plip_id,
6838              bpp.cmbn_ptip_opt_id,
6839              bpp.business_group_id,
6840              bpp.bnft_prvdr_pool_id,
6841              bpl.prvdd_val,
6842              bpl.prtt_enrt_rslt_id,
6843              bpp.uses_net_crs_mthd_flag
6844       from   ben_prtt_enrt_rslt_f   flex_pen,
6845              ben_bnft_prvdd_ldgr_f  bpl,
6846              ben_per_in_ler pil,
6847              ben_bnft_prvdr_pool_f  bpp
6848       where  flex_pen.person_id = p_person_id
6849       and    flex_pen.pgm_id    = p_pgm_id
6850       and    flex_pen.prtt_enrt_rslt_stat_cd is null
6851       and    flex_pen.enrt_cvg_thru_dt = hr_api.g_eot
6852       and    p_effective_date between
6853              flex_pen.effective_start_date and flex_pen.effective_end_date
6854       and    flex_pen.prtt_enrt_rslt_id = bpl.prtt_enrt_rslt_id
6855       and    bpl.prvdd_val is not null
6856       and    p_effective_date between
6857              bpl.effective_start_date and bpl.effective_end_date
6858       and    bpl.bnft_prvdr_pool_id = bpp.bnft_prvdr_pool_id
6859       and    p_effective_date between
6860              bpp.effective_start_date and bpp.effective_end_date
6861 and pil.per_in_ler_id=bpl.per_in_ler_id
6862 and pil.business_group_id=bpl.business_group_id
6863 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
6864 ;
6865    --
6866    -- Cursor to get forfeited amount for a pool.
6867    --
6868    cursor c_frftd_bpl(v_pool_id in number) is
6869       select bpl.frftd_val
6870       from   ben_bnft_prvdd_ldgr_f bpl,
6871              ben_per_in_ler pil
6872       where  bpl.bnft_prvdr_pool_id = v_pool_id
6873       and    bpl.prtt_enrt_rslt_id = l_flex_rslt_id
6874       and    bpl.business_group_id  = p_business_group_id
6875       and    bpl.frftd_val is not null
6876       and    p_effective_date between
6877              bpl.effective_start_date and bpl.effective_end_date
6878 and pil.per_in_ler_id=bpl.per_in_ler_id
6879 and pil.business_group_id=bpl.business_group_id
6880 and pil.per_in_ler_stat_cd not in ('VOIDD','BCKDT')
6881 ;
6882    --
6883    l_frftd_bpl_rec         c_frftd_bpl%rowtype;
6884    --
6885    cursor c_prv is
6886       select prv.prtt_rt_val_id,
6887              prv.rt_val,
6888              prv.rt_strt_dt
6889       from   ben_prtt_rt_val prv
6890             ,ben_acty_base_rt_f abr
6891       where  prv.prtt_enrt_rslt_id = l_flex_rslt_id
6892       and    prv.business_group_id  = p_business_group_id
6893       and    prv.prtt_rt_val_stat_cd is null
6894       and    prv.rt_end_dt >= p_effective_date  --------Bug 8601352
6895       and    abr.acty_base_rt_id = prv.acty_base_rt_id
6896       and    p_effective_date
6897              between abr.effective_start_date and
6898              abr.effective_end_date
6899       and    abr.business_group_id = prv.business_group_id
6900       and    abr.acty_typ_cd not in ('NCRDSTR','NCRUDED')
6901       and    abr.parnt_acty_base_rt_id is null
6902       order by 3 desc;
6903    --
6904    cursor c_prv2 is
6905       select prv.prtt_rt_val_id,
6906              prv.rt_val,
6907              prv.rt_strt_dt,
6908              prv.acty_typ_cd,
6909              prv.acty_base_rt_id
6910       from   ben_prtt_rt_val prv
6911       where  prv.prtt_enrt_rslt_id = l_flex_rslt_id
6912       and    prv.business_group_id  = p_business_group_id
6913       and    prv.prtt_rt_val_stat_cd is null
6914       and    prv.acty_typ_cd in ('NCRDSTR','NCRUDED')
6915       --9663838
6916       and   (( prv.rt_end_dt >= p_effective_date
6917              and nvl(prv.ended_per_in_ler_id,-1) <> p_per_in_ler_id)
6918         or   prv.ended_per_in_ler_id = p_per_in_ler_id)
6919       --9663838
6920       --and  prv.rt_end_dt >= p_effective_date   --------Bug 8601352
6921       order by 3 desc;
6922    --
6923    cursor c_abr(p_acty_typ_cd in varchar2) is
6924       select abr.acty_base_rt_id
6925       from   ben_acty_base_rt_f abr
6926             ,ben_prtt_enrt_rslt_f pen
6927       where  abr.pl_id = pen.pl_id
6928       and    pen.prtt_enrt_rslt_id = l_flex_rslt_id
6929       and    abr.business_group_id  = p_business_group_id
6930       and    pen.business_group_id  = abr.business_group_id
6931       and    pen.prtt_enrt_rslt_stat_cd is null
6932       and    p_effective_date
6933              between pen.effective_start_date and
6934              pen.effective_end_date
6935       and    abr.acty_typ_cd = p_acty_typ_cd;
6936     --
6937    cursor c_bpl_esd( p_bpl_id number ) is
6938       select bnft_prvdd_ldgr_id,
6939              effective_start_date,
6940              effective_end_date,
6941              object_version_number
6942       from ben_bnft_prvdd_ldgr_f bpl
6943       where bnft_prvdd_ldgr_id = p_bpl_id
6944       --and effective_start_date < p_eff_dt
6945       order by effective_start_date;
6946    --
6947    l_bpl_esd_rec c_bpl_esd%ROWTYPE; -- rajkiran
6948    l_ovn number;                            -- rajkiran
6949    l_count number := 0;
6950    l_proc varchar2(80) := g_package||'.recompute_flex_credits';
6951    --
6952 begin
6953    --
6954    hr_utility.set_location('Entering '||l_proc,5);
6955    --
6956    if p_pgm_id is null or
6957       p_person_id is null or
6958       p_prtt_enrt_rslt_id is null then
6959       --
6960       return;
6961       --
6962    end if;
6963    --
6964    hr_utility.set_location(l_proc, 10);
6965    --
6966    open  c_pgm;
6967    fetch c_pgm into l_flex_pgm;
6968    --
6969    if c_pgm%notfound then
6970       --
6971       close c_pgm;
6972       return;
6973       --
6974    else
6975       --
6976       close c_pgm;
6977       --
6978    end if;
6979    --
6980    -- Delete the debit ledger, if found.
6981    --
6982    open  c_dbt_bpl;
6983    fetch c_dbt_bpl into l_dbt_bpl_rec;
6984    --
6985    if c_dbt_bpl%found then
6986       --
6987       l_debit_ledger_deleted := true;
6988       l_recompute_excess     := true;
6989       l_debit_pool_id        := l_dbt_bpl_rec.bnft_prvdr_pool_id;
6990       l_flex_rslt_id         := l_dbt_bpl_rec.prtt_enrt_rslt_id;
6991       --
6992 /*      if l_dbt_bpl_rec.effective_start_date = p_effective_date then
6993          --
6994          l_datetrack_mode := hr_api.g_zap;
6995          l_effective_date := p_effective_date;
6996          --
6997       else
6998          --
6999          l_datetrack_mode := hr_api.g_delete;
7000          l_effective_date := p_effective_date -1;
7001          --
7002       end if;*/
7003        Open c_bpl_esd(l_dbt_bpl_rec.bnft_prvdd_ldgr_id);
7004        loop
7005         fetch c_bpl_esd into l_bpl_esd_rec;
7006         exit when c_bpl_esd%notfound;
7007          if l_bpl_esd_rec.effective_start_date = p_effective_date and
7008             l_count = 1 then
7009             --
7010             l_ovn := l_bpl_esd_rec.object_version_number;
7011             l_datetrack_mode := hr_api.g_zap;
7012             l_effective_date := p_effective_date;
7013             exit;
7014             --
7015          elsif (p_effective_date-1) between l_bpl_esd_rec.effective_start_date
7016                                     and l_bpl_esd_rec.effective_end_date then
7017             --
7018             l_ovn := l_bpl_esd_rec.object_version_number;
7019             l_datetrack_mode := hr_api.g_delete;
7020             l_effective_date := p_effective_date - 1;
7021             exit;
7022             --
7023          end if;
7024        end loop;
7025        close c_bpl_esd;
7026          --
7027          --
7028       ben_Benefit_Prvdd_Ledger_api.delete_Benefit_Prvdd_Ledger(
7029            p_validate              => false,
7030            p_bnft_prvdd_ldgr_id    => l_dbt_bpl_rec.bnft_prvdd_ldgr_id,
7031            p_effective_start_date  => l_effective_start_date,
7032            p_effective_end_date    => l_effective_end_date,
7033            p_object_version_number => l_ovn, --l_dbt_bpl_rec.object_version_number,
7034            p_effective_date        => l_effective_date,
7035            p_datetrack_mode        => l_datetrack_mode,
7036            p_business_group_id     => p_business_group_id,
7037            p_process_enrt_flag     => 'N');
7038       --
7039       hr_utility.set_location(l_proc,15);
7040       --
7041    end if;
7042    --
7043    close c_dbt_bpl;
7044    --
7045    hr_utility.set_location(l_proc,20);
7046    --
7047    -- Loop through all the pools for the program, to compute the total
7048    -- credits available after the de-enrollment.
7049    --
7050    l_total_credits := 0;
7051    --
7052    for l_crdt_bpp_rec in c_crdt_bpp loop
7053       --
7054       --  Set a flag for net credit method processing.
7055       --
7056       if l_crdt_bpp_rec.uses_net_crs_mthd_flag = 'Y' then
7057         l_uses_net_crs_mthd := true;
7058       end if;
7059       --
7060       hr_utility.set_location(l_proc,25);
7061       --
7062       l_flex_rslt_id          := l_crdt_bpp_rec.prtt_enrt_rslt_id;
7063       --
7064       l_epe_rec.pgm_id            := l_crdt_bpp_rec.pgm_id;
7065       l_epe_rec.plip_id           := l_crdt_bpp_rec.plip_id;
7066       l_epe_rec.ptip_id           := l_crdt_bpp_rec.ptip_id;
7067       l_epe_rec.cmbn_ptip_id      := l_crdt_bpp_rec.cmbn_ptip_id;
7068       l_epe_rec.cmbn_plip_id      := l_crdt_bpp_rec.cmbn_plip_id;
7069       l_epe_rec.cmbn_ptip_opt_id  := l_crdt_bpp_rec.cmbn_ptip_opt_id;
7070       l_epe_rec.business_group_id := l_crdt_bpp_rec.business_group_id;
7071       --
7072       -- Check whether the person is still eligible for the pool.
7073       --
7074       l_person_enrolled := person_enrolled_in_choice(
7075                               p_person_id         => p_person_id,
7076                               p_epe_rec           => l_epe_rec,
7077                               p_old_result_id     => p_prtt_enrt_rslt_id,
7078                               p_effective_date    => p_effective_date);
7079       --
7080       if l_person_enrolled then
7081          --
7082          hr_utility.set_location(l_proc,30);
7083          --
7084          l_total_credits := l_total_credits + l_crdt_bpp_rec.prvdd_val;
7085          --
7086          if l_crdt_bpp_rec.bnft_prvdr_pool_id <> l_debit_pool_id then
7087             --
7088             -- Reduce the forfeited credits, to calculate total credits,
7089             -- Only when the pool is not the same as deleted debit's pool,
7090             -- as the forfeited credits will be re-computed in that case.
7091             --
7092             open  c_frftd_bpl(l_crdt_bpp_rec.bnft_prvdr_pool_id);
7093             fetch c_frftd_bpl into l_frftd_bpl_rec;
7094             close c_frftd_bpl;
7095             --
7096             l_total_credits := l_total_credits
7097                                - nvl(l_frftd_bpl_rec.frftd_val,0);
7098             --
7099          end if;
7100          --
7101       else
7102          --
7103          -- Person is not eligible for the pool, so we need to delete
7104          -- all ledgers for the pool.
7105          --
7106          hr_utility.set_location(l_proc,35);
7107          --
7108          l_credit_ledger_deleted := true;
7109          --
7110          if l_crdt_bpp_rec.bnft_prvdr_pool_id = l_debit_pool_id then
7111             --
7112             -- All ledgers for this pool will get deleted, so no need
7113             -- to re-compute excess allocations.
7114             --
7115             l_recompute_excess := false;
7116             --
7117          end if;
7118          --
7119          delete_all_ledgers(
7120                    p_bnft_prvdr_pool_id => l_crdt_bpp_rec.bnft_prvdr_pool_id,
7121                    p_person_id          => p_person_id,
7122                    p_per_in_ler_id      => p_per_in_ler_id,
7123                    p_flex_rslt_id       => l_flex_rslt_id,
7124                    p_effective_date     => p_effective_date,
7125                    p_business_group_id  => p_business_group_id);
7126          --
7127       end if;
7128       --
7129    end loop;
7130    --
7131    hr_utility.set_location(l_proc,40);
7132    --
7133    if not(l_credit_ledger_deleted) and not(l_debit_ledger_deleted) then
7134       --
7135       -- No ledger deletion took place, it means everything is in order.
7136       --
7137       hr_utility.set_location(l_proc,45);
7138       return;
7139       --
7140    elsif l_recompute_excess then
7141       --
7142       -- Re-compute the excess amounts as a debit ledger is deleted,
7143       -- so we have more excess amount available.
7144       --
7145       hr_utility.set_location(l_proc,50);
7146       compute_excess
7147         (p_bnft_prvdr_pool_id  => l_debit_pool_id
7148         ,p_flex_rslt_id        => l_flex_rslt_id
7149         ,p_effective_date      => p_effective_date
7150         ,p_per_in_ler_id       => p_per_in_ler_id
7151         ,p_person_id           => p_person_id
7152         ,p_enrt_mthd_cd        => p_enrt_mthd_cd
7153         ,p_business_group_id   => p_business_group_id
7154         --
7155         ,p_frftd_val           => l_frftd_val
7156         ,p_def_exc_amount      => l_def_exc_amount
7157         ,p_bpl_cash_recd_val   => l_dummy_number
7158         );
7159       hr_utility.set_location('l_def_exc_amount:1 '||l_def_exc_amount ,50);
7160       --
7161       -- Reduce forfeited credits for the pool from which the
7162       -- debit ledger was deleted.
7163       --
7164       l_total_credits := l_total_credits - l_frftd_val;
7165       --
7166    end if;
7167    --
7168    if l_uses_net_crs_mthd then
7169      --
7170      --  Get the participant rt val to determine if the person
7171      --  previously has a net credit or debit amount.
7172      --
7173      for l_prv_rec in c_prv2 loop
7174        l_prv_exists := true;
7175        --
7176        --   Check if the amount has changed.
7177        --
7178        if l_prv_rec.acty_typ_cd = 'NCRDSTR' then
7179          if l_def_exc_amount >= 0 then
7180            if nvl(l_prv_rec.rt_val,0) <> l_def_exc_amount then
7181              --
7182              update_net_credit_rate
7183                (p_prtt_rt_val_id    => l_prv_rec.prtt_rt_val_id
7184                ,p_flex_rslt_id      => l_flex_rslt_id
7185                ,p_acty_base_rt_id   => l_prv_rec.acty_base_rt_id
7186                ,p_per_in_ler_id     => p_per_in_ler_id
7187                ,p_def_exc_amount    => l_def_exc_amount
7188                ,p_effective_date    => p_effective_date
7189                ,p_business_group_id => p_business_group_id
7190                );
7191            end if;
7192          else
7193            --
7194            --  Update the credit distribution rate to zero and write a
7195            --  deduction rate.
7196            --
7197            if nvl(l_prv_rec.rt_val,0) <> 0 then
7198              update_net_credit_rate
7199                (p_prtt_rt_val_id    => l_prv_rec.prtt_rt_val_id
7200                ,p_flex_rslt_id      => l_flex_rslt_id
7201                ,p_acty_base_rt_id   => l_prv_rec.acty_base_rt_id
7202                ,p_per_in_ler_id     => p_per_in_ler_id
7203                ,p_def_exc_amount    => 0
7204                ,p_effective_date    => p_effective_date
7205                ,p_business_group_id => p_business_group_id
7206                );
7207            end if;
7208          end if;
7209        elsif l_prv_rec.acty_typ_cd = 'NCRUDED' then
7210          --
7211          if l_def_exc_amount < 0 then
7212            if nvl(l_prv_rec.rt_val,0) <> abs(l_def_exc_amount) then
7213              update_net_credit_rate
7214                (p_prtt_rt_val_id    => l_prv_rec.prtt_rt_val_id
7215                ,p_flex_rslt_id      => l_flex_rslt_id
7216                ,p_acty_base_rt_id   => l_prv_rec.acty_base_rt_id
7217                ,p_per_in_ler_id     => p_per_in_ler_id
7218                ,p_def_exc_amount    => abs(l_def_exc_amount)
7219                ,p_effective_date    => p_effective_date
7220                ,p_business_group_id => p_business_group_id
7221                );
7222            end if;
7223          else
7224            --
7225            --  Update the Net deduction rate to zero and write a new
7226            --  distribution rate.
7227            --
7228            if nvl(l_prv_rec.rt_val,0) <> 0 then
7229              update_net_credit_rate
7230                (p_prtt_rt_val_id    => l_prv_rec.prtt_rt_val_id
7231                ,p_flex_rslt_id      => l_flex_rslt_id
7232                ,p_acty_base_rt_id   => l_prv_rec.acty_base_rt_id
7233                ,p_per_in_ler_id     => p_per_in_ler_id
7234                ,p_def_exc_amount    => 0
7235                ,p_effective_date    => p_effective_date
7236                ,p_business_group_id => p_business_group_id
7237                );
7238            end if;
7239          end if;
7240        end if;
7241      end loop;
7242        --
7243      if l_prv_exists = false then
7244        if l_def_exc_amount < 0 then
7245          --
7246          open c_abr('NCRUDED');
7247          fetch c_abr into l_acty_base_rt_id;
7248          close c_abr;
7249          --
7250          update_net_credit_rate
7251            (p_prtt_rt_val_id    => null
7252            ,p_flex_rslt_id      => l_flex_rslt_id
7253            ,p_acty_base_rt_id   => l_acty_base_rt_id
7254            ,p_per_in_ler_id     => p_per_in_ler_id
7255            ,p_def_exc_amount    => abs(l_def_exc_amount)
7256            ,p_effective_date    => p_effective_date
7257            ,p_business_group_id => p_business_group_id
7258           );
7259        elsif l_def_exc_amount > 0 then
7260          --
7261          open c_abr('NCRDSTR');
7262          fetch c_abr into l_acty_base_rt_id;
7263          close c_abr;
7264          --
7265          update_net_credit_rate
7266            (p_prtt_rt_val_id    => null
7267            ,p_flex_rslt_id      => l_flex_rslt_id
7268            ,p_acty_base_rt_id   => l_acty_base_rt_id
7269            ,p_per_in_ler_id     => p_per_in_ler_id
7270            ,p_def_exc_amount    => l_def_exc_amount
7271            ,p_effective_date    => p_effective_date
7272            ,p_business_group_id => p_business_group_id
7273           );
7274        end if;
7275      end if;
7276    end if; -- l_uses_net_crs_mthd.
7277    --
7278    open  c_prv;
7279    fetch c_prv into l_prtt_rt_val_id,
7280                     l_rt_val, l_rt_strt_dt;
7281    --
7282    -- Update the flex credit rate with the total credits,
7283    -- if the value has changed.
7284    --
7285    hr_utility.set_location ('Total Credits'||l_total_credits,111);
7286    hr_utility.set_location ('Rate Value '||l_rt_val,111);
7287    if c_prv%found and l_total_credits <> l_rt_val then
7288       --
7289       hr_utility.set_location(l_proc,55);
7290       update_rate(p_prtt_rt_val_id      => l_prtt_rt_val_id,
7291                   p_val                 => l_total_credits,
7292                   p_prtt_enrt_rslt_id   => l_flex_rslt_id,
7293                   p_ended_per_in_ler_id => p_per_in_ler_id,
7294                   p_effective_date      => p_effective_date,
7295                   p_business_group_id   => p_business_group_id);
7296       --
7297    end if;
7298 
7299    close c_prv;
7300 
7301    hr_utility.set_location('Leaving '||l_proc,999);
7302 
7303 end recompute_flex_credits;
7304 --
7305 --
7306 end ben_provider_pools;