DBA Data[Home] [Help]

PACKAGE BODY: APPS.BEN_DETERMINE_DATE

Source


1 PACKAGE BODY ben_determine_date as
2 /* $Header: bendetdt.pkb 120.30.12010000.2 2008/12/19 13:10:32 sallumwa ship $ */
3 -- --------------------------------------------------------------------
4 -- Get the plan year during the life event or effective date.
5 -- --------------------------------------------------------------------
6 g_debug boolean := hr_utility.debug_enabled;
7 --
8 --
9 -- ----------------------------------------------------------------------------
10 -- |----------------------< get_profile_ff_warn_val >-------------------------|
11 -- ----------------------------------------------------------------------------
12 --
13 -- Description:
14 -- Bug 5088591: The function returns the value of profile BEN_DISP_FF_WARN_MSG.
15 -- If profile is not set then the function returns N
16 --
17 -- ----------------------------------------------------------------------------
18 FUNCTION get_profile_ff_warn_val
19    RETURN VARCHAR2
20 IS
21 --
22 BEGIN
23    --
24    IF g_ben_disp_ff_warn_msg IS NULL
25    THEN
26       --
27       g_ben_disp_ff_warn_msg := nvl(fnd_profile.VALUE ('BEN_DISP_FF_WARN_MSG'), 'Y');
28       --
29    END IF;
30    --
31    RETURN g_ben_disp_ff_warn_msg;
32    --
33 --
34 END get_profile_ff_warn_val;
35 --
36 --
37 procedure  get_plan_year
38            (p_effective_date in date
39            ,p_lf_evt_ocrd_dt in date
40            ,p_pl_id          in number
41            ,p_pgm_id         in number
42            ,p_oipl_id        in number
43            ,p_date_cd        in varchar2
44            ,p_comp_obj_mode  in boolean default true
45            ,p_start_date     out nocopy date
46            ,p_end_date       out nocopy date) is
47 
48   l_proc   varchar2(80)  := g_package ||'ben_determine_date.get_plan_year';
49 
50   l_effective_date date;
51 
52   cursor c_pgm_popl_yr is
53   select yrp.start_date,
54          yrp.end_date
55     from ben_yr_perd yrp,
56          ben_popl_yr_perd cpy
57    where cpy.pgm_id = p_pgm_id
58      and cpy.yr_perd_id = yrp.yr_perd_id
59      and l_effective_date
60          between yrp.start_date
61              and yrp.end_date;
62   --
63   cursor c_pl_popl_yr is
64   select yrp.start_date,
65          yrp.end_date
66     from ben_yr_perd yrp,
67          ben_popl_yr_perd cpy
68    where cpy.pl_id = p_pl_id
69      and cpy.yr_perd_id = yrp.yr_perd_id
70      and l_effective_date
71          between yrp.start_date
72              and yrp.end_date;
73    --
74   cursor c_oipl_popl_yr is
75   select yrp.start_date,
76          yrp.end_date
77     from ben_yr_perd yrp,
78          ben_popl_yr_perd cpy,
79          ben_oipl_f cop
80    where cpy.pl_id = cop.pl_id
81      and cop.oipl_id = p_oipl_id
82      and cpy.yr_perd_id = yrp.yr_perd_id
83      and l_effective_date
84          between yrp.start_date
85              and yrp.end_date
86      and l_effective_date
87          between cop.effective_start_date
88              and cop.effective_end_date;
89 
90 begin
91 
92   l_effective_date := nvl(p_lf_evt_ocrd_dt,p_effective_date);
93   if p_comp_obj_mode then
94     if p_pl_id is not null then
95        open c_pl_popl_yr;
96        fetch c_pl_popl_yr into p_start_date, p_end_date;
97        close c_pl_popl_yr;
98 
99     elsif p_pgm_id is not null then
100       open c_pgm_popl_yr;
101       fetch c_pgm_popl_yr into p_start_date, p_end_date;
102       close c_pgm_popl_yr;
103 
104     elsif p_oipl_id is not null then
105       open c_oipl_popl_yr;
106       fetch c_oipl_popl_yr into p_start_date, p_end_date;
107       close c_oipl_popl_yr;
108 
109     else
110       if g_debug then
111          hr_utility.set_location('BEN_92489_CANNOT_CALC_DATE',55);
112       end if;
113 
114       fnd_message.set_name('BEN','BEN_92489_CANNOT_CALC_DATE');
115       fnd_message.set_token('DATE_CODE',p_date_cd);
116       fnd_message.set_token('L_PROC',l_proc);
117       fnd_message.raise_error;
118     end if;
119   else
120     p_start_date := to_date('01/01/'||to_char(l_effective_date,'YYYY'),'dd/mm/rrrr');
121     p_end_date := to_date('31/12/'||to_char(l_effective_date,'YYYY'),'dd/mm/rrrr');
122 
123   end if;
124 exception   -- nocopy changes
125   --
126   when others then
127     --
128     p_start_date    := null;
129     p_end_date      := null;
130     raise;
131     --
132 end get_plan_year;
133 
134 -- --------------------------------------------------------------------
135 -- Validating Coverage Rate Dates Computed Using Rule
136 -- --------------------------------------------------------------------
137 
138 procedure validate_rule_cd_date
139                       ( p_formula_id            in number,
140 		        p_computed_Date         in date ,
141                         p_lf_evt_ocrd_dt        in date,
142                         p_per_in_ler_id         in number,
143                         p_effective_date        in date,
144                         p_pgm_id                in number,
145                         p_pl_id                 in number,
146                         p_opt_id                in number,
147                         p_person_id             in number
148                       )
149               is
150 
151   cursor c_formula_type_id_name
152   is
153   select fft.formula_type_id,fft.formula_type_name
154   from   ff_formula_types fft,ff_formulas_f ff
155   where  ff.formula_id=p_formula_id
156   and    ff.formula_type_id= fft.formula_type_id
157   and    p_effective_date between ff.effective_start_date and ff.effective_end_date;
158 
159 
160   cursor c_pgm_name
161   is
162   select name
163   from   ben_pgm_f pgm
164   where  pgm_id=p_pgm_id
165   and    p_effective_Date between effective_start_date and effective_end_date;
166 
167   cursor c_pl_name
168   is
169   select name
170   from   ben_pl_f pln
171   where  pl_id=p_pl_id
172   and    p_effective_Date between effective_start_date and effective_end_date;
173 
174   cursor c_opt_name
175   is
176   select name
177   from   ben_opt_f opt
178   where  opt_id=p_opt_id
179   and    p_effective_Date between effective_start_date and effective_end_date;
180 
181   cursor c_mode
182   is
183   select 1
184   from   ben_per_in_ler pil,
185          ben_ler_f ler
186   where  pil.ler_id = ler.ler_id
187   and    pil.per_in_ler_id=p_per_in_ler_id
188   and    ler.typ_cd not like 'SCHED%'
189   and    p_lf_evt_ocrd_dt between effective_start_date and effective_end_date;
190 
191   cursor c_national_identifier
192   is
193   select national_identifier
194   from   per_all_people_f
195   where  person_id=p_person_id;
196 
197 
198   l_message_name1   varchar2(500) := 'BEN_94441_ENROL_ST_DT_RANGE';
199   l_message_name2   varchar2(500) := 'BEN_94464_ENROL_ED_DT_RANGE';
200   l_message_name3   varchar2(500) := 'BEN_93964_ENRO_DT_LT_LE_OCD_DT';
201 
202 
203   l_formula_type_id ff_formulas_f.formula_type_id%type;
204   l_formula_type_name ff_formula_types.formula_type_name%type;
205   l_pgm_name ben_pgm_f.name%type;
206   l_pl_name ben_pl_f.name%type;
207   l_opt_name ben_opt_f.name%type;
208   l_dummy number;
209   l_national_identifier per_all_people_f.national_identifier%type;
210 
211 begin
212    hr_utility.set_location('Entering validate_rule_cd_date',1000);
213 
214    open  c_formula_type_id_name;
215    fetch c_formula_type_id_name into l_formula_type_id,l_formula_type_name;
216    close c_formula_type_id_name;
217 
218 
219    if l_formula_type_id in (-27,-28,-29,-30,-504,-503,-66,-67) then
220 
221     open c_pgm_name;
222     fetch c_pgm_name into l_pgm_name;
223     close c_pgm_name;
224 
225     open c_pl_name;
226     fetch c_pl_name into l_pl_name;
227     close c_pl_name;
228 
229     open c_opt_name;
230     fetch c_opt_name into l_opt_name;
231     close c_opt_name;
232 
233     open  c_national_identifier;
234     fetch c_national_identifier into l_national_identifier;
235     close c_national_identifier;
236 
237   end if;
238 
239    if l_formula_type_id in (-27,-28,-29,-30,-504,-503,-66,-67) then
240           if  p_computed_Date <= hr_api.g_sot then
241 	  fnd_message.set_name('BEN','BEN_94441_ENROL_ST_DT_RANGE');
242 	  fnd_message.set_token('PARMA',l_formula_type_name || ' ' ||fnd_date.date_to_displaydate(p_computed_Date));
243           fnd_message.set_token('PARMB',fnd_date.date_to_displaydate(hr_api.g_sot));
244 	  fnd_message.set_token('PARMC','Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name );
245           benutils.write(p_text=>fnd_message.get);
246           ben_warnings.load_warning
247 		       (p_application_short_name  => 'BEN'
248 		       ,p_message_name            => l_message_name1
249 		       ,p_parma                   => l_formula_type_name || ' ' ||fnd_date.date_to_displaydate(p_computed_Date)
250 		       ,p_parmb    	          => fnd_date.date_to_displaydate(hr_api.g_sot)
251 		       ,p_parmc                   => 'Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name
252 		       ,p_person_id               => p_person_id
253 		       );
254       elsif p_computed_Date >= hr_api.g_eot then
255           fnd_message.set_name('BEN','BEN_94464_ENROL_ED_DT_RANGE');
256   	  fnd_message.set_token('PARMA',l_formula_type_name|| ' ' || fnd_date.date_to_displaydate(p_computed_Date));
257           fnd_message.set_token('PARMB',fnd_date.date_to_displaydate(hr_api.g_eot));
258 	  fnd_message.set_token('PARMC','Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name );
259           benutils.write(p_text=>fnd_message.get);
260 	  ben_warnings.load_warning
261 		       (p_application_short_name  => 'BEN'
262 		       ,p_message_name            => l_message_name2
263 		       ,p_parma                   => l_formula_type_name|| ' ' || fnd_date.date_to_displaydate(p_computed_Date)
264 		       ,p_parmb    	          => fnd_date.date_to_displaydate(hr_api.g_eot)
265 		       ,p_parmc                   => 'Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name
266 		       ,p_person_id               => p_person_id
267 		       );
268 
269     elsif l_formula_type_id in (-29,-66,-67) then
270       if p_computed_Date < p_lf_evt_ocrd_dt  then
271          fnd_message.set_name('BEN','BEN_93964_ENRO_DT_LT_LE_OCD_DT');
272          fnd_message.set_token('PARMA',l_formula_type_name || ' ' ||fnd_date.date_to_displaydate(p_computed_Date));
273          fnd_message.set_token('PARMB','Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name);
274 	 fnd_message.set_token('PARMC',fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt));
275 	 benutils.write(p_text=>fnd_message.get);
276 	 ben_warnings.load_warning
277 		       (p_application_short_name  => 'BEN'
278 		       ,p_message_name            => l_message_name3
279 		       ,p_parma                   => l_formula_type_name || ' ' ||fnd_date.date_to_displaydate(p_computed_Date)
280 		       ,p_parmb                   => 'Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name
281 		       ,p_parmc                   => fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt)
282 		       ,p_person_id               => p_person_id
283 		       );
284        end if;
285 
286 --Bug 5070692
287 
288    elsif l_formula_type_id in (-27,-28) then
289       if p_computed_Date < p_lf_evt_ocrd_dt  then
290          fnd_message.set_name('BEN','BEN_93964_ENRO_DT_LT_LE_OCD_DT');
291          fnd_message.set_token('PARMA',l_formula_type_name || ' ' ||fnd_date.date_to_displaydate(p_computed_Date));
292          fnd_message.set_token('PARMB','Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name);
293 	 fnd_message.set_token('PARMC',fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt));
294 
295 	 g_dep_rec.text := fnd_message.get;
296 	 g_dep_rec.rep_typ_cd := 'WARNING';
297 	 g_dep_rec.error_message_code :='BEN_93964_ENRO_DT_LT_LE_OCD_DT';
298 	 g_dep_rec.national_identifier :=l_national_identifier;
299 	 g_dep_rec.person_id :=p_person_id;
300 	 g_dep_rec.pgm_id :=p_pgm_id;
301 	 g_dep_rec.pl_id :=p_pl_id;
302 
303 --Bug 5070692
304 	 benutils.write(p_rec=>g_dep_rec);
305 	 ben_warnings.load_warning
306 		       (p_application_short_name  => 'BEN'
307 		       ,p_message_name            => l_message_name3
308 		       ,p_parma                   => l_formula_type_name || ' ' ||fnd_date.date_to_displaydate(p_computed_Date)
309 		       ,p_parmb                   => 'Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name
310 		       ,p_parmc                   => fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt)
311 		       ,p_person_id               => p_person_id
312 		       );
313        end if;
314 
315 --Bug 5070692
316 
317 --Bug 5076010
318     elsif l_formula_type_id = -30 then
319       if p_computed_Date < p_lf_evt_ocrd_dt  then
320          fnd_message.set_name('BEN','BEN_93964_ENRO_DT_LT_LE_OCD_DT');
321          fnd_message.set_token('PARMA',substr(l_formula_type_name,1,10) ||' '||'Coverage End Date'|| ' ' ||fnd_date.date_to_displaydate(p_computed_Date));
322          fnd_message.set_token('PARMB','Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name );
323 	 fnd_message.set_token('PARMC',fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt));
324 
325          benutils.write(p_text=>fnd_message.get);
326 	 ben_warnings.load_warning
327 		       (p_application_short_name  => 'BEN'
328 		       ,p_message_name            => l_message_name3
329 		       ,p_parma                   => substr(l_formula_type_name,1,10) ||' '||'Coverage End Date'|| ' ' ||fnd_date.date_to_displaydate(p_computed_Date)
330 		       ,p_parmb                   => 'Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name
331 		       ,p_parmc                   => fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt)
332 		       ,p_person_id               => p_person_id
333 		       );
334        end if;
335 --Bug 5076010
336 
337     else
338       open c_mode;
339       fetch c_mode into l_dummy;
340       if c_mode%found then
341         if p_computed_Date < p_lf_evt_ocrd_dt  then
342          fnd_message.set_name('BEN','BEN_93964_ENRO_DT_LT_LE_OCD_DT');
343          fnd_message.set_token('PARMA',l_formula_type_name|| ' ' ||fnd_date.date_to_displaydate(p_computed_Date));
344          fnd_message.set_token('PARMB','Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name );
345 	 fnd_message.set_token('PARMC',fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt));
346 
347            benutils.write(p_text=>fnd_message.get);
348            ben_warnings.load_warning
349                (p_application_short_name  => 'BEN'
350 	           ,p_message_name            => l_message_name3
351 	           ,p_parma                   => l_formula_type_name|| ' ' ||fnd_date.date_to_displaydate(p_computed_Date)
352 	           ,p_parmb                   => 'Program:'||' '|| l_pgm_name ||' '||'-'||' '||'Plan:'||' '|| l_pl_name ||' '||'-'||' '||'Option:'||' '|| l_opt_name
353 	           ,p_parmc                   => fnd_date.date_to_displaydate(p_lf_evt_ocrd_dt)
354 	           ,p_person_id               => p_person_id);
355         end if;
356       end if;
357       --
358       close c_mode; -- Bug fix 5057685. Moved here
359       --
360     end if;
361 end if;
362 hr_utility.set_location('Leaving validate_rule_cd_date',8888);
363 end validate_rule_cd_date;
364 
365 -- --------------------------------------------------------------------
366 -- Get the plan year that starts after the life event or effective date.
367 -- --------------------------------------------------------------------
368 procedure  get_next_plan_year
369            (p_effective_date in date
370            ,p_lf_evt_ocrd_dt in date
371            ,p_pl_id          in number
372            ,p_pgm_id         in number
373            ,p_oipl_id        in number
374            ,p_date_cd        in varchar2
375            ,p_comp_obj_mode  in boolean default true
376            ,p_start_date     out nocopy date
377            ,p_end_date       out nocopy date) is
378 
379   l_proc   varchar2(80)  := g_package ||'ben_determine_date.get_next_plan_year';
380   l_effective_date date;
381 
382   cursor c_pgm_next_popl_yr is
383   select yrp.start_date, yrp.end_date
384     from ben_yr_perd yrp,
385          ben_popl_yr_perd cpy
386    where cpy.pgm_id = p_pgm_id
387      and cpy.yr_perd_id = yrp.yr_perd_id
388      and l_effective_date < yrp.start_date
389    order by 1;
390   --
391   cursor c_pl_next_popl_yr is
392   select yrp.start_date, yrp.end_date
393     from ben_yr_perd yrp,
394          ben_popl_yr_perd cpy
395    where cpy.pl_id = p_pl_id
396      and cpy.yr_perd_id = yrp.yr_perd_id
397      and l_effective_date < yrp.start_date
398    order by 1;
399   --
400   cursor c_oipl_next_popl_yr is
401   select yrp.start_date, yrp.end_date
402     from ben_yr_perd yrp,
403          ben_popl_yr_perd cpy,
404          ben_oipl_f cop
405    where cpy.pl_id = cop.pl_id
406      and cop.oipl_id = p_oipl_id
407      and cpy.yr_perd_id = yrp.yr_perd_id
408      and l_effective_date < yrp.start_date
409      and l_effective_date between cop.effective_start_date
410      and cop.effective_end_date
411    order by 1;
412 
413 begin
414 
415   l_effective_date := nvl(p_lf_evt_ocrd_dt,p_effective_date);
416   if p_comp_obj_mode then
417     if p_pl_id is not null then
418       open c_pl_next_popl_yr;
419       fetch c_pl_next_popl_yr into p_start_date,p_end_date;
420       close c_pl_next_popl_yr;
421     elsif p_pgm_id is not null then
422       open c_pgm_next_popl_yr;
423       fetch c_pgm_next_popl_yr into  p_start_date,p_end_date;
424       close c_pgm_next_popl_yr;
425     elsif p_oipl_id is not null then
426       open c_oipl_next_popl_yr;
427       fetch c_oipl_next_popl_yr into  p_start_date,p_end_date;
428       close c_oipl_next_popl_yr;
429     else
430     if g_debug then
431         hr_utility.set_location('BEN_92489_CANNOT_CALC_DATE',55);
432     end if;
433       fnd_message.set_name('BEN','BEN_92489_CANNOT_CALC_DATE');
434       fnd_message.set_token('DATE_CODE',p_date_cd);
435       fnd_message.set_token('L_PROC',l_proc);
436       fnd_message.raise_error;
437     end if;
438   else
439     p_start_date := to_date('01/01/'||to_char(l_effective_date,'YYYY'),'dd/mm/rrrr');
440     p_end_date := to_date('31/12/'||to_char(l_effective_date,'YYYY'),'dd/mm/rrrr');
441 
442   end if;
443 exception   -- nocopy changes
444   --
445   when others then
446     --
447     p_start_date    := null;
448     p_end_date      := null;
449     raise;
450     --
451 end get_next_plan_year;
452 
453 -- --------------------------------------------------------------------
454 -- function get_event_date used to make consistent event date handling
455 -- for all codes which need it.
456 -- --------------------------------------------------------------------
457 function get_event_date
458   (p_cache_mode     in     boolean default false
459 /*
460   ,p_pil_row        in     ben_per_in_ler%rowtype
461 */
462   ,p_per_in_ler_id  in     number
463   ,p_effective_date in     date
464   ) return date
465 is
466   --
467   l_event_date         date;
468   --
469   cursor c_per_in_ler
470   is
471   select pil.lf_evt_ocrd_dt
472     from ben_per_in_ler pil
473    where pil.per_in_ler_id = p_per_in_ler_id;
474   --
475 begin
476   if p_per_in_ler_id is null then
477     l_event_date:=p_effective_date;
478   else
479     open c_per_in_ler;
480     fetch c_per_in_ler into l_event_date;
481     if c_per_in_ler%notfound then
482       close c_per_in_ler;
483     if g_debug then
484         hr_utility.set_location('BEN_91530_CANNOT_FIND_AED_DATE',40);
485     end if;
486       fnd_message.set_name('BEN','BEN_91530_CANNOT_FIND_AED_DATE');
487       fnd_message.raise_error;
488     end if;
489     close c_per_in_ler;
490   end if;
491   --
492   -- Single exit point for readability
493   --
494   return l_event_date;
495   --
496 end get_event_date;
497 --
498 --
499 -- function get_recorded_date used to make consistent date handling
500 -- for all new life event codes.
501 --
502 function get_recorded_date
503   (p_cache_mode     in     boolean default false
504 /*
505   ,p_pil_row        in     ben_per_in_ler%rowtype
506 */
507   ,p_per_in_ler_id  in     number
508   ,p_effective_date in     date
509   ) return date
510 is
511   --
512   l_pil_row        ben_pil_cache.g_pil_inst_row;
513   --
514   l_recorded_date  date;
515   --
516 /*
517   cursor c_per_in_ler
518   is
519   select pil.ntfn_dt
520     from ben_per_in_ler pil
521    where pil.per_in_ler_id = p_per_in_ler_id;
522   --
523 */
524 begin
525   --
526   if p_per_in_ler_id is null then
527     --
528     l_recorded_date := p_effective_date;
529     --
530 /*
531   elsif p_cache_mode
532     and p_pil_row.per_in_ler_id is not null
533   then
534     --
535     l_recorded_date := p_pil_row.ntfn_dt;
536     --
537 */
538   else
539     --
540     ben_pil_cache.PIL_GetPILDets
541       (p_per_in_ler_id => p_per_in_ler_id
542       ,p_inst_row      => l_pil_row
543       );
544     --
545     l_recorded_date := l_pil_row.ntfn_dt;
546     --
547     if l_pil_row.per_in_ler_id is null then
548       --
549       fnd_message.set_name('BEN','BEN_92391_CANT_FIND_RCRD_DATE');
550       fnd_message.raise_error;
551       --
552     end if;
553     --
554 /*
555   else
556     --
557     open c_per_in_ler;
558     fetch c_per_in_ler into l_recorded_date;
559     if c_per_in_ler%notfound then
560       close c_per_in_ler;
561       if g_debug then
562         hr_utility.set_location('BEN_92391_CANT_FIND_RCRD_DATE',40);
563       end if;
564       fnd_message.set_name('BEN','BEN_92391_CANT_FIND_RCRD_DATE');
565       fnd_message.raise_error;
566     end if;
567     close c_per_in_ler;
568     --
569 */
570   end if;
571   --
572   if l_recorded_date is null then
573     --
574     return p_effective_date;
575     --
576   else
577     --
578     return l_recorded_date;
579     --
580   end if;
581   --
582 end get_recorded_date;
583 --
584 procedure main
585   (p_cache_mode             in     boolean  default false
586   --
587   ,p_date_cd                in     varchar2
588   ,p_per_in_ler_id          in     number   default null
589   ,p_person_id              in     number   default null
590   ,p_pgm_id                 in     number   default null
591   ,p_pl_id                  in     number   default null
592   ,p_oipl_id                in     number   default null
593   ,p_elig_per_elctbl_chc_id in     number   default null -- optional for all
594   ,p_business_group_id      in     number   default null
595   ,p_formula_id             in     number   default null
596   ,p_acty_base_rt_id        in     number   default null -- as a context to formula calls
597   ,p_bnfts_bal_id           in     number   default null
598   ,p_effective_date         in     date
599   ,p_lf_evt_ocrd_dt         in     date     default null
600   ,p_start_date             in     date     default null
601   ,p_returned_date          out nocopy    date
602   ,p_parent_person_id       in     number   default null
603  -- Added two more parameters to fix the Bug 1531647
604   ,p_param1                 in     varchar2 default null
605   ,p_param1_value           in     varchar2 default null
606   ,p_enrt_cvg_end_dt        in     date     default null
607   ,p_comp_obj_mode          in     boolean  default true
608   ,p_fonm_cvg_strt_dt       in     date default null
609   ,p_fonm_rt_strt_dt        in     date default null
610   ,p_cmpltd_dt              in     date default null
611   )
612 is
613 --
614   l_proc               varchar2(80)  := g_package ||'.determine_date.main';
615   l_per_in_ler_id      number;
616   l_person_id          number;
617   l_pgm_id             number;
618   l_pl_id              number;
619   l_pl_typ_id          number;
620   l_ler_id             number;
621   l_oipl_id            number;
622   l_business_group_id  number;
623   l_date               date;
624   l_start_date         date;
625   l_end_date           date;
626   l_procg_end_dt       date;
627   l_next_popl_yr_strt  date;
628   l_next_popl_yr_end   date;
629   l_months             number;
630   l_outputs            ff_exec.outputs_t;
631   l_event_date         date;
632   l_recorded_date      date;
633   l_lf_evt_ocrd_dt     date;
634   l_hire_date          date;
635   l_jurisdiction_code  varchar2(30);
636   l_enrt_eff_strt_date date;
637   l_date_temp date;
638   --
639   l_pil_row            ben_per_in_ler%rowtype;
640   --
641  cursor c_asg is
642   select asg.assignment_id,asg.organization_id
643     from per_all_assignments_f asg
644    where asg.person_id = l_person_id
645      and asg.assignment_type <> 'C'
646      and asg.primary_flag = 'Y'
647      and nvl(p_fonm_cvg_strt_dt, nvl(p_lf_evt_ocrd_dt,p_effective_date)) between asg.effective_start_date
648                                                                          and asg.effective_end_date
649      order by asg.assignment_type desc , asg.effective_start_date desc ;
650   --
651   l_asg c_asg%rowtype;
652   --
653 
654   cursor c_per_elig_elctbl_chc is
655   select pil.per_in_ler_id,
656          pil.person_id,
657          epe.pgm_id,
658          epe.pl_id,
659          epe.pl_typ_id,
660          epe.oipl_id,
661          pil.ler_id,
662          epe.business_group_id,
663          epe.enrt_cvg_strt_dt
664     from ben_per_in_ler pil,
665          ben_elig_per_elctbl_chc epe
666    where epe.elig_per_elctbl_chc_id = p_elig_per_elctbl_chc_id
667      and epe.per_in_ler_id = pil.per_in_ler_id;
668   --
669   cursor c_opt(l_oipl_id  number) is
670     select opt_id
671     from ben_oipl_f oipl
672     where oipl_id = l_oipl_id
673         and business_group_id   = p_business_group_id
674         and nvl(l_lf_evt_ocrd_dt,p_effective_date)
675          between oipl.effective_start_date
676              and oipl.effective_end_date;
677   --
678   l_opt c_opt%rowtype;
679   --
680   cursor c_pl_typ(l_pl_id  number) is
681     select pl.pl_typ_id
682     from ben_pl_f pl
683     where pl.pl_id = l_pl_id
684         and pl.business_group_id   = p_business_group_id
685         and nvl(l_lf_evt_ocrd_dt,p_effective_date)
686          between pl.effective_start_date
687              and pl.effective_end_date;
688   --
689   cursor c_ler is
690     select pil.ler_id
691     from   ben_per_in_ler pil
692     where  pil.per_in_ler_id = p_per_in_ler_id
693       and  pil.business_group_id   = p_business_group_id;
694   --
695   cursor c_pay_period is
696   select tpe.start_date,
697          tpe.end_date
698     from per_time_periods tpe,
699          per_all_assignments_f asg
700    where tpe.payroll_id = asg.payroll_id
701      and   asg.assignment_type <> 'C'
702      and asg.business_group_id = l_business_group_id
703      and asg.person_id = l_person_id
704      and asg.primary_flag = 'Y'
705      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
706           between asg.effective_start_date
707               and asg.effective_end_date
708      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
709          between tpe.start_date
710              and tpe.end_date
711      --and rownum = 1
712   order by decode(asg.assignment_type,'E',1,2) asc;
713   --
714   Cursor c_state is
715   select region_2
716   from hr_locations_all loc,per_all_assignments_f asg
717   where loc.location_id = asg.location_id
718   and asg.person_id = p_person_id
719        and   asg.assignment_type <> 'C'
720        and p_effective_date between
721              asg.effective_start_date and asg.effective_end_date
722        and asg.business_group_id =p_business_group_id;
723 
724   l_state      c_state%rowtype;
725 
726   l_pay_period c_pay_period%rowtype;
727   --
728   cursor c_pay_period_for_date(p_date_dt date) is
729   select tpe.start_date,
730          tpe.end_date
731     from per_time_periods tpe,
732          per_all_assignments_f asg
733    where tpe.payroll_id = asg.payroll_id
734      and   asg.assignment_type <> 'C'
735      and asg.business_group_id = l_business_group_id
736      and asg.person_id = l_person_id
737      and asg.primary_flag = 'Y'
738      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
739            between asg.effective_start_date
740                and asg.effective_end_date
741      and p_date_dt between tpe.start_date and tpe.end_date
742   order by decode(asg.assignment_type,'E',1,2) asc;
743   --
744   --   and rownum = 1;
745   --
746   l_pay_period_for_date c_pay_period_for_date%rowtype;
747   --
748   --pay period on check date
749   cursor c_pay_period_for_check (p_date_dt date, p_assignment_type varchar2) is
750   select min(tpe.start_date )
751     from per_time_periods tpe,
752          per_all_assignments_f asg
753    where tpe.payroll_id = asg.payroll_id
754      and   asg.assignment_type = p_assignment_type
755      and asg.business_group_id = l_business_group_id
756      and asg.person_id = l_person_id
757      and asg.primary_flag = 'Y'
758      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
759            between asg.effective_start_date
760                and asg.effective_end_date
761      and p_date_dt <= nvl(tpe.regular_payment_date,tpe.end_date);
762   --
763   l_start_date_check   date;
764   --
765   cursor c_pay_period_for_check_end
766           (p_date_dt date, p_assignment_type varchar2) is
767   select max(tpe.end_date )
768     from per_time_periods tpe,
769          per_all_assignments_f asg
770    where tpe.payroll_id = asg.payroll_id
771      and   asg.assignment_type = p_assignment_type
772      and asg.business_group_id = l_business_group_id
773      and asg.person_id = l_person_id
774      and asg.primary_flag = 'Y'
775      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
776            between asg.effective_start_date
777                and asg.effective_end_date
778      and p_date_dt > nvl(tpe.regular_payment_date,tpe.end_date);
779   --
780   l_end_date_check     date;
781   --
782   cursor c_next_pay_period(p_date_dt date) is
783   select tpe.start_date,
784          tpe.end_date
785     from per_time_periods tpe,
786          per_all_assignments_f asg
787    where tpe.payroll_id = asg.payroll_id
788      and   asg.assignment_type <> 'C'
789      and asg.business_group_id = l_business_group_id
790      and asg.person_id = l_person_id
791      and asg.primary_flag = 'Y'
792      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
793          between asg.effective_start_date
794              and asg.effective_end_date
795      and tpe.start_date > p_date_dt
796   order by decode(asg.assignment_type,'E',1,2) asc,
797         tpe.start_date;
798   --   and rownum = 1
799   --
800   l_next_pay_period c_next_pay_period%rowtype;
801   --
802   --6025969 fix
803   cursor c_pre_pay_period(p_date_dt date) is
804   select tpe.start_date,
805          tpe.end_date
806     from per_time_periods tpe,
807          per_all_assignments_f asg
808    where tpe.payroll_id = asg.payroll_id
809      and   asg.assignment_type <> 'C'
810      and asg.business_group_id = l_business_group_id
811      and asg.person_id = l_person_id
812      and asg.primary_flag = 'Y'
813      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
814          between asg.effective_start_date
815              and asg.effective_end_date
816      and tpe.end_date < p_date_dt
817   order by decode(asg.assignment_type,'E',1,2) asc,
818         tpe.end_date desc;
819 
820   l_pre_pay_period c_pre_pay_period%rowtype;
821   --
822 
823   cursor c_pps is
824   select date_start
825   from per_periods_of_service  pps
826   where pps.person_id = p_person_id
827     and pps.date_start = (select max(pps1.date_start) -- this gets most recent
828                             from per_periods_of_service pps1
829                            where pps1.person_id = p_person_id
830                              and pps1.date_start = nvl(l_lf_evt_ocrd_dt,p_effective_date )
831                           );
832   --
833   cursor c_hire_date is
834     select max(date_start)
835     from per_periods_of_service  pps
836     where pps.person_id = p_person_id
837     and   pps.date_start <= nvl(l_lf_evt_ocrd_dt,p_effective_date);
838 
839   --
840   cursor c_pgm_popl_lim_yr is
841   select yrp.lmtn_yr_strt_dt,
842          yrp.lmtn_yr_end_dt
843     from ben_yr_perd yrp,
844          ben_popl_yr_perd cpy
845    where cpy.pgm_id = l_pgm_id
846      and cpy.yr_perd_id = yrp.yr_perd_id
847      and yrp.business_group_id   = l_business_group_id
848      and cpy.business_group_id   = l_business_group_id
849      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
850          between yrp.start_date
851              and yrp.end_date;
852   --
853   cursor c_pl_popl_lim_yr is
854   select yrp.lmtn_yr_strt_dt,
855          yrp.lmtn_yr_end_dt
856     from ben_yr_perd yrp,
857          ben_popl_yr_perd cpy
858    where cpy.pl_id = l_pl_id
859      and cpy.yr_perd_id = yrp.yr_perd_id
860      and yrp.business_group_id   = l_business_group_id
861      and cpy.business_group_id   = l_business_group_id
862      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
863          between yrp.start_date
864              and yrp.end_date;
865   --
866   cursor c_oipl_popl_lim_yr is
867   select yrp.lmtn_yr_strt_dt,
868          yrp.lmtn_yr_end_dt
869     from ben_yr_perd yrp,
870          ben_popl_yr_perd cpy,
871          ben_oipl_f cop
872    where cpy.pl_id = cop.pl_id
873      and cop.oipl_id = l_oipl_id
874      and cpy.yr_perd_id = yrp.yr_perd_id
875      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
876          between yrp.start_date
877              and yrp.end_date
878      and cpy.business_group_id   = l_business_group_id
879      and cop.business_group_id   = l_business_group_id
880      and yrp.business_group_id   = l_business_group_id
881      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
882          between cop.effective_start_date
883              and cop.effective_end_date;
884   --
885   cursor c_pil_popl is
886   select pel.enrt_perd_end_dt,
887          pel.procg_end_dt
888     from ben_pil_elctbl_chc_popl pel,
889          ben_elig_per_elctbl_chc epe
890    where epe.elig_per_elctbl_chc_id = p_elig_per_elctbl_chc_id
891      and epe.business_group_id   = l_business_group_id
892      and epe.pil_elctbl_chc_popl_id = pel.pil_elctbl_chc_popl_id
893      and pel.business_group_id   = l_business_group_id;
894   --
895   l_enrt_end_dt date;
896   --
897   --  Removed MIN() grouping for this cursor.  Based on the
898   --  where clause this should only return 1 row.
899   --
900   cursor c_cm_enrt_perd_strt_dt is
901   select enrt_perd_strt_dt
902   from ben_pil_elctbl_chc_popl
903   where per_in_ler_id = l_per_in_ler_id
904   and ((l_pgm_id is not null and pgm_id = l_pgm_id)
905        or (l_pgm_id is null and pl_id = l_pl_id)
906        or (l_pgm_id is null and l_pl_id is null))
907   and business_group_id   = l_business_group_id;
908   --
909   cursor c_cm_enrt_perd_end_dt is
910   select min(enrt_perd_end_dt)
911   from ben_pil_elctbl_chc_popl
912   where per_in_ler_id = l_per_in_ler_id
913   and ((l_pgm_id is not null and pgm_id = l_pgm_id)
914        or (l_pgm_id is null and pl_id = l_pl_id)
915        or (l_pgm_id is null and l_pl_id is null))
916   and business_group_id   = l_business_group_id;
917   --
918   cursor c_cm_dflt_asnd_dt is
919   select min(dflt_asnd_dt)
920   from ben_pil_elctbl_chc_popl
921   where per_in_ler_id = l_per_in_ler_id
922   and ((l_pgm_id is not null and pgm_id = l_pgm_id)
923        or (l_pgm_id is null and pl_id = l_pl_id)
924        or (l_pgm_id is null and l_pl_id is null))
925   and business_group_id   = l_business_group_id;
926   --
927   cursor c_cm_auto_asnd_dt is
928   select min(auto_asnd_dt)
929   from ben_pil_elctbl_chc_popl
930   where per_in_ler_id = l_per_in_ler_id
931   and ((l_pgm_id is not null and pgm_id = l_pgm_id)
932        or (l_pgm_id is null and pl_id = l_pl_id)
933        or (l_pgm_id is null and l_pl_id is null))
934   and business_group_id   = l_business_group_id;
935   --
936   cursor c_cm_elcns_made_dt is
937   select min(elcns_made_dt)
938   from ben_pil_elctbl_chc_popl
939   where per_in_ler_id = l_per_in_ler_id
940   and ((l_pgm_id is not null and pgm_id = l_pgm_id)
941        or (l_pgm_id is null and pl_id = l_pl_id)
942        or (l_pgm_id is null and l_pl_id is null))
943   and business_group_id   = l_business_group_id;
944   --
945   cursor c_cm_elig_prtn_strt_dt is
946   select min(prtn_strt_dt)
947   from ben_elig_per_f pep, ben_per_in_ler pil
948   where pep.person_id = p_person_id
949   and   pep.business_group_id   = p_business_group_id
950   and   p_effective_date = pep.effective_start_date
951   and   pil.per_in_ler_id(+) = pep.per_in_ler_id
952   and   pil.business_group_id = p_business_group_id
953   and   (   pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT')
954          or
955             pil.per_in_ler_stat_cd is null);
956   --
957   cursor c_cm_elig_prtn_end_dt is
958   select min(prtn_end_dt)
959   from ben_elig_per_f pep, ben_per_in_ler pil
960   where pep.person_id = p_person_id
961 --  and   pep.business_group_id   = p_business_group_id
962   and   nvl(p_fonm_cvg_strt_dt,p_effective_date )  = pep.effective_start_date
963   and   pil.per_in_ler_id(+) = pep.per_in_ler_id
964   and   pil.business_group_id = p_business_group_id
965   and   (   pil.per_in_ler_stat_cd not in ('VOIDD', 'BCKDT')
966          or
967             pil.per_in_ler_stat_cd is null);
968   --
969   cursor c_elig_cvg_dpnt_dts is
970   select  ecd.cvg_thru_dt,
971           ecd.effective_end_date
972     from  ben_elig_cvrd_dpnt_f ecd
973     where ecd.per_in_ler_id = l_per_in_ler_id
974       and ecd.business_group_id  = l_business_group_id
975       and nvl(l_lf_evt_ocrd_dt,p_effective_date)
976           between ecd.effective_start_date
977               and ecd.effective_end_date;
978   --
979   cursor c_prtt_enrt_rslt_dts is
980   select  pen.enrt_cvg_thru_dt,
981           pen.enrt_cvg_strt_dt,
982           pen.effective_start_date
983     from  ben_prtt_enrt_rslt_f pen
984     where pen.per_in_ler_id = l_per_in_ler_id and
985           pen.pl_id=l_pl_id
986       and nvl(pen.pgm_id,-1)=nvl(l_pgm_id,-1)
987       and nvl(pen.oipl_id,-1)=nvl(l_oipl_id,-1)
988       and pen.business_group_id  = l_business_group_id
989 -- Bug 1633284
990 /*
991       and nvl(l_lf_evt_ocrd_dt,p_effective_date)
992           between pen.effective_start_date
993               and pen.effective_end_date; */
994       and p_effective_date
995           between pen.effective_start_date
996              and pen.effective_end_date
997 --
998 -- Bug 4309203 Modified the effective_end_date to enrt_cvg_thru_dt as
999 --             effective_end_date would pick up invalid records.
1000       and pen.enrt_cvg_thru_dt = hr_api.g_eot ;
1001   --
1002 /*
1003   cursor c_prtt_rt_val_dts is
1004   select  prv.rt_strt_dt,
1005           prv.rt_end_dt
1006 
1007     from  ben_prtt_rt_val prv,
1008           ben_prtt_enrt_rslt_f pen
1009     where pen.per_in_ler_id = l_per_in_ler_id
1010       and pen.pl_id=l_pl_id
1011       and nvl(pen.pgm_id,-1)=nvl(l_pgm_id,-1)
1012       and nvl(pen.oipl_id,-1)=nvl(l_oipl_id,-1)
1013       and pen.prtt_enrt_rslt_id = prv.prtt_enrt_rslt_id
1014       and prv.business_group_id  = l_business_group_id
1015       and nvl(l_lf_evt_ocrd_dt,p_effective_date)
1016           between pen.effective_start_date
1017               and pen.effective_end_date;
1018 */
1019 --
1020   cursor c_prtt_rt_val_dts is
1021   select null
1022     from
1023           ben_prtt_enrt_rslt_f pen
1024     where pen.per_in_ler_id = l_per_in_ler_id
1025       and pen.pl_id=l_pl_id
1026       and nvl(pen.pgm_id,-1)=nvl(l_pgm_id,-1)
1027 --      and nvl(pen.oipl_id,-1)=nvl(l_oipl_id,-1)
1028 --      and pen.prtt_enrt_rslt_id = prv.prtt_enrt_rslt_id
1029       and pen.business_group_id  = l_business_group_id
1030       and p_effective_date
1031           between pen.effective_start_date
1032               and pen.effective_end_date;
1033 --
1034 -- Added to fix code ODBEWM
1035 --
1036    cursor c_enrt_rt_val_dt is
1037    select er.rt_strt_dt
1038     from  ben_enrt_rt er,
1039           ben_enrt_bnft eb
1040    where  eb.elig_per_elctbl_chc_id =p_elig_per_elctbl_chc_id
1041    and    er.elig_per_elctbl_chc_id is null
1042    and    eb.enrt_bnft_id=er.enrt_bnft_id
1043    --
1044    union
1045    --
1046    select er.rt_strt_dt
1047     from  ben_enrt_rt er
1048     where er.elig_per_elctbl_chc_id = p_elig_per_elctbl_chc_id ;
1049 
1050   cursor c_elig_dpnt_dts is
1051   select  ecd.cvg_strt_dt,
1052           ecd.cvg_thru_dt
1053     from  ben_elig_cvrd_dpnt_f ecd
1054     where ecd.per_in_ler_id = l_per_in_ler_id
1055       and ecd.business_group_id  = l_business_group_id
1056       and nvl(l_lf_evt_ocrd_dt,p_effective_date)
1057           between ecd.effective_start_date
1058               and ecd.effective_end_date;
1059 
1060   cursor c_birth_date is
1061   select  paf.date_of_birth
1062     from  per_all_people_f paf
1063     where paf.person_id = p_person_id;
1064   --
1065   cursor c_ler_id is
1066     select pil.ler_id
1067     from   ben_per_in_ler pil
1068     where  pil.per_in_ler_id = p_per_in_ler_id;
1069   --
1070  cursor c_pay_id is
1071   select asg.payroll_id
1072     from per_time_periods tpe,
1073          per_all_assignments_f asg
1074    where tpe.payroll_id = asg.payroll_id
1075      and   asg.assignment_type <> 'C'
1076      and asg.business_group_id = l_business_group_id
1077      and asg.person_id = l_person_id
1078      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
1079           between asg.effective_start_date
1080               and asg.effective_end_date
1081      and nvl(l_lf_evt_ocrd_dt,p_effective_date)
1082          between tpe.start_date
1083              and tpe.end_date
1084   order by decode(asg.assignment_type,'E',1,2) asc;
1085 
1086 
1087   l_payroll_id  number ;
1088   --
1089   l_enrt_cvg_end_dt date;
1090   l_enrt_cvg_strt_dt date;
1091   l_cvg_thru_dt date;
1092   l_effective_end_date date;
1093   l_cm_date date;
1094   l_dummy   varchar2(30) ;
1095   l_fonm_rt_cvg_strt_dt  date ;
1096   l_rl_lf_evt_ocrd_dt    date ;
1097   l_cmpltd_dt            date ;
1098   l_ben_disp_ff_warn_msg varchar2(10);
1099   l_env   ben_env_object.g_global_env_rec_type;  -- 6823087 (CWB Requirement for 'ENTRBL')
1100   l_mode  l_env.mode_cd%TYPE;                    -- 6823087 (CWB Requirement for 'ENTRBL')
1101 --
1102 begin
1103 
1104 --    hr_utility.trace_on (null, 'ORACLE');
1105 
1106   g_debug := hr_utility.debug_enabled;
1107   --
1108   -- Commented out for performance
1109   --
1110   --
1111   --  hr_utility.set_location('Entering :'|| l_proc,10);
1112   --        comment these out until we need to debug a code
1113   --  hr_utility.set_location('p_date_cd               : '||p_date_cd                ,15);
1114   --  hr_utility.set_location('p_per_in_ler_id         : '||p_per_in_ler_id          ,15);
1115   --  hr_utility.set_location('p_person_id             : '||p_person_id              ,15);
1116   --  hr_utility.set_location('p_pgm_id                : '||p_pgm_id                 ,15);
1117   --  hr_utility.set_location('p_pl_id                 : '||p_pl_id                  ,15);
1118   --  hr_utility.set_location('p_oipl_id               : '||p_oipl_id                ,15);
1119   --  hr_utility.set_location('p_elig_per_elctbl_chc_id: '||p_elig_per_elctbl_chc_id ,15);
1120   --  hr_utility.set_location('p_effective_date        : '||p_effective_date         ,15);
1121   --  hr_utility.set_location('p_lf_evt_ocrd_dt        : '||p_lf_evt_ocrd_dt         ,15);
1122   --  hr_utility.set_location('p_start_date            : '||p_start_date             ,15);
1123   --  hr_utility.set_location('person         : '||p_person_id         ,665);
1124   --  hr_utility.set_location('parent         : '||p_parent_person_id         ,665);
1125   --
1126   --- Fonm2  Determine the fonm and effective date
1127   l_fonm_rt_cvg_strt_dt  :=  nvl(p_fonm_cvg_strt_dt,p_fonm_rt_strt_dt) ;
1128   l_lf_evt_ocrd_dt      :=  nvl(l_fonm_rt_cvg_strt_dt, p_lf_evt_ocrd_dt ) ;
1129   ---
1130 
1131 
1132 
1133   If p_elig_per_elctbl_chc_id  is not null then
1134     --
1135     -- If electible choice id has a value, then gather other important data
1136     -- needed by routines below.  If electable choice is null, then the calling
1137     -- procedures will have to provide these values:   per_in_ler_id,
1138     -- person_id, pl_id, oipl_id, pgm_id.
1139     --
1140    if g_debug then
1141      hr_utility.set_location('open c_per_elig_elctbl_chc',10);
1142    end if;
1143 
1144     open c_per_elig_elctbl_chc;
1145 
1146     fetch c_per_elig_elctbl_chc into
1147           l_per_in_ler_id,
1148           l_person_id,
1149           l_pgm_id,
1150           l_pl_id,
1151           l_pl_typ_id,
1152           l_oipl_id,
1153           l_ler_id,
1154           l_business_group_id,
1155           l_enrt_cvg_strt_dt;
1156 
1157     if g_debug then
1158       hr_utility.set_location('l_enrt_cvg_strt_dt from c_per_elig_elctbl_chc'||l_enrt_cvg_strt_dt,19);
1159     end if;
1160 
1161     if c_per_elig_elctbl_chc%notfound then
1162       close c_per_elig_elctbl_chc;
1163     if g_debug then
1164         hr_utility.set_location('BEN_91529_CANNOT_FIND_ELEC_CHC',40);
1165     end if;
1166       fnd_message.set_name('BEN','BEN_91529_CANNOT_FIND_ELEC_CHC');
1167       fnd_message.set_token('L_PROC',l_proc);
1168       fnd_message.set_token('ELIG_PER_ELCTBL_CHC_ID',p_elig_per_elctbl_chc_id);
1169       fnd_message.raise_error;
1170     end if;
1171     close c_per_elig_elctbl_chc;
1172 
1173   else
1174 
1175     l_per_in_ler_id     := p_per_in_ler_id;
1176     l_person_id         := p_person_id;
1177     l_pgm_id            := p_pgm_id;
1178     l_pl_id             := p_pl_id;
1179     l_oipl_id           := p_oipl_id;
1180     l_business_group_id := p_business_group_id;
1181     --
1182 /*
1183     --
1184     -- Removed because called twice
1185     --
1186     open c_ler_id;
1187       --
1188       fetch c_ler_id into l_ler_id;
1189       --
1190     close c_ler_id;
1191 */
1192     --
1193   end if;
1194   --
1195   -- when the date determinne from pay period for the  contact
1196   -- decide whether the contct has pay_period  bug 1510665
1197 
1198 
1199   if p_parent_person_id is not null   and p_date_cd in
1200       ('AFDCPP','AFDFPP','ALDCPP','ALDLPPEPPY',
1201        'FDLPPEPPY','FDLPPEPPYCF','FDPPCF','LAFDFPP','LALDCPP','LALDLPPEPPY',
1202        'LFDPPCF', 'LWALDCPP','FDPPCF','LALDPPP','WALDCPP','WALDLPPEPPY',
1203        'LWALDLPPEPPY','FDLPPPPYAES', 'EEELDPPADI','LDPPFEE','LDPPOAEE',
1204        'FDPPCFES','FDPPFES','LESFDPPAD','FDPPFED','FDPPOED','FDPPELD' )
1205   then
1206        open c_pay_id ;
1207        fetch c_pay_id into l_payroll_id ;
1208        if c_pay_id%notfound then
1209           l_person_id := p_parent_person_id ;
1210           if g_debug then
1211             hr_utility.set_location('for chold prill not found '        ,665);
1212           end if;
1213        end if ;
1214        close c_pay_id ;
1215   end if ;
1216 
1217   if g_debug then
1218     hr_utility.set_location('person   : '||l_person_id      ,665);
1219   end if;
1220 /*
1221 
1222   -- Check for cache mode. If so then get the per in ler details
1223   -- for the per in ler id
1224   --
1225   if p_cache_mode then
1226     --
1227     ben_pil_object.get_object
1228       (p_per_in_ler_id => l_per_in_ler_id
1229       ,p_rec           => l_pil_row
1230       );
1231     --
1232   end if;
1233   --
1234 */
1235 
1236   if g_debug then
1237     hr_utility.set_location('l_lf_evt_ocrd_dt :'||l_lf_evt_ocrd_dt,19);
1238   end if;
1239   if g_debug then
1240     hr_utility.set_location('p_date_cd :'||p_date_cd,19);
1241   end if;
1242 
1243   -- AED - Event Date
1244 
1245   if p_date_cd in  ( 'AED' , 'NUMDOE')  then
1246   --
1247   if g_debug then
1248       hr_utility.set_location('Entering AED',10);
1249   end if;
1250     --
1251     if l_lf_evt_ocrd_dt is null then
1252     --
1253        p_returned_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
1254     --
1255     else
1256     --
1257        p_returned_date:= l_lf_evt_ocrd_dt;
1258     --
1259     end if;
1260   --
1261   -- WAED 1 prior or Event
1262   --
1263   elsif p_date_cd = 'WAED' then
1264   --
1265   if g_debug then
1266       hr_utility.set_location('Entering AED',10);
1267   end if;
1268       --
1269       if l_lf_evt_ocrd_dt is null then
1270         --
1271         l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
1272         --
1273       else
1274         --
1275         l_event_date:= l_lf_evt_ocrd_dt;
1276         --
1277       end if;
1278         --
1279         p_returned_date := l_event_date - 1;
1280         --
1281   -----------------------------------------------------------
1282   --
1283   -- OFDEP    - On Enrollment Period Start.
1284   -- 5DAFDEP  -  5 Days After Enrollment Period Start.
1285   -- 10DAFDEP - 10 Days After Enrollment Period Start.
1286   -- 10DBSEPD - 10 Days before Enrollment Period Start.
1287   -- 15DAFDEP - 15 Days After Enrollment Period Start.
1288   -- 20DAFDEP - 20 Days After Enrollment Period Start.
1289   -- 25DAFDEP - 25 Days After Enrollment Period Start.
1290   -- 30DAFDEP - 20 Days After Enrollment Period Start.
1291   -- TODFEPS  - 31 Days After Enrollment Period Start.
1292   -- FFDFEPS  - 45 Days After Enrollment Period Start.
1293   -- SDFEPSD  - 60 Days After Enrollment Period Start.
1294   -- NDFEPS   - 90 Days After Enrollment Period Start.
1295   --
1296   -----------------------------------------------------------
1297   elsif p_date_cd = 'FFDFEPS'  or p_date_cd = 'NDFEPS'   or
1298         p_date_cd = 'TODFEPS'  or p_date_cd = 'SDFEPSD'  or
1299         p_date_cd = 'OFDEP'    or p_date_cd = '5DAFDEP'  or
1300         p_date_cd = '10DAFDEP' or p_date_cd = '10DBSEPD' or
1301         p_date_cd = '15DAFDEP' or p_date_cd = '20DAFDEP' or
1302         p_date_cd = '25DAFDEP' or p_date_cd = '30DAFDEP' then
1303     --
1304   if g_debug then
1305       hr_utility.set_location('Entering '||p_date_cd,10);
1306   end if;
1307     --
1308     open c_cm_enrt_perd_strt_dt;
1309       fetch c_cm_enrt_perd_strt_dt into l_cm_date;
1310     --
1311     if c_cm_enrt_perd_strt_dt%notfound then
1312     --
1313       if p_start_date is not null then
1314       --
1315          l_cm_date := p_start_date;
1316       --
1317       else
1318       --
1319         close c_cm_enrt_perd_strt_dt;
1320       if g_debug then
1321          hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',40);
1322       end if;
1323         fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
1324         fnd_message.set_token('DATE_CODE',p_date_cd);
1325         fnd_message.set_token('L_PROC',l_proc);
1326         fnd_message.raise_error;
1327       --
1328       end if;
1329     --
1330     end if;
1331     --
1332     close c_cm_enrt_perd_strt_dt;
1333     --
1334     if p_date_cd  = 'OFDEP' then
1335     --
1336        p_returned_date := l_cm_date;
1337     --
1338     elsif p_date_cd  = '5DAFDEP' then
1339     --
1340       p_returned_date := l_cm_date + 5;
1341     --
1342     elsif p_date_cd  = '10DAFDEP' then
1343     --
1344       p_returned_date := l_cm_date + 10;
1345     --
1346     elsif p_date_cd  = '10DBSEPD' then
1347     --
1348       p_returned_date := l_cm_date - 10;
1349     --
1350     elsif p_date_cd  = '15DAFDEP' then
1351     --
1352       p_returned_date := l_cm_date + 15;
1353     --
1354     elsif p_date_cd  = '20DAFDEP' then
1355     --
1356       p_returned_date := l_cm_date + 20;
1357     --
1358     elsif p_date_cd  = '25DAFDEP' then
1359     --
1360       p_returned_date := l_cm_date + 25;
1361     --
1362     elsif p_date_cd  = '30DAFDEP' then
1363     --
1364       p_returned_date := l_cm_date + 30;
1365     --
1366     elsif p_date_cd  = 'TODFEPS' then
1367     --
1368        p_returned_date := l_cm_date + 31;
1369     --
1370     elsif p_date_cd  = 'FFDFEPS' then
1371     --
1372        p_returned_date := l_cm_date + 45;
1373     --
1374     elsif p_date_cd  = 'SDFEPSD' then
1375     --
1376       p_returned_date := l_cm_date + 60;
1377     --
1378     elsif p_date_cd  = 'NDFEPS' then
1379     --
1380        p_returned_date := l_cm_date + 90;
1381     --
1382     end if;
1383   --
1384   -- ------------------------------------------------------------------------
1385   -- Enrollment End
1386   -- ------------------------------------------------------------------------
1387   -- LDPPFEE - End of Pay Period After Enrollment End
1388   -- LDPPOAEE - End of Pay Period On or After Enrollment End
1389   -- FDLMPPYAES - First of Last Month in Year After enrollment
1390   -- FDLPPPPYAES - First of Last Pay Period in Event Year After Enrollment Start
1391   -- LDMFEE - Last day of Month after Enrollment End
1392   -- LDMOAEE - Last day of Month on or after Enrollment End
1393   -- EEELDPPADI - Earlier of Enrollment End or Last Day of Pay Period after
1394   --              Dedesignated or Ineligible
1395   -- EEELDMADI - Earlier of Enrollment End or Last Day of Month after
1396   --             Dedesignated or Ineligible
1397   -- EEDI - Earliest of Enrollment End, Dedesignated or Ineligible
1398   -- OCED - On the Coverage End Date.
1399   -- PECED - Participant's Enrollment Coverage End Date.
1400 
1401   elsif p_date_cd  = 'LDPPFEE' or p_date_cd = 'LDPPOAEE' or p_date_cd = 'FDLMPPYAES'
1402     or p_date_cd = 'FDLPPPPYAES' or p_date_cd = 'LDMFEE' or p_date_cd = 'LDMOAEE'
1403     or p_date_cd = 'EEELDPPADI' or p_date_cd = 'EEELDMADI' or p_date_cd = 'EEDI'
1404     or p_date_cd = 'OCED' or p_date_cd = 'PECED'  then
1405     --
1406     if g_debug then
1407       hr_utility.set_location('Entering '||p_date_cd,23);
1408     end if;
1409     --
1410     if l_lf_evt_ocrd_dt is null then
1411        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
1412     end if;
1413 
1414     if p_start_date is null then
1415        -- p_start_date is the enrollment coverage end date in this case.
1416        open c_prtt_enrt_rslt_dts;
1417        fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
1418                                     l_enrt_cvg_strt_dt,
1419                                     l_enrt_eff_strt_date;
1420        if g_debug then
1421          hr_utility.set_location('l_enrt_cvg_end_dt cursor returns ', 19);
1422        end if;
1423        close c_prtt_enrt_rslt_dts;
1424     else
1425        l_enrt_cvg_end_dt := p_start_date;
1426     end if;
1427 
1428 
1429     if p_date_cd = 'FDLPPPPYAES' or p_date_cd = 'EEELDPPADI' then
1430        if p_date_cd = 'FDLPPPPYAES' then
1431           -- First of Last Pay Period in Event Year
1432           get_plan_year
1433            (p_effective_date => p_effective_date
1434            ,p_lf_evt_ocrd_dt => l_enrt_cvg_strt_dt -- l_enrt_cvg_end_dt 5303167
1435            ,p_pl_id          => l_pl_id
1436            ,p_pgm_id         => l_pgm_id
1437            ,p_oipl_id        => l_oipl_id
1438            ,p_date_cd        => p_date_cd
1439            ,p_comp_obj_mode  => p_comp_obj_mode
1440            ,p_start_date     => l_start_date
1441            ,p_end_date       => l_end_date) ;
1442        elsif p_date_cd = 'EEELDPPADI' then
1443           -- Earlier of Enrollment End or Last Day of Pay Period after
1444           --              Dedesignated or Ineligible
1445           l_end_date := p_effective_date+1;
1446        end if;
1447 
1448        open c_pay_period_for_date(l_end_date);  -- l_end_date is just a parm
1449        fetch c_pay_period_for_date into
1450           l_start_date,
1451           l_end_date;  -- l_end_date is now the payroll end date.
1452 
1453        if c_pay_period_for_date%notfound and l_enrt_eff_strt_date is not null then
1454          close c_pay_period_for_date;
1455        if g_debug then
1456            hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',15);
1457        end if;
1458          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1459          fnd_message.set_token('DATE_CODE',p_date_cd);
1460          fnd_message.set_token('L_PROC',l_proc);
1461          fnd_message.set_token('PERSON_ID',l_person_id);
1462          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1463          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1464          fnd_message.raise_error;
1465        end if;
1466        close c_pay_period_for_date;
1467 
1468        if p_date_cd = 'FDLPPPPYAES' then
1469           p_returned_date := l_start_date;
1470        elsif p_date_cd = 'EEELDPPADI' then
1471           if l_enrt_cvg_end_dt < l_end_date then
1472              p_returned_date := l_enrt_cvg_end_dt;
1473           else
1474              p_returned_date := l_end_date;
1475           end if;
1476        end if;
1477 
1478     elsif p_date_cd  = 'LDPPFEE' or p_date_cd = 'LDPPOAEE'
1479        then
1480        -- End of Pay Period (On or) After
1481        if l_enrt_cvg_end_dt <>  hr_api.g_eot then
1482        --
1483        if g_debug then
1484          hr_utility.set_location(' Step 2 ' ,19);
1485        end if;
1486        open c_pay_period_for_date(l_enrt_cvg_end_dt);
1487        fetch c_pay_period_for_date into
1488           l_start_date,
1489           l_end_date;
1490 
1491        if c_pay_period_for_date%notfound and l_enrt_eff_strt_date is not null then
1492          close c_pay_period_for_date;
1493          if g_debug then
1494            hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',15);
1495          end if;
1496          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1497          fnd_message.set_token('DATE_CODE',p_date_cd);
1498          fnd_message.set_token('L_PROC',l_proc);
1499          fnd_message.set_token('PERSON_ID',l_person_id);
1500          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1501          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1502          fnd_message.raise_error;
1503        end if;
1504        close c_pay_period_for_date;
1505 
1506        else
1507           if g_debug then
1508             hr_utility.set_location('End of Time set 1 ' , 19 );
1509           end if;
1510           l_end_date := l_enrt_cvg_end_dt ;
1511        end if ;
1512 
1513        --
1514        if l_enrt_cvg_end_dt <>  hr_api.g_eot then
1515        --
1516        if l_end_date = l_enrt_cvg_end_dt then
1517           if p_date_cd = 'LDPPFEE' then
1518              -- End of Pay Period After
1519              -- retrieve last day of next pay period
1520              if g_debug then
1521                hr_utility.set_location(' Step 3 ' ,19);
1522              end if;
1523              open c_pay_period_for_date(l_end_date+1);
1524              fetch c_pay_period_for_date into
1525                 l_start_date,
1526                 l_end_date;
1527 
1528              if c_pay_period_for_date%notfound and
1529                l_enrt_eff_strt_date is not null then
1530                close c_pay_period_for_date;
1531                if g_debug then
1532                  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',25);
1533                end if;
1534                fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1535                fnd_message.set_token('DATE_CODE',p_date_cd);
1536                fnd_message.set_token('L_PROC',l_proc);
1537                fnd_message.set_token('PERSON_ID',l_person_id);
1538                fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1539                fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1540                fnd_message.raise_error;
1541              end if;
1542              close c_pay_period_for_date;
1543           end if;
1544        end if ;
1545        --
1546        else
1547           if g_debug then
1548             hr_utility.set_location('End of Time set 2' , 19 );
1549           end if;
1550           l_end_date := l_enrt_cvg_end_dt ;
1551        end if;
1552 
1553        p_returned_date := l_end_date;
1554 
1555     elsif p_date_cd = 'FDLMPPYAES' then
1556        -- First of Last Month in Year
1557        get_plan_year
1558            (p_effective_date =>  p_effective_date
1559            ,p_lf_evt_ocrd_dt => l_enrt_cvg_strt_dt -- 5303167 l_enrt_cvg_end_dt
1560            ,p_pl_id          => l_pl_id
1561            ,p_pgm_id         => l_pgm_id
1562            ,p_oipl_id        => l_oipl_id
1563            ,p_date_cd        => p_date_cd
1564            ,p_comp_obj_mode  => p_comp_obj_mode
1565            ,p_start_date     => l_start_date
1566            ,p_end_date       => l_end_date) ;
1567 
1568        p_returned_date := last_day(add_months(l_end_date,-1))+1;
1569        -- ? What if enrollment date is during last month of year
1570     elsif p_date_cd = 'LDMFEE'  then
1571        -- Last day of Month after
1572        p_returned_date := last_day(l_enrt_cvg_end_dt+1);
1573     elsif p_date_cd = 'LDMOAEE' then
1574        -- Last day of Month on or after
1575        p_returned_date := last_day(l_enrt_cvg_end_dt);
1576     elsif p_date_cd = 'EEELDMADI' then
1577        --  Earlier of Enrollment End or Last Day of Month after
1578        --  Dedesignated or Ineligible
1579        if l_enrt_cvg_end_dt < last_day(p_effective_date +1) then
1580           p_returned_date := l_enrt_cvg_end_dt;
1581        else
1582           p_returned_date := last_day(p_effective_date +1);
1583        end if;
1584     elsif p_date_cd = 'EEDI' then
1585        -- Earliest of Enrollment End, Dedesignated or Ineligible
1586        if l_enrt_cvg_end_dt < p_effective_date then
1587           p_returned_date := l_enrt_cvg_end_dt;
1588        else
1589           p_returned_date := p_effective_date ;
1590        end if;
1591     elsif p_date_cd = 'OCED' then
1592        -- Enrollment End
1593        p_returned_date := l_enrt_cvg_end_dt;
1594     elsif p_date_cd = 'PECED' then
1595        if p_enrt_cvg_end_dt is not null then
1596           p_returned_date := p_enrt_cvg_end_dt;
1597        else
1598           p_returned_date := l_enrt_cvg_end_dt;
1599        end if;
1600     end if;
1601 
1602   -- ------------------------------------------------------------------------
1603   -- Enrollment Start or Later
1604   -- ------------------------------------------------------------------------
1605   --
1606   -- LEMES - Later of Elections Made or Enrollment Start
1607   -- LFYEMES - First of Year After Later Elections or Enrollment Start
1608   -- LFMEMES - First of Month After Later Elections or Enrollment Start
1609   -- FDMCFES - First of Month on or After Enrollment Start
1610   -- FDMFES - First of Month After Enrollment Start
1611   -- LFPPEMES - First of Pay Period After Later Elections or Enrollment Start
1612   -- FDPPCFES - First of Pay Period On or After Enrollment
1613   -- FDPPFES - First of Pay Period after Enrollment Start
1614   -- LESWD - Later of Enrollment Start or When Dedesignated
1615   -- LESFDPPAD - Later of Enrt Strt or First Day of Pay Period after Dedesignated
1616   -- LESFDMAD - Later of Enrt Start or First Day of Month after Dedesignated
1617   -- FDSMFES - First of Semi month after enrollment start
1618   -- FDSMCFES - First of Semi Month on or After Enrollment
1619   -- LESFDSMAD - Later of Enrollment Start or First of Semi Month After
1620   --             Designated
1621   elsif p_date_cd  = 'LEMES' or p_date_cd = 'LFPPEMES' or p_date_cd = 'LFMEMES'
1622     or  p_date_cd  = 'LFYEMES'  or p_date_cd = 'FDMCFES'  or p_date_cd = 'FDMFES'
1623     or p_date_cd = 'FDPPCFES' or p_date_cd = 'FDPPFES'  or p_date_cd = 'LESWD'
1624     or p_date_cd = 'LESFDPPAD' or p_date_cd = 'LESFDMAD' or p_date_cd = 'FDPPELD'
1625     or p_date_cd = 'FDSMFES' or p_date_cd = 'FDSMCFES' or p_date_cd = 'LESFDSMAD'
1626     then
1627    if g_debug then
1628      hr_utility.set_location('Entering '||p_date_cd,17);
1629    end if;
1630    if g_debug then
1631      hr_utility.set_location('l_lf_evt_ocrd_dt :'||l_lf_evt_ocrd_dt , 17);
1632    end if;
1633 
1634     if l_lf_evt_ocrd_dt is null then
1635        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
1636        if g_debug then
1637          hr_utility.set_location('In the get_event_date '||l_lf_evt_ocrd_dt,17);
1638        end if ;
1639     end if;
1640 
1641    if g_debug then
1642      hr_utility.set_location('before cursor l_enrt_cvg_strt_dt:'||l_enrt_cvg_strt_dt,18);
1643    end if;
1644 
1645     open c_prtt_enrt_rslt_dts;
1646     fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
1647                                     l_enrt_cvg_strt_dt,
1648                                     l_enrt_eff_strt_date;
1649     -- bug fix 1633284  if the cursor fails, we are using the p_start_date and
1650     -- p_effective_date parameters for further processing.
1651 
1652         --Bug 5225815 commented the p_start_date not null check.
1653        --assign the default when no prior elections exists
1654 
1655        --Bug 5394353 Undid Modification done for Bug 5225815
1656        --Uncommented the p_start_date not null check.
1657        --p_start_date would be null in cases where rt_strt_dt is to be calculated at enrollment
1658 
1659     if c_prtt_enrt_rslt_dts%notfound  and   p_start_date is not null then
1660       l_enrt_cvg_strt_dt   := p_start_date ;
1661       l_enrt_eff_strt_date := p_effective_date ;
1662     end if;
1663     --
1664     close c_prtt_enrt_rslt_dts;
1665      if g_debug then
1666        hr_utility.set_location('l_enrt_cvg_end_dt :'||l_enrt_cvg_end_dt , 18);
1667      end if;
1668      if g_debug then
1669        hr_utility.set_location('l_enrt_cvg_strt_dt:'||l_enrt_cvg_strt_dt,18);
1670      end if;
1671      if g_debug then
1672        hr_utility.set_location('l_enrt_eff_strt_date:'||l_enrt_eff_strt_date,18);
1673      end if;
1674     --
1675     --
1676     if p_date_cd  = 'LEMES' or p_date_cd = 'LFPPEMES' or p_date_cd = 'LFMEMES'
1677        or p_date_cd  = 'LFYEMES'  then
1678        -- Later of Elections Made or Enrollment Start
1679        -- For all but LEMES, this is just a starting date. More code below.
1680        if l_enrt_cvg_strt_dt > l_enrt_eff_strt_date then
1681           p_returned_date := l_enrt_cvg_strt_dt;
1682        else
1683           p_returned_date := l_enrt_eff_strt_date;
1684        end if;
1685     else  -- start with enrollment start date.
1686           p_returned_date := l_enrt_cvg_strt_dt;
1687     end if;
1688       --
1689 
1690     -- LESFDSMAD - Later of Enrollment Start or First of Semi Month After
1691     --
1692     if p_date_cd = 'LESFDSMAD' then
1693       --
1694       if to_char(p_effective_date, 'DD') > 15 then
1695         --
1696         l_event_date := round(p_effective_date,'Month') ;
1697         --
1698       else
1699         --
1700         l_event_date := round(p_effective_date,'Month')+ 15  ;
1701         --
1702       end if;
1703       --
1704       if l_enrt_cvg_strt_dt > l_event_date then
1705         --
1706         p_returned_date := l_enrt_cvg_strt_dt ;
1707         --
1708       else
1709         --
1710         p_returned_date := l_event_date ;
1711         --
1712       end if;
1713     --
1714     end if ;
1715     --
1716     -- FDSMFES - First of Semi month after enrollment start
1717     -- FDSMCFES - First of Semi Month on or After Enrollment
1718     if p_date_cd = 'FDSMFES' or p_date_cd = 'FDSMCFES' then
1719       --
1720       if p_date_cd = 'FDSMCFES' and to_number(to_char(l_enrt_cvg_strt_dt, 'DD')) in ( 1, 16 )
1721       then
1722         --
1723         p_returned_date := l_enrt_cvg_strt_dt ;
1724         if g_debug then
1725           hr_utility.set_location('Case 1',15);
1726         end if;
1727         --
1728       elsif to_char(l_enrt_cvg_strt_dt, 'DD') > 15 then
1729         --
1730         p_returned_date := round(l_enrt_cvg_strt_dt,'Month')  ;
1731         if g_debug then
1732           hr_utility.set_location('Case 2',15);
1733         end if;
1734         --
1735       else
1736         --
1737         p_returned_date := round(l_enrt_cvg_strt_dt,'Month') + 15 ;
1738         if g_debug then
1739           hr_utility.set_location('Case 3'||l_enrt_cvg_strt_dt,15);
1740         end if;
1741         if g_debug then
1742           hr_utility.set_location('Case 3'||p_returned_date,15);
1743         end if;
1744 
1745         --
1746       end if;
1747       --
1748     end if;
1749     --
1750     if p_date_cd = 'FDPPCFES' or p_date_cd ='FDPPELD'  then
1751        -- First of Pay Period On or After Enrollment
1752        --
1753        if  p_date_cd ='FDPPELD' then
1754           p_returned_date := p_effective_date;
1755        end if;
1756        --
1757        open c_pay_period_for_date(p_returned_date);
1758        fetch c_pay_period_for_date into
1759           l_start_date,
1760           l_end_date;
1761 
1762        if c_pay_period_for_date%notfound and
1763          l_enrt_cvg_strt_dt is not null then
1764          if g_debug then
1765            hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',80);
1766          end if;
1767          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1768          fnd_message.set_token('DATE_CODE',p_date_cd);
1769          fnd_message.set_token('L_PROC',l_proc);
1770          fnd_message.set_token('PERSON_ID',l_person_id);
1771          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1772          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1773          fnd_message.raise_error;
1774        end if;
1775        close c_pay_period_for_date;
1776     end if;
1777 
1778     if (p_date_cd = 'FDPPCFES' or p_date_cd ='FDPPELD' ) and p_returned_date = l_start_date
1779        then
1780           null; -- use p_returned_date (enrt strt)
1781     elsif ((p_date_cd = 'FDPPCFES' or p_date_cd ='FDPPELD')
1782             and p_returned_date <> l_start_date)
1783           or p_date_cd = 'LFPPEMES' or p_date_cd = 'FDPPFES'
1784           or p_date_cd = 'LESFDPPAD' then
1785            if g_debug then
1786              hr_utility.set_location('LFPPEMES  First of Pay Period After',20);
1787            end if;
1788           -- First of Pay Period After
1789           if p_date_cd = 'LESFDPPAD' then
1790              -- need to get pay period after designation, not enrt strt.
1791              p_returned_date := nvl(l_fonm_rt_cvg_strt_dt, p_effective_date );
1792           end if;
1793           open c_next_pay_period(p_returned_date);
1794           fetch c_next_pay_period into l_next_pay_period;
1795 
1796           if g_debug then
1797             hr_utility.set_location('l_next_pay_period.start_date'||l_next_pay_period.start_date,20);
1798           end if;
1799           if c_next_pay_period%notfound and
1800              l_enrt_cvg_strt_dt is not null then
1801             close c_next_pay_period;
1802             if g_debug then
1803               hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',40);
1804             end if;
1805             fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1806             fnd_message.set_token('DATE_CODE',p_date_cd);
1807             fnd_message.set_token('L_PROC',l_proc);
1808             fnd_message.set_token('PERSON_ID',l_person_id);
1809             fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1810             fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1811             fnd_message.raise_error;
1812           end if;
1813           close c_next_pay_period;
1814 
1815           if p_date_cd = 'LESFDPPAD' and
1816              l_enrt_eff_strt_date > l_next_pay_period.start_date then
1817              -- Later of Enrt Strt or First Day of Pay Period after Dedesignated
1818              p_returned_date := l_enrt_eff_strt_date;
1819           else
1820              p_returned_date := l_next_pay_period.start_date;
1821              if g_debug then
1822                hr_utility.set_location('Else case :'||l_next_pay_period.start_date,20);
1823              end if;
1824           end if;
1825 
1826     elsif p_date_cd = 'LFMEMES' or p_date_cd = 'FDMCFES' or p_date_cd = 'FDMFES'
1827           then
1828           -- First of Month
1829           if p_date_cd = 'FDMCFES' and to_char(p_returned_date, 'dd') = '01' then
1830              null;  -- use p_returned_date (enrt strt)
1831           else
1832              p_returned_date := last_day(p_returned_date)+1;
1833           end if;
1834     elsif p_date_cd = 'LESFDMAD' then
1835           -- Later of Enrt Start or First Day of Month after Dedesignated
1836           if last_day(p_effective_date)+1 > p_returned_date then
1837              p_returned_date := last_day(p_effective_date)+1;
1838           end if;  -- else use p_returned date (enrt strt)
1839     elsif p_date_cd  = 'LFYEMES' then
1840           -- First of Year
1841           get_next_plan_year
1842                  (p_effective_date => p_effective_date
1843                  ,p_lf_evt_ocrd_dt => p_returned_date
1844                  ,p_pl_id          => l_pl_id
1845                  ,p_pgm_id         => l_pgm_id
1846                  ,p_oipl_id        => l_oipl_id
1847                  ,p_date_cd        => p_date_cd
1848                  ,p_comp_obj_mode  => p_comp_obj_mode
1849                  ,p_start_date     => l_next_popl_yr_strt
1850                  ,p_end_date       => l_next_popl_yr_end) ;
1851 
1852           p_returned_date :=l_next_popl_yr_strt;
1853     elsif  p_date_cd = 'LESWD' then
1854           -- Later of Enrollment Start or When Dedesignated
1855           if l_enrt_cvg_strt_dt > p_effective_date then
1856              p_returned_date := l_enrt_cvg_strt_dt;
1857           else
1858              p_returned_date := p_effective_date;
1859           end if;
1860     end if;
1861   --
1862   elsif p_date_cd = 'FDPPFED' or p_date_cd = 'LDPPFEFD' then
1863    --
1864    open c_next_pay_period(p_effective_date);
1865    fetch c_next_pay_period into l_next_pay_period;
1866    if g_debug then
1867      hr_utility.set_location('l_next_pay_period.start_date'||l_next_pay_period.start_date,20);
1868    end if;
1869    if c_next_pay_period%notfound then
1870       close c_next_pay_period;
1871       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1872       fnd_message.set_token('DATE_CODE',p_date_cd);
1873       fnd_message.set_token('L_PROC',l_proc);
1874       fnd_message.set_token('PERSON_ID',l_person_id);
1875       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1876       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1877       fnd_message.raise_error;
1878    end if;
1879    close c_next_pay_period;
1880    if p_date_cd = 'FDPPFED' then
1881      p_returned_date := l_next_pay_period.start_date;
1882    else
1883      p_returned_date := l_next_pay_period.end_date;
1884    end if;
1885  --
1886  -- ** FDPPOED - First of Pay Period On or After Effective Date
1887  elsif p_date_cd = 'FDPPOED' then
1888     -- Bug:4268494: Changed the logic. Get the current pay-period start/end dates
1889     -- If start_date <> effective_date, fetch next pay period start/end dates. Return start_date.
1890     hr_utility.set_location('Evaluate FDPPOED',10);
1891     l_event_date := NVL(l_fonm_rt_cvg_strt_dt,p_effective_date);
1892     --
1893     open c_pay_period;
1894     fetch c_pay_period into l_pay_period;
1895     if c_pay_period%notfound then
1896       close c_pay_period;
1897       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1898       fnd_message.set_token('DATE_CODE',p_date_cd);
1899       fnd_message.set_token('PERSON_ID',l_person_id);
1900       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1901       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1902       fnd_message.raise_error;
1903     end if;
1904     close c_pay_period ;
1905     --
1906     if l_event_date =  l_pay_period.start_date then
1907        p_returned_date := l_pay_period.start_date;
1908        hr_utility.set_location('l_pay_period.start_date '||l_pay_period.start_date,10);
1909     else
1910        --
1911        open c_next_pay_period(NVL(l_event_date,p_effective_date));
1912        fetch c_next_pay_period into l_next_pay_period;
1913        --
1914        if c_next_pay_period%notfound then
1915          close c_next_pay_period;
1916          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1917          fnd_message.set_token('DATE_CODE',p_date_cd);
1918          fnd_message.set_token('PERSON_ID',l_person_id);
1919          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1920          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1921          fnd_message.raise_error;
1922        end if;
1923        close c_next_pay_period;
1924        p_returned_date := l_next_pay_period.start_date;
1925        hr_utility.set_location('l_pay_period.start_date '|| l_next_pay_period.start_date,20);
1926        --
1927     end if;
1928     --
1929  --
1930  -- ** LDPPOEFD - 1 Prior or End of Pay Period On or After Effective Date
1931  elsif p_date_cd = 'LDPPOEFD' THEN
1932     -- Bug:4268494: Changed the logic. Get the current pay-period start/end dates
1933     -- If end_date <> effective_date, fetch next pay period start/end dates. Return end_date.
1934     hr_utility.set_location('Evaluate LDPPOEFD',10);
1935     l_event_date := NVL(l_fonm_rt_cvg_strt_dt,p_effective_date);
1936     --
1937     open c_pay_period;
1938     fetch c_pay_period into l_pay_period;
1939     if c_pay_period%notfound then
1940       close c_pay_period;
1941       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1942       fnd_message.set_token('DATE_CODE',p_date_cd);
1943       fnd_message.set_token('PERSON_ID',l_person_id);
1944       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1945       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1946       fnd_message.raise_error;
1947     end if;
1948     close c_pay_period ;
1949     --
1950     if l_event_date =  l_pay_period.end_date then
1951        p_returned_date := l_pay_period.end_date;
1952        hr_utility.set_location('l_pay_period.end_date '|| l_pay_period.end_date,20);
1953     else
1954        --
1955        open c_next_pay_period(NVL(l_event_date,p_effective_date));
1956        fetch c_next_pay_period into l_next_pay_period;
1957        --
1958        if c_next_pay_period%notfound then
1959          close c_next_pay_period;
1960          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
1961          fnd_message.set_token('DATE_CODE',p_date_cd);
1962          fnd_message.set_token('PERSON_ID',l_person_id);
1963          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
1964          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
1965          fnd_message.raise_error;
1966        end if;
1967        close c_next_pay_period;
1968        p_returned_date := l_next_pay_period.end_date;
1969        hr_utility.set_location('l_pay_period.end_date '|| l_next_pay_period.end_date,20);
1970        --
1971     end if;
1972   --
1973   -- AFDCSMFDFFNSM First of next Semi month,if from day is first,
1974   --               else first of next semi month
1975   elsif p_date_cd = 'AFDCSMFDFFNSM' then
1976 
1977     l_lf_evt_ocrd_dt := nvl(l_fonm_rt_cvg_strt_dt,p_effective_date ) ;   -- Age Determination
1978     --
1979     if to_number(to_char(l_lf_evt_ocrd_dt, 'DD')) in (1,16) then
1980       --
1981       p_returned_date := l_lf_evt_ocrd_dt ;
1982       --
1983     elsif to_char(l_lf_evt_ocrd_dt, 'DD') > 15 then
1984       --
1985       p_returned_date := round(l_lf_evt_ocrd_dt,'Month')  ;
1986       --
1987     else
1988       --
1989       p_returned_date := round(l_lf_evt_ocrd_dt,'Month') + 15 ;
1990       --
1991     end if;
1992 
1993     -- FDSMCF - First of Semi Month on or After Event
1994     --
1995     elsif p_date_cd = 'FDSMCF' then
1996       --
1997       if l_lf_evt_ocrd_dt is null then
1998       --
1999         l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2000       --
2001       else
2002         l_event_date:=  l_lf_evt_ocrd_dt ;
2003       --
2004       end if;
2005         --
2006         if to_number(to_char(l_event_date, 'DD')) in ( 1, 16 ) then
2007           --
2008           p_returned_date := l_event_date ;
2009           --
2010         elsif to_char(l_event_date, 'DD') > 15 then
2011           --
2012           p_returned_date := last_day(l_event_date)+1  ;
2013           --
2014         else
2015           --
2016           p_returned_date := trunc(l_event_date,'Month') + 15 ;
2017           --
2018         end if;
2019         --
2020     -- LAFDFSM - First of Semi Month After Later Event or Notified
2021     --
2022     elsif p_date_cd = 'LAFDFSM' then
2023       --
2024       if l_lf_evt_ocrd_dt is null then
2025       --
2026         l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2027       --
2028       else
2029       --
2030         l_event_date:= l_lf_evt_ocrd_dt;
2031       --
2032       end if;
2033 
2034       --
2035       l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2036       --
2037       if l_event_date > l_recorded_date then
2038         --
2039         if to_char(l_event_date, 'DD') > 15 then
2040           --
2041           p_returned_date := last_day(l_event_date)+1  ;
2042           --
2043         else
2044           --
2045           p_returned_date := trunc(l_event_date,'Month') + 15 ;
2046           --
2047         end if;
2048       else
2049         --
2050         if to_char(l_recorded_date, 'DD') > 15 then
2051           --
2052           p_returned_date := last_day(l_recorded_date)+1  ;
2053           --
2054         else
2055           --
2056           p_returned_date := trunc(l_recorded_date,'Month') + 15 ;
2057           --
2058         end if;
2059         --
2060       end if;
2061       --
2062     -- LFDSMCF - First of Semi Month on or After Later Event or Notified
2063     --
2064     elsif p_date_cd = 'LFDSMCF' then
2065       --
2066       if l_lf_evt_ocrd_dt is null then
2067       --
2068         l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2069       --
2070       else
2071       --
2072         l_event_date:= l_lf_evt_ocrd_dt;
2073       --
2074       end if;
2075       --
2076       l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2077       --
2078       if l_event_date > l_recorded_date then
2079         --
2080         if  to_number(to_char(l_event_date, 'DD')) in ( 1, 16 ) then
2081           --
2082           p_returned_date := l_event_date ;
2083           --
2084         elsif to_char(l_event_date, 'DD') > 15 then
2085           --
2086           p_returned_date := last_day(l_event_date)+1  ;
2087           --
2088         else
2089           --
2090           p_returned_date := trunc(l_event_date,'Month') + 15 ;
2091           --
2092         end if;
2093         --
2094       else
2095         --
2096         if to_number(to_char(l_recorded_date, 'DD')) in ( 1, 16 ) then
2097           --
2098           p_returned_date := l_recorded_date ;
2099           --
2100         elsif to_char(l_recorded_date, 'DD') > 15 then
2101           --
2102           p_returned_date := last_day(l_recorded_date)+1  ;
2103           --
2104         else
2105           --
2106           p_returned_date := trunc(l_recorded_date,'Month') + 15 ;
2107           --
2108         end if;
2109         --
2110       end if;
2111 
2112   -- 1 Prior or Later Event or Notified Semi Month End
2113   --
2114   elsif p_date_cd = 'LWALDCSM' then
2115   --    --
2116     if g_debug then
2117       hr_utility.set_location('Entering LWALDCSM',10);
2118     end if;
2119     --
2120        if l_lf_evt_ocrd_dt is null then
2121        --
2122           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2123        --
2124        else
2125        --
2126           l_event_date:= l_lf_evt_ocrd_dt;
2127        --
2128        end if;
2129        --
2130        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2131        --
2132        If l_recorded_date > l_event_date then
2133        --
2134           if to_char(l_recorded_date, 'DD') > 15 then
2135             p_returned_date := round(l_recorded_date,'Month')  ;
2136           else
2137             p_returned_date := round(l_recorded_date,'Month') + 15 ;
2138           end if;
2139        --
2140        else
2141        --
2142           --
2143           if to_char(l_event_date, 'DD') > 15 then
2144              p_returned_date := round(l_event_date,'Month')  ;
2145           else
2146              p_returned_date := round(l_event_date,'Month') + 15 ;
2147           end if;
2148           --
2149        End If;
2150     --
2151   -- 1 Prior or Semi Month End
2152   -- Semi Month end is if le is between 1 and 15 then take 15 else take end of month
2153   --
2154 
2155   elsif p_date_cd = 'WALDCSM' then
2156   --    --
2157     if g_debug then
2158       hr_utility.set_location('Entering WALDCSM',10);
2159     end if;
2160     --
2161        if l_lf_evt_ocrd_dt is null then
2162        --
2163           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2164        --
2165        else
2166        --
2167           l_event_date:= l_lf_evt_ocrd_dt;
2168        --
2169        end if;
2170        --
2171        if to_char(l_event_date, 'DD') > 15 then
2172           p_returned_date := last_day(l_event_date)  ;
2173        else
2174           p_returned_date := trunc(l_event_date,'Month') + 14 ;
2175        end if;
2176        --
2177        if g_debug then
2178          hr_utility.set_location('p_returned_date  '||p_returned_date , 19 );
2179        end if;
2180     --
2181   --
2182   -- EEELDNSMADI --  Earlier Participant Enrollment End or Next Semi Month End
2183   -- for dependent coverage end date
2184   elsif p_date_cd = 'EEELDNSMADI' then
2185   --
2186     if g_debug then
2187       hr_utility.set_location('Entering EEELDNSMADI',10);
2188     end if;
2189     --
2190       --
2191       --  p_effective_date   -- Designated Date
2192       --
2193       if to_char(p_effective_date, 'DD') > 15 then
2194         --
2195         l_event_date := round( nvl(l_fonm_rt_cvg_strt_dt,p_effective_date))+ 14 ;
2196         --
2197       else
2198         --
2199         l_event_date := last_day(nvl(l_fonm_rt_cvg_strt_dt,p_effective_date)) ;
2200         --
2201       end if;
2202       --
2203       open c_prtt_enrt_rslt_dts;
2204       --
2205       fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
2206                                       l_enrt_cvg_strt_dt,
2207                                       l_enrt_eff_strt_date;
2208       --
2209       close c_prtt_enrt_rslt_dts;
2210       --
2211       if l_enrt_cvg_end_dt < l_event_date then
2212         --
2213 	p_returned_date := l_enrt_cvg_end_dt ;
2214         --
2215       else
2216         --
2217         p_returned_date := l_event_date ;
2218         --
2219       end if;
2220       --
2221   -- ALDPSM End of Previuos Semi Month  ( if day between 1 and 15 then last day of prev month
2222   --                                      else 15th of the same month )
2223   --
2224   elsif p_date_cd = 'ALDPSM' then
2225   --
2226        --
2227        if g_debug then
2228          hr_utility.set_location('Entering ALDPSM',10);
2229        end if;
2230        --
2231        if l_lf_evt_ocrd_dt is null then
2232        --
2233           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2234        --
2235        else
2236        --
2237           l_event_date:= l_lf_evt_ocrd_dt;
2238        --
2239        end if;
2240        --
2241        if to_char(l_event_date, 'DD') > 15 then
2242           p_returned_date := trunc(l_event_date,'Month')+ 14 ;
2243        else
2244           p_returned_date := last_day(add_months(l_event_date,-1)) ;
2245        end if;
2246   --
2247   -- LALDPSM End of Previuos Semi Month later event or Notified
2248   --            ( if later day between 1 and 15 then last day of prev month
2249   --                                      else 15th of the same month )
2250   --
2251   elsif p_date_cd = 'LALDPSM' then
2252   --
2253        --
2254        if g_debug then
2255          hr_utility.set_location('Entering ALDPSM',10);
2256        end if;
2257        --
2258        if l_lf_evt_ocrd_dt is null then
2259        --
2260           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2261        --
2262        else
2263        --
2264           l_event_date:= l_lf_evt_ocrd_dt;
2265        --
2266        end if;
2267        --
2268        l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2269        --
2270        if l_event_date > l_recorded_date then
2271          --
2272          if to_char(l_event_date, 'DD') > 15 then
2273            --
2274            p_returned_date := trunc(l_event_date,'Month')+ 14 ;
2275            --
2276          else
2277            --
2278            p_returned_date := last_day(add_months(l_event_date,-1)) ;
2279            --
2280          end if;
2281          --
2282        else
2283          --
2284          if to_char(l_recorded_date, 'DD') > 15 then
2285            --
2286            p_returned_date := trunc(l_recorded_date,'Month')+ 14 ;
2287            --
2288          else
2289            --
2290            p_returned_date := last_day(add_months(l_recorded_date,-1)) ;
2291            --
2292          end if;
2293          --
2294        end if;
2295        --
2296   --
2297   --  ALDCSM End of Semi-month.
2298   --
2299   elsif p_date_cd = 'ALDCSM' then
2300     --
2301     if g_debug then
2302       hr_utility.set_location('Entering ALDCSM',10);
2303     end if;
2304     --
2305     if l_lf_evt_ocrd_dt is null then
2306       --
2307       l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2308       --
2309     else
2310       --
2311       l_event_date:= l_lf_evt_ocrd_dt;
2312       --
2313     end if;
2314     --
2315     if to_char(l_event_date, 'DD') > 15 then
2316        --
2317        p_returned_date := last_day(l_event_date)  ;
2318        --
2319     else
2320        --
2321        p_returned_date := trunc(l_event_date,'Month') + 14 ;
2322        --
2323     end if;
2324     --
2325     if g_debug then
2326       hr_utility.set_location('p_returned_date '||p_returned_date , 199) ;
2327     end if;
2328   --
2329   --  LALDCSM End of Semi-month later event or Notified.
2330   --
2331   elsif p_date_cd = 'LALDCSM' then
2332     --
2333     if g_debug then
2334       hr_utility.set_location('Entering LALDCSM',10);
2335     end if;
2336     --
2337     if l_lf_evt_ocrd_dt is null then
2338       --
2339       l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2340       --
2341     else
2342       --
2343       l_event_date:= l_lf_evt_ocrd_dt;
2344       --
2345     end if;
2346     --
2347     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2348     --
2349     if l_event_date > l_recorded_date then
2350       --
2351       if to_char(l_event_date, 'DD') > 15 then
2352         --
2353         p_returned_date := last_day(l_event_date)  ;
2354         --
2355       else
2356         --
2357         p_returned_date := trunc(l_event_date,'Month') + 14 ;
2358         --
2359       end if;
2360     else
2361       --
2362       if to_char(l_recorded_date, 'DD') > 15 then
2363         --
2364         p_returned_date := last_day(l_recorded_date)  ;
2365         --
2366       else
2367         --
2368         p_returned_date := trunc(l_recorded_date,'Month') + 14 ;
2369         --
2370       end if;
2371       --
2372     end if;
2373   --
2374   --  AFDFSM First of next Semi-month.
2375   --
2376   elsif p_date_cd = 'AFDFSM' then
2377     --
2378     if g_debug then
2379       hr_utility.set_location('Entering AFDFSM',10);
2380     end if;
2381     --
2382     if l_lf_evt_ocrd_dt is null then
2383     --
2384       l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2385     --
2386     else
2387     --
2388       l_event_date:= l_lf_evt_ocrd_dt;
2389     --
2390     end if;
2391     --
2392     if to_char(l_event_date, 'DD') > 15 then
2393       p_returned_date := round(l_event_date,'Month')  ;
2394     else
2395       p_returned_date := round(l_event_date,'Month') + 15 ;
2396     end if;
2397     --
2398   --  AFDCSM First of Semi-month.
2399   --
2400   elsif p_date_cd = 'AFDCSM' then
2401     --
2402     if g_debug then
2403       hr_utility.set_location('Entering AFDCSM',10);
2404     end if;
2405     --
2406     if l_lf_evt_ocrd_dt is null then
2407     --
2408       l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2409     --
2410     else
2411     --
2412       l_event_date:= l_lf_evt_ocrd_dt;
2413     --
2414     end if;
2415     --
2416     if to_char(l_event_date, 'DD') > 15 then
2417       --
2418       p_returned_date := trunc(l_event_date,'Month')+ 15  ;
2419       --
2420     else
2421       --
2422       p_returned_date := trunc(l_event_date,'Month')  ;
2423       --
2424     end if;
2425     --
2426 
2427 
2428   -- -------------------------------------------------------------------------
2429   -- After Completion
2430   -- -------------------------------------------------------------------------
2431   -- eg:  year 1-jan to 31-dec.  a. dt = 4-feb  b. dt=1-jan   c. dt=2-dec
2432   -- FDMCFC - First of Month on or After Completed
2433      -- a. 1-mar            b. 1-jan        c. 1-Jan-next
2434   -- FDMFC - First of Month After Completed
2435   -- FDLMPPYFC - First of Last Month in Year After Completion
2436      -- a. 1-dec            b. 1-dec        c. ?1-jan-next
2437   -- FDLPPPPYFC - First of Last Pay Period in Year After Completion
2438   -- FDPPCFC - First of Pay Period On or After Completion
2439      -- a. 1-mar            b. 1-jan        c. 1-jan-next  (if monthly)
2440   -- FDPPFC - First of Pay Period After Completion
2441   -- FDPPYCFC - First of Year On or After Completed
2442      -- a. 1-Jan-next       b. 1-Jan        c. 1-Jan-next
2443   -- FDPPYFC - First of Year After Completed
2444      -- a. 1-Jan-next       b. 1-Jan-next   c. 1-Jan-next
2445   -- FDSMFC First of Semi Month after Completion
2446      -- a. if day upto 15 then take 16th of the month else take 1st of following month
2447   -- FDSMCFC First of Semi Month on or after Completion
2448      -- a. if day upto 15 then take 16th of the month else take 1st of following month
2449      -- exception is for 1 take 1 and for 16 take 16
2450   --
2451   elsif p_date_cd  = 'FDMCFC' or p_date_cd = 'FDMFC' or p_date_cd = 'FDLMPPYFC'
2452      or p_date_cd = 'FDLPPPPYFC' or p_date_cd = 'FDPPCFC' or p_date_cd = 'FDPPFC'
2453      or p_date_cd = 'FDPPYCFC' or p_date_cd = 'FDPPYFC'
2454      or p_date_cd = 'FDSMFC' or p_date_cd = 'FDSMCFC'
2455   then
2456 
2457     if g_debug then
2458       hr_utility.set_location('Entering '||p_date_cd,14);
2459     end if;
2460 
2461     l_lf_evt_ocrd_dt := nvl(l_fonm_rt_cvg_strt_dt, p_effective_date) ;  -- completion date.
2462 
2463     l_cmpltd_dt     := nvl(p_cmpltd_dt,p_effective_date);
2464 
2465     if to_char( nvl(l_cmpltd_dt, p_effective_date), 'dd') = '01' and p_date_cd = 'FDMCFC' then
2466        -- First of Month on or After Completed
2467        -- p_returned_date :=  nvl(l_fonm_rt_cvg_strt_dt, p_effective_date);
2468        p_returned_date := l_cmpltd_dt ;
2469        --
2470     elsif p_date_cd = 'FDSMFC' then
2471       -- First of Semi Month after Completion
2472       if g_debug then
2473         hr_utility.set_location('Entering FDSMFC',10);
2474       end if;
2475       --
2476       /*
2477       if to_char(l_lf_evt_ocrd_dt, 'DD') > 15 then
2478         p_returned_date := round(l_lf_evt_ocrd_dt,'Month')  ;
2479       else
2480         p_returned_date := round(l_lf_evt_ocrd_dt,'Month') + 15 ;
2481       end if;
2482       */
2483       if to_char(l_cmpltd_dt, 'DD') > 15 then
2484         p_returned_date := round(l_cmpltd_dt,'Month')  ;
2485       else
2486         p_returned_date := round(l_cmpltd_dt,'Month') + 15 ;
2487       end if;
2488     --
2489     elsif p_date_cd = 'FDSMCFC' then
2490       -- First of Semi Month on or after Completion
2491       if g_debug then
2492         hr_utility.set_location('Entering FDSMCFC',10);
2493       end if;
2494       --
2495       /*
2496       if to_number(to_char(l_lf_evt_ocrd_dt, 'DD')) in ( 1, 16 )  then
2497          p_returned_date := l_lf_evt_ocrd_dt ;
2498       elsif to_char(l_lf_evt_ocrd_dt, 'DD') > 15 then
2499         p_returned_date := round(l_lf_evt_ocrd_dt,'Month')  ;
2500       else
2501         p_returned_date := round(l_lf_evt_ocrd_dt,'Month') + 15 ;
2502       end if;
2503       */
2504       if to_number(to_char(l_cmpltd_dt, 'DD')) in ( 1, 16 )  then
2505          p_returned_date := l_cmpltd_dt ;
2506       elsif to_char(l_cmpltd_dt, 'DD') > 15 then
2507         p_returned_date := round(l_cmpltd_dt,'Month')  ;
2508       else
2509         p_returned_date := round(l_cmpltd_dt,'Month') + 15 ;
2510       end if;
2511       --
2512     elsif (p_date_cd = 'FDMCFC' and to_char(l_cmpltd_dt, 'dd') <> '01')
2513           or p_date_cd = 'FDMFC' then
2514        -- First of Month After Completed
2515        -- p_returned_date := last_day(p_effective_date)+1;
2516        --
2517        p_returned_date := last_day(l_cmpltd_dt)+1;
2518        --
2519     elsif p_date_cd = 'FDLMPPYFC' or p_date_cd = 'FDPPYCFC' or p_date_cd = 'FDPPYFC'
2520        then
2521        get_plan_year
2522            (p_effective_date =>  p_effective_date
2523            ,p_lf_evt_ocrd_dt => l_cmpltd_dt -- l_lf_evt_ocrd_dt
2524            ,p_pl_id          => l_pl_id
2525            ,p_pgm_id         => l_pgm_id
2526            ,p_oipl_id        => l_oipl_id
2527            ,p_date_cd        => p_date_cd
2528            ,p_comp_obj_mode  => p_comp_obj_mode
2529            ,p_start_date     => l_start_date
2530            ,p_end_date       => l_end_date) ;
2531 
2532         if p_date_cd = 'FDLMPPYFC' then
2533            -- First of Last Month in Year After Completion
2534            if  nvl(l_cmpltd_dt, p_effective_date) >=  add_months(l_end_date,-1)+1 then
2535               -- if eff dt is on or after first day of last month in plan year
2536               -- use first day of month in next plan year?
2537               p_returned_date := last_day(l_end_date)+1;
2538            else
2539               p_returned_date := last_day(add_months(l_end_date,-1))+1;
2540            end if;
2541         elsif p_date_cd = 'FDPPYCFC' and l_cmpltd_dt = l_start_date then
2542            -- First of Year On or After Completed
2543               p_returned_date := l_start_date;
2544         elsif (p_date_cd = 'FDPPYCFC' and  nvl(l_cmpltd_dt, p_effective_date) <> l_start_date) or
2545                p_date_cd = 'FDPPYFC' then
2546                -- First of Year After Completed
2547                get_next_plan_year
2548                  (p_effective_date =>   p_effective_date
2549                  ,p_lf_evt_ocrd_dt => l_cmpltd_dt
2550                  ,p_pl_id          => l_pl_id
2551                  ,p_pgm_id         => l_pgm_id
2552                  ,p_oipl_id        => l_oipl_id
2553                  ,p_date_cd        => p_date_cd
2554                  ,p_comp_obj_mode  => p_comp_obj_mode
2555                  ,p_start_date     => l_start_date
2556                  ,p_end_date       => l_end_date) ;
2557 
2558                 p_returned_date :=l_start_date;
2559 
2560         end if;
2561     elsif p_date_cd = 'FDLPPPPYFC'  or p_date_cd = 'FDPPCFC'  or p_date_cd = 'FDPPFC'
2562          then
2563         -- First of Last Pay Period in Year After Completion
2564         -- FDLPPPPYFC  this is wrong - emailed Denise for correction.
2565 
2566         -- First of Pay Period (On or) After Completion
2567         if p_date_cd =  'FDPPCFC' then
2568            open c_pay_period_for_date( nvl(l_cmpltd_dt, p_effective_date));
2569            fetch c_pay_period_for_date into l_start_date, l_end_date;
2570            if c_pay_period_for_date%notfound and
2571               p_effective_date is not null then
2572               close c_pay_period_for_date;
2573             if g_debug then
2574                 hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',45);
2575             end if;
2576               fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
2577               fnd_message.set_token('DATE_CODE',p_date_cd);
2578               fnd_message.set_token('L_PROC',l_proc);
2579               fnd_message.set_token('PERSON_ID',l_person_id);
2580               fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
2581               fnd_message.set_token('EFFECTIVE_DATE',l_cmpltd_dt);
2582               fnd_message.raise_error;
2583 
2584            end if;
2585            close c_pay_period_for_date;
2586 
2587            if p_effective_date = l_start_date then
2588               p_returned_date := l_start_date;
2589            end if;
2590         end if;
2591 
2592         if (l_cmpltd_dt <> l_start_date and p_date_cd = 'FDPPCFC') or
2593            p_date_cd = 'FDPPFC' then
2594 
2595            open c_next_pay_period(nvl(l_cmpltd_dt,p_effective_date));
2596            fetch c_next_pay_period into l_next_pay_period;
2597 
2598            if c_next_pay_period%notfound and
2599               p_effective_date is not null then
2600              close c_next_pay_period;
2601            if g_debug then
2602                hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',55);
2603            end if;
2604              fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
2605              fnd_message.set_token('DATE_CODE',p_date_cd);
2606              fnd_message.set_token('L_PROC',l_proc);
2607              fnd_message.set_token('PERSON_ID',l_person_id);
2608              fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
2609              fnd_message.set_token('EFFECTIVE_DATE',l_cmpltd_dt);
2610              fnd_message.raise_error;
2611            end if;
2612            close c_next_pay_period;
2613 
2614            p_returned_date := l_next_pay_period.start_date;
2615         end if;
2616     end if;
2617 
2618 
2619   --
2620   -- AFDELY - First of Limitation Year
2621   --
2622   elsif p_date_cd  = 'AFDELY' then
2623   --
2624   if g_debug then
2625       hr_utility.set_location('Entering AFDELY',10);
2626   end if;
2627     --
2628     if l_lf_evt_ocrd_dt is null then
2629     --
2630        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2631     --
2632     else
2633     --
2634        l_lf_evt_ocrd_dt:= l_lf_evt_ocrd_dt;
2635     --
2636     end if;
2637     --
2638     --  get end date for comp object
2639     --
2640     if l_pl_id is not null then
2641       open c_pl_popl_lim_yr;
2642       fetch c_pl_popl_lim_yr into l_start_date, l_end_date;
2643       close c_pl_popl_lim_yr;
2644     --
2645     elsif l_pgm_id is not null then
2646       open c_pgm_popl_lim_yr;
2647       fetch c_pgm_popl_lim_yr into l_start_date, l_end_date;
2648       close c_pgm_popl_lim_yr;
2649     --
2650     elsif l_oipl_id is not null then
2651       open c_oipl_popl_lim_yr;
2652       fetch c_oipl_popl_lim_yr into l_start_date, l_end_date;
2653       close c_oipl_popl_lim_yr;
2654     --
2655     else
2656     if g_debug then
2657         hr_utility.set_location('BEN_92489_CANNOT_CALC_DATE',55);
2658     end if;
2659       fnd_message.set_name('BEN','BEN_92489_CANNOT_CALC_DATE');
2660       fnd_message.set_token('DATE_CODE',p_date_cd);
2661       fnd_message.set_token('L_PROC',l_proc);
2662       fnd_message.raise_error;
2663     --
2664     end if;
2665     --
2666     p_returned_date:= l_start_date;
2667   --
2668   -- ALDELMY - End of Limitation Year
2669   --
2670   elsif p_date_cd  = 'ALDELMY' then
2671   --
2672   if g_debug then
2673       hr_utility.set_location('Entering ALDELMY',10);
2674   end if;
2675     --
2676     if l_lf_evt_ocrd_dt is null then
2677     --
2678        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2679     --
2680     else
2681     --
2682        l_lf_evt_ocrd_dt:= l_lf_evt_ocrd_dt;
2683     --
2684     end if;
2685     --
2686     --  get end date for comp object
2687     --
2688     if l_pl_id is not null then
2689       open c_pl_popl_lim_yr;
2690       fetch c_pl_popl_lim_yr into l_start_date, l_end_date;
2691       close c_pl_popl_lim_yr;
2692     --
2693     elsif l_pgm_id is not null then
2694       open c_pgm_popl_lim_yr;
2695       fetch c_pgm_popl_lim_yr into l_start_date, l_end_date;
2696       close c_pgm_popl_lim_yr;
2697     --
2698     elsif l_oipl_id is not null then
2699       open c_oipl_popl_lim_yr;
2700       fetch c_oipl_popl_lim_yr into l_start_date, l_end_date;
2701       close c_oipl_popl_lim_yr;
2702     --
2703     else
2704     --  hr_utility.set_location('BEN_92489_CANNOT_CALC_DATE',55);
2705       fnd_message.set_name('BEN','BEN_92489_CANNOT_CALC_DATE');
2706       fnd_message.set_token('DATE_CODE',p_date_cd);
2707       fnd_message.set_token('L_PROC',l_proc);
2708       fnd_message.raise_error;
2709     --
2710     end if;
2711     --
2712     p_returned_date:= l_end_date;
2713   --
2714   -- FDODD - Within 5 Days After Due
2715   --
2716   elsif p_date_cd = 'FDODD' then
2717   --
2718   --  hr_utility.set_location('Entering FDODD',10);
2719     --
2720     p_returned_date:= p_effective_date + 5;
2721   --
2722   -- FRTYFV - Within 45 Days After Due
2723   --
2724   elsif p_date_cd = 'FRTYFV' then
2725   --
2726   --  hr_utility.set_location('Entering FRTYFV',10);
2727     --
2728     p_returned_date:= p_effective_date + 45;
2729   ---
2730   -- TDODD - Within 10 Days after Due
2731   --
2732   elsif p_date_cd = 'TDODD' then
2733   --
2734   --  hr_utility.set_location('Entering TDODD',10);
2735     --
2736     p_returned_date:= p_effective_date + 10;
2737   --
2738   -- THRTY - Within 30 Days after Due
2739   --
2740   elsif p_date_cd = 'THRTY' then
2741   --
2742   --  hr_utility.set_location('Entering THRTY',10);
2743     --
2744     p_returned_date:= p_effective_date + 30;
2745   --
2746   -- THRTYONE - Within 31 Days after Due
2747   --
2748   elsif p_date_cd = 'THRTYONE' then
2749   --
2750   --  hr_utility.set_location('Entering THRTYONE',10);
2751     --
2752     p_returned_date:= p_effective_date + 31;
2753   --
2754   -- DO - Date Occurred
2755   --
2756   elsif p_date_cd = 'DO' then
2757   --
2758   --  hr_utility.set_location('Entering DO',10);
2759 
2760     p_returned_date:= p_effective_date;
2761   --
2762   -- DR - Date Recorded or Notified
2763   --
2764   elsif p_date_cd in ('DR','ODNPE') then
2765   --
2766   --  hr_utility.set_location('Entering DR',10);
2767 
2768     p_returned_date:= sysdate;
2769   --
2770   -- LOR - Later Occurred Date or Recorded Date.
2771   --
2772   elsif p_date_cd = 'LOR' then
2773   --
2774   --  hr_utility.set_location('Entering LOR',10);
2775     --
2776     if sysdate > nvl(l_lf_evt_ocrd_dt,hr_api.g_sot) then
2777       p_returned_date := sysdate;
2778     else
2779       p_returned_date := p_effective_date;
2780     end if;
2781   --
2782   -- DAO - Day After Occurred Date.
2783   -- or
2784   -- ODAED - One day after Event Date
2785   --
2786     --
2787   elsif p_date_cd = 'DAO' or p_date_cd = 'ODAED' then
2788   --
2789   --  hr_utility.set_location('Entering DAO',10);
2790   --  hr_utility.set_location('Entering ODAED',10);
2791     --
2792     --p_returned_date := p_effective_date + 1;
2793     if l_lf_evt_ocrd_dt is null then
2794     --
2795        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2796     --
2797     else
2798     --
2799        l_event_date:= l_lf_evt_ocrd_dt;
2800     --
2801     end if;
2802     --
2803     p_returned_date:=l_event_date+1;
2804     --
2805   --
2806   -- LDAOR -  Later of Day After Occurred Date
2807   --          or Recorded Date.
2808   --
2809   elsif p_date_cd = 'LDAOR' then
2810   --
2811   --  hr_utility.set_location('Entering LDAOR',10);
2812     --
2813     if sysdate > (nvl(l_lf_evt_ocrd_dt,hr_api.g_sot) + 1) then
2814       p_returned_date := sysdate;
2815     else
2816       p_returned_date := p_effective_date + 1;
2817     end if;
2818   --
2819   -- ALDECLY - End of Calendar Year
2820   --
2821   elsif p_date_cd = 'ALDECLY' then
2822   --
2823   --  hr_utility.set_location('Entering ALDECLY',10);
2824     --
2825     if l_lf_evt_ocrd_dt is null then
2826     --
2827        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2828     --
2829     else
2830     --
2831        l_event_date:= l_lf_evt_ocrd_dt;
2832     --
2833     end if;
2834     --
2835     p_returned_date:= add_months(trunc(l_event_date,'YYYY'),12)-1;
2836     --
2837   --
2838   --
2839   -- FFDFED - 45 Days After Event
2840   --
2841   elsif p_date_cd = 'FFDFED' then
2842   --
2843   --  hr_utility.set_location('Entering FFDFED',10);
2844     --
2845     if l_lf_evt_ocrd_dt is null then
2846     --
2847        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2848     --
2849     else
2850     --
2851        l_event_date:= l_lf_evt_ocrd_dt;
2852     --
2853     end if;
2854     --
2855     p_returned_date := l_event_date+45;
2856   --
2857   -- NDFED - 90 Days After Event
2858   --
2859   elsif p_date_cd = 'NDFED' then
2860   --
2861   --  hr_utility.set_location('Entering NDFED',10);
2862     --
2863     if l_lf_evt_ocrd_dt is null then
2864     --
2865        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2866     --
2867     else
2868     --
2869        l_event_date:= l_lf_evt_ocrd_dt;
2870     --
2871     end if;
2872     --
2873     p_returned_date := l_event_date+90;
2874   --
2875   -- LFFDFED - 45 Days After Later Event or Notified
2876   --
2877   elsif p_date_cd = 'LFFDFED' then
2878     --
2879   --  hr_utility.set_location('Entering LFFDFED',10);
2880     --
2881     if l_lf_evt_ocrd_dt is null then
2882     --
2883        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2884     --
2885     else
2886     --
2887        l_event_date:= l_lf_evt_ocrd_dt;
2888     --
2889     end if;
2890     --
2891     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2892     --
2893     if l_event_date > l_recorded_date then
2894     --
2895        l_date := l_event_date+45;
2896     --
2897     else
2898     --
2899        l_date := l_recorded_date+45;
2900     --
2901     end if;
2902     --
2903     p_returned_date := l_date;
2904   --
2905   -- LNDFED - 90 Days After Later Event or Notified
2906   --
2907   elsif p_date_cd = 'LNDFED' then
2908     --
2909   --  hr_utility.set_location('Entering LNDFED',10);
2910     --
2911     if l_lf_evt_ocrd_dt is null then
2912     --
2913        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2914     --
2915     else
2916     --
2917        l_event_date:= l_lf_evt_ocrd_dt;
2918     --
2919     end if;
2920     --
2921     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2922     --
2923     if l_event_date > l_recorded_date then
2924     --
2925        l_date := l_event_date+90;
2926     --
2927     else
2928     --
2929        l_date := l_recorded_date+90;
2930     --
2931     end if;
2932     --
2933     p_returned_date := l_date;
2934   --
2935   -- LTDFED - 30 Days After Later Event or Notified
2936   --
2937   elsif p_date_cd = 'LTDFED' then
2938     --
2939   --  hr_utility.set_location('Entering LTDFED',10);
2940     --
2941     if l_lf_evt_ocrd_dt is null then
2942     --
2943        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2944     --
2945     else
2946     --
2947        l_event_date:= l_lf_evt_ocrd_dt;
2948     --
2949     end if;
2950     --
2951     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2952     --
2953     if l_event_date > l_recorded_date then
2954     --
2955        l_date := l_event_date+30;
2956     --
2957     else
2958     --
2959        l_date := l_recorded_date+30;
2960     --
2961     end if;
2962     --
2963     p_returned_date := l_date;
2964   --
2965   -- ALDPM - End of Previous Month
2966   --
2967   elsif p_date_cd = 'ALDPM' then
2968 
2969   --  hr_utility.set_location('Entering ALDPM',10);
2970 
2971     if l_lf_evt_ocrd_dt is null then
2972 
2973        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2974 
2975     else
2976 
2977        l_event_date:= l_lf_evt_ocrd_dt;
2978 
2979     end if;
2980 
2981     l_date := add_months(l_event_date,-1);
2982 
2983     p_returned_date := last_day(l_date);
2984 
2985   -- LALDPM - End of Previous Month Later Event or Notified
2986 
2987   elsif p_date_cd = 'LALDPM' then
2988 
2989   --  hr_utility.set_location('Entering LALDPM',10);
2990 
2991     if l_lf_evt_ocrd_dt is null then
2992 
2993        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
2994 
2995     else
2996 
2997        l_event_date:= l_lf_evt_ocrd_dt;
2998 
2999     end if;
3000 
3001     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3002 
3003     if l_event_date > l_recorded_date then
3004 
3005        l_date := add_months(l_event_date,-1);
3006 
3007     else
3008 
3009        l_date := add_months(l_recorded_date,-1);
3010 
3011     end if;
3012     p_returned_date := last_day(l_date);
3013 
3014   -- ALDPPP - End of Previous Pay period
3015 
3016   elsif p_date_cd = 'ALDPPP' then
3017     --
3018   --  hr_utility.set_location('Entering ALDPPP',10);
3019 
3020     if l_lf_evt_ocrd_dt is null then
3021 
3022        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3023 
3024     else
3025 
3026        l_lf_evt_ocrd_dt := l_lf_evt_ocrd_dt;
3027 
3028     end if;
3029 
3030     open c_pay_period;
3031       fetch c_pay_period into l_pay_period;
3032 
3033     if c_pay_period%notfound then
3034 
3035       close c_pay_period;
3036     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',65);
3037       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
3038       fnd_message.set_token('DATE_CODE',p_date_cd);
3039       fnd_message.set_token('L_PROC',l_proc);
3040       fnd_message.set_token('PERSON_ID',l_person_id);
3041       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
3042       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
3043       fnd_message.raise_error;
3044 
3045     end if;
3046 
3047     close c_pay_period ;
3048 
3049     p_returned_date := l_pay_period.start_date-1;
3050 
3051   -- LALDPPP - End of Previous Pay period Later Event or Notified
3052 
3053   elsif p_date_cd = 'LALDPPP' then
3054     --
3055   --  hr_utility.set_location('Entering LALDPPP',10);
3056 
3057     if l_lf_evt_ocrd_dt is null then
3058 
3059        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3060 
3061     else
3062 
3063        l_lf_evt_ocrd_dt := l_lf_evt_ocrd_dt;
3064 
3065     end if;
3066 
3067     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3068 
3069     -- Use later of event or recorded date
3070 
3071     if l_lf_evt_ocrd_dt < l_recorded_date then
3072 
3073        l_lf_evt_ocrd_dt := l_recorded_date;
3074 
3075     end if;
3076 
3077     open c_pay_period;
3078       fetch c_pay_period into l_pay_period;
3079 
3080     if c_pay_period%notfound then
3081 
3082       close c_pay_period;
3083     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',70);
3084       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
3085       fnd_message.set_token('DATE_CODE',p_date_cd);
3086       fnd_message.set_token('L_PROC',l_proc);
3087       fnd_message.set_token('PERSON_ID',l_person_id);
3088       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
3089       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
3090       fnd_message.raise_error;
3091 
3092     end if;
3093 
3094     close c_pay_period ;
3095 
3096     p_returned_date := l_pay_period.start_date-1;
3097 
3098 
3099   -- LALDPPPY - Last Day of previous Program or Plan Year
3100   --            Later Event or Notified
3101 
3102   elsif p_date_cd  = 'LALDPPPY' then
3103 
3104   --  hr_utility.set_location('Entering LALDPPPY',10);
3105 
3106     if l_lf_evt_ocrd_dt is null then
3107 
3108        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3109 
3110     else
3111 
3112        l_lf_evt_ocrd_dt := l_lf_evt_ocrd_dt;
3113 
3114     end if;
3115 
3116     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3117 
3118     -- Use later of event or recorded date
3119 
3120     if l_lf_evt_ocrd_dt < l_recorded_date then
3121 
3122        l_lf_evt_ocrd_dt := l_recorded_date;
3123 
3124     end if;
3125 
3126     get_plan_year
3127            (p_effective_date => p_effective_date
3128            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3129            ,p_pl_id          => l_pl_id
3130            ,p_pgm_id         => l_pgm_id
3131            ,p_oipl_id        => l_oipl_id
3132            ,p_date_cd        => p_date_cd
3133            ,p_comp_obj_mode  => p_comp_obj_mode
3134            ,p_start_date     => l_start_date
3135            ,p_end_date       => l_end_date) ;
3136 
3137     p_returned_date := l_start_date-1;
3138 
3139   -- -------------------------------------------------------------------
3140   -- First Day of Quarters
3141   -- -------------------------------------------------------------------
3142   -- eg:  year 1-jan to 31-dec.  a. dt = 1-feb  b. dt=1-jul   c. dt=1-nov
3143   -- *AFDCPPQ - First day of current program or plan Quarter
3144      -- a. 1-Jan            b. 1-jul        c. 1-Oct
3145   -- AFDFPPQ - First of next Quarter
3146      -- a. 1-Apr            b. 1-oct        c. 1-Jan-Next
3147   -- *LAFDFPPQ - First of Quarter After Later Event or Notified
3148      -- a. 1-Apr            b. 1-oct        c. 1-Jan-Next
3149   -- FDPPQCF - First of Quarter on or After Event
3150      -- a. 1-Apr            b. 1-jul        c. 1-Jan-Next
3151   -- *LFDPPQCF - First of Quarter on or After Later Event or Notified
3152      -- a. 1-Apr            b. 1-jul        c. 1-Jan-Next
3153 
3154   -- -------------------------------------------------------------------
3155   -- Last Day of Quarters
3156   -- -------------------------------------------------------------------
3157   -- *ALDCPPQ -  Last Day of the Current Program or Plan Quarter
3158      -- a. 31-Mar            b. 30-Sep        c. 31-dec
3159   -- LALDCPPQ - End of Quarter Later event or Notified
3160      -- a. 31-Mar            b. 30-Sep        c. 31-dec
3161   -- *ALDPPPQ - End of Previous Quarter
3162      -- a. 31-Dec-last       b. 30-Jun        c. 31-sep
3163   -- LALDPPPQ - End of Previous Quarter Later Event or Notified
3164      -- a. 31-Dec-last       b. 30-Jun        c. 31-sep
3165 
3166 
3167 
3168   elsif p_date_cd = 'AFDFPPQ' or p_date_cd = 'ALDCPPQ' or p_date_cd = 'FDPPQCF'
3169      or p_date_cd = 'AFDCPPQ' or p_date_cd = 'LAFDFPPQ' or p_date_cd = 'ALDPPPQ'
3170      or p_date_cd = 'LALDPPPQ' or p_date_cd = 'LALDCPPQ' or p_date_cd = 'LFDPPQCF'
3171      then
3172 
3173   --  hr_utility.set_location('Entering '||p_date_cd,12);
3174 
3175     if l_lf_evt_ocrd_dt is null then
3176        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3177     end if;
3178 
3179     if p_date_cd = 'LAFDFPPQ' or p_date_cd = 'LALDPPPQ' or p_date_cd = 'LALDCPPQ'
3180        or p_date_cd = 'LFDPPQCF' then
3181        -- Use later of event or recorded date
3182        l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3183        if l_lf_evt_ocrd_dt < l_recorded_date then
3184           l_lf_evt_ocrd_dt := l_recorded_date;
3185        end if;
3186     end if;
3187 
3188     get_plan_year
3189            (p_effective_date => p_effective_date
3190            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3191            ,p_pl_id          => l_pl_id
3192            ,p_pgm_id         => l_pgm_id
3193            ,p_oipl_id        => l_oipl_id
3194            ,p_date_cd        => p_date_cd
3195            ,p_comp_obj_mode  => p_comp_obj_mode
3196            ,p_start_date     => l_start_date
3197            ,p_end_date       => l_end_date) ;
3198 
3199     l_months := round(months_between(l_end_date,l_start_date)/4);
3200     l_date := add_months(l_start_date,l_months); -- end date of '1st' quarter
3201 
3202     -- First day of quarters:
3203     if p_date_cd = 'AFDFPPQ'  or p_date_cd = 'LAFDFPPQ' then -- First of next Qtr
3204        if l_lf_evt_ocrd_dt < l_date then
3205           p_returned_date := l_date;
3206 
3207        elsif l_lf_evt_ocrd_dt < add_months(l_date,l_months) then
3208           p_returned_date := add_months(l_date,l_months);
3209 
3210        elsif l_lf_evt_ocrd_dt < add_months(l_date,(2*l_months)) then
3211           p_returned_date := add_months(l_date,(2*l_months));
3212 
3213        elsif l_lf_evt_ocrd_dt <= l_end_date then
3214           p_returned_date := l_end_date+1;
3215        end if;
3216     elsif p_date_cd = 'FDPPQCF' or p_date_cd = 'LFDPPQCF' then
3217        --First of Quarter on or After Event
3218        if l_lf_evt_ocrd_dt = l_start_date then
3219           p_returned_date := l_start_date;
3220 
3221        elsif l_lf_evt_ocrd_dt <= l_date then
3222           p_returned_date := l_date;
3223        elsif l_lf_evt_ocrd_dt <= add_months(l_date,l_months) then
3224           p_returned_date := add_months(l_date,l_months);
3225 
3226        elsif l_lf_evt_ocrd_dt <= add_months(l_date,(2*l_months)) then
3227           p_returned_date := add_months(l_date,(2*l_months));
3228 
3229        elsif l_lf_evt_ocrd_dt <= l_end_date then
3230           p_returned_date := l_end_date+1;
3231        end if;
3232     elsif p_date_cd = 'AFDCPPQ' then  --First day of current pgm or plan Qtr
3233        if l_lf_evt_ocrd_dt < l_date then
3234           p_returned_date := l_start_date;
3235 
3236        elsif l_lf_evt_ocrd_dt < add_months(l_date,l_months) then
3237           p_returned_date := l_date;
3238 
3239        elsif l_lf_evt_ocrd_dt < add_months(l_date,(2*l_months)) then
3240           p_returned_date := add_months(l_date,(l_months));
3241 
3242        elsif l_lf_evt_ocrd_dt <= l_end_date then
3243           p_returned_date := add_months(l_date,(2*l_months));
3244        end if;
3245 
3246     -- last day of quarters:
3247     elsif p_date_cd = 'ALDCPPQ' or p_date_cd = 'LALDCPPQ' then
3248        --Last Day of the Current Pgm or Plan Qtr
3249        if l_lf_evt_ocrd_dt < l_date then
3250           p_returned_date := l_date-1;
3251 
3252        elsif l_lf_evt_ocrd_dt < add_months(l_date,l_months) then
3253          p_returned_date := add_months(l_date,l_months)-1;
3254 
3255        elsif l_lf_evt_ocrd_dt < add_months(l_date,(2*l_months)) then
3256           p_returned_date := add_months(l_date,(2*l_months))-1;
3257 
3258        elsif l_lf_evt_ocrd_dt <= l_end_date then
3259           p_returned_date := l_end_date;
3260        end if;
3261     elsif p_date_cd = 'ALDPPPQ' or p_date_cd = 'LALDPPPQ' then  -- End of Prev Qtr
3262        if l_lf_evt_ocrd_dt < l_date then
3263           p_returned_date := l_start_date-1;
3264 
3265        elsif l_lf_evt_ocrd_dt < add_months(l_date,l_months) then
3266           p_returned_date := l_date-1;
3267 
3268        elsif l_lf_evt_ocrd_dt < add_months(l_date,(2*l_months)) then
3269           p_returned_date := add_months(l_date,l_months)-1;
3270 
3271        elsif l_lf_evt_ocrd_dt <= l_end_date then
3272           p_returned_date := add_months(l_date,(2*l_months))-1;
3273        end if;
3274 
3275     end if;
3276 
3277   -- SDBED - Sixty days Before Event Date
3278   elsif p_date_cd = 'SDBED' then
3279 
3280   --  hr_utility.set_location('Entering SDBED',10);
3281 
3282     if l_lf_evt_ocrd_dt is null then
3283 
3284        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3285 
3286     else
3287 
3288        l_event_date:= l_lf_evt_ocrd_dt;
3289 
3290     end if;
3291 
3292     p_returned_date := l_event_date - 60;
3293 
3294 
3295   -- -------------------------------------------------------------------
3296   -- First Day of Semi (or Half) Years
3297   -- -------------------------------------------------------------------
3298 
3299   -- eg:  year 1-jan to 31-dec.  a. dt = 1-feb  b. dt=1-jul   c. dt=1-nov
3300   -- AFDCSPPY - First day of current program or plan semi year
3301      -- a. 1-Jan            b. 1-jul        c. 1-jul
3302   -- FDSPPYCF - First Day of Half year On or After Event
3303      -- a. 1-jul            b. 1-jul        c. 1-jan-next
3304   -- LFDPPSYCF - first of half year on or after later of event or notified
3305      -- a. 1-jul            b. 1-jul        c. 1-jan-next
3306   -- LAFDFPPSY - Later: First day of following program or plan semi year
3307      -- a. 1-jul            b. 1-jan-next    c. 1-jan-next
3308   -- AFDFPPSY - First day of following program or plan semi year
3309      -- a. 1-jul            b. 1-jan-next    c. 1-jan-next
3310 
3311   -- -------------------------------------------------------------------
3312   -- Last Day of Semi (or Half) Years
3313   -- -------------------------------------------------------------------
3314   -- ALDCPPSY - Last day of current program or plan semi year
3315      -- a. 30-jun            b. 31-dec       c. 31-dec
3316   -- LALDCPPSY - End of Half Year Later Event or Notified
3317      -- a. 30-jun            b. 31-dec       c. 31-dec
3318   -- ALDPPPSY - Last day of previous program or plan semi year
3319      -- a. 31-dec-prev       b. 31-jun       c. 31-jun
3320   -- LALDPPPSY - Last day of prev pgm or plan semiyear- Later Event or Notified
3321      -- a. 31-dec-prev       b. 31-jun       c. 31-jun
3322 
3323   elsif p_date_cd  = 'LFDPPSYCF' or p_date_cd  = 'FDSPPYCF' or p_date_cd = 'AFDCSPPY'
3324      or p_date_cd  = 'LAFDFPPSY' or p_date_cd = 'AFDFPPSY' or p_date_cd = 'ALDCPPSY'
3325      or p_date_cd = 'LALDCPPSY'  or p_date_cd = 'ALDPPPSY' or p_date_cd = 'LALDPPPSY'
3326      then
3327 
3328   --  hr_utility.set_location('Entering '||p_date_cd,22);
3329 
3330     if l_lf_evt_ocrd_dt is null then
3331        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3332     end if;
3333 
3334     if p_date_cd  = 'LFDPPSYCF' or p_date_cd = 'LAFDFPPSY'  or
3335        p_date_cd = 'LALDCPPSY' or p_date_cd = 'LALDPPPSY' then
3336        -- Use later of event or recorded date
3337        l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3338        if l_lf_evt_ocrd_dt < l_recorded_date then
3339           l_lf_evt_ocrd_dt := l_recorded_date;
3340        end if;
3341     end if;
3342 
3343     get_plan_year
3344            (p_effective_date => p_effective_date
3345            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3346            ,p_pl_id          => l_pl_id
3347            ,p_pgm_id         => l_pgm_id
3348            ,p_oipl_id        => l_oipl_id
3349            ,p_date_cd        => p_date_cd
3350            ,p_comp_obj_mode  => p_comp_obj_mode
3351            ,p_start_date     => l_start_date
3352            ,p_end_date       => l_end_date) ;
3353 
3354     l_months := round(months_between(l_end_date,l_start_date)/2);
3355     l_date := add_months(l_start_date,l_months);  -- half year date.
3356 
3357     --
3358     -- First Day of Semi (or Half) Years:
3359     --
3360     if p_date_cd = 'FDSPPYCF' or p_date_cd = 'LFDPPSYCF' then
3361        --First Day of Half year On or After Event
3362        if l_lf_evt_ocrd_dt = l_start_date then
3363           p_returned_date := l_start_date;
3364        elsif l_lf_evt_ocrd_dt <= l_date then
3365           p_returned_date := l_date;
3366        else
3367           p_returned_date := l_end_date+1;
3368        end if;
3369     elsif p_date_cd = 'LAFDFPPSY' or p_date_cd = 'AFDFPPSY' then
3370        -- First day of following program or plan semi year
3371        if l_lf_evt_ocrd_dt < l_date then
3372           p_returned_date := l_date;
3373        else
3374           p_returned_date := l_end_date+1;
3375        end if;
3376     elsif p_date_cd = 'AFDCSPPY' then
3377        -- First day of current program or plan semi year
3378        if l_lf_evt_ocrd_dt < l_date then
3379           p_returned_date := l_start_date;
3380        else
3381           p_returned_date := l_date;
3382        end if;
3383     --
3384     -- Last Day of Semi (or Half) Years:
3385     --
3386     elsif p_date_cd = 'ALDCPPSY' or p_date_cd = 'LALDCPPSY' then
3387        -- Last day of current program or plan semi year
3388        if l_lf_evt_ocrd_dt < l_date then
3389           p_returned_date := l_date -1;
3390        else
3391           p_returned_date := l_end_date;
3392        end if;
3393     elsif p_date_cd = 'ALDPPPSY' or p_date_cd = 'LALDPPPSY' then
3394         -- Last day of previous program or plan semi year
3395        if l_lf_evt_ocrd_dt < l_date then
3396           p_returned_date := l_start_date -1;
3397        else
3398           p_returned_date := l_date;
3399        end if;
3400 
3401     end if;
3402 
3403   -- ODCED - Dependent Coverage End
3404   elsif p_date_cd  = 'ODCED'  then
3405 
3406   --  hr_utility.set_location('Entering ODCED',10);
3407 
3408     if l_lf_evt_ocrd_dt is null then
3409 
3410        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3411 
3412 
3413     end if;
3414 
3415     open c_elig_dpnt_dts;
3416     fetch c_elig_dpnt_dts into l_start_date,
3417                                l_end_date;
3418 
3419     close c_elig_dpnt_dts;
3420 
3421     p_returned_date:= l_end_date;
3422 
3423   -- ODCSD - Dependent Coverage End
3424 
3425   elsif p_date_cd  = 'ODCSD'  then
3426 
3427   --  hr_utility.set_location('Entering ODCSD',10);
3428 
3429     if l_lf_evt_ocrd_dt is null then
3430 
3431        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3432 
3433     end if;
3434 
3435     open c_elig_dpnt_dts;
3436     fetch c_elig_dpnt_dts into l_start_date,
3437                                l_end_date;
3438 
3439     close c_elig_dpnt_dts;
3440 
3441     p_returned_date:= l_start_date;
3442 
3443   -- AFDECY - First of Calendar year
3444 
3445   elsif p_date_cd  = 'AFDECY'  then
3446 
3447   --  hr_utility.set_location('Entering AFDECY',10);
3448 
3449     if l_lf_evt_ocrd_dt is null then
3450 
3451        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3452 
3453     end if;
3454 
3455     p_returned_date:= trunc(l_lf_evt_ocrd_dt,'YYYY');
3456 
3457   -- 30DANED - 30 Days After Notice to Enroll
3458 
3459   elsif p_date_cd  = '30DANED'  then
3460 
3461   --  hr_utility.set_location('Entering 30DANED',10);
3462 
3463     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3464 
3465     p_returned_date := l_recorded_date + 30;
3466 
3467   -- AFDCMFDFFNM - First of Month,( if From Day(DOB) is First, First of Next Month )
3468   -- Example of the functionality
3469   --Case DOB 08/03/1935 , Event 08/03/2000 , return date should be 08/01/2000
3470   --Case DOB 08/01/1935 , Event 08/03/2000 , return date should be 09/01/2000
3471 
3472   elsif p_date_cd = 'AFDCMFDFFNM' then
3473 
3474   --  hr_utility.set_location('Entering AFDCMFDFFNM',10);
3475 
3476     if l_lf_evt_ocrd_dt is null then
3477 
3478        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3479 
3480 
3481     end if;
3482 
3483     open c_birth_date;
3484     fetch c_birth_date into l_start_date;
3485     close c_birth_date;
3486     --Bug 1802578
3487     /*
3488 
3489     l_date := last_day(add_months(l_event_date,-1))+1;
3490 
3491     if l_date = l_start_date then
3492 
3493        p_returned_date := add_months(l_date,1);
3494 
3495     else
3496 
3497        p_returned_date := l_date;
3498 
3499     end if;
3500     */
3501     declare
3502       l_day   number := null ;
3503     begin
3504       --
3505       l_day := to_number(to_char(l_start_date, 'DD')) ;
3506       if l_day = 1 then
3507         --
3508         p_returned_date := last_day(l_event_date)+1 ;
3509         --
3510       else
3511         --
3512         p_returned_date := last_day(add_months(l_event_date,-1))+1;
3513         --
3514       end if;
3515       --
3516     end ;
3517       --
3518   --
3519   -- DOD - Date of Determination
3520   --
3521   elsif p_date_cd = 'DOD' then
3522   --
3523   --  hr_utility.set_location('Entering DOD',10);
3524     --
3525     if l_lf_evt_ocrd_dt is null then
3526     --
3527        p_returned_date:= p_effective_date;
3528     --
3529     else
3530     --
3531        p_returned_date:= l_lf_evt_ocrd_dt;
3532     --
3533     end if;
3534     --
3535   --
3536   -- DODM1 - Date of Determination Minus 1 day
3537   --
3538   elsif p_date_cd = 'DODM1' then
3539   --
3540   --  hr_utility.set_location('Entering DODM1',10);
3541     --
3542     if l_lf_evt_ocrd_dt is null then
3543     --
3544        p_returned_date:= p_effective_date - 1;
3545     --
3546     else
3547     --
3548        p_returned_date:= l_lf_evt_ocrd_dt - 1;
3549     --
3550     end if;
3551     --
3552   --
3553   -- FDLMEPPYCF - First Day of Last Month in Event Program or Plan Year
3554   --              Concurrent with or Following
3555   --
3556   elsif p_date_cd  = 'FDLMEPPYCF' then
3557   --
3558   --  hr_utility.set_location('Entering FDLMEPPYCF',10);
3559     --
3560     if l_lf_evt_ocrd_dt is null then
3561     --
3562        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3563     --
3564     end if;
3565     --
3566     get_plan_year
3567            (p_effective_date => p_effective_date
3568            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3569            ,p_pl_id          => l_pl_id
3570            ,p_pgm_id         => l_pgm_id
3571            ,p_oipl_id        => l_oipl_id
3572            ,p_date_cd        => p_date_cd
3573            ,p_comp_obj_mode  => p_comp_obj_mode
3574            ,p_start_date     => l_start_date
3575            ,p_end_date       => l_end_date) ;
3576 
3577     if l_lf_evt_ocrd_dt = last_day(add_months(l_end_date,-1))+1 then
3578     --
3579        p_returned_date := last_day(add_months(l_end_date,-1))+1;
3580     --
3581     else
3582     --
3583        get_next_plan_year
3584                  (p_effective_date => p_effective_date
3585                  ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3586                  ,p_pl_id          => l_pl_id
3587                  ,p_pgm_id         => l_pgm_id
3588                  ,p_oipl_id        => l_oipl_id
3589                  ,p_date_cd        => p_date_cd
3590                  ,p_comp_obj_mode  => p_comp_obj_mode
3591                  ,p_start_date     => l_next_popl_yr_strt
3592                  ,p_end_date       => l_next_popl_yr_end) ;
3593        --
3594        p_returned_date := last_day(add_months(l_end_date,-1))+1;
3595     --
3596     end if;
3597     --
3598   --
3599   -- ALDPPPY - Last Day of previous Program or Plan Year
3600   --
3601   elsif p_date_cd  = 'ALDPPPY' then
3602   --
3603   --  hr_utility.set_location('Entering ALDPPPY',10);
3604     --
3605     if l_lf_evt_ocrd_dt is null then
3606     --
3607        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3608     --
3609     end if;
3610     --
3611     get_plan_year
3612            (p_effective_date => p_effective_date
3613            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3614            ,p_pl_id          => l_pl_id
3615            ,p_pgm_id         => l_pgm_id
3616            ,p_oipl_id        => l_oipl_id
3617            ,p_date_cd        => p_date_cd
3618            ,p_comp_obj_mode  => p_comp_obj_mode
3619            ,p_start_date     => l_start_date
3620            ,p_end_date       => l_end_date) ;
3621 
3622     p_returned_date := l_start_date-1;
3623     --
3624   --
3625   -- ALDLPPEPPY - Last Day Last Pay period of Event Program or plan Year
3626   --
3627   elsif p_date_cd  = 'ALDLPPEPPY' then
3628     --
3629   --  hr_utility.set_location('Entering ALDLPPEPPY',10);
3630     --
3631     if l_lf_evt_ocrd_dt is null then
3632 
3633        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3634 
3635 
3636     end if;
3637     --
3638     get_plan_year
3639            (p_effective_date => p_effective_date
3640            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3641            ,p_pl_id          => l_pl_id
3642            ,p_pgm_id         => l_pgm_id
3643            ,p_oipl_id        => l_oipl_id
3644            ,p_date_cd        => p_date_cd
3645            ,p_comp_obj_mode  => p_comp_obj_mode
3646            ,p_start_date     => l_start_date
3647            ,p_end_date       => l_end_date) ;
3648     --
3649     open c_pay_period_for_date(l_end_date);
3650     fetch c_pay_period_for_date into
3651           l_start_date,
3652           l_end_date;
3653     --
3654     if c_pay_period_for_date%notfound then
3655     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',95);
3656       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
3657       fnd_message.set_token('DATE_CODE',p_date_cd);
3658       fnd_message.set_token('L_PROC',l_proc);
3659       fnd_message.set_token('PERSON_ID',l_person_id);
3660       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
3661       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
3662       fnd_message.raise_error;
3663     --
3664     end if;
3665     --
3666     p_returned_date:=l_end_date;
3667     --
3668     close c_pay_period_for_date;
3669   --
3670   -- FDLPPEPPY - First Day Last Pay period of Event Program or plan Year
3671   --
3672   elsif p_date_cd  = 'FDLPPEPPY' then
3673     --
3674   --  hr_utility.set_location('Entering FDLPPEPPY',10);
3675     --
3676     if l_lf_evt_ocrd_dt is null then
3677     --
3678        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3679     --
3680     end if;
3681     --
3682     get_plan_year
3683            (p_effective_date => p_effective_date
3684            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3685            ,p_pl_id          => l_pl_id
3686            ,p_pgm_id         => l_pgm_id
3687            ,p_oipl_id        => l_oipl_id
3688            ,p_date_cd        => p_date_cd
3689            ,p_comp_obj_mode  => p_comp_obj_mode
3690            ,p_start_date     => l_start_date
3691            ,p_end_date       => l_end_date) ;
3692     --
3693     open c_pay_period_for_date(l_end_date);
3694     fetch c_pay_period_for_date into
3695           l_start_date,
3696           l_end_date;
3697     --
3698     if c_pay_period_for_date%notfound then
3699     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',100);
3700       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
3701       fnd_message.set_token('DATE_CODE',p_date_cd);
3702       fnd_message.set_token('L_PROC',l_proc);
3703       fnd_message.set_token('PERSON_ID',l_person_id);
3704       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
3705       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
3706       fnd_message.raise_error;
3707     end if;
3708     --
3709     p_returned_date:=l_start_date;
3710     --
3711     close c_pay_period_for_date;
3712     --
3713   --
3714   -- FDLPPEPPYCF - First Day Last Pay period in Year on or after event
3715   --
3716   elsif p_date_cd  = 'FDLPPEPPYCF' then
3717     --
3718   --  hr_utility.set_location('Entering FDLPPEPPYCF',10);
3719     --
3720     if l_lf_evt_ocrd_dt is null then
3721     --
3722        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3723     --
3724     end if;
3725     --
3726     get_plan_year
3727            (p_effective_date => p_effective_date
3728            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
3729            ,p_pl_id          => l_pl_id
3730            ,p_pgm_id         => l_pgm_id
3731            ,p_oipl_id        => l_oipl_id
3732            ,p_date_cd        => p_date_cd
3733            ,p_comp_obj_mode  => p_comp_obj_mode
3734            ,p_start_date     => l_start_date
3735            ,p_end_date       => l_end_date) ;
3736     --
3737     open c_pay_period_for_date(l_end_date);
3738     fetch c_pay_period_for_date into
3739           l_start_date,
3740           l_end_date;
3741     --
3742     if c_pay_period_for_date%notfound then
3743     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',105);
3744       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
3745       fnd_message.set_token('DATE_CODE',p_date_cd);
3746       fnd_message.set_token('L_PROC',l_proc);
3747       fnd_message.set_token('PERSON_ID',l_person_id);
3748       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
3749       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
3750       fnd_message.raise_error;
3751     end if;
3752     --
3753     p_returned_date:=l_start_date;
3754     --
3755     close c_pay_period_for_date;
3756   --
3757   -- ODBED - One day before Event Date
3758   --
3759   elsif p_date_cd =  'ODBED' then
3760     --
3761   --  hr_utility.set_location('Entering ODBED WODBED OR WEM',10);
3762     --
3763     if l_lf_evt_ocrd_dt is null then
3764     --
3765        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3766     --
3767     else
3768        l_event_date:= l_lf_evt_ocrd_dt;
3769     --
3770     end if;
3771     --
3772     p_returned_date:=l_event_date-1;
3773     --
3774   --
3775   -- WEM   - 1 Prior
3776   --
3777   elsif p_date_cd = 'WEM' then
3778     --
3779     if g_debug then
3780       hr_utility.set_location('Entering WEM',10);
3781     end if;
3782     --
3783     if l_lf_evt_ocrd_dt is null then
3784     --
3785        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3786     --
3787     else
3788     --
3789        l_event_date:= l_lf_evt_ocrd_dt;
3790     --
3791     end if;
3792     --
3793     p_returned_date:=nvl(p_start_date,l_event_date -1 ) ;
3794     --
3795   -- WODBED - 1 Prior or One day before event
3796   --
3797   elsif p_date_cd =  'WODBED' then
3798     --
3799     if g_debug then
3800       hr_utility.set_location('Entering WODBED',10);
3801     end if;
3802     --
3803       --
3804       if l_lf_evt_ocrd_dt is null then
3805       --
3806         l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3807       --
3808       else
3809       --
3810         l_event_date:= l_lf_evt_ocrd_dt;
3811       --
3812       end if;
3813       --
3814       p_returned_date:=l_event_date-1;
3815       --
3816   -- AFDCM - First day of Current Month
3817   --
3818   elsif p_date_cd = 'AFDCM' then
3819   --
3820   --  hr_utility.set_location('Entering AFDCM',10);
3821     --
3822     if l_lf_evt_ocrd_dt is null then
3823     --
3824        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3825     --
3826     else
3827     --
3828        l_event_date:= l_lf_evt_ocrd_dt;
3829     --
3830     end if;
3831     --
3832     l_date := add_months(l_event_date,-1);
3833     --
3834     p_returned_date := last_day(l_date)+1;
3835     --
3836   --
3837   -- ALDCM - Last day of Current Month
3838   --
3839   elsif p_date_cd = 'ALDCM'  then
3840   --
3841     if g_debug then
3842       hr_utility.set_location('Entering ALDCM',10);
3843     end if;
3844     --
3845     if l_lf_evt_ocrd_dt is null then
3846     --
3847        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3848     --
3849     else
3850     --
3851        l_event_date:= l_lf_evt_ocrd_dt;
3852     --
3853     end if;
3854     --
3855     p_returned_date := last_day(l_event_date);
3856   --
3857   -- WALDCM - 1 Prior or Month End
3858   --
3859   elsif p_date_cd = 'WALDCM' then
3860   --
3861     if g_debug then
3862       hr_utility.set_location('Entering WALDCM',10);
3863     end if;
3864     --
3865       if l_lf_evt_ocrd_dt is null then
3866       --
3867         l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3868       --
3869       else
3870         --
3871         l_event_date:= l_lf_evt_ocrd_dt;
3872         --
3873       end if;
3874       --
3875       p_returned_date := last_day(l_event_date);
3876     --
3877     --
3878   -- ALDFM - Last day of Following Month
3879   --
3880   elsif p_date_cd = 'ALDFM' then
3881   --
3882   --  hr_utility.set_location('Entering ALDFM',10);
3883     --
3884     if l_lf_evt_ocrd_dt is null then
3885     --
3886        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3887     --
3888     end if;
3889     --
3890     p_returned_date := last_day(add_months(l_lf_evt_ocrd_dt,1));
3891     --
3892   --
3893   -- AEOT - As of End of Time
3894   --
3895   elsif p_date_cd = 'AEOT' then
3896   --
3897   --  hr_utility.set_location('Entering AEOT',10);
3898     --
3899     p_returned_date := to_date('31-12-4712','DD-MM-YYYY');
3900   --
3901   -- A30DFPSD - As of 30 days from the participation start date
3902   --
3903   elsif p_date_cd = 'A30DFPSD' then
3904   --
3905   --  hr_utility.set_location('Entering A30DFPSD',10);
3906     --
3907     p_returned_date := p_start_date+30;
3908   --
3909   -- A45DFPSD - As of 45 days from the participation start date
3910   --
3911   elsif p_date_cd = 'A45DFPSD' then
3912   --
3913   --  hr_utility.set_location('Entering A45DFPSD',10);
3914     --
3915     p_returned_date := p_start_date+45;
3916   --
3917   -- A60DFPSD - As of 60 days from the participation start date
3918   --
3919   elsif p_date_cd = 'A60DFPSD' then
3920   --
3921   --  hr_utility.set_location('Entering A60DFPSD',10);
3922     --
3923     p_returned_date := p_start_date+60;
3924   --
3925   -- A12MFPSD - As of 12 months from the participation start date
3926   --
3927   elsif p_date_cd = 'A12MFPSD' then
3928   --
3929   --  hr_utility.set_location('Entering A12MFPSD',10);
3930     --
3931     p_returned_date := add_months(p_start_date,12);
3932   --
3933   -- A18MFPSD - As of 18 months from the participation start date
3934   --
3935   elsif p_date_cd = 'A18MFPSD' then
3936   --
3937   --  hr_utility.set_location('Entering A18MFPSD',10);
3938     --
3939     p_returned_date := add_months(p_start_date,18);
3940   --
3941   -- A1MFPSD - As of 1 months from the participation start date
3942   --
3943   elsif p_date_cd = 'A1MFPSD' then
3944   --
3945   --  hr_utility.set_location('Entering A1MFPSD',10);
3946     --
3947     p_returned_date := add_months(p_start_date,1);
3948   --
3949   -- A29MFPSD - As of 29 months from the participation start date
3950   --
3951   elsif p_date_cd = 'A29MFPSD' then
3952   --
3953   --  hr_utility.set_location('Entering A29MFPSD',10);
3954     --
3955     p_returned_date := add_months(p_start_date,29);
3956   --
3957   -- A36MFPSD - As of 36 months from the participation start date
3958   --
3959   elsif p_date_cd = 'A36MFPSD' then
3960   --
3961   --  hr_utility.set_location('Entering A36MFPSD',10);
3962     --
3963     p_returned_date := add_months(p_start_date,36);
3964   --
3965   -- As of First Day of Following Month
3966   --
3967   elsif p_date_cd = 'AFDFM' then
3968   --
3969   --  hr_utility.set_location('Entering AFDFM',10);
3970     --
3971     if l_lf_evt_ocrd_dt is null then
3972     --
3973        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3974     --
3975     else
3976     --
3977        l_event_date:= l_lf_evt_ocrd_dt;
3978     --
3979     end if;
3980     --
3981     p_returned_date := last_day(l_event_date)+1;
3982   --
3983   -- As of First Day of Following Month after 15 days
3984   --
3985   elsif p_date_cd = 'AFDFM15' then
3986   --
3987   --  hr_utility.set_location('Entering AFDFM15',10);
3988     --
3989     if l_lf_evt_ocrd_dt is null then
3990     --
3991        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
3992     --
3993     else
3994     --
3995        l_event_date:= l_lf_evt_ocrd_dt;
3996     --
3997     end if;
3998     --
3999     p_returned_date := last_day(l_event_date+15)+1;
4000   --
4001   -- ALDCPPY - Last day of current program or plan year
4002   --
4003   elsif p_date_cd  = 'ALDCPPY' then
4004     --
4005   --  hr_utility.set_location('Entering ALDCPPY',10);
4006     --
4007     if l_lf_evt_ocrd_dt is null then
4008     --
4009        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4010     --
4011     end if;
4012     --
4013     get_plan_year
4014            (p_effective_date => p_effective_date
4015            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
4016            ,p_pl_id          => l_pl_id
4017            ,p_pgm_id         => l_pgm_id
4018            ,p_oipl_id        => l_oipl_id
4019            ,p_date_cd        => p_date_cd
4020            ,p_comp_obj_mode  => p_comp_obj_mode
4021            ,p_start_date     => l_start_date
4022            ,p_end_date       => l_end_date) ;
4023     --
4024     p_returned_date :=l_end_date;
4025   --
4026 
4027   -- WALDCPPY - 1 Prior or Year End
4028   --
4029   elsif  p_date_cd = 'WALDCPPY' then
4030     --
4031     if g_debug then
4032       hr_utility.set_location('Entering ALDCPPY',10);
4033     end if;
4034     --
4035       --
4036       if l_lf_evt_ocrd_dt is null then
4037       --
4038         l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4039       --
4040       end if;
4041       --
4042       get_plan_year
4043            (p_effective_date => p_effective_date
4044            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
4045            ,p_pl_id          => l_pl_id
4046            ,p_pgm_id         => l_pgm_id
4047            ,p_oipl_id        => l_oipl_id
4048            ,p_date_cd        => p_date_cd
4049            ,p_comp_obj_mode  => p_comp_obj_mode
4050            ,p_start_date     => l_start_date
4051            ,p_end_date       => l_end_date) ;
4052       --
4053       p_returned_date :=l_end_date;
4054       --
4055   --
4056   -- FDLMPPY - First Day Last Month of current Program or Pl Year
4057   --
4058   elsif p_date_cd  = 'FDLMPPY' then
4059     --
4060   --  hr_utility.set_location('Entering FDLMPPY',10);
4061     --
4062     if l_lf_evt_ocrd_dt is null then
4063     --
4064        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4065     --
4066     end if;
4067     --
4068     get_plan_year
4069            (p_effective_date => p_effective_date
4070            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
4071            ,p_pl_id          => l_pl_id
4072            ,p_pgm_id         => l_pgm_id
4073            ,p_oipl_id        => l_oipl_id
4074            ,p_date_cd        => p_date_cd
4075            ,p_comp_obj_mode  => p_comp_obj_mode
4076            ,p_start_date     => l_start_date
4077            ,p_end_date       => l_end_date) ;
4078     --
4079     l_date := add_months(l_end_date,-1);
4080     p_returned_date := last_day(l_date)+1;
4081   --
4082   -- AFDCPPY - First day of current program or plan year
4083   --
4084   elsif p_date_cd  = 'AFDCPPY' then
4085     --
4086   --  hr_utility.set_location('Entering AFDCPPY',10);
4087     --
4088     if l_lf_evt_ocrd_dt is null then
4089     --
4090        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4091     --
4092     end if;
4093     --
4094     get_plan_year
4095            (p_effective_date => p_effective_date
4096            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
4097            ,p_pl_id          => l_pl_id
4098            ,p_pgm_id         => l_pgm_id
4099            ,p_oipl_id        => l_oipl_id
4100            ,p_date_cd        => p_date_cd
4101            ,p_comp_obj_mode  => p_comp_obj_mode
4102            ,p_start_date     => l_start_date
4103            ,p_end_date       => l_end_date) ;
4104     --
4105     p_returned_date :=l_start_date;
4106 
4107   --
4108   -- AFDFPPY - First day of following program or plan year
4109   --
4110   elsif p_date_cd = 'AFDFPPY' then
4111     --
4112   --  hr_utility.set_location('Entering AFDFPPY',10);
4113     --
4114     if l_lf_evt_ocrd_dt is null then
4115     --
4116        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4117     --
4118     --
4119     end if;
4120     --
4121     get_next_plan_year
4122                  (p_effective_date => p_effective_date
4123                  ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
4124                  ,p_pl_id          => l_pl_id
4125                  ,p_pgm_id         => l_pgm_id
4126                  ,p_oipl_id        => l_oipl_id
4127                  ,p_date_cd        => p_date_cd
4128                  ,p_comp_obj_mode  => p_comp_obj_mode
4129                  ,p_start_date     => l_next_popl_yr_strt
4130                  ,p_end_date       => l_next_popl_yr_end) ;
4131     --
4132     p_returned_date :=l_next_popl_yr_strt;
4133   --
4134   -- AFDCPP - First day of current pay period
4135   --
4136   elsif p_date_cd = 'AFDCPP' then
4137     --
4138   --  hr_utility.set_location('Entering AFDCPP',10);
4139     --
4140     if l_lf_evt_ocrd_dt is null then
4141     --
4142        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4143     --
4144     end if;
4145     --
4146     open c_pay_period;
4147       fetch c_pay_period into l_pay_period;
4148     --
4149     if c_pay_period%notfound then
4150     --
4151       close c_pay_period;
4152     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',110);
4153       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
4154       fnd_message.set_token('DATE_CODE',p_date_cd);
4155       fnd_message.set_token('L_PROC',l_proc);
4156       fnd_message.set_token('PERSON_ID',l_person_id);
4157       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4158       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4159       fnd_message.raise_error;
4160     --
4161     end if;
4162     --
4163     p_returned_date := l_pay_period.start_date;
4164     --
4165     close c_pay_period ;
4166   --
4167   -- ALDCPP - Last day of current pay period
4168   --
4169   elsif p_date_cd = 'ALDCPP' then
4170     --
4171     if g_debug then
4172       hr_utility.set_location('Entering ALDCPP',10);
4173     end if;
4174     --
4175     if l_lf_evt_ocrd_dt is null then
4176     --
4177        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4178     --
4179     end if;
4180     --
4181     open c_pay_period;
4182     fetch c_pay_period into l_pay_period;
4183     --
4184     if c_pay_period%notfound then
4185       close c_pay_period;
4186     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',115);
4187       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
4188       fnd_message.set_token('DATE_CODE',p_date_cd);
4189       fnd_message.set_token('L_PROC',l_proc);
4190       fnd_message.set_token('PERSON_ID',l_person_id);
4191       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4192       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4193       fnd_message.raise_error;
4194     end if;
4195     p_returned_date := l_pay_period.end_date;
4196     close c_pay_period ;
4197   --
4198   -- WALDCPP - 1 Prior or pay period end
4199   --
4200   elsif p_date_cd = 'WALDCPP' then
4201     --
4202     if g_debug then
4203       hr_utility.set_location('Entering WALDCPP',10);
4204     end if;
4205     --
4206       --
4207       if l_lf_evt_ocrd_dt is null then
4208       --
4209         l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4210       --
4211       end if;
4212       --
4213       open c_pay_period;
4214       fetch c_pay_period into l_pay_period;
4215       --
4216       if c_pay_period%notfound then
4217       close c_pay_period;
4218         if g_debug then
4219           hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',115);
4220         end if;
4221         fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
4222         fnd_message.set_token('DATE_CODE',p_date_cd);
4223         fnd_message.set_token('L_PROC',l_proc);
4224         fnd_message.set_token('PERSON_ID',l_person_id);
4225         fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4226         fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4227         fnd_message.raise_error;
4228       end if;
4229       --
4230       p_returned_date := l_pay_period.end_date;
4231       close c_pay_period ;
4232     --
4233   --
4234   -- AFDFPP - First day of following pay period
4235   --
4236   elsif p_date_cd = 'AFDFPP' then
4237     --
4238   --  hr_utility.set_location('Entering AFDFPP',10);
4239     --
4240     if l_lf_evt_ocrd_dt is null then
4241     --
4242        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4243     --
4244     end if;
4245     --
4246     open c_next_pay_period(nvl(l_lf_evt_ocrd_dt,p_effective_date));
4247     fetch c_next_pay_period into l_next_pay_period;
4248     --
4249     if c_next_pay_period%notfound then
4250       close c_next_pay_period;
4251     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',120);
4252       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
4253       fnd_message.set_token('DATE_CODE',p_date_cd);
4254       fnd_message.set_token('PERSON_ID',l_person_id);
4255       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4256       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4257       fnd_message.raise_error;
4258     end if;
4259     close c_next_pay_period;
4260 
4261     p_returned_date := l_next_pay_period.start_date;
4262   --
4263   -- APOCT1 - Prior October 1st
4264   --
4265   elsif p_date_cd = 'APOCT1' then
4266     -- tilak
4267     --
4268     --  hr_utility.set_location('Entering APOCT1',10);
4269     --
4270     if l_lf_evt_ocrd_dt is null then
4271     --
4272        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4273     --
4274     end if;
4275     --
4276     --- whne the le_evt_ocrd_dt is oct 1st it should return previos year october first
4277     --- so when the month is calculated the date is dedcited with 1
4278     l_date   :=  l_lf_evt_ocrd_dt-1 ;
4279     l_months := to_number(to_char(l_date,'MM'))+3;
4280     --
4281     -- subtract 12 if it's oct nov or dec so we dont go farther than 1 yr back.
4282     --
4283     if l_months > 12 then
4284       l_months := l_months - 12;
4285     end if;
4286     --
4287     l_date := add_months(l_date,-l_months);
4288     --
4289     -- Set to first of month of October
4290     --
4291     p_returned_date := last_day(l_date)+1;
4292     if g_debug then
4293       hr_utility.set_location(' APOCT1 date : ' || p_returned_date , 450) ;
4294     end if;
4295   -- tilak
4296 
4297   -- OMFED - One Month Afl_event_date p_date_cd  = 'OMFED' then
4298   --
4299   elsif p_date_cd = 'OMFED' then
4300     --
4301   --  hr_utility.set_location('Entering OMFED',10);
4302     --
4303     if l_lf_evt_ocrd_dt is null then
4304     --
4305        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4306     --
4307     else
4308     --
4309        l_event_date:= l_lf_evt_ocrd_dt;
4310     --
4311     end if;
4312     --
4313     p_returned_date := add_months(l_event_date,1);
4314   --
4315   -- TMFED - Two Months After Event Date
4316   --
4317   elsif p_date_cd  = 'TMFED' then
4318     --
4319   --  hr_utility.set_location('Entering TMFED',10);
4320     --
4321     if l_lf_evt_ocrd_dt is null then
4322     --
4323        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4324     --
4325     else
4326     --
4327        l_event_date:= l_lf_evt_ocrd_dt;
4328     --
4329     end if;
4330     --
4331     p_returned_date := add_months(l_event_date,2);
4332   --
4333   -- 30DFLED - Thirty days from Life Event Date
4334   --
4335   elsif p_date_cd = '30DFLED' then
4336     --
4337   --  hr_utility.set_location('Entering 30DFLED',10);
4338     --
4339     if l_lf_evt_ocrd_dt is null then
4340     --
4341        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4342     --
4343     else
4344     --
4345        l_event_date:= l_lf_evt_ocrd_dt;
4346     --
4347     end if;
4348     --
4349     p_returned_date := l_event_date + 30;
4350   --
4351   -- TDBED - Thirty days before event date
4352   --
4353   elsif p_date_cd = 'TDBED' then
4354     --
4355   --  hr_utility.set_location('Entering TDBED',10);
4356     --
4357     if l_lf_evt_ocrd_dt is null then
4358     --
4359        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4360     --
4361     else
4362     --
4363        l_event_date:= l_lf_evt_ocrd_dt;
4364     --
4365     end if;
4366     --
4367     p_returned_date := l_event_date - 30;
4368   --
4369   -- LTDBED - Later: Thirty days before event date or notified
4370   --
4371   elsif p_date_cd = 'LTDBED' then
4372     --
4373   --  hr_utility.set_location('Entering LTDBED',10);
4374     --
4375     if l_lf_evt_ocrd_dt is null then
4376     --
4377        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4378     --
4379     else
4380     --
4381        l_event_date:= l_lf_evt_ocrd_dt;
4382     --
4383     end if;
4384     --
4385     l_recorded_date := get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4386     --
4387     -- RCHASE Bug Fix, do not subtract 30 days from both.  Evaluate 30 days minus event date
4388 --    if l_recorded_date > l_event_date then
4389     --
4390 --       p_returned_date := l_recorded_date - 30;
4391     --
4392 --    else
4393     --
4394 --       p_returned_date := l_event_date - 30;
4395     --
4396 --    end if;
4397     if l_recorded_date > l_event_date-30 then
4398     --
4399        p_returned_date := l_recorded_date;
4400     --
4401     else
4402     --
4403        p_returned_date := l_event_date - 30;
4404     --
4405     end if;
4406   --
4407   -- SDFED - Sixty days After Event Date
4408   --
4409   elsif p_date_cd = 'SDFED' then
4410     --
4411   --  hr_utility.set_location('Entering SDFED',10);
4412     --
4413     if l_lf_evt_ocrd_dt is null then
4414     --
4415        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4416     --
4417     else
4418     --
4419        l_event_date:= l_lf_evt_ocrd_dt;
4420     --
4421     end if;
4422     --
4423     p_returned_date := l_event_date + 60;
4424     --
4425   --
4426   --
4427   -- LSDBED - Later: Sixty days before Event Date
4428   --
4429   elsif p_date_cd = 'LSDBED' then
4430     --
4431   --  hr_utility.set_location('Entering LSDBED',10);
4432     --
4433     if l_lf_evt_ocrd_dt is null then
4434     --
4435        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4436     --
4437     else
4438     --
4439        l_event_date:= l_lf_evt_ocrd_dt;
4440     --
4441     end if;
4442     --
4443     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4444 
4445     -- RCHASE Bug Fix, do not subtract 30 days from both.  Evaluate 30 days minus event date
4446     --If l_recorded_date > l_event_date then
4447     --
4448     --   p_returned_date := l_recorded_date - 60;
4449     --
4450     --else
4451     --
4452     --   p_returned_date := l_event_date - 60;
4453     --
4454     --end if;
4455     If l_recorded_date > l_event_date-60 then
4456     --
4457        p_returned_date := l_recorded_date;
4458     --
4459     else
4460     --
4461        p_returned_date := l_event_date - 60;
4462     --
4463     end if;
4464   --
4465   -- TODFED - Thirty-one days After Event Date
4466   --
4467   elsif p_date_cd  = 'TODFED' then
4468     --
4469   --  hr_utility.set_location('Entering TODFED',10);
4470     --
4471     if l_lf_evt_ocrd_dt is null then
4472     --
4473        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4474     --
4475     else
4476     --
4477        l_event_date:= l_lf_evt_ocrd_dt;
4478     --
4479     end if;
4480     --
4481     p_returned_date := l_event_date + 31;
4482   --
4483   -- LEPPPED - Later of Enrollment Period End Date and Processing End Date.
4484   --
4485   elsif p_date_cd = 'LEPPPED' then
4486     --
4487   --  hr_utility.set_location('Entering LEPPPED',10);
4488     --
4489     open c_pil_popl;
4490     fetch c_pil_popl into l_enrt_end_dt, l_procg_end_dt;
4491     --
4492     if c_pil_popl%notfound then
4493       close c_pil_popl;
4494     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',81);
4495       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4496       fnd_message.set_token('DATE_CODE',p_date_cd);
4497       fnd_message.set_token('L_PROC',l_proc);
4498       fnd_message.raise_error;
4499     end if;
4500     --
4501     close c_pil_popl;
4502     --
4503     if nvl(l_procg_end_dt,hr_api.g_sot) > nvl(l_enrt_end_dt,hr_api.g_sot) then
4504       l_enrt_end_dt := l_procg_end_dt;
4505     end if;
4506     --
4507     p_returned_date := l_enrt_end_dt;
4508   --
4509   elsif p_date_cd  = '5DBEEPD' then
4510     --
4511     -- 5DBEEPD - 5 Days before the end of the enrollment period end date
4512     --
4513   --  hr_utility.set_location('Entering 5DBEEPD',10);
4514     --
4515     open c_cm_enrt_perd_end_dt;
4516     fetch c_cm_enrt_perd_end_dt into l_cm_date;
4517     if c_cm_enrt_perd_end_dt%notfound then
4518       close c_cm_enrt_perd_end_dt;
4519     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',86);
4520       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4521       fnd_message.set_token('DATE_CODE',p_date_cd);
4522       fnd_message.set_token('L_PROC',l_proc);
4523       fnd_message.raise_error;
4524     end if;
4525     --
4526     close c_cm_enrt_perd_end_dt;
4527     --
4528     p_returned_date := l_cm_date - 5;
4529     --
4530 
4531   elsif p_date_cd  = '10DBEEPD' then
4532     --
4533     -- 10DBEEPD - 10 Days before the end of the enrollment period end date
4534     --
4535   --  hr_utility.set_location('Entering 10DBEEPD',10);
4536     --
4537     open c_cm_enrt_perd_end_dt;
4538     fetch c_cm_enrt_perd_end_dt into l_cm_date;
4539     if c_cm_enrt_perd_end_dt%notfound then
4540       close c_cm_enrt_perd_end_dt;
4541     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',87);
4542       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4543       fnd_message.set_token('DATE_CODE',p_date_cd);
4544       fnd_message.set_token('L_PROC',l_proc);
4545       fnd_message.raise_error;
4546     end if;
4547     --
4548     close c_cm_enrt_perd_end_dt;
4549     --
4550     p_returned_date := l_cm_date - 10;
4551     --
4552   elsif p_date_cd  = '20DBEEPD' then
4553     --
4554     -- 20DBEEPD - 20 Days before the end of the enrollment period end date
4555     --
4556   --  hr_utility.set_location('Entering 20DBEEPD',10);
4557     --
4558     open c_cm_enrt_perd_end_dt;
4559     fetch c_cm_enrt_perd_end_dt into l_cm_date;
4560     if c_cm_enrt_perd_end_dt%notfound then
4561       close c_cm_enrt_perd_end_dt;
4562     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',89);
4563       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4564       fnd_message.set_token('DATE_CODE',p_date_cd);
4565       fnd_message.set_token('L_PROC',l_proc);
4566       fnd_message.raise_error;
4567     end if;
4568     --
4569     close c_cm_enrt_perd_end_dt;
4570     --
4571     p_returned_date := l_cm_date - 20;
4572     --
4573   elsif p_date_cd  = '25DBEEPD' then
4574     --
4575     -- 25DBEEPD - 25 Days before the end of the enrollment period end date
4576     --
4577   --  hr_utility.set_location('Entering 25DBEEPD',10);
4578     --
4579     open c_cm_enrt_perd_end_dt;
4580     fetch c_cm_enrt_perd_end_dt into l_cm_date;
4581     if c_cm_enrt_perd_end_dt%notfound then
4582       close c_cm_enrt_perd_end_dt;
4583     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',90);
4584       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4585       fnd_message.set_token('DATE_CODE',p_date_cd);
4586       fnd_message.set_token('L_PROC',l_proc);
4587       fnd_message.raise_error;
4588     end if;
4589     --
4590     close c_cm_enrt_perd_end_dt;
4591     --
4592     p_returned_date := l_cm_date - 25;
4593     --
4594   elsif p_date_cd  = '30DBEEPD' then
4595     --
4596     -- 30DBEEPD - 30 Days before the end of the enrollment period end date
4597     --
4598   --  hr_utility.set_location('Entering 30DBEEPD',10);
4599     --
4600     open c_cm_enrt_perd_end_dt;
4601     fetch c_cm_enrt_perd_end_dt into l_cm_date;
4602     if c_cm_enrt_perd_end_dt%notfound then
4603       close c_cm_enrt_perd_end_dt;
4604     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',91);
4605       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4606       fnd_message.set_token('DATE_CODE',p_date_cd);
4607       fnd_message.set_token('L_PROC',l_proc);
4608       fnd_message.raise_error;
4609     end if;
4610     --
4611     close c_cm_enrt_perd_end_dt;
4612     --
4613     p_returned_date := l_cm_date - 30;
4614     --
4615 
4616   elsif p_date_cd  = '10DBDAD' then
4617     --
4618     -- 10DBDAD - 10 Days before the default applied date
4619     --
4620   --  hr_utility.set_location('Entering 10DBDAD',10);
4621     --
4622     open c_cm_dflt_asnd_dt;
4623     fetch c_cm_dflt_asnd_dt into l_cm_date;
4624     if c_cm_dflt_asnd_dt%notfound then
4625       close c_cm_dflt_asnd_dt;
4626     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',92);
4627       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4628       fnd_message.set_token('DATE_CODE',p_date_cd);
4629       fnd_message.set_token('L_PROC',l_proc);
4630       fnd_message.raise_error;
4631     end if;
4632     --
4633     close c_cm_dflt_asnd_dt;
4634     --
4635     p_returned_date := l_cm_date - 10;
4636     --
4637   elsif p_date_cd  = '14DBEPD' then
4638     --
4639     -- 14DBEPD - 14 Days before the Eligible to Participate Date
4640     --
4641   --  hr_utility.set_location('Entering 14DBEPD',10);
4642     --
4643     open c_cm_elig_prtn_strt_dt;
4644     fetch c_cm_elig_prtn_strt_dt into l_cm_date;
4645     if c_cm_elig_prtn_strt_dt%notfound then
4646       close c_cm_elig_prtn_strt_dt;
4647     --  hr_utility.set_location('BEN_91386_FIRST_INELIG',81);
4648       fnd_message.set_name('BEN', 'BEN_91386_FIRST_INELIG');
4649       fnd_message.set_token('DATE_CODE',p_date_cd);
4650       fnd_message.set_token('PROC',l_proc);
4651       fnd_message.set_token('PERSON_ID',to_char(p_person_id));
4652       fnd_message.set_token('BG_ID',to_char(p_business_group_id));
4653       fnd_message.set_token('EFFECTIVE_DATE',to_char(p_effective_date));
4654       fnd_message.raise_error;
4655     end if;
4656     close c_cm_elig_prtn_strt_dt;
4657     --
4658     p_returned_date := l_cm_date - 14;
4659     --
4660   elsif p_date_cd  = '14DBIPD' then
4661     --
4662     -- 14DBIPD - 14 Days before the Ineligible to Participate Date
4663     --
4664     open c_cm_elig_prtn_end_dt;
4665     fetch c_cm_elig_prtn_end_dt into l_cm_date;
4666     if c_cm_elig_prtn_end_dt%notfound then
4667       close c_cm_elig_prtn_end_dt;
4668     --  hr_utility.set_location('BEN_91386_FIRST_INELIG',93);
4669       fnd_message.set_name('BEN', 'BEN_91386_FIRST_INELIG');
4670       fnd_message.set_token('DATE_CODE',p_date_cd);
4671       fnd_message.set_token('PROC',l_proc);
4672       fnd_message.set_token('PERSON_ID',to_char(p_person_id));
4673       fnd_message.set_token('BG_ID',to_char(p_business_group_id));
4674       fnd_message.set_token('EFFECTIVE_DATE',to_char(p_effective_date));
4675       fnd_message.raise_error;
4676     end if;
4677     close c_cm_elig_prtn_end_dt;
4678     --
4679     p_returned_date := l_cm_date  - 14;
4680   --
4681   elsif p_date_cd  = '15DBEEPD' then
4682     --
4683     -- 15DBEEPD - 15 Days before the end of the enrollment period end date
4684     --
4685   --  hr_utility.set_location('Entering 15DBEEPD',10);
4686     --
4687     open c_cm_enrt_perd_end_dt;
4688     fetch c_cm_enrt_perd_end_dt into l_cm_date;
4689     if c_cm_enrt_perd_end_dt%notfound then
4690       close c_cm_enrt_perd_end_dt;
4691     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',88);
4692       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4693       fnd_message.set_token('DATE_CODE',p_date_cd);
4694       fnd_message.set_token('L_PROC',l_proc);
4695       fnd_message.raise_error;
4696     end if;
4697     --
4698     close c_cm_enrt_perd_end_dt;
4699     --
4700     p_returned_date := l_cm_date - 15;
4701   elsif p_date_cd  = 'OAED' then
4702     --
4703     -- OAED - On the Automatic Enrollment Date.
4704     --
4705   --  hr_utility.set_location('Entering OAED',10);
4706     --
4707     open c_cm_auto_asnd_dt;
4708     fetch c_cm_auto_asnd_dt into l_cm_date;
4709     if c_cm_auto_asnd_dt%notfound then
4710       close c_cm_auto_asnd_dt;
4711     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',98);
4712       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
4713       fnd_message.set_token('DATE_CODE',p_date_cd);
4714       fnd_message.set_token('L_PROC',l_proc);
4715       fnd_message.raise_error;
4716     end if;
4717     close c_cm_auto_asnd_dt;
4718     --
4719     p_returned_date := l_cm_date;
4720     --
4721 
4722   elsif p_date_cd  = 'OCSD' or p_date_cd = 'PECSD' then
4723     --
4724     -- OCSD - On the Coverage Start Date.
4725     --
4726   --  hr_utility.set_location('Entering OCSD',10);
4727     --
4728     open c_prtt_enrt_rslt_dts;
4729     fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
4730                                     l_enrt_cvg_strt_dt,
4731                                     l_enrt_eff_strt_date;
4732     if c_prtt_enrt_rslt_dts%notfound then
4733         --
4734         l_enrt_cvg_strt_dt := p_start_date ;
4735         --
4736         if g_debug then
4737           hr_utility.set_location('OCSD Not found ', 19);
4738         end if;
4739         --
4740     end if;
4741     --
4742     close c_prtt_enrt_rslt_dts;
4743     --
4744     p_returned_date := l_enrt_cvg_strt_dt;
4745     --
4746   --
4747   -- LFDMCF - First of Month on or After Later of Event or Notified
4748   --
4749   elsif p_date_cd  = 'LFDMCF' then
4750     --
4751   --  hr_utility.set_location('Entering LFDMCF',10);
4752     --
4753     if l_lf_evt_ocrd_dt is null then
4754     --
4755        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4756     --
4757     end if;
4758     --
4759     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4760     --
4761     if l_lf_evt_ocrd_dt > l_recorded_date then
4762     --
4763        if l_lf_evt_ocrd_dt <> last_day(add_months(l_lf_evt_ocrd_dt,-1))+1 then
4764        --
4765          p_returned_date := last_day(l_lf_evt_ocrd_dt)+1;
4766        --
4767        else
4768        --
4769          p_returned_date := l_lf_evt_ocrd_dt;
4770        --
4771        end if;
4772     --
4773     else
4774     --
4775        if l_recorded_date <> last_day(add_months(l_recorded_date,-1))+1 then
4776        --
4777          p_returned_date := last_day(l_recorded_date)+1;
4778        --
4779        else
4780        --
4781          p_returned_date := l_recorded_date;
4782        --
4783        end if;
4784     --
4785     end if;
4786     --
4787   --
4788   -- LFDPPCF - First of Pay Period on or After Later of Event or Notified
4789   --
4790   elsif p_date_cd  = 'LFDPPCF' then
4791     --
4792   --  hr_utility.set_location('Entering LFDPPCF',10);
4793     --
4794     if l_lf_evt_ocrd_dt is null then
4795     --
4796        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4797     --
4798     end if;
4799     --
4800     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4801     --
4802 
4803     if l_lf_evt_ocrd_dt > l_recorded_date  then
4804 
4805        open c_pay_period_for_date(l_lf_evt_ocrd_dt);
4806        fetch c_pay_period_for_date into
4807           l_start_date,
4808           l_end_date;
4809        --
4810        if c_pay_period_for_date%notfound then
4811        --
4812          close c_pay_period_for_date;
4813        --  hr_utility.set_location('BEN_92380_CANNOT_CALC_LFDPPCF',96);
4814        --  hr_utility.set_location('l_lf_evt_ocrd_dt'||to_char(l_lf_evt_ocrd_dt),96);
4815          fnd_message.set_name('BEN','BEN_92380_CANNOT_CALC_LFDPPCF');
4816          fnd_message.set_token('L_PROC',l_proc);
4817          fnd_message.set_token('PERSON_ID',l_person_id);
4818          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4819          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4820          fnd_message.set_token('LF_EVT_OCRD_DT',l_lf_evt_ocrd_dt);
4821          fnd_message.raise_error;
4822        --
4823        end if;
4824        --
4825        close c_pay_period_for_date;
4826        --
4827        if l_lf_evt_ocrd_dt =  l_start_date then
4828        --
4829           p_returned_date := l_start_date;
4830        --
4831        else
4832        --
4833           open c_next_pay_period(nvl(l_lf_evt_ocrd_dt,p_effective_date));
4834           fetch c_next_pay_period into l_next_pay_period;
4835           --
4836           if c_next_pay_period%notfound then
4837             close c_next_pay_period;
4838           --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',125);
4839             fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
4840             fnd_message.set_token('DATE_CODE',p_date_cd);
4841             fnd_message.set_token('PERSON_ID',l_person_id);
4842             fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4843             fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4844             fnd_message.raise_error;
4845           end if;
4846           --
4847           p_returned_date := l_next_pay_period.start_date;
4848           --
4849           close c_next_pay_period;
4850        --
4851        end if;
4852     --
4853     else
4854     --
4855        open c_pay_period_for_date(l_recorded_date);
4856        fetch c_pay_period_for_date into
4857           l_start_date,
4858           l_end_date;
4859        --
4860        if c_pay_period_for_date%notfound then
4861        --
4862          close c_pay_period_for_date;
4863        --  hr_utility.set_location('BEN_92380_CANNOT_CALC_LFDPPCF',97);
4864        --  hr_utility.set_location('l_recorded_date'||to_char(l_recorded_date),97);
4865          fnd_message.set_name('BEN','BEN_92380_CANNOT_CALC_LFDPPCF');
4866          fnd_message.set_token('L_PROC',l_proc);
4867          fnd_message.set_token('PERSON_ID',l_person_id);
4868          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4869          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4870          fnd_message.set_token('LF_EVT_OCRD_DT',l_lf_evt_ocrd_dt);
4871          fnd_message.raise_error;
4872        --
4873        end if;
4874        --
4875        close c_pay_period_for_date;
4876        --
4877        if l_recorded_date =  l_start_date then
4878        --
4879           p_returned_date := l_start_date;
4880        --
4881        else
4882        --
4883           l_lf_evt_ocrd_dt := l_recorded_date;
4884           --
4885           open c_next_pay_period(nvl(l_lf_evt_ocrd_dt,p_effective_date));
4886           fetch c_next_pay_period into l_next_pay_period;
4887           --
4888           if c_next_pay_period%notfound then
4889             close c_next_pay_period;
4890           --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',130);
4891             fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
4892             fnd_message.set_token('DATE_CODE',p_date_cd);
4893             fnd_message.set_token('PERSON_ID',l_person_id);
4894             fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
4895             fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
4896             fnd_message.raise_error;
4897           end if;
4898           --
4899           p_returned_date := l_next_pay_period.start_date;
4900           --
4901           close c_next_pay_period;
4902        --
4903        end if;
4904     --
4905     end if;
4906   --
4907   -- LELD - Latest of Elections, Event or Notified
4908   --
4909   elsif p_date_cd  = 'LELD' then
4910     --
4911   --  hr_utility.set_location('Entering LELD',10);
4912     --
4913     if l_lf_evt_ocrd_dt is null then
4914     --
4915        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4916     --
4917     end if;
4918     --
4919     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4920     --
4921     open c_prtt_enrt_rslt_dts;
4922     fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
4923                                     l_enrt_cvg_strt_dt,
4924                                     l_enrt_eff_strt_date;
4925     --
4926     if l_enrt_eff_strt_date is null then
4927       l_enrt_eff_strt_date:=p_effective_date;
4928     end if;
4929     --
4930     l_cm_date:=l_enrt_eff_strt_date;
4931     --
4932     close c_prtt_enrt_rslt_dts;
4933 
4934     if l_lf_evt_ocrd_dt > l_recorded_date  then
4935     --
4936        if l_lf_evt_ocrd_dt > l_cm_date then
4937        --
4938          p_returned_date := l_lf_evt_ocrd_dt;
4939        --
4940        else
4941          -- also for null condition
4942          p_returned_date:=l_cm_date;
4943        end if;
4944     --
4945     elsif l_recorded_date > l_cm_date then
4946     --
4947        p_returned_date := l_recorded_date;
4948     --
4949     else
4950     --
4951        p_returned_date := l_cm_date;
4952     --
4953     end if;
4954     --
4955   --
4956   -- LELDED - Later of Elections or Event
4957   --
4958   elsif p_date_cd  = 'LELDED' then
4959     --
4960   --  hr_utility.set_location('Entering LELDED',10);
4961     --
4962     if l_lf_evt_ocrd_dt is null then
4963     --
4964        l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
4965     --
4966     end if;
4967     --
4968     open c_prtt_enrt_rslt_dts;
4969     fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
4970                                     l_enrt_cvg_strt_dt,
4971                                     l_enrt_eff_strt_date;
4972     --
4973     if l_enrt_eff_strt_date is null then
4974       l_enrt_eff_strt_date:=p_effective_date;
4975     end if;
4976     --
4977     l_cm_date:=l_enrt_eff_strt_date;
4978     --
4979     close c_prtt_enrt_rslt_dts;
4980     --
4981     if l_lf_evt_ocrd_dt > l_cm_date then
4982     --
4983       p_returned_date := l_lf_evt_ocrd_dt;
4984     --
4985     else
4986     --
4987       p_returned_date := l_cm_date;
4988     --
4989     end if;
4990     --
4991   --
4992   -- ODEWM - On the Day Elections Were Made.
4993   --
4994   elsif p_date_cd  = 'ODEWM' then
4995     --
4996     -- should return null if result with current pil
4997     -- is not found, i.e. in benmngle.  gets date at
4998     -- time of enrollment.
4999     --
5000   --  hr_utility.set_location('Entering ODEWM',10);
5001     --
5002     open c_prtt_enrt_rslt_dts;
5003     fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
5004                                     l_enrt_cvg_strt_dt,
5005                                     l_enrt_eff_strt_date;
5006     --
5007     close c_prtt_enrt_rslt_dts;
5008     --
5009     if l_enrt_eff_strt_date is null then
5010       l_enrt_eff_strt_date:=p_effective_date;
5011     end if;
5012     --
5013     p_returned_date := l_enrt_eff_strt_date;
5014     --
5015     --
5016     -- AFDELD First of Month After Elections Made
5017     --
5018     elsif p_date_cd  = 'AFDELD' then
5019     --
5020       -- should return null if result with current pil
5021       -- is not found, i.e. in benmngle.  gets date at
5022       -- time of enrollment.
5023       --
5024       hr_utility.set_location('Entering AFDELD',10);
5025     --
5026     open c_prtt_enrt_rslt_dts;
5027     fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
5028                                     l_enrt_cvg_strt_dt,
5029                                     l_enrt_eff_strt_date;
5030     --
5031     close c_prtt_enrt_rslt_dts;
5032     hr_utility.set_location('Entering AFDELD ' || l_enrt_eff_strt_date ,10);
5033     --
5034     -- when the first time cvg calcualted there may not be a result
5035     if l_enrt_eff_strt_date is null then
5036        l_enrt_eff_strt_date :=p_effective_date;
5037     end if;
5038     p_returned_date := last_day(l_enrt_eff_strt_date) + 1 ;
5039 
5040     hr_utility.set_location('AFDELD ' ||  p_returned_date ,10);
5041     --
5042     --
5043     -- FDMELD First of Month on or After Elections Made
5044     elsif p_date_cd  = 'FDMELD' then
5045     --
5046       -- should return null if result with current pil
5047       -- is not found, i.e. in benmngle.  gets date at
5048       -- time of enrollment.
5049       --
5050       hr_utility.set_location('Entering FDMELD',10);
5051     --
5052     open c_prtt_enrt_rslt_dts;
5053     fetch c_prtt_enrt_rslt_dts into l_enrt_cvg_end_dt,
5054                                     l_enrt_cvg_strt_dt,
5055                                     l_enrt_eff_strt_date;
5056     --
5057     close c_prtt_enrt_rslt_dts;
5058     --
5059     if l_enrt_eff_strt_date is null then
5060       l_enrt_eff_strt_date :=p_effective_date;
5061     end if ;
5062     p_returned_date := trunc( add_months( (l_enrt_eff_strt_date -1 ) ,1) , 'MM') ;
5063 
5064 
5065   elsif p_date_cd  = 'ODD' then
5066     --
5067     -- ODD - On the De-enrollment Date.
5068     --
5069   --  hr_utility.set_location('Entering ODD',10);
5070     --
5071     p_returned_date := p_effective_date;
5072     --
5073   elsif p_date_cd  = 'ODAD' then
5074     --
5075     -- ODAD - On the Default Applied Date.
5076     --
5077   --  hr_utility.set_location('Entering ODAD',10);
5078     --
5079     open c_cm_dflt_asnd_dt;
5080     fetch c_cm_dflt_asnd_dt into l_cm_date;
5081     if c_cm_dflt_asnd_dt%notfound then
5082       close c_cm_dflt_asnd_dt;
5083     --  hr_utility.set_location('BEN_91942_PEL_NOT_FOUND',96);
5084       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
5085       fnd_message.set_token('DATE_CODE',p_date_cd);
5086       fnd_message.set_token('L_PROC',l_proc);
5087       fnd_message.raise_error;
5088     end if;
5089     close c_cm_dflt_asnd_dt;
5090     --
5091     p_returned_date := l_cm_date;
5092     --
5093   elsif p_date_cd  = 'OEPD' then
5094     --
5095     -- OEPD - On the Eligible to Participate Date.
5096     --
5097   --  hr_utility.set_location('Entering OEPD',10);
5098     --
5099     open c_cm_elig_prtn_strt_dt;
5100     fetch c_cm_elig_prtn_strt_dt into l_cm_date;
5101     if c_cm_elig_prtn_strt_dt%notfound then
5102       close c_cm_elig_prtn_strt_dt;
5103     --  hr_utility.set_location('BEN_92381_ELIG_PER_NOT_FOUND',96);
5104       fnd_message.set_name('BEN', 'BEN_92381_ELIG_PER_NOT_FOUND');
5105       fnd_message.set_token('PROC',l_proc);
5106       fnd_message.set_token('DATE_CODE',p_date_cd);
5107       fnd_message.set_token('PERSON_ID',to_char(p_person_id));
5108       fnd_message.set_token('BG_ID',to_char(p_business_group_id));
5109       fnd_message.set_token('EFFECTIVE_DATE',to_char(p_effective_date));
5110       fnd_message.raise_error;
5111     end if;
5112     close c_cm_elig_prtn_strt_dt;
5113     --
5114     p_returned_date := l_cm_date;
5115     --
5116   elsif p_date_cd  = 'OIPD' then
5117     --
5118     -- OIPD - On the Ineligible to Participate Date.
5119     --
5120   --  hr_utility.set_location('Entering OIPD',10);
5121     --
5122     open c_cm_elig_prtn_end_dt;
5123     fetch c_cm_elig_prtn_end_dt into l_cm_date;
5124     if c_cm_elig_prtn_end_dt%notfound then
5125       close c_cm_elig_prtn_end_dt;
5126     --  hr_utility.set_location('BEN_92381_ELIG_PER_NOT_FOUND',98);
5127       fnd_message.set_name('BEN', 'BEN_92381_ELIG_PER_NOT_FOUND');
5128       fnd_message.set_token('PROC',l_proc);
5129       fnd_message.set_token('DATE_CODE',p_date_cd);
5130       fnd_message.set_token('PERSON_ID',to_char(p_person_id));
5131       fnd_message.set_token('BG_ID',to_char(p_business_group_id));
5132       fnd_message.set_token('EFFECTIVE_DATE',to_char(p_effective_date));
5133       fnd_message.raise_error;
5134     end if;
5135     close c_cm_elig_prtn_end_dt;
5136     --
5137     p_returned_date := l_cm_date;
5138     --
5139   elsif p_date_cd  = 'OLDEP' then
5140     --
5141     -- OLDEP - On the last day of the enrollment period.
5142     --
5143   --  hr_utility.set_location('Entering OLDEP',10);
5144     --
5145     open c_cm_enrt_perd_end_dt;
5146     fetch c_cm_enrt_perd_end_dt into l_cm_date;
5147     if c_cm_enrt_perd_end_dt%notfound then
5148       close c_cm_enrt_perd_end_dt;
5149       fnd_message.set_name('BEN', 'BEN_91942_PEL_NOT_FOUND');
5150       fnd_message.set_token('DATE_CODE',p_date_cd);
5151       fnd_message.set_token('L_PROC',l_proc);
5152       fnd_message.raise_error;
5153     end if;
5154     close c_cm_enrt_perd_end_dt;
5155     --
5156     p_returned_date := l_cm_date;
5157     --
5158   elsif p_date_cd  = 'OED' or p_date_cd = 'ENTRBL' or p_date_cd = 'WAENT' or p_date_cd = 'ENTRBLFD' then -- For ICD
5159 
5160     --6823087 -- For CWB (mode = W), if the rate start date code is 'Enterable'
5161               -- we should retrun the rate start date as null
5162 
5163     if p_date_cd = 'ENTRBL' or p_date_cd = 'WAENT' then
5164       ben_env_object.get(p_rec => l_env);
5165 
5166         if nvl(l_env.mode_cd,'~') = 'W' then
5167           p_returned_date := null;
5168         else  -- not CWB
5169           p_returned_date := p_effective_date;
5170         end if;
5171 
5172     else   -- OED
5173     --
5174     -- OED - On the effective date.
5175     --
5176   --  hr_utility.set_location('Entering OED',10);
5177     --
5178     p_returned_date := p_effective_date;
5179   --
5180   -- ODBEFFD - One day before the effective date.
5181   --
5182 
5183     end if;
5184 
5185     --6823087
5186 
5187   elsif p_date_cd  = 'ODBEFFD' then
5188     --
5189   --  hr_utility.set_location('Entering ODBEFFD',10);
5190     --
5191     p_returned_date := p_effective_date-1;
5192     --
5193   --Bug 6212793
5194   elsif p_date_cd  = '10DFED' then
5195       p_returned_date := p_effective_date+10;
5196   elsif p_date_cd  = '20DFED' then
5197       p_returned_date := p_effective_date+20;
5198   elsif p_date_cd  = '30DFED' then
5199       p_returned_date := p_effective_date+30;
5200   ----Bug --Bug 6212793
5201   --
5202   -- FDPPYCF - First Day of Program or Plan Year Concurrent with or Following
5203   --
5204   elsif p_date_cd  = 'FDPPYCF' then
5205   --
5206   --  hr_utility.set_location('Entering FDPPYCF',10);
5207     --
5208     if l_lf_evt_ocrd_dt is null then
5209     --
5210        l_lf_evt_ocrd_dt := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5211     --
5212     end if;
5213     --
5214     get_plan_year
5215            (p_effective_date => p_effective_date
5216            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
5217            ,p_pl_id          => l_pl_id
5218            ,p_pgm_id         => l_pgm_id
5219            ,p_oipl_id        => l_oipl_id
5220            ,p_date_cd        => p_date_cd
5221            ,p_comp_obj_mode  => p_comp_obj_mode
5222            ,p_start_date     => l_start_date
5223            ,p_end_date       => l_end_date) ;
5224     --
5225     if l_start_date = nvl(l_lf_evt_ocrd_dt,p_effective_date) then
5226     --
5227        p_returned_date :=l_start_date;
5228     --
5229     else
5230     --
5231       get_next_plan_year
5232                  (p_effective_date => p_effective_date
5233                  ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
5234                  ,p_pl_id          => l_pl_id
5235                  ,p_pgm_id         => l_pgm_id
5236                  ,p_oipl_id        => l_oipl_id
5237                  ,p_date_cd        => p_date_cd
5238                  ,p_comp_obj_mode  => p_comp_obj_mode
5239                  ,p_start_date     => l_next_popl_yr_strt
5240                  ,p_end_date       => l_next_popl_yr_end) ;
5241        --
5242        p_returned_date :=l_next_popl_yr_strt;
5243     --
5244     end if;
5245   --
5246   -- FDMCF - First Day of Month concurrent with or following
5247   --
5248   elsif p_date_cd  = 'FDMCF' then
5249     --
5250   --  hr_utility.set_location('Entering FDMCF',10);
5251     --
5252     if l_lf_evt_ocrd_dt is null then
5253     --
5254        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5255     --
5256     else
5257     --
5258        l_event_date:= l_lf_evt_ocrd_dt;
5259     --
5260     end if;
5261     --
5262     l_date := add_months(l_event_date,-1);
5263 
5264     if l_event_date = last_day(l_date)+1 then
5265     --
5266        p_returned_date := last_day(l_date)+1;
5267     --
5268     else
5269     --
5270        p_returned_date := last_day(l_event_date)+1;
5271 
5272     --
5273     end if;
5274   --
5275   -- FDPPCF - First day of pay period concurrent with or following
5276   --
5277   elsif p_date_cd = 'FDPPCF' then
5278     --
5279   --  hr_utility.set_location('Entering FDPPCF',10);
5280     --
5281     if l_lf_evt_ocrd_dt is null then
5282     --
5283        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5284     --
5285     else
5286     --
5287        l_event_date:= l_lf_evt_ocrd_dt;
5288     --
5289     end if;
5290     --
5291     open c_pay_period;
5292     fetch c_pay_period into l_pay_period;
5293     if c_pay_period%notfound then
5294       close c_pay_period;
5295     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',135);
5296       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
5297       fnd_message.set_token('DATE_CODE',p_date_cd);
5298       fnd_message.set_token('PERSON_ID',l_person_id);
5299       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
5300       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
5301       fnd_message.raise_error;
5302     end if;
5303     --
5304     close c_pay_period ;
5305     --
5306     if l_event_date =  l_pay_period.start_date then
5307     --
5308        p_returned_date := l_pay_period.start_date;
5309     --
5310     else
5311     --
5312        open c_next_pay_period(nvl(l_lf_evt_ocrd_dt,p_effective_date));
5313        fetch c_next_pay_period into l_next_pay_period;
5314        --
5315        if c_next_pay_period%notfound then
5316          close c_next_pay_period;
5317        --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',140);
5318          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
5319          fnd_message.set_token('DATE_CODE',p_date_cd);
5320          fnd_message.set_token('PERSON_ID',l_person_id);
5321          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
5322          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
5323          fnd_message.raise_error;
5324        end if;
5325        --
5326        p_returned_date := l_next_pay_period.start_date;
5327        --
5328        close c_next_pay_period;
5329     --
5330     end if;
5331   --
5332   -- RL - Rule
5333   --
5334   elsif p_date_cd  = 'RL' then
5335     --
5336   --  hr_utility.set_location('Entering RL',10);
5337     --
5338     open c_asg;
5339     fetch c_asg into l_asg; --if notfound, don't care, will pass null to formula
5340     close c_asg;
5341     --
5342 /* -- 4031733 - Cursor c_state populates l_state variable which is no longer
5343    -- used in the package. Cursor can be commented
5344 
5345    if p_person_id is not null then
5346       open c_state;
5347       fetch c_state into l_state;
5348       close c_state;
5349 
5350       --if l_state.region_2 is not null then
5351 
5352       --  l_jurisdiction_code :=
5353       --    pay_mag_utils.lookup_jurisdiction_code
5354       --      (p_state => l_state.region_2);
5355       --end if;
5356 
5357    end if;
5358 */
5359 
5360    if l_oipl_id is not null then
5361       open c_opt(l_oipl_id);
5362       fetch c_opt into l_opt;
5363       close c_opt;
5364    end if;
5365 
5366    if l_pl_id is not null then
5367      open c_pl_typ(l_pl_id);
5368      fetch c_pl_typ into l_pl_typ_id;
5369      close c_pl_typ;
5370    end if;
5371 
5372     if g_debug then
5373       hr_utility.set_location ('ler_id '||to_char(l_ler_id),70);
5374     end if;
5375    if l_per_in_ler_id is not null then
5376      open c_ler;
5377      fetch c_ler into l_ler_id;
5378      close c_ler;
5379    end if;
5380 
5381     if g_debug then
5382       hr_utility.set_location ('Organization_id 	'||l_asg.organization_id,10);
5383     end if;
5384     if g_debug then
5385       hr_utility.set_location ('assignment_id 	'||l_asg.assignment_id,15);
5386     end if;
5387     if g_debug then
5388       hr_utility.set_location ('Business_group_id '||p_business_group_id,20);
5389     end if;
5390     if g_debug then
5391       hr_utility.set_location ('pgm_id 		'||l_pgm_id,30);
5392     end if;
5393     if g_debug then
5394       hr_utility.set_location ('pl_id 		'||l_pl_id,40);
5395     end if;
5396     if g_debug then
5397       hr_utility.set_location ('pl_typ_id 	'||l_pl_typ_id,50);
5398     end if;
5399     if g_debug then
5400       hr_utility.set_location ('opt_id 		'||l_opt.opt_id,60);
5401     end if;
5402     if g_debug then
5403       hr_utility.set_location ('ler_id 		'||l_ler_id,70);
5404     end if;
5405     if g_debug then
5406       hr_utility.set_location ('p_acty_base_rt_id '||p_acty_base_rt_id,50);
5407     end if;
5408     if g_debug then
5409       hr_utility.set_location ('p_bnfts_bal_id 	'||p_bnfts_bal_id,60);
5410     end if;
5411     if g_debug then
5412       hr_utility.set_location ('jurisdiction_code '||l_jurisdiction_code,70);
5413     end if;
5414 
5415     -- for all other codes LE date is calcualted when the  LE date is null
5416     -- this update does that for  formula too
5417     -- when ever there is a call from ben_newly_ineligible->delete_enrollment->bendetdt.main
5418     --  LE date is null and effectiv date is  le_date -1
5419 
5420     if p_lf_evt_ocrd_dt is  null then
5421        l_rl_lf_evt_ocrd_dt :=  get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5422     else
5423        l_rl_lf_evt_ocrd_dt := p_lf_evt_ocrd_dt ;
5424     end if ;
5425 
5426     -- Call formula initialise routine
5427     -- Added param1 and param1_value parameters to fix bug 1531647
5428 
5429     if g_debug then
5430       hr_utility.set_location ('p_formula_id	'||p_formula_id,1689);
5431     end if;
5432 
5433     l_outputs := benutils.formula
5434       (p_formula_id        => p_formula_id
5435       ,p_effective_date    => nvl(l_rl_lf_evt_ocrd_dt,p_effective_date)
5436       ,p_business_group_id => p_business_group_id
5437       ,p_assignment_id     => l_asg.assignment_id
5438       ,p_organization_id   => l_asg.organization_id
5439       ,p_pgm_id            => l_pgm_id
5440       ,p_pl_id             => l_pl_id
5441       ,p_pl_typ_id         => l_pl_typ_id
5442       ,p_opt_id            => l_opt.opt_id
5443       ,p_ler_id            => l_ler_id
5444       ,p_acty_base_rt_id   => p_acty_base_rt_id
5445       ,p_bnfts_bal_id      => p_bnfts_bal_id
5446       ,p_elig_per_elctbl_chc_id   => p_elig_per_elctbl_chc_id
5447  -- Added two more parameters to fix the Bug 1531647
5448       ,p_param1            => p_param1
5449       ,p_param1_value      => p_param1_value
5450       -- FONM
5451       ,p_param2             => 'BEN_IV_RT_STRT_DT'
5452       ,p_param2_value       => fnd_date.date_to_canonical(nvl(p_fonm_rt_strt_dt,ben_manage_life_events.g_fonm_rt_strt_dt))
5453       ,p_param3             => 'BEN_IV_CVG_STRT_DT'
5454       ,p_param3_value       => fnd_date.date_to_canonical(nvl(p_fonm_cvg_strt_dt,ben_manage_life_events.g_fonm_cvg_strt_dt))
5455       ,p_param4             => 'BEN_IV_PERSON_ID'             -- Bug 5331889
5456       ,p_param4_value       => to_char(p_person_id)
5457       ,p_jurisdiction_code => l_jurisdiction_code);
5458     --
5459 
5460     p_returned_date := fnd_date.canonical_to_date(l_outputs(l_outputs.first).value);
5461 
5462     if g_debug then
5463      hr_utility.set_location ('p_returned_date='||p_returned_date,1689);
5464     end if;
5465     --
5466     --
5467     l_ben_disp_ff_warn_msg := get_profile_ff_warn_val();
5468     --
5469     IF l_ben_disp_ff_warn_msg = 'Y'  /* Bug 5088591 */
5470     THEN
5471       -- For Bug#5070692 in Dependent case p_effective_date was passing as NULL
5472       --
5473       if p_effective_date is null then
5474          l_date_temp := nvl(l_rl_lf_evt_ocrd_dt,p_lf_evt_ocrd_dt) ;
5475       end if;
5476       --
5477       validate_rule_cd_date
5478                            ( p_formula_id     => p_formula_id
5479   			    ,p_computed_Date  => p_returned_date
5480   			    ,p_lf_evt_ocrd_dt => l_rl_lf_evt_ocrd_dt
5481   			    ,p_per_in_ler_id  => p_per_in_ler_id
5482   			    ,p_effective_date => l_date_temp
5483   			    ,p_pgm_id         => p_pgm_id
5484   			    ,p_pl_id          => p_pl_id
5485   			    ,p_opt_id         => l_opt.opt_id
5486   			    ,p_person_id      => p_person_id
5487             		   );
5488       --
5489     END IF;
5490     --
5491   --
5492   --
5493   -- LRD - Later of Recorded Date and Event Date
5494   --
5495   elsif p_date_cd  in ( 'LRD' , 'NUMDOEN')  then
5496   --  hr_utility.set_location('Entering LRD',10);
5497 
5498     if l_lf_evt_ocrd_dt is null then
5499        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5500     else
5501        l_event_date:= l_lf_evt_ocrd_dt;
5502     end if;
5503 
5504     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5505 
5506     If l_recorded_date > l_event_date then
5507         p_returned_date:=l_recorded_date;
5508     else
5509         p_returned_date:=l_event_date;
5510     End If;
5511     --
5512   -- notified date
5513   elsif p_date_cd  =  'NUMDON'  then
5514        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5515        if l_recorded_date is null then
5516           if l_lf_evt_ocrd_dt is null then
5517              l_recorded_date := get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5518           else
5519              l_recorded_date := l_lf_evt_ocrd_dt;
5520           end if;
5521        end if ;
5522        p_returned_date:=l_recorded_date;
5523   --
5524   -- LODBED - One day before later of Recorded Date and Event Date
5525   --
5526   elsif p_date_cd in ('LODBED','LWEM') then
5527     --
5528   --  hr_utility.set_location('Entering LODBED OR LWEM',10);
5529     --
5530     if l_lf_evt_ocrd_dt is null then
5531     --
5532        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5533     --
5534     else
5535     --
5536        l_event_date:= l_lf_evt_ocrd_dt;
5537     --
5538     end if;
5539     --
5540     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5541 
5542     If l_recorded_date > l_event_date then
5543         p_returned_date:=l_recorded_date-1;
5544     else
5545         p_returned_date:=l_event_date-1;
5546     End If;
5547     --
5548     if p_date_cd = 'LWEM' and p_start_date > p_returned_date then
5549       --
5550       p_returned_date := p_start_date ;
5551       --
5552     end if;
5553 
5554      --
5555   --
5556   -- LALDCM - End of Month Later Event or Notified
5557   --
5558   elsif p_date_cd = 'LALDCM'  then
5559     --
5560   --  hr_utility.set_location('Entering LALDCM',10);
5561     --
5562     if l_lf_evt_ocrd_dt is null then
5563     --
5564        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5565     --
5566     else
5567     --
5568        l_event_date:= l_lf_evt_ocrd_dt;
5569     --
5570     end if;
5571     --
5572     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5573 
5574     If l_recorded_date > l_event_date then
5575        p_returned_date := last_day(l_recorded_date);
5576     else
5577        p_returned_date := last_day(l_event_date);
5578     End If;
5579   --
5580   -- LALDFM - End of Month After Later Event or Notified
5581   --
5582   elsif p_date_cd = 'LALDFM' then
5583     --
5584   --  hr_utility.set_location('Entering LALDFM',10);
5585     --
5586     if l_lf_evt_ocrd_dt is null then
5587     --
5588        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5589     --
5590     else
5591     --
5592        l_event_date:= l_lf_evt_ocrd_dt;
5593     --
5594     end if;
5595     --
5596     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5597     If l_recorded_date > l_event_date then
5598        p_returned_date := last_day(add_months(l_recorded_date,1));
5599     else
5600        p_returned_date := last_day(add_months(l_event_date,1));
5601     End If;
5602   --
5603   -- LAFDFM - Later: First Day of Following Month
5604   --
5605   elsif p_date_cd = 'LAFDFM' then
5606     --
5607   --  hr_utility.set_location('Entering LAFDFM',10);
5608     --
5609     if l_lf_evt_ocrd_dt is null then
5610     --
5611        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5612     --
5613     else
5614     --
5615        l_event_date:= l_lf_evt_ocrd_dt;
5616     --
5617     end if;
5618     --
5619     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5620     If l_recorded_date > l_event_date then
5621        p_returned_date := last_day(l_recorded_date)+1;
5622     else
5623        p_returned_date := last_day(l_event_date)+1;
5624     End If;
5625  --
5626  -- LALDCPPY - Later: Last day of current program or plan year
5627  --
5628  elsif p_date_cd  = 'LALDCPPY' then
5629    --
5630   --  hr_utility.set_location('Entering LALDCPPY',10);
5631     --
5632     if l_lf_evt_ocrd_dt is null then
5633     --
5634        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5635     --
5636     else
5637     --
5638        l_event_date:= l_lf_evt_ocrd_dt;
5639     --
5640     end if;
5641     --
5642    l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5643 
5644    If l_recorded_date > l_event_date then
5645       l_lf_evt_ocrd_dt := l_recorded_date;
5646    else
5647       l_lf_evt_ocrd_dt  := l_event_date;
5648    End If;
5649 
5650     get_plan_year
5651            (p_effective_date => p_effective_date
5652            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
5653            ,p_pl_id          => l_pl_id
5654            ,p_pgm_id         => l_pgm_id
5655            ,p_oipl_id        => l_oipl_id
5656            ,p_date_cd        => p_date_cd
5657            ,p_comp_obj_mode  => p_comp_obj_mode
5658            ,p_start_date     => l_start_date
5659            ,p_end_date       => l_end_date) ;
5660     --
5661     p_returned_date :=l_end_date;
5662  --
5663  -- LAFDLMEPPY - First of Last Month in Year Later Event or Notified
5664  --
5665  elsif p_date_cd  = 'LAFDLMEPPY' then
5666    --
5667   --  hr_utility.set_location('Entering LAFDLMEPPY',10);
5668     --
5669     if l_lf_evt_ocrd_dt is null then
5670     --
5671        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5672     --
5673     else
5674     --
5675        l_event_date:= l_lf_evt_ocrd_dt;
5676     --
5677     end if;
5678     --
5679    l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5680 
5681    If l_recorded_date > l_event_date then
5682       l_lf_evt_ocrd_dt := l_recorded_date;
5683    else
5684       l_lf_evt_ocrd_dt  := l_event_date;
5685    End If;
5686 
5687     get_plan_year
5688            (p_effective_date => p_effective_date
5689            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
5690            ,p_pl_id          => l_pl_id
5691            ,p_pgm_id         => l_pgm_id
5692            ,p_oipl_id        => l_oipl_id
5693            ,p_date_cd        => p_date_cd
5694            ,p_comp_obj_mode  => p_comp_obj_mode
5695            ,p_start_date     => l_start_date
5696            ,p_end_date       => l_end_date) ;
5697     --
5698     p_returned_date := last_day(add_months(l_end_date,-1))+1;
5699     --
5700  --
5701  -- LALDLPPEPPY - End of Last Pay Period of Event Year Later Event or Notified
5702  --
5703  elsif p_date_cd  = 'LALDLPPEPPY' then
5704    --
5705   --  hr_utility.set_location('Entering LALDLPPEPPY',10);
5706     --
5707     if l_lf_evt_ocrd_dt is null then
5708     --
5709        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5710     --
5711     else
5712     --
5713        l_event_date:= l_lf_evt_ocrd_dt;
5714     --
5715     end if;
5716     --
5717     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5718     --
5719     If l_recorded_date > l_event_date then
5720        l_lf_evt_ocrd_dt := l_recorded_date;
5721     else
5722        l_lf_evt_ocrd_dt  := l_event_date;
5723     End If;
5724     --
5725     open c_pay_period_for_date(l_lf_evt_ocrd_dt);
5726     fetch c_pay_period_for_date into
5727           l_start_date,
5728           l_end_date;
5729     --
5730     if c_pay_period_for_date%notfound then
5731     --
5732       close c_pay_period_for_date;
5733     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',145);
5734       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
5735       fnd_message.set_token('DATE_CODE',p_date_cd);
5736       fnd_message.set_token('PERSON_ID',l_person_id);
5737       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
5738       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
5739       fnd_message.raise_error;
5740     --
5741     end if;
5742     --
5743     close c_pay_period_for_date;
5744     --
5745     p_returned_date := l_end_date;
5746     --
5747   --
5748   -- WALDLPPEPPY - 1 Prior or Last Pay Period End
5749   --
5750   elsif p_date_cd  = 'WALDLPPEPPY' then
5751     --
5752     if g_debug then
5753       hr_utility.set_location('Entering WALDLPPEPPY',10);
5754     end if;
5755     --
5756        if l_lf_evt_ocrd_dt is null then
5757        --
5758           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5759        --
5760        else
5761        --
5762           l_event_date := l_lf_evt_ocrd_dt;
5763        --
5764        end if;
5765        --
5766       --
5767       open c_pre_pay_period(l_event_date);
5768        fetch c_pre_pay_period into l_pre_pay_period;
5769 
5770        --
5771        if c_pre_pay_period%notfound then
5772        --
5773          close c_pre_pay_period;
5774       --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',150);
5775          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
5776          fnd_message.set_token('DATE_CODE',p_date_cd);
5777          fnd_message.set_token('PERSON_ID',l_person_id);
5778          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
5779          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
5780          fnd_message.raise_error;
5781        --
5782        end if;
5783        --
5784 	close c_pre_pay_period;
5785        --
5786 	p_returned_date := l_pre_pay_period.end_date;
5787     --
5788     --
5789   --
5790   -- LODBEWM - 1 Prior New Rate Start or 1 Before Later Event or Notified
5791   --
5792   elsif p_date_cd  = 'LODBEWM' then
5793     --
5794   --  hr_utility.set_location('Entering LODBEWM',10);
5795     --
5796     open c_prtt_rt_val_dts;
5797     --
5798 /*
5799       fetch c_prtt_rt_val_dts into l_start_date,
5800                                    l_end_date;
5801 */
5802     fetch c_prtt_rt_val_dts into l_dummy ;
5803     --
5804     if c_prtt_rt_val_dts%notfound then
5805     --
5806        if l_lf_evt_ocrd_dt is null then
5807        --
5808           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5809        --
5810        end if;
5811        --
5812        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5813        --
5814        --RCHASE Bug fix logic
5815        --If l_recorded_date > l_event_date then
5816        If l_recorded_date > l_event_date-1 then
5817        --
5818           --RCHASE Bug fix logic
5819           --p_returned_date := l_recorded_date - 1;
5820           p_returned_date := l_recorded_date;
5821        --
5822        else
5823        --
5824           p_returned_date := l_event_date - 1;
5825        --
5826        End If;
5827     --
5828     else
5829     --
5830 /*
5831       open c_enrt_rt_val_dt;
5832       --
5833       fetch c_enrt_rt_val_dt into l_end_date;
5834       if c_enrt_rt_val_dt%found then
5835           --
5836           if g_debug then
5837             hr_utility.set_location('l_end_date - 1',1999);
5838           end if;
5839           p_returned_date := l_end_date - 1;
5840           --
5841       else
5842           --
5843        if l_lf_evt_ocrd_dt is null then
5844        --
5845        if g_debug then
5846          hr_utility.set_location('l_lf_evt_ocrd_dt is null',1999);
5847        end if;
5848        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5849        --
5850        end if;
5851 
5852           p_returned_date := l_event_date - 1;
5853           --
5854       end if;
5855       --
5856       close c_enrt_rt_val_dt ;
5857 */
5858 
5859     --  p_returned_date := p_start_date - 1;
5860         p_returned_date := null ; -- This is handled in rate and coverages call
5861     --
5862     end if;
5863     --
5864     close c_prtt_rt_val_dts;
5865 
5866     --
5867   --
5868   -- ODBEWM - 1 Prior New Rate Start or 1 Day Before Event
5869   --
5870   elsif p_date_cd  = 'ODBEWM' then
5871     --
5872     if g_debug then
5873       hr_utility.set_location('Entering ODBEWM',10);
5874     end if;
5875     --
5876     open c_prtt_rt_val_dts;
5877     --
5878 /*
5879       fetch c_prtt_rt_val_dts into l_start_date,
5880                                    l_end_date;
5881 */
5882       fetch c_prtt_rt_val_dts into l_dummy ;
5883     --
5884     if g_debug then
5885       hr_utility.set_location('l_start_date'||l_start_date , 1999);
5886     end if;
5887     if g_debug then
5888       hr_utility.set_location('l_end_date'||l_end_date , 1999);
5889     end if;
5890 
5891     if c_prtt_rt_val_dts%notfound then
5892     --
5893        if l_lf_evt_ocrd_dt is null then
5894        --
5895        if g_debug then
5896          hr_utility.set_location('l_lf_evt_ocrd_dt is null',1999);
5897        end if;
5898           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5899        --
5900        end if;
5901        --
5902        if g_debug then
5903          hr_utility.set_location('l_event_date - 1',1999);
5904        end if;
5905        p_returned_date := l_event_date - 1;
5906     --
5907     else
5908 /*
5909 
5910       open c_enrt_rt_val_dt;
5911       --
5912       fetch c_enrt_rt_val_dt into l_end_date;
5913       --
5914       if c_enrt_rt_val_dt%found then
5915           --
5916          if g_debug then
5917            hr_utility.set_location('l_end_date - 1',1999);
5918          end if;
5919          p_returned_date := l_end_date - 1;
5920           --
5921       else
5922 
5923        if l_lf_evt_ocrd_dt is null then
5924        --
5925        if g_debug then
5926          hr_utility.set_location('p_lf_evt_ocrd_dt is null',1999);
5927        end if;
5928        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5929        --
5930        end if;
5931           if g_debug then
5932             hr_utility.set_location('l_lf_evt_ocrd_dt else'||l_event_date , 1999);
5933           end if;
5934           p_returned_date := l_event_date - 1;
5935           --
5936       end if;
5937 
5938       -- p_returned_date := l_start_date - 1;
5939       --
5940       close c_enrt_rt_val_dt ;
5941 */
5942    -- p_returned_date := p_start_date - 1;
5943       p_returned_date := null ; -- This is handled in rate and coverages call
5944 
5945     --
5946     end if;
5947     --
5948     close c_prtt_rt_val_dts;
5949     --
5950   --
5951   -- LWODBED - 1 Prior or 1 Day Before Later of Event or Notified
5952   --
5953   elsif p_date_cd  = 'LWODBED' then
5954     --
5955     if g_debug then
5956       hr_utility.set_location('Entering LWODBED',10);
5957     end if;
5958     --
5959        if l_lf_evt_ocrd_dt is null then
5960        --
5961           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5962        --
5963        else
5964        --
5965           l_event_date:= l_lf_evt_ocrd_dt;
5966        --
5967        end if;
5968        --
5969        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5970        --
5971        --RCHASE Bug fix logic
5972        --If l_recorded_date > l_event_date then
5973        If l_recorded_date > l_event_date-1 then
5974        --
5975           --RCHASE Bug fix logic
5976           --p_returned_date := l_recorded_date - 1;
5977           p_returned_date := l_recorded_date;
5978        --
5979        else
5980        --
5981           p_returned_date := l_event_date - 1;
5982        --
5983        End If;
5984     --
5985   --
5986   -- LWALDLPPEPPY - 1 Prior or Later Event or Notified Last Pay Period End
5987   --
5988   elsif p_date_cd  = 'LWALDLPPEPPY' then
5989   --
5990     if g_debug then
5991       hr_utility.set_location('Entering LWALDLPPEPPY',10);
5992     end if;
5993     --
5994        if l_lf_evt_ocrd_dt is null then
5995        --
5996           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
5997        --
5998        else
5999        --
6000           l_event_date:= l_lf_evt_ocrd_dt;
6001        --
6002        end if;
6003        --
6004        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6005        --
6006        If l_recorded_date > l_event_date then
6007        --
6008           l_lf_evt_ocrd_dt := l_recorded_date;
6009           --
6010            get_plan_year
6011            (p_effective_date => p_effective_date
6012            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6013            ,p_pl_id          => l_pl_id
6014            ,p_pgm_id         => l_pgm_id
6015            ,p_oipl_id        => l_oipl_id
6016            ,p_date_cd        => p_date_cd
6017            ,p_comp_obj_mode  => p_comp_obj_mode
6018            ,p_start_date     => l_start_date
6019            ,p_end_date       => l_end_date) ;
6020           --
6021           open c_pay_period_for_date(l_end_date);
6022           fetch c_pay_period_for_date into
6023                 l_start_date,
6024                 l_end_date;
6025           --
6026           if c_pay_period_for_date%notfound then
6027           --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',160);
6028             fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
6029             fnd_message.set_token('DATE_CODE',p_date_cd);
6030             fnd_message.set_token('PERSON_ID',l_person_id);
6031             fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
6032             fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
6033             fnd_message.raise_error;
6034           --
6035           end if;
6036           --
6037           close c_pay_period_for_date;
6038           --
6039           p_returned_date := l_end_date;
6040        --
6041        else
6042        --
6043           l_lf_evt_ocrd_dt := l_event_date;
6044           --
6045           get_plan_year
6046            (p_effective_date => p_effective_date
6047            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6048            ,p_pl_id          => l_pl_id
6049            ,p_pgm_id         => l_pgm_id
6050            ,p_oipl_id        => l_oipl_id
6051            ,p_date_cd        => p_date_cd
6052            ,p_comp_obj_mode  => p_comp_obj_mode
6053            ,p_start_date     => l_start_date
6054            ,p_end_date       => l_end_date) ;
6055           --
6056           open c_pay_period_for_date(l_end_date);
6057           fetch c_pay_period_for_date into
6058                 l_start_date,
6059                 l_end_date;
6060           --
6061           if c_pay_period_for_date%notfound then
6062           --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',165);
6063             fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
6064             fnd_message.set_token('DATE_CODE',p_date_cd);
6065             fnd_message.set_token('PERSON_ID',l_person_id);
6066             fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
6067             fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
6068             fnd_message.raise_error;
6069           --
6070           end if;
6071           --
6072           close c_pay_period_for_date;
6073           --
6074           p_returned_date := l_end_date;
6075        --
6076        End If;
6077     --
6078   --
6079   -- LWALDCPPY - 1 Prior or Later Event or Notified Year End
6080   --
6081   elsif p_date_cd  = 'LWALDCPPY' then
6082   --
6083     if g_debug then
6084       hr_utility.set_location('Entering LWALDCPPY',10);
6085     end if;
6086     --
6087        if l_lf_evt_ocrd_dt is null then
6088        --
6089           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6090        --
6091        else
6092        --
6093           l_event_date:= l_lf_evt_ocrd_dt;
6094        --
6095        end if;
6096        --
6097        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6098        --
6099        If l_recorded_date > l_event_date then
6100        --
6101           l_lf_evt_ocrd_dt := l_recorded_date;
6102           --
6103           get_plan_year
6104            (p_effective_date => p_effective_date
6105            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6106            ,p_pl_id          => l_pl_id
6107            ,p_pgm_id         => l_pgm_id
6108            ,p_oipl_id        => l_oipl_id
6109            ,p_date_cd        => p_date_cd
6110            ,p_comp_obj_mode  => p_comp_obj_mode
6111            ,p_start_date     => l_start_date
6112            ,p_end_date       => l_end_date) ;
6113           --
6114           p_returned_date := l_end_date;
6115        --
6116        else
6117        --
6118           l_lf_evt_ocrd_dt := l_event_date;
6119           --
6120            get_plan_year
6121            (p_effective_date => p_effective_date
6122            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6123            ,p_pl_id          => l_pl_id
6124            ,p_pgm_id         => l_pgm_id
6125            ,p_oipl_id        => l_oipl_id
6126            ,p_date_cd        => p_date_cd
6127            ,p_comp_obj_mode  => p_comp_obj_mode
6128            ,p_start_date     => l_start_date
6129            ,p_end_date       => l_end_date) ;
6130           --
6131           p_returned_date := l_end_date;
6132        --
6133        End If;
6134     --
6135   --
6136   -- LWALDCPP - 1 Prior or Later Event or Notified Pay Period End
6137   --
6138   elsif p_date_cd  = 'LWALDCPP' then
6139   --
6140     if g_debug then
6141       hr_utility.set_location('Entering LWALDCPP',10);
6142     end if;
6143     --
6144        if l_lf_evt_ocrd_dt is null then
6145        --
6146           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6147        --
6148        else
6149        --
6150           l_event_date:= l_lf_evt_ocrd_dt;
6151        --
6152        end if;
6153        --
6154        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6155        --
6156        If l_recorded_date > l_event_date then
6157        --
6158           open c_pay_period_for_date(l_recorded_date);
6159           fetch c_pay_period_for_date into
6160              l_start_date,
6161              l_end_date;
6162           close c_pay_period_for_date;
6163           --
6164           p_returned_date := l_end_date;
6165        --
6166        else
6167        --
6168           open c_pay_period_for_date(l_event_date);
6169           fetch c_pay_period_for_date into
6170              l_start_date,
6171              l_end_date;
6172           close c_pay_period_for_date;
6173           --
6174           p_returned_date := l_end_date;
6175        --
6176        End If;
6177     --
6178   --
6179   -- LWALDCM - 1 Prior or Later Event or Notified Month End
6180   --
6181   elsif p_date_cd  = 'LWALDCM' then
6182     --
6183     if g_debug then
6184       hr_utility.set_location('Entering LWALDCM',10);
6185     end if;
6186     --
6187        if l_lf_evt_ocrd_dt is null then
6188        --
6189           l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6190        --
6191        else
6192        --
6193           l_event_date:= l_lf_evt_ocrd_dt;
6194        --
6195        end if;
6196        --
6197        l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6198        --
6199        If l_recorded_date > l_event_date then
6200        --
6201           p_returned_date := last_day(l_recorded_date);
6202        --
6203        else
6204        --
6205           p_returned_date := last_day(l_event_date);
6206        --
6207        End If;
6208     --
6209   --
6210   -- LFDPPYCF - First of Year On or After Later of Event or Notified
6211   --
6212   elsif p_date_cd  = 'LFDPPYCF' then
6213   --
6214   --  hr_utility.set_location('Entering LFDPPYCF',10);
6215     --
6216     if l_lf_evt_ocrd_dt is null then
6217     --
6218        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6219     --
6220     else
6221     --
6222        l_event_date:= l_lf_evt_ocrd_dt;
6223     --
6224     end if;
6225     --
6226     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6227 
6228     If l_recorded_date > l_event_date then
6229        l_lf_evt_ocrd_dt := l_recorded_date;
6230     else
6231        l_lf_evt_ocrd_dt  := l_event_date;
6232     End If;
6233 
6234     get_plan_year
6235            (p_effective_date => p_effective_date
6236            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6237            ,p_pl_id          => l_pl_id
6238            ,p_pgm_id         => l_pgm_id
6239            ,p_oipl_id        => l_oipl_id
6240            ,p_date_cd        => p_date_cd
6241            ,p_comp_obj_mode  => p_comp_obj_mode
6242            ,p_start_date     => l_start_date
6243            ,p_end_date       => l_end_date) ;
6244     --
6245     if l_lf_evt_ocrd_dt = l_start_date then
6246     --
6247       p_returned_date :=l_start_date;
6248     --
6249     else
6250     --
6251       get_next_plan_year
6252                  (p_effective_date => p_effective_date
6253                  ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6254                  ,p_pl_id          => l_pl_id
6255                  ,p_pgm_id         => l_pgm_id
6256                  ,p_oipl_id        => l_oipl_id
6257                  ,p_date_cd        => p_date_cd
6258                  ,p_comp_obj_mode  => p_comp_obj_mode
6259                  ,p_start_date     => l_next_popl_yr_strt
6260                  ,p_end_date       => l_next_popl_yr_end) ;
6261       --
6262       p_returned_date :=l_next_popl_yr_strt;
6263     --
6264     end if;
6265     --
6266   --
6267   -- LAFDCPPY - Later: First day of current program or plan year
6268   --
6269   elsif p_date_cd  = 'LAFDCPPY' then
6270   --
6271   --  hr_utility.set_location('Entering LAFDCPPY',10);
6272     --
6273     if l_lf_evt_ocrd_dt is null then
6274     --
6275        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6276     --
6277     else
6278     --
6279        l_event_date:= l_lf_evt_ocrd_dt;
6280     --
6281     end if;
6282     --
6283     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6284 
6285     If l_recorded_date > l_event_date then
6286        l_lf_evt_ocrd_dt := l_recorded_date;
6287     else
6288        l_lf_evt_ocrd_dt  := l_event_date;
6289     End If;
6290 
6291     get_plan_year
6292            (p_effective_date => p_effective_date
6293            ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6294            ,p_pl_id          => l_pl_id
6295            ,p_pgm_id         => l_pgm_id
6296            ,p_oipl_id        => l_oipl_id
6297            ,p_date_cd        => p_date_cd
6298            ,p_comp_obj_mode  => p_comp_obj_mode
6299            ,p_start_date     => l_start_date
6300            ,p_end_date       => l_end_date) ;
6301     --
6302     p_returned_date :=l_start_date;
6303   --
6304   -- LAFDFPPY - Later: First day of following program or plan year
6305   --
6306   elsif p_date_cd = 'LAFDFPPY' then
6307     --
6308   --  hr_utility.set_location('Entering LAFDFPPY',10);
6309     --
6310     if l_lf_evt_ocrd_dt is null then
6311     --
6312        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6313     --
6314     else
6315     --
6316        l_event_date:= l_lf_evt_ocrd_dt;
6317     --
6318     end if;
6319     --
6320     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6321 
6322     If l_recorded_date > l_event_date then
6323        l_lf_evt_ocrd_dt := l_recorded_date;
6324     else
6325        l_lf_evt_ocrd_dt  := l_event_date;
6326     End If;
6327 
6328     get_next_plan_year
6329                  (p_effective_date => p_effective_date
6330                  ,p_lf_evt_ocrd_dt => l_lf_evt_ocrd_dt
6331                  ,p_pl_id          => l_pl_id
6332                  ,p_pgm_id         => l_pgm_id
6333                  ,p_oipl_id        => l_oipl_id
6334                  ,p_date_cd        => p_date_cd
6335                  ,p_comp_obj_mode  => p_comp_obj_mode
6336                  ,p_start_date     => l_next_popl_yr_strt
6337                  ,p_end_date       => l_next_popl_yr_end) ;
6338     --
6339     p_returned_date :=l_next_popl_yr_strt;
6340   --
6341   -- LALDCPP - Later: Last day of current pay period
6342   --
6343   elsif p_date_cd = 'LALDCPP' then
6344     --
6345   --  hr_utility.set_location('Entering LALDCPP',10);
6346     --
6347     if l_lf_evt_ocrd_dt is null then
6348     --
6349        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6350     --
6351     else
6352     --
6353        l_event_date:= l_lf_evt_ocrd_dt;
6354     --
6355     end if;
6356     --
6357     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6358 
6359     If l_recorded_date > l_event_date then
6360        l_lf_evt_ocrd_dt := l_recorded_date;
6361     else
6362        l_lf_evt_ocrd_dt  := l_event_date;
6363     End If;
6364 
6365     open c_pay_period;
6366     fetch c_pay_period into l_pay_period;
6367     if c_pay_period%notfound then
6368       close c_pay_period;
6369     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',170);
6370       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
6371       fnd_message.set_token('DATE_CODE',p_date_cd);
6372       fnd_message.set_token('PERSON_ID',l_person_id);
6373       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
6374       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
6375       fnd_message.raise_error;
6376     end if;
6377     p_returned_date := l_pay_period.end_date;
6378     close c_pay_period ;
6379   --
6380   --
6381   -- LAFDFPP - Later: First day of following pay period
6382   --
6383   elsif p_date_cd = 'LAFDFPP' then
6384     --
6385   --  hr_utility.set_location('Entering LAFDFPP',10);
6386     --
6387     if l_lf_evt_ocrd_dt is null then
6388     --
6389        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6390     --
6391     else
6392     --
6393        l_event_date:= l_lf_evt_ocrd_dt;
6394     --
6395     end if;
6396     --
6397     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6398 
6399     If l_recorded_date > l_event_date then
6400        l_lf_evt_ocrd_dt := l_recorded_date;
6401     else
6402        l_lf_evt_ocrd_dt  := l_event_date;
6403     End If;
6404 
6405     open c_next_pay_period(nvl(l_lf_evt_ocrd_dt,p_effective_date));
6406     fetch c_next_pay_period into l_next_pay_period;
6407     --
6408     if c_next_pay_period%notfound then
6409       close c_next_pay_period;
6410     --  hr_utility.set_location('BEN_91477_PAY_PERIOD_MISSING',170);
6411       fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
6412       fnd_message.set_token('DATE_CODE',p_date_cd);
6413       fnd_message.set_token('PERSON_ID',l_person_id);
6414       fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
6415       fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
6416       fnd_message.raise_error;
6417     end if;
6418     p_returned_date := l_next_pay_period.start_date;
6419     close c_next_pay_period;
6420   --
6421   -- LTODFED - Thirty-one days After Later of Recorded Date and Event Date
6422   --
6423   elsif p_date_cd  = 'LTODFED' then
6424     --
6425   --  hr_utility.set_location('Entering LTODFED',10);
6426     --
6427     if l_lf_evt_ocrd_dt is null then
6428     --
6429        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6430     --
6431     else
6432     --
6433        l_event_date:= l_lf_evt_ocrd_dt;
6434     --
6435     end if;
6436     --
6437     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6438     If l_recorded_date > l_event_date then
6439        p_returned_date := l_recorded_date + 31;
6440     else
6441        p_returned_date := l_event_date + 31;
6442     End If;
6443   --
6444   -- LSDFED - Sixty days After Later of Recorded Date and Event Date
6445   --
6446   elsif p_date_cd  = 'LSDFED' then
6447     --
6448   --  hr_utility.set_location('Entering LSDFED',10);
6449     --
6450     if l_lf_evt_ocrd_dt is null then
6451     --
6452        l_event_date:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6453     --
6454     else
6455     --
6456        l_event_date:= l_lf_evt_ocrd_dt;
6457 
6458     end if;
6459 
6460     l_recorded_date:= get_recorded_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6461 
6462     If l_recorded_date > l_event_date then
6463        p_returned_date := l_recorded_date + 60;
6464     else
6465        p_returned_date := l_event_date + 60;
6466     End If;
6467   elsif p_date_cd = 'NA' then
6468        -- Not Applicable
6469        p_returned_date := null ;
6470 
6471     --- date related to hire dates
6472  elsif p_date_cd in ( '30DAHRD','31DAHRD','60DAHRD','61DAHRD') then
6473 
6474        -- get the latest hire date, if fails error
6475        if g_debug then
6476          hr_utility.set_location(' Hire date '  , 99 );
6477        end if;
6478        open c_pps ;
6479        fetch c_pps into l_hire_date ;
6480        close c_pps ;
6481        if g_debug then
6482          hr_utility.set_location(' Hire date ' || l_hire_date, 99 );
6483        end if;
6484        if  l_hire_date is null then
6485            fnd_message.set_name('BEN','BEN_92489_CANNOT_CALC_DATE');
6486            fnd_message.set_token('DATE_CODE',p_date_cd);
6487            fnd_message.set_token('L_PROC',l_proc);
6488            fnd_message.raise_error;
6489        end if ;
6490        if g_debug then
6491          hr_utility.set_location(' Hire date ' || l_hire_date, 99 );
6492        end if;
6493 
6494        if p_date_cd = '30DAHRD' then
6495           p_returned_date := l_hire_date + 30 ;
6496        elsif p_date_cd = '31DAHRD' then
6497           p_returned_date := l_hire_date + 31 ;
6498        elsif p_date_cd = '60DAHRD' then
6499           p_returned_date := l_hire_date + 60 ;
6500        elsif p_date_cd = '61DAHRD' then
6501           p_returned_date := l_hire_date + 61 ;
6502        end if ;
6503         if g_debug then
6504           hr_utility.set_location(' p_date_cd  ' || p_returned_date, 99 );
6505         end if;
6506 
6507   elsif p_date_cd in ('AFDELY','ALDELMY','FDM','FDMCFC','FDMFC',
6508                       'FDPP','FDPPQCFC','FDPPQFC','FDPPSYCFC','FDPPSYFC',                      'FDPPYCFC','FDPPYFC','FDQ','FFDFED','FFDFEPS',
6509                       'FDPPY','LDPPFEE','LDPPOAEE','LWALDCPPQ','LWALDPPSY',
6510                       'NDFEPS','WALDCPPQ','WALDCPPSY','FDCY','FFDFEPS',
6511                       'FDLY','FDQ','FDSY', 'LFSEMES'
6512                      ) then
6513 
6514    --  hr_utility.set_location('Future Date Code:  '||p_date_cd , 222);
6515      fnd_message.set_name('BEN','FUTURE_DATE_CD_DO_NOT_USE');
6516      fnd_message.set_token('DATE_CODE',p_date_cd);
6517      fnd_message.raise_error;
6518 
6519    elsif p_date_cd in ('FDPPFCDE','FDPPFCDEL') then
6520      --
6521      if g_debug then
6522          hr_utility.set_location(' Step 2 ' ,181);
6523      end if;
6524      --
6525      if p_date_cd = 'FDPPFCDE' then
6526        --
6527        if l_lf_evt_ocrd_dt is null then
6528          l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6529        end if;
6530        l_start_date := l_lf_evt_ocrd_dt;
6531      else
6532        --
6533        hr_utility.set_location('p effective date '||p_effective_date,182);
6534        l_start_date := p_effective_date;
6535      end if;
6536      --
6537      open c_pay_period_for_check(l_start_date, 'E');
6538      fetch c_pay_period_for_check into l_start_date;
6539      if c_pay_period_for_check%notfound then
6540        --
6541        close c_pay_period_for_check;
6542        open c_pay_period_for_check(l_start_date, 'B');
6543        fetch c_pay_period_for_check into l_start_date;
6544        if c_pay_period_for_check%notfound then
6545          --
6546          close c_pay_period_for_check;
6547          --
6548          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
6549          fnd_message.set_token('DATE_CODE',p_date_cd);
6550          fnd_message.set_token('L_PROC',l_proc);
6551          fnd_message.set_token('PERSON_ID',l_person_id);
6552          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
6553          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
6554          fnd_message.raise_error;
6555          --
6556        end if;
6557      else
6558        --  rehire and new hire validation
6559        open c_hire_date;
6560        fetch c_hire_date into l_hire_date;
6561        close c_hire_date;
6562        --
6563        if l_hire_date > l_start_date then
6564          --
6565          l_start_date := l_hire_date;
6566          --
6567        end if;
6568        --
6569      end if;
6570      --
6571      close c_pay_period_for_check;
6572      p_returned_date := l_start_date;
6573 
6574    elsif p_date_cd in ('WAPPCDE','WAPPDEL') then
6575      --
6576      if g_debug then
6577          hr_utility.set_location(' Step 2 ' ,181);
6578      end if;
6579      --
6580      if p_date_cd = 'WAPPCDE' then
6581        --
6582        if l_lf_evt_ocrd_dt is null then
6583          l_lf_evt_ocrd_dt:= get_event_date(p_cache_mode,l_per_in_ler_id,p_effective_date);
6584        end if;
6585        l_end_date := l_lf_evt_ocrd_dt;
6586      else
6587        --
6588        l_end_date := p_effective_date;
6589      end if;
6590      --
6591      open c_pay_period_for_check_end (l_end_date, 'E');
6592      fetch c_pay_period_for_check_end  into l_end_date;
6593      if c_pay_period_for_check_end %notfound then
6594        --
6595        close c_pay_period_for_check_end ;
6596        open c_pay_period_for_check_end (l_end_date, 'B');
6597        fetch c_pay_period_for_check_end  into l_end_date;
6598        if c_pay_period_for_check_end %notfound then
6599          --
6600          close c_pay_period_for_check_end ;
6601          --
6602          fnd_message.set_name('BEN','BEN_91477_PAY_PERIOD_MISSING');
6603          fnd_message.set_token('DATE_CODE',p_date_cd);
6604          fnd_message.set_token('L_PROC',l_proc);
6605          fnd_message.set_token('PERSON_ID',l_person_id);
6606          fnd_message.set_token('BUSINESS_GROUP_ID',l_business_group_id);
6607          fnd_message.set_token('EFFECTIVE_DATE',p_effective_date);
6608          fnd_message.raise_error;
6609          --
6610        end if;
6611      end if;
6612      --
6613      close c_pay_period_for_check_end ;
6614      p_returned_date := l_end_date;
6615   else
6616 
6617      hr_utility.set_location('Unknown Date Code:  '||p_date_cd , 222);
6618      fnd_message.set_name('BEN','BEN_91534_BEN_UNKNOWN_DATE_CD');
6619      fnd_message.set_token('DATE_CODE',p_date_cd);
6620      fnd_message.set_token('L_PROC',l_proc);
6621      fnd_message.raise_error;
6622 
6623   end  if;
6624   --
6625   if g_debug then
6626     hr_utility.set_location('p_returned_date: '||p_returned_date,15);
6627   end if;
6628   p_returned_date := trunc(p_returned_date);
6629   --
6630   if g_debug then
6631     hr_utility.set_location('sent dt code ' || p_date_cd ,596);
6632   end if;
6633   if g_debug then
6634     hr_utility.set_location('sent_dt p_returned_date: '||p_returned_date,15);
6635   end if;
6636   if g_debug then
6637     hr_utility.set_location('Leaving : '||l_proc , 20);
6638   end if;
6639 exception   -- nocopy changes
6640   --
6641   when others then
6642     --
6643     p_returned_date := null;
6644     raise;
6645     --
6646 end main;
6647 --
6648 -- Procedure Name
6649 --    Rate and Coverage Dates
6650 -- Purpose
6651 --    This procedure is used to determine the (rate or coverage) (start or end)
6652 --    date, code, and rule.  It calls ben_determine_date if the absolute date
6653 --    is needed.
6654 --
6655 procedure rate_and_coverage_dates
6656   (p_cache_mode             in     boolean default false
6657   --
6658   -- Cache related parameters
6659   --
6660   ,p_pgm_row                in     ben_cobj_cache.g_pgm_inst_row
6661   := ben_cobj_cache.g_pgm_default_row
6662   ,p_ptip_row               in     ben_cobj_cache.g_ptip_inst_row
6663   := ben_cobj_cache.g_ptip_default_row
6664   ,p_plip_row               in     ben_cobj_cache.g_plip_inst_row
6665   := ben_cobj_cache.g_plip_default_row
6666   ,p_pl_row                 in     ben_cobj_cache.g_pl_inst_row
6667   := ben_cobj_cache.g_pl_default_row
6668   --
6669   ,p_per_in_ler_id          in     number  default null
6670   ,p_person_id              in     number  default null
6671   ,p_pgm_id                 in     number  default null
6672   ,p_pl_id                  in     number  default null
6673   ,p_oipl_id                in     number  default null
6674   ,p_par_ptip_id            in     number  default null
6675   ,p_par_plip_id            in     number  default null
6676   ,p_lee_rsn_id             in     number  default null
6677   ,p_enrt_perd_id           in     number  default null
6678   ,p_enrt_perd_for_pl_id    in     number  default null
6679   --
6680              -- which dates is R for rate, C for coverage, B for both
6681   ,p_which_dates_cd         in     varchar2      default 'B'
6682              -- will error if Y and an absolute date not found
6683              --   Note: codes must allways be found.
6684   ,p_date_mandatory_flag    in     varchar2      default 'Y'
6685              -- compute_dates_flag is Y for compute dates, N for Don't
6686   ,p_compute_dates_flag     in     varchar2      default 'Y'
6687              --
6688              -- optional for everything
6689              --
6690   ,p_elig_per_elctbl_chc_id in     number  default null
6691   ,p_acty_base_rt_id        in     number  default null
6692   ,p_business_group_id      in     number
6693   ,p_start_date             in     date    default null
6694   ,p_end_date               in     date    default null
6695   ,p_effective_date         in     date
6696   ,p_lf_evt_ocrd_dt         in     date    default null
6697   --
6698   ,p_enrt_cvg_strt_dt          out nocopy date
6699   ,p_enrt_cvg_strt_dt_cd       out nocopy varchar2
6700   ,p_enrt_cvg_strt_dt_rl       out nocopy number
6701   ,p_rt_strt_dt                out nocopy date
6702   ,p_rt_strt_dt_cd             out nocopy varchar2
6703   ,p_rt_strt_dt_rl             out nocopy number
6704   ,p_enrt_cvg_end_dt           out nocopy date
6705   ,p_enrt_cvg_end_dt_cd        out nocopy varchar2
6706   ,p_enrt_cvg_end_dt_rl        out nocopy number
6707   ,p_rt_end_dt                 out nocopy date
6708   ,p_rt_end_dt_cd              out nocopy varchar2
6709   ,p_rt_end_dt_rl              out nocopy number
6710   )
6711 is
6712   --
6713   type t_rec is record
6714     (order_number        number
6715     ,enrt_cvg_strt_dt_cd varchar2(30)
6716     ,enrt_cvg_strt_dt_rl number
6717     ,enrt_cvg_end_dt_cd  varchar2(30)
6718     ,enrt_cvg_end_dt_rl  number
6719     ,rt_strt_dt_cd       varchar2(30)
6720     ,rt_strt_dt_rl       number
6721     ,rt_end_dt_cd        varchar2(30)
6722     ,rt_end_dt_rl        number
6723     );
6724   --
6725   type t_tab is table of t_rec index by binary_integer;
6726   --
6727   l_proc                  varchar2(72) := g_package||'.rate_and_coverage_dates';
6728   --
6729   l_union_set             t_tab;
6730   --
6731   l_found                 boolean;
6732   l_done                  boolean:=FALSE;
6733   l_enrt_cvg_strt_dt      date;
6734   l_enrt_cvg_strt_dt_cd   varchar2(30);
6735   l_enrt_cvg_strt_dt_rl   number;
6736   l_rt_strt_dt            date;
6737   l_rt_strt_dt_cd         varchar2(30);
6738   l_rt_strt_dt_rl         number;
6739   l_enrt_cvg_end_dt       date;
6740   l_enrt_cvg_end_dt_cd    varchar2(30);
6741   l_enrt_cvg_end_dt_rl    number;
6742   l_rt_end_dt             date;
6743   l_rt_end_dt_cd          varchar2(30);
6744   l_rt_end_dt_rl          number;
6745   l_pgm_id                number;
6746   l_pl_id                 number;
6747   l_enrt_perd_id          number;
6748   l_lee_rsn_id            number;
6749   l_oipl_id               number;
6750   l_per_in_ler_id         number;
6751   l_person_id             number;
6752   l_pass_cvg_end_dt       date;
6753   l_effective_date        date;
6754   l_plip_id               number;
6755   l_ptip_id               number;
6756   l_enrt_perd_for_pl_id   number;
6757   l_unionele_num          pls_integer;
6758   l_unionmaxele_num       pls_integer;
6759   l_pass_cvg_strt_dt      date;
6760   l_fonm_cvg_strt_dt      date;
6761   --
6762   -- Bug No 3965571
6763   --
6764   l_pln_name               ben_pl_f.name%type;
6765   l_pgm_name              ben_pgm_f.name%type;
6766   l_opt_name               ben_opt_f.name%type;
6767   --
6768   -- Cursor declaration.
6769   --
6770   -- Scheduled enrolment for plans in programs
6771   --
6772   cursor c_sched_for_plip
6773     (c_effective_date in date
6774     ,c_pgm_id         in number
6775     ,c_ptip_id        in number
6776     ,c_pl_id          in number
6777     ,c_plip_id        in number
6778     ,c_enrt_perd_id   in number
6779     ,c_epfp_id        in number
6780     ,c_per_in_ler_id  in number -- bug 4356591
6781     )
6782   is
6783     select  1 order_number,
6784             epp.enrt_cvg_strt_dt_cd,
6785             epp.enrt_cvg_strt_dt_rl,
6786             epp.enrt_cvg_end_dt_cd,
6787             epp.enrt_cvg_end_dt_rl,
6788             epp.rt_strt_dt_cd,
6789             epp.rt_strt_dt_rl,
6790             epp.rt_end_dt_cd,
6791             epp.rt_end_dt_rl
6792     from    ben_enrt_perd_for_pl_f epp
6793     where   epp.ENRT_PERD_FOR_PL_ID = c_epfp_id
6794     and     c_effective_date
6795       between epp.effective_start_date and epp.effective_end_date
6796    union
6797 -- Bug # 4356591
6798 -- in case of a plip. if the enrt_perd details are defined at the plan level
6799 -- then details are selected by this select. The enrt_perd_id stored in
6800 -- ben_pil_elctbl_chc is of the pgm or the first plan defined in the program
6801    select  2 order_number,
6802             epd.enrt_cvg_strt_dt_cd,
6803             epd.enrt_cvg_strt_dt_rl,
6804             epd.enrt_cvg_end_dt_cd,
6805             epd.enrt_cvg_end_dt_rl,
6806             epd.rt_strt_dt_cd,
6807             epd.rt_strt_dt_rl,
6808             epd.rt_end_dt_cd,
6809             epd.rt_end_dt_rl
6810    from     ben_popl_enrt_typ_cycl_f popl,
6811 	    ben_enrt_perd epd,
6812 	    ben_per_in_ler pil
6813     where   popl.pl_id = c_pl_id
6814     and     epd.popl_enrt_typ_cycl_id = popl.popl_enrt_typ_cycl_id
6815     and     pil.per_in_ler_id = c_per_in_ler_id
6816     and     epd.asnd_lf_evt_dt = pil.lf_evt_ocrd_dt /* removed join btw PIL and LER instead made join btw epd $ pil.*/
6817     and     c_effective_date between popl.effective_start_date and popl.effective_end_date
6818     and     (    enrt_cvg_strt_dt_cd is not null
6819              and enrt_cvg_end_dt_cd is not null
6820              and rt_strt_dt_cd is not null
6821              and rt_end_dt_cd is not null )
6822  -- end 4356591
6823    union
6824   -- Bug # 4356591
6825   -- If the enrt_perd details defined at program level and not at plan level
6826   -- then details are selected by this select.
6827     select  3 order_number,
6828             epd.enrt_cvg_strt_dt_cd,
6829             epd.enrt_cvg_strt_dt_rl,
6830             epd.enrt_cvg_end_dt_cd,
6831             epd.enrt_cvg_end_dt_rl,
6832             epd.rt_strt_dt_cd,
6833             epd.rt_strt_dt_rl,
6834             epd.rt_end_dt_cd,
6835             epd.rt_end_dt_rl
6836     from    ben_popl_enrt_typ_cycl_f popl, -- start 4356591
6837 	    ben_enrt_perd epd,
6838 	    ben_per_in_ler pil
6839     where   popl.pgm_id = c_pgm_id
6840     and     epd.popl_enrt_typ_cycl_id = popl.popl_enrt_typ_cycl_id
6841     and     pil.per_in_ler_id = c_per_in_ler_id
6842     and     epd.asnd_lf_evt_dt = pil.lf_evt_ocrd_dt /* removed join btw PIL and LER instead made join btw epd $ pil.*/
6843     and     c_effective_date between popl.effective_start_date and popl.effective_end_date
6844     and     (    enrt_cvg_strt_dt_cd is not null
6845              and enrt_cvg_end_dt_cd is not null
6846              and rt_strt_dt_cd is not null
6847              and rt_end_dt_cd is not null ) -- end 4356591
6848    union
6849     select  4 order_number,
6850             plp.enrt_cvg_strt_dt_cd,
6851             plp.enrt_cvg_strt_dt_rl,
6852             plp.enrt_cvg_end_dt_cd,
6853             plp.enrt_cvg_end_dt_rl,
6854             plp.rt_strt_dt_cd,
6855             plp.rt_strt_dt_rl,
6856             plp.rt_end_dt_cd,
6857             plp.rt_end_dt_rl
6858     from    ben_plip_f plp
6859     where   plp.plip_id=c_plip_id
6860     and     c_effective_date
6861       between plp.effective_start_date and plp.effective_end_date
6862   union
6863     select  5 order_number,
6864             pln.enrt_cvg_strt_dt_cd,
6865             pln.enrt_cvg_strt_dt_rl,
6866             pln.enrt_cvg_end_dt_cd,
6867             pln.enrt_cvg_end_dt_rl,
6868             pln.rt_strt_dt_cd,
6869             pln.rt_strt_dt_rl,
6870             pln.rt_end_dt_cd,
6871             pln.rt_end_dt_rl
6872     from    ben_pl_f pln
6873     where   pln.pl_id=c_pl_id
6874     and     c_effective_date
6875       between pln.effective_start_date and pln.effective_end_date
6876   union
6877     select  6 order_number,
6878             ptip.enrt_cvg_strt_dt_cd,
6879             ptip.enrt_cvg_strt_dt_rl,
6880             ptip.enrt_cvg_end_dt_cd,
6881             ptip.enrt_cvg_end_dt_rl,
6882             ptip.rt_strt_dt_cd,
6883             ptip.rt_strt_dt_rl,
6884             ptip.rt_end_dt_cd,
6885             ptip.rt_end_dt_rl
6886     from    ben_ptip_f ptip
6887     where   ptip.ptip_id=c_ptip_id
6888     and     c_effective_date
6889       between ptip.effective_start_date and ptip.effective_end_date
6890   union
6891     select  7 order_number,
6892             pgm.enrt_cvg_strt_dt_cd,
6893             pgm.enrt_cvg_strt_dt_rl,
6894             pgm.enrt_cvg_end_dt_cd,
6895             pgm.enrt_cvg_end_dt_rl,
6896             pgm.rt_strt_dt_cd,
6897             pgm.rt_strt_dt_rl,
6898             pgm.rt_end_dt_cd,
6899             pgm.rt_end_dt_rl
6900     from    ben_pgm_f pgm
6901     where   pgm.pgm_id = c_pgm_id
6902     and     c_effective_date
6903       between pgm.effective_start_date and pgm.effective_end_date
6904       order by 1; -- bug 5717428
6905   --
6906   -- Scheduled enrolment for plans not in programs
6907   --
6908   cursor c_sched_for_pl_nip is
6909     select  '2' order_number,
6910             enrt_cvg_strt_dt_cd,
6911             enrt_cvg_strt_dt_rl,
6912             enrt_cvg_end_dt_cd,
6913             enrt_cvg_end_dt_rl,
6914             rt_strt_dt_cd,
6915             rt_strt_dt_rl,
6916             rt_end_dt_cd,
6917             rt_end_dt_rl
6918     from    ben_enrt_perd
6919     where   enrt_perd_id=l_enrt_perd_id and
6920             business_group_id =p_business_group_id
6921   union
6922     select  '4' order_number,
6923             enrt_cvg_strt_dt_cd,
6924             enrt_cvg_strt_dt_rl,
6925             enrt_cvg_end_dt_cd,
6926             enrt_cvg_end_dt_rl,
6927             rt_strt_dt_cd,
6928             rt_strt_dt_rl,
6929             rt_end_dt_cd,
6930             rt_end_dt_rl
6931     from    ben_pl_f
6932     where   pl_id=l_pl_id and
6933             business_group_id =p_business_group_id and
6934             nvl(p_lf_evt_ocrd_dt,p_effective_date) between
6935               effective_start_date and effective_end_date
6936   order by 1;
6937   --
6938   -- Life event enrolment for plans in programs
6939   --
6940   cursor c_life_for_plip
6941     (c_effective_date    in date
6942     ,c_epfp_id           in number
6943     ,c_lee_rsn_id        in number
6944     ,c_plip_id           in number
6945     ,c_pl_id             in number
6946     ,c_ptip_id           in number
6947     ,c_pgm_id            in number
6948     ,c_per_in_ler_id     in number
6949     )
6950   is
6951     select  1 order_number,
6952             epp.enrt_cvg_strt_dt_cd,
6953             epp.enrt_cvg_strt_dt_rl,
6954             epp.enrt_cvg_end_dt_cd,
6955             epp.enrt_cvg_end_dt_rl,
6956             epp.rt_strt_dt_cd,
6957             epp.rt_strt_dt_rl,
6958             epp.rt_end_dt_cd,
6959             epp.rt_end_dt_rl
6960     from    ben_enrt_perd_for_pl_f epp
6961     where   epp.ENRT_PERD_FOR_PL_ID=c_epfp_id
6962     and     c_effective_date
6963     between epp.effective_start_date and epp.effective_end_date
6964     and     (    enrt_cvg_strt_dt_cd is not null
6965              and enrt_cvg_end_dt_cd is not null
6966              and rt_strt_dt_cd is not null
6967              and rt_end_dt_cd is not null
6968             )
6969    union
6970 -- Bug # 2527347
6971 -- in case of a plip. if the lee_rsn details are defined at the plan level
6972 -- then details are selected by this select. The lee_rsn_id stored in
6973 -- ben_pil_elctbl_chc is of the pgm or the first plan defined in the program
6974     select  2 order_number,
6975             lee.enrt_cvg_strt_dt_cd,
6976             lee.enrt_cvg_strt_dt_rl,
6977             lee.enrt_cvg_end_dt_cd,
6978             lee.enrt_cvg_end_dt_rl,
6979             lee.rt_strt_dt_cd,
6980             lee.rt_strt_dt_rl,
6981             lee.rt_end_dt_cd,
6982             lee.rt_end_dt_rl
6983     from    ben_popl_enrt_typ_cycl_f popl,
6984 	    ben_lee_rsn_f lee,
6985 	    ben_ler_f     ler,
6986 	    ben_per_in_ler pil
6987     where   popl.pl_id = c_pl_id
6988     and     c_effective_date between popl.effective_start_date and popl.effective_end_date
6989     and     lee.popl_enrt_typ_cycl_id = popl.popl_enrt_typ_cycl_id
6990     and     pil.per_in_ler_id = c_per_in_ler_id
6991     and     ler.ler_id = pil.ler_id
6992     and     lee.ler_id = ler.ler_id
6993     and     c_effective_date between lee.effective_start_date and lee.effective_end_date
6994     and     c_effective_date between popl.effective_start_date and popl.effective_end_date
6995     and     (    enrt_cvg_strt_dt_cd is not null
6996              and enrt_cvg_end_dt_cd is not null
6997              and rt_strt_dt_cd is not null
6998              and rt_end_dt_cd is not null )
6999 -- end bug # 2527347
7000    union
7001    -- Bug # 4356591
7002    -- If the lee_rsn_details defined at program level and not at plan level
7003    -- then details are selected by this select.
7004     select  3 order_number,
7005             lee.enrt_cvg_strt_dt_cd,
7006             lee.enrt_cvg_strt_dt_rl,
7007             lee.enrt_cvg_end_dt_cd,
7008             lee.enrt_cvg_end_dt_rl,
7009             lee.rt_strt_dt_cd,
7010             lee.rt_strt_dt_rl,
7011             lee.rt_end_dt_cd,
7012             lee.rt_end_dt_rl
7013     from    ben_popl_enrt_typ_cycl_f popl, -- start 4356591
7014 	    ben_lee_rsn_f lee,
7015 	    ben_per_in_ler pil
7016     where   popl.pgm_id = c_pgm_id
7017     and     c_effective_date between popl.effective_start_date and popl.effective_end_date
7018     and     lee.popl_enrt_typ_cycl_id = popl.popl_enrt_typ_cycl_id
7019     and     pil.per_in_ler_id = c_per_in_ler_id
7020     and     lee.ler_id = pil.ler_id
7021     and     c_effective_date between lee.effective_start_date and lee.effective_end_date
7022     and     c_effective_date between popl.effective_start_date and popl.effective_end_date -- end 4356591
7023     and     (    enrt_cvg_strt_dt_cd is not null
7024              and enrt_cvg_end_dt_cd is not null
7025              and rt_strt_dt_cd is not null
7026              and rt_end_dt_cd is not null
7027             )
7028   union
7029     select  4 order_number,
7030             plp.enrt_cvg_strt_dt_cd,
7031             plp.enrt_cvg_strt_dt_rl,
7032             plp.enrt_cvg_end_dt_cd,
7033             plp.enrt_cvg_end_dt_rl,
7034             plp.rt_strt_dt_cd,
7035             plp.rt_strt_dt_rl,
7036             plp.rt_end_dt_cd,
7037             plp.rt_end_dt_rl
7038     from    ben_plip_f plp
7039     where   plp.plip_id=c_plip_id
7040     and     c_effective_date
7041       between plp.effective_start_date and plp.effective_end_date
7042     and     (    enrt_cvg_strt_dt_cd is not null
7043              and enrt_cvg_end_dt_cd is not null
7044              and rt_strt_dt_cd is not null
7045              and rt_end_dt_cd is not null
7046             )
7047   union
7048     select  5 order_number,
7049             pln.enrt_cvg_strt_dt_cd,
7050             pln.enrt_cvg_strt_dt_rl,
7051             pln.enrt_cvg_end_dt_cd,
7052             pln.enrt_cvg_end_dt_rl,
7053             pln.rt_strt_dt_cd,
7054             pln.rt_strt_dt_rl,
7055             pln.rt_end_dt_cd,
7056             pln.rt_end_dt_rl
7057     from    ben_pl_f pln
7058     where   pln.pl_id=c_pl_id
7059     and     c_effective_date
7060       between pln.effective_start_date and pln.effective_end_date
7061     and     (    enrt_cvg_strt_dt_cd is not null
7062              and enrt_cvg_end_dt_cd is not null
7063              and rt_strt_dt_cd is not null
7064              and rt_end_dt_cd is not null
7065             )
7066   union
7067     select  6 order_number,
7068             ptip.enrt_cvg_strt_dt_cd,
7069             ptip.enrt_cvg_strt_dt_rl,
7070             ptip.enrt_cvg_end_dt_cd,
7071             ptip.enrt_cvg_end_dt_rl,
7072             ptip.rt_strt_dt_cd,
7073             ptip.rt_strt_dt_rl,
7074             ptip.rt_end_dt_cd,
7075             ptip.rt_end_dt_rl
7076     from    ben_ptip_f ptip
7077     where   ptip.ptip_id=c_ptip_id
7078     and     c_effective_date
7079       between ptip.effective_start_date and ptip.effective_end_date
7080     and     (    enrt_cvg_strt_dt_cd is not null
7081              and enrt_cvg_end_dt_cd is not null
7082              and rt_strt_dt_cd is not null
7083              and rt_end_dt_cd is not null
7084             )
7085   union
7086     select  7 order_number,
7087             pgm.enrt_cvg_strt_dt_cd,
7088             pgm.enrt_cvg_strt_dt_rl,
7089             pgm.enrt_cvg_end_dt_cd,
7090             pgm.enrt_cvg_end_dt_rl,
7091             pgm.rt_strt_dt_cd,
7092             pgm.rt_strt_dt_rl,
7093             pgm.rt_end_dt_cd,
7094             pgm.rt_end_dt_rl
7095     from    ben_pgm_f pgm
7096     where   pgm.pgm_id=c_pgm_id
7097     and     c_effective_date
7098       between pgm.effective_start_date and pgm.effective_end_date
7099     and     (    enrt_cvg_strt_dt_cd is not null
7100              and enrt_cvg_end_dt_cd is not null
7101              and rt_strt_dt_cd is not null
7102              and rt_end_dt_cd is not null
7103             )
7104     order by 1 ; --  Bug 2122643
7105   --
7106   -- Life event enrolment for plans not in programs
7107   --
7108   cursor c_life_for_pl_nip is
7109     select  '2' order_number,
7110             enrt_cvg_strt_dt_cd,
7111             enrt_cvg_strt_dt_rl,
7112             enrt_cvg_end_dt_cd,
7113             enrt_cvg_end_dt_rl,
7114             rt_strt_dt_cd,
7115             rt_strt_dt_rl,
7116             rt_end_dt_cd,
7117             rt_end_dt_rl
7118     from    ben_lee_rsn_f
7119     where   lee_rsn_id=l_lee_rsn_id and
7120             business_group_id =p_business_group_id and
7121             nvl(p_lf_evt_ocrd_dt,p_effective_date) between
7122               effective_start_date and effective_end_date
7123   union
7124     select  '4' order_number,
7125             enrt_cvg_strt_dt_cd,
7126             enrt_cvg_strt_dt_rl,
7127             enrt_cvg_end_dt_cd,
7128             enrt_cvg_end_dt_rl,
7129             rt_strt_dt_cd,
7130             rt_strt_dt_rl,
7131             rt_end_dt_cd,
7132             rt_end_dt_rl
7133     from    ben_pl_f
7134     where   pl_id=l_pl_id and
7135             business_group_id =p_business_group_id and
7136             nvl(p_lf_evt_ocrd_dt,p_effective_date) between
7137               effective_start_date and effective_end_date
7138   order by 1;
7139   --
7140   l_rec c_life_for_plip%rowtype;
7141   --
7142   -- get elig_per_elctbl_chc_info
7143   --
7144   cursor c_epe_info is
7145     select
7146         epe.pl_id,
7147         epe.pgm_id,
7148         pel.enrt_perd_id,
7149         pel.lee_rsn_id,
7150         epe.oipl_id,
7151         epe.per_in_ler_id,
7152         pil.person_id,
7153         epe.fonm_cvg_strt_dt
7154     from
7155         ben_elig_per_elctbl_chc epe,
7156         ben_pil_elctbl_chc_popl pel,
7157         ben_per_in_ler pil
7158     where
7159         epe.elig_per_elctbl_chc_id=p_elig_per_elctbl_chc_id and
7160         pel.pil_elctbl_chc_popl_id = epe.pil_elctbl_chc_popl_id and
7161         pil.per_in_ler_id=epe.per_in_ler_id;
7162         -- removed these joins, as a run was getting a null BG id passed in
7163         -- and if we have the chc id we shouldn't need bg id:
7164         --pel.business_group_id =p_business_group_id and
7165         --pil.business_group_id =p_business_group_id
7166         --epe.business_group_id =p_business_group_id and
7167   --
7168   cursor c_gptip_id
7169     (c_pl_id          in number
7170     ,c_pgm_id         in number
7171     ,c_effective_date in date
7172     )
7173   is
7174     select ptp.ptip_id
7175     from ben_ptip_f ptp,
7176          ben_pl_f pln
7177     where ptp.pl_typ_id = pln.pl_typ_id
7178     and   c_effective_date
7179       between pln.effective_start_date and pln.effective_end_date
7180     and   ptp.pgm_id    = c_pgm_id
7181     and   pln.pl_id     = c_pl_id
7182     and   c_effective_date
7183       between ptp.effective_start_date and ptp.effective_end_date;
7184 
7185   --
7186   cursor c_gplip_id
7187     (c_pl_id          in number
7188     ,c_pgm_id         in number
7189     ,c_effective_date in date
7190     )
7191   is
7192     select plp.plip_id
7193     from ben_plip_f plp
7194     where plp.pgm_id    = c_pgm_id
7195     and   plp.pl_id     = c_pl_id
7196     and   c_effective_date
7197       between plp.effective_start_date and plp.effective_end_date;
7198 
7199   cursor c_glee_epfp_id
7200     (c_pl_id          in number
7201     ,c_lee_rsn_id     in number
7202     ,c_effective_date in date
7203     )
7204   is
7205     select epfp.ENRT_PERD_FOR_PL_ID
7206     from ben_enrt_perd_for_pl_f epfp
7207     where epfp.lee_rsn_id = c_lee_rsn_id
7208     and   epfp.pl_id      = c_pl_id
7209     and   c_effective_date
7210       between epfp.effective_start_date and epfp.effective_end_date;
7211 
7212   cursor c_genp_epfp_id
7213     (c_pl_id          in number
7214     ,c_enrt_perd_id   in number
7215     ,c_effective_date in date
7216     )
7217   is
7218     select epfp.ENRT_PERD_FOR_PL_ID
7219     from ben_enrt_perd_for_pl_f epfp
7220     where epfp.enrt_perd_id = c_enrt_perd_id
7221     and   epfp.pl_id        = c_pl_id
7222     and   c_effective_date
7223       between epfp.effective_start_date and epfp.effective_end_date;
7224 
7225   cursor c_gepp_dets
7226     (c_epfp_id        in number
7227     ,c_effective_date in date
7228     )
7229   is
7230     select 1 order_number,
7231             epp.enrt_cvg_strt_dt_cd,
7232             epp.enrt_cvg_strt_dt_rl,
7233             epp.enrt_cvg_end_dt_cd,
7234             epp.enrt_cvg_end_dt_rl,
7235             epp.rt_strt_dt_cd,
7236             epp.rt_strt_dt_rl,
7237             epp.rt_end_dt_cd,
7238             epp.rt_end_dt_rl
7239     from    ben_enrt_perd_for_pl_f epp
7240     where   epp.ENRT_PERD_FOR_PL_ID=c_epfp_id
7241     and     c_effective_date
7242     between epp.effective_start_date and epp.effective_end_date
7243     and     (    enrt_cvg_strt_dt_cd is not null
7244              and enrt_cvg_end_dt_cd is not null
7245              and rt_strt_dt_cd is not null
7246              and rt_end_dt_cd is not null
7247             );
7248 
7249   l_epp_rec c_gepp_dets%rowtype;
7250 
7251   cursor c_gleersn_dets
7252     (c_lee_rsn_id     in number
7253     ,c_effective_date in date
7254     )
7255   is
7256     select 2 order_number,
7257             lee.enrt_cvg_strt_dt_cd,
7258             lee.enrt_cvg_strt_dt_rl,
7259             lee.enrt_cvg_end_dt_cd,
7260             lee.enrt_cvg_end_dt_rl,
7261             lee.rt_strt_dt_cd,
7262             lee.rt_strt_dt_rl,
7263             lee.rt_end_dt_cd,
7264             lee.rt_end_dt_rl
7265     from    ben_lee_rsn_f lee
7266     where   lee.lee_rsn_id=c_lee_rsn_id
7267     and     c_effective_date
7268       between lee.effective_start_date and lee.effective_end_date
7269     and     (    enrt_cvg_strt_dt_cd is not null
7270              and enrt_cvg_end_dt_cd is not null
7271              and rt_strt_dt_cd is not null
7272              and rt_end_dt_cd is not null
7273             );
7274 
7275   --
7276   -- Bug No 3965571
7277   --
7278   cursor c_pln
7279   (c_pl_id     in number
7280   ,c_effective_date in date)
7281   is
7282     select pln.name
7283         from ben_pl_f pln
7284 	   where pln.pl_id = c_pl_id and pln.business_group_id = p_business_group_id
7285                      and c_effective_date between pln.effective_start_date and pln.effective_end_date;
7286 
7287   cursor c_pgm
7288   (c_pgm_id     in number
7289   ,c_effective_date in date)
7290   is
7291     select pgm.name
7292         from ben_pgm_f pgm
7293 	   where pgm.pgm_id = c_pgm_id and pgm.business_group_id = p_business_group_id
7294                      and c_effective_date between pgm.effective_start_date and pgm.effective_end_date;
7295 
7296   cursor c_opt
7297   (c_oipl_id   in number
7298   ,c_effective_date in date)
7299   is
7300    select opt.name
7301         from ben_oipl_f oipl, ben_opt_f opt
7302           where oipl.business_group_id=p_business_group_id
7303                    and oipl.opt_id = opt.opt_id and oipl.oipl_id = c_oipl_id
7304 		   and c_effective_date between oipl.effective_start_date and oipl.effective_end_date
7305 		   and c_effective_date between opt.effective_start_date and opt.effective_end_date;
7306 --
7307 -- End 3965571
7308 --
7309 
7310   l_leersn_rec c_gleersn_dets%rowtype;
7311   l_pil_row    ben_per_in_ler%rowtype;
7312 
7313 
7314 begin
7315   --
7316   g_debug := hr_utility.debug_enabled;
7317   if g_debug then
7318     hr_utility.set_location ('Entering '||l_proc,10);
7319   end if;
7320 /*
7321   if p_which_dates_cd = 'R' then
7322     --hr_utility.set_location('p_cache_mode               '|| p_cache_mode             , 1687);
7323     if g_debug then
7324       hr_utility.set_location('p_per_in_ler_id             '|| p_per_in_ler_id          , 1687);
7325     end if;
7326     if g_debug then
7327       hr_utility.set_location('p_person_id              	 '|| p_person_id              , 1687);
7328     end if;
7329     if g_debug then
7330       hr_utility.set_location('p_pgm_id                 	 '|| p_pgm_id                 , 1687);
7331     end if;
7332     if g_debug then
7333       hr_utility.set_location('p_pl_id                  	 '|| p_pl_id                  , 1687);
7334     end if;
7335     if g_debug then
7336       hr_utility.set_location('p_oipl_id                	 '|| p_oipl_id                , 1687);
7337     end if;
7338     if g_debug then
7339       hr_utility.set_location('p_par_ptip_id            	 '|| p_par_ptip_id            , 1687);
7340     end if;
7341     if g_debug then
7342       hr_utility.set_location('p_lee_rsn_id             	 '|| p_lee_rsn_id             , 1687);
7343     end if;
7344     if g_debug then
7345       hr_utility.set_location('p_enrt_perd_id           	 '|| p_enrt_perd_id           , 1687);
7346     end if;
7347     if g_debug then
7348       hr_utility.set_location('p_enrt_perd_for_pl_id    	 '|| p_enrt_perd_for_pl_id    , 1687);
7349     end if;
7350     if g_debug then
7351       hr_utility.set_location('p_which_dates_cd         	 '|| p_which_dates_cd         , 1687);
7352     end if;
7353     if g_debug then
7354       hr_utility.set_location('p_date_mandatory_flag    	 '|| p_date_mandatory_flag    , 1687);
7355     end if;
7356     if g_debug then
7357       hr_utility.set_location('p_compute_dates_flag     	 '|| p_compute_dates_flag     , 1687);
7358     end if;
7359     if g_debug then
7360       hr_utility.set_location('p_elig_per_elctbl_chc_id 	 '|| p_elig_per_elctbl_chc_id , 1687);
7361     end if;
7362     if g_debug then
7363       hr_utility.set_location('p_acty_base_rt_id        	 '|| p_acty_base_rt_id        , 1687);
7364     end if;
7365     if g_debug then
7366       hr_utility.set_location('p_business_group_id      	 '|| p_business_group_id      , 1687);
7367     end if;
7368     if g_debug then
7369       hr_utility.set_location('p_start_date             	 '|| p_start_date             , 1687);
7370     end if;
7371     if g_debug then
7372       hr_utility.set_location('p_end_date               	 '|| p_end_date               , 1687);
7373     end if;
7374     if g_debug then
7375       hr_utility.set_location('p_effective_date         	 '|| p_effective_date         , 1687);
7376     end if;
7377     if g_debug then
7378       hr_utility.set_location('p_lf_evt_ocrd_dt         	 '|| p_lf_evt_ocrd_dt         , 1687);
7379     end if;
7380   end if;
7381 */
7382   --
7383 
7384 
7385   l_effective_date := nvl(p_lf_evt_ocrd_dt,p_effective_date);
7386 
7387   hr_utility.set_location('l_effective_date ' || l_effective_date, 2080);
7388   --
7389   -- Get the necessary info to start processing
7390   --
7391   if p_cache_mode
7392   then
7393     --
7394     l_person_id           := p_person_id;
7395     l_per_in_ler_id       := p_per_in_ler_id;
7396     l_pgm_id              := p_pgm_id;
7397     l_ptip_id             := p_par_ptip_id;
7398     l_pl_id               := p_pl_id;
7399     l_plip_id             := p_par_plip_id;
7400     l_oipl_id             := p_oipl_id;
7401     l_enrt_perd_id        := p_enrt_perd_id;
7402     l_lee_rsn_id          := p_lee_rsn_id;
7403     l_enrt_perd_for_pl_id := p_enrt_perd_for_pl_id;
7404     --l_fonm_cvg_strt_dt    := ben_manage_life_events.g_fonm_cvg_strt_dt ;
7405     --
7406   elsif p_elig_per_elctbl_chc_id is not null then
7407     --
7408     if g_debug then
7409       hr_utility.set_location(l_proc , 20);
7410     end if;
7411     --
7412     -- get from elig_per_elctbl_chc
7413     --   Note: Don't need all the args just the ones for direct use by
7414     --         this module, let determine date take care of itself
7415     --
7416     open c_epe_info;
7417     fetch c_epe_info into
7418       l_pl_id,
7419       l_pgm_id,
7420       l_enrt_perd_id,
7421       l_lee_rsn_id,
7422       l_oipl_id,
7423       l_per_in_ler_id,
7424       l_person_id,
7425       l_fonm_cvg_strt_dt
7426     ;
7427     --
7428     if g_debug then
7429       hr_utility.set_location('l_oipl_id '||l_oipl_id,19);
7430     end if;
7431     if c_epe_info%notfound then
7432       close c_epe_info;
7433       fnd_message.set_name('BEN','BEN_91457_ELCTBL_CHC_NOT_FOUND');
7434       --fnd_message.set_token('ID', to_char(p_business_group_id));
7435       fnd_message.set_token('ID', to_char(p_elig_per_elctbl_chc_id));
7436       fnd_message.set_token('PROC',l_proc);
7437       fnd_message.raise_error;
7438     end if;
7439     if g_debug then
7440       hr_utility.set_location(l_proc , 40);
7441     end if;
7442 
7443     close c_epe_info;
7444   else
7445     if g_debug then
7446       hr_utility.set_location(l_proc , 50);
7447     end if;
7448     --
7449     -- use args
7450     --
7451     l_pl_id:=p_pl_id;
7452     l_pgm_id:=p_pgm_id;
7453     l_enrt_perd_id:=p_enrt_perd_id;
7454     l_lee_rsn_id:=p_lee_rsn_id;
7455     l_oipl_id:=p_oipl_id;
7456     l_per_in_ler_id:=p_per_in_ler_id;
7457     l_person_id:=p_person_id;
7458     --
7459   end if;
7460   --
7461   if g_debug then
7462     hr_utility.set_location(l_proc , 60);
7463     hr_utility.set_location('FONM FLAG ' || ben_manage_life_events.fonm   , 60);
7464     hr_utility.set_location('FONM gc date  ' ||  ben_manage_life_events.g_fonm_cvg_strt_dt  , 60);
7465     hr_utility.set_location('FONM gr date  ' ||  ben_manage_life_events.g_fonm_rt_strt_dt  , 60);
7466     hr_utility.set_location('FONM epe date  ' || l_fonm_cvg_strt_dt  , 60);
7467     hr_utility.set_location('which date code   ' ||  p_which_dates_cd   , 60);
7468     hr_utility.set_location('plan    ' ||  p_pl_id    , 60);
7469 
7470   end if;
7471 
7472 
7473   --
7474   -- Get the codes and rules
7475   --
7476   -- Open and fetch first row
7477   --
7478   if l_pgm_id is not null then
7479     --
7480     -- Get the ptip id for the pl and pgm
7481     --
7482     if l_ptip_id is null then
7483     --  hr_utility.set_location(' c_gptip_id: '||l_proc , 140);
7484       open c_gptip_id
7485         (c_pl_id          => l_pl_id
7486         ,c_pgm_id         => l_pgm_id
7487         ,c_effective_date => l_effective_date
7488         );
7489       fetch c_gptip_id into l_ptip_id;
7490       close c_gptip_id;
7491       if g_debug then
7492         hr_utility.set_location(' Dn c_gptip_id: '||l_proc , 140);
7493       end if;
7494     else
7495       l_ptip_id := p_par_ptip_id;
7496     end if;
7497     --
7498     -- Get the plip id for the pl and pgm
7499     --
7500     if l_plip_id is null then
7501       if g_debug then
7502         hr_utility.set_location(' c_gplip_id: '||l_proc , 140);
7503       end if;
7504       open c_gplip_id
7505         (c_pl_id          => l_pl_id
7506         ,c_pgm_id         => l_pgm_id
7507         ,c_effective_date => l_effective_date
7508         );
7509       fetch c_gplip_id into l_plip_id;
7510       close c_gplip_id;
7511       if g_debug then
7512         hr_utility.set_location(' Dn c_gplip_id: '||l_proc , 140);
7513       end if;
7514     else
7515       l_plip_id := p_par_plip_id;
7516     end if;
7517     --
7518     if l_enrt_perd_id is not null then
7519       if g_debug then
7520         hr_utility.set_location(' Op c_SCFP: '||l_proc , 140);
7521       end if;
7522       --
7523       -- Check if the enrt perd for pl id is passed in
7524       --
7525       if l_enrt_perd_for_pl_id is null then
7526         --
7527         open c_genp_epfp_id
7528           (c_pl_id          => l_pl_id
7529           ,c_enrt_perd_id   => l_enrt_perd_id
7530           ,c_effective_date => l_effective_date
7531           );
7532         fetch c_genp_epfp_id into l_enrt_perd_for_pl_id;
7533         close c_genp_epfp_id;
7534         --
7535       else
7536         --
7537         l_enrt_perd_for_pl_id := p_enrt_perd_for_pl_id;
7538         --
7539       end if;
7540       --
7541       open c_sched_for_plip
7542         (c_effective_date => l_effective_date
7543         ,c_pgm_id         => l_pgm_id
7544         ,c_ptip_id        => l_ptip_id
7545         ,c_pl_id          => l_pl_id
7546         ,c_plip_id        => l_plip_id
7547         ,c_enrt_perd_id   => l_enrt_perd_id
7548         ,c_epfp_id        => l_enrt_perd_for_pl_id
7549 	,c_per_in_ler_id  => l_per_in_ler_id      -- bug 4356591
7550         );
7551       fetch c_sched_for_plip into l_rec;
7552       if g_debug then
7553         hr_utility.set_location(' Dn Fet c_SCFP: '||l_proc , 140);
7554 	hr_utility.set_location(' l_rec.enrt_cvg_strt_dt_cd : '|| l_rec.enrt_cvg_strt_dt_cd , 140);
7555         hr_utility.set_location(' l_rec.enrt_cvg_end_dt_cd : '|| l_rec.enrt_cvg_end_dt_cd , 140);
7556       end if;
7557       l_found:=c_sched_for_plip%found;
7558     else
7559       --
7560       -- do life event plip processing
7561       -- also used for unrestricted plip
7562       --
7563       if g_debug then
7564         hr_utility.set_location(' Op c_glee_epfp_id: '||l_proc , 150);
7565       end if;
7566       --
7567       if l_enrt_perd_for_pl_id is null then
7568         --
7569         open c_glee_epfp_id
7570           (c_pl_id          => l_pl_id
7571           ,c_lee_rsn_id     => l_lee_rsn_id
7572           ,c_effective_date => l_effective_date
7573           );
7574         fetch c_glee_epfp_id into l_enrt_perd_for_pl_id;
7575         close c_glee_epfp_id;
7576         --
7577       else
7578         --
7579         l_enrt_perd_for_pl_id := p_enrt_perd_for_pl_id;
7580         --
7581       end if;
7582       --
7583    if g_debug then
7584      hr_utility.set_location(' Dn c_glee_epfp_id: '||l_proc , 150);
7585    end if;
7586       --
7587       -- Fetch the first row from the union cache
7588       --
7589       if p_pgm_row.pgm_id is not null
7590         and p_ptip_row.ptip_id is not null
7591         and p_plip_row.plip_id is not null
7592         and p_pl_row.pl_id is not null
7593       then
7594 
7595       if g_debug then
7596         hr_utility.set_location(' Building the union cache ', 1687);
7597       end if;
7598         --
7599         -- Build up union set
7600         --
7601         l_unionele_num := 0;
7602         l_union_set.delete;
7603         --
7604         open c_gepp_dets
7605           (c_epfp_id        => l_enrt_perd_for_pl_id
7606           ,c_effective_date => l_effective_date
7607           );
7608         fetch c_gepp_dets into l_epp_rec;
7609         if c_gepp_dets%found then
7610           --
7611           l_union_set(l_unionele_num).order_number        := 1;
7612           l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd := l_epp_rec.enrt_cvg_strt_dt_cd;
7613           l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl := l_epp_rec.enrt_cvg_strt_dt_rl;
7614           l_union_set(l_unionele_num).enrt_cvg_end_dt_cd  := l_epp_rec.enrt_cvg_end_dt_cd;
7615           l_union_set(l_unionele_num).enrt_cvg_end_dt_rl  := l_epp_rec.enrt_cvg_end_dt_rl;
7616           l_union_set(l_unionele_num).rt_strt_dt_cd       := l_epp_rec.rt_strt_dt_cd;
7617           l_union_set(l_unionele_num).rt_strt_dt_rl       := l_epp_rec.rt_strt_dt_rl;
7618           l_union_set(l_unionele_num).rt_end_dt_cd        := l_epp_rec.rt_end_dt_cd;
7619           l_union_set(l_unionele_num).rt_end_dt_rl        := l_epp_rec.rt_end_dt_rl;
7620           l_unionele_num := l_unionele_num+1;
7621           --
7622         end if;
7623         close c_gepp_dets;
7624         --
7625 if g_debug then
7626   hr_utility.set_location(' l_lee_rsn_id: '||l_lee_rsn_id, 1687);
7627 end if;
7628 
7629         open c_gleersn_dets
7630           (c_lee_rsn_id     => l_lee_rsn_id
7631           ,c_effective_date => l_effective_date
7632           );
7633         fetch c_gleersn_dets into l_leersn_rec;
7634         if c_gleersn_dets%found then
7635           --
7636           l_union_set(l_unionele_num).order_number        := 2;
7637           l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd := l_leersn_rec.enrt_cvg_strt_dt_cd;
7638           l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl := l_leersn_rec.enrt_cvg_strt_dt_rl;
7639           l_union_set(l_unionele_num).enrt_cvg_end_dt_cd  := l_leersn_rec.enrt_cvg_end_dt_cd;
7640           l_union_set(l_unionele_num).enrt_cvg_end_dt_rl  := l_leersn_rec.enrt_cvg_end_dt_rl;
7641           l_union_set(l_unionele_num).rt_strt_dt_cd       := l_leersn_rec.rt_strt_dt_cd;
7642           l_union_set(l_unionele_num).rt_strt_dt_rl       := l_leersn_rec.rt_strt_dt_rl;
7643           l_union_set(l_unionele_num).rt_end_dt_cd        := l_leersn_rec.rt_end_dt_cd;
7644           l_union_set(l_unionele_num).rt_end_dt_rl        := l_leersn_rec.rt_end_dt_rl;
7645           l_unionele_num := l_unionele_num+1;
7646           --
7647         end if;
7648         close c_gleersn_dets;
7649         --
7650         if p_plip_row.plip_id is not null
7651           and (p_plip_row.enrt_cvg_strt_dt_cd is not null
7652               and p_plip_row.enrt_cvg_end_dt_cd is not null
7653               and p_plip_row.rt_strt_dt_cd is not null
7654               and p_plip_row.rt_end_dt_cd is not null
7655               )
7656         then
7657           --
7658           l_union_set(l_unionele_num).order_number        := 3;
7659           l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd := p_plip_row.enrt_cvg_strt_dt_cd;
7660           l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl := p_plip_row.enrt_cvg_strt_dt_rl;
7661           l_union_set(l_unionele_num).enrt_cvg_end_dt_cd  := p_plip_row.enrt_cvg_end_dt_cd;
7662           l_union_set(l_unionele_num).enrt_cvg_end_dt_rl  := p_plip_row.enrt_cvg_end_dt_rl;
7663           l_union_set(l_unionele_num).rt_strt_dt_cd       := p_plip_row.rt_strt_dt_cd;
7664           l_union_set(l_unionele_num).rt_strt_dt_rl       := p_plip_row.rt_strt_dt_rl;
7665           l_union_set(l_unionele_num).rt_end_dt_cd        := p_plip_row.rt_end_dt_cd;
7666           l_union_set(l_unionele_num).rt_end_dt_rl        := p_plip_row.rt_end_dt_rl;
7667           l_unionele_num := l_unionele_num+1;
7668           --
7669         end if;
7670         --
7671         if p_pl_row.pl_id is not null
7672           and (p_pl_row.enrt_cvg_strt_dt_cd is not null
7673               and p_pl_row.enrt_cvg_end_dt_cd is not null
7674               and p_pl_row.rt_strt_dt_cd is not null
7675               and p_pl_row.rt_end_dt_cd is not null
7676               )
7677         then
7678           --
7679           l_union_set(l_unionele_num).order_number        := 4;
7680           l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd := p_pl_row.enrt_cvg_strt_dt_cd;
7681           l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl := p_pl_row.enrt_cvg_strt_dt_rl;
7682           l_union_set(l_unionele_num).enrt_cvg_end_dt_cd  := p_pl_row.enrt_cvg_end_dt_cd;
7683           l_union_set(l_unionele_num).enrt_cvg_end_dt_rl  := p_pl_row.enrt_cvg_end_dt_rl;
7684           l_union_set(l_unionele_num).rt_strt_dt_cd       := p_pl_row.rt_strt_dt_cd;
7685           l_union_set(l_unionele_num).rt_strt_dt_rl       := p_pl_row.rt_strt_dt_rl;
7686           l_union_set(l_unionele_num).rt_end_dt_cd        := p_pl_row.rt_end_dt_cd;
7687           l_union_set(l_unionele_num).rt_end_dt_rl        := p_pl_row.rt_end_dt_rl;
7688           l_unionele_num := l_unionele_num+1;
7689           --
7690         end if;
7691         --
7692         if p_ptip_row.ptip_id is not null
7693           and (p_ptip_row.enrt_cvg_strt_dt_cd is not null
7694               and p_ptip_row.enrt_cvg_end_dt_cd is not null
7695               and p_ptip_row.rt_strt_dt_cd is not null
7696               and p_ptip_row.rt_end_dt_cd is not null
7697               )
7698         then
7699           --
7700           l_union_set(l_unionele_num).order_number        := 5;
7701           l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd := p_ptip_row.enrt_cvg_strt_dt_cd;
7702           l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl := p_ptip_row.enrt_cvg_strt_dt_rl;
7703           l_union_set(l_unionele_num).enrt_cvg_end_dt_cd  := p_ptip_row.enrt_cvg_end_dt_cd;
7704           l_union_set(l_unionele_num).enrt_cvg_end_dt_rl  := p_ptip_row.enrt_cvg_end_dt_rl;
7705           l_union_set(l_unionele_num).rt_strt_dt_cd       := p_ptip_row.rt_strt_dt_cd;
7706           l_union_set(l_unionele_num).rt_strt_dt_rl       := p_ptip_row.rt_strt_dt_rl;
7707           l_union_set(l_unionele_num).rt_end_dt_cd        := p_ptip_row.rt_end_dt_cd;
7708           l_union_set(l_unionele_num).rt_end_dt_rl        := p_ptip_row.rt_end_dt_rl;
7709           l_unionele_num := l_unionele_num+1;
7710           --
7711         end if;
7712         --
7713         if p_pgm_row.pgm_id is not null
7714           and (p_pgm_row.enrt_cvg_strt_dt_cd is not null
7715               and p_pgm_row.enrt_cvg_end_dt_cd is not null
7716               and p_pgm_row.rt_strt_dt_cd is not null
7717               and p_pgm_row.rt_end_dt_cd is not null
7718               )
7719         then
7720           --
7721           l_union_set(l_unionele_num).order_number        := 6;
7722           l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd := p_pgm_row.enrt_cvg_strt_dt_cd;
7723           l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl := p_pgm_row.enrt_cvg_strt_dt_rl;
7724           l_union_set(l_unionele_num).enrt_cvg_end_dt_cd  := p_pgm_row.enrt_cvg_end_dt_cd;
7725           l_union_set(l_unionele_num).enrt_cvg_end_dt_rl  := p_pgm_row.enrt_cvg_end_dt_rl;
7726           l_union_set(l_unionele_num).rt_strt_dt_cd       := p_pgm_row.rt_strt_dt_cd;
7727           l_union_set(l_unionele_num).rt_strt_dt_rl       := p_pgm_row.rt_strt_dt_rl;
7728           l_union_set(l_unionele_num).rt_end_dt_cd        := p_pgm_row.rt_end_dt_cd;
7729           l_union_set(l_unionele_num).rt_end_dt_rl        := p_pgm_row.rt_end_dt_rl;
7730           l_unionele_num := l_unionele_num+1;
7731           --
7732         end if;
7733         --
7734         if l_union_set.count > 0 then
7735           --
7736           l_found           := TRUE;
7737           l_unionele_num    := 0;
7738           l_unionmaxele_num := l_union_set.count-1;
7739           --
7740           l_rec.order_number        := l_union_set(l_unionele_num).order_number;
7741           l_rec.enrt_cvg_strt_dt_cd := l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd;
7742           l_rec.enrt_cvg_strt_dt_rl := l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl;
7743           l_rec.enrt_cvg_end_dt_cd  := l_union_set(l_unionele_num).enrt_cvg_end_dt_cd;
7744           l_rec.enrt_cvg_end_dt_rl  := l_union_set(l_unionele_num).enrt_cvg_end_dt_rl;
7745           l_rec.rt_strt_dt_cd       := l_union_set(l_unionele_num).rt_strt_dt_cd;
7746           l_rec.rt_strt_dt_rl       := l_union_set(l_unionele_num).rt_strt_dt_rl;
7747           l_rec.rt_end_dt_cd        := l_union_set(l_unionele_num).rt_end_dt_cd;
7748           l_rec.rt_end_dt_rl        := l_union_set(l_unionele_num).rt_end_dt_rl;
7749           l_unionele_num := l_unionele_num+1;
7750           --
7751         else
7752           --
7753           l_found := FALSE;
7754           --
7755         end if;
7756         --
7757 if g_debug then
7758   hr_utility.set_location(' Done Building the union cache l_unionele_num = '||l_unionele_num, 1687);
7759 end if;
7760       else
7761         --
7762         open c_life_for_plip
7763           (c_effective_date => l_effective_date
7764           ,c_pgm_id         => l_pgm_id
7765           ,c_ptip_id        => l_ptip_id
7766           ,c_pl_id          => l_pl_id
7767           ,c_plip_id        => l_plip_id
7768           ,c_lee_rsn_id     => l_lee_rsn_id
7769           ,c_epfp_id        => l_enrt_perd_for_pl_id
7770           ,c_per_in_ler_id  => l_per_in_ler_id
7771           );
7772 if g_debug then
7773   hr_utility.set_location('Fetching rt start date cd' , 1687);
7774 end if;
7775         fetch c_life_for_plip into l_rec;
7776       if g_debug then
7777         hr_utility.set_location(' Dn Fet c_LFP: '||l_proc , 150);
7778       end if;
7779         l_found:=c_life_for_plip%found;
7780         --
7781       end if;
7782       --
7783     end if;
7784   else
7785     if l_enrt_perd_id is not null then
7786       if g_debug then
7787         hr_utility.set_location(l_proc , 190);
7788       end if;
7789       -- do scheduled pl_nip processing
7790       open c_sched_for_pl_nip;
7791       fetch c_sched_for_pl_nip into l_rec;
7792       l_found:=c_sched_for_pl_nip%found;
7793     else
7794       if g_debug then
7795         hr_utility.set_location(l_proc , 200);
7796       end if;
7797       -- do life event pl_nip processing
7798       -- also used for unrestricted pl_nip
7799 if g_debug then
7800   hr_utility.set_location(' Doing c_life_for_pl_nip', 1687);
7801 end if;
7802       open c_life_for_pl_nip;
7803       fetch c_life_for_pl_nip into l_rec;
7804       l_found:=c_life_for_pl_nip%found;
7805     end if;
7806   end if;
7807   loop
7808     if g_debug then
7809       hr_utility.set_location(l_proc , 240);
7810     end if;
7811     exit when l_found = FALSE;
7812     --
7813     -- process rates
7814     --
7815     if g_debug then
7816       hr_utility.set_location(l_proc , 250);
7817     end if;
7818     if p_which_dates_cd in ('R','B') then
7819       if l_rt_strt_dt_cd is null and
7820          l_rec.rt_strt_dt_cd is not null then
7821       if g_debug then
7822           hr_utility.set_location(l_proc , 270);
7823       end if;
7824         l_rt_strt_dt_cd:=l_rec.rt_strt_dt_cd;
7825         l_rt_strt_dt_rl:=l_rec.rt_strt_dt_rl;
7826       end if;
7827       if l_rt_end_dt_cd is null and
7828          l_rec.rt_end_dt_cd is not null then
7829         if g_debug then
7830           hr_utility.set_location(l_proc , 290);
7831         end if;
7832         l_rt_end_dt_cd:=l_rec.rt_end_dt_cd;
7833         l_rt_end_dt_rl:=l_rec.rt_end_dt_rl;
7834       end if;
7835     end if;
7836     --
7837     if g_debug then
7838       hr_utility.set_location(l_proc , 310);
7839     end if;
7840     --
7841     -- process coverage
7842     --
7843     if p_which_dates_cd in ('C','B') then
7844       if l_enrt_cvg_strt_dt_cd is null and
7845          l_rec.enrt_cvg_strt_dt_cd is not null then
7846         if g_debug then
7847           hr_utility.set_location(l_proc , 330);
7848         end if;
7849         l_enrt_cvg_strt_dt_cd:=l_rec.enrt_cvg_strt_dt_cd;
7850         l_enrt_cvg_strt_dt_rl:=l_rec.enrt_cvg_strt_dt_rl;
7851       end if;
7852       if l_enrt_cvg_end_dt_cd is null and
7853          l_rec.enrt_cvg_end_dt_cd is not null then
7854         if g_debug then
7855           hr_utility.set_location(l_proc , 350);
7856         end if;
7857         l_enrt_cvg_end_dt_cd:=l_rec.enrt_cvg_end_dt_cd;
7858         l_enrt_cvg_end_dt_rl:=l_rec.enrt_cvg_end_dt_rl;
7859       end if;
7860     end if;
7861     --
7862     if g_debug then
7863       hr_utility.set_location(l_proc , 360);
7864     end if;
7865     --
7866     -- determine if done
7867     --
7868     if ((p_which_dates_cd ='C' and
7869          l_enrt_cvg_strt_dt_cd is not null and
7870          l_enrt_cvg_end_dt_cd is not null) or
7871         (p_which_dates_cd ='R' and
7872          l_rt_strt_dt_cd is not null and
7873          l_rt_end_dt_cd is not null) or
7874         (p_which_dates_cd ='B' and
7875          l_enrt_cvg_strt_dt_cd is not null and
7876          l_enrt_cvg_end_dt_cd is not null and
7877          l_rt_strt_dt_cd is not null and
7878          l_rt_end_dt_cd is not null)) then
7879       if g_debug then
7880         hr_utility.set_location(l_proc , 380);
7881       end if;
7882       l_found:=FALSE;
7883       l_done:=TRUE;
7884     else
7885       if g_debug then
7886         hr_utility.set_location(l_proc , 390);
7887       end if;
7888       --
7889       -- if not done then get the next row
7890       --
7891       if l_pgm_id is not null then
7892         if l_enrt_perd_id is not null then
7893           if g_debug then
7894             hr_utility.set_location(l_proc , 410);
7895           end if;
7896           -- do scheduled plip processing
7897           fetch c_sched_for_plip into l_rec;
7898           l_found:=c_sched_for_plip%found;
7899         else
7900           -- do life event plip processing
7901           -- also used for unrestricted pl_nip
7902           if g_debug then
7903             hr_utility.set_location(' Fet c_LFPLIP: '||l_proc , 420);
7904           end if;
7905           if p_pgm_row.pgm_id is not null
7906             and p_ptip_row.ptip_id is not null
7907             and p_plip_row.plip_id is not null
7908             and p_pl_row.pl_id is not null
7909           then
7910             --
7911             l_rec.order_number        := l_union_set(l_unionele_num).order_number;
7912             l_rec.enrt_cvg_strt_dt_cd := l_union_set(l_unionele_num).enrt_cvg_strt_dt_cd;
7913             l_rec.enrt_cvg_strt_dt_rl := l_union_set(l_unionele_num).enrt_cvg_strt_dt_rl;
7914             l_rec.enrt_cvg_end_dt_cd  := l_union_set(l_unionele_num).enrt_cvg_end_dt_cd;
7915             l_rec.enrt_cvg_end_dt_rl  := l_union_set(l_unionele_num).enrt_cvg_end_dt_rl;
7916             l_rec.rt_strt_dt_cd       := l_union_set(l_unionele_num).rt_strt_dt_cd;
7917             l_rec.rt_strt_dt_rl       := l_union_set(l_unionele_num).rt_strt_dt_rl;
7918             l_rec.rt_end_dt_cd        := l_union_set(l_unionele_num).rt_end_dt_cd;
7919             l_rec.rt_end_dt_rl        := l_union_set(l_unionele_num).rt_end_dt_rl;
7920             --
7921             if l_unionele_num = l_unionmaxele_num then
7922               --
7923               l_found := FALSE;
7924               --
7925             else
7926               --
7927               l_found := TRUE;
7928               l_unionele_num := l_unionele_num+1;
7929               --
7930             end if;
7931             --
7932           else
7933             --
7934             fetch c_life_for_plip into l_rec;
7935             l_found:=c_life_for_plip%found;
7936             --
7937           end if;
7938         end if;
7939       else
7940         if l_enrt_perd_id is not null then
7941           if g_debug then
7942             hr_utility.set_location(l_proc , 460);
7943           end if;
7944           -- do scheduled pl_nip processing
7945           fetch c_sched_for_pl_nip into l_rec;
7946           l_found:=c_sched_for_pl_nip%found;
7947         else
7948           if g_debug then
7949             hr_utility.set_location(l_proc , 470);
7950           end if;
7951           -- do life event pl_nip processing
7952           -- also used for unrestricted pl_nip
7953           fetch c_life_for_pl_nip into l_rec;
7954           l_found:=c_life_for_pl_nip%found;
7955         end if;
7956       end if;
7957     end if;
7958     if g_debug then
7959       hr_utility.set_location(' End loop: '||l_proc , 420);
7960     end if;
7961   end loop;
7962   --
7963   -- close cursors
7964   --
7965   if l_pgm_id is not null then
7966     if l_enrt_perd_id is not null then
7967       if g_debug then
7968         hr_utility.set_location(l_proc , 860);
7969       end if;
7970       -- do scheduled plip processing
7971       close c_sched_for_plip;
7972     else
7973       if g_debug then
7974         hr_utility.set_location(l_proc , 870);
7975       end if;
7976       -- do life event plip processing
7977       -- also used for unrestricted plip
7978       if p_pgm_row.pgm_id is not null
7979         or p_ptip_row.ptip_id is not null
7980         or p_plip_row.plip_id is not null
7981         or p_pl_row.pl_id is not null
7982       then
7983         --
7984         null;
7985         --
7986       else
7987         --
7988         close c_life_for_plip;
7989         --
7990       end if;
7991       --
7992     end if;
7993   else
7994     if l_enrt_perd_id is not null then
7995       if g_debug then
7996         hr_utility.set_location(l_proc , 900);
7997       end if;
7998       -- do scheduled pl_nip processing
7999       close c_sched_for_pl_nip;
8000     else
8001       if g_debug then
8002         hr_utility.set_location(l_proc , 910);
8003       end if;
8004       -- do life event pl_nip processing
8005       -- also used for unrestricted pl_nip
8006       close c_life_for_pl_nip;
8007     end if;
8008   end if;
8009     --
8010     -- Bug no 3965571
8011     --
8012     open c_pln
8013           (c_pl_id                => l_pl_id
8014 	  ,c_effective_date => l_effective_date);
8015      fetch c_pln into l_pln_name;
8016      close c_pln;
8017     --
8018     open c_pgm
8019           (c_pgm_id             => l_pgm_id
8020 	  ,c_effective_date => l_effective_date);
8021      fetch c_pgm into l_pgm_name;
8022      close c_pgm;
8023      --
8024     open c_opt
8025           (c_oipl_id             => l_oipl_id
8026 	  ,c_effective_date => l_effective_date);
8027           fetch c_opt into l_opt_name;
8028      close c_opt;
8029 
8030  --  End Bug 3965571
8031 
8032   --
8033   -- must be done
8034   --
8035   if l_done=FALSE then
8036     if g_debug then
8037       hr_utility.set_location(l_proc , 530);
8038     end if;
8039 
8040     -- Bug No 3965571 : All ids are replaced by their names in the error message's tokens.
8041 
8042     if p_which_dates_cd in ('R','B') and
8043        l_rt_strt_dt_cd is null then
8044       if g_debug then
8045         hr_utility.set_location('BEN_91455_RT_STRT_DT_NOT_FOUND' , 540);
8046       end if;
8047       fnd_message.set_name('BEN','BEN_91455_RT_STRT_DT_NOT_FOUND');
8048       fnd_message.set_token('PLAN_ID',l_pln_name);
8049       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8050       fnd_message.set_token('PGM_ID',l_pgm_name);
8051       fnd_message.set_token('OIPL_ID',l_opt_name);
8052       fnd_message.raise_error;
8053     elsif p_which_dates_cd in ('R','B') and
8054           l_rt_end_dt_cd is null then
8055       if g_debug then
8056         hr_utility.set_location('BEN_91703_NOT_DET_RATE_END_DT' , 550);
8057       end if;
8058       fnd_message.set_name('BEN','BEN_91703_NOT_DET_RATE_END_DT');
8059       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8060       fnd_message.set_token('PGM_ID',l_pgm_name);
8061       fnd_message.set_token('PL_ID',l_pln_name);
8062       fnd_message.set_token('OIPL_ID',l_opt_name);
8063       fnd_message.raise_error;
8064     elsif p_which_dates_cd in ('C','B') and
8065           l_enrt_cvg_strt_dt_cd is null then
8066       if g_debug then
8067         hr_utility.set_location('BEN_91453_CVG_STRT_DT_NOT_FOUN' , 560);
8068       end if;
8069       fnd_message.set_name('BEN','BEN_91453_CVG_STRT_DT_NOT_FOUN');
8070       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8071       fnd_message.set_token('PGM_ID',l_pgm_name);
8072       fnd_message.set_token('PLAN_ID',l_pln_name);
8073       fnd_message.set_token('OIPL_ID',l_opt_name);
8074       fnd_message.raise_error;
8075     elsif p_which_dates_cd in ('C','B') and
8076           l_enrt_cvg_end_dt_cd is null then
8077       if g_debug then
8078         hr_utility.set_location(l_proc , 570);
8079       end if;
8080       fnd_message.set_name('BEN','BEN_91702_NOT_DET_CVG_END_DT');
8081       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8082       fnd_message.set_token('PGM_ID',l_pgm_name);
8083       fnd_message.set_token('PL_ID',l_pln_name);
8084       fnd_message.set_token('OIPL_ID',l_opt_name);
8085       fnd_message.raise_error;
8086     end if;
8087     --
8088   end if;
8089   --
8090   -- If necessary call the date function to get the absolute date
8091   if p_compute_dates_flag='Y' then
8092     --
8093     if (l_enrt_cvg_strt_dt_cd is not NULL) then
8094       if g_debug then
8095         hr_utility.set_location(' ECSDCD DETDT_MN '||l_proc , 630);
8096       end if;
8097       main
8098         (p_cache_mode             => p_cache_mode
8099         ,p_date_cd                => l_enrt_cvg_strt_dt_cd
8100         ,p_formula_id             => l_enrt_cvg_strt_dt_rl
8101         ,p_elig_per_elctbl_chc_id => p_elig_per_elctbl_chc_id
8102         ,p_business_group_id      => p_business_group_id
8103         ,p_effective_date         => p_effective_date
8104         ,p_lf_evt_ocrd_dt         => p_lf_evt_ocrd_dt
8105         ,p_returned_date          => l_enrt_cvg_strt_dt
8106         ,p_per_in_ler_id          => l_per_in_ler_id
8107         ,p_person_id              => l_person_id
8108         ,p_pgm_id                 => l_pgm_id
8109         ,p_pl_id                  => l_pl_id
8110         ,p_oipl_id                => l_oipl_id
8111         ,p_acty_base_rt_id        => p_acty_base_rt_id
8112         ,p_start_date             => p_start_date);
8113     if g_debug then
8114         hr_utility.set_location(' DN ECSDCD DETDT_MN '||l_proc , 630);
8115     end if;
8116     --
8117     end if;
8118     --
8119 /*
8120     -- ikasire - l_enrt_cvg_strt_dt is passed as a parameter for p_start_date
8121     -- to determinate the cvg end date which depends on cvg strt dt.
8122     -- Testing Only for 'WALDCSM'
8123     if l_enrt_cvg_end_dt_cd in ('WALDCSM','LWALDCSM', 'LWALDCM',
8124                                 'LWALDCPP','LWALDCPPY','LWALDLPPEPPY',
8125                                 'LWEM','LWODBED','WAED','WALDCM' ,
8126                                 'WALDCPP','WALDCPPY','WALDLPPEPPY',
8127                                 'WEM','WODBED' )
8128     then
8129       hr_utility.set_location('pasing start date '||l_enrt_cvg_strt_dt , 199);
8130       l_pass_cvg_strt_dt := l_enrt_cvg_strt_dt ;
8131     else
8132       l_pass_cvg_strt_dt := p_start_date ;
8133     end if;
8134 */
8135     l_pass_cvg_strt_dt := p_start_date ;
8136     --
8137     if (l_enrt_cvg_end_dt_cd is not NULL) then
8138       hr_utility.set_location(' ECEDCD DETDT_MN '||l_proc , 610);
8139       main
8140         (p_cache_mode             => p_cache_mode
8141         ,p_date_cd                => l_enrt_cvg_end_dt_cd
8142         ,p_formula_id             => l_enrt_cvg_end_dt_rl
8143         ,p_elig_per_elctbl_chc_id => p_elig_per_elctbl_chc_id
8144         ,p_business_group_id      => p_business_group_id
8145         ,p_effective_date         => p_effective_date
8146         ,p_lf_evt_ocrd_dt         => p_lf_evt_ocrd_dt
8147         ,p_per_in_ler_id          => l_per_in_ler_id
8148         ,p_person_id              => l_person_id
8149         ,p_pgm_id                 => l_pgm_id
8150         ,p_pl_id                  => l_pl_id
8151         ,p_oipl_id                => l_oipl_id
8152         ,p_acty_base_rt_id        => p_acty_base_rt_id
8153         ,p_start_date             => l_pass_cvg_strt_dt
8154         ,p_returned_date          => l_enrt_cvg_end_dt
8155         );
8156     --  hr_utility.set_location(' Dn ECEDCD DETDT_MN '||l_proc , 610);
8157       --
8158     end if;
8159     --
8160 
8161     -- rate start date calcualted before the end date
8162     if l_rt_strt_dt_cd is not NULL then
8163       if g_debug then
8164         hr_utility.set_location(' SDC DETDT_MN '||l_proc , 670);
8165       end if;
8166       --
8167       -- Passing Enrollment coverage
8168       --
8169       if l_enrt_cvg_strt_dt is not null then
8170         --
8171         l_pass_cvg_strt_dt := l_enrt_cvg_strt_dt ;
8172         --
8173       else
8174         --
8175         l_pass_cvg_strt_dt := p_start_date ;
8176         --
8177       end if;
8178       --
8179       main
8180         (p_cache_mode             => p_cache_mode
8181         ,p_date_cd                => l_rt_strt_dt_cd
8182         ,p_formula_id             => l_rt_strt_dt_rl
8183         ,p_elig_per_elctbl_chc_id => p_elig_per_elctbl_chc_id
8184         ,p_business_group_id      => p_business_group_id
8185         ,p_effective_date         => p_effective_date
8186         ,p_lf_evt_ocrd_dt         => p_lf_evt_ocrd_dt
8187         ,p_returned_date          => l_rt_strt_dt
8188         ,p_per_in_ler_id          => l_per_in_ler_id
8189         ,p_person_id              => l_person_id
8190         ,p_pgm_id                 => l_pgm_id
8191         ,p_pl_id                  => l_pl_id
8192         ,p_oipl_id                => l_oipl_id
8193         ,p_acty_base_rt_id        => p_acty_base_rt_id
8194         ,p_start_date             => l_pass_cvg_strt_dt );
8195 
8196       if g_debug then
8197         hr_utility.set_location(' rate start date =  '||l_rt_strt_dt , 1687);
8198       end if;
8199       if g_debug then
8200         hr_utility.set_location(' Dn SDC DETDT_MN '||l_proc , 670);
8201       end if;
8202       --- assign the fonm rat from the rate so the end date rule can use the fonm value
8203       if ben_manage_life_events.fonm  = 'Y'  or  l_fonm_cvg_strt_dt is not null then
8204          if ben_manage_life_events.g_fonm_rt_strt_dt is null or
8205             ben_manage_life_events.g_fonm_rt_strt_dt <> l_rt_strt_dt then
8206              ben_manage_life_events.g_fonm_rt_strt_dt :=  l_rt_strt_dt ;
8207          end if ;
8208          hr_utility.set_location('FONM gr date  ' ||  ben_manage_life_events.g_fonm_rt_strt_dt  , 60);
8209       end if ;
8210 
8211     end if;
8212 
8213 
8214     if l_rt_end_dt_cd is not NULL then
8215       -- There are rate end dates that are based on the coverage end date.
8216       -- Since we haven't updated the result yet, we need to pass the cvg end date
8217       -- into main.  Bug 1155069.
8218       if l_enrt_cvg_end_dt is not null then
8219          l_pass_cvg_end_dt := l_enrt_cvg_end_dt;
8220       else
8221          -- otherwise pass in the start date that was passed in to this proc.
8222          l_pass_cvg_end_dt := p_start_date;
8223       end if;
8224       --
8225       -- Bug 1647095.
8226       -- The case when coverage end date is enterable, then the actual
8227       -- coverage end date should come from the api.
8228       --
8229       if l_enrt_cvg_end_dt_cd = 'ENTRBL' and p_end_date is not null then
8230         l_pass_cvg_end_dt := p_end_date;
8231       end if;
8232       --
8233       if g_debug then
8234         hr_utility.set_location(' EDC DETDT_MN '||l_proc , 650);
8235       end if;
8236       main
8237         (p_cache_mode             => p_cache_mode
8238         ,p_date_cd                => l_rt_end_dt_cd
8239         ,p_formula_id             => l_rt_end_dt_rl
8240         ,p_elig_per_elctbl_chc_id => p_elig_per_elctbl_chc_id
8241         ,p_business_group_id      => p_business_group_id
8242         ,p_effective_date         => p_effective_date
8243         ,p_lf_evt_ocrd_dt         => p_lf_evt_ocrd_dt
8244         ,p_returned_date          => l_rt_end_dt
8245         ,p_per_in_ler_id          => l_per_in_ler_id
8246         ,p_person_id              => l_person_id
8247         ,p_pgm_id                 => l_pgm_id
8248         ,p_pl_id                  => l_pl_id
8249         ,p_oipl_id                => l_oipl_id
8250         ,p_acty_base_rt_id        => p_acty_base_rt_id
8251         ,p_start_date             => l_pass_cvg_end_dt);
8252       if g_debug then
8253         hr_utility.set_location(' Dn EDC DETDT_MN '||l_proc , 650);
8254       end if;
8255 
8256     end if;
8257     --
8258     -- rate start date moved above end date to detdermine the fomn_dt_strt_Dt
8259     -- when calc rt_end_dt_cd
8260 
8261 
8262 
8263     -- In case of the rate end date codes which depend on rate start date codes
8264     -- LODBEWM and ODBEWM. Presently rt_end_dt is coming out of a cursor using
8265     -- ben_enrt_rt table. If rt_strt_dt_cd is stored in that table instead of
8266     -- rt_strt_dt we get a null . So our plan is to get the rt_end_dt from
8267     -- rt_strt_dt - 1 , if the above codes are used and the result is null
8268     --
8269     if l_rt_end_dt_cd = 'LODBEWM' or l_rt_end_dt_cd = 'ODBEWM'
8270     then
8271        --
8272        if l_rt_end_dt is null
8273        then
8274           if g_debug then
8275             hr_utility.set_location('Special condition for LODBEWM ODBEWM ', 199);
8276           end if;
8277           l_rt_end_dt := l_rt_strt_dt - 1 ;
8278           if g_debug then
8279             hr_utility.set_location('l_rt_end_dt '||l_rt_end_dt , 199) ;
8280           end if;
8281        end if;
8282        --
8283     end if;
8284     --
8285     if l_rt_strt_dt_cd in ( 'FDSMCFES' , 'FDSMFES' , 'LFMESMES'  )
8286        and l_rt_strt_dt is NULL
8287     then
8288       --
8289       if l_rt_strt_dt_cd = 'FDSMCFES'
8290          and to_number(to_char(l_enrt_cvg_strt_dt, 'DD')) in ( 1, 16 )
8291       then
8292         --
8293         l_rt_strt_dt := l_enrt_cvg_strt_dt ;
8294         if g_debug then
8295           hr_utility.set_location('Case 1',15);
8296         end if;
8297         --
8298       elsif to_char(l_enrt_cvg_strt_dt, 'DD') > 15 then
8299         --
8300         l_rt_strt_dt := round(l_enrt_cvg_strt_dt,'Month')  ;
8301         if g_debug then
8302           hr_utility.set_location('Case 2',15);
8303         end if;
8304         --
8305       else
8306         --
8307         l_rt_strt_dt := round(l_enrt_cvg_strt_dt,'Month') + 15 ;
8308         if g_debug then
8309           hr_utility.set_location('Case 3'||l_enrt_cvg_strt_dt,15);
8310         end if;
8311 
8312         --
8313       end if;
8314       --
8315     end if;
8316   --
8317   end if;
8318 
8319   --
8320   -- If date is set null out code and rule
8321   --
8322   if l_rt_strt_dt is not null and
8323      (not do_date_at_enrollment(l_rt_strt_dt_cd)) and
8324      p_date_mandatory_flag='N' then
8325     if g_debug then
8326       hr_utility.set_location(l_proc , 700);
8327     end if;
8328     l_rt_strt_dt_cd:=null;
8329     l_rt_strt_dt_rl:=null;
8330   end if;
8331   --
8332   if l_rt_end_dt is not null and
8333      (not do_date_at_enrollment(l_rt_end_dt_cd)) and
8334      p_date_mandatory_flag='N' then
8335     if g_debug then
8336       hr_utility.set_location(l_proc , 720);
8337     end if;
8338     l_rt_end_dt_cd:=null;
8339     l_rt_end_dt_rl:=null;
8340   end if;
8341   --
8342   if l_enrt_cvg_strt_dt is not null and
8343      (not do_date_at_enrollment(l_enrt_cvg_strt_dt_cd)) and
8344      p_date_mandatory_flag='N' then
8345     if g_debug then
8346       hr_utility.set_location(l_proc , 740);
8347     end if;
8348     l_enrt_cvg_strt_dt_cd:=null;
8349     l_enrt_cvg_strt_dt_rl:=null;
8350   end if;
8351   --
8352   if l_enrt_cvg_end_dt is not null and
8353      (not do_date_at_enrollment(l_enrt_cvg_end_dt_cd)) and
8354      p_date_mandatory_flag ='N' then
8355     if g_debug then
8356       hr_utility.set_location(l_proc , 760);
8357     end if;
8358     l_enrt_cvg_end_dt_cd:=null;
8359     l_enrt_cvg_end_dt_rl:=null;
8360   end if;
8361   --
8362   -- if p_date_mandatory_flag is set to Y generate appropriate
8363   -- message if the hard date is not found.
8364   -- That is all. Have a happy day.
8365   --
8366 
8367   if p_date_mandatory_flag='Y' and
8368      p_compute_dates_flag='Y' then
8369     if g_debug then
8370       hr_utility.set_location(l_proc , 780);
8371     end if;
8372 
8373     -- Bug No 3965571 : All ids are replaced by their names in the error message's tokens.
8374 
8375     if p_which_dates_cd in ('R','B') and
8376        l_rt_strt_dt is null then
8377       if g_debug then
8378         hr_utility.set_location('BEN_91455_RT_STRT_DT_NOT_FOUND',37);
8379       end if;
8380       fnd_message.set_name('BEN','BEN_91455_RT_STRT_DT_NOT_FOUND');
8381       fnd_message.set_token('PLAN_ID',l_pln_name);
8382       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8383       fnd_message.set_token('PGM_ID',l_pgm_name);
8384       fnd_message.set_token('OIPL_ID',l_opt_name);
8385       fnd_message.raise_error;
8386     elsif p_which_dates_cd in ('R','B') and
8387           l_rt_end_dt is null then
8388       if g_debug then
8389         hr_utility.set_location('BEN_91703_NOT_DET_RATE_END_DT',37);
8390       end if;
8391       fnd_message.set_name('BEN','BEN_91703_NOT_DET_RATE_END_DT');
8392       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8393       fnd_message.set_token('PGM_ID',l_pgm_name);
8394       fnd_message.set_token('PL_ID',l_pln_name);
8395       fnd_message.set_token('OIPL_ID',l_opt_name);
8396       fnd_message.raise_error;
8397     elsif p_which_dates_cd in ('C','B') and
8398         l_enrt_cvg_strt_dt is null then
8399       if g_debug then
8400         hr_utility.set_location('BEN_91453_CVG_STRT_DT_NOT_FOUN',37);
8401       end if;
8402       fnd_message.set_name('BEN','BEN_91453_CVG_STRT_DT_NOT_FOUN');
8403       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8404       fnd_message.set_token('PGM_ID',l_pgm_name);
8405       fnd_message.set_token('PLAN_ID',l_pln_name);
8406       fnd_message.set_token('OIPL_ID',l_opt_name);
8407       fnd_message.raise_error;
8408     elsif p_which_dates_cd in ('C','B') and
8409           l_enrt_cvg_end_dt is null then
8410       if g_debug then
8411         hr_utility.set_location('BEN_91702_NOT_DET_CVG_END_DT',37);
8412       end if;
8413       fnd_message.set_name('BEN','BEN_91702_NOT_DET_CVG_END_DT');
8414       fnd_message.set_token('PERSON_ID',to_char(l_person_id));
8415       fnd_message.set_token('PGM_ID',l_pgm_name);
8416       fnd_message.set_token('PL_ID',l_pln_name);
8417       fnd_message.set_token('OIPL_ID',l_opt_name);
8418       fnd_message.raise_error;
8419     end if;
8420 
8421   end if;
8422   --
8423   -- Move the results back into the out parms
8424   --
8425   p_rt_strt_dt:=l_rt_strt_dt;
8426   p_rt_strt_dt_cd:=l_rt_strt_dt_cd;
8427   p_rt_strt_dt_rl:=l_rt_strt_dt_rl;
8428   p_rt_end_dt:=l_rt_end_dt;
8429   p_rt_end_dt_cd:=l_rt_end_dt_cd;
8430   p_rt_end_dt_rl:=l_rt_end_dt_rl;
8431   p_enrt_cvg_strt_dt:=l_enrt_cvg_strt_dt;
8432   p_enrt_cvg_strt_dt_cd:=l_enrt_cvg_strt_dt_cd;
8433   p_enrt_cvg_strt_dt_rl:=l_enrt_cvg_strt_dt_rl;
8434   p_enrt_cvg_end_dt:=l_enrt_cvg_end_dt;
8435   p_enrt_cvg_end_dt_cd:=l_enrt_cvg_end_dt_cd;
8436   p_enrt_cvg_end_dt_rl:=l_enrt_cvg_end_dt_rl;
8437   --
8438 --  hr_utility.set_location(' Leaving:'||l_proc, 930);
8439 --
8440 
8441 exception   -- nocopy changes
8442   --
8443   when others then
8444     --
8445     p_enrt_cvg_strt_dt         := null;
8446     p_enrt_cvg_strt_dt_cd      := null;
8447     p_enrt_cvg_strt_dt_rl      := null;
8448     p_rt_strt_dt               := null;
8449     p_rt_strt_dt_cd            := null;
8450     p_rt_strt_dt_rl            := null;
8451     p_enrt_cvg_end_dt          := null;
8452     p_enrt_cvg_end_dt_cd       := null;
8453     p_enrt_cvg_end_dt_rl       := null;
8454     p_rt_end_dt                := null;
8455     p_rt_end_dt_cd             := null;
8456     p_rt_end_dt_rl             := null;
8457     raise;
8458     --
8459 end rate_and_coverage_dates;
8460 --
8461 function do_date_at_enrollment(p_date_cd in varchar2) return boolean is
8462 begin
8463   if p_date_cd in ('ODEWM'
8464                   , 'LELD'
8465                   , 'LELDED'
8466                   , 'ENTRBL'
8467                   , 'WAENT'
8468 		  , 'ENTRBLFD' -- ICD ENH
8469                   , 'RL'   --  Bug 2122643
8470                   , 'LDPPFEFD'
8471                   , 'LDPPOEFD'
8472                   , 'FDPPFED'
8473                   , 'FDPPOED'
8474                   , 'AFDELD'
8475                   , 'FDMELD'
8476                   , 'FDPPFCDEL'
8477                   , 'FDPPELD'
8478                   , 'WAPPDEL'
8479                   ) then
8480     return true;
8481   else
8482     return false;
8483   end if;
8484   -- defense
8485   return true;
8486 end do_date_at_enrollment;
8487 --
8488 procedure rate_and_coverage_dates_nc
8489   (p_per_in_ler_id          in     number  default null
8490   ,p_person_id              in     number  default null
8491   ,p_pgm_id                 in     number  default null
8492   ,p_pl_id                  in     number  default null
8493   ,p_oipl_id                in     number  default null
8494   ,p_par_ptip_id            in     number  default null
8495   ,p_par_plip_id            in     number  default null
8496   ,p_lee_rsn_id             in     number  default null
8497   ,p_enrt_perd_id           in     number  default null
8498   ,p_enrt_perd_for_pl_id    in     number  default null
8499   ,p_which_dates_cd         in     varchar2      default 'B'
8500   ,p_date_mandatory_flag    in     varchar2      default 'Y'
8501   ,p_compute_dates_flag     in     varchar2      default 'Y'
8502   ,p_elig_per_elctbl_chc_id in     number  default null
8503   ,p_acty_base_rt_id        in     number  default null
8504   ,p_business_group_id      in     number
8505   ,p_start_date             in     date    default null
8506   ,p_end_date               in     date    default null
8507   ,p_effective_date         in     date
8508   ,p_lf_evt_ocrd_dt         in     date    default null
8509   ,p_enrt_cvg_strt_dt          out nocopy date
8510   ,p_enrt_cvg_strt_dt_cd       out nocopy varchar2
8511   ,p_enrt_cvg_strt_dt_rl       out nocopy number
8512   ,p_rt_strt_dt                out nocopy date
8513   ,p_rt_strt_dt_cd             out nocopy varchar2
8514   ,p_rt_strt_dt_rl             out nocopy number
8515   ,p_enrt_cvg_end_dt           out nocopy date
8516   ,p_enrt_cvg_end_dt_cd        out nocopy varchar2
8517   ,p_enrt_cvg_end_dt_rl        out nocopy number
8518   ,p_rt_end_dt                 out nocopy date
8519   ,p_rt_end_dt_cd              out nocopy varchar2
8520   ,p_rt_end_dt_rl              out nocopy number
8521   ) is
8522   begin
8523     --
8524     rate_and_coverage_dates
8525       (p_per_in_ler_id          => p_per_in_ler_id
8526       ,p_person_id              => p_person_id
8527       ,p_pgm_id                 => p_pgm_id
8528       ,p_pl_id                  => p_pl_id
8529       ,p_oipl_id                => p_oipl_id
8530       ,p_par_ptip_id            => p_par_ptip_id
8531       ,p_par_plip_id            => p_par_plip_id
8532       ,p_lee_rsn_id             => p_lee_rsn_id
8533       ,p_enrt_perd_id           => p_enrt_perd_id
8534       ,p_enrt_perd_for_pl_id    => p_enrt_perd_for_pl_id
8535       ,p_which_dates_cd         =>p_which_dates_cd
8536       ,p_date_mandatory_flag    => p_date_mandatory_flag
8537       ,p_compute_dates_flag     => p_compute_dates_flag
8538       ,p_elig_per_elctbl_chc_id => p_elig_per_elctbl_chc_id
8539       ,p_acty_base_rt_id        => p_acty_base_rt_id
8540       ,p_business_group_id      => p_business_group_id
8541       ,p_start_date             => p_start_date
8542       ,p_end_date               =>  p_end_date
8543       ,p_effective_date         => p_effective_date
8544       ,p_lf_evt_ocrd_dt         => p_lf_evt_ocrd_dt
8545       ,p_enrt_cvg_strt_dt       => p_enrt_cvg_strt_dt
8546       ,p_enrt_cvg_strt_dt_cd     => p_enrt_cvg_strt_dt_cd
8547       ,p_enrt_cvg_strt_dt_rl     => p_enrt_cvg_strt_dt_rl
8548       ,p_rt_strt_dt              => p_rt_strt_dt
8549       ,p_rt_strt_dt_cd            => p_rt_strt_dt_cd
8550       ,p_rt_strt_dt_rl            => p_rt_strt_dt_rl
8551       ,p_enrt_cvg_end_dt          => p_enrt_cvg_end_dt
8552       ,p_enrt_cvg_end_dt_cd       => p_enrt_cvg_end_dt_cd
8553       ,p_enrt_cvg_end_dt_rl       => p_enrt_cvg_end_dt_rl
8554       ,p_rt_end_dt                => p_rt_end_dt
8555       ,p_rt_end_dt_cd             => p_rt_end_dt_cd
8556       ,p_rt_end_dt_rl             => p_rt_end_dt_rl
8557    );
8558   end rate_and_coverage_dates_nc ;
8559   --
8560 END ben_determine_date;