DBA Data[Home] [Help]

PACKAGE BODY: APPS.BEN_PROVIDER_POOLS

Source


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