DBA Data[Home] [Help]

PACKAGE BODY: APPS.BEN_EXT_THREAD

Source


1 package body ben_ext_thread as
2 /* $Header: benxthrd.pkb 120.49.12020000.3 2012/07/03 13:02:08 amnaraya ship $ */
3 
4 /*
5 +==============================================================================+
6 |   Copyright (c) 1997 Oracle Corporation                                      |
7 |      Redwood Shores, California, USA                                         |
8 |           All rights reserved.                                               |
9 +==============================================================================+
10 
11 Name
12         Benefit Extract Thread
13 Purpose
14         This package is used to multithread benefit extract process
15 History
16         Date             Who        Version    What?
17         10/15/98         Pdas       115.0      Created.
18         10/30/98         Yrathman   115.1      Added messages
19         11/04/98         Pdas       115.2      Modified call to
20                                                benutils.get_parameter
21         11/06/98         Yrathman   115.3      Added header/trailer
22         11/06/98         PDas       115.4      Added Person Count, Record Count
23         11/23/98         PDas       115.5      Added logic for smart totals
24         12/09/98         PDas       115.6      Modified call to process_ext_ht_recs
25         12/09/98         Thayden    115.7      Added logic for directroy name.
26         12/30/98         PDas       115.8      Added BENXSMRY report.
27         01/22/99         Pdas       115.9      Changed concurrent program date parameters
28                                                to varchar2.
29         02/10/99         Pdas       115.10     Modified process_ext_ht_recs procedure
30         02/17/99         Pdas       115.11     Modified process_ext_ht_recs procedure error handling
31         02/24/99         Pdas       115.12     Modified procedure process
32         02/26/99         Pdas       115.13     Modified procedure process
33         03/02/99         Pdas       115.14     Modified procedure process
34         03/09/99         Tmathers   115.15     changed != to <>.
35         03/09/99         Gperry     115.16     is to AS
36         03/09/99         Tmathers   115.18     Made dates MLS.
37         03/10/99         Tmathers   115.19     Missed one
38         03/22/99         Tmathers   115.20     Changed -MON- to /MM/
39         04/07/99         Thayden    115.22     Leapfrog version 115.4 for patch.
40         04/07/99         Thayden    115.23     Communication Fixes
41         04/13/99         Isen       115.24     Added upd_cm_sent_dt_flag to the
42                                                cursor ext_dfn_c
43         05/07/99         Asen       115.26     Solved the date conversion problem and moved the function
44             Calc_ext_date to benxutil.pkb, change the call accordingly.
45         05/13/99         Thayden    115.27     Major Fixes.
46         05/17/99         Isen       115.28     Corrected dates for header/triler
47                                                Bug - 2106
48         06/03/99         Asen       115.29     Added two parameters to action api call.
49         06/16/99         Isen       115.30     Added foreign key ref ext_rslt_id
50         07/09/99         Jcarpent   115.31     Added checks for backed out nocopy pil
51         20/07/99         Gperry     115.32     genutils -> benutils package
52                                                rename.
53         06/08/99         Asen       115.33     Added messages : Entering, Exiting.
54         08/31/99         Asen       115.34     Changed call to decode program (from ben_ext_decodes).
55         09/22/99         Thayden    115.36     Changes to cm and chg cursors (new incl crit).
56         09/27/99         Thayden    115.37     Added defaults for filename and directory.
57         10/01/99         Thayden    115.38     Changed call to calc_smart_totals.
58         10/04/99         Thayden    115.39     Added call for post processing rule.
59         10/06/99         Thayden    115.40     Fix bugs related with 115.39 changes.
60                                                Fix sql navigator erroring.
61         10/07/99         Thayden    115.41     load fnd_sessions table call.
62         10/11/99         Thayden    115.42     Comment calls to calc_ext_date.
63         11/03/99         Thayden    115.43     Move decode logic back to ben_ext_fmt.
64         01/10/2000       Thayden    115.44     Fix post process rule call.
65         02/06/2000       Thayden    115.45     Added performance enhancements.
66         02/23/2000       Thayden    115.46     Fixed performance enhancements.
67         03/01/2000       RChase     115.47     Fixed dynamic cursors in build_select_statements.
68                                                to use explict date conversion.  This clears up
69                                                issues with clients using NLS date formats other
70                                                than DD-MON-YYYY.
71                                                Updated process procedure to output a wrapped dynamic
72                                                sql statement to the log file for better debugging
73                                                purposes.
74         03/14/2000       Thayden    115.48     Advanced Conditions for headers and trailers.
75         05/31/2000       gperry     115.49     Tuning.
76         06/20/2000       jcarpent   115.50     Fix for Tuning fix,
77                                                uninitialized collection.
78                                                bug 5339.
79         08/10/2000       tilak      115.51     Dynamic sql date format changed from YYYY to RRRR to
80                                                support all the other lang
81         08/17/2000       tilak      115.52
82         08/17/2000       tilak      115.53     bug 1381514 fixed before calling calc_ext_date the type of code is calidat                                               ed
83         08/17/2000       tilak      115.54     Backport of 115.48 for 11.5.2
84                                                patchset with NLS fixes from
85                                                115.51.  wwbug 1391217.
86         08/30/2000       stee       115.55     Leapfrog of 115.53.
87         09/03/2000       stee       115.56     Leapfrog of 115.51.
88         09/03/2000       stee       115.57     Leapfrog of 115.55.
89         12/06/2000       rchase     115.58     Leapfrog of 115.56 with fixes
90                                                for 1521958.  Added rule contexts.
91         12/07/2000       jcarpent   115.59     Merged version of 115.57 and 115.58.
92         01/23/2001       rchase     115.60     Bug 1608852. Correct thread issue.
93                                                Lock being released before status
94                                                updating batch range status.
95         01/30/2001       tilak      115.61     error message changed ,get from get_error_messages
96                                                with element name
97         06/06/2001       tilak      115.62     caling formula is added for header/trailer - 1786750
98         06/21/2001       tilak      115.63     gv$_system_parameter used instead of v$system_paramter
99         07/27/2001       tilak      115.65     Change Event Dynomic sql is cahnged,
100                                                chg_eff_Dt is replaced  p_effective date
101         11/07/2001       mhoyes     115.66   - bug 2100912. Moved call to ben_extract.set_ext_lvls
102                                                from chunk level to thread level. Globals were
103                                                being over refreshed.
104         11/09/2001       mhoyes     115.67   - bug 2100912. Moved call to ben_extract.setup_rcd_typ_lvl
105                                                from chunk level to thread level. Globals were
106                                                being over refreshed.
107         11/26/2001       mhoyes     115.68   - dbdrv lines.
108 
109         01/20/2002      tjesumic    115.69   - restart process added
110         02/12/2002 hnarayan    115.70     added procedure update_ht_strt_end_dt to update
111              data which correspond to fields STRTDT and ENDDT
112              in header and trailer records of result detail.
113         03/11/2002      tjesumic    115.71   -  UTF changes
114         03/12/2002      ikasire     115.72      BEN UTF8 Changes
115         05/06/2002      tjesumic    115.73      p_ext_crit_prfl_id ,p_rquest_id added as
116                                                 paramter to process_ext_ht_recs
117         05/16/2002      tjesumic    115.74      dynamic sql chnaged the assg date validate changed from nvl to (+)                                                bug : 2376285
118         05/21/2002      tjesumic    115.75      PLPLCY element added for header level
119         05/24/2002      tjesumic    115.76     change event dynomic sql fixed
120         08-Jun-02       pabodla    115.78      Do not select the contingent worker
121                                                assignment when assignment data is
122                                                fetched.
123         17-Jun-02       pabodla    115.79      Fetching assignment data even if assignment_type is null
124         15-Aug-02       tjesumic   115.79      Max_lenght added for String Element
125         28-Aug-02       tjesumic   115.80      if the string Element is null and max_lenght defined
126                                                string element will be considered as space
127         17-dec-02       tjesumic   115.81      115.78,115.79 reversed
128         27-Dec-02       lakrish    115.84      NOCOPY changes
129         13-Feb-03       rpillay    115.85      HRMS Debug Performance changes to
130                                                 hr_utility.set_location calls
131         23-Aug-03       tjesumic   115.86      calcualted records added for  detail record
132         02-Oct-03       tjesumic   115.87      Upper/lower/initcap applied for string format mask
133         29-Oct-03       tjesumic   115.88      total for detail records are considers only non hiden records
134                                                hiden recors are filterd
135         30-dec-03       mmudigon   115.89      Bug 3232205. Modified cursors on
136                                                ben_person_actions to drive by
137                                                benefit_action_id
138         19-Jan-03       tjesumic   115.91      New procedire load_extract added to import and export the
139                                                extract definition
140         19-Jan-03       tjesumic   115.92      extract sedded or not decided by the extract not the parameter
141         19-Jan-03       tjesumic   115.92      view name cahnged to ben_pl_v
142         26-Jan-03       tjesumic   115.94      validate_mode parameterized for fndload concurren mgr
143         26-Jan-03       tjesumic   115.95      Extract header formula format mask is fixed
144         10-Feb-03       tjesumic   115.96      Person Type usage added in dynamic sql  - ppt
145         21-Apr-04       tjesumic   115.97      when the header/trailed element is null and mandatory
146                                                then throw the warning , if the record is mandatory then
147                                                throw the error and rollback the header and trailer
148         26-May-04       mmudigon   115.98      Bug 367237. Parameters changed
149                                                for Restart procedure
150         04-Jun-04       mmudigon   115.99      GSCC file.sql.6 fix
151         06-Jul-04       tjesumic   115.100     for security check_asg_security added and per_people_f view used
152         06-Jul-04       tjesumic   115.104     brought forward the version 100 above the leaf frog
153         02-Aug-04       nhunur     115.105     ensure request_id is not passed as null to benefit actions api.
154         06-Aug-04       nhunur     115.106     3810114 - Added code to prevent over polling of fnd_conc_requests.
155         13-aug-04       tjesumic   115.107     chg_actl_ct is truncated to validated the dates
156         18-Nov-04       rpinjala   115.108     New procedure chk_pqp_extract added
157         19-Nov-04       rpinjala   115.109     Changed chk_pqp_extract procedure.
158         05-Dec-04       rpinjala   115.110     Changed chk_pqp_extract procedure.
159         15-Dec-04       tjesumic   115.111     ext_rcd_in_file_id added to ben_Ext_rslt_dtl table
160         01-Feb-05       tjesumic   115.112     300 elements allowed in  a record
161         08-Mar-05       tjesumic   115.113     check_asg_security changed for performance
162         09-Mar-05       tjesumic   115.114     check_asg_security changed for performance
163         22-Mar-05       tjesumic   115.115     CWB (CW) , subheader codes changes
164                                                new extract type for 'CW' and new header and trialer procedure
165                                                for subheader and new  criteria for both added
166        24-Mar-05        tjesumic   115.116     position extracted from  position base table
167        30-Mar-05        tjesumic   115.117    new param p_subhdr_chg_log added for nfc extract to get postion
168                                                suheader from  history table
169        30-Mar-05        tjesumic   115.118    nfc changes
170        31-Mar-05        tjesumic   115.119    GHR changes
171        15-Mar-05        tjesumic   115.120    Global/Cross bg changes added
172        27-Apr-05        rpinjala   115.121    Changed chk_pqp_extract procedure.
173        28-Apr-05        tjesumic   115.122    to_date change to canonical_to_date for formula result date conversion
174        30-Apr-05        tjesumic   115.123    string element added for  rule element
175        12-May-05        tjesumic   115.124    g_ext_dfn_id and g_ext_rslt_id intialised in subheader for subhdr formula
176        17-May-05        tjesumic   115.125    p_ghr_date parameter changed to p_eff_start/end_dte
177        08-Jun-05        tjesumic   115.125    pennserver enhancement for new parameter, outpput type
178                                               effective, actual date and pauroll change events
179        08-Jun-05        tjesumic   115.127    pennserver enhancement
180        13-Jun-05        tjesumic   115.128    payroll dynamic sql changed
181        13-Jun-05        tjesumic   115.129    ghr sql cahnged for pos02
182        25-Jul-05        tjesumic   115.130    Dynamic sql build changed fro performance  and bug 4440823
183        22-Aug-05        tjesumic   115.131    business group id variable initalization in security check is changed
184        22-Aug-05        rbingi     115.132    Bug 4545881 - Global flag retrieved from Ext_Crit_Prfl
185        21-Sep-05        tjesumic   115.133    grade inforamtion extracted in  subheader
186        13-Sep-05        tjesumic   115.134    when the extract excuted with only subheader, the process should not
187                                               go throuh person information
188        9-nov-05         nhunur     115.135    xcl > per for performance - bug 4721453
189        6-Dec-05         tjesumic   115.136    cm_display_flag is  added and validated
190        9-Dec-05         tjesumic   115.137    benxmlwrit called for cm_display on
191       10-Dec-05         tjesumic   115.138    output_code to set the output type for display
192       20-Dec-05         tjesumic   115.139    cm_downlaod added for download and GHR fix as per 4609093
193       20-Dec-05         tjesumic   115.141    c_xdoi cursor closed
194       22-Dec-05         tjesumic   115.142    XSL changed to EXCEL
195       11-Jan-06         tjesumic   115.143    restart changed to send correct parameters to process
196       02-Feb-06         tjesumic   115.144    restart changed to process the errorerd ranges from advance condition
197       12-Feb-06         tjesumic   115.145    Assignment set added in extract criteria's person level
198       15-Mar-06         tjesumic   115.146    Penserver Sql changes
199       23-Mar-06         tjesumic   115.147    Penserver Sql changes
200       27-Mar-06         tjesumic   115.148    debug  function call removed
201       29-Mar-06         tjesumic   115.149    end dte and start parsed  in update_ht_strt_end_dt
202       28-APR-06         hgattu     115.150    new param p_out_dummy is added to process procedure(5131931)
203       07-Aug-06         tjesumic   115.151    parameter p_out_dummy passed in wrong position
204       06-Oct-06         tjesumic   115.152    The Advance Date criteria added to Dynamic sql
205                                               Pay change event sql splited into two, 1 for non Asg event
206                                               another one for Asg Events. New procedure
207                                               chck_non_asg_pay_evt and build_adv_criteria added.
208      20-oct-06          tjesumic   115.153    The dynamic query changed to calc the criteria value and validated
209                                               as in clause without using extract table. per_all_people removed
210                                               from pay change event query
211      23-Oct-06          tjesumic   115.154    nocopy added
212      31-Oct-06          tjesumic   115.155    payroll id added to the dynamic query
213      07-Dec-06          tjesumic   115.156    subheader's org, job and grade from and to date are validated
214      12-Feb-07          tjesumic   115.157    allow overide param added for uploading file
215                                               required file benextse.lct 115.40 , benextse,pkh/pkb 115.24/73
216      13-Feb-07          tjesumic   115.158    Legislation ang global check in pay_event_updates table is removed
217                                               The primary key is passed from process event table.
218      07-mar-07          tjesumic   115.159    Dynamic sql error is fixed
219      20-mar-07          tjesumic   115.162    115.160 and 161 reverted
220      04-Sep-07          tjesumic   115.163    total count and detail count elements are fixed by adding ext_rcd_in_file_id in validation
221      26-Nov-07          tjesumic   115.164    when the extract is global, for 'PPT' person type usage the criteria are validated from table
222                                               big - 6642051
223      30-Apr-08          vkodedal   115.165    Changes required for penserver - performance fix--6895935,6801389,6995291
224      11-Aug-08          vkodedal   115.167    Penserver perf issue-7274509
225      25-Aug-08          vkodedal   115.168    Penserver perf issue-7341530
226      12-Sep-08          jvaradra   115.170    Penserver perf issue-7358558
227      30-Mar-09          vkodedal   115.172,173    Bug#8335771 -Restart process not spawning threads - get l_num_range as count
228      18-Jun-10          vkodedal   115.174    Bug#9823193  Added ORDERED hint
229      16-Dec-11          velvanop   115.175    Bug#13509481: Added person selection rule parameter for the Extract process
230 */
231 
232 --
233 --
234 -- ----------------------------------------------------------------------------
235 -- |                     Private Global Definitions                           |
236 -- ----------------------------------------------------------------------------
237 --
238 g_debug boolean := hr_utility.debug_enabled;
239 --
240 g_val_def ben_ext_fmt.valtabtyp :=
241            ben_ext_fmt.valtabtyp(null,null,null,null,null,null,null,null,null,null,
242                                  null,null,null,null,null,null,null,null,null,null,
243                                  null,null,null,null,null,null,null,null,null,null,
244                                  null,null,null,null,null,null,null,null,null,null,
245                                  null,null,null,null,null,null,null,null,null,null,
246                                  null,null,null,null,null,null,null,null,null,null,
247                                  null,null,null,null,null,null,null,null,null,null,
248                                  null,null,null,null,null,null,null,null,null,null,
249                                  null,null,null,null,null,null,null,null,null,null,
250                                  null,null,null,null,null,null,null,null,null,null,
251                                  null,null,null,null,null,null,null,null,null,null,
252                                  null,null,null,null,null,null,null,null,null,null,
253                                  null,null,null,null,null,null,null,null,null,null,
254                                  null,null,null,null,null,null,null,null,null,null,
255                                  null,null,null,null,null,null,null,null,null,null,
256                                  null,null,null,null,null,null,null,null,null,null,
257                                  null,null,null,null,null,null,null,null,null,null,
258                                  null,null,null,null,null,null,null,null,null,null,
259                                  null,null,null,null,null,null,null,null,null,null,
260                                  null,null,null,null,null,null,null,null,null,null,
261                                  null,null,null,null,null,null,null,null,null,null,
262                                  null,null,null,null,null,null,null,null,null,null,
263                                  null,null,null,null,null,null,null,null,null,null,
264                                  null,null,null,null,null,null,null,null,null,null,
265                                  null,null,null,null,null,null,null,null,null,null,
266                                  null,null,null,null,null,null,null,null,null,null,
267                                  null,null,null,null,null,null,null,null,null,null,
268                                  null,null,null,null,null,null,null,null,null,null,
269                                  null,null,null,null,null,null,null,null,null,null,
270                                  null,null,null,null,null,null,null,null,null,null
271                                  );
272 --
273 -- ----------------------------------------------------------------------------
274 -- |-------------------------< write_error >-----------------------------------|
275 -- ----------------------------------------------------------------------------
276 --
277 Procedure write_error(p_err_num             in number,
278                       p_err_name            in varchar2,
279                       p_typ_cd              in varchar2,
280                       p_person_id           in number,
281                       p_request_id          in number,
282                       p_ext_rslt_id         in number,
283                       p_business_group_id   in number
284                       ) IS
285 --
286   l_proc               varchar2(72);
287 --
288 begin
289 --
290   if g_debug then
291     l_proc := g_package || '.write_error';
292     hr_utility.set_location('Entering'||l_proc, 5);
293   end if;
294 --
295   if p_business_group_id is not null then
296 --
297     ben_ext_util.write_err
298          (p_err_num           => p_err_num,
299           p_err_name          => p_err_name,
300           p_typ_cd            => p_typ_cd,
301           p_person_id         => p_person_id,
302           p_request_id        => p_request_id,
303           p_ext_rslt_id        => p_ext_rslt_id,
304           p_business_group_id => p_business_group_id
305          );
306 --
307     commit;
308 --
309   end if;
310 --
311   if g_debug then
312     hr_utility.set_location('Exiting'||l_proc, 15);
313   end if;
314 --
315 end write_error;
316 --
317 -- ----------------------------------------------------------------------------
318 -- |---------------------< get_ext_prmtrs >-----------------------------------|
319 -- ----------------------------------------------------------------------------
320 --
321 Procedure get_ext_prmtrs(p_ext_dfn_id               in  number,
322                          p_business_group_id        in  number,
323                          p_data_typ_cd              in out nocopy varchar2,
324                          p_ext_typ_cd               in out nocopy varchar2,
325                          p_ext_crit_prfl_id         in out nocopy number,
326                          p_ext_file_id              in out nocopy number,
327                          p_ext_strt_dt              in out nocopy varchar2,
328                          p_ext_end_dt               in out nocopy varchar2,
329                          p_prmy_sort_cd             in out nocopy varchar2,
330                          p_scnd_sort_cd             in out nocopy varchar2,
331                          p_output_name              in out nocopy varchar2,
332                          p_drctry_name              in out nocopy varchar2,
333                          p_apnd_rqst_id_flag        in out nocopy varchar2,
334                          p_kickoff_wrt_prc_flag     in out nocopy varchar2,
335                          p_use_eff_dt_for_chgs_flag in out nocopy varchar2,
336                          p_ext_post_prcs_rl         in out nocopy number,
337                          p_ext_global_flag          in out nocopy varchar2,
338                          p_cm_display_flag          in out nocopy varchar2,
339                          p_output_type              in out nocopy varchar2,
340                          p_xdo_template_id          in out nocopy number
341                        ) IS
342 --
343   l_proc               varchar2(72);
344 --
345   cursor ext_dfn_c is
346   SELECT data_typ_cd
347        , ext_typ_cd
348        , strt_dt
349        , end_dt
350        , ext_crit_prfl_id
351        , ext_file_id
352        , prmy_sort_cd
353        , scnd_sort_cd
354        , output_name
355        , drctry_name
356        , apnd_rqst_id_flag
357        , kickoff_wrt_prc_flag
358        , use_eff_dt_for_chgs_flag
359        , upd_cm_sent_dt_flag
360        , ext_post_prcs_rl
361        , ext_global_flag
362        , output_type
363        , xdo_template_id
364        , cm_display_flag
365   FROM  ben_ext_dfn
366   WHERE ext_dfn_id = p_ext_dfn_id;
367 --
368 
369   -- subhead
370   cursor c_ext_file (p_file_id number) is
371   select ext_data_elmt_in_rcd_id1,
372          ext_data_elmt_in_rcd_id2
373   from  ben_Ext_file exf
374   where exf.ext_file_id = p_file_id ;
375 
376 
377   cursor  c_ext_elmt (p_data_elmt_in_rcd_id  number
378                       ) is
379   select  exf.short_name
380   from ben_ext_fld  exf,
381        ben_Ext_data_elmt_in_rcd edr,
382        ben_ext_data_elmt        ede
383   where edr.ext_data_elmt_in_rcd_id = p_data_elmt_in_rcd_id
384     and edr.ext_data_elmt_id     = ede.ext_Data_elmt_id
385     and ede.ext_fld_id           = exf.ext_fld_id (+)
386     ;
387 
388   l_ext_rcd c_ext_file%rowtype ;
389 
390 
391   cursor c_ext_global  is
392   select ext_global_flag
393   from ben_ext_crit_prfl
394   where ext_crit_prfl_id = p_ext_crit_prfl_id
395   ;
396 
397 begin
398 --
399   if g_debug then
400     l_proc := g_package||'.get_ext_prmtrs';
401     hr_utility.set_location('Entering'||l_proc, 5);
402   end if;
403 --
404   open ext_dfn_c;
405   fetch ext_dfn_c into p_data_typ_cd,
406                        p_ext_typ_cd,
407                        p_ext_strt_dt,
408                        p_ext_end_dt,
409                        p_ext_crit_prfl_id,
410                        p_ext_file_id,
411                        p_prmy_sort_cd,
412                        p_scnd_sort_cd,
413                        p_output_name,
414                        p_drctry_name,
415                        p_apnd_rqst_id_flag,
416                        p_kickoff_wrt_prc_flag,
417                        p_use_eff_dt_for_chgs_flag,
418                        ben_ext_person.g_upd_cm_sent_dt_flag,
419                        p_ext_post_prcs_rl,
420                        p_ext_global_flag,
421                        p_output_type ,
422                        p_xdo_template_id,
423                        p_cm_display_flag
424                        ;
425 --
426   if ext_dfn_c%notfound then
427     ben_ext_thread.g_err_num := 91873;
428     ben_ext_thread.g_err_name := 'BEN_91873_EXT_NOT_FOUND';
429     close ext_dfn_c;
430     raise g_job_failure_error;
431   end if;
432 --
433   close ext_dfn_c;
434 --
435 
436 -- confirm the global flag from criteria too
437 --  for CWB the criterai may not global but definition is global
438 
439  /*if p_ext_global_flag <> 'Y'  and  p_Ext_crit_prfl_id is not null  then
440       open c_ext_global ;
441       fetch c_ext_global into p_ext_global_flag ;
442       close c_ext_global ;
443    end if ; */ -- Bug 4545881 by rbingi
444   --
445   -- For CWB type is always global
446   -- for other types same as the Criteria profile
447   if p_data_typ_cd = 'CW' then
448     --
449     p_ext_global_flag := 'Y' ;
450     --
451   else
452     --
453     open c_ext_global ;
454     fetch c_ext_global into p_ext_global_flag ;
455     close c_ext_global ;
456     --
457   end if;
458 
459   --subhead
460   open c_ext_file(p_ext_file_id) ;
461   fetch c_ext_file into l_ext_rcd ;
462   close c_ext_file ;
463 
464   if l_ext_rcd.ext_data_elmt_in_rcd_id1 is not null then
465      open  c_ext_elmt(l_ext_rcd.ext_data_elmt_in_rcd_id1) ;
466      fetch c_ext_elmt into g_ext_group_elmt1 ;
467      close c_ext_elmt ;
468 
469      if l_ext_rcd.ext_data_elmt_in_rcd_id2 is not null then
470         open  c_ext_elmt(l_ext_rcd.ext_data_elmt_in_rcd_id2) ;
471         fetch c_ext_elmt into g_ext_group_elmt2 ;
472         close c_ext_elmt ;
473      end if ;
474   end if ;
475   -- eof subheader
476 
477   if g_debug then
478     hr_utility.set_location('Exiting'||l_proc, 15);
479   end if;
480 --
481 Exception
482 --
483   when g_job_failure_error then
484     raise g_job_failure_error;
485 --
486 End get_ext_prmtrs;
487 --
488 -- ----------------------------------------------------------------------------
489 -- |--------------------------< update_ht_strt_end_dt >-------------------------|
490 -- ----------------------------------------------------------------------------
491 --
492 Procedure update_ht_strt_end_dt (p_ext_rslt_id  number)
493 IS
494 --
495   l_proc          varchar2(72);
496 --
497   l_cursor_id     integer;
498   l_cnt_rows_upd  number;
499   l_tot_string    varchar2(5000) := null;
500   l_col_name      varchar2(5000) := null;
501   l_value        varchar2(5000) := null;
502   l_curr_string   varchar2(600)  := null;  --UTF8
503   l_run_strt_dt   date := null;
504   l_run_end_dt    date := null;
505   l_temp_dt       date := null;
506 --
507   cursor c_ext_rslt is
508    SELECT rslt.run_strt_dt , rslt.run_end_dt
509    FROM    ben_ext_rslt rslt
510    WHERE   rslt.ext_rslt_id = p_ext_rslt_id;
511 --
512   cursor c_ext_rcd is
513    SELECT distinct rdtl.ext_rcd_id ext_rcd_id
514    FROM ben_ext_rcd rcd, ben_ext_rslt_dtl rdtl
515    WHERE  rdtl.ext_rslt_id = p_ext_rslt_id
516      and  rdtl.ext_rcd_id = rcd.ext_rcd_id
517      and rcd.rcd_type_cd in ('H','T');
518 --
519   cursor c_data_elmt_seq_num(p_ext_rcd_id number) is
520    SELECT elrc.seq_num, fld.short_name, elmt.frmt_mask_cd
521    FROM ben_ext_data_elmt_in_rcd elrc, ben_ext_data_elmt elmt, ben_ext_fld fld
522    WHERE elrc.ext_rcd_id = p_ext_rcd_id
523      and elrc.ext_data_elmt_id = elmt.ext_data_elmt_id
524      and elmt.ext_fld_id = fld.ext_fld_id
525      and ltrim(rtrim(fld.short_name)) in ('STRTDT','ENDDT');
526 
527 BEGIN
528   --
529   if g_debug then
530     l_proc := g_package||'.update_ht_strt_end_dt';
531     hr_utility.set_location('Entering'||l_proc, 5);
532   end if;
533   --
534   --
535   l_cursor_id := DBMS_SQL.OPEN_CURSOR;
536   --
537   open c_ext_rslt;
538   fetch c_ext_rslt into l_run_strt_dt, l_run_end_dt ;
539   close c_ext_rslt;
540   --
541   for rcd_rec in c_ext_rcd loop
542     --
543     l_tot_string := null;
544     for elmt_rec in c_data_elmt_seq_num(rcd_rec.ext_rcd_id) loop
545       --
546       if (elmt_rec.short_name = 'STRTDT') then
547         l_temp_dt := l_run_strt_dt;
548       elsif (elmt_rec.short_name = 'ENDDT') then
549         l_temp_dt := l_run_end_dt;
550       else
551         l_temp_dt := null;
552       end if;
553       --
554 
555       l_col_name    := ' VAL_' || ltrim(to_char(elmt_rec.seq_num,'09'))  ;
556       if (elmt_rec.frmt_mask_cd is null) then
557         --l_curr_string := ' VAL_' || ltrim(to_char(elmt_rec.seq_num,'09')) || ' = ''' || to_char(l_temp_dt) || ''' ' ;
558         l_value       :=  to_char(l_temp_dt)  ;
559       else
560         --l_curr_string := ' VAL_' || ltrim(to_char(elmt_rec.seq_num,'09')) || ' = ''' ||
561         -- to_char(l_temp_dt,hr_general.decode_lookup('BEN_EXT_FRMT_MASK',elmt_rec.frmt_mask_cd)) || ''' ' ;
562         l_col_name    := ' VAL_' || ltrim(to_char(elmt_rec.seq_num,'09'))  ;
563         l_value       :=  to_char(l_temp_dt,hr_general.decode_lookup('BEN_EXT_FRMT_MASK',elmt_rec.frmt_mask_cd)) ;
564       end if;
565       --
566       /*
567       if l_tot_string is null then
568         l_tot_string := ' SET ' || l_curr_string ;
569       else
570         l_tot_string := l_tot_string || ' , ' || l_curr_string ;
571       end if;
572       --
573     end loop;
574     --
575 
576     if l_tot_string is not null then
577       */
578       --
579       /*
580       l_tot_string := 'UPDATE BEN_EXT_RSLT_DTL ' || l_tot_string
581          || ' WHERE ext_rslt_id = ' || to_char(p_ext_rslt_id)
582          || ' AND   ext_rcd_id  = ' || to_char(rcd_rec.ext_rcd_id);
583 
584       */
585 
586       l_tot_string := 'UPDATE BEN_EXT_RSLT_DTL SET  ' ||  l_col_name  ||  '   = :VAL  where ext_rslt_id = :RSLT_ID and ext_rcd_id  = :RCD_ID' ;
587 
588       DBMS_SQL.PARSE(l_cursor_id, l_tot_string, dbms_sql.v7);
589 
590 
591       --DBMS_SQL.BIND_VARIABLE(l_cursor_id, ':COL' , l_col_name  );
592       DBMS_SQL.BIND_VARIABLE(l_cursor_id, ':VAL' , l_value  );
593       DBMS_SQL.BIND_VARIABLE(l_cursor_id, ':RSLT_ID' , p_ext_rslt_id  );
594       DBMS_SQL.BIND_VARIABLE(l_cursor_id, ':RCD_ID' , rcd_rec.ext_rcd_id  );
595 
596 
597       l_cnt_rows_upd := DBMS_SQL.EXECUTE(l_cursor_id);
598       --
599   --  end if;
600     --
601       end loop;
602   end loop;
603   --
604   DBMS_SQL.CLOSE_CURSOR(l_cursor_id);
605 --
606 --
607   if g_debug then
608     hr_utility.set_location('Exiting'||l_proc, 15);
609   end if;
610 --
611 EXCEPTION
612  --
613  when OTHERS then
614    if DBMS_SQL.IS_OPEN(l_cursor_id) then
615      DBMS_SQL.CLOSE_CURSOR(l_cursor_id);
616    end if;
617  raise;
618 
619 END update_ht_strt_end_dt;
620 
621 
622 
623 
624 --
625 -- ----------------------------------------------------------------------------
626 -- |--------------------------< get_ht_elmt_data >-------------------------|
627 -- ----------------------------------------------------------------------------
628 --
629 Function get_ht_elmt_data(p_ext_rslt_id              in number,
630                           p_data_elmt_typ_cd         in varchar2 ,
631                           p_short_name               in varchar2 ,
632                           p_ext_data_elmt_id         in number,
633                           p_frmt_mask_cd             in varchar2 default null,
634                           p_frmt_mask_lookup_cd      in varchar2 default null,
635                           p_dflt_val                 in varchar2 default null,
636                           p_data_elmt_rl             in number   default null,
637                           p_pl_id                    in number   default null,
638                           p_business_group_id        in number,
639                           p_effective_date           in date ,
640                           p_request_id               in number,
641                           p_ext_file_id              in number   default null,
642                           p_ttl_fnctn_cd             in varchar2 default null,
643                           p_ttl_sum_ext_data_elmt_id in number   default null,
644                           p_ttl_cond_ext_data_elmt_id in number   default null,
645                           p_rcd_typ_cd               in varchar2 default null,
646                           p_group_val_01             in varchar2  default null,
647                           p_group_val_02             in varchar2  default null,
648                           p_ext_per_bg_id            in varchar2  default null,
649                           p_String_val               in varchar2  default null
650                           )
651                           return varchar2 IS
652 
653 
654   cursor c_rule_type(p_rule_id ff_formulas_f.formula_id%type) is
655     select formula_type_id
656     from   ff_formulas_f
657     where  formula_id = p_rule_id
658     and    p_effective_date
659            between effective_start_date
660            and     effective_end_date;
661 
662   cursor bus_c (p_bg_id  number) is
663   select name
664   from   per_business_groups
665   where  organization_id = p_bg_id ;
666 
667   cursor c_plcy (l_pl_id Number , l_typ_cd varchar2) is
668    select a.plcy_r_grp
669      from ben_popl_org_f a,
670           ben_popl_org_role_f b
671     where a.pl_id = l_pl_id
672       and a.plcy_r_grp is not null
673       and a.popl_org_id = b.popl_org_id
674       and b.org_role_typ_cd = l_typ_cd
675       and p_effective_date between a.effective_start_date
676                              and a.effective_end_date
677        and p_effective_date between b.effective_start_date
678                              and b.effective_end_date;
679 
680   cursor c_plplcy (l_pl_id Number) is
681    select a.plcy_r_grp
682      from ben_popl_org_f a
683     where a.pl_id = l_pl_id
684       and a.plcy_r_grp is not null
685       and p_effective_date between a.effective_start_date
686                              and a.effective_end_date ;
687 
688 
689   cursor c_custid (l_pl_id Number , l_typ_cd varchar2) is
690    select a.cstmr_num
691      from ben_popl_org_f a,
692           ben_popl_org_role_f b
693     where a.pl_id = l_pl_id
694       and a.cstmr_num is not null
695       and a.popl_org_id = b.popl_org_id
696       and b.org_role_typ_cd = l_typ_cd
697       and p_effective_date between a.effective_start_date
698                              and a.effective_end_date
699        and p_effective_date between b.effective_start_date
700                              and b.effective_end_date;
701 
702   cursor c_rolname (l_pl_id Number , l_typ_cd varchar2) is
703    select b.name
704    from ben_popl_org_f a,
705         ben_popl_org_role_f b
706    where a.pl_id = l_pl_id
707      and b.name is not null
708      and b.org_role_typ_cd = l_typ_cd
709       and a.popl_org_id = b.popl_org_id
710      and p_effective_date between a.effective_start_date
711          and a.effective_end_date
712      and p_effective_date between b.effective_start_date
713          and b.effective_end_date;
714 
715 
716   cursor get_per_cnt (p_group_val_01 varchar2 ,
717                       p_group_val_02 varchar2) is
718   select count(distinct person_id)
719   from   ben_ext_rslt_dtl xrd
720   where  xrd.ext_rslt_id = p_ext_rslt_id
721   and    person_id not in (0, 999999999999)
722   and    xrd.group_val_01 = p_group_val_01
723   and    nvl(xrd.group_val_02,'-1')  =  nvl(p_group_val_02,'-1') ;
724 
725 
726    cursor get_dtl_cnt(p_group_val_01 varchar2 ,
727                       p_group_val_02 varchar2) is
728    select count(*)
729    from   ben_ext_rslt_dtl xrd ,
730           ben_ext_rcd_in_file erf
731    where  xrd.ext_rslt_id = p_ext_rslt_id
732     and   xrd.ext_rcd_id  = erf.ext_rcd_id
733     and   xrd.ext_rcd_in_file_id  = erf.ext_rcd_in_file_id
734     and   erf.ext_file_id = p_ext_file_id
735     and   erf.hide_flag    = 'N'
736     and   xrd. person_id not in (0, 999999999999)
737     and   xrd.group_val_01 = p_group_val_01
738     and   nvl(xrd.group_val_02,'-1')  =  nvl(p_group_val_02,'-1')
739   ;
740 
741 
742   cursor get_ttl_cnt(p_group_val_01 varchar2 ,
743                       p_group_val_02 varchar2) is
744    select count(*)
745    from   ben_ext_rslt_dtl xrd ,
746           ben_ext_rcd_in_file erf
747    where  xrd.ext_rslt_id  = p_ext_rslt_id
748     and   xrd.ext_rcd_id   = erf.ext_rcd_id
749     and   xrd.ext_rcd_in_file_id  = erf.ext_rcd_in_file_id
750     and   erf.ext_file_id  = p_ext_file_id
751     and   erf.hide_flag    = 'N'
752     and   xrd.group_val_01 = p_group_val_01
753     and   nvl(xrd.group_val_02,'   ')  =  nvl(p_group_val_02,'   ')
754   ;
755 
756   cursor get_subtrl_cnt is
757  select count(*)
758   from   ben_ext_rcd_in_file fil, ben_ext_rcd rcd
759   where  fil.ext_rcd_id = rcd.ext_rcd_id
760   and    fil.ext_file_id = p_ext_file_id
761   and    rcd.rcd_type_cd = 'L';
762 
763 
764  l_subtrl_cnt    number ;
765  l_business_group_name    per_business_groups.name%TYPE;
766  l_rule_type  c_rule_type%rowtype;
767  l_outputs  ff_exec.outputs_t;
768  l_dummy_var  varchar2(1000) ;
769  l_proc           varchar2(72) ;
770  l_rslt_elmt      varchar2(600) := null;  -- UTF8
771  l_rslt_elmt_fmt  varchar2(600) := null;
772 
773 Begin
774 
775   g_debug := hr_utility.debug_enabled;
776   if g_debug then
777     l_proc := g_package||'.get_ht_elmt_data';
778     hr_utility.set_location('Entering'||l_proc, 5);
779   end if;
780 
781     IF p_data_elmt_typ_cd = 'F' THEN
782 
783        IF p_short_name = 'EFFDT' THEN
784            l_rslt_elmt := ben_ext_fmt.apply_format_mask(p_effective_date, P_frmt_mask_cd);
785         ELSIF p_short_name = 'STRTDT' THEN
786           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_ext_strt_dt, P_frmt_mask_cd);
787         ELSIF p_short_name = 'ENDDT' THEN
788           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_ext_end_dt, P_frmt_mask_cd);
789         ELSIF p_short_name = 'RUNDT' THEN
790           l_rslt_elmt := ben_ext_fmt.apply_format_mask(sysdate, P_frmt_mask_cd);
791         ELSIF p_short_name = 'RECCNT' THEN
792 
793           open  get_subtrl_cnt ;
794           fetch  get_subtrl_cnt into l_subtrl_cnt ;
795           close  get_subtrl_cnt ;
796 
797           --
798           -- 1  there can be sub trailer without  grouping
799           -- 2  there could be multiple subtrailer
800           -- 3  sub trailer has to be counted when the total in subtrailer
801           -- 4  sub trailer has to be counted when the total in trailer or header
802           -- 5  g_rec_cnt calcualted when the header or trailer processes so
803           --    we have to count them for non grouping subtrailer
804 
805           if  g_subtrl_cnt is null or g_subtrl_cnt = 0  then
806               l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_rec_cnt + nvl(l_subtrl_cnt,0)  , P_frmt_mask_cd);
807           else
808                l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_rec_cnt+ nvl(g_subtrl_cnt,0)  , P_frmt_mask_cd);
809           end if ;
810 
811           --- sub grouping trailer
812           if p_rcd_typ_cd = 'L' and  p_group_val_01 is not null then
813              l_rslt_elmt := null ;
814              open get_ttl_cnt (p_group_val_01 ,
815                                p_group_val_02)  ;
816              fetch get_ttl_cnt into l_rslt_elmt ;
817              close  get_ttl_cnt ;
818 
819              hr_utility.set_location( ' ttl  count ' || l_rslt_elmt , 99 ) ;
820              hr_utility.set_location( ' su ttl   ' || l_subtrl_cnt , 99 ) ;
821              if l_rslt_elmt is not null or l_subtrl_cnt is not null  then
822                 l_rslt_elmt :=
823                  ben_ext_fmt.apply_format_mask(nvl(to_number(l_rslt_elmt),0)+
824                               nvl(l_subtrl_cnt,0),P_frmt_mask_cd);
825              end if ;
826 
827           end if ;
828 
829         ELSIF p_short_name = 'DTLCNT' THEN
830           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_dtl_cnt, P_frmt_mask_cd);
831            --- sub grouping trailer
832           if p_rcd_typ_cd = 'L' and  p_group_val_01 is not null then
833              l_rslt_elmt := null ;
834              open get_dtl_cnt (p_group_val_01 ,
835                                p_group_val_02)  ;
836              fetch get_dtl_cnt into l_rslt_elmt ;
837              close  get_dtl_cnt ;
838              hr_utility.set_location( ' dtl  count ' || l_rslt_elmt , 99 ) ;
839              if l_rslt_elmt is not null then
840                 l_rslt_elmt := ben_ext_fmt.apply_format_mask(l_rslt_elmt, P_frmt_mask_cd);
841              end if ;
842 
843           end if ;
844 
845         ELSIF p_short_name = 'HDRCNT' THEN
846           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_hdr_cnt, P_frmt_mask_cd);
847         ELSIF p_short_name = 'TRLCNT' THEN
848           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_trl_cnt, P_frmt_mask_cd);
849         ELSIF p_short_name = 'PERCNT' THEN
850           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_per_cnt, P_frmt_mask_cd);
851           if p_rcd_typ_cd = 'L' and  p_group_val_01 is not null then
852              l_rslt_elmt := null ;
853              open get_per_cnt (p_group_val_01 ,
854                                p_group_val_02)  ;
855              fetch get_per_cnt into l_rslt_elmt ;
856              close  get_per_cnt ;
857 
858              if l_rslt_elmt is not null then
859                 l_rslt_elmt := ben_ext_fmt.apply_format_mask(l_rslt_elmt, P_frmt_mask_cd);
860              end if ;
861 
862           end if ;
863 
864         ELSIF p_short_name = 'BSGRP' THEN
865           if p_ext_per_bg_id is not null then
866 
867               open bus_c (p_ext_per_bg_id );
868              fetch bus_c into l_business_group_name;
869              close bus_c;
870           else
871              open bus_c (p_business_group_id);
872              fetch bus_c into l_business_group_name;
873              close bus_c;
874           end if ;
875           l_rslt_elmt := l_business_group_name;
876 
877         ELSIF p_short_name = 'PBSGRP' THEN
878           open bus_c (p_business_group_id);
879           fetch bus_c into l_business_group_name;
880           close bus_c;
881           l_rslt_elmt := l_business_group_name;
882 
883         ELSIF p_short_name = 'SHDRCNT' THEN
884           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_subhdr_cnt, P_frmt_mask_cd);
885         ELSIF p_short_name = 'STRLCNT' THEN
886           l_rslt_elmt := ben_ext_fmt.apply_format_mask(g_subtrl_cnt, P_frmt_mask_cd);
887 
888         ELSIF p_short_name = 'EXRQID' THEN
889              l_rslt_elmt := ben_ext_fmt.apply_format_mask(p_request_id,P_frmt_mask_cd);
890         ELSIF p_short_name = 'SPOIDNO' THEN
891               open c_custid (p_pl_id ,'SPON');
892               fetch c_custid into l_rslt_elmt ;
893               close c_custid ;
894         ELSIF p_short_name = 'SPONAME' THEN
895               open c_rolname (p_pl_id ,'SPON');
896               fetch c_rolname into l_rslt_elmt ;
897               close c_rolname ;
898         ELSIF p_short_name = 'SPOPONO' THEN
899               open c_plcy (p_pl_id ,'SPON');
900               fetch c_plcy into l_rslt_elmt ;
901               close c_plcy ;
902         ELSIF p_short_name = 'SPOTYPCD' THEN
903                l_rslt_elmt  := 'SPON' ;
904         ELSIF p_short_name = 'INSIDNO' THEN
905               open c_custid (p_pl_id ,'INSR');
906               fetch c_custid into l_rslt_elmt ;
907               close c_custid ;
908         ELSIF p_short_name = 'INSNAME' THEN
909              open  c_rolname (p_pl_id ,'INSR');
910              fetch c_rolname into l_rslt_elmt ;
911              close c_rolname  ;
912         ELSIF p_short_name = 'INSPONO' THEN
913               open c_plcy (p_pl_id ,'INSR');
914               fetch c_plcy into l_rslt_elmt ;
915               close c_plcy ;
916         ELSIF p_short_name = 'INSTYPCD' THEN
917                l_rslt_elmt  := 'INSR' ;
918         ELSIF p_short_name = 'BROIDNO' THEN
919               open c_custid (p_pl_id ,'BROK');
920               fetch c_custid into l_rslt_elmt ;
921               close c_custid ;
922 
923         ELSIF p_short_name = 'BRONAME' THEN
924               open c_rolname (p_pl_id ,'BROK');
925               fetch c_rolname into l_rslt_elmt ;
926               close c_rolname ;
927         ELSIF p_short_name = 'BROPONO' THEN
928              open c_plcy (p_pl_id ,'BROK');
929               fetch c_plcy into l_Rslt_elmt ;
930               close c_plcy ;
931         ELSIF p_short_name = 'BROTYPCD' THEN
932               l_rslt_elmt  := 'BROK' ;
933         ELSIF p_short_name = 'PLPLCY' THEN
934               open  c_plplcy (p_pl_id );
935               fetch c_plplcy into l_rslt_elmt ;
936               close c_plplcy ;
937         ELSIF p_short_name = 'FILLER' THEN
938           l_rslt_elmt := null;
939         END IF;
940     ELSIF p_data_elmt_typ_cd = 'D' THEN
941 
942         IF p_short_name = 'BSGRP' THEN
943           if p_ext_per_bg_id is not null then
944               l_rslt_elmt := ben_ext_fmt.apply_decode
945                         (to_char(p_ext_per_bg_id),
946                         p_ext_data_elmt_id,
947                         p_dflt_val);
948           else
949              l_rslt_elmt := ben_ext_fmt.apply_decode
950                         (to_char(p_business_group_id),
951                         p_ext_data_elmt_id,
952                         p_dflt_val);
953           end if ;
954        ELSIF p_short_name = 'PBSGRP' THEN
955              l_rslt_elmt := ben_ext_fmt.apply_decode
956                         (to_char(p_business_group_id),
957                         p_ext_data_elmt_id,
958                         p_dflt_val);
959 
960        END IF ;
961 
962 
963    ELSIF p_data_elmt_typ_cd = 'R' THEN
964         -- data element is a rule:
965 
966            --
967        if g_debug then
968          hr_utility.set_location(' Rule546:'||P_data_elmt_rl, 39);
969        end if;
970 
971 
972        open  c_rule_type(P_data_elmt_rl);
973        fetch c_rule_type into l_rule_type;
974        close c_rule_type;
975 
976        if g_debug then
977          hr_utility.set_location(' rule type :'||l_rule_type.formula_type_id, 39);
978          hr_utility.set_location(' ext_dfn_id  :'||ben_ext_thread.g_ext_dfn_id, 39);
979          hr_utility.set_location(' rslt id  :'||ben_ext_thread.g_ext_rslt_id, 39);
980        end if;
981        if l_rule_type.formula_type_id = -546 then
982           l_outputs := benutils.formula
983                       (p_formula_id         => p_data_elmt_rl,
984                        p_effective_date     => p_effective_date ,
985                        p_business_group_id  => nvl(p_ext_per_bg_id, p_business_group_id)
986                        ,p_param1             => 'EXT_DFN_ID'
987                        ,p_param1_value       => to_char(nvl(ben_ext_thread.g_ext_dfn_id, -1))
988                        ,p_param2             => 'EXT_RSLT_ID'
989                        ,p_param2_value       => to_char(nvl(ben_ext_thread.g_ext_rslt_id, -1))
990                        ,p_param3             => 'EXT_USER_VALUE'
991                        ,p_param3_value       => p_string_val
992                        ,p_param4             => 'EXT_PROCESS_BUSINESS_GROUP'
993                        ,p_param4_value       =>  to_char(p_business_group_id)
994                     );
995 
996            l_rslt_elmt := l_outputs(l_outputs.first).value;
997            --
998            hr_utility.set_location(' rule result :'|| l_rslt_elmt, 39);
999 
1000            -- format mask handling
1001            if p_frmt_mask_lookup_cd is not null then
1002               begin
1003                 if substr(p_frmt_mask_lookup_cd,1,1) = 'N' then
1004                     l_rslt_elmt_fmt := ben_ext_fmt.apply_format_mask(to_number(l_rslt_elmt), p_frmt_mask_cd);
1005                     l_rslt_elmt := l_rslt_elmt_fmt;
1006                 end if;
1007                 if substr(p_frmt_mask_lookup_cd,1,1) = 'D' then
1008                     l_rslt_elmt_fmt :=  ben_ext_fmt.apply_format_mask(fnd_date.canonical_to_date(l_rslt_elmt), p_frmt_mask_cd);
1009                     l_rslt_elmt := l_rslt_elmt_fmt;
1010                 end if ;
1011               exception  -- incase l_rslt_elmt is not valid for formatting, just don't format it.
1012                 when others then
1013                   null;
1014               end;
1015            end if ;
1016            hr_utility.set_location(' rule result frmt :'|| l_rslt_elmt, 39);
1017        else
1018          --- this avoid copying the previous result
1019          l_rslt_elmt := null ;
1020        end if ;
1021 
1022       ELSIF p_data_elmt_typ_cd = 'T' THEN
1023         --
1024         -- data element is a total:
1025         -- ---------------------------------------------------------
1026         ben_ext_smart_total.calc_smart_total
1027         (p_ext_rslt_id => p_ext_rslt_id,
1028          p_ttl_fnctn_cd => p_ttl_fnctn_cd,
1029          p_ttl_sum_ext_data_elmt_id => p_ttl_sum_ext_data_elmt_id,
1030          p_ttl_cond_ext_data_elmt_id => p_ttl_cond_ext_data_elmt_id,
1031          p_ext_data_elmt_id => p_ext_data_elmt_id,
1032          p_frmt_mask_cd => p_frmt_mask_cd,
1033          p_ext_file_id => p_ext_file_id,
1034          p_business_group_id => p_business_group_id,
1035          p_smart_total => l_rslt_elmt);
1036 
1037 
1038     end if ;
1039 
1040     return l_rslt_elmt ;
1041 
1042 Exception
1043    when others then
1044     raise;  -- such that the calling pgm will handle the rest.
1045 
1046 
1047 End get_ht_elmt_data ;
1048 
1049 
1050 
1051 -- ----------------------------------------------------------------------------
1052 -- |---------< get_calc_value >---------------------------------------------|
1053 -- ----------------------------------------------------------------------------
1054 function get_calc_value
1055                          (
1056                           p_ext_rslt_id         in number,
1057                           p_ext_file_id         in number,
1058                           p_data_elmt_typ_cd    in varchar2 ,
1059                           p_short_name          in varchar2 ,
1060                           p_ext_data_elmt_id    in number,
1061                           p_frmt_mask_cd        in varchar2 default null,
1062                           p_frmt_mask_lookup_cd in varchar2 default null,
1063                           p_dflt_val            in varchar2 default null,
1064                           p_data_elmt_rl        in number   default null,
1065                           p_pl_id               in number   default null,
1066                           p_business_group_id   in number,
1067                           p_effective_date      in date ,
1068                           p_request_id          in number ,
1069                           p_calc                in varchar2 ,
1070                           p_rcd_typ_cd          in varchar2 default null
1071                            ) return varchar2 IS
1072 --
1073  l_proc               varchar2(72) := g_package||'get_calc_value';
1074  l_number                       varchar2(1);
1075  l_max_len                      integer ;
1076 --
1077  l_rslt_elmt          varchar2(4000) ;
1078  l_rslt_calc          varchar2(4000) ;
1079  Cursor c_calc_elmt is
1080  select ewc.seq_num
1081        ,xel.ext_data_elmt_id
1082        , xel.data_elmt_typ_cd
1083        , xel.data_elmt_rl
1084        , xel.name
1085        , xel.string_val
1086        , xel.dflt_val
1087        , xel.max_length_num
1088        , xel.ttl_fnctn_cd
1089        , xel.ttl_cond_oper_cd
1090        , xel.ttl_cond_val
1091        , xel.ttl_sum_ext_data_elmt_id
1092        , xel.ttl_cond_ext_data_elmt_id
1093        , efl.short_name
1094  from ben_Ext_where_clause ewc,
1095       ben_Ext_data_elmt    xel,
1096       ben_ext_fld          efl
1097  where ewc.ext_data_elmt_id = p_ext_data_elmt_id
1098    and xel.ext_data_elmt_id = ewc.cond_ext_data_elmt_id
1099    and xel.ext_fld_id       = efl.ext_fld_id (+)  ;
1100 begin
1101 
1102     hr_utility.set_location('Entering'||l_proc, 5);
1103     for elmt in c_calc_elmt Loop
1104             l_rslt_calc :=get_ht_elmt_data(p_ext_rslt_id        => p_ext_rslt_id,
1105                           p_data_elmt_typ_cd    => elmt.data_elmt_typ_cd ,
1106                           p_short_name          => elmt.short_name ,
1107                           p_ext_data_elmt_id    => elmt.ext_data_elmt_id,
1108                           p_frmt_mask_cd        => p_frmt_mask_cd ,
1109                           p_frmt_mask_lookup_cd => p_frmt_mask_lookup_cd,
1110                           p_dflt_val            => elmt.dflt_val  ,
1111                           p_data_elmt_rl        => elmt.data_elmt_rl ,
1112                           p_pl_id               => p_pl_id ,
1113                           p_business_group_id   => p_business_group_id,
1114                           p_effective_date      => p_effective_date ,
1115                           p_request_id          => p_request_id,
1116                           p_ext_file_id         => p_ext_file_id,
1117                           p_ttl_fnctn_cd        => elmt.ttl_fnctn_cd ,
1118                           p_ttl_sum_ext_data_elmt_id  =>   elmt.ttl_sum_ext_data_elmt_id,
1119                           p_ttl_cond_ext_data_elmt_id =>  elmt.ttl_cond_ext_data_elmt_id,
1120                           p_rcd_typ_cd          => p_rcd_typ_cd
1121                           ) ;
1122             hr_utility.set_location (' HT ' || elmt.short_name || l_rslt_calc , 999 );
1123             hr_utility.set_location (' type  ' || elmt.short_name || elmt.data_elmt_typ_cd , 999 );
1124 
1125          Begin
1126             l_rslt_calc := to_number(l_rslt_calc) ;
1127          exception
1128             when value_error then
1129                l_rslt_calc := null ;
1130          end ;
1131 
1132          if  l_rslt_elmt is null  then
1133              l_rslt_elmt := l_rslt_calc ;
1134          else
1135             if l_rslt_calc is not null then
1136                l_rslt_elmt := ben_ext_fmt.Calculate_calc_value
1137                                (p_firtst_value   => to_number(l_rslt_elmt)
1138                                ,p_second_value   => l_rslt_calc
1139                                ,p_calc           => p_calc ) ;
1140             end if ;
1141          end if ;
1142          hr_utility.set_location (' HT result ' ||  l_rslt_elmt , 999 );
1143     end loop ;
1144     l_rslt_elmt := ben_ext_fmt.apply_format_mask(to_number(l_rslt_elmt), p_frmt_mask_cd);
1145 
1146 
1147    hr_utility.set_location (' HT return result ' ||  l_rslt_elmt , 999 );
1148    hr_utility.set_location(' Exiting:'||l_proc, 15);
1149    return l_rslt_elmt ;
1150 end get_calc_value ;
1151 
1152 
1153 
1154 
1155 --
1156 -- ----------------------------------------------------------------------------
1157 -- |--------------------------< process_ext_ht_recs >-------------------------|
1158 -- ----------------------------------------------------------------------------
1159 --
1160 Procedure process_ext_ht_recs(p_ext_rslt_id         in number,
1161                               p_ext_file_id         in number,
1162                               p_ext_typ_cd          in varchar2,
1163                               p_rcd_typ_cd          in varchar2,
1164                               p_business_group_id   in number,
1165                               p_effective_date      in date,
1166                               p_group_val_01        in varchar2  default null,
1167                               p_group_val_02        in varchar2  default null,
1168                               p_request_id          in number default null,
1169                               p_ext_crit_prfl_id    in number default null,
1170                               p_ext_per_bg_id       in number default null
1171                              )
1172                               IS
1173 --
1174   l_proc               varchar2(72);
1175 --
1176   l_chg                varchar2(1) := 'I';
1177   l_ext_rcd_id         number(15) := null;
1178   l_ext_rcd_in_file_id number(15) := null;
1179   l_rslt_elmt          varchar2(600) := null;  -- UTF8
1180 --
1181   l_ext_rslt_dtl_id        number(15);
1182   l_object_version_number  number(15);
1183 --
1184   l_trans_num              number(15);
1185   l_person_id              number(15);
1186   l_prmy_sort_val          ben_ext_rslt_dtl.prmy_sort_val%TYPE;  -- UTF8 varchar2(30);
1187   l_scnd_sort_val          ben_ext_rslt_dtl.scnd_sort_val%TYPE;  -- UTF8 varchar2(30);
1188   l_exclude_this_rcd_flag  boolean;
1189   l_write_rcd              varchar2(1);
1190   l_elmt_name              varchar2(2000) ;
1191   l_error_message          varchar2(2000) ;
1192   l_rqd_elmt_available     varchar2(1) := 'Y' ;
1193   l_group_val_01           varchar2(2000) ;
1194   l_group_val_02           varchar2(2000) ;
1195 --
1196   cursor ext_rcd_typ_c is
1197   select a.ext_rcd_id,
1198          b.ext_rcd_in_file_id,
1199          b.seq_num,
1200          b.sprs_cd,
1201          b.rqd_flag
1202   from   ben_ext_rcd          a,
1203          ben_ext_rcd_in_file  b
1204   where  a.ext_rcd_id  = b.ext_rcd_id
1205   and    b.ext_file_id = p_ext_file_id
1206   and    a.rcd_type_cd = p_rcd_typ_cd
1207   order by b.seq_num;
1208 --
1209   cursor rcd_elmt_c  is
1210   select a.ext_data_elmt_in_rcd_id,
1211          a.seq_num,
1212          a.sprs_cd,
1213          a.strt_pos,
1214          a.dlmtr_val,
1215          a.rqd_flag,
1216          b.ext_data_elmt_id,
1217          b.data_elmt_typ_cd,
1218          b.data_elmt_rl,
1219          b.name,
1220          hr_general.decode_lookup('BEN_EXT_FRMT_MASK',b.frmt_mask_cd) frmt_mask_cd,
1221          b.frmt_mask_cd frmt_mask_lookup_cd ,
1222          b.string_val,
1223          b.dflt_val,
1224          b.max_length_num,
1225          b.just_cd,
1226          b.ttl_fnctn_cd,
1227          b.ttl_cond_oper_cd,
1228          b.ttl_cond_val,
1229          b.ttl_sum_ext_data_elmt_id,
1230          b.ttl_cond_ext_data_elmt_id,
1231          c.short_name
1232   from   ben_ext_data_elmt_in_rcd    a,
1233          ben_ext_data_elmt           b,
1234          ben_ext_fld                 c
1235   where  a.ext_rcd_id = l_ext_rcd_id
1236   and    a.ext_data_elmt_id = b.ext_data_elmt_id
1237   and    b.ext_fld_id = c.ext_fld_id (+)
1238   order by a.seq_num;
1239 --
1240 
1241   CURSOR get_pl_val IS
1242   SELECT b.val_1
1243   FROM ben_ext_crit_typ a,
1244        ben_ext_crit_val b
1245   WHERE  a.ext_crit_typ_id = b.ext_crit_typ_id
1246     and  a.crit_typ_cd = 'BPL'
1247     and  a.ext_crit_prfl_id = p_ext_crit_prfl_id;
1248 
1249 
1250   cursor get_subtrl_cnt is
1251   select count(*)
1252   from   ben_Ext_rslt_dtl xrd,
1253          ben_ext_rcd rcd,
1254          ben_ext_rcd_in_file erf
1255    where  xrd.ext_rslt_id = p_ext_rslt_id
1256     and   xrd.ext_rcd_id = erf.ext_rcd_id
1257     and   xrd.ext_rcd_in_file_id  = erf.ext_rcd_in_file_id
1258     and   erf.ext_file_id = p_ext_file_id
1259     and   rcd.ext_rcd_id = erf.ext_rcd_id
1260     and   erf.hide_flag    = 'N'
1261    and    rcd.rcd_type_cd = 'L' ;
1262 
1263 l_pl_id   number ;
1264 l_dummy_var  varchar2(1000) ;
1265 --
1266 begin
1267 --
1268   g_debug := hr_utility.debug_enabled;
1269   if g_debug then
1270     l_proc := g_package||'.process_ext_ht_recs';
1271     hr_utility.set_location('Entering'||l_proc, 5);
1272   end if;
1273 --
1274   savepoint head_trail;
1275 --
1276 -- assign values for sorting
1277 -- for now let's do this, but maybe there is a better way
1278 --
1279   IF p_rcd_typ_cd = 'H' THEN
1280     l_person_id := 0;
1281     l_trans_num := 0;
1282     l_prmy_sort_val := '   ';
1283     l_scnd_sort_val := '   ';
1284     l_group_val_01  := '   ' ;
1285     l_group_val_02  := '   ' ;
1286   elsif   p_rcd_typ_cd = 'T' THEN
1287     l_person_id := 999999999999;
1288     l_trans_num := 999999999999;
1289     l_prmy_sort_val := null;
1290     l_scnd_sort_val := null;
1291     l_group_val_01  := null ;
1292     l_group_val_02  := null ;
1293   elsif  p_rcd_typ_cd = 'L' THEN
1294     l_person_id := 999999999999;
1295     l_trans_num := 999999999999;
1296     l_prmy_sort_val := null;
1297     l_scnd_sort_val := null;
1298     l_group_val_01  := p_group_val_01 ;
1299     l_group_val_02  := p_group_val_02 ;
1300     if p_group_val_01 = '   '  then
1301        l_group_val_01  := null ;
1302        l_group_val_02  := null ;
1303     elsif p_group_val_02 = '   '  then
1304        l_group_val_02  := null ;
1305     end if ;
1306 
1307 
1308   END IF;
1309 
1310   if  p_rcd_typ_cd <>  'L' and g_subtrl_cnt = 0 and
1311       ben_ext_thread.g_ext_group_elmt1 is not null   then
1312      open  get_subtrl_cnt   ;
1313      fetch get_subtrl_cnt into g_subtrl_cnt ;
1314      close  get_subtrl_cnt ;
1315   end if ;
1316 
1317 -- Get the  plan type for ansi
1318   open get_pl_val ;
1319   fetch get_pl_val into l_pl_id ;
1320   close get_pl_val ;
1321 
1322 
1323 -- This loop will be executed for each record in the extract definition
1324 -- of a given record type (Header, Trailer)
1325 --
1326   FOR rcd IN ext_rcd_typ_c LOOP
1327 --
1328     l_ext_rcd_id          := rcd.ext_rcd_id;
1329     l_ext_rcd_in_file_id  := rcd.ext_rcd_in_file_id;
1330     l_rqd_elmt_available  := 'Y';
1331 --
1332 -- Initialize array
1333 --
1334     ben_ext_fmt.g_val_tab := g_val_def;
1335 --
1336     FOR elmt IN rcd_elmt_c LOOP
1337 --
1338       l_rslt_elmt := null ;
1339       if g_debug then
1340         hr_utility.set_location('element '||elmt.short_name, 5);
1341         hr_utility.set_location('element type  '||elmt.data_elmt_typ_cd, 5);
1342       end if;
1343       IF elmt.data_elmt_typ_cd in ( 'F','D','R')  THEN
1344 
1345           l_rslt_elmt := get_ht_elmt_data(p_ext_rslt_id        => p_ext_rslt_id,
1346                           p_data_elmt_typ_cd    => elmt.data_elmt_typ_cd ,
1347                           p_short_name          => elmt.short_name ,
1348                           p_ext_data_elmt_id    => elmt.ext_data_elmt_id,
1349                           p_frmt_mask_cd        => elmt.frmt_mask_cd ,
1350                           p_frmt_mask_lookup_cd => elmt.frmt_mask_lookup_cd,
1351                           p_dflt_val            => elmt.dflt_val  ,
1352                           p_data_elmt_rl        => elmt.data_elmt_rl ,
1353                           p_pl_id               => l_pl_id ,
1354                           p_business_group_id   => p_business_group_id,
1355                           p_effective_date      => p_effective_date ,
1356                           p_request_id          => p_request_id ,
1357                           p_ext_file_id         => p_ext_file_id,
1358                           p_rcd_typ_cd          => p_rcd_typ_cd ,
1359                           p_group_val_01        => l_group_val_01,
1360                           p_group_val_02        => l_group_val_02,
1361                           p_ext_per_bg_id       => p_ext_per_bg_id ,
1362                           p_String_val          => elmt.String_val
1363                           ) ;
1364 
1365          if substr(elmt.frmt_mask_lookup_cd,1,1)  = 'C' then
1366             l_rslt_elmt := ben_ext_fmt.apply_format_Function(l_rslt_elmt, elmt.frmt_mask_lookup_cd);
1367          end if ;
1368 
1369 --
1370       ELSIF elmt.data_elmt_typ_cd = 'C' THEN
1371                l_rslt_elmt :=   get_calc_value
1372                          (p_ext_rslt_id        => p_ext_rslt_id,
1373                           p_data_elmt_typ_cd    => elmt.data_elmt_typ_cd ,
1374                           p_short_name          => elmt.short_name ,
1375                           p_ext_data_elmt_id    => elmt.ext_data_elmt_id,
1376                           p_frmt_mask_cd        => elmt.frmt_mask_cd ,
1377                           p_frmt_mask_lookup_cd => elmt.frmt_mask_lookup_cd,
1378                           p_dflt_val            => elmt.dflt_val  ,
1379                           p_data_elmt_rl        => elmt.data_elmt_rl ,
1380                           p_pl_id               => l_pl_id ,
1381                           p_business_group_id   => p_business_group_id,
1382                           p_effective_date      => p_effective_date ,
1383                           p_request_id          => p_request_id,
1384                           p_ext_file_id         => p_ext_file_id,
1385                           p_calc                => elmt.ttl_fnctn_cd ,
1386                           p_rcd_typ_cd          => p_rcd_typ_cd
1387                          );
1388 
1389 --
1390       ELSIF elmt.data_elmt_typ_cd = 'S' THEN
1391             --
1392             -- data element is a string:
1393             l_rslt_elmt := elmt.string_val;
1394             if g_debug then
1395               hr_utility.set_location(' mass lengh   :'||elmt.max_length_num , 39);
1396             end if;
1397              IF elmt.max_length_num is not null THEN
1398                  l_rslt_elmt := rPad(nvl(l_rslt_elmt,' '),elmt.max_length_num );
1399                  if g_debug then
1400                    hr_utility.set_location('ele    :'||l_rslt_elmt , 39);
1401                  end if;
1402              end if ;
1403       ELSIF elmt.data_elmt_typ_cd = 'T' THEN
1404 
1405         ben_ext_smart_total.calc_smart_total
1406         (p_ext_rslt_id => p_ext_rslt_id,
1407          p_ttl_fnctn_cd => elmt.ttl_fnctn_cd,
1408          p_ttl_sum_ext_data_elmt_id => elmt.ttl_sum_ext_data_elmt_id,
1409          p_ttl_cond_ext_data_elmt_id => elmt.ttl_cond_ext_data_elmt_id,
1410          p_ext_data_elmt_id => elmt.ext_data_elmt_id,
1411          p_frmt_mask_cd => elmt.frmt_mask_cd,
1412          p_ext_file_id => p_ext_file_id,
1413          p_group_val_01  => p_group_val_01 ,
1414          p_group_val_02  => p_group_val_02 ,
1415          p_business_group_id => p_business_group_id,
1416          p_smart_total => l_rslt_elmt);
1417 
1418          --- set fromat mask
1419 
1420          ---
1421 
1422        /*
1423 
1424         l_rslt_elmt :=get_ht_elmt_data(p_ext_rslt_id  => p_ext_rslt_id,
1425                           p_data_elmt_typ_cd    => elmt.data_elmt_typ_cd ,
1426                           p_short_name          => elmt.short_name ,
1427                           p_ext_data_elmt_id    => elmt.ext_data_elmt_id,
1428                           p_frmt_mask_cd        => elmt.frmt_mask_cd ,
1429                           p_dflt_val            => elmt.dflt_val  ,
1430                           p_data_elmt_rl        => elmt.data_elmt_rl ,
1431                           p_pl_id               => l_pl_id ,
1432                           p_business_group_id   => p_business_group_id,
1433                           p_effective_date      => p_effective_date ,
1434                           p_request_id          => p_request_id,
1435                           p_ext_file_id         => p_ext_file_id,
1436                           p_ttl_fnctn_cd        => elmt.ttl_fnctn_cd ,
1437                           p_ttl_sum_ext_data_elmt_id  =>   elmt.ttl_sum_ext_data_elmt_id,
1438                           p_ttl_cond_ext_data_elmt_id =>  elmt.ttl_cond_ext_data_elmt_id
1439                           ) ;
1440 
1441 
1442       */
1443 --
1444       END IF;
1445 --
1446       -- if resulting data element is null, substitute it with
1447       -- default value
1448 --
1449       IF l_rslt_elmt is null then
1450 --
1451         l_rslt_elmt := elmt.dflt_val;
1452 --
1453       END IF;
1454 --
1455       -- truncate data element
1456 --
1457       IF elmt.max_length_num is not null THEN
1458 --
1459         l_rslt_elmt := SUBSTR(l_rslt_elmt, 1, elmt.max_length_num);
1460 --
1461       END IF;
1462 --
1463       if g_debug then
1464         hr_utility.set_location(' max ele    :'||l_rslt_elmt , 39);
1465       end if;
1466       -- if data element is mandatory, but l_rslt_elmt is null then
1467       -- raise ht error
1468       -- when the element is required and not elemnt skip the record
1469       -- dont skip the entire header or details : tilak
1470 
1471 --
1472       IF elmt.rqd_flag = 'Y' and (l_rslt_elmt is null) then
1473 --
1474         g_err_name  := 'BEN_91887_EXT_RQD_DATA_ELMT';
1475         l_elmt_name :=  elmt.name ;
1476         l_error_message :=
1477         ben_ext_fmt.get_error_msg(to_number(substr(g_err_name, 5, 5)),g_err_name,l_elmt_name );
1478         -- when the elmt is null and required  can not create a record , if the record also required
1479         -- then throw the error or throw the warning , the assumption is  header allwasy has one record
1480         -- for a record definition
1481         if nvl(rcd.rqd_flag,'N') =  'Y' then
1482            write_error(p_err_num    => to_number(substr(g_err_name, 5, 5)),
1483                 p_err_name          => l_error_message,
1484                 p_typ_cd            => 'F',
1485                 p_person_id         => null,
1486                 p_request_id        => fnd_global.conc_request_id,
1487                 p_ext_rslt_id       => p_ext_rslt_id,
1488                 p_business_group_id => p_business_group_id
1489                );
1490             raise g_ht_error;
1491         else
1492 
1493               write_error(p_err_num => to_number(substr(g_err_name, 5, 5)),
1494                 p_err_name          => l_error_message,
1495                 p_typ_cd            => 'W',
1496                 p_person_id         => null,
1497                 p_request_id        => fnd_global.conc_request_id,
1498                 p_ext_rslt_id       => p_ext_rslt_id,
1499                 p_business_group_id => p_business_group_id
1500                );
1501 
1502         end if ;
1503         l_rqd_elmt_available  := 'N' ;
1504         exit ;
1505 --        raise g_ht_error;
1506 --
1507       END IF;
1508 --
1509       --val_tab(elmt.seq_num) := l_rslt_elmt;
1510       ben_ext_fmt.g_val_tab(elmt.seq_num) := l_rslt_elmt;
1511 --
1512     END LOOP;
1513 --
1514      if l_rqd_elmt_available = 'Y'  then
1515         l_write_rcd := 'Y';
1516         --
1517         ben_ext_adv_conditions.data_elmt_in_rcd
1518            (p_ext_rcd_id => l_ext_rcd_id,
1519             p_exclude_this_rcd_flag => l_exclude_this_rcd_flag);
1520          --
1521          if l_exclude_this_rcd_flag = true then
1522             l_write_rcd := 'N';
1523          end if;
1524            --
1525          if l_write_rcd = 'Y' then
1526              ben_ext_adv_conditions.rcd_in_file
1527               (p_ext_rcd_in_file_id => rcd.ext_rcd_in_file_id,
1528                p_sprs_cd => rcd.sprs_cd,
1529                p_exclude_this_rcd_flag => l_exclude_this_rcd_flag);
1530                --
1531               if l_exclude_this_rcd_flag = true then
1532                  l_write_rcd := 'N';
1533               end if;
1534               --
1535          end if;
1536 --
1537          if l_write_rcd = 'Y' then
1538 --
1539          --  call 'create ext detail api' here
1540 --
1541             ben_ext_rslt_dtl_api.create_ext_rslt_dtl
1542               (p_validate                   =>  false
1543              ,p_ext_rslt_dtl_id            =>  l_ext_rslt_dtl_id
1544              ,p_prmy_sort_val              =>  l_prmy_sort_val
1545              ,p_scnd_sort_val              =>  l_scnd_sort_val
1546              ,p_trans_seq_num              =>  l_trans_num
1547              ,p_rcrd_seq_num               =>  rcd.seq_num
1548              ,p_ext_rslt_id                =>  p_ext_rslt_id
1549              ,p_ext_rcd_id                 =>  l_ext_rcd_id
1550              ,p_person_id                  =>  l_person_id
1551              ,p_business_group_id          =>  p_business_group_id
1552              ,p_val_01                     =>  ben_ext_fmt.g_val_tab(1)
1553              ,p_val_02                     =>  ben_ext_fmt.g_val_tab(2)
1554              ,p_val_03                     =>  ben_ext_fmt.g_val_tab(3)
1555              ,p_val_04                     =>  ben_ext_fmt.g_val_tab(4)
1556              ,p_val_05                     =>  ben_ext_fmt.g_val_tab(5)
1557              ,p_val_06                     =>  ben_ext_fmt.g_val_tab(6)
1558              ,p_val_07                     =>  ben_ext_fmt.g_val_tab(7)
1559              ,p_val_08                     =>  ben_ext_fmt.g_val_tab(8)
1560              ,p_val_09                     =>  ben_ext_fmt.g_val_tab(9)
1561              ,p_val_10                     =>  ben_ext_fmt.g_val_tab(10)
1562              ,p_val_11                     =>  ben_ext_fmt.g_val_tab(11)
1563              ,p_val_12                     =>  ben_ext_fmt.g_val_tab(12)
1564              ,p_val_13                     =>  ben_ext_fmt.g_val_tab(13)
1565              ,p_val_14                     =>  ben_ext_fmt.g_val_tab(14)
1566              ,p_val_15                     =>  ben_ext_fmt.g_val_tab(15)
1567              ,p_val_16                     =>  ben_ext_fmt.g_val_tab(16)
1568              ,p_val_17                     =>  ben_ext_fmt.g_val_tab(17)
1569              ,p_val_18                     =>  ben_ext_fmt.g_val_tab(18)
1570              ,p_val_19                     =>  ben_ext_fmt.g_val_tab(19)
1571              ,p_val_20                     =>  ben_ext_fmt.g_val_tab(20)
1572              ,p_val_21                     =>  ben_ext_fmt.g_val_tab(21)
1573              ,p_val_22                     =>  ben_ext_fmt.g_val_tab(22)
1574              ,p_val_23                     =>  ben_ext_fmt.g_val_tab(23)
1575              ,p_val_24                     =>  ben_ext_fmt.g_val_tab(24)
1576              ,p_val_25                     =>  ben_ext_fmt.g_val_tab(25)
1577              ,p_val_26                     =>  ben_ext_fmt.g_val_tab(26)
1578              ,p_val_27                     =>  ben_ext_fmt.g_val_tab(27)
1579              ,p_val_28                     =>  ben_ext_fmt.g_val_tab(28)
1580              ,p_val_29                     =>  ben_ext_fmt.g_val_tab(29)
1581              ,p_val_30                     =>  ben_ext_fmt.g_val_tab(30)
1582              ,p_val_31                     =>  ben_ext_fmt.g_val_tab(31)
1583              ,p_val_32                     =>  ben_ext_fmt.g_val_tab(32)
1584              ,p_val_33                     =>  ben_ext_fmt.g_val_tab(33)
1585              ,p_val_34                     =>  ben_ext_fmt.g_val_tab(34)
1586              ,p_val_35                     =>  ben_ext_fmt.g_val_tab(35)
1587              ,p_val_36                     =>  ben_ext_fmt.g_val_tab(36)
1588              ,p_val_37                     =>  ben_ext_fmt.g_val_tab(37)
1589              ,p_val_38                     =>  ben_ext_fmt.g_val_tab(38)
1590              ,p_val_39                     =>  ben_ext_fmt.g_val_tab(39)
1591              ,p_val_40                     =>  ben_ext_fmt.g_val_tab(40)
1592              ,p_val_41                     =>  ben_ext_fmt.g_val_tab(41)
1593              ,p_val_42                     =>  ben_ext_fmt.g_val_tab(42)
1594              ,p_val_43                     =>  ben_ext_fmt.g_val_tab(43)
1595              ,p_val_44                     =>  ben_ext_fmt.g_val_tab(44)
1596              ,p_val_45                     =>  ben_ext_fmt.g_val_tab(45)
1597              ,p_val_46                     =>  ben_ext_fmt.g_val_tab(46)
1598              ,p_val_47                     =>  ben_ext_fmt.g_val_tab(47)
1599              ,p_val_48                     =>  ben_ext_fmt.g_val_tab(48)
1600              ,p_val_49                     =>  ben_ext_fmt.g_val_tab(49)
1601              ,p_val_50                     =>  ben_ext_fmt.g_val_tab(50)
1602              ,p_val_51                     =>  ben_ext_fmt.g_val_tab(51)
1603              ,p_val_52                     =>  ben_ext_fmt.g_val_tab(52)
1604              ,p_val_53                     =>  ben_ext_fmt.g_val_tab(53)
1605              ,p_val_54                     =>  ben_ext_fmt.g_val_tab(54)
1606              ,p_val_55                     =>  ben_ext_fmt.g_val_tab(55)
1607              ,p_val_56                     =>  ben_ext_fmt.g_val_tab(56)
1608              ,p_val_57                     =>  ben_ext_fmt.g_val_tab(57)
1609              ,p_val_58                     =>  ben_ext_fmt.g_val_tab(58)
1610              ,p_val_59                     =>  ben_ext_fmt.g_val_tab(59)
1611              ,p_val_60                     =>  ben_ext_fmt.g_val_tab(60)
1612              ,p_val_61                     =>  ben_ext_fmt.g_val_tab(61)
1613              ,p_val_62                     =>  ben_ext_fmt.g_val_tab(62)
1614              ,p_val_63                     =>  ben_ext_fmt.g_val_tab(63)
1615              ,p_val_64                     =>  ben_ext_fmt.g_val_tab(64)
1616              ,p_val_65                     =>  ben_ext_fmt.g_val_tab(65)
1617              ,p_val_66                     =>  ben_ext_fmt.g_val_tab(66)
1618              ,p_val_67                     =>  ben_ext_fmt.g_val_tab(67)
1619              ,p_val_68                     =>  ben_ext_fmt.g_val_tab(68)
1620              ,p_val_69                     =>  ben_ext_fmt.g_val_tab(69)
1621              ,p_val_70                     =>  ben_ext_fmt.g_val_tab(70)
1622              ,p_val_71                     =>  ben_ext_fmt.g_val_tab(71)
1623              ,p_val_72                     =>  ben_ext_fmt.g_val_tab(72)
1624              ,p_val_73                     =>  ben_ext_fmt.g_val_tab(73)
1625              ,p_val_74                     =>  ben_ext_fmt.g_val_tab(74)
1626              ,p_val_75                     =>  ben_ext_fmt.g_val_tab(75)
1627              ,p_val_76                     =>  ben_ext_fmt.g_val_tab(76)
1628              ,p_val_77                     =>  ben_ext_fmt.g_val_tab(77)
1629              ,p_val_78                     =>  ben_ext_fmt.g_val_tab(78)
1630              ,p_val_79                     =>  ben_ext_fmt.g_val_tab(79)
1631              ,p_val_80                     =>  ben_ext_fmt.g_val_tab(80)
1632              ,p_val_81                     =>  ben_ext_fmt.g_val_tab(81)
1633              ,p_val_82                     =>  ben_ext_fmt.g_val_tab(82)
1634              ,p_val_83                     =>  ben_ext_fmt.g_val_tab(83)
1635              ,p_val_84                     =>  ben_ext_fmt.g_val_tab(84)
1636              ,p_val_85                     =>  ben_ext_fmt.g_val_tab(85)
1637              ,p_val_86                     =>  ben_ext_fmt.g_val_tab(86)
1638              ,p_val_87                     =>  ben_ext_fmt.g_val_tab(87)
1639              ,p_val_88                     =>  ben_ext_fmt.g_val_tab(88)
1640              ,p_val_89                     =>  ben_ext_fmt.g_val_tab(89)
1641              ,p_val_90                     =>  ben_ext_fmt.g_val_tab(90)
1642              ,p_val_91                     =>  ben_ext_fmt.g_val_tab(91)
1643              ,p_val_92                     =>  ben_ext_fmt.g_val_tab(92)
1644              ,p_val_93                     =>  ben_ext_fmt.g_val_tab(93)
1645              ,p_val_94                     =>  ben_ext_fmt.g_val_tab(94)
1646              ,p_val_95                     =>  ben_ext_fmt.g_val_tab(95)
1647              ,p_val_96                     =>  ben_ext_fmt.g_val_tab(96)
1648              ,p_val_97                     =>  ben_ext_fmt.g_val_tab(97)
1649              ,p_val_98                     =>  ben_ext_fmt.g_val_tab(98)
1650              ,p_val_99                     =>  ben_ext_fmt.g_val_tab(99)
1651              ,p_val_100                    =>  ben_ext_fmt.g_val_tab(100)
1652              ,p_val_101                    =>  ben_ext_fmt.g_val_tab(101)
1653              ,p_val_102                    =>  ben_ext_fmt.g_val_tab(102)
1654              ,p_val_103                    =>  ben_ext_fmt.g_val_tab(103)
1655              ,p_val_104                    =>  ben_ext_fmt.g_val_tab(104)
1656              ,p_val_105                    =>  ben_ext_fmt.g_val_tab(105)
1657              ,p_val_106                    =>  ben_ext_fmt.g_val_tab(106)
1658              ,p_val_107                    =>  ben_ext_fmt.g_val_tab(107)
1659              ,p_val_108                    =>  ben_ext_fmt.g_val_tab(108)
1660              ,p_val_109                    =>  ben_ext_fmt.g_val_tab(109)
1661              ,p_val_110                    =>  ben_ext_fmt.g_val_tab(110)
1662              ,p_val_111                    =>  ben_ext_fmt.g_val_tab(111)
1663              ,p_val_112                    =>  ben_ext_fmt.g_val_tab(112)
1664              ,p_val_113                    =>  ben_ext_fmt.g_val_tab(113)
1665              ,p_val_114                    =>  ben_ext_fmt.g_val_tab(114)
1666              ,p_val_115                    =>  ben_ext_fmt.g_val_tab(115)
1667              ,p_val_116                    =>  ben_ext_fmt.g_val_tab(116)
1668              ,p_val_117                    =>  ben_ext_fmt.g_val_tab(117)
1669              ,p_val_118                    =>  ben_ext_fmt.g_val_tab(118)
1670              ,p_val_119                    =>  ben_ext_fmt.g_val_tab(119)
1671              ,p_val_120                    =>  ben_ext_fmt.g_val_tab(120)
1672              ,p_val_121                    =>  ben_ext_fmt.g_val_tab(121)
1673              ,p_val_122                    =>  ben_ext_fmt.g_val_tab(122)
1674              ,p_val_123                    =>  ben_ext_fmt.g_val_tab(123)
1675              ,p_val_124                    =>  ben_ext_fmt.g_val_tab(124)
1676              ,p_val_125                    =>  ben_ext_fmt.g_val_tab(125)
1677              ,p_val_126                    =>  ben_ext_fmt.g_val_tab(126)
1678              ,p_val_127                    =>  ben_ext_fmt.g_val_tab(127)
1679              ,p_val_128                    =>  ben_ext_fmt.g_val_tab(128)
1680              ,p_val_129                    =>  ben_ext_fmt.g_val_tab(129)
1681              ,p_val_130                    =>  ben_ext_fmt.g_val_tab(130)
1682              ,p_val_131                    =>  ben_ext_fmt.g_val_tab(131)
1683              ,p_val_132                    =>  ben_ext_fmt.g_val_tab(132)
1684              ,p_val_133                    =>  ben_ext_fmt.g_val_tab(133)
1685              ,p_val_134                    =>  ben_ext_fmt.g_val_tab(134)
1686              ,p_val_135                    =>  ben_ext_fmt.g_val_tab(135)
1687              ,p_val_136                    =>  ben_ext_fmt.g_val_tab(136)
1688              ,p_val_137                    =>  ben_ext_fmt.g_val_tab(137)
1689              ,p_val_138                    =>  ben_ext_fmt.g_val_tab(138)
1690              ,p_val_139                    =>  ben_ext_fmt.g_val_tab(139)
1691              ,p_val_140                    =>  ben_ext_fmt.g_val_tab(140)
1692              ,p_val_141                    =>  ben_ext_fmt.g_val_tab(141)
1693              ,p_val_142                    =>  ben_ext_fmt.g_val_tab(142)
1694              ,p_val_143                    =>  ben_ext_fmt.g_val_tab(143)
1695              ,p_val_144                    =>  ben_ext_fmt.g_val_tab(144)
1696              ,p_val_145                    =>  ben_ext_fmt.g_val_tab(145)
1697              ,p_val_146                    =>  ben_ext_fmt.g_val_tab(146)
1698              ,p_val_147                    =>  ben_ext_fmt.g_val_tab(147)
1699              ,p_val_148                    =>  ben_ext_fmt.g_val_tab(148)
1700              ,p_val_149                    =>  ben_ext_fmt.g_val_tab(149)
1701              ,p_val_150                    =>  ben_ext_fmt.g_val_tab(150)
1702              ,p_val_151                    =>  ben_ext_fmt.g_val_tab(151)
1703              ,p_val_152                    =>  ben_ext_fmt.g_val_tab(152)
1704              ,p_val_153                    =>  ben_ext_fmt.g_val_tab(153)
1705              ,p_val_154                    =>  ben_ext_fmt.g_val_tab(154)
1706              ,p_val_155                    =>  ben_ext_fmt.g_val_tab(155)
1707              ,p_val_156                    =>  ben_ext_fmt.g_val_tab(156)
1708              ,p_val_157                    =>  ben_ext_fmt.g_val_tab(157)
1709              ,p_val_158                    =>  ben_ext_fmt.g_val_tab(158)
1710              ,p_val_159                    =>  ben_ext_fmt.g_val_tab(159)
1711              ,p_val_160                    =>  ben_ext_fmt.g_val_tab(160)
1712              ,p_val_161                    =>  ben_ext_fmt.g_val_tab(161)
1713              ,p_val_162                    =>  ben_ext_fmt.g_val_tab(162)
1714              ,p_val_163                    =>  ben_ext_fmt.g_val_tab(163)
1715              ,p_val_164                    =>  ben_ext_fmt.g_val_tab(164)
1716              ,p_val_165                    =>  ben_ext_fmt.g_val_tab(165)
1717              ,p_val_166                    =>  ben_ext_fmt.g_val_tab(166)
1718              ,p_val_167                    =>  ben_ext_fmt.g_val_tab(167)
1719              ,p_val_168                    =>  ben_ext_fmt.g_val_tab(168)
1720              ,p_val_169                    =>  ben_ext_fmt.g_val_tab(169)
1721              ,p_val_170                    =>  ben_ext_fmt.g_val_tab(170)
1722              ,p_val_171                    =>  ben_ext_fmt.g_val_tab(171)
1723              ,p_val_172                    =>  ben_ext_fmt.g_val_tab(172)
1724              ,p_val_173                    =>  ben_ext_fmt.g_val_tab(173)
1725              ,p_val_174                    =>  ben_ext_fmt.g_val_tab(174)
1726              ,p_val_175                    =>  ben_ext_fmt.g_val_tab(175)
1727              ,p_val_176                    =>  ben_ext_fmt.g_val_tab(176)
1728              ,p_val_177                    =>  ben_ext_fmt.g_val_tab(177)
1729              ,p_val_178                    =>  ben_ext_fmt.g_val_tab(178)
1730              ,p_val_179                    =>  ben_ext_fmt.g_val_tab(179)
1731              ,p_val_180                    =>  ben_ext_fmt.g_val_tab(180)
1732              ,p_val_181                    =>  ben_ext_fmt.g_val_tab(181)
1733              ,p_val_182                    =>  ben_ext_fmt.g_val_tab(182)
1734              ,p_val_183                    =>  ben_ext_fmt.g_val_tab(183)
1735              ,p_val_184                    =>  ben_ext_fmt.g_val_tab(184)
1736              ,p_val_185                    =>  ben_ext_fmt.g_val_tab(185)
1737              ,p_val_186                    =>  ben_ext_fmt.g_val_tab(186)
1738              ,p_val_187                    =>  ben_ext_fmt.g_val_tab(187)
1739              ,p_val_188                    =>  ben_ext_fmt.g_val_tab(188)
1740              ,p_val_189                    =>  ben_ext_fmt.g_val_tab(189)
1741              ,p_val_190                    =>  ben_ext_fmt.g_val_tab(190)
1742              ,p_val_191                    =>  ben_ext_fmt.g_val_tab(191)
1743              ,p_val_192                    =>  ben_ext_fmt.g_val_tab(192)
1744              ,p_val_193                    =>  ben_ext_fmt.g_val_tab(193)
1745              ,p_val_194                    =>  ben_ext_fmt.g_val_tab(194)
1746              ,p_val_195                    =>  ben_ext_fmt.g_val_tab(195)
1747              ,p_val_196                    =>  ben_ext_fmt.g_val_tab(196)
1748              ,p_val_197                    =>  ben_ext_fmt.g_val_tab(197)
1749              ,p_val_198                    =>  ben_ext_fmt.g_val_tab(198)
1750              ,p_val_199                    =>  ben_ext_fmt.g_val_tab(199)
1751              ,p_val_200                    =>  ben_ext_fmt.g_val_tab(200)
1752              ,p_val_201                    =>  ben_ext_fmt.g_val_tab(201)
1753              ,p_val_202                    =>  ben_ext_fmt.g_val_tab(202)
1754              ,p_val_203                    =>  ben_ext_fmt.g_val_tab(203)
1755              ,p_val_204                    =>  ben_ext_fmt.g_val_tab(204)
1756              ,p_val_205                    =>  ben_ext_fmt.g_val_tab(205)
1757              ,p_val_206                    =>  ben_ext_fmt.g_val_tab(206)
1758              ,p_val_207                    =>  ben_ext_fmt.g_val_tab(207)
1759              ,p_val_208                    =>  ben_ext_fmt.g_val_tab(208)
1760              ,p_val_209                    =>  ben_ext_fmt.g_val_tab(209)
1761              ,p_val_210                    =>  ben_ext_fmt.g_val_tab(210)
1762              ,p_val_211                    =>  ben_ext_fmt.g_val_tab(211)
1763              ,p_val_212                    =>  ben_ext_fmt.g_val_tab(212)
1764              ,p_val_213                    =>  ben_ext_fmt.g_val_tab(213)
1765              ,p_val_214                    =>  ben_ext_fmt.g_val_tab(214)
1766              ,p_val_215                    =>  ben_ext_fmt.g_val_tab(215)
1767              ,p_val_216                    =>  ben_ext_fmt.g_val_tab(216)
1768              ,p_val_217                    =>  ben_ext_fmt.g_val_tab(217)
1769              ,p_val_218                    =>  ben_ext_fmt.g_val_tab(218)
1770              ,p_val_219                    =>  ben_ext_fmt.g_val_tab(219)
1771              ,p_val_220                    =>  ben_ext_fmt.g_val_tab(220)
1772              ,p_val_221                    =>  ben_ext_fmt.g_val_tab(221)
1773              ,p_val_222                    =>  ben_ext_fmt.g_val_tab(222)
1774              ,p_val_223                    =>  ben_ext_fmt.g_val_tab(223)
1775              ,p_val_224                    =>  ben_ext_fmt.g_val_tab(224)
1776              ,p_val_225                    =>  ben_ext_fmt.g_val_tab(225)
1777              ,p_val_226                    =>  ben_ext_fmt.g_val_tab(226)
1778              ,p_val_227                    =>  ben_ext_fmt.g_val_tab(227)
1779              ,p_val_228                    =>  ben_ext_fmt.g_val_tab(228)
1780              ,p_val_229                    =>  ben_ext_fmt.g_val_tab(229)
1781              ,p_val_230                    =>  ben_ext_fmt.g_val_tab(230)
1782              ,p_val_231                    =>  ben_ext_fmt.g_val_tab(231)
1783              ,p_val_232                    =>  ben_ext_fmt.g_val_tab(232)
1784              ,p_val_233                    =>  ben_ext_fmt.g_val_tab(233)
1785              ,p_val_234                    =>  ben_ext_fmt.g_val_tab(234)
1786              ,p_val_235                    =>  ben_ext_fmt.g_val_tab(235)
1787              ,p_val_236                    =>  ben_ext_fmt.g_val_tab(236)
1788              ,p_val_237                    =>  ben_ext_fmt.g_val_tab(237)
1789              ,p_val_238                    =>  ben_ext_fmt.g_val_tab(238)
1790              ,p_val_239                    =>  ben_ext_fmt.g_val_tab(239)
1791              ,p_val_240                    =>  ben_ext_fmt.g_val_tab(240)
1792              ,p_val_241                    =>  ben_ext_fmt.g_val_tab(241)
1793              ,p_val_242                    =>  ben_ext_fmt.g_val_tab(242)
1794              ,p_val_243                    =>  ben_ext_fmt.g_val_tab(243)
1795              ,p_val_244                    =>  ben_ext_fmt.g_val_tab(244)
1796              ,p_val_245                    =>  ben_ext_fmt.g_val_tab(245)
1797              ,p_val_246                    =>  ben_ext_fmt.g_val_tab(246)
1798              ,p_val_247                    =>  ben_ext_fmt.g_val_tab(247)
1799              ,p_val_248                    =>  ben_ext_fmt.g_val_tab(248)
1800              ,p_val_249                    =>  ben_ext_fmt.g_val_tab(249)
1801              ,p_val_250                    =>  ben_ext_fmt.g_val_tab(250)
1802              ,p_val_251                    =>  ben_ext_fmt.g_val_tab(251)
1803              ,p_val_252                    =>  ben_ext_fmt.g_val_tab(252)
1804              ,p_val_253                    =>  ben_ext_fmt.g_val_tab(253)
1805              ,p_val_254                    =>  ben_ext_fmt.g_val_tab(254)
1806              ,p_val_255                    =>  ben_ext_fmt.g_val_tab(255)
1807              ,p_val_256                    =>  ben_ext_fmt.g_val_tab(256)
1808              ,p_val_257                    =>  ben_ext_fmt.g_val_tab(257)
1809              ,p_val_258                    =>  ben_ext_fmt.g_val_tab(258)
1810              ,p_val_259                    =>  ben_ext_fmt.g_val_tab(259)
1811              ,p_val_260                    =>  ben_ext_fmt.g_val_tab(260)
1812              ,p_val_261                    =>  ben_ext_fmt.g_val_tab(261)
1813              ,p_val_262                    =>  ben_ext_fmt.g_val_tab(262)
1814              ,p_val_263                    =>  ben_ext_fmt.g_val_tab(263)
1815              ,p_val_264                    =>  ben_ext_fmt.g_val_tab(264)
1816              ,p_val_265                    =>  ben_ext_fmt.g_val_tab(265)
1817              ,p_val_266                    =>  ben_ext_fmt.g_val_tab(266)
1818              ,p_val_267                    =>  ben_ext_fmt.g_val_tab(267)
1819              ,p_val_268                    =>  ben_ext_fmt.g_val_tab(268)
1820              ,p_val_269                    =>  ben_ext_fmt.g_val_tab(269)
1821              ,p_val_270                    =>  ben_ext_fmt.g_val_tab(270)
1822              ,p_val_271                    =>  ben_ext_fmt.g_val_tab(271)
1823              ,p_val_272                    =>  ben_ext_fmt.g_val_tab(272)
1824              ,p_val_273                    =>  ben_ext_fmt.g_val_tab(273)
1825              ,p_val_274                    =>  ben_ext_fmt.g_val_tab(274)
1826              ,p_val_275                    =>  ben_ext_fmt.g_val_tab(275)
1827              ,p_val_276                    =>  ben_ext_fmt.g_val_tab(276)
1828              ,p_val_277                    =>  ben_ext_fmt.g_val_tab(277)
1829              ,p_val_278                    =>  ben_ext_fmt.g_val_tab(278)
1830              ,p_val_279                    =>  ben_ext_fmt.g_val_tab(279)
1831              ,p_val_280                    =>  ben_ext_fmt.g_val_tab(280)
1832              ,p_val_281                    =>  ben_ext_fmt.g_val_tab(281)
1833              ,p_val_282                    =>  ben_ext_fmt.g_val_tab(282)
1834              ,p_val_283                    =>  ben_ext_fmt.g_val_tab(283)
1835              ,p_val_284                    =>  ben_ext_fmt.g_val_tab(284)
1836              ,p_val_285                    =>  ben_ext_fmt.g_val_tab(285)
1837              ,p_val_286                    =>  ben_ext_fmt.g_val_tab(286)
1838              ,p_val_287                    =>  ben_ext_fmt.g_val_tab(287)
1839              ,p_val_288                    =>  ben_ext_fmt.g_val_tab(288)
1840              ,p_val_289                    =>  ben_ext_fmt.g_val_tab(289)
1841              ,p_val_290                    =>  ben_ext_fmt.g_val_tab(290)
1842              ,p_val_291                    =>  ben_ext_fmt.g_val_tab(291)
1843              ,p_val_292                    =>  ben_ext_fmt.g_val_tab(292)
1844              ,p_val_293                    =>  ben_ext_fmt.g_val_tab(293)
1845              ,p_val_294                    =>  ben_ext_fmt.g_val_tab(294)
1846              ,p_val_295                    =>  ben_ext_fmt.g_val_tab(295)
1847              ,p_val_296                    =>  ben_ext_fmt.g_val_tab(296)
1848              ,p_val_297                    =>  ben_ext_fmt.g_val_tab(297)
1849              ,p_val_298                    =>  ben_ext_fmt.g_val_tab(298)
1850              ,p_val_299                    =>  ben_ext_fmt.g_val_tab(299)
1851              ,p_val_300                    =>  ben_ext_fmt.g_val_tab(300)
1852              ,p_group_val_01               =>  l_group_val_01
1853              ,p_group_val_02               =>  l_group_val_02
1854              ,p_program_application_id     =>  fnd_global.prog_appl_id
1855              ,p_program_id                 =>  fnd_global.conc_program_id
1856              ,p_program_update_date        =>  sysdate
1857              ,p_request_id                 =>  fnd_global.conc_request_id
1858              ,p_object_version_number      =>  l_object_version_number
1859              ,p_ext_per_bg_id            =>  p_ext_per_bg_id
1860              ,p_ext_rcd_in_file_id         =>  l_ext_rcd_in_file_id
1861              );
1862           --
1863          end if;
1864      end if ;
1865 --
1866   END LOOP;
1867 --
1868   commit;
1869 --
1870   if g_debug then
1871     hr_utility.set_location ('Exiting '||l_proc,15);
1872   end if;
1873 --
1874 exception
1875 --
1876   when g_ht_error then
1877 
1878     rollback to head_trail;
1879     l_error_message :=
1880     ben_ext_fmt.get_error_msg(to_number(substr(g_err_name, 5, 5)),g_err_name,l_elmt_name );
1881     write_error(p_err_num           => to_number(substr(g_err_name, 5, 5)),
1882                 p_err_name          => l_error_message,
1883                 p_typ_cd            => 'F',
1884                 p_person_id         => null,
1885                 p_request_id        => fnd_global.conc_request_id,
1886                 p_ext_rslt_id       => p_ext_rslt_id,
1887                 p_business_group_id => p_business_group_id
1888                );
1889     raise ben_ext_thread.g_job_failure_error;
1890 --
1891   when others then
1892 
1893     rollback to head_trail;
1894     raise;  -- such that the calling pgm will handle the rest.
1895 --
1896 end process_ext_ht_recs;
1897 --
1898 
1899 
1900 
1901 Procedure process_subtrailer(p_ext_rslt_id         in number,
1902                               p_ext_file_id         in number,
1903                               p_ext_typ_cd          in varchar2,
1904                               p_rcd_typ_cd          in varchar2,
1905                               p_business_group_id   in number,
1906                               p_effective_date      in date,
1907                               p_request_id          in number default null,
1908                               p_ext_group_elmt1     in varchar2,
1909                               p_ext_group_elmt2     in varchar2,
1910                               p_ext_crit_prfl_id    in number default null)
1911                               IS
1912 --
1913   l_proc               varchar2(72);
1914 --
1915    cursor get_subhdr_group is
1916   select distinct
1917          xrd.group_val_01,
1918          xrd.group_val_02,
1919          nvl(xrd.ext_per_bg_id,-1)  ext_per_bg_id
1920   from   ben_Ext_rslt_dtl xrd,
1921          ben_ext_rcd rcd,
1922          ben_ext_rcd_in_file erf
1923    where  xrd.ext_rslt_id = p_ext_rslt_id
1924     and   xrd.ext_rcd_id = erf.ext_rcd_id
1925     and   erf.ext_file_id = p_ext_file_id
1926     and   rcd.ext_rcd_id = erf.ext_rcd_id
1927    and    rcd.rcd_type_cd = 'S'
1928    and    ltrim(xrd.group_val_01)  is not null ;
1929 
1930 --
1931   l_ext_per_bg_id  number ;
1932 begin
1933 --
1934   g_debug := hr_utility.debug_enabled;
1935   if g_debug then
1936     l_proc := g_package||'.process_subtrailer';
1937     hr_utility.set_location('Entering'||l_proc, 5);
1938   end if;
1939 
1940   if p_ext_group_elmt1 is null then
1941          process_ext_ht_recs
1942              (p_ext_rslt_id       => p_ext_rslt_id,
1943               p_ext_file_id       => p_ext_file_id,
1944               p_ext_typ_cd        => p_ext_typ_cd,
1945               p_rcd_typ_cd        => p_rcd_typ_cd,
1946               p_business_group_id => p_business_group_id,
1947               p_effective_date    => p_effective_date,
1948               p_request_id        => p_request_id,
1949               p_ext_crit_prfl_id  => p_ext_crit_prfl_id ,
1950               p_ext_per_bg_id     => p_business_group_id
1951               );
1952   else
1953 
1954     for i  in get_subhdr_group   Loop
1955 
1956          l_ext_per_bg_id :=  i.ext_per_bg_id ;
1957          if i.ext_per_bg_id =  -1 then
1958             l_ext_per_bg_id := null ;
1959          end if  ;
1960 
1961 
1962          process_ext_ht_recs
1963              (p_ext_rslt_id       => p_ext_rslt_id,
1964               p_ext_file_id       => p_ext_file_id,
1965               p_ext_typ_cd        => p_ext_typ_cd,
1966               p_rcd_typ_cd        => p_rcd_typ_cd,
1967               p_business_group_id => p_business_group_id,
1968               p_effective_date    => p_effective_date,
1969               p_group_val_01      => i.group_val_01,
1970               p_group_val_02      => i.group_val_02,
1971               p_request_id        => p_request_id,
1972               p_ext_crit_prfl_id  => p_ext_crit_prfl_id ,
1973               p_ext_per_bg_id     => l_ext_per_bg_id );
1974 
1975     end Loop ;
1976 
1977   end if ;
1978 
1979 
1980 --
1981   if g_debug then
1982     hr_utility.set_location ('Exiting '||l_proc,15);
1983   end if;
1984 --
1985 --
1986 end process_subtrailer;
1987 
1988 
1989 
1990 
1991 -- ----------------------------------------------------------------------------
1992 -- |--------------------< check_all_threads_finished >------------------------|
1993 -- ----------------------------------------------------------------------------
1994 --
1995 Procedure check_all_threads_finished
1996   (p_effective_date        in     date
1997   ,p_business_group_id     in     number
1998   ,p_data_typ_cd           in     varchar2
1999   ,p_ext_typ_cd            in     varchar2
2000   ,p_ext_crit_prfl_id      in     number
2001   ,p_ext_rslt_id           in     number
2002   ,p_request_id            in     number
2003   ,p_ext_file_id           in     number
2004   ,p_ext_strt_dt           in     date
2005   ,p_ext_end_dt            in     date
2006   ,p_master_process_flag   in     varchar2
2007   ) is
2008 --
2009   l_proc         varchar2(80);
2010   l_no_threads           boolean := true;
2011   l_globals_filled       boolean;
2012   l_dummy                varchar2(1);
2013   l_count                binary_integer;
2014   l_xrs_object_version_number number;
2015   l_error_cd             hr_lookups.lookup_code%TYPE; -- UTF8 varchar2(80);
2016 --
2017   cursor c_threads(p_request_id number) is
2018   select null
2019   from   fnd_concurrent_requests fnd
2020   where  fnd.phase_code <> 'C'
2021   and    fnd.request_id = p_request_id;
2022 --
2023   cursor get_hdr_cnt is
2024   select count(*)
2025   from   ben_ext_rcd_in_file fil, ben_ext_rcd rcd
2026   where  fil.ext_rcd_id = rcd.ext_rcd_id
2027   and    fil.ext_file_id = p_ext_file_id
2028   and    rcd.rcd_type_cd = 'H';
2029 --
2030   cursor get_trl_cnt is
2031   select count(*)
2032   from   ben_ext_rcd_in_file fil, ben_ext_rcd rcd
2033   where  fil.ext_rcd_id = rcd.ext_rcd_id
2034   and    fil.ext_file_id = p_ext_file_id
2035   and    rcd.rcd_type_cd = 'T';
2036 --
2037 
2038   cursor get_subhdr_cnt is
2039   select count(*)
2040   from   ben_Ext_rslt_dtl xrd,
2041          ben_ext_rcd rcd,
2042          ben_ext_rcd_in_file erf
2043    where  xrd.ext_rslt_id = p_ext_rslt_id
2044     and   xrd.ext_rcd_id = erf.ext_rcd_id
2045     and   xrd.ext_rcd_in_file_id  = erf.ext_rcd_in_file_id
2046     and   erf.ext_file_id = p_ext_file_id
2047     and   rcd.ext_rcd_id = erf.ext_rcd_id
2048     and   erf.hide_flag    = 'N'
2049    and    rcd.rcd_type_cd = 'S' ;
2050 
2051 --
2052 
2053   cursor get_dtl_cnt is
2054    select count(*)
2055    from   ben_ext_rslt_dtl xrd , ben_ext_rcd_in_file erf
2056    where  xrd.ext_rslt_id = p_ext_rslt_id
2057     and   xrd.ext_rcd_id = erf.ext_rcd_id
2058     and   xrd.ext_rcd_in_file_id  = erf.ext_rcd_in_file_id
2059     and   erf.ext_file_id = p_ext_file_id
2060     and   erf.hide_flag    = 'N'
2061     and   person_id not in (0, 999999999999) ;
2062 
2063 --  select count(*)
2064 --  from   ben_ext_rslt_dtl xrd
2065 --  where  xrd.ext_rslt_id = p_ext_rslt_id;
2066 --
2067   cursor get_per_cnt is
2068   select count(distinct person_id)
2069   from   ben_ext_rslt_dtl xrd
2070   where  xrd.ext_rslt_id = p_ext_rslt_id
2071   and    person_id not in (0, 999999999999);
2072 --
2073   cursor get_err_cnt is
2074   select count(*)
2075   from   ben_ext_rslt_err err
2076   where  err.ext_rslt_id = p_ext_rslt_id;
2077 --
2078 begin
2079 --
2080   if g_debug then
2081     l_proc := g_package||'.check_all_threads_finished';
2082     hr_utility.set_location ('Entering '||l_proc,5);
2083   end if;
2084 --
2085   if g_num_processes <> 0 then
2086   --
2087     while l_no_threads loop
2088     --
2089       l_no_threads := false;
2090       for l_count in 1..g_num_processes loop
2091       --
2092         open c_threads(g_processes_rec(l_count));
2093         fetch c_threads into l_dummy;
2094         if c_threads%found then
2095 --
2096 -- Slave is still running
2097 --
2098             l_no_threads := true;
2099             close c_threads;
2100             exit;
2101         end if;
2102         close c_threads;
2103         --
2104       end loop;
2105       --
2106       -- To prevent over polling of fnd_concurrent_requests
2107       -- like all ben batch processes sleep for a while.
2108       --
2109       If (l_no_threads) then
2110         dbms_lock.sleep(5);
2111       End if;
2112       --
2113     end loop;
2114     --
2115   end if;
2116 --
2117 -- Log process information
2118 -- This is master specific only
2119 --
2120   if p_master_process_flag = 'Y' then
2121 --
2122     open get_dtl_cnt;
2123     fetch get_dtl_cnt into g_dtl_cnt;
2124     close get_dtl_cnt;
2125 --
2126     open get_hdr_cnt;
2127     fetch get_hdr_cnt into g_hdr_cnt;
2128     close get_hdr_cnt;
2129 --
2130     open get_trl_cnt;
2131     fetch get_trl_cnt into g_trl_cnt;
2132     close get_trl_cnt;
2133 --
2134 
2135     open get_subhdr_cnt;
2136     fetch get_subhdr_cnt into g_subhdr_cnt;
2137     close get_subhdr_cnt;
2138 --
2139     open get_per_cnt;
2140     fetch get_per_cnt into g_per_cnt;
2141     close get_per_cnt;
2142 --
2143     open get_err_cnt;
2144     fetch get_err_cnt into g_err_cnt;
2145     close get_err_cnt;
2146 --
2147     g_rec_cnt := g_dtl_cnt + g_hdr_cnt + g_trl_cnt + g_subhdr_cnt ;
2148 --
2149   End if;
2150 --
2151   if g_debug then
2152     hr_utility.set_location ('Exiting '||l_proc,15);
2153   end if;
2154 --
2155 end check_all_threads_finished;
2156 --
2157 --
2158 -- =======================================================================
2159 --                          <<do_multithread>>
2160 -- -----------------------------------------------------------------------
2161 -- This is the main batch procedure to be called from the concurrent manager
2162 -- or interactively to start extract.
2163 --
2164 -- ========================================================================
2165 procedure do_multithread
2166              (errbuf                  out nocopy    varchar2
2167              ,retcode                 out nocopy    number
2168              ,p_benefit_action_id     in     number
2169              ,p_ext_dfn_id            in     number
2170              ,p_thread_id             in     number
2171              ,p_effective_date        in     varchar2
2172              ,p_business_group_id     in     number
2173              ,p_data_typ_cd           in     varchar2
2174              ,p_ext_typ_cd            in     varchar2
2175              ,p_ext_crit_prfl_id      in     number
2176              ,p_ext_rslt_id           in     number
2177              ,p_ext_file_id           in     number
2178              ,p_ext_strt_dt           in     varchar2
2179              ,p_ext_end_dt            in     varchar2
2180              ,p_prmy_sort_cd          in     varchar2
2181              ,p_scnd_sort_cd          in     varchar2
2182              ,p_output_name           in     varchar2
2183              ,p_apnd_rqst_id_flag     in     varchar2
2184              ,p_request_id            in     number
2185              ,p_use_eff_dt_for_chgs_flag in varchar2
2186              ,p_master_process_flag   in varchar2
2187              ,p_eff_start_date        in     varchar2
2188              ,p_eff_end_date          in     varchar2
2189              ,p_act_start_date        in     varchar2
2190              ,p_act_end_date          in     varchar2
2191              ,p_penserv_mode          in     varchar2
2192              ) is
2193 --
2194 -- Local variable declaration
2195 --
2196   l_proc                   varchar2(80);
2197   l_range_id               ben_batch_ranges.range_id%type;
2198   l_start_person_action_id number := 0;
2199   l_end_person_action_id   number := 0;
2200   l_xrs_object_version_number number;
2201   l_line                      number := 0;
2202   l_threads                   number;
2203   l_chunk_size                number;
2204   l_effective_date            date;
2205   l_ext_strt_dt               date;
2206   l_ext_end_dt                date;
2207   l_use_eff_dt_for_chgs_flag  ben_ext_dfn.use_eff_dt_for_chgs_flag%TYPE; -- UTF8 varchar2(30);
2208   l_failure_in_other_thread   boolean := false;
2209 --
2210 -- Cursors declaration
2211 --
2212   cursor c_range_thread is
2213   select ran.range_id
2214   ,ran.starting_person_action_id
2215   ,ran.ending_person_action_id
2216   from ben_batch_ranges ran
2217   where ran.range_status_cd = 'U'
2218   and ran.BENEFIT_ACTION_ID  = P_BENEFIT_ACTION_ID
2219   and rownum < 2
2220   for update of ran.range_status_cd;
2221 --
2222   cursor c_range_err is
2223   select 1
2224   from ben_batch_ranges ran
2225   where ran.range_status_cd = 'E'
2226   and ran.BENEFIT_ACTION_ID  = P_BENEFIT_ACTION_ID;
2227 --
2228   l_dummy                     number;
2229   l_dummy_c                   varchar2(1) ;
2230   l_status                    integer;
2231   -- l_value1                    varchar2(20);  Not Used
2232   -- l_value2                    varchar2(20);  Not Used
2233   l_commit                    number;
2234   --
2235   -- decide the change event source forevery thread
2236   -- Change event source type
2237   --
2238   cursor c_celt(p_type varchar2)  is
2239     select 'X'
2240     from   ben_ext_crit_typ xct
2241            ,ben_Ext_crit_val xcv
2242     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
2243     and    xct.crit_typ_cd  = p_type
2244     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id  ;
2245   --
2246 
2247 
2248 Begin
2249 --
2250    g_debug := hr_utility.debug_enabled;
2251   if g_debug then
2252     l_proc := g_package||'.do_multithread';
2253     hr_utility.set_location ('Entering '||l_proc,5);
2254   end if;
2255   g_ext_dfn_id := p_ext_dfn_id;
2256   g_ext_rslt_id := p_ext_rslt_id;
2257 --
2258   l_effective_date := to_date(p_effective_date, 'YYYY/MM/DD HH24:MI:SS');
2259   l_effective_date := to_date(to_char(trunc(l_effective_date), 'DD/MM/RRRR'),
2260                       'DD/MM/RRRR');
2261 --
2262 --
2263   l_ext_strt_dt := to_date(p_ext_strt_dt, 'YYYY/MM/DD HH24:MI:SS');
2264   l_ext_strt_dt := to_date(to_char(trunc(l_ext_strt_dt), 'DD/MM/RRRR'),
2265                       'DD/MM/RRRR');
2266 --
2267   l_ext_end_dt := to_date(p_ext_end_dt, 'YYYY/MM/DD HH24:MI:SS');
2268   l_ext_end_dt := to_date(to_char(trunc(l_ext_end_dt), 'DD/MM/RRRR'),
2269                       'DD/MM/RRRR');
2270 
2271 
2272   g_effective_start_date  := to_date(p_eff_start_date, 'YYYY/MM/DD HH24:MI:SS');
2273   g_effective_start_date  := to_date(to_char(trunc(g_effective_start_date), 'DD/MM/RRRR'),
2274                       'DD/MM/RRRR');
2275 
2276   g_effective_end_date  := to_date(p_eff_end_date, 'YYYY/MM/DD HH24:MI:SS');
2277   g_effective_end_date  := to_date(to_char(trunc(g_effective_end_date), 'DD/MM/RRRR'),
2278                       'DD/MM/RRRR');
2279 
2280 
2281 
2282   g_actual_start_date  := to_date(p_act_start_date, 'YYYY/MM/DD HH24:MI:SS');
2283   g_actual_start_date  := to_date(to_char(trunc(g_actual_start_date), 'DD/MM/RRRR'),
2284                       'DD/MM/RRRR');
2285 
2286   g_actual_end_date  := to_date(p_act_end_date, 'YYYY/MM/DD HH24:MI:SS');
2287   g_actual_end_date  := to_date(to_char(trunc(g_actual_end_date), 'DD/MM/RRRR'),
2288                       'DD/MM/RRRR');
2289 
2290 
2291 --
2292 -- load the fnd_session table in case a rule dbi needs it.
2293 --
2294   dt_fndate.change_ses_date
2295     (p_ses_date => l_effective_date,
2296      p_commit => l_commit);
2297 --
2298   l_line := 2;
2299 --
2300   benutils.get_parameter
2301   (p_business_group_id => p_business_group_id
2302   ,p_batch_exe_cd => 'BENXTRCT'
2303   ,p_threads => l_threads
2304   ,p_chunk_size => l_chunk_size
2305   ,p_max_errors => g_max_errors_allowed);
2306 --
2307   --
2308   -- MH moved call to thread level (ben_ext_thread.do_multithread)
2309   -- rather than chunk level to minimize
2310   -- memory consumption
2311   --
2312   -- Determine extract Levels
2313   --
2314   ben_extract.set_ext_lvls
2315     (p_ext_file_id         => p_ext_file_id
2316     ,p_business_group_id   => p_business_group_id
2317     );
2318   --
2319   -- Setup record and required level tables
2320   --
2321   ben_extract.setup_rcd_typ_lvl
2322     (p_ext_file_id => p_ext_file_id
2323     );
2324 
2325   --
2326   -- decide the source for the change event from the change event log
2327   g_chg_ext_from_ben      := 'N' ;
2328   g_chg_ext_from_pay      := 'N' ;
2329 
2330   if p_data_typ_cd = 'C' and p_ext_crit_prfl_id  is not null then
2331      open c_celt ('CPE') ;
2332      fetch c_celt into  l_dummy_c ;
2333      if c_celt%found then
2334         g_chg_ext_from_pay      := 'Y' ;
2335      else
2336         g_chg_ext_from_ben      := 'Y' ;
2337      end if ;
2338      close c_celt ;
2339 
2340      open c_celt ('CCE') ;
2341      fetch c_celt into  l_dummy_c ;
2342      if c_celt%found then
2343         g_chg_ext_from_ben      := 'Y' ;
2344      end if ;
2345      close c_celt ;
2346 
2347      hr_utility.set_location (' thread CELT ben/pay ' || g_chg_ext_from_ben || ' / ' || g_chg_ext_from_pay, 99 ) ;
2348   end if ;
2349   --
2350 
2351   --
2352   Loop
2353 --
2354     open c_range_err;
2355     fetch c_range_err into l_dummy;
2356     if c_range_err%found then
2357       l_failure_in_other_thread    := TRUE;
2358       close c_range_err;
2359       exit;
2360     end if;
2361     close c_range_err;
2362 --
2363     open c_range_thread;
2364     fetch c_range_thread
2365          into l_range_id,l_start_person_action_id,l_end_person_action_id;
2366     Exit when c_range_thread%notfound;
2367     --RCHASE 115.59 - move update within cursor context.
2368 --
2369   l_line := 3;
2370 --
2371       update ben_batch_ranges ran set ran.range_status_cd = 'P'
2372       where ran.range_id = l_range_id;
2373 --
2374       commit;
2375 --
2376     close c_range_thread;
2377 --
2378   l_line := 4;
2379 --
2380     ben_extract.Xtrct_skltn
2381    (p_ext_dfn_id => p_ext_dfn_id
2382    ,p_business_group_id => p_business_group_id
2383    ,p_effective_date => l_effective_date
2384    ,p_benefit_action_id => p_benefit_action_id
2385    ,p_range_id => l_range_id
2386    ,p_start_person_action_id => l_start_person_action_id
2387    ,p_end_person_action_id => l_end_person_action_id
2388    ,p_data_typ_cd        => p_data_typ_cd
2389    ,p_ext_typ_cd         => p_ext_typ_cd
2390    ,p_ext_crit_prfl_id   => p_ext_crit_prfl_id
2391    ,p_ext_rslt_id        => p_ext_rslt_id
2392    ,p_ext_file_id        => p_ext_file_id
2393    ,p_ext_strt_dt        => l_ext_strt_dt
2394    ,p_ext_end_dt         => l_ext_end_dt
2395    ,p_prmy_sort_cd       => p_prmy_sort_cd
2396    ,p_scnd_sort_cd       => p_scnd_sort_cd
2397    ,p_request_id         => p_request_id
2398    ,p_use_eff_dt_for_chgs_flag => p_use_eff_dt_for_chgs_flag
2399    ,p_penserv_mode       => p_penserv_mode              ---- vkodedal changes for penserver 30-Apr-2008
2400    );
2401 --
2402   l_line := 5;
2403 --
2404   End loop;
2405 --
2406   l_line := 6;
2407 --
2408  -- halt all other threads when job failure.
2409   if l_failure_in_other_thread then
2410      ben_ext_thread.g_err_num := 92184;
2411      ben_ext_thread.g_err_name := 'BEN_92184_THREAD_HALTED';
2412      raise g_job_failure_error;
2413   end if;
2414 --
2415   l_line := 7;
2416 --
2417   if fnd_global.conc_request_id <> -1 then
2418     fnd_message.set_name('BEN', 'BEN_92190_THREAD_LABEL');
2419     fnd_file.put_line(fnd_file.log, fnd_message.get || ' ' ||to_char(p_thread_id));
2420     fnd_file.put_line(fnd_file.log, ' ');
2421     if p_master_process_flag <> 'Y' then
2422       fnd_message.set_name('BEN', 'BEN_92185_THREAD_SUCCESS');
2423       fnd_file.put_line(fnd_file.log, fnd_message.get);
2424       fnd_file.put_line(fnd_file.log, ' ');
2425     end if;
2426   end if;
2427 --
2428   if g_debug then
2429     hr_utility.set_location ('Exiting '||l_proc,15);
2430   end if;
2431 --
2432 Exception
2433 --
2434   when g_job_failure_error then
2435   --
2436   --  this will halt all other threads.
2437   update ben_batch_ranges
2438     set range_status_cd = 'E'
2439     where range_id = l_range_id;
2440   --
2441   --  write to the extract error table, so error will be reported
2442   --  on the error report and viewed on the extract result form.
2443   if ben_ext_thread.g_err_num <> 92184 then  --don't write if halt warning.
2444     write_error(p_err_num           => ben_ext_thread.g_err_num,
2445                 p_err_name          => null , --ben_ext_thread.g_err_name,
2446                 p_typ_cd            => 'F',
2447                 p_person_id         => null,
2448                 p_request_id        => p_request_id,
2449                 p_ext_rslt_id       => p_ext_rslt_id,
2450                 p_business_group_id => p_business_group_id
2451                );
2452     commit;
2453   end if;
2454   --
2455   if fnd_global.conc_request_id <> -1 then
2456     --
2457     --  write end of thread statistics which shows totals of what was
2458     --  processed
2459     fnd_message.set_name('BEN', 'BEN_92190_THREAD_LABEL');
2460     fnd_file.put_line(fnd_file.log, fnd_message.get || ' ' ||to_char(p_thread_id));
2461     --
2462     --  write the error message to the log file.
2463     --
2464     if ben_ext_thread.g_err_num <> 92184 then
2465       fnd_message.set_name('BEN', 'BEN_92186_THREAD_FAILURE');
2466       fnd_file.put_line(fnd_file.log, fnd_message.get);
2467       fnd_file.put_line(fnd_file.log, ' ');
2468     end if;
2469     fnd_message.set_name('BEN', ben_ext_thread.g_err_name);
2470     fnd_file.put_line(fnd_file.log, fnd_message.get);
2471     --
2472     --  if slave process, we are done.  If master process, raise job failure in
2473     --  calling program (ben_ext_thread.process)
2474     --
2475   end if;
2476   --
2477   if p_master_process_flag = 'Y' then
2478     raise;
2479   else -- slave
2480     fnd_message.raise_error;
2481   end if;
2482 
2483 
2484   when others then
2485   --
2486   --  this will halt all other threads.
2487   --
2488     update ben_batch_ranges
2489     set range_status_cd = 'E'
2490     where range_id = l_range_id;
2491   --
2492     fnd_message.set_name('BEN', 'BEN_92190_THREAD_LABEL');
2493     fnd_file.put_line(fnd_file.log, fnd_message.get || ' ' ||to_char(p_thread_id));
2494   --  if slave process, we are done.  If master process, raise job failure in
2495   --  calling program (ben_ext_thread.process)
2496   --
2497     if p_master_process_flag = 'Y' then
2498       raise;
2499     else  -- slave
2500       write_error(p_err_num           => null,
2501                 p_err_name          => substr(sqlerrm, 1, 200),
2502                 p_typ_cd            => 'F',
2503                 p_person_id         => null,
2504                 p_request_id        => p_request_id,
2505                 p_ext_rslt_id       => p_ext_rslt_id,
2506                 p_business_group_id => p_business_group_id
2507                );
2508       --
2509       commit;
2510       --
2511       if fnd_global.conc_request_id <> -1 then
2512         fnd_message.set_name('BEN', 'BEN_92186_THREAD_FAILURE');
2513         fnd_file.put_line(fnd_file.log, fnd_message.get);
2514         fnd_file.put_line(fnd_file.log, ' ');
2515         fnd_message.set_name('PER', 'FFU10_GENERAL_ORACLE_ERROR');
2516         fnd_message.set_token('2', substr(sqlerrm, 1, 200));
2517         fnd_file.put_line(fnd_file.log, fnd_message.get);
2518         fnd_message.raise_error;
2519       end if;
2520       --
2521     end if;
2522 --
2523 end do_multithread;
2524 --
2525 -- =======================================================================
2526 --                          <<Procedure:Initialize_Globals>>
2527 -- -----------------------------------------------------------------------
2528 -- -----------------------------------------------------------------------
2529 -- This is procedure initializes all globals in the pkh.
2530 -- ========================================================================
2531 --
2532 procedure initialize_globals is
2533 --
2534 --
2535 -- Local variable declaration
2536 --
2537   l_proc                   varchar2(80);
2538 --
2539 begin
2540 --
2541   if g_debug then
2542     l_proc := g_package||'.initialize_globals';
2543     hr_utility.set_location ('Entering '||l_proc,5);
2544   end if;
2545 --
2546   for i in 1..g_processes_rec.count loop
2547     if g_processes_rec.exists(i) then
2548       g_processes_rec(i) := null;
2549     end if;
2550   end loop;
2551 --
2552   g_max_errors_allowed := 0;
2553   g_num_processes := 0;
2554   g_err_name := null;
2555   g_err_num := null;
2556   g_ext_strt_dt := null;
2557   g_ext_end_dt := null;
2558   g_err_cnt := 0;
2559   g_per_cnt := 0;
2560   g_rec_cnt := 0;
2561   g_dtl_cnt := 0;
2562   g_hdr_cnt := 0;
2563   g_trl_cnt := 0;
2564   g_subhdr_cnt := 0;
2565   g_subtrl_cnt := 0;
2566 --
2567   if g_debug then
2568     hr_utility.set_location ('Exiting '||l_proc,15);
2569   end if;
2570 --
2571 end initialize_globals;
2572 -- =======================================================================
2573 --                          <<Procedure:Thread_Summary>>
2574 -- -----------------------------------------------------------------------
2575 -- -----------------------------------------------------------------------
2576 -- This is procedure logs the status of all threads in the master's log file.
2577 -- ========================================================================
2578 --
2579 procedure thread_summary is
2580 --
2581   l_proc                   varchar2(80);
2582   l_text varchar2(70);
2583 --
2584 begin
2585 --
2586   if g_debug then
2587     l_proc := g_package||'.thread_summary';
2588     hr_utility.set_location ('Entering '||l_proc,5);
2589   end if;
2590 --
2591   fnd_file.put_line(fnd_file.log, ' ');
2592   fnd_message.set_name('BEN', 'BEN_92191_ALL_THREAD_LABEL');
2593   fnd_file.put_line(fnd_file.log, fnd_message.get);
2594   fnd_file.put_line(fnd_file.log, ' ');
2595   fnd_message.set_name('BEN', 'BEN_92192_EXT_STATS_LABEL');
2596   fnd_file.put_line(fnd_file.log, fnd_message.get);
2597   fnd_message.set_name('BEN', 'BEN_91956_EXT_TOT_REC');
2598   fnd_file.put_line(fnd_file.log, '  ' || fnd_message.get || ' ' || to_char(g_rec_cnt));
2599 --
2600   fnd_message.set_name('BEN', 'BEN_91957_EXT_TOT_PER');
2601   fnd_file.put_line(fnd_file.log, '  ' || fnd_message.get || ' ' || to_char(g_per_cnt));
2602 --
2603   fnd_message.set_name('BEN', 'BEN_91958_EXT_TOT_ERR');
2604   fnd_file.put_line(fnd_file.log, '  ' || fnd_message.get || '  ' || to_char(g_err_cnt));
2605 --
2606   fnd_file.put_line(fnd_file.log, ' ');
2607 --
2608   if g_debug then
2609     hr_utility.set_location ('Exiting '||l_proc,15);
2610   end if;
2611 --
2612 end thread_summary;
2613 
2614 
2615 ----
2616 
2617 
2618 Procedure build_adv_criteria(
2619                       p_ext_crit_prfl_id   in     number default null,
2620                       p_ext_dfn_id         in     number,
2621                       p_business_group_id  in     number,
2622                       p_effective_date     in     date,
2623                       p_source             in     varchar2 default 'BEN' ,
2624                       p_select_statement   in out nocopy long) is
2625 
2626  l_proc                   varchar2(80);
2627  l_text varchar2(70);
2628  --
2629  cursor c1 is
2630  select ecv.ext_crit_val_id
2631  from ben_ext_crit_typ ect, ben_ext_crit_val ecv
2632  where ect.crit_typ_cd = 'ADV'
2633  and ect.ext_crit_typ_id = ecv.ext_crit_typ_id
2634  and ect.ext_crit_prfl_id = p_ext_crit_prfl_id
2635  ;
2636 
2637 
2638 
2639  cursor c2 (p_ext_crit_val_id number) is
2640  select ecc.crit_typ_cd,
2641        ecc.oper_cd,
2642        ecc.val_1,
2643        ecc.val_2
2644  from ben_ext_crit_cmbn ecc
2645  where  ecc.ext_crit_val_id = p_ext_crit_val_id
2646  ;
2647 
2648  l_sql_string long  ;
2649  l_first_time varchar2(1) ;
2650  l_first_val  varchar2(1) ;
2651  l_prev_first_val   varchar2(1) ;
2652  l_from_date  date ;
2653  l_to_date    date ;
2654 
2655 begin
2656 --
2657   if g_debug then
2658     l_proc := g_package||'.build_adv_criteria';
2659     hr_utility.set_location ('Entering '||l_proc,5);
2660   end if;
2661 
2662   l_sql_string := ''  ;
2663   l_first_time := 'Y' ;
2664   l_first_val  := 'Y' ;
2665   l_prev_first_val  := 'Y' ;
2666 
2667   if  p_ext_crit_prfl_id is not null then
2668       -- curosr on combination criteria type
2669       for i in c1
2670       Loop
2671 
2672          l_first_val  := 'Y' ;
2673           -- curosr on combination criteria value  type
2674          for k in  c2(i.ext_crit_val_id)
2675          Loop
2676              -- whne we switch to different criteria type
2677              -- add ( or ) 'or' condition a
2678              if l_prev_first_val <> 'Y' and l_first_val = 'Y' then
2679                 l_sql_string := l_sql_string || ') OR ( ' ;
2680              else
2681                 --- starting for the valu set
2682                 if l_first_val = 'Y' then
2683                    l_sql_string := l_sql_string || ' ( ' ;
2684                 else
2685                    l_sql_string := l_sql_string || ' AND  ' ;
2686                 end if ;
2687 
2688              end if;
2689              -- eof variable manpulation
2690              -- bof set the variable values
2691              l_first_val  := 'N' ;
2692              l_first_time := 'N' ;
2693              -- eof set the variable values
2694 
2695 
2696              -- bof get the values from
2697              if k.crit_typ_cd in ('CAD','CED') then
2698                 if k.crit_typ_cd  = 'CAD' then
2699                    if p_source = 'PAY' then
2700                          l_sql_string := l_sql_string || 'xcl.CREATION_DATE ' ;
2701                    elsif p_source = 'BEN' then
2702                          l_sql_string := l_sql_string || 'xcl.CHG_ACTL_DT ' ;
2703                    end if ;
2704                 elsif  k.crit_typ_cd  = 'CED' then
2705                    if p_source = 'PAY' then
2706                       l_sql_string := l_sql_string || 'xcl.EFFECTIVE_DATE ' ;
2707                    elsif p_source = 'BEN' then
2708                       l_sql_string := l_sql_string || ' xcl.CHG_EFF_DT ' ;
2709                    end if ;
2710                 end if ;
2711                 -- determne the from date
2712                 l_from_date  := ben_ext_util.calc_ext_date
2713                                                 (p_ext_date_cd => k.val_1,
2714                                                  p_abs_date    => p_effective_date,
2715                                                  p_ext_dfn_id => p_ext_dfn_id);
2716 
2717                 if k.oper_cd = 'EQ' then
2718                    if k.crit_typ_cd  = 'CED' then
2719                          l_sql_string := l_sql_string ||' =  to_date('''||
2720                                 to_char(l_from_date,'DD-MM-RRRR')||''',''DD-MM-RRRR'')';
2721 
2722                    Else
2723                        l_sql_string := l_sql_string|| ' BETWEEN '
2724                                                    ||'to_date('''||to_char(l_from_date,'DD-MM-RRRR HH24:MI:SS')
2725                                                    ||''',''DD-MM-RRRR HH24:MI:SS'')'
2726                                                    ||' AND '
2727                                                    ||'to_date('''||to_char(l_from_date+0.99999,'DD-MM-RRRR HH24:MI:SS')
2728                                                    ||''',''DD-MM-RRRR HH24:MI:SS'')';
2729                    End if ;
2730                 elsif k.oper_cd = 'NE' then
2731                    if k.crit_typ_cd  = 'CED' then
2732                          l_sql_string := l_sql_string ||'<>  to_date('''||
2733                                 to_char(l_from_date,'DD-MM-RRRR')||''',''DD-MM-RRRR'')';
2734 
2735                    Else
2736                        l_sql_string := l_sql_string|| ' NOT BETWEEN '
2737                                                    ||'to_date('''||to_char(l_from_date,'DD-MM-RRRR HH24:MI:SS')
2738                                                    ||''',''DD-MM-RRRR'')'
2739                                                    ||' AND '
2740                                                    ||'to_date('''||to_char(l_from_date+0.99999,'DD-MM-RRRR HH24:MI:SS')
2741                                                    ||''',''DD-MM-RRRR HH24:MI:SS'')';
2742                    end if;
2743 
2744                 elsif k.oper_cd = 'BE' then
2745                    l_to_date  := ben_ext_util.calc_ext_date
2746                                                 (p_ext_date_cd => k.val_2,
2747                                                  p_abs_date    => p_effective_date,
2748                                                  p_ext_dfn_id => p_ext_dfn_id);
2749                    if k.crit_typ_cd  = 'CAD' then
2750 
2751                        l_sql_string := l_sql_string|| ' BETWEEN '
2752                                                    ||'to_date('''||to_char(l_from_date,'DD-MM-RRRR HH24:MI:SS')
2753                                                    ||''',''DD-MM-RRRR HH24:MI:SS'')'
2754                                                    ||' AND '
2755                                                    ||'to_date('''||to_char(l_to_date+0.99999,'DD-MM-RRRR HH24:MI:SS')
2756                                                    ||''',''DD-MM-RRRR HH24:MI:SS'')';
2757                    else
2758                        l_sql_string := l_sql_string|| ' BETWEEN '
2759                                                    ||'to_date('''||to_char(l_from_date,'DD-MM-RRRR')||''',''DD-MM-RRRR'')'
2760                                                    ||' AND '
2761                                                    || 'to_date('''||to_char(l_to_date,'DD-MM-RRRR')||''',''DD-MM-RRRR'')';
2762                     end if ;
2763 
2764                  end if ;
2765              elsif  k.crit_typ_cd  = 'CCE' then
2766                       -- if this is ben change event get value else  so dont do anything
2767                  if p_source = 'BEN' then
2768                     l_sql_string := l_sql_string || ' xcl.chg_evt_cd ' ;
2769                     if k.oper_cd = 'EQ' then
2770                        l_sql_string := l_sql_string || ' = ''' || k.val_1 || '''' ;
2771                     elsif k.oper_cd = 'NE' then
2772                        l_sql_string := l_sql_string || ' <> '''|| k.val_1  ||'''' ;
2773                     end if ;
2774                  else
2775                     l_sql_string := l_sql_string || ' TRUE' ;
2776                  end if ;
2777              elsif  k.crit_typ_cd  = 'CPE' then
2778                       -- now we are not complicating the sql , this will be
2779                       -- taken care in evaluation part so do nothing
2780                       l_sql_string := l_sql_string || ' TRUE' ;
2781              end if ;
2782 
2783          end Loop ;
2784 
2785          -- set the previous value
2786          l_prev_first_val  := l_first_val ;
2787 
2788 
2789       end Loop ;
2790 
2791   end if ;
2792 
2793 
2794   if l_first_time = 'Y' then
2795      l_sql_string := null ;
2796   else
2797      l_sql_string := '( ' || l_sql_string || ' ) ) ' ;
2798   end if ;
2799 
2800   p_select_statement := l_sql_string ;
2801 
2802  if g_debug then
2803     hr_utility.set_location ('Exiting '||l_proc,15);
2804   end if;
2805 
2806 end ;
2807 
2808 
2809 
2810 Procedure chck_non_asg_pay_evt(
2811                       p_ext_crit_prfl_id    in  number ,
2812                       p_ext_dfn_id         in     number,
2813                       p_business_group_id  in     number,
2814                       p_effective_date     in     date,
2815                       p_effective_from_date in  date default null,
2816                       p_effective_to_date   in  date default null,
2817                       p_actual_from_date    in  date default null,
2818                       p_actual_to_date      in  date default null,
2819                       p_adv_crit_exist      in  varchar2 default null,
2820                       p_result              out nocopy  varchar2 ) is
2821 
2822  l_proc                   varchar2(80);
2823  l_text varchar2(70);
2824 
2825 
2826  cursor c_non_asg_crit is
2827  select 'X'
2828  from ben_ext_crit_typ ect
2829      ,ben_ext_crit_val ecv
2830      ,pay_datetracked_events pde
2831      ,pay_dated_tables pdt
2832   where ect.ext_crit_prfl_id = p_ext_crit_prfl_id
2833     and ect.crit_typ_cd = 'CPE'
2834     and ecv.ext_crit_typ_id = ect.ext_crit_typ_id
2835     and pde.event_group_id = to_number(ecv.val_1)
2836     and pde.dated_table_id = pdt.dated_table_id
2837     and pdt.TABLE_NAME in ( 'PAY_LINK_INPUT_VALUES_F'
2838                            ,'PAY_ELEMENT_LINKS_F'
2839                            ,'PAY_INPUT_VALUES_F'
2840                            ,'PAY_ALL_PAYROLLS_F'
2841                            ,'PAY_ELEMENT_TYPES_F'
2842                            ,'PAY_GRADE_RULES_F'
2843                            ,'PAY_USER_COLUMN_INSTANCES_F'
2844                            ,'FF_GLOBALS_F'
2845                            )
2846    ;
2847 
2848 
2849 
2850  cursor c_non_asg_tabel_id is
2851  select pde.dated_table_id,pde.business_group_id ,pde.LEGISLATION_CODE,pde.update_type
2852  from ben_ext_crit_typ ect
2853      ,ben_ext_crit_val ecv
2854      ,pay_datetracked_events pde
2855      ,pay_dated_tables pdt
2856   where ect.ext_crit_prfl_id = p_ext_crit_prfl_id
2857     and ect.crit_typ_cd = 'CPE'
2858     and ecv.ext_crit_typ_id = ect.ext_crit_typ_id
2859     and pde.event_group_id = to_number(ecv.val_1)
2860     and pde.dated_table_id = pdt.dated_table_id
2861     and pdt.TABLE_NAME in ( 'PAY_LINK_INPUT_VALUES_F'
2862                            ,'PAY_ELEMENT_LINKS_F'
2863                            ,'PAY_INPUT_VALUES_F'
2864                            ,'PAY_ALL_PAYROLLS_F'
2865                            ,'PAY_ELEMENT_TYPES_F'
2866                            ,'PAY_GRADE_RULES_F'
2867                            ,'PAY_USER_COLUMN_INSTANCES_F'
2868                            ,'FF_GLOBALS_F'
2869                            )
2870    ;
2871 
2872  l_result   varchar2(1) ;
2873  l_dummy    varchar2(1) ;
2874  l_dated_table_id pay_dated_tables.dated_table_id%type ;
2875  l_update_type varchar2(10) ;
2876  l_legislation_code varchar2(35) ;
2877  l_sql  varchar2(4000) ;
2878  l_adv_sql  varchar2(4000) ;
2879  l_column_name varchar2(35) ;
2880 
2881 
2882  TYPE nonasgevt is REF CURSOR;
2883   --
2884  c_nonasgevt     nonasgevt;
2885 
2886 
2887 Begin
2888 
2889 
2890   --
2891   if g_debug then
2892     l_proc := g_package||'.chck_non_asg_pay_evt';
2893     hr_utility.set_location ('Entering '||l_proc,5);
2894   end if;
2895 
2896 
2897     hr_utility.set_location ('actual date  '||p_actual_from_date ,5);
2898     hr_utility.set_location ('actual date  '||p_actual_to_date  ,5);
2899     hr_utility.set_location ('effect date  '||p_effective_from_date ,5);
2900     hr_utility.set_location ('effect date  '||p_effective_to_date  ,5);
2901     hr_utility.set_location ('adv cond   '||p_adv_crit_exist  ,5);
2902 
2903 
2904   l_result  := 'N' ;
2905   p_result  := 'N' ;
2906   open c_non_asg_crit ;
2907   fetch c_non_asg_crit into l_dummy ;
2908   IF c_non_asg_crit%notfound then
2909     -- when there is criteria with non asg table
2910     -- do not process further
2911     close c_non_asg_crit ;
2912     hr_utility.set_location ('Exiting no criteria found '||l_proc,10);
2913     Return ;
2914   End if ;
2915   close c_non_asg_crit ;
2916 
2917   -- when there is a event group with non asg table
2918   -- check there is any event on that dates
2919 
2920   for i  in c_non_asg_tabel_id
2921   Loop
2922 
2923      l_sql :=  ' Select ''X''  From pay_event_updates peu ' ||
2924                ' where  peu.dated_table_id = '|| i.dated_table_id ||
2925                ' and peu.event_type  = '''|| i.update_type || '''' ;
2926 
2927      if p_business_group_id is not null then
2928         l_sql:=l_sql||' and (peu.business_group_id is null or peu.business_group_id ='||p_business_group_id ||')' ;
2929      end if ;
2930 
2931         if i.legislation_code is not null then
2932            l_sql := l_sql ||' and peu.legislation_code = ''' || i.legislation_code || ''''  ;
2933         end if ;
2934 
2935         l_sql := l_sql ||
2936                ' and  exists ( ' ||
2937                '  Select xcl.process_event_id from pay_process_events xcl' ||
2938                '  where  xcl.event_update_id = peu.event_update_id' ||
2939                   --- if the event created for a bg then validate the bg with  extract bg
2940                '    and (peu.business_group_id is null or xcl.business_group_id = peu.business_group_id)';
2941 
2942      -- if the event group define for a bg then validate the bg with event
2943      if i.business_group_id is not null then
2944         l_sql := l_sql ||  ' and xcl.business_group_id  = ' ||  i.business_group_id   ;
2945      else
2946         if p_business_group_id is not null then
2947           l_sql:=l_sql||'and  xcl.business_group_id ='||p_business_group_id ;
2948         end if ;
2949      end if ;
2950 
2951      if p_actual_to_date  is not  null then
2952         l_sql := l_sql || ' and xcl.creation_date between  to_date(''' ||
2953                   to_char(p_actual_from_date,'DD-MM-RRRR HH24:MI:SS')|| ''',''DD-MM-RRRR HH24:MI:SS'') and to_date('''||
2954                   to_char(p_actual_to_date+0.99999,'DD-MM-RRRR HH24:MI:SS')  || ''',''DD-MM-RRRR HH24:MI:SS'') ';
2955      end if ;
2956 
2957       if p_effective_to_date  is not  null then
2958         l_sql := l_sql || ' and xcl.effective_date between  to_date(''' ||
2959                   to_char(p_effective_from_date,'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
2960                   to_char(p_effective_to_date,'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
2961      end if ;
2962 
2963      if p_adv_crit_exist = 'Y' then
2964 
2965 
2966          build_adv_criteria(
2967                        p_ext_crit_prfl_id   => p_ext_crit_prfl_id ,
2968                        p_ext_dfn_id         => p_ext_dfn_id,
2969                        p_business_group_id  => p_business_group_id,
2970                        p_effective_date     => p_effective_date,
2971                        p_source             => 'PAY' ,
2972                        p_select_statement   => l_ADV_sql) ;
2973        if l_ADV_sql is not null then
2974 
2975           l_sql := l_sql || ' AND ' || l_adv_sql ;
2976        end if ;
2977 
2978     end if ;
2979 
2980     l_sql := l_sql || ' )  ' ;
2981 
2982 
2983 
2984     --- Process the cursor
2985     begin
2986       open c_nonasgevt for l_sql ;
2987     exception
2988     --
2989     when others then
2990            --
2991          fnd_file.put_line(fnd_file.log,'Error executing this dynamically build payroll event SQL Statement:');
2992     end;
2993     --
2994     fetch c_nonasgevt into l_dummy ;
2995     if  c_nonasgevt%found then
2996         close c_nonasgevt ;
2997         l_result := 'Y' ;
2998         exit ;
2999     end if ;
3000     close c_nonasgevt ;
3001          --
3002 
3003  End Loop ;
3004 
3005   hr_utility.set_location ('Non Asg Pay event found  '||l_result,15);
3006   p_result := l_result ;
3007   if g_debug then
3008     hr_utility.set_location ('Exiting '||l_proc,15);
3009   end if;
3010 
3011 
3012 End chck_non_asg_pay_evt ;
3013 
3014 
3015 ----
3016 
3017 Procedure get_ext_crit_string(p_ext_crit_prfl_id in number ,
3018                               p_ext_crit_typ     in varchar2,
3019                               p_ext_num_str      in varchar2 default 'N' ,
3020                               p_string           out nocopy varchar2 ) is
3021 
3022 
3023 l_proc                   varchar2(80);
3024 cursor c_crit_all is
3025     select xcv.val_1
3026     from   ben_ext_crit_typ xct,
3027            ben_ext_crit_val xcv
3028     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3029     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3030     and    xct.crit_typ_cd = p_ext_crit_typ;
3031 
3032   l_crit_all varchar2(4000) ;
3033   l_first_time varchar2(1) ;
3034 
3035 
3036 Begin
3037    if g_debug then
3038     l_proc := g_package||'.get_ext_crit_string';
3039     hr_utility.set_location ('Entering '||l_proc,5);
3040   end if;
3041 
3042   l_crit_all := '' ;
3043   l_first_time := 'Y' ;
3044 
3045   for i in  c_crit_all
3046   Loop
3047 
3048      if l_first_time = 'N' then
3049         l_crit_all := l_crit_all || ',' ;
3050      end if ;
3051 
3052      if p_ext_num_str = 'S' then
3053         l_crit_all := l_crit_all || '''' ;
3054      end if ;
3055 
3056      l_crit_all := l_crit_all || ltrim(rtrim(i.val_1))  ;
3057      if p_ext_num_str = 'S' then
3058         l_crit_all := l_crit_all || '''' ;
3059      end if ;
3060 
3061      l_first_time := 'N' ;
3062 
3063   End Loop ;
3064 
3065   if l_first_time = 'N'  then
3066      p_string :=  '(' || l_crit_all  || ')' ;
3067   end if ;
3068 
3069   hr_utility.set_location ('Exiting '||l_proc,15);
3070 End get_ext_crit_string ;
3071 
3072 
3073 
3074 -- =======================================================================
3075 --                    <<Procedure:Build_Select_Statement>>
3076 -- -----------------------------------------------------------------------
3077 -- This procedure was added for performance improvements to filter as much
3078 -- data as possible up front using the criteria profiles.  It probably can
3079 -- be expanded to include more criteria such as state and zip code.  The
3080 -- sql statement is build dynamically, and used in an open statement in the
3081 -- calling program.
3082 --
3083 Procedure build_select_statement
3084                      (p_data_typ_cd        in     varchar2,
3085                       p_ext_crit_prfl_id   in     number default null,
3086                       p_ext_dfn_id         in     number,
3087                       p_business_group_id  in     number,
3088                       p_effective_date     in     date,
3089                       p_ext_rslt_id        in     number ,
3090                       p_ext_global_flag    in     varchar2 default null,
3091                       p_eff_start_date     in     date default null,
3092                       p_eff_end_date       in     date default null,
3093                       p_act_start_date     in     date default null,
3094                       p_act_end_date       in     date default null,
3095                       p_select_statement   in out nocopy long,
3096                       p_penserv_date       in     date default null) is
3097   --
3098   l_dynamic_sql long;
3099   l_dynamic_pay_sql long;
3100   l_dynamic_ben_sql long;
3101   l_dynamic_adv_sql long;
3102   l_pay_spl_process varchar2(1) ;
3103 
3104 
3105  -- For Pensrv
3106 
3107   l_pen_config_values pqp_utilities.t_config_values;
3108 
3109   l_pen_membership_col     VARCHAR2(20);
3110   l_pen_membership_context VARCHAR2(80);
3111 
3112   --
3113   -- Full Name/Person ID
3114   --
3115   cursor c_pid is
3116     select 'Y',
3117            xct.excld_flag
3118     from   ben_ext_crit_typ xct,
3119            ben_ext_crit_val xcv
3120     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3121     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3122     and    xct.crit_typ_cd = 'PID';
3123   --
3124   l_pid_exists varchar2(1) := 'N';
3125   l_pid_exclude varchar2(1);
3126   --
3127   -- Assignment Organization
3128   --
3129   cursor c_por is
3130     select 'Y',
3131            xct.excld_flag
3132     from   ben_ext_crit_typ xct,
3133            ben_ext_crit_val xcv
3134     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3135     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3136     and    xct.crit_typ_cd = 'POR';
3137   --
3138   l_por_exists varchar2(1) := 'N';
3139   l_por_exclude varchar2(1);
3140   --
3141   -- Assignment Status
3142   --
3143   cursor c_pas is
3144     select 'Y',
3145            xct.excld_flag
3146     from   ben_ext_crit_typ xct,
3147            ben_ext_crit_val xcv
3148     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3149     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3150     and    xct.crit_typ_cd = 'PAS';
3151   --
3152   l_pas_exists varchar2(1) := 'N';
3153   l_pas_exclude varchar2(1);
3154   --
3155 
3156   -- Assignment Location
3157   --
3158   cursor c_plo is
3159     select 'Y',
3160            xct.excld_flag
3161     from   ben_ext_crit_typ xct,
3162            ben_ext_crit_val xcv
3163     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3164     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3165     and    xct.crit_typ_cd = 'PLO';
3166   --
3167   l_plo_exists varchar2(1) := 'N';
3168   l_plo_exclude varchar2(1);
3169   --
3170   -- Person Benefits Group
3171   --
3172   cursor c_pbg is
3173     select 'Y',
3174            xct.excld_flag
3175     from   ben_ext_crit_typ xct,
3176            ben_ext_crit_val xcv
3177     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3178     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3179     and    xct.crit_typ_cd = 'PBG';
3180   --
3181   l_pbg_exists varchar2(1) := 'N';
3182   l_pbg_exclude varchar2(1);
3183   --
3184 
3185    -- Person business_grp Group
3186   --
3187   cursor c_pbgr is
3188     select 'Y',
3189            xct.excld_flag
3190     from   ben_ext_crit_typ xct,
3191            ben_ext_crit_val xcv
3192     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3193     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3194     and    xct.crit_typ_cd = 'PBGR';
3195   --
3196   l_pbgr_exists varchar2(1) := 'N';
3197   l_pbgr_exclude varchar2(1);
3198   --
3199 
3200   -- person type usage
3201 
3202   cursor c_ppt is
3203     select 'Y',
3204            xct.excld_flag
3205     from   ben_ext_crit_typ xct,
3206            ben_ext_crit_val xcv
3207     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3208     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3209     and    xct.crit_typ_cd = 'PPT';
3210   --
3211   l_ppt_exists varchar2(1) := 'N';
3212   l_ppt_exclude varchar2(1);
3213   --
3214 
3215 
3216 
3217   l_Source_dummy varchar2(1) ;
3218 
3219   -- Change Event Name
3220   --
3221   cursor c_cce is
3222     select 'Y',
3223            xct.excld_flag
3224     from   ben_ext_crit_typ xct
3225            ,ben_ext_crit_val xcv
3226     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3227     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3228     and    xct.crit_typ_cd = 'CCE';
3229   --
3230   l_cce_exists varchar2(1) := 'N';
3231   l_cce_exclude varchar2(1);
3232 
3233 
3234 cursor c_cpe is
3235     select 'Y',
3236            xct.excld_flag
3237     from   ben_ext_crit_typ xct
3238            ,ben_ext_crit_val xcv
3239     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3240     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3241     and    xct.crit_typ_cd = 'CPE';
3242 
3243   l_cpe_exists varchar2(1) := 'N';
3244   l_cpe_exclude varchar2(1);
3245 
3246 
3247 
3248   --
3249   -- Change Actual Date
3250   --
3251   cursor c_cad is
3252     Select 'Y',
3253            xct.excld_flag,
3254            xcv.val_1,
3255            xcv.val_2
3256     from   ben_ext_crit_typ xct,
3257            ben_ext_crit_val xcv
3258     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3259     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3260     and    xct.crit_typ_cd = 'CAD';
3261 
3262   l_cad_exists varchar2(1) := 'N';
3263   l_cad_exclude varchar2(1);
3264   l_cad_val1 ben_ext_crit_val.val_1%type ;
3265   l_cad_val2 ben_ext_crit_val.val_2%type ;
3266   l_cad_date_from date;
3267   l_cad_date_to date;
3268   --  person data link
3269 
3270    cursor c_pdl is
3271     Select 'Y',
3272            xct.excld_flag,
3273            xcv.val_1
3274     from   ben_ext_crit_typ xct,
3275            ben_ext_crit_val xcv
3276     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3277     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3278     and    xct.crit_typ_cd = 'PDL';
3279 
3280   l_pdl_exists varchar2(1) := 'N';
3281   l_pdl_exclude varchar2(1);
3282   l_pdl_val1 ben_ext_crit_val.val_1%type ;
3283 
3284   --  CWB
3285    cursor c_wplr is
3286    select 'Y',
3287            xct.excld_flag ,
3288            xct.EXT_CRIT_TYP_ID
3289     from   ben_ext_crit_typ xct,
3290            ben_ext_crit_val xcv
3291     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3292     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3293     and    xct.crit_typ_cd = 'WPLPR';
3294   --
3295   l_wplr_exists   varchar2(1) := 'N';
3296   l_wplr_exclude  varchar2(1);
3297   l_wlpr_EXT_CRIT_TYP_ID  number ;
3298 
3299 
3300   -- person Assignment_set   PASGSET
3301 
3302 
3303    cursor c_pasgset  is
3304     Select 'Y',
3305            xct.excld_flag,
3306            xcv.val_1
3307     from   ben_ext_crit_typ xct,
3308            ben_ext_crit_val xcv
3309     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3310     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3311     and    xct.crit_typ_cd = 'PASGSET';
3312 
3313   l_pasgset_exists varchar2(1) := 'N';
3314   l_pasgset_exclude varchar2(1);
3315   l_pasgset_val1 ben_ext_crit_val.val_1%type ;
3316 
3317 
3318 
3319 
3320 
3321   --
3322   -- Change Effective Date
3323   --
3324   cursor c_ced is
3325     select 'Y',
3326            xct.excld_flag,
3327            xcv.val_1,
3328            xcv.val_2
3329     from   ben_ext_crit_typ xct,
3330            ben_ext_crit_val xcv
3331     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3332     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3333     and    xct.crit_typ_cd = 'CED';
3334   --
3335   l_ced_exists varchar2(1) := 'N';
3336   l_ced_exclude varchar2(1);
3337   l_ced_val1 ben_ext_crit_val.val_1%type;
3338   l_ced_val2 ben_ext_crit_val.val_2%type;
3339   l_ced_date_from date;
3340   l_ced_date_to date;
3341   --
3342 
3343 
3344 
3345   -- Communication Type
3346   --
3347   cursor c_mtp is
3348     select 'Y',
3349            xct.excld_flag
3350     from   ben_ext_crit_typ xct,
3351            ben_ext_crit_val xcv
3352     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3353     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3354     and    xct.crit_typ_cd = 'MTP';
3355   --
3356   l_mtp_exists varchar2(1) := 'N';
3357   l_mtp_exclude varchar2(1);
3358   --
3359   -- Communication To Be Sent Date
3360   --
3361   cursor c_mtbsdt is
3362     select 'Y',
3363            xct.excld_flag,
3364            xcv.val_1,
3365            xcv.val_2
3366     from   ben_ext_crit_typ xct,
3367            ben_ext_crit_val xcv
3368     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3369     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3370     and    xct.crit_typ_cd = 'MTBSDT';
3371   --
3372   l_mtbsdt_exists varchar2(1) := 'N';
3373   l_mtbsdt_exclude varchar2(1);
3374   l_mtbsdt_val1 ben_ext_crit_val.val_1%type;
3375   l_mtbsdt_val2 ben_ext_crit_val.val_2%type;
3376   l_mtbsdt_date_from date;
3377   l_mtbsdt_date_to date;
3378 
3379   --
3380   cursor c_adv is
3381     Select 'Y'
3382     from   ben_ext_crit_typ xct
3383     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3384     and    xct.crit_typ_cd = 'ADV';
3385   --
3386   l_adv_exists varchar2(1) := 'N';
3387 
3388   -- Communication Sent Date
3389   --
3390   cursor c_msdt is
3391     Select 'Y',
3392            xct.excld_flag,
3393            xcv.val_1,
3394            xcv.val_2
3395     from   ben_ext_crit_typ xct,
3396            ben_ext_crit_val xcv
3397     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3398     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3399     and    xct.crit_typ_cd = 'MSDT';
3400   --
3401   l_msdt_exists varchar2(1) := 'N';
3402   l_msdt_exclude varchar2(1);
3403   l_msdt_val1 ben_ext_crit_val.val_1%type;
3404   l_msdt_val2 ben_ext_crit_val.val_2%type;
3405   l_msdt_date_from date;
3406   l_msdt_date_to date;
3407 
3408   -- Person payroll id
3409   cursor c_rrl is
3410     select 'Y',
3411            xct.excld_flag
3412     from   ben_ext_crit_typ xct,
3413            ben_ext_crit_val xcv
3414     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
3415     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
3416     and    xct.crit_typ_cd = 'RRL';
3417   --
3418   l_rrl_exists varchar2(1) := 'N';
3419   l_rrl_exclude varchar2(1);
3420 
3421   --For Penserver 115.70
3422   -- Cursor to check for PQP Earnings Extract
3423     cursor c_pen_ern is
3424     select bed.ext_dfn_id
3425       from ben_ext_dfn bed
3426      where bed.name = 'PQP GB PenServer Standard Interface - Earnings History';
3427   --
3428   l_pen_ext_dfn_id number;
3429 
3430   --
3431   l_pay_evt_non_asg   varchar2(1) ;
3432   l_crit_val_all  varchar2(4000) ;
3433   --
3434 begin
3435   --
3436   open c_pid;
3437     fetch c_pid into l_pid_exists, l_pid_exclude;
3438   close c_pid;
3439   --
3440   open c_por;
3441     fetch c_por into l_por_exists, l_por_exclude;
3442   close c_por;
3443   --
3444   open c_pas;
3445     fetch c_pas into l_pas_exists, l_pas_exclude;
3446   close c_pas;
3447   --
3448   open c_plo;
3449     fetch c_plo into l_plo_exists, l_plo_exclude;
3450   close c_plo;
3451   --
3452   open c_pbg;
3453     fetch c_pbg into l_pbg_exists, l_pbg_exclude;
3454   close c_pbg;
3455   -- person business group
3456   open c_pbgr;
3457     fetch c_pbgr into l_pbgr_exists, l_pbgr_exclude;
3458   close c_pbgr;
3459 
3460   -- person type usage
3461   open c_ppt;
3462   fetch c_ppt into l_ppt_exists, l_ppt_exclude;
3463   close c_ppt;
3464   --
3465   open c_cpe;
3466   fetch c_cpe into l_cpe_exists, l_cpe_exclude;
3467   if c_cpe%found then
3468      g_chg_ext_from_pay := 'Y' ;
3469   else
3470      g_chg_ext_from_ben := 'Y' ;
3471   end if ;
3472   close c_cpe;
3473 
3474 
3475   open c_cce;
3476   fetch c_cce into l_cce_exists, l_cce_exclude;
3477   if c_cce%found then
3478       g_chg_ext_from_ben := 'Y' ;
3479   end if ;
3480   close c_cce;
3481 
3482   -- person payroll
3483   open c_rrl;
3484   fetch c_rrl into l_rrl_exists, l_rrl_exclude;
3485   close c_rrl;
3486 
3487 
3488   hr_utility.set_location( 'CELT exist ' || g_chg_ext_from_ben || ' / ' || g_chg_ext_from_pay , 99 ) ;
3489 
3490   -- cwb
3491 
3492   open c_wplr;
3493   fetch c_wplr into l_wplr_exists, l_wplr_exclude, l_wlpr_EXT_CRIT_TYP_ID ;
3494   close c_wplr;
3495 
3496   -- when the date param is passes used the data param
3497   if p_act_start_date is not null and p_act_end_date is not null then
3498      l_cad_exists  := 'Y'  ;
3499      l_cad_exclude := 'N' ;
3500      l_cad_val1    := to_char(p_act_start_date , 'MM/DD/RRRR') ;
3501      l_cad_val2    := to_char(p_act_end_date , 'MM/DD/RRRR') ;
3502   else
3503      open c_cad;
3504      fetch c_cad into l_cad_exists, l_cad_exclude, l_cad_val1, l_cad_val2;
3505      close c_cad;
3506   end if ;
3507   --
3508   open c_pdl;
3509   fetch c_pdl into l_pdl_exists, l_pdl_exclude, l_pdl_val1;
3510   close c_pdl;
3511 
3512 
3513 
3514   --- Assignment set
3515   open c_pasgset;
3516   fetch c_pasgset  into l_pasgset_exists, l_pasgset_exclude, l_pasgset_val1;
3517   close c_pasgset ;
3518 
3519 
3520 
3521   -- when the date param is passes used the data param
3522   if p_eff_start_date is not null and p_eff_end_date is not null then
3523      l_ced_exists  := 'Y'  ;
3524      l_ced_exclude := 'N' ;
3525      l_ced_val1    := to_char(p_eff_start_date , 'MM/DD/RRRR') ;
3526      l_ced_val2    := to_char(p_eff_end_date , 'MM/DD/RRRR') ;
3527   else
3528     open c_ced;
3529       fetch c_ced into l_ced_exists, l_ced_exclude, l_ced_val1, l_ced_val2;
3530     close c_ced;
3531  end if ;
3532  --
3533 
3534 
3535  if g_chg_ext_from_pay = 'Y' and  ( l_cpe_exclude = 'Y' or  l_cad_exclude = 'Y' or l_ced_exclude = 'Y' ) then
3536     -- with payroll cahnge event exclude criteria on Event groups or Effective date or Actaul date is not allowed
3537 
3538     ben_ext_thread.g_err_num := 94264;
3539     ben_ext_thread.g_err_name := 'BEN_94264_EXT_PAY_CHG_EXCLD';
3540     raise g_job_failure_error;
3541  end if ;
3542 
3543 
3544 
3545 
3546 
3547   open c_mtp;
3548     fetch c_mtp into l_mtp_exists, l_mtp_exclude;
3549   close c_mtp;
3550   --
3551   open c_mtbsdt;
3552     fetch c_mtbsdt into l_mtbsdt_exists, l_mtbsdt_exclude, l_mtbsdt_val1, l_mtbsdt_val2;
3553   close c_mtbsdt;
3554   --
3555   open c_msdt;
3556     fetch c_msdt into l_msdt_exists, l_msdt_exclude, l_msdt_val1, l_msdt_val2;
3557   close c_msdt;
3558    -- ADV
3559   open c_adv ;
3560   fetch c_adv into l_adv_exists ;
3561   close c_adv ;
3562 
3563 
3564   --
3565   if p_data_typ_cd in ('F') then
3566      -- For Penserver 115.70
3567      IF p_penserv_date is not null
3568      THEN
3569         -- check if the current processed extract is Earnings
3570 	  OPEN c_pen_ern;
3571         FETCH c_pen_ern into l_pen_ext_dfn_id;
3572         CLOSE c_pen_ern;
3573 
3574         IF l_pen_ext_dfn_id = p_ext_dfn_id
3575         THEN
3576            l_dynamic_sql :=
3577             '  SELECT distinct(per.person_id)  person_id ' ||
3578             '    FROM per_all_assignments_f ben_asg ' ||
3579             '         ,per_periods_of_service ppos ' ||
3580             '         ,per_all_people_f per ' ||
3581             '   WHERE per.person_id =  ben_asg.person_id (+) ' ||
3582             '     AND ben_asg.period_of_service_id = ppos.period_of_service_id ' ||
3583             '     AND ((ppos.actual_termination_date is NULL) ' ||
3584             '           OR ' ||
3585             '           (ppos.actual_termination_date >= to_date('''||to_char((add_months(p_effective_date,-1) + 1) ,'DD/MM/YYYY') ||''',''DD/MM/YYYY''))' ||
3586             '           OR ' ||
3587             '           ((ppos.actual_termination_date < to_date('''||to_char((add_months(p_effective_date,-1) + 1) ,'DD/MM/YYYY') ||''',''DD/MM/YYYY'') '||
3588             '             AND EXISTS (SELECT 1 ' ||
3589             '                           FROM pay_assignment_actions paa ' ||
3590             '                                ,pay_run_results prr ' ||
3591             '                                ,pay_payroll_actions ppa ' ||
3592             '                          WHERE paa.assignment_id = ben_asg.assignment_id ' ||
3593             '                            AND paa.assignment_action_id = prr.assignment_action_id ' ||
3594             '                            AND paa.payroll_action_id = ppa.payroll_action_id ' ||
3595             '                            AND ppa.effective_date between to_date('''||to_char((add_months(p_effective_date,-1) + 1) ,'DD/MM/YYYY') ||''',''DD/MM/YYYY'') '||
3596             '                                                                and last_day(to_date(''' || to_char(p_effective_date,'DD/MM/YYYY') || ''',''DD/MM/YYYY'')) '||
3597             '                         ) ' ||
3598             '             ) ' ||
3599             '           ) ' ||
3600             '         ) ' ||
3601             '     AND NVL(ppos.actual_termination_date, GREATEST(TO_DATE('''||to_char((add_months(p_effective_date,-1) + 1) ,'DD/MM/YYYY') ||''',''DD/MM/YYYY''),ppos.date_start)) ' ||
3602 		'                                                                                                   BETWEEN ben_asg.effective_start_date AND ben_asg.effective_end_date ' ;
3603         ELSE
3604            l_dynamic_sql :=
3605             'select distinct(per.person_id)  person_id ' ||
3606             'from ' ||
3607             'per_all_people_f per, ' ||
3608             'per_all_assignments_f ben_asg ' ||
3609             'where ' ||
3610             'per.person_id = ben_asg.person_id (+)' ||
3611             ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') between per.effective_start_date and per.effective_end_date ' ||
3612             ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  || ''',''DD-MM-YYYY'') between ben_asg.effective_start_date (+) ' ||
3613             ' and ben_asg.effective_end_date (+)  ';
3614         END IF;
3615      ELSE
3616         l_dynamic_sql :=
3617             'select distinct(per.person_id)  person_id ' ||
3618             'from ' ||
3619             'per_all_people_f per, ' ||
3620             'per_all_assignments_f ben_asg ' ||
3621             'where ' ||
3622             'per.person_id = ben_asg.person_id (+)' ||
3623             ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') between per.effective_start_date and per.effective_end_date ' ||
3624             ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  || ''',''DD-MM-YYYY'') between ben_asg.effective_start_date (+) ' ||
3625             ' and ben_asg.effective_end_date (+)  ';
3626      END IF;
3627 
3628     -- For pensrv, Attribute check.Bug# 7341530
3629     IF (p_penserv_date is not null)
3630     THEN
3631        PQP_UTILITIES.get_config_type_values(
3632                     p_configuration_type   =>    'PQP_GB_PENSERVER_ELIGBLTY_CONF'
3633                    ,p_business_group_id    =>    p_business_group_id
3634                    ,p_legislation_code     =>    'GB' --g_legislation_code
3635                    ,p_tab_config_values    =>    l_pen_config_values
3636                    );
3637 
3638       IF l_pen_config_values.COUNT > 0
3639       THEN
3640          l_pen_membership_context  :=  l_pen_config_values(l_pen_config_values.FIRST).pcv_information1;
3641          l_pen_membership_col      :=  l_pen_config_values(l_pen_config_values.FIRST).pcv_information2;
3642 
3643 	   IF l_pen_membership_col is not null
3644          THEN
3645 
3646 		IF l_pen_membership_context = 'Global Data Elements'
3647             THEN
3648                l_dynamic_sql := l_dynamic_sql || ' and ben_asg.'||l_pen_membership_col||' IS NOT NULL';
3649             ELSE
3650                l_dynamic_sql := l_dynamic_sql || ' and ben_asg.'||l_pen_membership_col||' IS NOT NULL'
3651                                      || ' and ben_asg.ASS_ATTRIBUTE_CATEGORY = '||''''||l_pen_membership_context||'''';
3652             END IF;
3653 
3654          END IF;
3655 
3656 	 END IF;
3657     END IF;
3658 
3659     if nvl(P_ext_global_flag,'N') <> 'Y'  then
3660       l_dynamic_sql := l_dynamic_sql||'  and   per.business_group_id = '||p_business_group_id||' ' ;
3661     end if ;
3662 
3663 
3664 
3665     if g_debug then
3666       hr_utility.set_location('DATE FORMAT 1 ', 470);
3667     end if;
3668   end if;
3669 
3670   -- intitialising
3671   l_pay_spl_process := 'N' ;
3672   l_pay_evt_non_asg := 'N';
3673 
3674   if p_data_typ_cd in ('C') then
3675      l_dynamic_sql := ' ' ;
3676      --- for system extract  change event
3677      -- when the celt criteria defined or not
3678      -- if the criterai value is null
3679      -- if the criteria value is not null and  BEN is defined in the critertia
3680 
3681      if g_chg_ext_from_ben = 'Y'  then
3682         l_dynamic_ben_sql :=
3683         'select distinct(xcl.person_id)  person_id ' ||
3684         'from ' ||
3685         'ben_ext_chg_evt_log xcl, ' ||
3686         'per_all_people_f per, ' ||
3687         'per_all_assignments_f ben_asg ' ||
3688         'where ' ||
3689         'xcl.person_id = per.person_id ' ||
3690         ' and xcl.person_id = ben_asg.person_id (+) ' ||
3691         ' and xcl.chg_eff_dt between per.effective_start_date and per.effective_end_date '  ||
3692         ' and xcl.chg_eff_dt between ben_asg.effective_start_date (+) ' ||
3693         ' and ben_asg.effective_end_date (+)  '
3694         ;
3695 
3696         if nvl(P_ext_global_flag, 'N' )  <>  'Y' then
3697            l_dynamic_ben_sql := l_dynamic_ben_sql ||
3698            ' and  per.business_group_id = ' ||p_business_group_id   ;
3699         end if ;
3700 
3701 
3702      end if ;
3703      -- for Payroll change event
3704      -- when the celt criteria defined and
3705      -- pay is part of the defintion
3706      hr_utility.set_location('g_chg_ext_from_pay   ' || g_chg_ext_from_pay , 470);
3707 
3708 
3709      if g_chg_ext_from_pay = 'Y'   then
3710         --- calcaulte the date for finding the non asg exisit in pay process events
3711 
3712         if p_data_typ_cd in ('C') then
3713            if l_cad_exists = 'Y'  then
3714               if l_cad_val1 in ('CHAD','CHED') then
3715                  l_cad_date_from   := hr_api.g_sot ;
3716                  l_cad_date_to     := hr_api.g_eot ;
3717               Else
3718                  l_cad_date_from := ben_ext_util.calc_ext_date (p_ext_date_cd => l_cad_val1,
3719                                                     p_abs_date    => p_effective_date,
3720                                                     p_ext_dfn_id => p_ext_dfn_id);
3721                  l_cad_date_to := ben_ext_util.calc_ext_date (p_ext_date_cd => l_cad_val2,
3722                                                p_abs_date    => p_effective_date,
3723                                                p_ext_dfn_id => p_ext_dfn_id);
3724               end if ;
3725 
3726 
3727               if l_ced_exists = 'Y'  then
3728                  if l_ced_val1 in ('CHAD','CHED') then
3729                     l_cad_date_from   := hr_api.g_sot ;
3730                     l_cad_date_to     := hr_api.g_eot ;
3731                  Else
3732                     l_ced_date_from := ben_ext_util.calc_ext_date (p_ext_date_cd => l_ced_val1,
3733                                                  p_abs_date    => p_effective_date,
3734                                                  p_ext_dfn_id => p_ext_dfn_id);
3735                     l_ced_date_to := ben_ext_util.calc_ext_date (p_ext_date_cd => l_ced_val2,
3736                                                p_abs_date    => p_effective_date,
3737                                                p_ext_dfn_id => p_ext_dfn_id);
3738                  end if;
3739               end if ;
3740 
3741            End if ;
3742         End if ;
3743 
3744         ---- check the payroll non asg exist
3745 
3746         hr_utility.set_location('calling chck_non_asg_pay_evt   '  , 470);
3747 
3748         chck_non_asg_pay_evt(
3749                       p_ext_crit_prfl_id    =>  p_ext_crit_prfl_id ,
3750                       p_ext_dfn_id          =>  p_ext_dfn_id,
3751                       p_business_group_id   =>  p_business_group_id,
3752                       p_effective_date      =>  p_effective_date,
3753                       p_effective_from_date =>  l_ced_date_from,
3754                       p_effective_to_date   =>  l_ced_date_to,
3755                       p_actual_from_date    =>  l_cad_date_from,
3756                       p_actual_to_date      =>  l_cad_date_to,
3757                       p_adv_crit_exist      =>  l_adv_exists,
3758                       p_result              =>  l_pay_evt_non_asg ) ;
3759 
3760 
3761         -- if non asg pay evt found then select the all the employees in
3762         -- the database with  other person level criteria
3763         l_pay_spl_process  := 'N' ;
3764 
3765 
3766         if nvl(l_pay_evt_non_asg,'N') = 'Y'  then
3767            l_dynamic_pay_sql :=
3768              'select distinct(per.person_id)  person_id ' ||
3769              'from ' ||
3770              'per_all_people_f  per , ' ||
3771              'per_all_assignments_f ben_asg  ' ||
3772              'where ' ||
3773              '  ben_asg.person_id  = per.person_id  '  ||
3774              ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
3775              ''',''DD-MM-RRRR'') between per.effective_start_date and per.effective_end_date '
3776             ;
3777 
3778          Else
3779             l_dynamic_pay_sql :=
3780              'select distinct(ben_asg.person_id)  person_id ' ||
3781              'from ' ||
3782              'pay_process_events xcl, ' ||
3783              'per_all_assignments_f ben_asg  ' ||
3784              'where ' ||
3785              ' xcl.assignment_id  = ben_asg.assignment_id  ' ||
3786              ' and xcl.effective_date  between ben_Asg.effective_start_date and ben_Asg.effective_end_date '  ||
3787              ' and xcl.business_group_id = ben_Asg.business_group_id '
3788              ;
3789 
3790             l_pay_spl_process := 'Y' ;
3791 
3792         End if ;
3793 
3794 
3795         if nvl(P_ext_global_flag, 'N' )  <>  'Y' then
3796             if l_pay_spl_process = 'N' then
3797                l_dynamic_pay_sql := l_dynamic_pay_sql ||
3798               ' and  per.business_group_id = ' ||p_business_group_id   ;
3799             else
3800               l_dynamic_pay_sql := l_dynamic_pay_sql ||
3801               ' and  ben_Asg.business_group_id = ' ||p_business_group_id   ;
3802             end if ;
3803         end if ;
3804 
3805       end if ;
3806 
3807       -- End Update
3808       if g_debug then
3809         hr_utility.set_location('DATE FORMAT 2 ', 470);
3810       end if;
3811   end if;  -- 'C'
3812 
3813   if p_data_typ_cd in ('CM') then
3814      l_dynamic_sql :=
3815      'select distinct(pcm.person_id)  person_id ' ||
3816      'from ' ||
3817      'ben_per_cm_f pcm, ' ||
3818      'ben_per_cm_prvdd_f pcp, ' ||
3819      'per_all_people_f per, ' ||
3820      'per_all_assignments_f ben_asg ' ||
3821      'where ' ||
3822      'pcm.per_cm_id = pcp.per_cm_id ' ||
3823      ' and pcm.person_id = per.person_id ' ||
3824      ' and per.person_id = ben_asg.person_id (+) ' ||
3825      ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
3826      ''',''DD-MM-RRRR'') between per.effective_start_date and per.effective_end_date ' ||
3827      ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
3828      ''',''DD-MM-RRRR'') between pcm.effective_start_date and pcm.effective_end_date ' ||
3829      ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
3830      ''',''DD-MM-RRRR'') between pcp.effective_start_date and pcp.effective_end_date ' ||
3831      'and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
3832      ''',''DD-MM-RRRR'') between ben_asg.effective_start_date (+)  ' ||
3833      'and ben_asg.effective_end_date (+) ';
3834 
3835      if nvl(P_ext_global_flag,'N') <> 'Y'  then
3836        l_dynamic_sql := l_dynamic_sql||' and  pcm.business_group_id = '||p_business_group_id||' ' ;
3837      end if ;
3838 
3839      if g_debug then
3840        hr_utility.set_location('DATE FORMAT 3 ', 470);
3841      end if;
3842   end if;  -- 'CM'
3843 
3844   ---  CWB
3845 
3846   if p_data_typ_cd in ('CW') then
3847      l_dynamic_sql :=
3848      'select distinct(cpi.person_id)  person_id ' ||
3849      'from ' ||
3850      'ben_cwb_person_info  cpi, ' ||
3851      'per_all_people_f per, ' ||
3852      'per_all_assignments_f ben_asg ' ||
3853      'where ' ||
3854      'cpi.person_id = per.person_id ' ||
3855      ' and cpi.person_id = ben_asg.person_id (+) ' ||
3856       --  ' and cpi.business_group_id = ' || p_business_group_id ||
3857      ' and  cpi.effective_date  between per.effective_start_date and per.effective_end_date '  ||
3858      ' and cpi.effective_date  between ben_asg.effective_start_date (+) ' ||
3859      ' and ben_asg.effective_end_date (+)  '
3860      ;
3861 
3862      -- End Update
3863   end if;
3864   ---  CWB
3865 
3866 
3867   -- genral criteria
3868   if l_pid_exists = 'Y' then
3869 
3870      if l_pay_spl_process = 'N' then
3871         if l_pid_exclude = 'N' then
3872            l_dynamic_sql := l_dynamic_sql || ' and  ( per.person_id in ';
3873         else
3874            l_dynamic_sql := l_dynamic_sql || ' and  ( per.person_id not in ';
3875         end if;
3876 
3877         /*
3878         l_dynamic_sql := l_dynamic_sql ||
3879          '(select to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1))
3880                from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
3881          ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
3882          ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
3883          'xct.crit_typ_cd = ''PID'') ';
3884         */
3885 
3886 
3887      Else
3888         if l_pid_exclude = 'N' then
3889            l_dynamic_sql := l_dynamic_sql || ' and  ( ben_asg.person_id in ';
3890         else
3891            l_dynamic_sql := l_dynamic_sql || ' and  ( ben_asg.person_id not in ';
3892         end if;
3893      End if ;
3894 
3895      --- get the unique id in variable
3896      l_crit_val_all := null ;
3897      get_ext_crit_string(p_ext_crit_prfl_id  => p_ext_crit_prfl_id  ,
3898                       p_ext_crit_typ      => 'PID' ,
3899                       p_string            => l_crit_val_all ) ;
3900 
3901      l_dynamic_sql := l_dynamic_sql || nvl(l_crit_val_all, '(-1)') || ')' ;
3902 
3903 
3904   end if;  -- eof PID
3905   --
3906 
3907   if l_pasgset_exists = 'Y' then
3908 
3909      if l_pasgset_exclude = 'N' then
3910           l_dynamic_sql := l_dynamic_sql || ' and   ( exists ';
3911      else
3912           l_dynamic_sql := l_dynamic_sql || ' and  ( not exists ';
3913      end if ;
3914       --- this logic is taken from the pkg  pyadcutl.pkb
3915       l_dynamic_sql := l_dynamic_sql ||
3916       '(  SELECT /*+ ORDERED USE_NL (xct, xcv, aset) */ 1 FROM ben_ext_crit_typ xct, ben_ext_crit_val xcv, hr_assignment_sets aset ' ||
3917       ' where xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
3918       ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
3919       ' xct.crit_typ_cd = ''PASGSET''  and  to_number(xcv.val_1) = aset.assignment_set_id  ' ||
3920       ' and (not exists (select 1  from hr_assignment_set_amendments hasa ' ||
3921       '                  where hasa.assignment_set_id = aset.assignment_set_id and hasa.include_or_exclude = ''I'') ' ||
3922       ' or exists (select 1 from hr_assignment_set_amendments hasa ' ||
3923       ' where hasa.assignment_set_id=aset.assignment_set_id  '||
3924       '       and hasa.assignment_id = ben_asg.assignment_id and hasa.include_or_exclude = ''I'' ) '||
3925       '  ) ' ||
3926       ' and not exists (select 1 from hr_assignment_set_amendments hasa  ' ||
3927       ' where hasa.assignment_set_id=aset.assignment_set_id'||
3928       '   and hasa.assignment_id =  ben_asg.assignment_id and hasa.include_or_exclude = ''E'')   )) ' ;
3929 
3930   end if ;  -- eof PASGSET
3931 
3932   if l_por_exists = 'Y' then
3933      if l_por_exclude = 'N' then
3934         l_dynamic_sql := l_dynamic_sql||' and (( ben_asg.organization_id  in ';
3935       else
3936         l_dynamic_sql := l_dynamic_sql ||' and ((ben_asg.organization_id is null) or (ben_asg.organization_id  not in ';
3937      end if;
3938      /*
3939      if l_pay_spl_process = 'N' then
3940         l_dynamic_sql := l_dynamic_sql ||
3941          '(select to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1))
3942                from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
3943          ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
3944          ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
3945          'xct.crit_typ_cd = ''POR''))) ';
3946 
3947      Else
3948      */
3949 
3950 
3951      --- get the unique id in variable
3952      l_crit_val_all := null ;
3953      get_ext_crit_string(p_ext_crit_prfl_id     => p_ext_crit_prfl_id  ,
3954                               p_ext_crit_typ    => 'POR' ,
3955                               p_string          => l_crit_val_all ) ;
3956 
3957      l_dynamic_sql := l_dynamic_sql || nvl(l_crit_val_all, '(-1)') || '))' ;
3958 
3959   end if; -- eof POR
3960   --
3961   if l_pas_exists = 'Y' then
3962      if l_pas_exclude = 'N' then
3963         l_dynamic_sql := l_dynamic_sql||' and ( (ben_asg.assignment_status_type_id in ';
3964      else
3965         l_dynamic_sql := l_dynamic_sql || ' and ((ben_asg.assignment_status_type_id is null) or
3966                      (ben_asg.assignment_status_type_id not in ';
3967      end if;
3968 
3969      --- get the unique id in variable
3970      l_crit_val_all := null ;
3971      get_ext_crit_string(p_ext_crit_prfl_id        => p_ext_crit_prfl_id  ,
3972                               p_ext_crit_typ    => 'PAS' ,
3973                               p_string          => l_crit_val_all ) ;
3974 
3975      l_dynamic_sql := l_dynamic_sql || nvl(l_crit_val_all, '(-1)') || '))' ;
3976 
3977      /*
3978      l_dynamic_sql := l_dynamic_sql ||
3979      '(select to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1))  |
3980      ' from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
3981      ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
3982      ' and   ben_asg.assignment_status_type_id  = to_number(xcv.val_1)  '||
3983      ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id  '||
3984      ' and  xct.crit_typ_cd = ''PAS''))) ';
3985      */
3986 
3987   end if;  -- eof PAS
3988   --
3989   if l_plo_exists = 'Y' then
3990     if l_plo_exclude = 'N' then
3991       l_dynamic_sql := l_dynamic_sql || ' and ( (ben_asg.location_id in ';
3992     else
3993       l_dynamic_sql := l_dynamic_sql || ' and ((ben_asg.location_id is null)  or (ben_asg.location_id not in ';
3994     end if;
3995     /*
3996     l_dynamic_sql := l_dynamic_sql ||
3997     '(select to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1)) ' ||
3998     '  from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
3999     ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4000     ' and   ben_asg.location_id  = to_number(xcv.val_1)  '||
4001     ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
4002     'xct.crit_typ_cd = ''PLO''))) ';
4003     */
4004 
4005     --- get the unique id in variable
4006     l_crit_val_all := null ;
4007     get_ext_crit_string(p_ext_crit_prfl_id  => p_ext_crit_prfl_id  ,
4008                       p_ext_crit_typ      => 'PLO' ,
4009                       p_string            => l_crit_val_all ) ;
4010 
4011     l_dynamic_sql := l_dynamic_sql || nvl(l_crit_val_all, '(-1)') || '))' ;
4012 
4013 
4014   end if;  -- eof PLO
4015 
4016   -- person payroll id
4017   if l_rrl_exists = 'Y' then
4018      if l_rrl_exclude = 'N' then
4019         l_dynamic_sql := l_dynamic_sql || ' and ( (ben_asg.payroll_id in ';
4020      else
4021        l_dynamic_sql := l_dynamic_sql || ' and ((ben_asg.payroll_id is null)  or (ben_asg.payroll_id not in ';
4022      end if;
4023      l_crit_val_all := null ;
4024      get_ext_crit_string(p_ext_crit_prfl_id  => p_ext_crit_prfl_id  ,
4025                       p_ext_crit_typ      => 'RRL' ,
4026                       p_string            => l_crit_val_all ) ;
4027 
4028      l_dynamic_sql := l_dynamic_sql || nvl(l_crit_val_all, '(-1)') || '))' ;
4029 
4030   end if;  -- eof RRL
4031 
4032 
4033 
4034   if l_pbg_exists = 'Y' then
4035      if l_pay_spl_process = 'N' then
4036         if l_pbg_exclude = 'N' then
4037            l_dynamic_sql := l_dynamic_sql || ' and ( (per.benefit_group_id  in ';
4038         else
4039            l_dynamic_sql := l_dynamic_sql || ' and ((per.benefit_group_id is null) or (per.benefit_group_id  not in ';
4040         end if;
4041            l_dynamic_sql := l_dynamic_sql ||
4042            '(select  to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1))
4043                  from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
4044            ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4045            ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
4046            'xct.crit_typ_cd = ''PBG''))) ';
4047      else
4048 
4049          if l_pbg_exclude = 'N' then
4050             l_dynamic_sql := l_dynamic_sql || ' and ( ( exists  ';
4051           else
4052             l_dynamic_sql := l_dynamic_sql || ' and ( ( not exists ';
4053           end if;
4054 
4055           l_dynamic_sql := l_dynamic_sql ||
4056           '(select ''x''  from per_all_people_f per , ben_ext_crit_typ xct, ben_ext_crit_val xcv  where ' ||
4057           ' per.person_id = ben_asg.person_id   ' ||
4058           ' and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
4059           ''',''DD-MM-RRRR'') between per.effective_start_date and per.effective_end_date ' ||
4060           ' and per.benefit_group_id  = to_number(xcv.val_1) '||
4061           ' and xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4062           ' and xct.ext_crit_typ_id = xcv.ext_crit_typ_id  '||
4063           ' and xct.crit_typ_cd = ''PBG'') )) ';
4064 
4065      end if ;
4066 
4067   end if;  -- eof PBG
4068 
4069   -- Person business group id
4070   if l_pbgr_exists = 'Y' then
4071      if l_pay_spl_process = 'N' then
4072         if l_pbgr_exclude = 'N' then
4073           l_dynamic_sql := l_dynamic_sql || ' and  ( per.business_group_id  in ';
4074         else
4075           l_dynamic_sql := l_dynamic_sql || ' and  ( per.business_group_id not in ';
4076         end if;
4077      else
4078          if l_pbgr_exclude = 'N' then
4079             l_dynamic_sql := l_dynamic_sql || ' and  ( ben_asg.business_group_id  in ';
4080          else
4081             l_dynamic_sql := l_dynamic_sql || ' and  ( ben_asg.business_group_id not in ';
4082          end if;
4083      end if ;
4084 
4085      --- Tilak  get the unique id in variable
4086      l_crit_val_all := null ;
4087      get_ext_crit_string(p_ext_crit_prfl_id  => p_ext_crit_prfl_id  ,
4088                        p_ext_crit_typ      => 'PBGR' ,
4089                        p_string            => l_crit_val_all ) ;
4090      l_dynamic_sql := l_dynamic_sql || nvl(l_crit_val_all, '(-1)') || ')' ;
4091 
4092      /*
4093      l_dynamic_sql := l_dynamic_sql ||
4094      '(select to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1))
4095              from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
4096      ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4097      ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
4098      'xct.crit_typ_cd = ''PBGR'') ';
4099     */
4100 
4101   end if; -- eof PBGR
4102 
4103   -- person type usage
4104   if l_ppt_exists = 'Y' then
4105 
4106      hr_utility.set_location('PPT excluded ' || l_ppt_exclude, 99 ) ;
4107      if l_pay_spl_process = 'N' then
4108         if l_ppt_exclude = 'N' then
4109            l_dynamic_sql := l_dynamic_sql || ' and per.person_id  in ';
4110         else
4111            l_dynamic_sql := l_dynamic_sql || ' and per.person_id not in ';
4112         end if;
4113      else
4114         if l_ppt_exclude = 'N' then
4115            l_dynamic_sql := l_dynamic_sql || ' and ben_asg.person_id  in ';
4116         else
4117            l_dynamic_sql := l_dynamic_sql || ' and ben_asg.person_id not in ';
4118         end if;
4119 
4120      end if ;
4121 
4122      -- 6642051
4123      if nvl(P_ext_global_flag,'N') = 'Y'  then
4124 
4125         l_dynamic_sql := l_dynamic_sql ||
4126         ' (select  ptu.person_id  from  per_person_type_usages_f ptu  ' ;
4127         if l_pay_spl_process = 'N' then
4128            l_dynamic_sql := l_dynamic_sql ||
4129            ' where ptu.person_id = per.person_id ' ;
4130         else
4131            l_dynamic_sql := l_dynamic_sql ||
4132           ' where ptu.person_id = ben_asg.person_id ' ;
4133         end if ;
4134 
4135         l_dynamic_sql := l_dynamic_sql ||
4136         '   and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
4137         ''',''DD-MM-RRRR'') between ptu.effective_start_date and ptu.effective_end_date ' ||
4138         ' and ptu.person_type_id in '||
4139         '    (select to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1)) ' ||
4140         '   from ben_ext_crit_typ xct , ben_ext_crit_val xcv ' ||
4141         '    where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4142         '    and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
4143         '    xct.crit_typ_cd = ''PPT'')) ';
4144 
4145      else
4146 
4147         --- get the unique id in variable
4148         l_crit_val_all := null ;
4149         get_ext_crit_string(p_ext_crit_prfl_id  => p_ext_crit_prfl_id  ,
4150                        p_ext_crit_typ      => 'PPT' ,
4151                        p_string            => l_crit_val_all ) ;
4152 
4153 
4154         if l_pay_spl_process = 'N' then
4155            l_dynamic_sql := l_dynamic_sql ||
4156            ' (select  ptu.person_id  from  per_person_type_usages_f ptu  where ptu.person_id = per.person_id ' ;
4157         else
4158            l_dynamic_sql := l_dynamic_sql ||
4159            ' (select  ptu.person_id  from  per_person_type_usages_f ptu  where ptu.person_id = ben_Asg.person_id ' ;
4160         end if ;
4161 
4162         l_dynamic_sql := l_dynamic_sql ||
4163         '   and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
4164         ''',''DD-MM-RRRR'') between ptu.effective_start_date and ptu.effective_end_date ' ||
4165         ' and ptu.person_type_id in '|| nvl(l_crit_val_all, '(-1)') || ')' ;
4166 
4167      end if ;
4168 
4169 
4170      /*
4171       '    (select to_number(decode(ltrim (xcv.val_1,''0123456789''),NULL,xcv.val_1,-1))
4172                from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
4173       '    where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4174       '    and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id  '||
4175       '    and   ptu.person_type_id = to_number(xcv.val_1) and '||
4176       '    xct.crit_typ_cd = ''PPT'')) ';
4177      */
4178 
4179   end if;
4180 
4181   if l_cce_exists = 'Y' and p_data_typ_cd in ('C') then
4182 
4183      if l_dynamic_BEN_sql is not null then
4184         if l_cce_exclude = 'N' then
4185            l_dynamic_BEN_sql  :=  l_dynamic_BEN_sql || ' and  ( xcl.chg_evt_cd in ';
4186         else
4187            l_dynamic_BEN_sql := l_dynamic_BEN_sql || ' and  ( xcl.chg_evt_cd not in ';
4188         end if;
4189 
4190 
4191         l_crit_val_all := null ;
4192         get_ext_crit_string(p_ext_crit_prfl_id  => p_ext_crit_prfl_id  ,
4193                               p_ext_crit_typ    => 'CCE' ,
4194                               p_ext_num_str     => 'S' ,
4195                               p_string          => l_crit_val_all ) ;
4196 
4197          l_dynamic_BEN_sql := l_dynamic_BEN_sql || nvl(l_crit_val_all, '(''-1'')') || ')' ;
4198 
4199          /*
4200 
4201          l_dynamic_BEN_sql :=  l_dynamic_BEN_sql ||
4202         '(select xcv.val_1 from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
4203         ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4204         ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id  '||
4205         ' and xct.crit_typ_cd = ''CCE'') ';
4206         */
4207 
4208      end if ;
4209   end if ; -- eof CCE
4210 
4211   if l_cpe_exists = 'Y' and p_data_typ_cd in ('C') then
4212 
4213      -- if any issue with following sql , pls contact PQP
4214      if l_dynamic_PAY_sql is not null then
4215 
4216         if nvl(l_pay_evt_non_asg,'N') = 'N'  then
4217 
4218            ---get the unique id in variable
4219            l_crit_val_all := null ;
4220            get_ext_crit_string(p_ext_crit_prfl_id  => p_ext_crit_prfl_id  ,
4221                               p_ext_crit_typ    => 'CPE' ,
4222                               p_string          => l_crit_val_all ) ;
4223 
4224 
4225            if l_cpe_exclude = 'N' then
4226               l_dynamic_PAY_Sql  :=  l_dynamic_PAY_Sql || ' and exists ';
4227            else
4228               l_dynamic_PAY_Sql := l_dynamic_PAY_Sql || ' and not exists ';
4229            end if;
4230            /* Added for Penserver Performance (check elements in elements sets if table_name is PAY_ELEMENT_ENTRIES_F */
4231 	      -- BEGIN for Pensrv
4232 	      if(p_penserv_date is not null)
4233             then
4234 		-- END for Pensrv
4235                -- as per the babu sql
4236                l_dynamic_PAY_sql := l_dynamic_PAY_sql ||
4237                ' (select pde.event_group_id ' ||
4238                ' from  pay_datetracked_events pde, ' ||
4239                '  pay_event_updates peu ' ||
4240 		    -- BEGIN for Pensrv
4241                ' ,pay_dated_tables pdt '||
4242                -- END for Pensrv
4243                ' where  ' ||
4244                --' and (pde.business_group_id = bg.organization_id OR  (pde.business_group_id IS NULL ' ||
4245                --' and (pde.legislation_code is null or pde.legislation_code = bg.org_information9) ) ) ' ||
4246                --' and xct.crit_typ_cd = ''CPE'' and xct.ext_crit_prfl_id = ' ||  p_ext_crit_prfl_id ||
4247                --' and xct.ext_crit_typ_id = xcv.ext_crit_typ_id ' ||
4248                --' and pde.event_group_id =  to_number(xcv.val_1) ' ||
4249                '  pde.event_group_id in   ' || l_crit_val_all  ||
4250                ' and xcl.event_update_id = peu.event_update_id ' ||
4251                ' and peu.dated_table_id = pde.dated_table_id ' ||
4252                --' and (pde.column_name is null or pde.column_name = peu.column_name) ' ||
4253                --' and (peu.business_group_id = pde.business_group_id OR (peu.business_group_id is null ' ||
4254                --' and (peu.legislation_code is null or peu.legislation_code = pde.legislation_code)))' ||
4255 		   -- BEGIN for Pensrv
4256 		   --  Modified the below sql to cater for purge events more efficiently
4257                ' AND    pde.dated_table_id  = pdt.dated_table_id ' ||
4258                ' AND    ((pdt.table_name = '||'''PAY_ELEMENT_ENTRIES_F'''||
4259                '          AND (EXISTS (SELECT 1 '  ||
4260                '                      FROM  pay_event_group_usages pegu ' ||
4261                '                            ,pay_element_type_rules petr ' ||
4262                '                            ,pay_element_entries_f  peef ' ||
4263                '                       WHERE pegu.event_group_id = pde.event_group_id ' ||
4264                '                       AND   ((xcl.noted_value IS NOT NULL ' ||
4265                '                               AND xcl.noted_value = petr.element_type_id ' ||
4266                '                               AND petr.element_set_id = pegu.element_set_id ' ||
4267                '                              ) ' ||
4268                '                              OR ' ||
4269                '                              (xcl.surrogate_key = peef.element_entry_id ' ||
4270                '                               AND peef.assignment_id = ben_asg.assignment_id ' ||
4271                '                               AND peef.element_type_id = petr.element_type_id ' ||
4272                '                               AND petr.element_set_id = pegu.element_set_id ' ||
4273                '                              ) ' ||
4274                '                             ) ' ||
4275                '                      ) ' ||
4276                '              ) ' ||
4277                '        ) ' ||
4278                '        OR  ' ||
4279                '        (pdt.table_name = '||'''PAY_ELEMENT_ENTRY_VALUES_F'''||
4280                '          AND (EXISTS (SELECT 1 '  ||
4281                '                      FROM  pay_event_group_usages pegu ' ||
4282                '                            ,pay_element_type_rules petr ' ||
4283                '                            ,pay_element_entries_f  peef ' ||
4284                '                            ,pay_element_entry_values_f  peevf ' ||
4285                '                      WHERE  peef.assignment_id = ben_asg.assignment_id ' ||
4286                '                      AND    peef.element_type_id = petr.element_type_id ' ||
4287                '                      AND    petr.element_set_id = pegu.element_set_id ' ||
4288                '                      AND    pegu.event_group_id = pde.event_group_id ' ||
4289                '                      AND    peef.element_entry_id = peevf.element_entry_id ' ||
4290                '                      AND    xcl.surrogate_key = peevf.element_entry_value_id ' ||
4291                '                      ) ' ||
4292                '              ) ' ||
4293                '        ) ' ||
4294                '        OR  ' ||
4295                '        (pdt.table_name not in (' || '''PAY_ELEMENT_ENTRIES_F''' ||','|| '''PAY_ELEMENT_ENTRY_VALUES_F''' ||
4296                '        )) ' ||
4297                '        ) ' ||
4298                -- End for pensrv
4299                ' ) ' ;
4300 		 else
4301                -- as per the babu sql
4302                l_dynamic_PAY_sql := l_dynamic_PAY_sql ||
4303                ' (select pde.event_group_id ' ||
4304                ' from  pay_datetracked_events pde, ' ||
4305                '  pay_event_updates peu ' ||
4306                ' where  ' ||
4307                --' and (pde.business_group_id = bg.organization_id OR  (pde.business_group_id IS NULL ' ||
4308                --' and (pde.legislation_code is null or pde.legislation_code = bg.org_information9) ) ) ' ||
4309                --' and xct.crit_typ_cd = ''CPE'' and xct.ext_crit_prfl_id = ' ||  p_ext_crit_prfl_id ||
4310                --' and xct.ext_crit_typ_id = xcv.ext_crit_typ_id ' ||
4311                --' and pde.event_group_id =  to_number(xcv.val_1) ' ||
4312                '  pde.event_group_id in   ' || l_crit_val_all  ||
4313                ' and xcl.event_update_id = peu.event_update_id ' ||
4314                ' and peu.dated_table_id = pde.dated_table_id ' ||
4315                --' and (pde.column_name is null or pde.column_name = peu.column_name) ' ||
4316                --' and (peu.business_group_id = pde.business_group_id OR (peu.business_group_id is null ' ||
4317                --' and (peu.legislation_code is null or peu.legislation_code = pde.legislation_code)))' ||
4318                ' ) ' ;
4319 		 end if;
4320         end if ;
4321      end if ;
4322   end if;  -- CPE
4323   -- change event actual date
4324   if l_cad_exists = 'Y' and p_data_typ_cd in ('C') then
4325      if g_debug then
4326         hr_utility.set_location(' called from benxthrd  1  '|| l_cad_val1 ,514);
4327      end if;
4328      if l_cad_val1 in ('CHAD','CHED') then
4329          l_cad_date_from   := hr_api.g_sot ;
4330          l_cad_date_to     := hr_api.g_eot ;
4331      Else
4332         l_cad_date_from := ben_ext_util.calc_ext_date (p_ext_date_cd => l_cad_val1,
4333                                                  p_abs_date    => p_effective_date,
4334                                                  p_ext_dfn_id => p_ext_dfn_id);
4335         l_cad_date_to := ben_ext_util.calc_ext_date (p_ext_date_cd => l_cad_val2,
4336                                                p_abs_date    => p_effective_date,
4337                                                p_ext_dfn_id => p_ext_dfn_id);
4338      end if ;
4339 
4340      if l_dynamic_BEN_sql is not null then
4341 
4342         if l_cad_exclude = 'N' then
4343            l_dynamic_BEN_sql :=  l_dynamic_BEN_sql ||   ' and xcl.chg_actl_dt  between to_date(''' ||
4344            to_char(nvl(l_cad_date_from,hr_api.g_sot),'DD-MM-RRRR HH24:MI:SS')||
4345                             ''',''DD-MM-RRRR HH24:MI:SS'') and to_date(''' ||
4346            to_char(nvl(l_cad_date_to+0.99999,hr_api.g_eot),'DD-MM-RRRR HH24:MI:SS') || ''',''DD-MM-RRRR HH24:MI:SS'') ';
4347         else
4348            l_dynamic_BEN_sql :=  l_dynamic_BEN_sql ||
4349            ' and xcl.chg_actl_dt  not between to_date(''' ||
4350            to_char(nvl(l_cad_date_from,hr_api.g_sot),'DD-MM-RRRR HH24:MI:SS')  ||
4351                                ''',''DD-MM-RRRR HH24:MI:SS'') and to_date(''' ||
4352             to_char(nvl(l_cad_date_to+0.99999,hr_api.g_eot),'DD-MM-RRRR HH24:MI:SS') ||''',''DD-MM-RRRR HH24:MI:SS'') ';
4353         end if;
4354      end if ;
4355 
4356      if l_dynamic_PAY_sql is not null then
4357         if nvl(l_pay_evt_non_asg,'N') = 'N'  then
4358            if l_cad_exclude = 'N' then
4359               l_dynamic_PAY_sql :=  l_dynamic_PAY_sql ||  ' and xcl.CREATION_DATE  between to_date(''' ||
4360               to_char(nvl(l_cad_date_from,hr_api.g_sot),'DD-MM-RRRR HH24:MI:SS')  ||
4361                                 ''',''DD-MM-RRRR HH24:MI:SS'') and to_date(''' ||
4362               to_char(nvl(l_cad_date_to+0.99999,hr_api.g_eot),'DD-MM-RRRR HH24:MI:SS')||''',''DD-MM-RRRR HH24:MI:SS'')';
4363            else
4364               l_dynamic_PAY_sql := l_dynamic_PAY_sql ||
4365               ' and xcl.CREATION_DATE not between to_date(''' ||
4366               to_char(nvl(l_cad_date_from,hr_api.g_sot),'DD-MM-RRRR HH24:MI:SS')  ||
4367                                 ''',''DD-MM-RRRR HH24:MI:SS'') and to_date(''' ||
4368              to_char(nvl(l_cad_date_to+0.99999,hr_api.g_eot),'DD-MM-YYYY HH24:MI:SS')||''',''DD-MM-RRRR HH24:MI:SS'') ';
4369            end if;
4370         end if ;
4371      end if ;
4372   end if;  -- CAD
4373   -- chage event Effective date
4374   if l_ced_exists = 'Y' and p_data_typ_cd in ('C') then
4375      if g_debug then
4376         hr_utility.set_location(' called from benxthrd  2  '|| l_ced_val1 ,514);
4377      end if;
4378      if l_ced_val1 in ('CHAD','CHED') then
4379         l_cad_date_from   := hr_api.g_sot ;
4380         l_cad_date_to     := hr_api.g_eot ;
4381      Else
4382         l_ced_date_from := ben_ext_util.calc_ext_date (p_ext_date_cd => l_ced_val1,
4383                                                  p_abs_date    => p_effective_date,
4384                                                  p_ext_dfn_id => p_ext_dfn_id);
4385         l_ced_date_to := ben_ext_util.calc_ext_date (p_ext_date_cd => l_ced_val2,
4386                                                p_abs_date    => p_effective_date,
4387                                                p_ext_dfn_id => p_ext_dfn_id);
4388 
4389      End if ;
4390 
4391      if l_dynamic_BEN_sql is not null then
4392 
4393         if l_ced_exclude = 'N' then
4394            l_dynamic_BEN_sql := l_dynamic_BEN_sql || ' and xcl.chg_eff_dt between to_date(''' ||
4395            to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4396            to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
4397         else
4398            l_dynamic_BEN_sql := l_dynamic_BEN_sql ||
4399            ' and xcl.chg_eff_dt not between to_date(''' ||
4400            to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4401            to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
4402         end if;
4403      end if ;
4404 
4405 /*
4406      if l_dynamic_PAY_sql is not null then
4407         if nvl(l_pay_evt_non_asg,'N') = 'N'  then
4408            if l_ced_exclude = 'N' then
4409              l_dynamic_PAY_sql := l_dynamic_PAY_sql || ' and xcl.EFFECTIVE_DATE between to_date(''' ||
4410              to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4411              to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
4412            else
4413              l_dynamic_PAY_sql := l_dynamic_PAY_sql ||
4414              ' and xcl.EFFECTIVE_DATE not between to_date(''' ||
4415              to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4416              to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
4417            end if;
4418         end if ;
4419      end if ;
4420   end if;
4421 */
4422  if l_dynamic_PAY_sql is not null then
4423         if nvl(l_pay_evt_non_asg,'N') = 'N'  then
4424            if l_ced_exclude = 'N' then
4425             -- penserver performance fix - vkodedal
4426              if(p_penserv_date is not null)
4427              then
4428 
4429              	l_dynamic_PAY_sql := l_dynamic_PAY_sql || ' and ( ( xcl.EFFECTIVE_DATE between to_date(''' ||
4430              	to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4431              	to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') '
4432 				 || ' and xcl.CREATION_DATE between to_date(''' ||
4433              	to_char(nvl(p_penserv_date,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4434              	to_char(nvl(l_ced_date_to + 1,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') )'
4435              			 || ' or ( xcl.CREATION_DATE between to_date(''' ||
4436              	to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4437              	to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') '
4438              			 || ' and  xcl.EFFECTIVE_DATE between to_date(''' ||
4439              	to_char(nvl(p_penserv_date,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4440              	to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ))';
4441 
4442 			-- For pensrv, Attribute check.Bug# 7341530
4443 
4444 			PQP_UTILITIES.get_config_type_values(
4445                     p_configuration_type   =>    'PQP_GB_PENSERVER_ELIGBLTY_CONF'
4446                    ,p_business_group_id    =>    p_business_group_id
4447                    ,p_legislation_code     =>    'GB' --g_legislation_code
4448                    ,p_tab_config_values    =>    l_pen_config_values
4449                    );
4450 
4451 			 IF l_pen_config_values.COUNT > 0
4452                    THEN
4453                      l_pen_membership_context  :=  l_pen_config_values(l_pen_config_values.FIRST).pcv_information1;
4454                      l_pen_membership_col      :=  l_pen_config_values(l_pen_config_values.FIRST).pcv_information2;
4455 
4456 			   IF l_pen_membership_col is not null
4457                      THEN
4458 
4459 				IF l_pen_membership_context = 'Global Data Elements'
4460                         THEN
4461                            l_dynamic_PAY_sql := l_dynamic_PAY_sql || ' and ben_asg.'||l_pen_membership_col||' IS NOT NULL';
4462                         ELSE
4463                            l_dynamic_PAY_sql := l_dynamic_PAY_sql || ' and ben_asg.'||l_pen_membership_col||' IS NOT NULL'
4464                                              || ' and ben_asg.ASS_ATTRIBUTE_CATEGORY = '||''''||l_pen_membership_context||'''';
4465                         END IF;
4466 
4467 			   END IF;
4468 
4469 			 END IF;
4470 
4471              else ----- vkodedal
4472              	l_dynamic_PAY_sql := l_dynamic_PAY_sql || ' and xcl.EFFECTIVE_DATE between to_date(''' ||
4473              	to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4474              	to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
4475              end if;
4476            else
4477              l_dynamic_PAY_sql := l_dynamic_PAY_sql ||
4478              ' and xcl.EFFECTIVE_DATE not between to_date(''' ||
4479              to_char(nvl(l_ced_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4480              to_char(nvl(l_ced_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
4481            end if;
4482         end if ;
4483      end if ;
4484   end if;
4485   --
4486   -- advance criteria
4487 
4488   if l_adv_exists = 'Y' and  p_data_typ_cd in ('C') then
4489 
4490      if l_dynamic_PAY_sql is not null then
4491         if nvl(l_pay_evt_non_asg,'N') = 'N'  then
4492            build_adv_criteria(
4493                     p_ext_crit_prfl_id   => p_ext_crit_prfl_id ,
4494                     p_ext_dfn_id         => p_ext_dfn_id,
4495                     p_business_group_id  => p_business_group_id,
4496                     p_effective_date     => p_effective_date,
4497                     p_source             => 'PAY' ,
4498                     p_select_statement   => l_dynamic_ADV_sql) ;
4499 
4500            if l_dynamic_ADV_sql is not null then
4501               l_dynamic_PAY_sql := l_dynamic_PAY_sql || ' AND ' || l_dynamic_ADV_sql ;
4502            end if ;
4503         end if ;
4504      end if ;
4505 
4506      if l_dynamic_BEN_sql is not null then
4507 
4508         build_adv_criteria(
4509                     p_ext_crit_prfl_id   => p_ext_crit_prfl_id ,
4510                     p_ext_dfn_id         => p_ext_dfn_id,
4511                     p_business_group_id  => p_business_group_id,
4512                     p_effective_date     => p_effective_date,
4513                     p_source             => 'BEN' ,
4514                     p_select_statement   => l_dynamic_ADV_sql) ;
4515 
4516         if l_dynamic_BEN_sql is not null then
4517            l_dynamic_BEN_sql := l_dynamic_BEN_sql || ' AND ' || l_dynamic_ADV_sql ;
4518         end if ;
4519 
4520      end if ;
4521   end if ;
4522 
4523   -- communication type
4524   if l_mtp_exists = 'Y' and p_data_typ_cd in ('CM') then
4525      if l_mtp_exclude = 'N' then
4526         l_dynamic_sql := l_dynamic_sql || ' and to_char(pcm.cm_typ_id) in ';
4527      else
4528         l_dynamic_sql := l_dynamic_sql || ' and to_char(pcm.cm_typ_id) not in ';
4529     end if;
4530     l_dynamic_sql := l_dynamic_sql ||
4531     '(select xcv.val_1 from ben_ext_crit_typ xct ,ben_ext_crit_val xcv ' ||
4532     ' where  xct.ext_crit_prfl_id = ' || p_ext_crit_prfl_id ||
4533     ' and   xct.ext_crit_typ_id = xcv.ext_crit_typ_id and '||
4534     'xct.crit_typ_cd = ''MTP'') ';
4535   end if;
4536   -- communication tobe send date
4537   if l_mtbsdt_exists = 'Y' and p_data_typ_cd in ('CM') then
4538      l_mtbsdt_date_from := ben_ext_util.calc_ext_date (p_ext_date_cd => l_mtbsdt_val1,
4539                                                  p_abs_date    => p_effective_date,
4540                                                  p_ext_dfn_id => p_ext_dfn_id);
4541      l_mtbsdt_date_to := ben_ext_util.calc_ext_date (p_ext_date_cd => l_mtbsdt_val2,
4542                                                p_abs_date    => p_effective_date,
4543                                                p_ext_dfn_id => p_ext_dfn_id);
4544      if l_mtbsdt_exclude = 'N' then
4545         l_dynamic_sql := l_dynamic_sql || ' and pcp.to_be_sent_dt between to_date(''' ||
4546         to_char(nvl(l_mtbsdt_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4547         to_char(nvl(l_mtbsdt_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') ';
4548      else
4549         l_dynamic_sql := l_dynamic_sql ||
4550         ' and (pcp.to_be_sent_dt is null or pcp.to_be_sent_dt not between to_date(''' ||
4551         to_char(nvl(l_mtbsdt_date_from,hr_api.g_sot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4552         to_char(nvl(l_mtbsdt_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'')) ';
4553      end if;
4554   end if;
4555   -- communication send date
4556   if l_msdt_exists = 'Y' and p_data_typ_cd in ('CM') then
4557      l_msdt_date_from := ben_ext_util.calc_ext_date (p_ext_date_cd => l_msdt_val1,
4558                                                  p_abs_date    => p_effective_date,
4559                                                  p_ext_dfn_id => p_ext_dfn_id);
4560      l_msdt_date_to := ben_ext_util.calc_ext_date (p_ext_date_cd => l_msdt_val2,
4561                                                p_abs_date    => p_effective_date,
4562                                                p_ext_dfn_id => p_ext_dfn_id);
4563      if l_msdt_exclude = 'N' then
4564         l_dynamic_sql := l_dynamic_sql || ' and pcp.sent_dt between to_date(''' ||
4565         to_char(nvl(l_msdt_date_from,hr_api.g_sot),'DD-MM-YYYY')  || ''',''DD-MM-RRRR'') and to_date(''' ||
4566         to_char(nvl(l_msdt_date_to,hr_api.g_eot),'DD-MM-RRRR')  || ''',''DD-MM-RRRR'')';
4567      else
4568         l_dynamic_sql := l_dynamic_sql ||
4569         ' and (pcp.sent_dt is null or pcp.sent_dt not between to_date(''' ||
4570         to_char(nvl(l_msdt_date_from,hr_api.g_sot),'DD-MM-RRRR') || ''',''DD-MM-RRRR'') and to_date(''' ||
4571         to_char(nvl(l_msdt_date_to,hr_api.g_eot),'DD-MM-RRRR') || ''',''DD-MM-RRRR'')) ';
4572      end if;
4573   end if;
4574 
4575 
4576   -- person data link
4577   if l_pdl_exists = 'Y' then
4578      hr_utility.set_location('pdl excluded ' || l_pdl_exclude, 99 ) ;
4579      if l_pdl_exclude = 'N' then
4580         l_dynamic_sql := l_dynamic_sql || ' and EXISTS  ';
4581      else
4582         l_dynamic_sql := l_dynamic_sql || ' and  not EXISTS  ';
4583      end if ;
4584      if l_pdl_val1  = 'PDLJOB' then
4585         l_dynamic_sql := l_dynamic_sql ||
4586                   '  (select 1 from   per_jobs job  where job.job_id = ben_asg.job_id  and ' ||
4587                   '    exists ( select group_val_01 from  ben_ext_rslt_dtl erd  where  ' ||
4588                   '   erd.ext_rslt_id = ' || p_ext_rslt_id  ||
4589                   '   and group_val_01 is not null  and group_val_01 = job.name  ) )   '  ;
4590      elsif l_pdl_val1  = 'PDLPOS' then
4591            l_dynamic_sql := l_dynamic_sql ||
4592                   '  (select 1 from   HR_ALL_POSITIONS_F pos where pos.position_id = ben_asg.position_id and '||
4593                   '    to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
4594                   ''',''DD-MM-RRRR'') between pos.effective_start_date and pos.effective_end_date and  ' ||
4595                   '   exists ( select group_val_01 from  ben_ext_rslt_dtl erd  where  ' ||
4596                   '   erd.ext_rslt_id = ' || p_ext_rslt_id  ||
4597                   '   and group_val_01 is not null and group_val_01 = pos.name ))  '  ;
4598 
4599      elsif l_pdl_val1  = 'PDLPAY' then
4600                 l_dynamic_sql := l_dynamic_sql ||
4601                   '  (select 1 from   pay_payrolls  pay  where pay.payroll_id  = ben_asg.payroll_id   and ' ||
4602                   '   exists ( select group_val_01 from  ben_ext_rslt_dtl erd  where  ' ||
4603                   '   erd.ext_rslt_id = ' || p_ext_rslt_id  ||
4604                   '   and  group_val_01 is not null and group_val_01 = pay.payroll_name   ))  '  ;
4605      elsif l_pdl_val1  = 'PDLLOC' then
4606                    l_dynamic_sql := l_dynamic_sql ||
4607                   '  (select 1 from   hr_locations_all  loc  where loc.location_id = ben_asg.location_id  ' ||
4608                   '   and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
4609                   ''',''DD-MM-RRRR'') between loc.effective_start_date and loc.effective_end_date ' ||
4610                   '  and  exists ( select group_val_01 from  ben_ext_rslt_dtl erd  where  ' ||
4611                   '   erd.ext_rslt_id = ' || p_ext_rslt_id  ||
4612                   '   and  group_val_01 is not null and group_val_01 = loc.location_code )) '  ;
4613      elsif  l_pdl_val1  = 'PDLEAPP' then
4614           l_dynamic_sql := l_dynamic_sql ||
4615           '  ( select 1 from per_time_periods tim  where   ben_asg.payroll_id  = tim.payroll_id  ' ||
4616           '   and to_date(''' || to_char(p_effective_date,'DD-MM-RRRR')  ||
4617           ''',''DD-MM-RRRR'') between   tim.start_date and tim.end_date   ' ||
4618           '  and  ben_asg.effective_start_date<=tim.end_date and ben_asg.effective_end_date>= tim.start_date) ';
4619 
4620 
4621      elsif l_pdl_val1  = 'PDLGRD' then
4622                    l_dynamic_sql := l_dynamic_sql ||
4623                   '  (select 1 from   per_grades  grd  where grd.grade_id = ben_asg.grade_id  ' ||
4624                   '  and  exists ( select group_val_01 from  ben_ext_rslt_dtl erd  where  ' ||
4625                   '   erd.ext_rslt_id = ' || p_ext_rslt_id  ||
4626                   '   and  group_val_01 is not null and group_val_01 = grd.name )) '  ;
4627 
4628       end if;
4629 
4630    end if;
4631 
4632    hr_utility.set_location(   p_data_typ_cd  || '   '|| l_wplr_exists , 99 ) ;
4633 
4634    if l_wplr_exists = 'Y' and p_data_typ_cd in ('CW') then
4635       if l_wplr_exclude = 'N' then
4636          l_dynamic_sql := l_dynamic_sql || ' and exists  '  ;
4637       else
4638          l_dynamic_sql := l_dynamic_sql || ' and not exists  '  ;
4639       end if ;
4640       l_dynamic_sql := l_dynamic_sql ||
4641         '   ( select 1  from ben_ext_crit_val cvl , ben_enrt_perd enp   , ben_per_in_ler pil' ||
4642         '   where cpi.group_per_in_ler_id = pil.per_in_ler_id and pil.group_pl_id =   cvl.val_2 '   ||
4643         '   and  cvl.EXT_CRIT_TYP_ID = ' ||  l_wlpr_EXT_CRIT_TYP_ID  ||
4644         '   and  enp.enrt_perd_id   =  cvl.val_1   and  pil.lf_evt_ocrd_dt =  enp.ASND_LF_EVT_DT   ) ' ;
4645    end if;
4646 
4647    if p_data_typ_cd =  'C'  then
4648 
4649       if  l_dynamic_BEN_sql is not null then
4650           l_dynamic_BEN_sql := l_dynamic_BEN_sql || '  ' ||  nvl(l_dynamic_sql, ' ' )  ;
4651       end if ;
4652       if l_dynamic_PAY_sql is not null then
4653           l_dynamic_PAY_sql := l_dynamic_PAY_sql || '  ' ||  nvl(l_dynamic_sql, ' ' )  ;
4654       end if ;
4655 
4656       l_dynamic_sql  :=   l_dynamic_BEN_sql ;
4657       if l_dynamic_sql  is not null  then
4658          hr_utility.set_location(' first  l_dynamic_sql not null ' , 99 );
4659 
4660          if l_dynamic_PAY_sql is not null then
4661             hr_utility.set_location(' first  l_dynamic_pay sql not null ' , 99 );
4662             l_dynamic_sql  :=   l_dynamic_sql || '   UNION ' ||
4663                              l_dynamic_PAY_sql ;
4664          end if ;
4665      else
4666          hr_utility.set_location(' first  l_dynamic_pay sql  null ' , 99 );
4667          if l_dynamic_PAY_sql is not null then
4668             l_dynamic_sql  :=   l_dynamic_PAY_sql  ;
4669          end if ;
4670      end if ;
4671 
4672   end if ;
4673 
4674   hr_utility.set_location(' build completed  ' , 99 );
4675   p_select_statement := l_dynamic_sql;
4676 
4677   --ptilak(p_select_statement) ;
4678 
4679 
4680 end build_select_statement;
4681 
4682 
4683 
4684 
4685 
4686 
4687 Function  check_asg_security
4688           (p_person_id          in number
4689           ,p_effective_date     in date
4690           ,p_business_group_id  in number )
4691           return boolean as
4692 
4693  l_proc                   varchar2(80) := g_package||'.check_asg_security';
4694 
4695  cursor c_asg is
4696  select asg.ASSIGNMENT_TYPE ,asg.assignment_id,asg.business_group_id
4697    from per_all_assignments_f asg
4698    where asg.person_id = p_person_id
4699      and asg.primary_flag =  'Y'
4700      and p_effective_date between asg.effective_start_date
4701          and asg.effective_end_date
4702   ;
4703 
4704 
4705   cursor c_sec_asg (p_assignment_id number) is
4706   select 'x'
4707    from per_all_assignments_f asg
4708    where asg.assignment_id = p_assignment_id
4709      and p_effective_date between asg.effective_start_date
4710          and asg.effective_end_date  ;
4711 
4712 
4713 
4714  l_ASSIGNMENT_TYPE     per_all_assignments_f.ASSIGNMENT_TYPE%type ;
4715  l_assignment_id       per_all_assignments_f.assignment_id%type ;
4716  l_business_group_id   per_all_assignments_f.business_group_id%type ;
4717  l_return  boolean ;
4718  l_temp     varchar2(1) ;
4719 begin
4720 
4721  l_return  := true ;
4722 -- if g_debug then
4723 --    hr_utility.set_location ('Entering '||l_proc,5);
4724 --    hr_utility.set_location ('person  '||p_person_id ,5);
4725 -- end if;
4726 
4727  if  HR_SECURITY.VIEW_ALL  = 'Y'  and hr_general.get_xbg_profile = 'Y'  then
4728 --     hr_utility.set_location ('Exiting '||l_proc,5);
4729      return l_return ;
4730  end if ;
4731 
4732 /* moved down
4733   -- this is common validation for  asg and person
4734  if hr_general.get_xbg_profile <> 'Y'
4735     and hr_general.get_business_group_id is not null
4736     and hr_general.get_business_group_id <>  p_business_group_id   then
4737 --    hr_utility.set_location ('Exiting  false'||l_proc,8);
4738     Return false ;
4739  end if ;
4740 
4741 */
4742 
4743 
4744  open  c_asg ;
4745  fetch c_asg into l_ASSIGNMENT_TYPE,l_assignment_id, l_business_group_id  ;
4746  if c_asg%notfound then
4747     -- if there is no assignmnet, allow the security , it could be dpnt
4748  --       hr_utility.set_location ('assignment not exist dependent  ',5);
4749     close c_asg ;
4750     return l_return ;
4751  end if ;
4752  close c_asg ;
4753 
4754 
4755 
4756   -- this is common validation for  asg and person
4757  if hr_general.get_xbg_profile <> 'Y'
4758     and hr_general.get_business_group_id is not null
4759     and hr_general.get_business_group_id <>  l_business_group_id   then
4760 --    hr_utility.set_location ('Exiting  false'||l_proc,8);
4761     Return false ;
4762  end if ;
4763 
4764 
4765  -- if the assignment type is  E or C validate against the sec view
4766  if l_ASSIGNMENT_TYPE in ('E','C') then
4767 
4768     if HR_SECURITY.VIEW_ALL  <> 'Y' and
4769        HR_SECURITY.SHOW_RECORD('PER_ALL_ASSIGNMENTS_F', L_ASSIGNMENT_ID, P_PERSON_ID, L_ASSIGNMENT_TYPE) <> 'TRUE'  then
4770   --     hr_utility.set_location ('Exiting  false'||l_proc,9);
4771        return false ;
4772     end if ;
4773  end if ;
4774 
4775 
4776 -- if g_debug then
4777 --    hr_utility.set_location ('Exiting '||l_proc,15);
4778 -- end if;
4779  return l_return ;
4780 
4781 end  check_asg_security ;
4782 
4783 procedure init_sub_lvl  is
4784   l_proc                   varchar2(80) := g_package||'.init_sub_lvl';
4785 begin
4786 
4787  if g_debug then
4788     hr_utility.set_location ('Entering '||l_proc,5);
4789  end if;
4790  ben_ext_person.g_location_id              := null;
4791  ben_ext_person.g_location_code            := null;
4792  ben_ext_person.g_location_addr1           := null;
4793  ben_ext_person.g_location_addr2           := null;
4794  ben_ext_person.g_location_addr3           := null;
4795  ben_ext_person.g_location_city            := null;
4796  ben_ext_person.g_location_country         := null;
4797  ben_ext_person.g_location_zip             := null;
4798  ben_ext_person.g_location_region1         := null;
4799  ben_ext_person.g_location_region2         := null;
4800  ben_ext_person.g_location_region3         := null;
4801  ben_ext_person.g_alc_flex_01              := null;
4802  ben_ext_person.g_alc_flex_02              := null;
4803  ben_ext_person.g_alc_flex_03              := null;
4804  ben_ext_person.g_alc_flex_04              := null;
4805  ben_ext_person.g_alc_flex_05              := null;
4806  ben_ext_person.g_alc_flex_06              := null;
4807  ben_ext_person.g_alc_flex_07              := null;
4808  ben_ext_person.g_alc_flex_08              := null;
4809  ben_ext_person.g_alc_flex_09              := null;
4810  ben_ext_person.g_alc_flex_10              := null;
4811  ben_ext_person.g_position_id              := null;
4812  ben_ext_person.g_position                 := null;
4813  ben_ext_person.g_pos_flex_01              := null;
4814  ben_ext_person.g_pos_flex_02          := null;
4815  ben_ext_person.g_pos_flex_03          := null;
4816  ben_ext_person.g_pos_flex_04          := null;
4817  ben_ext_person.g_pos_flex_05          := null;
4818  ben_ext_person.g_pos_flex_06          := null;
4819  ben_ext_person.g_pos_flex_07          := null;
4820  ben_ext_person.g_pos_flex_08          := null;
4821  ben_ext_person.g_pos_flex_09          := null;
4822  ben_ext_person.g_pos_flex_10          := null;
4823  ben_ext_person.g_job_id               := null;
4824  ben_ext_person.g_job                  := null;
4825  ben_ext_person.g_job_flex_01          := null;
4826  ben_ext_person.g_job_flex_02          := null;
4827  ben_ext_person.g_job_flex_03          := null;
4828  ben_ext_person.g_job_flex_04          := null;
4829  ben_ext_person.g_job_flex_05          := null;
4830  ben_ext_person.g_job_flex_06          := null;
4831  ben_ext_person.g_job_flex_07          := null;
4832  ben_ext_person.g_job_flex_08          := null;
4833  ben_ext_person.g_job_flex_09          := null;
4834  ben_ext_person.g_job_flex_10          := null;
4835  ben_ext_person.g_payroll              := null;
4836  ben_ext_person.g_payroll_period_type  := null ;
4837  ben_ext_person.g_prl_flex_01          := null ;
4838  ben_ext_person.g_prl_flex_02          := null ;
4839  ben_ext_person.g_prl_flex_03          := null ;
4840  ben_ext_person.g_prl_flex_04          := null ;
4841  ben_ext_person.g_prl_flex_05          := null ;
4842  ben_ext_person.g_prl_flex_06          := null ;
4843  ben_ext_person.g_prl_flex_07          := null ;
4844  ben_ext_person.g_prl_flex_08          := null ;
4845  ben_ext_person.g_prl_flex_09          := null ;
4846  ben_ext_person.g_prl_flex_10          := null ;
4847  ben_ext_person.g_payroll_period_number:= null ;
4848  ben_ext_person.g_payroll_period_strtdt:= null ;
4849  ben_ext_person.g_payroll_period_enddt := null ;
4850  ben_ext_person.g_payroll_costing      := null ;
4851  ben_ext_person.g_payroll_costing_id   := null ;
4852  ben_ext_person.g_payroll_consolidation_set    := null ;
4853  ben_ext_person.g_payroll_consolidation_set_id := null ;
4854  ben_ext_person.g_group_elmt_value1             := null ;
4855  ben_ext_person.g_group_elmt_value2             := null ;
4856  --
4857  ben_ext_person.g_employee_grade_id    := null;
4858  ben_ext_person.g_employee_grade       := null;
4859  ben_ext_person.g_grd_flex_01          := null;
4860  ben_ext_person.g_grd_flex_02          := null;
4861  ben_ext_person.g_grd_flex_03          := null;
4862  ben_ext_person.g_grd_flex_04          := null;
4863  ben_ext_person.g_grd_flex_05          := null;
4864  ben_ext_person.g_grd_flex_06          := null;
4865  ben_ext_person.g_grd_flex_07          := null;
4866  ben_ext_person.g_grd_flex_08          := null;
4867  ben_ext_person.g_grd_flex_09          := null;
4868  ben_ext_person.g_grd_flex_10          := null;
4869 
4870 
4871  if g_debug then
4872     hr_utility.set_location ('Exiting '||l_proc,15);
4873  end if;
4874 end init_sub_lvl ;
4875 
4876 
4877 procedure  process_subheader(p_ext_file_id        in number
4878                              ,p_ext_dfn_id         in number
4879                              ,p_ext_rslt_id        in number
4880                              ,p_data_typ_cd        in varchar2
4881                              ,p_ext_typ_cd         in varchar2
4882                              ,p_effective_date     in date
4883                              ,p_ext_crit_prfl_id   in number
4884                              ,p_ext_global_flag    in varchar2
4885                              ,p_business_group_id  in number
4886                              ,p_subhdr_ghr_from_dt in date default null
4887                              ,p_subhdr_ghr_to_dt   in date default null
4888                              ) is
4889 
4890   l_proc                   varchar2(80) := g_package||'.process_subheader';
4891 
4892   cursor c_pos(p_org_id number,
4893                p_bg_id  number ) is
4894   select  pos.position_id,pos.job_id
4895     from  HR_ALL_POSITIONS_F  pos
4896     where pos.business_group_id =  p_bg_id
4897       and pos.organization_id   = p_org_id
4898       and p_effective_date between pos.EFFECTIVE_START_DATE
4899           and  nvl(pos.EFFECTIVE_END_DATE ,p_effective_date)
4900    ;
4901 
4902   cursor c_job (p_bg_id  number )   is
4903   select  job.job_id
4904     from  per_jobs_vl job
4905     where job.business_group_id = p_bg_id
4906     and   p_effective_date between job.date_from and nvl(job.date_to,p_effective_date)
4907     ;
4908 
4909 
4910   cursor c_loc(p_bg_id  number ) is
4911   select loc.location_id
4912     from hr_locations_all loc
4913    where loc.business_group_id = p_bg_id
4914          or loc.business_group_id is null    -- for global location
4915    ;
4916 
4917 
4918   cursor c_org (p_bg_id  number ) is
4919   select org.organization_id
4920          ,org.name
4921     from hr_all_organization_units_vl org
4922    where org.business_group_id = p_bg_id
4923    and   p_effective_date between org.date_from and nvl(org.date_to,p_effective_date) ;
4924 
4925 
4926   cursor c_pay (p_bg_id  number )  is
4927   select payroll_id
4928   from   pay_payrolls_f  pay
4929    where pay.business_group_id = p_bg_id
4930     -- and pay.organization_id   = p_org_id
4931      and p_effective_date between pay.EFFECTIVE_START_DATE  and  pay.EFFECTIVE_END_DATE
4932      ;
4933 
4934 
4935   cursor c_grade (p_bg_id  number )  is
4936   select grade_id
4937   from   per_grades_vl  grd
4938    where grd.business_group_id = p_bg_id
4939    and   p_effective_date between grd.date_from and nvl(grd.date_to,p_effective_date)
4940   ;
4941 
4942   cursor c_pos2(p_org_id number,
4943                 p_bg_id  number ,
4944                 p_subhdr_ghr_from_dt  date ,
4945                 p_subhdr_ghr_to_dt   date
4946                ) is
4947   select
4948          distinct pos.position_id position_id
4949    from  ghr_pa_history  gph  ,
4950          HR_ALL_POSITIONS_F pos
4951   where  (   (gph.table_name = 'HR_ALL_POSITIONS_F'
4952               and pos.POSITION_ID = gph.information1 )
4953           or (gph.table_name = 'PER_POSITION_EXTRA_INFO'
4954               and pos.position_id = gph.information4 --  info4 is position_id
4955               and gph.information5 in ('GHR_US_POS_GRP1','GHR_US_POS_GRP2' ,'GHR_US_POS_VALID_GRADE','GHR_US_POS_GRP3',
4956                                        'GHR_US_POS_OBLIG', 'GHR_US_POS_MASS_ACTIONS', 'GHR_US_POSITION_LANGUAGE',
4957                                        'GHR_US_POSITION_INTERDISC', 'GHR_US_POSITION_DESCRIPTION' )
4958              )
4959          )
4960      and pos.business_group_id = p_bg_id
4961      and pos.organization_id   = p_org_id
4962      and gph.effective_date between pos.EFFECTIVE_START_DATE
4963          and  pos.EFFECTIVE_END_DATE
4964      and ( p_subhdr_ghr_from_dt is null
4965           or (
4966                trunc(gph.effective_date) between p_subhdr_ghr_from_dt and nvl(p_subhdr_ghr_to_dt, p_subhdr_ghr_from_dt)
4967                 or
4968                ( trunc(gph.process_date) between   p_subhdr_ghr_from_dt and nvl(p_subhdr_ghr_to_dt, p_subhdr_ghr_from_dt)
4969                  and   trunc(gph.effective_date) <=  nvl(p_subhdr_ghr_to_dt, p_subhdr_ghr_from_dt)
4970                )
4971              )
4972          )
4973    ;
4974 
4975 
4976    cursor c_ced is
4977     select
4978            xct.excld_flag,
4979            xcv.val_1,
4980            xcv.val_2
4981     from   ben_ext_crit_typ xct,
4982            ben_ext_crit_val xcv
4983     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
4984     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
4985     and    xct.crit_typ_cd = 'CED';
4986 
4987 
4988    cursor c_cad is
4989     Select
4990            xct.excld_flag,
4991            xcv.val_1,
4992            xcv.val_2
4993     from   ben_ext_crit_typ xct,
4994            ben_ext_crit_val xcv
4995     where  xct.ext_crit_prfl_id = p_ext_crit_prfl_id
4996     and    xct.ext_crit_typ_id = xcv.ext_crit_typ_id
4997     and    xct.crit_typ_cd = 'CAD';
4998 
4999 
5000 
5001    cursor c_bg is
5002    select business_group_id , name
5003    from per_business_groups
5004    where  p_ext_global_flag = 'Y'
5005      or   business_group_id =  p_business_group_id
5006    ;
5007 
5008    cursor c_pbg is
5009    select business_group_id , name
5010    from per_business_groups
5011    where business_group_id =  p_business_group_id
5012   ;
5013 
5014 
5015   l_include                 varchar2(1)   ;
5016   l_person_id               number ;
5017   l_business_group_id       number ;
5018   l_proc_business_group_id  number ;
5019   l_proc_business_group_name per_business_groups.name%type ;
5020 
5021   p_chg_actl_dt_from  date ;
5022   p_chg_actl_dt_to    date ;
5023   p_chg_eff_dt_from   date ;
5024   p_chg_eff_dt_to     date ;
5025   l_actl_exclude_flag      varchar2(1) ;
5026   l_eff_exclude_flag      varchar2(1) ;
5027   l_val1   ben_ext_crit_val.val_1%type ;
5028   l_val2   ben_ext_crit_val.val_1%type ;
5029 
5030 
5031 Begin
5032 
5033  l_person_id := 0   ;
5034  l_include   := 'Y' ;
5035  if g_debug then
5036     hr_utility.set_location ('Entering '||l_proc,5);
5037  end if;
5038   --
5039   ben_extract.set_ext_lvls
5040     (p_ext_file_id         => p_ext_file_id
5041     ,p_business_group_id   => p_business_group_id
5042     );
5043 
5044   if nvl(ben_extract.g_subhead_dfn,'N') <> 'Y' then
5045     if g_debug then
5046        hr_utility.set_location ('Exiting  no definition found '||l_proc,10);
5047     end if;
5048     return ;
5049   end if ;
5050   --
5051    IF p_ext_crit_prfl_id is not null THEN
5052     --
5053       ben_ext_person.g_effective_date := p_effective_date  ;
5054       ben_ext_evaluate_inclusion.Determine_Incl_Crit_To_Check(p_ext_crit_prfl_id);
5055     --
5056    END IF;
5057 
5058 
5059   -- Setup record and required level tables
5060   --
5061   ben_extract.setup_rcd_typ_lvl
5062     (p_ext_file_id => p_ext_file_id
5063     );
5064   --
5065   g_ext_dfn_id := p_ext_dfn_id;
5066   g_ext_rslt_id := p_ext_rslt_id;
5067 
5068   hr_utility.set_location ( ' org ' || ben_extract.g_org_lvl || '  pos  '|| ben_extract.g_pos_lvl ||
5069                              '  pay ' ||  ben_extract.g_pay_lvl || '  bg ' || ben_extract.g_bg_csr  , 99 ) ;
5070   --- get the processing bg
5071   --if  ben_extract.g_bg_csr = 'Y' then
5072       open c_pbg ;
5073       fetch c_pbg into
5074             ben_extract.g_proc_business_group_id ,
5075             ben_extract.g_proc_business_group_name;
5076       close c_pbg ;
5077       hr_utility.set_location ( ' proc bg ' || ben_extract.g_proc_business_group_name , 99 ) ;
5078   --end if ;
5079 
5080 
5081   for l_bg  in c_bg  Loop
5082 
5083       if p_ext_crit_prfl_id is not null then
5084          ben_ext_evaluate_inclusion.Evaluate_subhead_incl
5085          (
5086           p_business_group_id  => l_bg.business_group_id,
5087           p_include            => l_include,
5088           p_effective_date     => p_effective_date );
5089       end if ;
5090 
5091 
5092       if p_ext_crit_prfl_id is not null then
5093           ben_ext_evaluate_inclusion.Evaluate_subhead_incl
5094              (p_business_group_id  => l_bg.business_group_id    ,
5095               p_include            => l_include,
5096               p_effective_date     =>  p_effective_date );
5097       end if ;
5098 
5099 
5100       if l_include = 'Y'  then
5101 
5102          if  ben_extract.g_bg_csr = 'Y' then
5103 
5104              ben_ext_person.g_business_group_id  := l_bg.business_group_id ;
5105              ben_extract.g_business_group_name   := l_bg.name  ;
5106          end if ;
5107 
5108          --- extract organization and postion level
5109          if ben_extract.g_org_lvl = 'Y' or ben_extract.g_pos_lvl = 'Y'   then
5110               for i in c_org(l_bg.business_group_id)  loop
5111 
5112                   hr_utility.set_location ('_organization '||i.name,10);
5113                   if ben_extract.g_org_lvl = 'Y'  then
5114 
5115                      if p_ext_crit_prfl_id is not null then
5116                         ben_ext_evaluate_inclusion.Evaluate_subhead_incl
5117                             (p_organization_id     => i.organization_id  ,
5118                              p_business_group_id  => l_bg.business_group_id     ,
5119                              p_include            => l_include,
5120                              p_effective_date     =>  p_effective_date );
5121                      end if ;
5122 
5123                      if l_include = 'Y' then
5124                         ben_ext_person.g_employee_organization_id := i.organization_id ;
5125                         ben_ext_person.g_employee_organization    := i.name ;
5126                         ben_ext_fmt.process_ext_recs(p_ext_rslt_id     => p_ext_rslt_id,
5127                                              p_ext_file_id       => p_ext_file_id,
5128                                              p_data_typ_cd       => p_data_typ_cd,
5129                                              p_ext_typ_cd        => p_ext_typ_cd,
5130                                              p_rcd_typ_cd        => 'S',
5131                                              p_low_lvl_cd        => 'OR',
5132                                              p_person_id         => l_person_id,
5133                                              p_chg_evt_cd        => null,
5134                                              p_business_group_id => p_business_group_id,
5135                                              p_ext_per_bg_id    => l_bg.business_group_id,
5136                                              p_effective_date    => p_effective_date
5137                                              );
5138                       end if ;  -- include
5139                   end if ; -- ben_extract.g_org_lvl
5140 
5141                   --- position
5142 
5143                   if ben_extract.g_pos_lvl = 'Y'  then
5144 
5145                       hr_utility.set_location( 'spl flag ' || g_subhdr_chg_log , 99 );
5146 
5147                        if  g_subhdr_chg_log = 'Y'  then
5148 
5149                            ---
5150                            for j in c_pos2 (i.organization_id
5151                                      ,l_bg.business_group_id
5152                                      ,p_subhdr_ghr_from_dt
5153                                      ,p_subhdr_ghr_to_dt
5154                                     )  Loop
5155 
5156                             init_sub_lvl ;
5157                             l_include := 'Y' ;
5158                             -- call inclusion criteris
5159                             if p_ext_crit_prfl_id is not null then
5160                                ben_ext_evaluate_inclusion.Evaluate_subhead_incl
5161                                (p_organization_id    => i.organization_id  ,
5162                                 p_position_id        => j.position_id  ,
5163                                 p_business_group_id  => l_bg.business_group_id     ,
5164                                 p_include            => l_include,
5165                                 p_effective_date     => p_effective_date
5166                                );
5167 
5168                                 hr_utility.set_location( ' include  ' || l_include , 99 );
5169 
5170                             end if ;
5171 
5172                            -- call the function from per_person_extract
5173                            if  l_include = 'Y' then
5174                                ben_ext_person.g_employee_organization_id := i.organization_id ;
5175                                ben_ext_person.g_employee_organization    := i.name ;
5176                                ben_ext_person.g_position_id              := j.position_id ;
5177 
5178                                ben_ext_person.get_pos_info(p_position_id    =>  j.position_id,
5179                                                            p_effective_date =>  p_effective_date) ;
5180 
5181                                hr_utility.set_location ('position '||j.position_id,10);
5182                                ben_ext_fmt.process_ext_recs(
5183                                                 p_ext_rslt_id       => p_ext_rslt_id,
5184                                                 p_ext_file_id       => p_ext_file_id,
5185                                                 p_data_typ_cd       => p_data_typ_cd,
5186                                                 p_ext_typ_cd        => p_ext_typ_cd,
5187                                                 p_rcd_typ_cd        => 'S',
5188                                                 p_low_lvl_cd        => 'PO',
5189                                                 p_person_id         => l_person_id,
5190                                                 p_chg_evt_cd        => null,
5191                                                 p_business_group_id => p_business_group_id,
5192                                                 p_ext_per_bg_id    => l_bg.business_group_id,
5193                                                 p_effective_date    => p_effective_date
5194                                                 );
5195                            end if ;
5196                         end loop ;
5197 
5198 
5199                     else
5200                         for j in c_pos (i.organization_id
5201                                     ,l_bg.business_group_id)  Loop
5202                             init_sub_lvl ;
5203                             l_include := 'Y' ;
5204                             -- call inclusion criteris
5205                             if p_ext_crit_prfl_id is not null then
5206                                ben_ext_evaluate_inclusion.Evaluate_subhead_incl
5207                                   (p_organization_id     => i.organization_id  ,
5208                                    p_position_id         => j.position_id  ,
5209                                    p_business_group_id  => l_bg.business_group_id     ,
5210                                    p_include            => l_include,
5211                                    p_effective_date     =>  p_effective_date );
5212                             end if ;
5213 
5214                             -- call the function from per_person_extract
5215                             if l_include = 'Y' then
5216                                ben_ext_person.g_employee_organization_id := i.organization_id ;
5217                                ben_ext_person.g_employee_organization    := i.name ;
5218                                ben_ext_person.g_position_id              := j.position_id ;
5219                                ben_ext_person.get_pos_info(p_position_id     =>  j.position_id,
5220                                                         p_effective_date =>  p_effective_date ) ;
5221 
5222                                hr_utility.set_location ('position '||j.position_id,10);
5223                                ben_ext_fmt.process_ext_recs(p_ext_rslt_id       => p_ext_rslt_id,
5224                                                 p_ext_file_id       => p_ext_file_id,
5225                                                 p_data_typ_cd       => p_data_typ_cd,
5226                                                 p_ext_typ_cd        => p_ext_typ_cd,
5227                                                 p_rcd_typ_cd        => 'S',
5228                                                 p_low_lvl_cd        => 'PO',
5229                                                 p_person_id         => l_person_id,
5230                                                 p_chg_evt_cd        => null,
5231                                                 p_business_group_id => p_business_group_id,
5232                                                 p_ext_per_bg_id    => l_bg.business_group_id,
5233                                                 p_effective_date    => p_effective_date
5234                                                 );
5235                            end if ;
5236                        end loop ;
5237                     end if ; --- g_subhdr_chg_log
5238                   end if ;
5239            end loop ;
5240          end if ;
5241 
5242 
5243          --- payroll
5244 
5245 
5246          if ben_extract.g_pay_lvl = 'Y'  then
5247             for j in c_pay (l_bg.business_group_id )   Loop
5248                 init_sub_lvl ;
5249                 l_include := 'Y' ;
5250                 -- call inclusion criteris
5251                 if p_ext_crit_prfl_id is not null then
5252                    ben_ext_evaluate_inclusion.Evaluate_subhead_incl
5253                             (
5254                              p_payroll_id          => j.payroll_id  ,
5255                              p_business_group_id  => l_bg.business_group_id     ,
5256                              p_include            => l_include,
5257                             p_effective_date     =>  p_effective_date );
5258                 end if ;
5259                  --
5260                 -- call the function from per_person_extract
5261                 if  l_include = 'Y' then
5262                      ben_ext_person.get_payroll_info(p_payroll_id     =>  j.payroll_id,
5263                                                   p_effective_date =>  p_effective_date ) ;
5264 
5265                      hr_utility.set_location ('payroll id  '||j.payroll_id,10);
5266                      ben_ext_fmt.process_ext_recs(p_ext_rslt_id       => p_ext_rslt_id,
5267                                              p_ext_file_id       => p_ext_file_id,
5268                                              p_data_typ_cd       => p_data_typ_cd,
5269                                              p_ext_typ_cd        => p_ext_typ_cd,
5270                                              p_rcd_typ_cd        => 'S',
5271                                              p_low_lvl_cd        => 'PY',
5272                                              p_person_id         => l_person_id,
5273                                              p_chg_evt_cd        => null,
5274                                              p_business_group_id => p_business_group_id,
5275                                              p_ext_per_bg_id    => l_bg.business_group_id,
5276                                              p_effective_date    => p_effective_date
5277                                              );
5278                 end if ;
5279             end loop ;
5280          end if ;
5281 
5282 
5283 
5284 
5285          --- extract  job level
5286 
5287          if ben_extract.g_job_lvl = 'Y'  then
5288             for i in c_job (l_bg.business_group_id)  loop
5289                init_sub_lvl ;
5290                l_include := 'Y' ;
5291                -- call inclusion criteris
5292                if p_ext_crit_prfl_id is not null then
5293                   ben_ext_evaluate_inclusion.Evaluate_subhead_incl(
5294                              p_job_id              =>  i.job_id  ,
5295                              p_business_group_id  => l_bg.business_group_id     ,
5296                              p_include            => l_include,
5297                              p_effective_date     =>  p_effective_date );
5298                end if ;
5299                --
5300                -- call the function from per_person_extract
5301                if  l_include = 'Y' then
5302                    ben_ext_person.g_job_id              := i.job_id ;
5303                    ben_ext_person.get_job_info(p_job_id     =>  i.job_id,
5304                                             p_effective_date =>  p_effective_date ) ;
5305 
5306                    hr_utility.set_location ('job id  '||i.job_id,10);
5307                    ben_ext_fmt.process_ext_recs(p_ext_rslt_id       => p_ext_rslt_id,
5308                                              p_ext_file_id       => p_ext_file_id,
5309                                              p_data_typ_cd       => p_data_typ_cd,
5310                                              p_ext_typ_cd        => p_ext_typ_cd,
5311                                              p_rcd_typ_cd        => 'S',
5312                                              p_low_lvl_cd        => 'JB',
5313                                              p_person_id         => l_person_id,
5314                                              p_chg_evt_cd        => null,
5315                                              p_business_group_id => p_business_group_id,
5316                                              p_ext_per_bg_id    => l_bg.business_group_id,
5317                                              p_effective_date    => p_effective_date
5318                                              );
5319                end if ;
5320 
5321             end loop;
5322          end if ;
5323          --- extractr location level
5324 
5325          if ben_extract.g_loc_lvl = 'Y'  then
5326             for i in c_loc (l_bg.business_group_id)  loop
5327                 init_sub_lvl ;
5328                  l_include := 'Y' ;
5329                -- call inclusion criteris
5330                if p_ext_crit_prfl_id is not null then
5331                   ben_ext_evaluate_inclusion.Evaluate_subhead_incl(
5332                                 p_location_id        => i.location_id  ,
5333                                 p_business_group_id  => l_bg.business_group_id ,
5334                                 p_include            => l_include,
5335                                 p_effective_date     =>  p_effective_date );
5336                end if ;
5337                --
5338                -- call the function from per_person_extract
5339                if  l_include = 'Y' then
5340                    ben_ext_person.g_location_id              := i.location_id ;
5341                    ben_ext_person.get_loc_info(p_location_id     =>  i.location_id,
5342                                                p_effective_date =>  p_effective_date ) ;
5343 
5344                    hr_utility.set_location ('payroll id  '||i.location_id,10);
5345                    ben_ext_fmt.process_ext_recs(p_ext_rslt_id       => p_ext_rslt_id,
5346                                                 p_ext_file_id       => p_ext_file_id,
5347                                                 p_data_typ_cd       => p_data_typ_cd,
5348                                                 p_ext_typ_cd        => p_ext_typ_cd,
5349                                                 p_rcd_typ_cd        => 'S',
5350                                                 p_low_lvl_cd        => 'LO',
5351                                                 p_person_id         => l_person_id,
5352                                                 p_chg_evt_cd        => null,
5353                                                 p_business_group_id => p_business_group_id,
5354                                                 p_ext_per_bg_id    => l_bg.business_group_id,
5355                                                 p_effective_date    => p_effective_date
5356                                                 );
5357                end if ;
5358             end loop;
5359          end if ;
5360 
5361          -- extract grade informations
5362         if ben_extract.g_grd_lvl = 'Y'  then
5363             for i in c_grade (l_bg.business_group_id)  loop
5364                 init_sub_lvl ;
5365                  l_include := 'Y' ;
5366                -- call inclusion criteris
5367                if p_ext_crit_prfl_id is not null then
5368                   ben_ext_evaluate_inclusion.Evaluate_subhead_incl(
5369                                 p_grade_id        => i.grade_id  ,
5370                                 p_business_group_id  => l_bg.business_group_id ,
5371                                 p_include            => l_include,
5372                                 p_effective_date     =>  p_effective_date );
5373                end if ;
5374                --
5375                -- call the function from per_person_extract
5376                if  l_include = 'Y' then
5377                    ben_ext_person.g_employee_grade_id := i.grade_id ;
5378                    ben_ext_person.get_grade_info(p_grade_id     =>  i.grade_id,
5379                                                p_effective_date =>  p_effective_date ) ;
5380 
5381                    hr_utility.set_location ('grade id  '||i.grade_id,10);
5382                    ben_ext_fmt.process_ext_recs(p_ext_rslt_id       => p_ext_rslt_id,
5383                                                 p_ext_file_id       => p_ext_file_id,
5384                                                 p_data_typ_cd       => p_data_typ_cd,
5385                                                 p_ext_typ_cd        => p_ext_typ_cd,
5386                                                 p_rcd_typ_cd        => 'S',
5387                                                 p_low_lvl_cd        => 'GR',
5388                                                 p_person_id         => l_person_id,
5389                                                 p_chg_evt_cd        => null,
5390                                                 p_business_group_id => p_business_group_id,
5391                                                 p_ext_per_bg_id    => l_bg.business_group_id,
5392                                                 p_effective_date    => p_effective_date
5393                                                 );
5394                end if ;
5395             end loop;
5396          end if ;
5397 
5398 
5399       end if ;  -- bg include
5400   end Loop ;   -- bg level
5401   --reintialize all the variable before person is extracted
5402   init_sub_lvl ;
5403  if g_debug then
5404     hr_utility.set_location ('Exiting '||l_proc,15);
5405  end if;
5406 
5407 End ;
5408 
5409 
5410 
5411 
5412 -- =======================================================================
5413 --                          <<Procedure:Process>>
5414 -- -----------------------------------------------------------------------
5415 -- -----------------------------------------------------------------------
5416 -- This is the main batch procedure to be called from the concurrent manager
5417 -- or interactively to start extract.
5418 -- ========================================================================
5419 --
5420 procedure process
5421   (errbuf                 out nocopy varchar2
5422   ,retcode                out nocopy varchar2
5423   ,p_benefit_action_id    in  number
5424   ,p_ext_dfn_id           in  number
5425   ,p_effective_date       in  varchar2
5426   ,p_business_group_id    in  number
5427   ---
5428   ,p_output_type          in  varchar2 default null
5429   ,p_out_dummy            in  varchar2  default null
5430   ,p_xdo_template_id      in  number default null
5431   ,p_eff_start_date       in  varchar2   default null
5432   ,p_eff_end_date         in  varchar2   default null
5433   ,p_act_start_date       in  varchar2   default null
5434   ,p_act_end_date         in  varchar2   default null
5435   ---for restart
5436   ,p_ext_rslt_id          in  number default null
5437   -- for PQP subheader
5438   ,p_subhdr_chg_log       in  varchar2  default null
5439   ,p_penserv_date         in  date      default null    -- vkodedal changes for penserver
5440   ,p_penserv_mode         in  varchar2  default 'N'     ---      ''       ''
5441   ,p_person_selection_rl in  number   default null
5442 ) is
5443   --
5444   -- if directory not specified, then grab the first
5445   -- in the utl_file_dir path
5446   --
5447   /* cursor c_get_dflt_dir is
5448     select substr(value,1,instr(value,',')-1)
5449     from gv$system_parameter
5450     where name = 'utl_file_dir'
5451     and value is not null ; */
5452   -- bug 3187407
5453   -- to handle situations where only 1 directory specified in utl_file_dir.
5454   --
5455   cursor c_get_dflt_dir is
5456   select decode (instr(ltrim(value),','), 0 ,
5457          ltrim(value),
5458          substrb(value,1,instr(ltrim(value),',')-1) )
5459   from v$parameter where name = 'utl_file_dir';
5460   --
5461   l_proc                   varchar2(80) := g_package||'.process';
5462   --
5463   -- Update by RChase 01-MAR-2000
5464   --
5465   l_current_loc NUMBER:=0;
5466   -- End Update
5467   l_request_id             number;
5468   l_benefit_action_id      ben_benefit_actions.benefit_action_id%type;
5469   l_person_id              l_number_type := l_number_type();
5470   l_person_action_id       l_number_type := l_number_type();
5471   l_range_id               ben_batch_ranges.range_id%type;
5472   l_object_version_number  ben_batch_ranges.object_version_number%type;
5473   l_chunk_size             number(5) := 20;
5474   l_threads                number(5) := 1;
5475   l_max_errors_allowed     number(5) := 20;
5476   l_start_person_action_id number := 0;
5477   l_end_person_action_id   number := 0;
5478   l_prev_person_id         number := 0;
5479   l_person_cnt             number := 0;
5480   l_step                   number := 0;
5481   l_num_range              number := 0;
5482   l_dump_num               number(15);
5483   l_count                  number;
5484   l_data_typ_cd            hr_lookups.lookup_code%TYPE ; -- UTF8 varchar2(30);
5485   l_ext_typ_cd             hr_lookups.lookup_code%TYPE ; -- UTF8 varchar2(30);
5486   l_ext_crit_prfl_id       number(15);
5487   l_ext_file_id            number(15);
5488   l_ext_rslt_id            number(15);
5489   l_ext_strt_dt_cd         hr_lookups.lookup_code%TYPE ; -- UTF8 varchar2(30);
5490   l_ext_end_dt_cd          hr_lookups.lookup_code%TYPE ; -- UTF8 varchar2(30);
5491   l_ext_strt_dt            date;
5492   l_ext_end_dt             date;
5493   l_prmy_sort_cd           hr_lookups.lookup_code%TYPE ; -- UTF8 varchar2(30);
5494   l_scnd_sort_cd           hr_lookups.lookup_code%TYPE ; -- UTF8 varchar2(30);
5495   l_output_name            ben_ext_rslt.output_name%type ;
5496   l_drctry_name            ben_ext_rslt.drctry_name%type ;
5497   l_apnd_rqst_id_flag      ben_ext_dfn.apnd_rqst_id_flag%TYPE;  -- UTF8 varchar2(40);
5498   l_kickoff_wrt_prc_flag   ben_ext_dfn.kickoff_wrt_prc_flag%TYPE;  -- UTF8 varchar2(40);
5499   l_use_eff_dt_for_chgs_flag  ben_ext_dfn.use_eff_dt_for_chgs_flag%TYPE ; -- UTF8 varchar2(40);
5500   l_ext_post_prcs_rl       number;
5501   l_xrs_object_version_number number;
5502   l_error_cd               hr_lookups.lookup_code%TYPE ; -- UTF8 varchar2(80);
5503   l_line                   number := 0;
5504   l_effective_date         date;
5505   l_conc_request_id        number;
5506   l_select_statement       varchar2(32000);
5507   l_num_rows               number := 0;
5508   l_ext_global_flag        varchar2(30) ;
5509   l_cm_display_flag        varchar2(30) ;
5510   l_ext_stat_cd            varchar2(30) ;
5511   l_dummy                  varchar2(30) ;
5512   --
5513 
5514   cursor c_rslt is
5515     select object_version_number
5516     from ben_ext_rslt
5517     where ext_rslt_id = p_ext_rslt_id ;
5518 
5519   -- check the defintion of detail levle
5520    cursor chk_D_lvl(p_ext_file_id number) is
5521      select 'Y'
5522      from ben_ext_rcd         a,
5523           ben_ext_rcd_in_file  b
5524      where a.ext_rcd_id  = b.ext_rcd_id
5525        and b.ext_file_id = p_ext_file_id
5526        and a.rcd_type_cd = 'D'
5527      ;
5528   --
5529   cursor c_Ext_err_only (c_ext_rslt_id  number) is
5530   select 'x'  from
5531   ben_Ext_rslt_err
5532   where typ_cd  = 'E'
5533   and  ext_rslt_id = c_ext_rslt_id
5534   ;
5535   --
5536   l_outputs  ff_exec.outputs_t;
5537   --
5538   TYPE PersonCurType is REF CURSOR;
5539   --
5540   PersonCur     PersonCurType;
5541   --
5542   TYPE PersonRec is RECORD
5543   (person_id   number(15));
5544   --
5545   l_rec         PersonRec;
5546   l_status      integer;
5547   l_chg_actl_strt_dt date;
5548   l_chg_actl_end_dt date;
5549   l_chg_eff_strt_dt date;
5550   l_chg_eff_end_dt date;
5551   l_to_be_sent_strt_dt date;
5552   l_to_be_sent_end_dt date;
5553   l_commit number;
5554   l_output_type  varchar2(30) ;
5555   l_output_code  varchar2(30) ;
5556   l_xdo_template_id  number ;
5557   l_eff_start_date   date  ;
5558   l_eff_end_date     date  ;
5559   l_act_start_date   date  ;
5560   l_act_end_date     date  ;
5561   l_D_lvl_found varchar2(1) ;
5562   --
5563 
5564   cursor c_xdoi (c_xdo_id  number)  is
5565   select application_short_name ,
5566          template_code ,
5567          default_language,
5568          default_territory
5569   from xdo_templates_b
5570  where template_id = c_xdo_id  ;
5571 
5572  l_application_short_name  xdo_templates_b.application_short_name%type ;
5573  l_template_code           xdo_templates_b.template_code%type ;
5574  l_default_language        xdo_templates_b.default_language%type ;
5575  l_default_territory       xdo_templates_b.default_territory%type ;
5576  l_fnd_out                 boolean ;
5577 
5578  l_skip                   boolean;
5579  l_rule_value             varchar2(30);
5580 
5581 Begin
5582   --
5583   g_debug := hr_utility.debug_enabled;
5584   if g_debug then
5585     hr_utility.set_location ('Entering '||l_proc,5);
5586   end if;
5587   --
5588 
5589   l_conc_request_id := fnd_global.conc_request_id;
5590   g_subhdr_chg_log  := nvl(p_subhdr_chg_log, 'N') ;
5591   --
5592   initialize_globals;
5593   --
5594   l_effective_date := to_date(p_effective_date, 'YYYY/MM/DD HH24:MI:SS');
5595   l_effective_date := to_date(to_char(trunc(l_effective_date), 'DD/MM/RRRR'),
5596                       'DD/MM/RRRR');
5597 
5598 
5599   if p_eff_start_date is not null then
5600 
5601      l_eff_start_date := to_date(p_eff_start_date, 'YYYY/MM/DD HH24:MI:SS');
5602      l_eff_start_date := to_date(to_char(trunc(l_eff_start_date), 'DD/MM/RRRR'),
5603                       'DD/MM/RRRR');
5604 
5605      l_eff_end_date := to_date(p_eff_end_date, 'YYYY/MM/DD HH24:MI:SS');
5606      l_eff_end_date := to_date(to_char(trunc(l_eff_end_date), 'DD/MM/RRRR'),
5607                       'DD/MM/RRRR');
5608 
5609   end if ;
5610 
5611   if p_act_start_date is not null then
5612      l_act_start_date := to_date(p_act_start_date, 'YYYY/MM/DD HH24:MI:SS');
5613      l_act_start_date := to_date(to_char(trunc(l_act_start_date), 'DD/MM/RRRR'),
5614                       'DD/MM/RRRR');
5615 
5616      l_act_end_date := to_date(p_act_end_date, 'YYYY/MM/DD HH24:MI:SS');
5617      l_act_end_date := to_date(to_char(trunc(l_act_end_date), 'DD/MM/RRRR'),
5618                       'DD/MM/RRRR');
5619 
5620 
5621   end if ;
5622 
5623   -- if customer pass one parameter errro
5624   if ( p_eff_start_date is not null and p_eff_end_date is null )
5625      or (  p_act_start_date is not null and p_act_end_date is null )
5626      then
5627     ben_ext_thread.g_err_num := 91919;
5628     ben_ext_thread.g_err_name := 'BEN_91919_EXT_END_DT_NULL';
5629     raise g_job_failure_error;
5630 
5631   end if ;
5632 
5633 
5634   l_line := 1;
5635   --
5636   -- load the fnd_session table in case a rule dbi needs it.
5637   --
5638   dt_fndate.change_ses_date
5639     (p_ses_date => l_effective_date,
5640      p_commit => l_commit);
5641   --
5642   if p_ext_dfn_id is null then
5643     --
5644     hr_api.mandatory_arg_error(p_api_name       => l_proc
5645                               ,p_argument       => 'p_ext_dfn_id'
5646                               ,p_argument_value => p_ext_dfn_id);
5647     --
5648   elsif p_effective_date is null then
5649     --
5650     hr_api.mandatory_arg_error(p_api_name       => l_proc
5651                               ,p_argument       => 'p_effective_date'
5652                               ,p_argument_value => p_effective_date);
5653     --
5654   elsif p_business_group_id is null then
5655     --
5656     hr_api.mandatory_arg_error(p_api_name       => l_proc
5657                               ,p_argument       => 'p_business_group_id'
5658                               ,p_argument_value => p_business_group_id);
5659     --
5660   end if;
5661   --
5662   l_line := 2;
5663   --
5664   get_ext_prmtrs
5665     (p_ext_dfn_id               =>  p_ext_dfn_id,
5666      p_business_group_id        =>  p_business_group_id,
5667      p_data_typ_cd              =>  l_data_typ_cd,
5668      p_ext_typ_cd               =>  l_ext_typ_cd,
5669      p_ext_crit_prfl_id         =>  l_ext_crit_prfl_id,
5670      p_ext_file_id              =>  l_ext_file_id,
5671      p_ext_strt_dt              =>  l_ext_strt_dt_cd,
5672      p_ext_end_dt               =>  l_ext_end_dt_cd,
5673      p_prmy_sort_cd             =>  l_prmy_sort_cd,
5674      p_scnd_sort_cd             =>  l_scnd_sort_cd,
5675      p_output_name              =>  l_output_name,
5676      p_drctry_name              =>  l_drctry_name,
5677      p_apnd_rqst_id_flag        =>  l_apnd_rqst_id_flag,
5678      p_kickoff_wrt_prc_flag     =>  l_kickoff_wrt_prc_flag,
5679      p_use_eff_dt_for_chgs_flag => l_use_eff_dt_for_chgs_flag,
5680      p_ext_post_prcs_rl         => l_ext_post_prcs_rl,
5681      p_ext_global_flag          => l_ext_global_flag ,
5682      p_cm_display_flag          => l_cm_display_flag ,
5683      p_output_type              =>  l_output_type,
5684      p_xdo_template_id          =>  l_xdo_template_id
5685     );
5686   --
5687   if l_ext_file_id is null then
5688     --
5689     ben_ext_thread.g_err_num := 91917;
5690     ben_ext_thread.g_err_name := 'BEN_91917_EXT_FILE_NULL';
5691     raise g_job_failure_error;
5692     --
5693   end if;
5694 
5695   l_D_lvl_found := 'N' ;
5696   open chk_D_lvl(l_ext_file_id) ;
5697   fetch chk_D_lvl into l_D_lvl_found ;
5698   close chk_D_lvl ;
5699 
5700  if p_output_type is not null then
5701     l_output_type := p_output_type ;
5702     l_xdo_template_id := nvl(p_xdo_template_id, l_xdo_template_id) ;
5703     -- in case ext dfn for pdf and  parameter is file then
5704     -- nullify the template if
5705     if p_output_type in  ('F','X') and l_xdo_template_id is not null then
5706        l_xdo_template_id := null ;
5707     end if ;
5708  end if ;
5709 
5710 
5711   --
5712   -- Determine output file name
5713   --
5714   IF l_output_name is not null and l_apnd_rqst_id_flag = 'Y' THEN
5715     --
5716     l_output_name := l_output_name || '.' ||
5717                      to_char(l_conc_request_id);
5718     --
5719   END IF;
5720   --
5721   l_line := 3;
5722   --  Calculate actual extract dates
5723   --
5724   /*
5725   l_ext_strt_dt := ben_ext_util.calc_ext_date
5726                    (p_ext_date_cd => l_ext_strt_dt_cd,
5727                     p_abs_date    => l_effective_date,
5728                     p_ext_dfn_id => p_ext_dfn_id
5729                    );
5730   g_ext_strt_dt := l_ext_strt_dt;
5731   --
5732   IF l_data_typ_cd = 'F' THEN
5733     --
5734     l_ext_end_dt := l_ext_strt_dt;
5735     --
5736   ELSE
5737     --
5738     l_ext_end_dt := ben_ext_util.calc_ext_date
5739                     (p_ext_date_cd => l_ext_end_dt_cd,
5740                      p_abs_date    => l_effective_date,
5741                      p_ext_dfn_id => p_ext_dfn_id
5742                      );
5743     --
5744   END IF;
5745   --
5746   if l_ext_end_dt < l_ext_strt_dt then
5747     ben_ext_thread.g_err_num := 92080;
5748     ben_ext_thread.g_err_name := 'BEN_92080_EXT_STRT_END_DT';
5749     raise g_job_failure_error;
5750   end if;
5751   --
5752   g_ext_end_dt := l_ext_end_dt;
5753   --
5754   if l_ext_strt_dt is null then
5755     ben_ext_thread.g_err_num := 91918;
5756     ben_ext_thread.g_err_name := 'BEN_91918_EXT_STRT_DT_NULL';
5757     raise g_job_failure_error;
5758   end if;
5759   --
5760   if l_ext_end_dt is null then
5761     ben_ext_thread.g_err_num := 91919;
5762     ben_ext_thread.g_err_name := 'BEN_91919_EXT_END_DT_NULL';
5763     raise g_job_failure_error;
5764   end if;
5765   --
5766   */
5767   --
5768   l_line := 4;
5769   --
5770   if l_output_name is null then
5771     l_output_name := 'outfile';
5772   end if;
5773   --
5774   if l_drctry_name is null then
5775     --
5776     open c_get_dflt_dir;
5777       --
5778       fetch c_get_dflt_dir into l_drctry_name;
5779       --
5780     close c_get_dflt_dir;
5781     --
5782   end if;
5783   --
5784 
5785 
5786   if l_cm_display_flag = 'Y' then
5787      l_output_name := null ;
5788      l_drctry_name := null ;
5789   end if ;
5790 
5791 
5792   if p_benefit_action_id is not null and p_ext_rslt_id is not null then
5793 
5794      l_ext_rslt_id := p_ext_rslt_id ;
5795      open c_rslt ;
5796      fetch c_rslt into  l_xrs_object_version_number ;
5797      close c_rslt ;
5798 
5799 
5800      if l_xrs_object_version_number is  null then
5801          ben_ext_rslt_api.create_ext_rslt
5802            (p_validate                => FALSE
5803           ,p_ext_rslt_id             => l_ext_rslt_id
5804           ,p_run_strt_dt             => sysdate
5805           ,p_run_end_dt              => null
5806           ,p_ext_stat_cd             => 'X'
5807           ,p_eff_dt                  => l_effective_date
5808           ,p_ext_strt_dt             => l_ext_strt_dt
5809           ,p_ext_end_dt              => l_ext_end_dt
5810           ,p_output_name             => l_output_name
5811          ,p_drctry_name             => l_drctry_name
5812          ,p_ext_dfn_id              => p_ext_dfn_id
5813          ,p_business_group_id       => p_business_group_id
5814          ,p_program_application_id  => fnd_global.prog_appl_id
5815          ,p_program_id              => fnd_global.conc_program_id
5816          ,p_program_update_date     => sysdate
5817          ,p_request_id              => l_conc_request_id
5818          ,p_output_type             => l_output_type
5819          ,p_xdo_template_id         => l_xdo_template_id
5820          ,p_object_version_number   => l_xrs_object_version_number
5821          ,p_effective_date          => l_effective_date);
5822 
5823         commit;
5824      end if ;
5825   else
5826      ben_ext_rslt_api.create_ext_rslt
5827        (p_validate                => FALSE
5828        ,p_ext_rslt_id             => l_ext_rslt_id
5829        ,p_run_strt_dt             => sysdate
5830        ,p_run_end_dt              => null
5831        ,p_ext_stat_cd             => 'X'
5832        ,p_eff_dt                  => l_effective_date
5833        ,p_ext_strt_dt             => l_ext_strt_dt
5834        ,p_ext_end_dt              => l_ext_end_dt
5835        ,p_output_name             => l_output_name
5836        ,p_drctry_name             => l_drctry_name
5837        ,p_ext_dfn_id              => p_ext_dfn_id
5838        ,p_business_group_id       => p_business_group_id
5839        ,p_program_application_id  => fnd_global.prog_appl_id
5840        ,p_program_id              => fnd_global.conc_program_id
5841        ,p_program_update_date     => sysdate
5842        ,p_request_id              => l_conc_request_id
5843        ,p_output_type             => l_output_type
5844        ,p_xdo_template_id         => l_xdo_template_id
5845        ,p_object_version_number   => l_xrs_object_version_number
5846        ,p_effective_date          => l_effective_date);
5847      --
5848      commit;
5849      --
5850  end if ;
5851 
5852   if g_debug then
5853     hr_utility.set_location(' resilt_id ' || l_ext_rslt_id,177 ) ;
5854   end if;
5855   benutils.get_parameter
5856     (p_business_group_id => p_business_group_id
5857     ,p_batch_exe_cd => 'BENXTRCT'
5858     ,p_threads => l_threads
5859     ,p_chunk_size => l_chunk_size
5860     ,p_max_errors => l_max_errors_allowed);
5861   --
5862   l_line := 5;
5863   --
5864   if p_benefit_action_id is null then
5865 
5866 
5867 
5868     --
5869     ben_benefit_actions_api.create_perf_benefit_actions
5870       (p_validate               => FALSE
5871       ,p_benefit_action_id      => l_benefit_action_id
5872       ,p_process_date           => l_effective_date
5873       ,p_uneai_effective_date   => NULL
5874       ,p_mode_cd                => 'S'
5875       ,p_derivable_factors_flag => 'N'
5876       ,p_close_uneai_flag       => 'N'
5877       ,p_validate_flag          => 'N'
5878       ,p_person_id              => NULL
5879       ,p_person_type_id         => NULL
5880       ,p_pgm_id                 => p_ext_dfn_id
5881       ,p_business_group_id      => p_business_group_id
5882       ,p_pl_id                  => l_ext_rslt_id
5883       ,p_popl_enrt_typ_cycl_id  => NULL
5884       ,p_no_programs_flag       => 'N'
5885       ,p_no_plans_flag          => 'N'
5886       ,p_comp_selection_rl      => NULL
5887       ,p_person_selection_rl    => p_person_selection_rl
5888       ,p_ler_id                 => NULL
5889       ,p_organization_id        => NULL
5890       ,p_benfts_grp_id          => NULL
5891       ,p_location_id            => NULL
5892       ,p_pstl_zip_rng_id        => NULL
5893       ,p_rptg_grp_id            => NULL
5894       ,p_pl_typ_id              => NULL
5895       ,p_opt_id                 => NULL
5896       ,p_eligy_prfl_id          => NULL
5897       ,p_vrbl_rt_prfl_id        => NULL
5898       ,p_legal_entity_id        => NULL
5899       ,p_payroll_id             => NULL
5900       ,p_request_id             => nvl(l_conc_request_id, fnd_global.conc_request_id )
5901       ,p_inelg_action_cd        => 'X' --Unique for extract benefit action recs
5902       ,p_debug_messages_flag    => 'N'
5903       ,p_object_version_number  => l_object_version_number
5904       ,p_effective_date         => l_effective_date);
5905     --
5906     commit;
5907     --
5908     l_line := 6;
5909     --
5910     l_line := 7;
5911     --
5912     benutils.g_benefit_action_id := l_benefit_action_id;
5913     benutils.g_thread_id         := 99;
5914     --
5915     l_line := 8;
5916 
5917     -- process the subheader
5918 
5919     process_subheader(p_ext_file_id        => l_ext_file_id
5920                       ,p_ext_dfn_id         => p_ext_dfn_id
5921                       ,p_ext_rslt_id        => l_ext_rslt_id
5922                       ,p_data_typ_cd        => l_data_typ_cd
5923                       ,p_ext_typ_cd         => l_ext_typ_cd
5924                       ,p_effective_date     => l_effective_date
5925                       ,p_ext_crit_prfl_id   => l_ext_crit_prfl_id
5926                       ,p_ext_global_flag    => nvl(l_ext_global_flag, 'N')
5927                       ,p_business_group_id  => p_business_group_id
5928                       ,p_subhdr_ghr_from_dt => l_eff_start_date
5929                       ,p_subhdr_ghr_to_dt   => l_eff_end_date
5930                       ) ;
5931 
5932 
5933     --
5934     l_person_cnt := 0;
5935 
5936     --
5937     --- dont build the person information when there is no detail defintion
5938     --- after intoridction of subheader , there could be setup with subheader alone
5939     if l_D_lvl_found = 'Y' then
5940        build_select_statement
5941          (p_data_typ_cd          => l_data_typ_cd,
5942           p_ext_crit_prfl_id     => l_ext_crit_prfl_id,
5943           p_ext_dfn_id           => p_ext_dfn_id,
5944           p_business_group_id    => p_business_group_id,
5945           p_effective_date       => l_effective_date,
5946           p_ext_rslt_id          => l_ext_rslt_id ,
5947           p_ext_global_flag      => nvl(l_ext_global_flag, 'N') ,
5948           p_eff_start_date       => l_eff_start_date,
5949           p_eff_end_date         => l_eff_end_date,
5950           p_act_start_date       => l_act_start_date,
5951           p_act_end_date         => l_act_end_date,
5952           p_select_statement     => l_select_statement, --out
5953           p_penserv_date         => p_penserv_date);
5954        --
5955 
5956        begin
5957          --
5958          open PersonCur for l_select_statement;
5959 
5960          --
5961        exception
5962          --
5963          when others then
5964            --
5965            fnd_file.put_line(fnd_file.log,'Error executing this dynamically build SQL Statement: ');
5966            -- Update by RChase, 01-MAR-2000
5967            -- fnd_file.put_line(fnd_file.log, '  ' ||l_select_statement);
5968            FOR i in 1..LENGTH(l_select_statement) LOOP
5969              --
5970              IF mod(i,80)=0 OR i=LENGTH(l_select_statement) THEN
5971                --
5972                fnd_file.put_line(fnd_file.log,'  ' ||substr(l_select_statement,l_current_loc+1,i-l_current_loc));
5973                l_current_loc:=i;
5974                --
5975               END IF;
5976               --
5977             END LOOP;
5978             --
5979             raise;
5980             --
5981        end;
5982        --
5983        l_line := 10;
5984        --
5985        loop
5986          --
5987          fetch personcur into l_rec;
5988          exit when personcur%notfound;
5989          --
5990 
5991 	  l_skip := FALSE;
5992         --
5993 	         /* Bug 13509481 */
5994 		 if p_person_selection_rl is not null then
5995 		  --
5996 		    hr_utility.set_location ('Calling Ben_batch_utils.person_selection_rule...',100);
5997 		    hr_utility.set_location ('l_rec.person_id '||l_rec.person_id,100);
5998 			  l_rule_value :=
5999 			     ben_batch_utils.person_selection_rule
6000 			       (p_person_id               => l_rec.person_id
6001 			       ,p_business_group_id       => p_business_group_id
6002 			       ,p_person_selection_rule_id => p_person_selection_rl
6003 			       ,p_effective_date          => l_effective_date);
6004 		   hr_utility.set_location ('l_rule_value '||l_rule_value,100);
6005 			  --
6006 			  if l_rule_value = 'N' then
6007 			    l_skip := TRUE;
6008 			  elsif l_rule_value = 'Y' then
6009 			      l_skip := FALSE;
6010 			  else
6011 			     l_skip := TRUE;
6012 			  end if;
6013 		  --
6014 		  end if;
6015 
6016              if not l_skip then
6017 		 if check_asg_security (p_person_id      => l_rec.person_id
6018 				       ,p_effective_date => l_effective_date
6019 				       ,p_business_group_id => p_business_group_id ) then
6020 
6021 		     l_person_cnt := l_person_cnt + 1;
6022 		     l_num_rows := l_num_rows + 1;
6023 		     --
6024 		     l_person_action_id.extend(1);
6025 		    --
6026 		     select ben_person_actions_s.nextval
6027 		     into   l_person_action_id(l_num_rows)
6028 		     from   sys.dual;
6029 		     --
6030 		     l_person_id.extend(1);
6031 		     l_person_id(l_num_rows) := l_rec.person_id;
6032 		     --
6033 		     If l_num_rows = l_chunk_size then
6034 		       --
6035 		       forall l_count in 1..l_num_rows
6036 			 --
6037 			 insert into ben_person_actions
6038 			     (person_action_id,
6039 			      person_id,
6040 			      ler_id,
6041 			      benefit_action_id,
6042 			      action_status_cd,
6043 			      object_version_number)
6044 			   values
6045 			     (l_person_action_id(l_count),
6046 			      l_person_id(l_count),
6047 			      null,
6048 			      l_benefit_action_id,
6049 			      'U',
6050 			      1);
6051 		       --
6052 		       select ben_batch_ranges_s.nextval
6053 		       into   l_range_id
6054 		       from   sys.dual;
6055 		       --
6056 		       l_start_person_action_id := l_person_action_id(1);
6057 		       l_end_person_action_id := l_person_action_id(l_num_rows);
6058 		       --
6059 		       insert into ben_batch_ranges
6060 			 (range_id,
6061 			  benefit_action_id,
6062 			  range_status_cd,
6063 			  starting_person_action_id,
6064 			  ending_person_action_id,
6065 			  object_version_number)
6066 		       values
6067 			 (l_range_id,
6068 			  l_benefit_action_id,
6069 			  'U',
6070 			  l_start_person_action_id,
6071 			  l_end_person_action_id,
6072 			  1);
6073 		       --
6074 		       -- Dispose of varray
6075 		       --
6076 		       l_person_action_id.delete;
6077 		       l_person_id.delete;
6078 		       --
6079 		       l_num_rows := 0;
6080 		       l_num_range := l_num_range + 1;
6081 		       --
6082 		     End if;
6083 		     --
6084 		  End if ;
6085 	  end if;
6086        End loop;
6087        close personcur ;
6088 
6089        -- Create a range of data to be multithreaded for the rest of
6090        -- person_action_id which are left over.
6091        --
6092        l_line := 12;
6093        --
6094        If l_num_rows > 0 then
6095          --
6096          forall l_count in 1..l_num_rows
6097            --
6098            -- Bulk bind in person actions
6099            --
6100            insert into ben_person_actions
6101              (person_action_id,
6102               person_id,
6103               ler_id,
6104               benefit_action_id,
6105               action_status_cd,
6106               object_version_number)
6107            values
6108              (l_person_action_id(l_count),
6109               l_person_id(l_count),
6110               null,
6111               l_benefit_action_id,
6112               'U',
6113               1);
6114          --
6115          l_num_range := l_num_range + 1;
6116          --
6117          -- Get next sequence for the range
6118          --
6119          select ben_batch_ranges_s.nextval
6120          into   l_range_id
6121          from   sys.dual;
6122          --
6123          l_start_person_action_id := l_person_action_id(1);
6124          l_end_person_action_id := l_person_action_id(l_num_rows);
6125          --
6126          insert into ben_batch_ranges
6127            (range_id,
6128             benefit_action_id,
6129             range_status_cd,
6130             starting_person_action_id,
6131             ending_person_action_id,
6132             object_version_number)
6133          values
6134               (l_range_id,
6135             l_benefit_action_id,
6136             'U',
6137             l_start_person_action_id,
6138             l_end_person_action_id,
6139             1);
6140          --
6141          l_num_rows := 0;
6142          --
6143          -- Dispose of data in varrays
6144          --
6145          l_person_action_id.delete;
6146          l_person_id.delete;
6147          --
6148        End if;
6149      end if ; -- only when the detail level found
6150        --
6151    Else -- benefit_action_id is null
6152       select count(*)
6153       into   l_person_cnt
6154       from ben_person_actions
6155       where  benefit_action_id = p_benefit_action_id
6156       and    ACTION_STATUS_CD='U';
6157 --vkodedal 30-Mar-2009    Bug#8335771 -Restart process not spawning threads
6158     select count(*)
6159       into   l_num_range
6160       from ben_batch_ranges
6161       where  benefit_action_id = p_benefit_action_id
6162       and    range_status_cd='U';
6163       ---
6164       l_benefit_action_id := p_benefit_action_id ;
6165      ---
6166    End if; -- End of if benefit_action_id is null;
6167    --
6168    -- Now to multithread the code.
6169    -- We can't multithread unless we are commiting data as we need to
6170    -- reset the status of the processed ranges and force a commit.
6171    --
6172    l_line := 15;
6173    --
6174    commit;
6175    --
6176    fnd_file.put_line(fnd_file.log, 'Total People Scanned:  ' || l_person_cnt);
6177    fnd_file.put_line(fnd_file.log, 'l_num_range:  '          || l_num_range);
6178    fnd_file.put_line(fnd_file.log, 'l_threads:  '            || l_threads);
6179    --
6180    l_count := 0;
6181    --
6182    If l_person_cnt > 0 and l_conc_request_id <> -1 then
6183      --
6184      For l_count in 1..least(l_threads,l_num_range) - 1 loop
6185        --
6186        l_request_id := fnd_request.submit_request
6187        (application => 'BEN'
6188        ,program     => 'BENXTHRD'
6189        ,description => 'Thread '||to_char(l_count+1)
6190        ,sub_request => FALSE
6191        ,argument1   => l_benefit_action_id
6192        ,argument2   => p_ext_dfn_id
6193        ,argument3   => l_count+1 -- so that we start on 2, because master is 1.
6194        ,argument4   => p_effective_date
6195        ,argument5   => p_business_group_id
6196        ,argument6   => l_data_typ_cd
6197        ,argument7   => l_ext_typ_cd
6198        ,argument8   => l_ext_crit_prfl_id
6199        ,argument9   => l_ext_rslt_id
6200        ,argument10  => l_ext_file_id
6201        ,argument11  => to_char(l_ext_strt_dt, 'YYYY/MM/DD HH24:MI:SS')
6202        ,argument12  => to_char(l_ext_end_dt, 'YYYY/MM/DD HH24:MI:SS')
6203        ,argument13  => l_prmy_sort_cd
6204        ,argument14  => l_scnd_sort_cd
6205        ,argument15  => l_output_name
6206        ,argument16  => l_apnd_rqst_id_flag
6207        ,argument17  => l_conc_request_id
6208        ,argument18  => l_use_eff_dt_for_chgs_flag
6209        ,argument19  => 'N'
6210        ,argument20  => p_eff_start_date
6211        ,argument21  => p_eff_end_date
6212        ,argument22  => p_act_start_date
6213        ,argument23  => p_act_end_date
6214        ,argument24  => p_penserv_mode       --------vkodedal changes for penserver
6215      );
6216        --
6217        g_num_processes := g_num_processes + 1;
6218        g_processes_rec(g_num_processes) := l_request_id;
6219        --
6220      End loop;
6221      --
6222    End if;
6223    --
6224    l_line := 16;
6225    --
6226    do_multithread
6227      (errbuf                     => errbuf
6228      ,retcode                    => retcode
6229      ,p_benefit_action_id        => l_benefit_action_id
6230      ,p_ext_dfn_id               => p_ext_dfn_id
6231      ,p_thread_id                => 1
6232      ,p_effective_date           => p_effective_date
6233      ,p_business_group_id        => p_business_group_id
6234      ,p_data_typ_cd              => l_data_typ_cd
6235      ,p_ext_typ_cd               => l_ext_typ_cd
6236      ,p_ext_crit_prfl_id         => l_ext_crit_prfl_id
6237      ,p_ext_rslt_id              => l_ext_rslt_id
6238      ,p_ext_file_id              => l_ext_file_id
6239      ,p_ext_strt_dt              => to_char(l_ext_strt_dt, 'YYYY/MM/DD HH24:MI:SS')
6240      ,p_ext_end_dt               => to_char(l_ext_end_dt, 'YYYY/MM/DD HH24:MI:SS')
6241      ,p_prmy_sort_cd             => l_prmy_sort_cd
6242      ,p_scnd_sort_cd             => l_scnd_sort_cd
6243      ,p_output_name              => l_output_name
6244      ,p_apnd_rqst_id_flag        => l_apnd_rqst_id_flag
6245      ,p_request_id               => l_conc_request_id
6246      ,p_use_eff_dt_for_chgs_flag => l_use_eff_dt_for_chgs_flag
6247      ,p_master_process_flag      => 'Y'
6248      ,p_eff_start_date           => p_eff_start_date
6249      ,p_eff_end_date             => p_eff_end_date
6250      ,p_act_start_date           => p_act_start_date
6251      ,p_act_end_date             => p_act_end_date
6252      ,p_penserv_mode             => p_penserv_mode
6253      );
6254    --
6255    l_line := 116;
6256    --
6257    check_all_threads_finished
6258      (p_effective_date      => l_effective_date
6259      ,p_business_group_id   => p_business_group_id
6260      ,p_data_typ_cd         => l_data_typ_cd
6261      ,p_ext_typ_cd          => l_ext_typ_cd
6262      ,p_ext_crit_prfl_id    => l_ext_crit_prfl_id
6263      ,p_ext_rslt_id         => l_ext_rslt_id
6264      ,p_request_id          => l_conc_request_id
6265      ,p_ext_file_id         => l_ext_file_id
6266      ,p_ext_strt_dt         => l_ext_strt_dt
6267      ,p_ext_end_dt          => l_ext_end_dt
6268      ,p_master_process_flag => 'Y');
6269    --
6270 
6271   --  Process sub trailer
6272   --
6273   process_subtrailer
6274     (p_ext_rslt_id       => l_ext_rslt_id,
6275      p_ext_file_id       => l_ext_file_id,
6276      p_ext_typ_cd        => l_ext_typ_cd,
6277      p_rcd_typ_cd        => 'L',
6278      p_business_group_id => p_business_group_id,
6279      p_effective_date    => l_effective_date,
6280      p_request_id        => l_conc_request_id,
6281      p_ext_group_elmt1   =>  ben_ext_thread.g_ext_group_elmt1,
6282      p_ext_group_elmt2   =>  ben_ext_thread.g_ext_group_elmt2,
6283      p_ext_crit_prfl_id  => l_ext_crit_prfl_id);
6284   --
6285   --  Process Footer Records
6286 
6287 
6288 
6289   --  Process Header Records
6290   --
6291   process_ext_ht_recs
6292     (p_ext_rslt_id       => l_ext_rslt_id,
6293      p_ext_file_id       => l_ext_file_id,
6294      p_ext_typ_cd        => l_ext_typ_cd,
6295      p_rcd_typ_cd        => 'H',
6296      p_business_group_id => p_business_group_id,
6297      p_effective_date    => l_effective_date,
6298      p_request_id        => l_conc_request_id,
6299      p_ext_crit_prfl_id  => l_ext_crit_prfl_id);
6300   --
6301   --  Process Footer Records
6302   --
6303   process_ext_ht_recs
6304     (p_ext_rslt_id       => l_ext_rslt_id,
6305      p_ext_file_id       => l_ext_file_id,
6306      p_ext_typ_cd        => l_ext_typ_cd,
6307      p_rcd_typ_cd        => 'T',
6308      p_business_group_id => p_business_group_id,
6309      p_effective_date    => l_effective_date,
6310      p_request_id        => l_conc_request_id,
6311      p_ext_crit_prfl_id  => l_ext_crit_prfl_id );
6312   --
6313   -- Call Extract Post Processing Rule.  This rule is a
6314   -- catch all, that can call a function to do additional
6315   -- iserting or deleting of records, changing sort order,
6316   -- updating fields, or anything.  Since this rule is used
6317   -- with formula function, it returns nothing.  l_output
6318   -- will not be used here.
6319   --
6320   if l_ext_post_prcs_rl is not null then
6321     --
6322     l_outputs := benutils.formula
6323       (p_formula_id        =>  l_ext_post_prcs_rl,
6324        p_effective_date    => l_effective_date,
6325        p_business_group_id => p_business_group_id,
6326        p_param1            => 'EXT_RSLT_ID',
6327        p_param1_value      => to_char(l_ext_rslt_id)
6328        --RChase pass extract definition id as input value
6329       ,p_param2             => 'EXT_DFN_ID'
6330       ,p_param2_value       => to_char(nvl(ben_ext_thread.g_ext_dfn_id, -1)));
6331     --
6332   end if;
6333   --
6334   commit;  -- anything that the formula did.
6335   --
6336   if g_err_cnt > 0 then
6337     --
6338     -- Call update API to update Extract Run Rslts row here
6339     -- Extract status - Completed with Errors
6340     --
6341     l_line := 126;
6342     l_ext_stat_cd  := 'E'  ;
6343     --
6344     if l_ext_rslt_id is not null then
6345       --
6346        open c_Ext_err_only( l_ext_rslt_id) ;
6347        fetch c_Ext_err_only into l_dummy ;
6348        if c_Ext_err_only%notfound then
6349           l_ext_stat_cd  := 'W'  ;
6350        end if ;
6351        close c_Ext_err_only ;
6352 
6353 
6354       ben_ext_rslt_api.update_ext_rslt
6355         (p_validate                       => false
6356         ,p_ext_rslt_id                    => l_ext_rslt_id
6357         ,p_run_end_dt                     => sysdate
6358         ,p_ext_stat_cd                    => l_ext_stat_cd
6359         ,p_tot_rec_num                    => g_rec_cnt
6360         ,p_tot_per_num                    => g_per_cnt
6361         ,p_tot_err_num                    => g_err_cnt
6362         ,p_program_application_id         => fnd_global.prog_appl_id
6363         ,p_program_id                     => fnd_global.conc_program_id
6364         ,p_program_update_date            => sysdate
6365         ,p_request_id                     => l_conc_request_id
6366         ,p_object_version_number          => l_xrs_object_version_number
6367         ,p_effective_date                 => l_effective_date);
6368       --
6369     end if;
6370     --
6371   else
6372     --
6373     if l_ext_rslt_id is not null then
6374       --
6375       ben_ext_rslt_api.update_ext_rslt
6376         (p_validate                       => false
6377         ,p_ext_rslt_id                    => l_ext_rslt_id
6378         ,p_run_end_dt                     => sysdate
6379         ,p_ext_stat_cd                    => 'S'
6380         ,p_tot_rec_num                    => g_rec_cnt
6381         ,p_tot_per_num                    => g_per_cnt
6382         ,p_tot_err_num                    => g_err_cnt
6383         ,p_program_application_id         => fnd_global.prog_appl_id
6384         ,p_program_id                     => fnd_global.conc_program_id
6385         ,p_program_update_date            => sysdate
6386         ,p_request_id                     => l_conc_request_id
6387         ,p_object_version_number          => l_xrs_object_version_number
6388         ,p_effective_date                 => l_effective_date);
6389       --
6390     end if;
6391     --
6392   end if;
6393   --
6394   -- Bug fix 1801219
6395   update_ht_strt_end_dt(l_ext_rslt_id);
6396   -- End fix 1801219
6397   commit;
6398   --
6399   if l_conc_request_id <> -1 then
6400     --
6401     fnd_message.set_name('BEN', 'BEN_92185_THREAD_SUCCESS');
6402     fnd_file.put_line(fnd_file.log, fnd_message.get);
6403     fnd_file.put_line(fnd_file.log, ' ');
6404     --
6405     l_line := 127;
6406     --
6407     thread_summary;
6408     --
6409     l_request_id := fnd_request.submit_request
6410                     (application => 'BEN',
6411                      program     => 'BENXERRO',
6412                      description => NULL,
6413                      sub_request => FALSE,
6414                      argument1   => l_conc_request_id
6415                      );
6416     --
6417     if l_ext_rslt_id is not null then
6418       --
6419       l_request_id := fnd_request.submit_request
6420                     (application => 'BEN',
6421                      program     => 'BENXSMRY',
6422                      description => NULL,
6423                      sub_request => FALSE,
6424                      argument1   => l_ext_rslt_id);
6425       --
6426     end if;
6427     --
6428     if l_kickoff_wrt_prc_flag = 'Y' then
6429        --
6430        if l_ext_rslt_id is not null then
6431           -- when the output is XDO type and display on call different process
6432           if l_cm_display_flag = 'Y' and l_xdo_template_id is not null then
6433 
6434              open  c_xdoi (l_xdo_template_id ) ;
6435              fetch c_xdoi  into
6436                  l_application_short_name ,
6437                  l_template_code ,
6438                  l_default_language,
6439                  l_default_territory ;
6440              close  c_xdoi ;
6441 
6442              if l_output_type = 'H'  then
6443                 l_output_code := 'HTML' ;
6444              elsif  l_output_type = 'R'  then
6445                 l_output_code := 'RTF' ;
6446              elsif  l_output_type = 'P'  then
6447                 l_output_code := 'PDF' ;
6448              elsif  l_output_type = 'E'  then
6449                 l_output_code := 'EXCEL' ;
6450              else
6451                 l_output_code := 'PDF' ;
6452              end if ;
6453 
6454              --- popilate the variable for post poroccing of cm - templates
6455 
6456              l_fnd_out := fnd_request.add_layout
6457                             (template_appl_name => l_application_short_name,
6458                              template_code      => l_template_code,
6459                              template_language  => l_default_language,
6460                              template_territory => l_default_territory,
6461                              output_format      => l_output_code
6462                             ) ;
6463 
6464              --- call the concurrent manager with  XML output
6465 
6466 
6467              l_request_id := fnd_request.submit_request
6468                     (application => 'BEN',
6469                      program     => 'BENXMLWRIT',
6470                      description => NULL,
6471                      sub_request => FALSE,
6472                      argument1   => l_ext_rslt_id,
6473                      argument2   => l_output_type ,
6474 		     argument3   => p_out_dummy ,
6475                      argument4   => null,
6476                      argument5   => 'BENXMLWRIT'
6477                    );
6478 
6479           Else
6480 
6481              l_request_id := fnd_request.submit_request
6482                     (application => 'BEN',
6483                      program     => 'BENXWRIT',
6484                      description => NULL,
6485                      sub_request => FALSE,
6486                      argument1   => l_ext_rslt_id,
6487                      argument2   => l_output_type ,
6488 		     argument3   => p_out_dummy ,
6489                      argument4   => l_xdo_template_id,
6490                      argument5   => 'BENXWRIT'
6491                    );
6492           end if ;
6493         --
6494       end if;
6495       --
6496     end if;
6497     --
6498   end if;
6499   --
6500   if g_debug then
6501     hr_utility.set_location ('Exiting '||l_proc,15);
6502   end if;
6503   --
6504 exception
6505   --
6506   when g_job_failure_error then
6507     --
6508     if l_conc_request_id <> -1 then
6509       --
6510       check_all_threads_finished
6511         (p_effective_date      => l_effective_date
6512         ,p_business_group_id   => p_business_group_id
6513         ,p_data_typ_cd         => l_data_typ_cd
6514         ,p_ext_typ_cd          => l_ext_typ_cd
6515         ,p_ext_crit_prfl_id    => l_ext_crit_prfl_id
6516         ,p_ext_rslt_id         => l_ext_rslt_id
6517         ,p_request_id          => l_conc_request_id
6518         ,p_ext_file_id         => l_ext_file_id
6519         ,p_ext_strt_dt         => l_ext_strt_dt
6520         ,p_ext_end_dt          => l_ext_end_dt
6521         ,p_master_process_flag => 'Y');
6522       --
6523     end if;
6524     --
6525     if l_ext_rslt_id is not null then
6526       --
6527       ben_ext_rslt_api.update_ext_rslt
6528         (p_validate                => false
6529         ,p_ext_rslt_id             => l_ext_rslt_id
6530         ,p_run_end_dt              => sysdate
6531         ,p_ext_stat_cd             => 'F'
6532         ,p_tot_rec_num             => g_rec_cnt
6533         ,p_tot_per_num             => g_per_cnt
6534         ,p_tot_err_num             => g_err_cnt
6535         ,p_program_application_id  => fnd_global.prog_appl_id
6536         ,p_program_id              => fnd_global.conc_program_id
6537         ,p_program_update_date     => sysdate
6538         ,p_request_id              => l_conc_request_id
6539         ,p_object_version_number   => l_xrs_object_version_number
6540         ,p_effective_date          => l_effective_date);
6541       --
6542       commit;
6543       --
6544     end if;
6545     --
6546     if l_conc_request_id <> -1 then
6547       --
6548       l_request_id := fnd_request.submit_request
6549                     (application => 'BEN',
6550                      program     => 'BENXERRO',
6551                      description => NULL,
6552                      sub_request => FALSE,
6553                      argument1   => l_conc_request_id);
6554       --
6555       thread_summary;
6556       fnd_message.set_name('BEN',g_err_name);
6557       fnd_file.put_line(fnd_file.log, fnd_message.get);
6558       fnd_message.raise_error;
6559       --
6560     end if;
6561     --
6562   when others then
6563     --
6564     -- Call update API to update Extract Run Rslts row here
6565     -- Extract status - Job Failure
6566     --
6567     --
6568     if l_conc_request_id <> -1 then
6569       --
6570       check_all_threads_finished
6571         (p_effective_date      => l_effective_date
6572         ,p_business_group_id   => p_business_group_id
6573         ,p_data_typ_cd         => l_data_typ_cd
6574         ,p_ext_typ_cd          => l_ext_typ_cd
6575         ,p_ext_crit_prfl_id    => l_ext_crit_prfl_id
6576         ,p_ext_rslt_id         => l_ext_rslt_id
6577         ,p_request_id          => l_conc_request_id
6578         ,p_ext_file_id         => l_ext_file_id
6579         ,p_ext_strt_dt         => l_ext_strt_dt
6580         ,p_ext_end_dt          => l_ext_end_dt
6581         ,p_master_process_flag => 'Y');
6582       --
6583     end if;
6584     --
6585     if l_ext_rslt_id is not null then
6586       --
6587       ben_ext_rslt_api.update_ext_rslt
6588         (p_validate               => false
6589         ,p_ext_rslt_id            => l_ext_rslt_id
6590         ,p_run_end_dt             => sysdate
6591         ,p_ext_stat_cd            => 'F'
6592         ,p_tot_rec_num            => g_rec_cnt
6593         ,p_tot_per_num            => g_per_cnt
6594         ,p_tot_err_num            => g_err_cnt
6595         ,p_program_application_id => fnd_global.prog_appl_id
6596         ,p_program_id             => fnd_global.conc_program_id
6597         ,p_program_update_date    => sysdate
6598         ,p_request_id             => l_conc_request_id
6599         ,p_object_version_number  => l_xrs_object_version_number
6600         ,p_effective_date         => l_effective_date);
6601       --
6602       write_error(p_err_num           => null,
6603                   p_err_name          => substr(sqlerrm, 1, 200),
6604                   p_typ_cd            => 'F',
6605                   p_person_id         => null,
6606                   p_request_id        => l_conc_request_id,
6607                   p_ext_rslt_id       => l_ext_rslt_id,
6608                   p_business_group_id => p_business_group_id);
6609       commit;
6610       --
6611     end if;
6612     --
6613     if l_conc_request_id <> -1 then
6614       --
6615       l_request_id := fnd_request.submit_request
6616                     (application => 'BEN',
6617                      program     => 'BENXERRO',
6618                      description => NULL,
6619                      sub_request => FALSE,
6620                      argument1   => l_conc_request_id);
6621       --
6622       thread_summary;
6623       fnd_message.set_name('PER', 'FFU10_GENERAL_ORACLE_ERROR');
6624       fnd_message.set_token('2', substr(sqlerrm, 1, 200));
6625       fnd_file.put_line(fnd_file.log, fnd_message.get);
6626       fnd_message.raise_error;
6627       --
6628     end if;
6629     --
6630 End process;
6631 
6632 
6633 
6634 procedure  ext_restart_clear(
6635             p_benefit_action_id      in number
6636            ,p_ext_rslt_id            in number
6637            ,p_start_person_action_id in number
6638            ,p_end_person_action_id   in number
6639            ,p_effective_date         in date
6640           ) is
6641 
6642   l_package        varchar2(80);
6643 
6644   cursor per_cursor is
6645    select person_id
6646    from   ben_person_actions act
6647    where act.person_action_id between p_start_person_action_id
6648                               and     p_end_person_action_id
6649      and act.benefit_action_id = p_benefit_action_id
6650      and action_status_cd <> 'P'  ;
6651 
6652    cursor c_xrdt (c_person_id number) is
6653    select ext_rslt_dtl_id,
6654           object_version_number
6655    from ben_ext_rslt_dtl
6656    where  person_id = c_person_id
6657      and  ext_rslt_id = p_ext_rslt_id ;
6658 
6659 
6660    cursor c_xrer (c_person_id number) is
6661    select ext_rslt_err_id,
6662           object_version_number
6663    from ben_ext_rslt_err
6664    where  person_id = c_person_id
6665      and  ext_rslt_id = p_ext_rslt_id ;
6666 
6667    ---
6668 
6669    l_object_version_number number ;
6670 
6671 begin
6672    if g_debug then
6673      l_package := g_package||'.ext_restart_clear';
6674      hr_utility.set_location ('Entering '||l_package,10);
6675      hr_utility.set_location ('result_id  '||p_ext_rslt_id,10);
6676    end if;
6677 
6678    for pact in per_cursor
6679    loop
6680 
6681       if g_debug then
6682         hr_utility.set_location ('person '||pact.person_id,10);
6683       end if;
6684      ---delete extract results for the person proccessed
6685      ---  and in the error range
6686      for xrdt in c_xrdt(pact.person_id)
6687      loop
6688 
6689          if g_debug then
6690            hr_utility.set_location ('deleting '|| pact.person_id, 177);
6691          end if;
6692 
6693          l_object_version_number := xrdt.object_version_number ;
6694          ben_EXT_RSLT_DTL_api.delete_EXT_RSLT_DTL
6695                (p_ext_rslt_dtl_id       => xrdt.ext_rslt_dtl_id
6696                ,p_object_version_number => l_object_version_number
6697                );
6698      end loop ;
6699 
6700      ---delete all the error created for the persopns
6701      for xrer in c_xrer(pact.person_id)
6702      loop
6703 
6704          if g_debug then
6705            hr_utility.set_location ('deleting error'||pact.person_id, 177);
6706          end if;
6707 
6708          l_object_version_number := xrer.object_version_number ;
6709          ben_EXT_RSLT_ERR_api.delete_EXT_RSLT_ERR
6710                (p_ext_rslt_err_id       => xrer.ext_rslt_err_id
6711                ,p_object_version_number => l_object_version_number
6712                ,p_effective_date        => p_effective_date
6713                );
6714      end loop ;
6715 
6716    end loop ;
6717    if g_debug then
6718      hr_utility.set_location ('Leaving '||l_package,10);
6719    end if;
6720 end  ext_restart_clear ;
6721 --
6722 --old proc. Not used
6723 --
6724 /*procedure restart(errbuf                    out nocopy    varchar2
6725                  ,retcode                   out nocopy    varchar2
6726                  ,p_benefit_action_id       in     number
6727                  ,p_ext_rslt_id             in     number  default null) is
6728 --
6729 l_package        varchar2(80);
6730 
6731 cursor c_parameters is
6732 select pgm_id
6733        ,pl_id
6734        ,business_group_id
6735        ,process_date
6736 from   ben_benefit_actions ben
6737 where  ben.benefit_action_id = p_benefit_action_id;
6738 
6739 
6740 cursor c_range_err is
6741   select *
6742   from ben_batch_ranges ran
6743   where ran.range_status_cd = 'E'
6744   and ran.BENEFIT_ACTION_ID  = P_BENEFIT_ACTION_ID;
6745 
6746 
6747 --
6748   l_parameters c_parameters%rowtype;
6749   l_errbuf       varchar2(80);
6750   l_retcode      number;
6751   l_ext_rslt_id number ;
6752 --
6753 begin
6754    g_debug := hr_utility.debug_enabled;
6755   if g_debug then
6756     l_package := g_package||'.restart';
6757     hr_utility.set_location ('Entering '||l_package,10);
6758   end if;
6759   --
6760   -- get the parameters for a previous run and do a restart
6761   --
6762   open c_parameters;
6763     --
6764     fetch c_parameters into l_parameters;
6765     if c_parameters%notfound then
6766       --
6767       fnd_message.set_name('BEN','BEN_91666_BENMNGLE_NO_RESTART');
6768       fnd_message.raise_error;
6769       --
6770     end if;
6771     --
6772   close c_parameters;
6773 
6774   l_ext_rslt_id :=  l_parameters.pl_id ;
6775   if l_ext_rslt_id is null  then
6776      l_ext_rslt_id := p_ext_rslt_id ;
6777      if l_ext_rslt_id is null  then
6778        --- create new error for this
6779        fnd_message.set_name('BEN','BEN_91666_RSLT_ID_NOT_FOUND');
6780        fnd_message.raise_error;
6781      end if ;
6782   end if ;
6783 
6784 
6785 
6786   --- Clearup all detail of the person processed
6787   --- and the action_status not updated
6788   --- changes the errored status of the ranges to
6789   --- unprocessed
6790   if l_ext_rslt_id is not null then
6791      for rng in  c_range_err
6792      Loop
6793         if g_debug then
6794           hr_utility.set_location ('range id  '||rng.starting_person_action_id,10);
6795         end if;
6796         ext_restart_clear(
6797             p_benefit_action_id => p_benefit_action_id
6798            ,p_ext_rslt_id       => l_ext_rslt_id
6799            ,p_start_person_action_id => rng.starting_person_action_id
6800            ,p_end_person_action_id   => rng.ending_person_action_id
6801            ,p_effective_date         => l_parameters.process_date
6802           ) ;
6803         ---- range_id is updated
6804         update ben_batch_ranges
6805           set range_Status_cd = 'U'
6806           where  range_id = rng.range_id ;
6807      end loop ;
6808   end if ;
6809 
6810 
6811 
6812   process(l_errbuf
6813           ,l_retcode
6814           ,p_benefit_action_id
6815           ,l_parameters.pgm_id -- cover for ext_id
6816           ,fnd_date.date_to_canonical(l_parameters.process_date)
6817           ,l_parameters.business_group_id
6818           ,l_ext_rslt_id);
6819 
6820   if g_debug then
6821     hr_utility.set_location ('Leaving '||l_package,10);
6822   end if;
6823 end; */
6824 --
6825 --new proc
6826 --
6827 -- =============================================================================
6828 -- ~ Chk_PQP_Extract:
6829 -- =============================================================================
6830 PROCEDURE Chk_PQP_Extract
6831          (p_ext_dfn_id        IN NUMBER
6832          ,p_ext_rslt_id       IN NUMBER
6833          ,p_business_group_id IN NUMBER
6834          ,p_conc_req_id       IN NUMBER
6835           ) AS
6836    -- Cursor to check if the extract is PQP extract.
6837    CURSOR check_pqp_extract (c_ext_dfn_id        IN NUMBER,
6838                              c_business_group_id IN NUMBER) IS
6839    SELECT  ea.ext_dfn_type
6840      FROM  ben_ext_dfn ed,
6841            per_business_groups bg,
6842            pqp_extract_attributes ea
6843     WHERE ((bg.business_group_id = ed.business_group_id)OR
6844           (bg.legislation_code  = ed.legislation_code) OR
6845           (ed.business_group_id IS NULL AND
6846            ed.legislation_code  IS NULL)
6847           )
6848       AND bg.business_group_id = c_business_group_id
6849       AND ed.ext_dfn_id        = ea.ext_dfn_id
6850       AND ed.ext_dfn_id        = c_ext_dfn_id
6851       AND ea.ext_dfn_type in
6852         ('PEN_FPR', 'PEN_CHG', 'FID_PTC','FID_CAC',
6853          'FID_ERC', 'FID_LPY', 'FID_ATE','FID_CHG',
6854          'GBL_FPR', 'GBL_CHG', 'GBL_MUL_CHG');
6855    --
6856    CURSOR csr_org_req (c_ext_dfn_id IN NUMBER
6857                       ,c_ext_rslt_id IN NUMBER
6858                       ,c_business_group_id IN NUMBER) IS
6859    SELECT bba.pgm_id
6860          ,bba.pl_id
6861          ,bba.benefit_action_id
6862          ,bba.business_group_id
6863          ,bba.process_date
6864          ,bba.request_id
6865      FROM ben_benefit_actions bba
6866     WHERE bba.pl_id  = c_ext_rslt_id
6867       AND bba.pgm_id = c_ext_dfn_id
6868       AND bba.business_group_id = c_business_group_id;
6869    l_org_req            csr_org_req%ROWTYPE;
6870 
6871    -- Cursor to get the extract parameters of the last req.
6872    CURSOR csr_req_params ( c_req_id IN NUMBER) IS
6873    SELECT *
6874      FROM fnd_concurrent_requests
6875     WHERE request_id = c_req_id;
6876 
6877    l_conc_params        csr_req_params%ROWTYPE;
6878    l_pqp_extract        BOOLEAN;
6879    l_ext_type           VARCHAR2(50);
6880    l_session_id         NUMBER;
6881    l_conc_request_id    NUMBER;
6882    l_proc_name          VARCHAR2(150);
6883 
6884 BEGIN
6885   IF g_debug THEN
6886      l_proc_name := g_package||'.Chk_PQP_Extract';
6887      Hr_Utility.set_location('Entering: '||l_proc_name, 5);
6888   END IF;
6889 
6890    OPEN check_pqp_extract (p_ext_dfn_id,
6891                           p_business_group_id);
6892   FETCH check_pqp_extract INTO l_ext_type;
6893   l_pqp_extract := check_pqp_extract%FOUND;
6894   CLOSE check_pqp_extract;
6895   --
6896   -- Return if not a PQP seeded/copied extract.
6897   --
6898   IF NOT l_pqp_extract THEN
6899     RETURN;
6900   END IF;
6901   --
6902   -- Delete the existing completed extracts for the extract def. within
6903   -- this business group.
6904   --
6905   DELETE FROM  pay_us_rpt_totals
6906   WHERE tax_unit_id        = p_conc_req_id
6907     AND attribute5         = 'EXTRACT_COMPLETED'
6908     AND business_group_id  = p_business_group_id
6909     AND organization_id    = p_business_group_id
6910     AND location_id        = p_ext_dfn_id;
6911 
6912   OPEN csr_org_req (c_ext_dfn_id        => p_ext_dfn_id
6913                    ,c_ext_rslt_id       => p_ext_rslt_id
6914                    ,c_business_group_id => p_business_group_id);
6915   FETCH csr_org_req INTO l_org_req;
6916   CLOSE csr_org_req;
6917   --
6918   -- Get the old paramters used by the extract, based on the concurrent
6919   -- request id passed.
6920   --
6921    OPEN csr_req_params(l_org_req.request_id);
6922   FETCH csr_req_params INTO l_conc_params;
6923   CLOSE csr_req_params;
6924   --
6925   -- Get session id and the concurrent req. id
6926   --
6927   l_session_id      :=  Userenv('SESSIONID');
6928   l_conc_request_id :=   Fnd_Global.conc_request_id;
6929   --
6930   IF l_ext_type IN
6931      ('PEN_FPR', 'PEN_CHG', 'FID_PTC','FID_CAC',
6932       'FID_ERC', 'FID_LPY', 'FID_ATE','FID_CHG') THEN
6933 
6934     INSERT INTO pay_us_rpt_totals
6935     (session_id         -- session id
6936     ,organization_name  -- Conc. Program Name
6937     ,business_group_id  -- business group id
6938     ,organization_id    -- -do-
6939     ,location_id        -- Ext Def Id
6940     ,tax_unit_id        -- concurrent request id
6941     ,value1             -- extract def. id
6942     ,value2             -- element set id
6943     ,value3             -- element type id
6944     ,value4             -- Payroll Id
6945     ,value5             -- GRE Org Id
6946     ,value6             -- Consolidation set id
6947     ,attribute1         -- Selection Criteria
6948     ,attribute2         -- Reporting dimension
6949     ,attribute3         -- Extract Start Date
6950     ,attribute4         -- Extract End Date
6951     ,attribute5         -- Status
6952     )
6953     VALUES
6954     (l_session_id
6955     ,'US Pension Extracts'
6956     ,p_business_group_id      -- p_business_group_id
6957     ,p_business_group_id      -- Org Id
6958     ,p_ext_dfn_id             -- location id for key
6959     ,l_conc_request_id        -- New Conc Req Id.
6960     ,l_conc_params.argument2  -- p_ext_dfn_id
6961     ,l_conc_params.argument9  -- p_element_set_id
6962     ,l_conc_params.argument12 -- p_element_type_id
6963     ,l_conc_params.argument17 -- p_payroll_id
6964     ,l_conc_params.argument16 -- p_gre_id
6965     ,l_conc_params.argument20 -- p_con_set
6966     ,l_conc_params.argument7  -- p_selection_criteria
6967     ,l_conc_params.argument5  -- p_reporting_dimension
6968     ,l_conc_params.argument14 -- p_start_date
6969     ,l_conc_params.argument15 -- p_end_date
6970     ,'EXTRACT_RUNNING'        -- Status
6971     );
6972     ELSIF l_ext_type IN
6973         ('GBL_FPR', 'GBL_CHG', 'GBL_MUL_CHG')   THEN
6974 
6975     INSERT INTO pay_us_rpt_totals
6976     (session_id         -- session id
6977     ,organization_name  -- Conc. Program Name
6978     ,business_group_id  -- business group id
6979     ,organization_id    -- -do-
6980     ,location_id        -- Ext Def Id
6981     ,tax_unit_id        -- concurrent request id
6982     ,value1             -- extract def. id
6983     ,value2             -- element set id
6984     ,value3             -- element type id
6985     ,value4             -- Payroll Id
6986     ,value5             -- GRE Org Id
6987     ,value6             -- Consolidation set id
6988     ,attribute1         -- Selection Criteria
6989     ,attribute2         -- Reporting dimension
6990     ,attribute3         -- Extract Start Date
6991     ,attribute4         -- Extract End Date
6992     ,attribute5         -- Organization Name
6993     ,attribute6         -- Person Type
6994     ,attribute7         -- Location
6995     )
6996     VALUES
6997     (l_session_id               -- session id
6998     ,'Global Pension Extracts'
6999     ,p_business_group_id        -- p_business_group_id
7000     ,p_business_group_id        -- Org Id
7001     ,p_ext_dfn_id               -- ext dfn id for key purpose
7002     ,l_conc_request_id          -- New Conc Req Id.
7003     ,l_conc_params.argument2    -- p_ext_dfn_id
7004     ,l_conc_params.argument8    -- p_element_set_id
7005     ,l_conc_params.argument10   -- p_element_type_id
7006     ,l_conc_params.argument15   -- p_payroll_id
7007     ,l_conc_params.argument14   -- p_gre_id
7008     ,l_conc_params.argument18   -- p_con_set
7009     ,l_conc_params.argument6    -- p_selection_criteria
7010     ,l_conc_params.argument4    -- p_reporting_dimension
7011     ,l_conc_params.argument12   -- p_start_date
7012     ,l_conc_params.argument13   -- p_end_date
7013     ,l_conc_params.argument20   -- p_org_id
7014     ,l_conc_params.argument21   -- p_person_type_id
7015     ,l_conc_params.argument22   -- p_location_id
7016     );
7017 
7018   END IF;
7019   IF g_debug THEN
7020      Hr_Utility.set_location('Leaving: '||l_proc_name, 80);
7021   END IF;
7022 END Chk_PQP_Extract;
7023 
7024 -- =============================================================================
7025 -- ~ Restart:
7026 -- =============================================================================
7027 procedure Restart(errbuf                    out nocopy  varchar2
7028                  ,retcode                   out nocopy  varchar2
7029                  ,p_ext_dfn_id              in  number
7030                  ,p_concurrent_request_id   in  number) is
7031 
7032 
7033   l_errbuf            varchar2(80);
7034   l_retcode           number;
7035   l_ext_rslt_id       number ;
7036   l_benefit_action_id number;
7037   l_business_group_id number;
7038   l_session_id        number;
7039   l_request_id        number;
7040   l_pqp_extract       boolean;
7041   l_dummy             varchar2(1);
7042   l_package           varchar2(80);
7043 
7044   cursor c_parameters is
7045   select pgm_id
7046         ,pl_id
7047         ,benefit_action_id
7048         ,business_group_id
7049         ,process_date
7050     from ben_benefit_actions ben
7051    where ben.request_id = p_concurrent_request_id;
7052   l_parameters        c_parameters%rowtype;
7053 
7054   cursor c_range_err is
7055    select *
7056      from ben_batch_ranges ran
7057     where ran.range_status_cd  in ( 'E', 'W')
7058       and ran.benefit_action_id  = l_benefit_action_id;
7059 
7060 
7061 BEGIN
7062 
7063   g_debug := hr_utility.debug_enabled;
7064   if g_debug then
7065     l_package := g_package||'.restart';
7066     hr_utility.set_location ('Entering '||l_package,10);
7067   end if;
7068   --
7069   -- get the parameters for a previous run and do a restart
7070   --
7071   open c_parameters;
7072     --
7073     fetch c_parameters into l_parameters;
7074     if c_parameters%notfound then
7075       --
7076       fnd_message.set_name('BEN','BEN_91666_BENMNGLE_NO_RESTART');
7077       fnd_message.raise_error;
7078       --
7079     end if;
7080     --
7081   close c_parameters;
7082 
7083   l_ext_rslt_id       :=  l_parameters.pl_id ;
7084   l_benefit_action_id := l_parameters.benefit_action_id;
7085   l_business_group_id := l_parameters.business_group_id;
7086 
7087   if l_ext_rslt_id is null  then
7088      -- create new error for this
7089      fnd_message.set_name('BEN','BEN_91666_RSLT_ID_NOT_FOUND');
7090      fnd_message.raise_error;
7091   end if ;
7092   --
7093   -- Check to see if the extract being re-started is a PQP US/Global seeded
7094   -- or copied extract, if yes then the following procedure insert the values
7095   -- of the extract parameters of the failed run into table pay_us_rpt_totals.
7096   --
7097   Chk_PQP_Extract
7098   (p_ext_dfn_id        => p_ext_dfn_id
7099   ,p_ext_rslt_id       => l_ext_rslt_id
7100   ,p_business_group_id => l_business_group_id
7101   ,p_conc_req_id       => p_concurrent_request_id
7102    );
7103   --
7104   -- Clearup all detail of the person processed and the action_status
7105   -- not updated changes the errored status of the ranges to unprocessed
7106   --
7107   if l_ext_rslt_id is not null then
7108      for rng in  c_range_err
7109      Loop
7110         if g_debug then
7111           hr_utility.set_location ('range id  '||rng.starting_person_action_id,10);
7112         end if;
7113         ext_restart_clear
7114         (p_benefit_action_id      => l_benefit_action_id
7115         ,p_ext_rslt_id            => l_ext_rslt_id
7116         ,p_start_person_action_id => rng.starting_person_action_id
7117         ,p_end_person_action_id   => rng.ending_person_action_id
7118         ,p_effective_date         => l_parameters.process_date
7119          ) ;
7120         -- range_id is updated
7121         update ben_batch_ranges
7122            set range_status_cd = 'U'
7123          where range_id        = rng.range_id;
7124      end loop;
7125   end if ;
7126   -- Re-process the failed records.
7127 
7128   process(errbuf               =>  l_errbuf
7129          ,retcode              =>  l_retcode
7130          ,p_benefit_action_id  =>  l_benefit_action_id
7131          ,p_ext_dfn_id         =>  l_parameters.pgm_id -- cover for ext_id
7132          ,p_effective_date     =>  fnd_date.date_to_canonical(l_parameters.process_date)
7133          ,p_business_group_id  =>  l_parameters.business_group_id
7134          ,p_ext_rslt_id        =>  l_ext_rslt_id);
7135 
7136   /*
7137   process(l_errbuf
7138          ,l_retcode
7139          ,l_benefit_action_id
7140          ,l_parameters.pgm_id -- cover for ext_id
7141          ,fnd_date.date_to_canonical(l_parameters.process_date)
7142          ,l_parameters.business_group_id
7143          ,l_ext_rslt_id);
7144 
7145   */
7146 
7147   if g_debug then
7148     hr_utility.set_location ('Leaving '||l_package,10);
7149   end if;
7150 
7151 end Restart;
7152 
7153 -- =============================================================================
7154 -- ~ Load_Extract:
7155 -- =============================================================================
7156 Procedure load_extract(
7157           errbuf                     out nocopy varchar2
7158          ,retcode                    out nocopy number
7159          ,p_mode                     in varchar2
7160          ,p_seeded                   in varchar2 default 'N'
7161          ,p_loader_file              in varchar2 default null
7162          ,p_file_name                in varchar2
7163          ,p_view_name                in varchar2 default null
7164          ,p_extract_file_id          in number   default null
7165          ,p_business_group_id        in number
7166          ,p_validate                 in  varchar2 default 'N'
7167          ,p_allow_override           in  varchar2 default 'N'
7168        ) is
7169 
7170 
7171    l_proc                       varchar2(72) := g_package||'Load_extract';
7172 
7173 
7174    --
7175    cursor c_threads(p_request_id number) is
7176    select null
7177    from   fnd_concurrent_requests fnd
7178    where  /*fnd.phase_code <> 'C' and */
7179          fnd.request_id = p_request_id;
7180    --
7181    cursor c_bg_name is
7182    select name
7183      from per_business_groups  bg
7184      where  business_group_id = p_business_group_id ;
7185 
7186    cursor c_ext is
7187    select name,business_group_id
7188      from ben_ext_file
7189     where ext_file_id  = p_extract_file_id ;
7190 
7191    l_business_goup_name    per_business_groups.name%Type  ;
7192    l_request_id            number ;
7193    l_business_group_name   varchar2(2000) ;
7194    l_business_group_id     number ;
7195    l_view_name             varchar2(2000) ;
7196    l_loader_file           varchar2(2000) := p_loader_file ;
7197    l_ext_file_name         varchar2(2000) ;
7198    l_seeded                varchar2(2000) := p_seeded     ;
7199    l_dummy                 varchar2(1) ;
7200    l_validate              varchar2(2000) ;
7201    l_override              varchar2(2000) ;
7202 begin
7203   --
7204   fnd_msg_pub.initialize;
7205   --
7206   hr_utility.set_location('Entering:'|| l_proc, 10);
7207        hr_utility.set_location('p_mode    '||  p_mode ,10) ;
7208        hr_utility.set_location('p_seeded    '||  p_seeded ,10) ;
7209        hr_utility.set_location('p_loader_file '|| p_loader_file,10) ;
7210        hr_utility.set_location('p_file_name  '||  p_file_name ,10) ;
7211        hr_utility.set_location('p_view_name '||   p_view_name,10) ;
7212        hr_utility.set_location('p_extract_file_id'||p_extract_file_id ,10) ;
7213        hr_utility.set_location('p_business_group_id'||p_business_group_id ,10) ;
7214        hr_utility.set_location('p_validate  '|| p_validate ,10) ;
7215   --
7216   --
7217   -- Issue a savepoint if operating in validation only mode
7218   --
7219   savepoint SUBMIT_COPY_REQUEST;
7220   --
7221   hr_utility.set_location(l_proc, 20);
7222 
7223    open c_bg_name ;
7224    fetch c_bg_name into  l_business_goup_name ;
7225    if  c_bg_name%notfound  then
7226        close c_bg_name ;
7227        fnd_message.set_name('BEN','BEN_91000_INVALID_BUS_GROUP');
7228        fnd_message.raise_error;
7229    end if ;
7230    close c_bg_name ;
7231 
7232   -- decide whether it is  seede or not for when the process is export
7233   if p_mode = 'EXP' then
7234      -- get the extract name and validate
7235      open c_ext  ;
7236      fetch c_ext into l_ext_file_name,l_business_group_id ;
7237      close c_ext ;
7238      --
7239      if  l_ext_file_name is  null then
7240          fnd_message.set_name('BEN','BEN_91000_INVALID_EXT_NAME');
7241          fnd_message.raise_error;
7242      end if ;
7243      --
7244      if l_business_group_id is not null then
7245         l_seeded := 'N' ;
7246         l_business_group_name := 'BUSINESS_GROUP='|| l_business_goup_name ;
7247      else
7248         l_seeded := 'Y' ;
7249      end if ;
7250 
7251      l_ext_file_name := 'FILE_NAME='|| l_ext_file_name ;
7252   else
7253     --- import to system
7254     -- the upload only suppport business group base uploads
7255     -- seeded should be uploaded from hrglobals
7256     l_business_group_name := 'UPLOAD_BUSINESS_GROUP='|| l_business_goup_name ;
7257     l_view_name  := 'LEG_VIEW=' || 'BEN_PL_V' ;
7258 
7259     -- if validate mode then set the variable
7260     if p_validate = 'Y' then
7261        l_validate  := 'VALIDATE_MODE='|| p_validate ;
7262     end if ;
7263 
7264     if p_allow_override = 'Y' then
7265         l_override  :=  'EXT_OVERRIDE='||p_allow_override ;
7266     end if ;
7267   end if ;
7268 
7269 
7270   hr_utility.set_location('Bg Name ' || l_business_group_name , 20);
7271 
7272 
7273   if p_file_name is null then
7274      fnd_message.set_name('BEN','BEN_91000_INVALID_FILE_NAME');
7275      fnd_message.raise_error;
7276   end if ;
7277 
7278   if l_loader_file is null then
7279      l_loader_file :=   '@ben:/patch/115/import/benextse.lct';
7280   end if ;
7281 
7282   --
7283 
7284   if p_mode  = 'EXP' then
7285      l_request_id := fnd_request.submit_request
7286                   (application => 'BEN'
7287                   ,program     => 'BENXUPLDR'
7288                   ,description => NULL
7289                   ,sub_request => FALSE
7290                   ,argument1   => 'DOWNLOAD'
7291                   ,argument2   => l_loader_file
7292                   ,argument3   => p_file_name
7293                   ,argument4   => 'EXTRACT'
7294                   ,argument5   => l_ext_file_name
7295                   ,argument6   => 'CM_DOWNLOAD=Y'
7296                   ,argument7   => l_business_group_name
7297                  );
7298   else
7299      l_request_id := fnd_request.submit_request
7300                   (application => 'BEN'
7301                   ,program     => 'BENXDNLDR'
7302                   ,description => NULL
7303                   ,sub_request => FALSE
7304                   ,argument1   => 'UPLOAD_PARTIAL'
7305                   ,argument2   => l_loader_file
7306                   ,argument3   => p_file_name
7307                   ,argument4   => 'EXTRACT'
7308                   ,argument5   => l_view_name
7309                   ,argument6   => l_business_group_name
7310                   ,argument7   => l_validate
7311                   ,argument8   => l_override
7312                  );
7313   end if ;
7314 
7315   -- When in validation only mode raise the Validate_Enabled exception
7316   --
7317   hr_utility.set_location(' Leaving:'||l_proc, 70);
7318   --
7319 exception
7320   --
7321   when app_exception.application_exception then
7322 
7323     fnd_msg_pub.add;
7324 
7325     --
7326   when others then
7327     --
7328     -- A validation or unexpected error has occured
7329     --
7330     ROLLBACK TO SUBMIT_COPY_REQUEST;
7331     raise;
7332     --
7333 end Load_extract;
7334 
7335 
7336 --
7337 End ben_ext_thread;