DBA Data[Home] [Help]

PACKAGE BODY: APPS.PAY_BALANCE_PKG

Source


1 package body pay_balance_pkg as
2 -- $Header: pybaluex.pkb 120.38.12010000.2 2008/10/01 06:12:29 ankagarw ship $
3 -- Declare tables:
4 type con_name_array is table of ff_contexts.context_name%type
5                        index by binary_integer;
6 type con_num_array  is table of ff_contexts.context_id%type
7                        index by binary_integer;
8 type con_type_array is table of ff_contexts.data_type%type
9                        index by binary_integer;
10 type con_val_array  is table of pay_run_result_values.result_value%type
11                        index by binary_integer;
12 type par_val_array  is table of varchar2(80)
13                        index by binary_integer;
14 type number_array  is table of number
15                        index by binary_integer;
16 --
17 con_name_tab    con_name_array;
18 con_id_tab      con_num_array;
19 con_type_tab    con_type_array;
20 con_value_tab   con_val_array;
21 --
22 no_rows_con_tab binary_integer := 0;
23 --
24 -- Cache for Route contexts
25 --
26 type route_contexts_cache_type is record
27 (
28  cxt_id                    number_array,
29  sz                        number
30 );
31 route_contexts_cache route_contexts_cache_type;
32 --
33 -- Cache for Route Parameters
34 --
35 type route_parameters_cache_type is record
36 (
37  seq_no                    number_array,
38  par_val                   par_val_array,
39  sz                        number
40 );
41 route_parameters_cache route_parameters_cache_type;
42 --
43 -- The following type and varable are used for Dynamic SQL
44 --
45 MAX_DYN_SQL_SIZE number := 20000;
46 MAX_TRACE_SIZE   number := 255;
47 --
48 -- The follow types and variables are used to map core contexts
49 -- onto legislative contexts.
50 --
51 type t_context_map_rec is record
52 (core_context_name     ff_contexts.context_name%type,
53  loc_context_name      pay_legislation_contexts.legislation_name%type
54 );
55 --
56 type t_context_map_tab is table of t_context_map_rec index by binary_integer;
57 --
58 g_legislation_code per_business_groups_perf.legislation_code%type;
59 g_context_mapping  t_context_map_tab;
60 --
61 -- tables for dimension cache
62 --
63 type t_def_bal_rec is record
64 (dimension_type       pay_balance_dimensions.dimension_type%type,
65  expiry_check_code    pay_balance_dimensions.expiry_checking_code%type,
66  expiry_check_lvl     pay_balance_dimensions.expiry_checking_level%type,
67  dimension_name       pay_balance_dimensions.dimension_name%type,
68  jurisdiction_lvl     pay_balance_types.jurisdiction_level%type,
69  balance_type_column  pay_dimension_routes.balance_type_column%type,
70  decode_required      pay_dimension_routes.decode_required%type,
71  balance_dimension_id pay_balance_dimensions.balance_dimension_id%type,
72  dimension_lvl        pay_balance_dimensions.dimension_level%type,
73  period_type          pay_balance_dimensions.period_type%type,
74  balance_type_id      pay_balance_types.balance_type_id%type,
75  dim_rou_rr_route_id  pay_dimension_routes.route_id%type,
76  tu_needed            boolean,
77  jc_needed            boolean,
78  si_needed            boolean,
79  sn_needed            boolean,
80  st_needed            boolean,
81  st2_needed           boolean,
82  td_needed            boolean,
83  bd_needed            boolean,
84  lu_needed            boolean,
85  sn2_needed           boolean,
86  org_needed           boolean,
87  start_rb_ptr         number,
88  end_rb_ptr           number,
89  rr_ptr               number
90 );
91 --
92 type t_def_bal_tab is table of t_def_bal_rec index by binary_integer;
93 --
94 g_debug boolean := hr_utility.debug_enabled;
95 g_defbal_cache t_def_bal_tab;
96 --
97 --
98 -- tables for dimension route cache
99 --
100 type t_dim_rou_rec is record
101 (
102  balance_dimension_id pay_balance_dimensions.balance_dimension_id%type,
103  priority             pay_dimension_routes.priority%type,
104  route_type           pay_dimension_routes.route_type%type,
105  run_def_bal_id       pay_defined_balances.defined_balance_id%type,
106  retrieval_column     pay_dimension_routes.retrieval_column%type
107 );
108 --
109 type t_dim_rou_tab is table of t_dim_rou_rec index by binary_integer;
110 --
111 g_dimrou_cache t_dim_rou_tab;
112 --
113 --
114 -- Internal cache type for batch retrieval
115 --
116 type t_int_bal_rec is record
117 (
118  defined_balance_id   pay_defined_balances.defined_balance_id%type,
119  balance_dimension_id pay_balance_dimensions.balance_dimension_id%type,
120  balance_type_id      pay_balance_types.balance_type_id%type,
121  balance_found        boolean,
122  balance_value        number,
123  jurisdiction_level   pay_balance_types.jurisdiction_level%type,
124  tax_unit_id          pay_assignment_actions.tax_unit_id%type,
125  jurisdiction_code    pay_run_results.jurisdiction_code%type,
126  source_id            number,
127  source_text          pay_run_result_values.result_value%type,
128  source_number        number,
129  source_text2         pay_run_result_values.result_value%type,
130  time_def_id          pay_run_results.time_definition_id%type,
131  balance_date         pay_run_results.end_date%type,
132  original_entry_id    number,
133  local_unit_id        pay_run_results.local_unit_id%type,
134  source_number2       number,
135  organization_id      number
136 );
137 --
138 type t_int_bal_cache is table of t_int_bal_rec index by binary_integer;
139 --
140 -- Internal Run Balance link list used in rollback
141 --
142 type t_int_rb_rec is record
143 (
144  run_balance_id       pay_run_balances.run_balance_id%type,
145  defined_balance_id   pay_defined_balances.defined_balance_id%type,
146  payroll_action_id    pay_payroll_actions.payroll_action_id%type,
147  tax_unit_id          pay_assignment_actions.tax_unit_id%type,
148  jurisdiction_code    pay_run_results.jurisdiction_code%type,
149  source_id            number,
150  source_text          pay_run_result_values.result_value%type,
151  source_number        number,
152  source_text2         pay_run_result_values.result_value%type,
153  time_def_id          pay_run_results.time_definition_id%type,
154  balance_date         pay_run_results.end_date%type,
155  local_unit_id        pay_run_results.local_unit_id%type,
156  source_number2       number,
157  organization_id      number,
158  next                 number
159 );
160 --
161 type t_int_rb_cache is table of t_int_rb_rec index by binary_integer;
162 --
163 -- Batch retrieval cache.
164 --
165 type t_batch_rec is record
166 (
167  balance_type_id  pay_balance_types.balance_type_id%type,
168  balance_value    number,
169  source_index     number   -- This is used to index back to the
170                            -- original PL/SQL table.
171 );
172 --
173 type t_batch_list is table of t_batch_rec index by binary_integer;
174 --
175 --
176 -- Number array
177 --
178 type number_tab is table of number index by binary_integer;
179 --
180 --
181 -- Context Details cache
182 --
183 type t_context_details_rec is record
184 (
185   tax_unit_id           pay_assignment_actions.tax_unit_id%type,
186   jurisdiction_code     pay_run_results.jurisdiction_code%type,
187   source_id             pay_run_result_values.result_value%type,
188   source_text           pay_run_result_values.result_value%type,
189   source_number         pay_run_result_values.result_value%type,
190   source_text2          pay_run_result_values.result_value%type,
191   time_def_id           pay_run_results.time_definition_id%type,
192   balance_date          pay_run_results.end_date%type,
193   local_unit_id        pay_run_results.local_unit_id%type,
194   source_number2       number,
195   organization_id      number,
196   prv_tax_unit_id       pay_assignment_actions.tax_unit_id%type,
197   prv_jurisdiction_code pay_run_results.jurisdiction_code%type,
198   prv_source_id         pay_run_result_values.result_value%type,
199   prv_source_text       pay_run_result_values.result_value%type,
200   prv_source_number     pay_run_result_values.result_value%type,
201   prv_source_text2      pay_run_result_values.result_value%type,
202   prv_time_def_id       pay_run_results.time_definition_id%type,
203   prv_balance_date      pay_run_results.end_date%type,
204   prv_local_unit_id     pay_run_results.local_unit_id%type,
205   prv_source_number2    number,
206   prv_organization_id   number,
207   tu_needed             boolean,
208   jc_needed             boolean,
209   si_needed             boolean,
210   st_needed             boolean,
211   sn_needed             boolean,
212   st2_needed            boolean,
213   td_needed             boolean,
214   bd_needed             boolean,
215   lu_needed             boolean,
216   sn2_needed            boolean,
217   org_needed            boolean,
218   tu_set                boolean,
219   jc_set                boolean,
220   si_set                boolean,
221   st_set                boolean,
222   sn_set                boolean,
223   st2_set               boolean,
224   td_set                boolean,
225   bd_set                boolean,
226   lu_set                boolean,
227   sn2_set               boolean,
228   org_set               boolean
229 );
230 --
231 -- Run result record
232 --
233 type t_run_result_rec is record
234  (run_result_id          number
235  ,element_type_id        number
236  ,jurisdiction_code      pay_run_results.jurisdiction_code%type
237  ,assignment_action_id   number
238  ,assignment_id          number
239  ,tax_unit_id            number
240  ,payroll_action_id      number
241  ,time_def_id            pay_run_results.time_definition_id%type
242  ,balance_date           pay_run_results.end_date%type
243  ,local_unit_id          pay_run_results.local_unit_id%type
244  ,source_number2         number
245  ,organization_id        number
246  ,business_group_id      number
247  ,legislation_code       per_business_groups.legislation_code%type
248  ,effective_date         date
249  );
250 --
251 -- Cache parameters for new_defined_balance
252 --
253 cached       boolean  := FALSE;
254 g_low_volume pay_action_parameters.parameter_value%type := 'N';
255 --
256 -- tables for expiry_checking cache
257 --
258 type pay_act_id_tab is table of pay_payroll_actions.payroll_action_id%type
259                        index by binary_integer;
260 type expiry_tab     is table of NUMBER(38) index by binary_integer;
261 type dim_nm_tab    is table of pay_balance_dimensions.dimension_name%type
262                       index by binary_integer;
263 --
264 t_own_pay_action   pay_act_id_tab;
265 t_usr_pay_action   pay_act_id_tab;
266 t_dim_nm           dim_nm_tab;
267 t_expiry           expiry_tab;
268 --
269 -- The following globals are used in remove_asg_contrib
270 g_payroll_action       pay_payroll_actions.payroll_action_id%type;
271 g_rlb_grp_defbals      t_balance_value_tab;
272 g_rlb_asg_defbals      t_balance_value_tab;
273 g_grp_maintained_rb    t_int_rb_cache;
274 g_grp_rb_ptr_list      number_array;
275 g_si_needed_chr        pay_legislation_rules.rule_mode%type;
276 g_st_needed_chr        pay_legislation_rules.rule_mode%type;
277 g_sn_needed_chr        pay_legislation_rules.rule_mode%type;
278 g_st2_needed_chr       pay_legislation_rules.rule_mode%type;
279 g_sn2_needed_chr       pay_legislation_rules.rule_mode%type;
280 g_org_needed_chr       pay_legislation_rules.rule_mode%type;
281 g_save_run_bals        pay_legislation_rules.rule_mode%type;
282 --
283 -- The following globals are used in get_rb_status
284 g_aa_id                NUMBER := NULL;
285 g_retrieval_date       pay_payroll_actions.effective_date%type := NULL;
286 g_bus_grp_id           pay_payroll_actions.business_group_id%type := NULL;
287 g_payroll_id           pay_payroll_actions.payroll_id%type := NULL;
288 g_action_type          pay_payroll_actions.action_type%type := NULL;
289 --
290 -- Run result record, used in get_run_result_info.
291 g_run_result_rec         t_run_result_rec;
292 --
293 g_oracle_version       NUMBER; -- holds the oracle version number and is
294                                -- set by get_oracle_db_version
295 --
296 -- Setup the internal context settings
297 --
298 g_con_tax_unit_id         pay_latest_balances.tax_unit_id%type;
299 g_con_jurisdiction_code   pay_latest_balances.jurisdiction_code%type;
300 g_con_original_entry_id   pay_latest_balances.original_entry_id%type;
301 g_con_source_id           pay_latest_balances.source_id%type;
302 g_con_source_text         pay_latest_balances.source_text%type;
303 g_con_source_text2        pay_latest_balances.source_text2%type;
304 g_con_source_number       pay_latest_balances.source_number%type;
305 g_con_tax_group           pay_latest_balances.tax_group%type;
306 g_con_payroll_id          pay_latest_balances.payroll_id%type;
307 g_con_local_unit_id       pay_latest_balances.local_unit_id%type;
308 g_con_organization_id     pay_latest_balances.organization_id%type;
309 g_con_source_number2      pay_latest_balances.source_number2%type;
310 --
311 -- Copy of Hr_General2.get_oracle_db_version to avoid a very nasty patching
312 -- issue in the c-code chain.
313 --  Bug 2865665.
314 -- --------------------------------------------------------------------------
315 -- |-------------------< get_oracle_db_version >----------------------------|
316 -- --------------------------------------------------------------------------
317 -- This function returns the current (major) ORACLE version number in the
318 -- format x.x (where x is a number):
319 -- e.g. 8.0, 8.1, 9.0, 9.1
320 -- If for any reason the version number cannot be identified, NULL is
321 -- returned
322 FUNCTION get_oracle_db_version RETURN NUMBER IS
323   l_proc          VARCHAR2(72);
324   l_version       VARCHAR2(30);
325   l_compatibility VARCHAR2(30);
326 BEGIN
327   g_debug := hr_utility.debug_enabled;
328   IF g_debug THEN
329     l_proc := 'pay_balance_pkg.get_oracle_db_version';
330     hr_utility.set_location('Entering:'||l_proc, 5);
331   END IF;
332   -- check to see if the g_oracle_version already exists
333   IF g_oracle_version IS NULL THEN
334     -- get the current ORACLE version and compatibility values
335     dbms_utility.db_version(l_version, l_compatibility);
336     -- the oracle version number is held in the format:
337     -- x.x.x.x.x
338     -- set the version number to the first decimal position
339     -- e.g. 9.1.2.0.0 returns 9.1
340     --      9.0.1.2.0 returns 9.0
341     --      8.1.7.3.0 returns 8.1
342     --      8.0.2.1.0 returns 8.0
343     --
344     -- modified line below to include a NUMBER format model to get
345     -- around, numeric format problems which have been identified
346     -- in wwbug 2772209
347     -- note: an important assumption is made here; the oracle
348     -- version is always returned with a period '.' as a seperator
349     -- regardless of NLS.
350     g_oracle_version :=
351       TO_NUMBER(SUBSTRB(l_version,1,INSTRB(l_version,'.',1,2)-1),'99.99');
352   END IF;
353   IF g_debug THEN
354     hr_utility.set_location('Leaving:'||l_proc, 10);
355   END IF;
356   -- return the value
357   RETURN(g_oracle_version);
358 EXCEPTION
359   WHEN OTHERS THEN
360     -- an unexpected error was raised and is most probably caused by
361     -- the TO_NUMBER conversion. Because of this, return NULL
362     -- indicating that the Oracle Version number could NOT be assertained
363     IF g_debug THEN
364        hr_utility.set_location('Leaving:'||l_proc, 15);
365     END IF;
366     RETURN(NULL);
367 END get_oracle_db_version;
368 --
369 --------------------------------------------------------------------------
370 -- procedure split_jurisdiction
371 --------------------------------------------------------------------------
372 procedure split_jurisdiction(p_jurisdiction in            varchar2,
373                              p_jur1         in out nocopy varchar2,
374                              p_jur2         in out nocopy varchar2,
375                              p_jur3         in out nocopy varchar2
376                             )
377 is
378 idx number;
379 prev_idx number;
380 begin
381 --
382     p_jur1 := null;
383     p_jur2 := null;
384     p_jur3 := null;
385 --
386     if p_jurisdiction is not null then
387 --
388       idx := instr(p_jurisdiction, '-');
389 --
390       -- Set Comp 1
391       if idx = 0 then
392         p_jur1 := p_jurisdiction;
393       else
394 --
395         -- Set Comp1 and move to Comp2
396         p_jur1   := substr(p_jurisdiction, 1, idx-1);
397         prev_idx := idx;
398         idx      := instr(p_jurisdiction, '-', prev_idx+1);
399 --
400         if idx = 0 then
401            p_jur2 := substr(p_jurisdiction, prev_idx+1);
402         else
403            p_jur2 := substr(p_jurisdiction, prev_idx+1, idx -1 - prev_idx);
404            p_jur3 := substr(p_jurisdiction, idx+1);
405         end if;
406       end if;
407 --
408     end if;
409 --
410 end split_jurisdiction;
411 --
412 --------------------------- get_period_type_start -------------------------------
413  /* Name    : get_period_type_start
414   Purpose   : This returns the start date of a period type given an
415               effective_date.
416   Arguments :
417        p_period_type is mandatory
418        p_effective_date is mandatory
419        p_start_date_code is only required for period_type DYNAMIC
420        p_payroll_id is only required for period_type PERIOD
421        p_bus_grp is only needed for period_type TYEAR, TQUARTER, FYEAR and
422                  FQUARTER
423        p_action_type is only needed for period_type PAYMENT
424        p_asg_action is only needed for period_type PAYMENT
425   Notes     :
426  */
427 procedure get_period_type_start(p_period_type    in            varchar2,
428                                p_effective_date  in            date,
429                                p_start_date         out nocopy date,
430                                p_start_date_code in            varchar2 default null,
431                                p_payroll_id      in            number   default null,
432                                p_bus_grp         in            number   default null,
433                                p_action_type     in            varchar2 default null,
434                                p_asg_action      in            number   default null)
435 is
436 l_return_date date;
437 l_statem              varchar2(2000);  -- used with dynamic pl/sql
438 sql_cursor           integer;
439 l_rows               integer;
440 begin
441    g_debug := hr_utility.debug_enabled;
442 --
443    l_return_date := to_date('0001/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS');
444 --
445    if (p_period_type = 'YEAR') then
446 --
447      l_return_date := trunc(p_effective_date, 'Y');
448 --
449    elsif (p_period_type = 'QUARTER') then
450 --
451      l_return_date := trunc(p_effective_date, 'Q');
452 --
453    elsif (p_period_type = 'PERIOD') then
454 --
455      select start_date
456        into l_return_date
457        from per_time_periods
458       where payroll_id = p_payroll_id
459         and p_effective_date between start_date
460                                  and end_date;
461 --
462    elsif (p_period_type = 'MONTH') then
463 --
464      l_return_date := trunc(p_effective_date, 'MM');
465 --
466    elsif (p_period_type = 'RUN') then
467 --
468      l_return_date := p_effective_date;
469 --
470    elsif (p_period_type = 'TYEAR') then
471 --
472      l_return_date:= pay_ip_route_support.tax_year(p_bus_grp,
473                                    p_effective_date);
474 --
475    elsif (p_period_type = 'TQUARTER') then
476 --
477      l_return_date:= pay_ip_route_support.tax_quarter(p_bus_grp,
478                                    p_effective_date);
479 --
480    elsif (p_period_type = 'FYEAR') then
481 --
482      l_return_date:= pay_ip_route_support.fiscal_year(p_bus_grp,
483                                    p_effective_date);
484 --
485    elsif (p_period_type = 'FQUARTER') then
486 --
487      l_return_date:= pay_ip_route_support.fiscal_quarter(p_bus_grp,
488                                    p_effective_date);
489 --
490    elsif (p_period_type = 'PAYMENT') then
491 --
492       if (p_action_type in ('R', 'Q')) then
493 --
494          l_return_date := p_effective_date;
495 --
496       elsif (p_action_type in ('P', 'U')) then
497 --
498          select min(effective_date)
499            into l_return_date
500            from pay_payroll_actions ppa,
501                 pay_assignment_actions paa,
502                 pay_action_interlocks pai
503           where pai.locking_action_id = p_asg_action
504             and pai.locked_action_id = paa.assignment_action_id
505             and paa.payroll_action_id = ppa.payroll_action_id;
506       else
507 --
508           l_return_date := to_date('0001/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS');
509 --
510       end if;
511 --
512    elsif (p_period_type = 'DYNAMIC') then
513 --
514      if (p_start_date_code is null) then
515        --
516        -- Dynamic but no code supplied return start of time
517        --
518        l_return_date := to_date('0001/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS');
519      else
520 --
521        l_statem := 'begin ' || p_start_date_code || ' (';
522        l_statem := l_statem||' p_effective_date => :l_eff_date, ';
523        l_statem := l_statem||' p_start_date     => :l_start_date, ';
524        l_statem := l_statem||' p_payroll_id     => :l_payroll, ';
525        l_statem := l_statem||' p_bus_grp        => :l_bus_grp, ';
526        l_statem := l_statem||' p_asg_action     => :l_asg_act); end;  ';
527 --
528        sql_cursor := dbms_sql.open_cursor;
529        --
530        dbms_sql.parse(sql_cursor, l_statem, dbms_sql.v7);
531        --
532        --
533        dbms_sql.bind_variable(sql_cursor, 'l_eff_date',   p_effective_date);
534        dbms_sql.bind_variable(sql_cursor, 'l_start_date', p_start_date);
535        dbms_sql.bind_variable(sql_cursor, 'l_payroll',    p_payroll_id);
536        dbms_sql.bind_variable(sql_cursor, 'l_bus_grp',    p_bus_grp);
537        dbms_sql.bind_variable(sql_cursor, 'l_asg_act',    p_asg_action);
538        --
539        l_rows := dbms_sql.execute(sql_cursor);
540        --
541        if (l_rows = 1) then
542           dbms_sql.variable_value(sql_cursor, 'l_start_date',
543                                   l_return_date);
544           dbms_sql.close_cursor(sql_cursor);
545 --
546        else
547           l_return_date := to_date('0001/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS');
548           dbms_sql.close_cursor(sql_cursor);
549        end if;
550 --
551      end if;
552 --
553    end if;
554 --
555    if (l_return_date is null) then
556      p_start_date := to_date('0001/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS');
557    else
558      p_start_date := l_return_date;
559    end if;
560 --
561 end get_period_type_start;
562 --
563 --------------------------- get_period_type_end ------------------------------
564 --
565  /* Name    : get_period_type_end
566   Purpose   : This returns the end date of a period type given an
567               effective_date. This should be used in conjuction
568               with expiry checking.
569   Arguments :
570        p_period_type is mandatory
571        p_effective_date is mandatory
572        p_payroll_id is only required for period_type PERIOD
573        p_action_type is only needed for period_type PAYMENT
574        p_asg_action is only needed for period_type PAYMENT
575   Notes     :
576        If p_end_date is returned as null then expiry checking code
577        should be used.
578  */
579 procedure get_period_type_end(p_period_type    in            varchar2,
580                                p_effective_date  in            date,
581                                p_end_date           out nocopy date,
582                                p_payroll_id      in            number   default null,
583                                p_action_type     in            varchar2 default null,
584                                p_asg_action      in            number   default null)
585 is
586 l_return_date date;
587 l_statem              varchar2(2000);  -- used with dynamic pl/sql
588 sql_cursor           integer;
589 l_rows               integer;
590 begin
591    g_debug := hr_utility.debug_enabled;
592 --
593    l_return_date := null;
594 --
595    if (p_period_type = 'YEAR') then
596 --
597      l_return_date := trunc(add_months(p_effective_date, 12), 'Y') -1;
598 --
599    elsif (p_period_type = 'QUARTER') then
600 --
601      l_return_date := trunc(add_months(p_effective_date, 3), 'Q') -1;
602 --
603 --
604    elsif (p_period_type = 'PERIOD') then
605 --
606      select end_date
607        into l_return_date
608        from per_time_periods
609       where payroll_id = p_payroll_id
610         and p_effective_date between start_date
611                                  and end_date;
612 --
613    elsif (p_period_type = 'MONTH') then
614 --
615      l_return_date := trunc(add_months(p_effective_date, 1) , 'MM') -1;
616 --
617    elsif (p_period_type = 'RUN') then
618 --
619      l_return_date := p_effective_date;
620 --
621    elsif (p_period_type = 'TYEAR') then
622 --
623      l_return_date:= null;
624 --
625    elsif (p_period_type = 'TQUARTER') then
626 --
627      l_return_date:= null;
628 --
629    elsif (p_period_type = 'FYEAR') then
630 --
631      l_return_date:= null;
632 --
633    elsif (p_period_type = 'FQUARTER') then
634 --
635      l_return_date:= null;
636 --
637    elsif (p_period_type = 'PAYMENT') then
638 --
639       if (p_action_type in ('R', 'Q')) then
640 --
641          l_return_date := p_effective_date;
642 --
643       elsif (p_action_type in ('P', 'U')) then
644 --
645          select max(effective_date)
646            into l_return_date
647            from pay_payroll_actions ppa,
648                 pay_assignment_actions paa,
649                 pay_action_interlocks pai
650           where pai.locking_action_id = p_asg_action
651             and pai.locked_action_id = paa.assignment_action_id
652             and paa.payroll_action_id = ppa.payroll_action_id;
653       else
654 --
655           l_return_date := null;
656 --
657       end if;
658 --
659    elsif (p_period_type = 'DYNAMIC') then
660 --
661      l_return_date := null;
662 --
663    end if;
664 --
665    p_end_date := l_return_date;
666 --
667 end get_period_type_end;
668 --
669 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
670 --
671 --                            load_defbal_cache                           +
672 --                                                                        +
673 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
674 /*
675 NAME
676 
677         load_defbal_cache  -  Load a dimension in to the cache.
678 
679 DESCRIPTION
680 
681 */
682 procedure load_defbal_cache(p_defined_balance_id in         number,
683                             p_defbal_rec         out nocopy t_def_bal_rec)
684 is
685 --
686 cursor get_bal_contexts (p_def_bal_id in number) is
687 select context_name
688 from   ff_contexts             fc,
689        ff_route_context_usages frcu,
690        pay_balance_dimensions  pbd,
691        pay_defined_balances    pdb
692 where  pdb.defined_balance_id = p_def_bal_id
693 and    pdb.balance_dimension_id = pbd.balance_dimension_id
694 and    pbd.route_id = frcu.route_id
695 and    frcu.context_id = fc.context_id;
696 --
697 cursor get_rr_route(p_def_bal_id in number) is
698     select pdr.run_dimension_id
699     ,      pdr.priority
700     ,      pdr.route_type
701     ,      null run_def_bal_id
702     ,      pdr.retrieval_column
703     from   pay_dimension_routes pdr
704     ,      pay_defined_balances pdb -- balance defined balance
705     where pdb.balance_dimension_id = pdr.balance_dimension_id
706     and    pdb.defined_balance_id = p_def_bal_id
707     and    pdr.route_type = 'RR';
708 --
709 CURSOR get_rb_routes(p_def_bal_id number)
710 IS
711 select pdr.run_dimension_id
712 ,      pdr.priority
713 ,      pdr.route_type
714 ,      rdb.defined_balance_id run_def_bal_id
715 ,      pdr.retrieval_column
716 from   pay_dimension_routes pdr
717 ,      pay_defined_balances pdb -- balance defined balance
718 ,      pay_defined_balances rdb -- run defined balance
719 where pdb.balance_dimension_id = pdr.balance_dimension_id
720 and    pdb.defined_balance_id = p_def_bal_id
721 and    rdb.balance_type_id = pdb.balance_type_id
722 and    rdb.balance_dimension_id = pdr.run_dimension_id
723 and    pdr.route_type = 'SRB'
724 order by 2;
725 --
726 begin
727     --
728     -- An on demand cache of dimension details is built, if the current
729     -- defined_balance_id does not exist in the cache, then the details are
730     -- returned from the original select stmt and added to the cache.
731     --
732     if not g_defbal_cache.exists(p_defined_balance_id) then
733     --
734       if g_debug then
735          hr_utility.set_location ('pay_balance_pkg.load_defbal_cache', 10);
736       end if;
737       select DIM.dimension_type,
738              DIM.expiry_checking_code,
739              DIM.expiry_checking_level,
740              DIM.dimension_name,
741              nvl(TYP.jurisdiction_level, 0),
742              DEF.balance_dimension_id,
743              DEF.balance_type_id,
744              DIM.dimension_level,
745              DIM.period_type
746       into   g_defbal_cache(p_defined_balance_id).dimension_type,
747              g_defbal_cache(p_defined_balance_id).expiry_check_code,
748              g_defbal_cache(p_defined_balance_id).expiry_check_lvl,
749              g_defbal_cache(p_defined_balance_id).dimension_name,
750              g_defbal_cache(p_defined_balance_id).jurisdiction_lvl,
751              g_defbal_cache(p_defined_balance_id).balance_dimension_id,
752              g_defbal_cache(p_defined_balance_id).balance_type_id,
753              g_defbal_cache(p_defined_balance_id).dimension_lvl,
754              g_defbal_cache(p_defined_balance_id).period_type
755       from   pay_balance_dimensions         DIM
756       ,      pay_defined_balances           DEF
757       ,      pay_balance_types              TYP
758       where  DIM.balance_dimension_id     = DEF.balance_dimension_id
759       and    TYP.balance_type_id          = DEF.balance_type_id
760       and    DEF.defined_balance_id       = p_defined_balance_id;
761       --
762       -- Now get the dimension route details
763       --
764       begin
765         select pdr.balance_type_column,
766                pdr.decode_required,
767                pdr.route_id
768         into   g_defbal_cache(p_defined_balance_id).balance_type_column,
769                g_defbal_cache(p_defined_balance_id).decode_required,
770                g_defbal_cache(p_defined_balance_id).dim_rou_rr_route_id
771         from   pay_dimension_routes pdr
772         ,      pay_defined_balances pdb
773         where  pdb.balance_dimension_id = pdr.balance_dimension_id
774         and    pdb.defined_balance_id = p_defined_balance_id
775         and    pdr.route_type = 'RR';
776 --
777       exception
778         when no_data_found then
779           g_defbal_cache(p_defined_balance_id).balance_type_column := NULL;
780           g_defbal_cache(p_defined_balance_id).decode_required     := NULL;
781           g_defbal_cache(p_defined_balance_id).dim_rou_rr_route_id := NULL;
782       end;
783 --
784       g_defbal_cache(p_defined_balance_id).tu_needed := FALSE;
785       g_defbal_cache(p_defined_balance_id).jc_needed := FALSE;
786       g_defbal_cache(p_defined_balance_id).si_needed := FALSE;
787       g_defbal_cache(p_defined_balance_id).sn_needed := FALSE;
788       g_defbal_cache(p_defined_balance_id).st_needed := FALSE;
789       g_defbal_cache(p_defined_balance_id).st2_needed := FALSE;
790       g_defbal_cache(p_defined_balance_id).td_needed := FALSE;
791       g_defbal_cache(p_defined_balance_id).bd_needed := FALSE;
792       g_defbal_cache(p_defined_balance_id).lu_needed := FALSE;
793       g_defbal_cache(p_defined_balance_id).sn2_needed := FALSE;
794       g_defbal_cache(p_defined_balance_id).org_needed := FALSE;
795 --
796       -- Get the contexts needed.
797       for ctrec in get_bal_contexts(p_defined_balance_id) loop
798         if g_debug then
799            hr_utility.set_location('pay_balance_pkg.load_defbal_cache', 10);
800         end if;
801         if ctrec.context_name = 'TAX_UNIT_ID' then
802           g_defbal_cache(p_defined_balance_id).tu_needed := TRUE;
803         end if;
804         if ctrec.context_name = 'JURISDICTION_CODE' then
805           g_defbal_cache(p_defined_balance_id).jc_needed := TRUE;
806         end if;
807         if ctrec.context_name = 'SOURCE_ID' then
808           g_defbal_cache(p_defined_balance_id).si_needed := TRUE;
809         end if;
810         if ctrec.context_name = 'SOURCE_TEXT' then
811           g_defbal_cache(p_defined_balance_id).st_needed := TRUE;
812         end if;
813         if ctrec.context_name = 'SOURCE_TEXT2' then
814           g_defbal_cache(p_defined_balance_id).st2_needed := TRUE;
815         end if;
816         if ctrec.context_name = 'SOURCE_NUMBER' then
817           g_defbal_cache(p_defined_balance_id).sn_needed := TRUE;
818         end if;
819         if ctrec.context_name = 'TIME_DEFINITION_ID' then
820           g_defbal_cache(p_defined_balance_id).td_needed := TRUE;
821         end if;
822         if ctrec.context_name = 'BALANCE_DATE' then
823           g_defbal_cache(p_defined_balance_id).bd_needed := TRUE;
824         end if;
825         if ctrec.context_name = 'LOCAL_UNIT_ID' then
826           g_defbal_cache(p_defined_balance_id).lu_needed := TRUE;
827         end if;
828         if ctrec.context_name = 'SOURCE_NUMBER2' then
829           g_defbal_cache(p_defined_balance_id).sn2_needed := TRUE;
830         end if;
831         if ctrec.context_name = 'ORGANIZATION_ID' then
832           g_defbal_cache(p_defined_balance_id).org_needed := TRUE;
833         end if;
834       end loop;
835 --
836       -- Now setup the dimension routes.
837       declare
838         l_position number;
839         l_strt_position number;
840         l_found    boolean;
841       begin
842 --
843         g_defbal_cache(p_defined_balance_id).start_rb_ptr := NULL;
844         g_defbal_cache(p_defined_balance_id).end_rb_ptr   := NULL;
845         g_defbal_cache(p_defined_balance_id).rr_ptr       := NULL;
846 --
847         for rrrec in get_rr_route(p_defined_balance_id) loop
848 --
849           l_position := g_dimrou_cache.count + 1;
850           g_dimrou_cache(l_position).balance_dimension_id :=
851                         g_defbal_cache(p_defined_balance_id).balance_dimension_id;
852           g_dimrou_cache(l_position).priority             := rrrec.priority;
853           g_dimrou_cache(l_position).route_type           := rrrec.route_type;
854           g_dimrou_cache(l_position).run_def_bal_id       := rrrec.run_def_bal_id;
855           g_dimrou_cache(l_position).retrieval_column     := rrrec.retrieval_column;
856 --
857         end loop;
858 --
859         g_defbal_cache(p_defined_balance_id).rr_ptr := l_position;
860 --
861         l_found := FALSE;
862         l_strt_position := g_dimrou_cache.count + 1;
863         for rbrec in get_rb_routes(p_defined_balance_id) loop
864 --
865           l_found := TRUE;
866           l_position := g_dimrou_cache.count + 1;
867           g_dimrou_cache(l_position).balance_dimension_id :=
868                         g_defbal_cache(p_defined_balance_id).balance_dimension_id;
869           g_dimrou_cache(l_position).priority             := rbrec.priority;
870           g_dimrou_cache(l_position).route_type           := rbrec.route_type;
871           g_dimrou_cache(l_position).run_def_bal_id       := rbrec.run_def_bal_id;
872           g_dimrou_cache(l_position).retrieval_column     := rbrec.retrieval_column;
873 --
874         end loop;
875 --
876         if (l_found = TRUE) then
877           g_defbal_cache(p_defined_balance_id).start_rb_ptr := l_strt_position;
878           g_defbal_cache(p_defined_balance_id).end_rb_ptr   := l_position;
879         end if;
880 --
881       end;
882 --
883     end if;
884     if g_debug then
885        hr_utility.set_location ('pay_balance_pkg.load_defbal_cache', 15);
886     end if;
887 --
888     p_defbal_rec := g_defbal_cache(p_defined_balance_id);
889 --
890 end load_defbal_cache;
891 --
892 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
893 --
894 --                            chk_context                                 +
895 --                                                                        +
896 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
897 /*
898 NAME
899 
900         chk_context     -  Checks the passed context has an associated
901                            entry with the given route in the
902                            pay_route_context_usages.
903 
904 DESCRIPTION
905    This function will be called by the pay_balance_values_v view.
906 
907 */
908 function chk_context (p_context_id number,
909                       p_route_id   number) return varchar2
910 is
911   --
912   cursor csr_context_usages is
913      select 'Y'
914        from ff_route_context_usages frc
915       where frc.route_id = p_route_id
916         and frc.context_id = p_context_id;
917   --
918   l_temp varchar2(1);
919   --
920 begin
921  --
922  open csr_context_usages;
923  fetch csr_context_usages into l_temp;
924  close csr_context_usages;
925  --
926  return l_temp;
927  --
928 end chk_context;
929 --
930 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
931 --
932 --                            get_context                                 +
933 --                                                                        +
934 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
935 /*
936 NAME
937 
938         get_context     -  retrieve values of contexts set by set_contexts
939 
940 DESCRIPTION
941    This function may be called from the archiving process to determine the
942    values of contexts that have been set earlier in order to archive this
943    information.  The function passes in a context_id as a numebr and expects
944    a varchar2 as a return.
945 
946 */
947 function get_context_internal (p_context_name  varchar2) return varchar2
948 is
949 
950    v_con_value     varchar2(60);
951    v_context_found boolean;
952 
953 BEGIN
954    g_debug := hr_utility.debug_enabled;
955    if g_debug then
956       hr_utility.set_location ('pybaluex.get_context_internal',1);
957       hr_utility.trace(no_rows_con_tab);
958    end if;
959    v_context_found := FALSE;
960 
961    for i in 0..no_rows_con_tab-1 LOOP
962 
963       -- is this the context were looking for
964 
965       if con_name_tab(i) = p_context_name then
966          v_con_value:=con_value_tab(i); -- set return variable
967          v_context_found := TRUE; -- set found flag
968          if g_debug then
969             hr_utility.trace('i= '||i);
970             hr_utility.trace('name = '||con_name_tab(i));
971             hr_utility.trace('value = '||con_value_tab(i));
972          end if;
973          EXIT; -- drop out of loop
974      end if;
975    end loop;
976 --
977 if v_context_found = TRUE then
978    return v_con_value;
979 else
980    return NULL;
981 end if;
982 --
983 if g_debug then
984    hr_utility.set_location ('pybaluex.get_context_internal',3);
985 end if;
986 --
987 end get_context_internal;
988 --
989 function get_context (p_context_name  varchar2) return varchar2
990 is
991 
992    v_con_value     varchar2(60);
993    v_context_found boolean;
994 
995 BEGIN
996    g_debug := hr_utility.debug_enabled;
997    if g_debug then
998       hr_utility.set_location ('pybaluex.get_context',1);
999       hr_utility.trace(no_rows_con_tab);
1000    end if;
1001 
1002    v_con_value := get_context_internal(p_context_name);
1003 
1004    if (v_con_value is not null) then
1005      return v_con_value;
1006    else
1007      null;
1008    end if;
1009 --
1010    if g_debug then
1011       hr_utility.set_location ('pybaluex.get_context',3);
1012    end if;
1013 --
1014 end get_context;
1015 --
1016 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1017 --                                                                        +
1018 --                            set_context                                 +
1019 --                                                                        +
1020 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1021 --
1022 /*
1023 NAME
1024   set_context      - set up the run time contexts ready to run a balance or
1025                      database item.
1026 DESCRIPTION
1027   This routine must be called for each context that is used in the database
1028   item.  The context information is held in package level pl/sql tables which
1029   will remain current for the entire sql session.  These pl/sql tables are
1030   referred to by the balance and database item routines.
1031   Subsequent calls to this routine with the same context name will update the
1032   relevant row in the pl/sql tables.
1033   Since the context name is converted to upper case, the calling routine may
1034   pass the context name in either case.
1035 */
1036 procedure set_context
1037 (
1038     p_context_name   in varchar2,
1039     p_context_value  in varchar2
1040 ) is
1041 l_context_id      ff_contexts.context_id%type;
1042 l_context_name    ff_contexts.context_name%type;
1043 l_context_type    ff_contexts.data_type%type;
1044 l_count           binary_integer;
1045 l_context_found   boolean;
1046 begin
1047   g_debug := hr_utility.debug_enabled;
1048   if g_debug then
1049      hr_utility.set_location ('pay_balance_pkg.set_context', 10);
1050      hr_utility.trace('p_context_name  : ' || p_context_name);
1051      hr_utility.trace('p_context_value : ' || p_context_value);
1052   end if;
1053   l_context_found := FALSE;
1054   l_context_name := upper(p_context_name);
1055   --
1056   -- check to see if the context is present
1057   --
1061         --
1058   l_count := 0;
1059   while (l_count < no_rows_con_tab) loop
1060      if (con_name_tab (l_count) = l_context_name) then
1062         -- The context name is already present in the pl/sql tales, so update
1063         -- its value:
1064         --
1065         con_value_tab (l_count) := p_context_value;
1066         l_context_found := TRUE;
1067         exit;   -- exit while loop
1068     end if;
1069     l_count := l_count + 1;
1070   end loop;
1071   --
1072   if (l_context_found = FALSE) then
1073     --
1074     -- its a new context, insert into tables
1075     --
1076     if g_debug then
1077        hr_utility.set_location ('pay_balance_pkg.set_context', 20);
1078     end if;
1079     select context_id,
1080            data_type
1081     into   l_context_id,
1082            l_context_type
1083     from   ff_contexts
1084     where  context_name = l_context_name;
1085     --
1086     con_name_tab  (no_rows_con_tab) := ltrim(rtrim(l_context_name));
1087     con_id_tab    (no_rows_con_tab) := ltrim(rtrim(l_context_id));
1088     con_type_tab  (no_rows_con_tab) := ltrim(rtrim(l_context_type));
1089     con_value_tab (no_rows_con_tab) := ltrim(rtrim(p_context_value));
1090     --
1091     no_rows_con_tab := no_rows_con_tab +1;
1092   end if;
1093   --
1094   -- print out the current state of the tables for debug purposes:
1095   --
1096   l_count := 0;
1097     if g_debug then
1098        hr_utility.trace
1099        ('i  type  context id  context name           context value');
1100        hr_utility.trace
1101        ('-  ----  ----------  ------------           -------------');
1102      while (l_count < no_rows_con_tab) loop
1103        hr_utility.trace (rpad(to_char(l_count), 3)       ||
1104                          rpad(con_type_tab(l_count), 6)  ||
1105                          rpad(con_id_tab(l_count), 12)   ||
1106                          rpad(con_name_tab(l_count), 23) ||
1107                          con_value_tab(l_count));
1108        l_count := l_count + 1;
1109      end loop;
1110     end if;
1111 
1112     /*
1113         Store a flatened version for the latest balance fetch
1114     */
1115 
1116     if (l_context_name = 'TAX_UNIT_ID') then
1117        g_con_tax_unit_id := ltrim(rtrim(p_context_value));
1118     elsif (l_context_name = 'JURISDICTION_CODE') then
1119        g_con_jurisdiction_code := ltrim(rtrim(p_context_value));
1120     elsif (l_context_name = 'ORIGINAL_ENTRY_ID') then
1121        g_con_original_entry_id := ltrim(rtrim(p_context_value));
1122     elsif (l_context_name = 'SOURCE_ID') then
1123        g_con_source_id := ltrim(rtrim(p_context_value));
1124     elsif (l_context_name = 'SOURCE_TEXT') then
1125        g_con_source_text := ltrim(rtrim(p_context_value));
1126     elsif (l_context_name = 'SOURCE_TEXT2') then
1127        g_con_source_text2 := ltrim(rtrim(p_context_value));
1128     elsif (l_context_name = 'SOURCE_NUMBER') then
1129        g_con_source_number := ltrim(rtrim(p_context_value));
1130     elsif (l_context_name = 'TAX_GROUP') then
1131        g_con_tax_group := ltrim(rtrim(p_context_value));
1132     elsif (l_context_name = 'PAYROLL_ID') then
1133        g_con_payroll_id := ltrim(rtrim(p_context_value));
1134     elsif (l_context_name = 'LOCAL_UNIT_ID') then
1135        g_con_local_unit_id := ltrim(rtrim(p_context_value));
1136     elsif (l_context_name = 'ORGANIZATION_ID') then
1137        g_con_organization_id := ltrim(rtrim(p_context_value));
1138     elsif (l_context_name = 'SOURCE_NUMBER2') then
1139        g_con_source_number2 := ltrim(rtrim(p_context_value));
1140     end if;
1141 
1142 end set_context;
1143 --
1144 /*
1145 NAME
1146   set_context      - set up the run time contexts ready to run a balance or
1147                      database item.
1148 DESCRIPTION
1149    This route attempts to map legislative contexts to the Core Contexts.
1150 */
1151 procedure set_context
1152 (
1153     p_legislation_code in varchar2,
1154     p_context_name     in varchar2,
1155     p_context_value    in varchar2
1156 ) is
1157 --
1158 cnt   number;
1159 found boolean;
1160 l_context_name ff_contexts.context_name%type;
1161 --
1162 begin
1163 --
1164    -- Reset buffers if needed
1165 --
1166    if (p_legislation_code <> g_legislation_code
1167        or g_legislation_code is null) then
1168 --
1169      g_context_mapping.delete;
1170 --
1171    end if;
1172 --
1173    cnt := 1;
1174    found := FALSE;
1175    while (cnt <= g_context_mapping.count
1176          and found = FALSE)  loop
1177 --
1178      if (g_context_mapping(cnt).loc_context_name = p_context_name) then
1179        found := TRUE;
1180      else
1181        cnt := cnt + 1;
1182      end if;
1183 --
1184    end loop;
1185 --
1186    -- If the context name is not already in buffer then get it.
1187    if (found = FALSE) then
1188 --
1189      begin
1190        select fc.context_name
1191          into l_context_name
1192          from ff_contexts fc,
1193               pay_legislation_contexts plc
1194         where plc.legislation_name = p_context_name
1195           and plc.context_id = fc.context_id
1196           and plc.legislation_code = p_legislation_code;
1197 --
1198      exception
1199        when no_data_found then
1203      -- Place mapping in buffer
1200           l_context_name := p_context_name;
1201      end;
1202 --
1204      cnt := g_context_mapping.count + 1;
1205      g_context_mapping(cnt).core_context_name := l_context_name;
1206      g_context_mapping(cnt).loc_context_name  := p_context_name;
1207 --
1208    end if;
1209 --
1210    -- Now set the context
1211    set_context(g_context_mapping(cnt).core_context_name,
1212                p_context_value);
1213 --
1214 end set_context;
1215 --
1216 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1217 --                                                                        +
1218 --                              run_db_item                               +
1219 --                                                                        +
1220 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1221 --
1222 /*
1223 NAME
1224   run_db_item      - Retrieve a database item value.
1225 --
1226 DESCRIPTION
1227   This function is used to retrieve a database item value.  The sql to be run
1228   needs to be assembled from the information held in the entity horizon (in
1229   particular the tables ff_database_items and ff_routes).  Having retrieved
1230   the route (the part of the sql statement after the 'FROM' clause) into a
1231   large string, the context bind variables and where clause fillers need to be
1232   replaced.  The text string is then updated with the definition text
1233   information from ff_database_items (the part of the sql statement after
1234   the 'SELECT' and before the 'FROM').  The complete text string is then
1235   executed using dynamic pl/sql.
1236 */
1237 function run_db_item
1238 (
1239     p_database_name    in  varchar2,
1240     p_bus_group_id     in  number,
1241     p_legislation_code in  varchar2
1242 ) return varchar2 is
1243 p_db_output varchar2(240);
1244 --
1245 -- Cursor Declarations
1246 -- Used to retrieve the context bind variables for the route.
1247 --
1248 cursor ro_context (p_route_id  number)
1249 is
1250 select context_id,
1251        sequence_no
1252 from   ff_route_context_usages
1253 where  route_id = p_route_id
1254 order  by sequence_no;
1255 --
1256 -- Used to retrieve the where clause fillers for the route
1257 --
1258 cursor ro_wclause (p_user_entity_id   number,
1259                    p_route_id         number)
1260 is
1261 select RP.sequence_no,
1262        replace(RPV.value, '''', null) value
1263 from   ff_route_parameter_values    RPV
1264 ,      ff_route_parameters          RP
1265 where  RPV.user_entity_id         = p_user_entity_id
1266 and    RP.route_id                = p_route_id
1267 and    RPV.route_parameter_id     = RP.route_parameter_id
1268 order  by RP.sequence_no;
1269 --
1270 sql_cursor               integer;
1271 l_rows                   integer;
1272 l_definition_text        ff_database_items.definition_text%type;
1273 l_data_type              ff_database_items.data_type%type;
1274 l_user_entity_id         ff_user_entities.user_entity_id%type;
1275 l_creator_type           ff_user_entities.creator_type%type;
1276 l_notfound_allowed_flag  ff_user_entities.notfound_allowed_flag%type;
1277 l_route_id               ff_routes.route_id%type;
1278 l_context_name           ff_contexts.context_name%type;
1279 l_text                   varchar2(20000);     -- large array for route text
1280 l_replace_text           varchar2(80);
1281 l_error_text             varchar2(200);   -- used for sql error messages
1282 l_o_hint                 varchar2(2000); -- Route optimiser hint
1283 l_count                  number;
1284 l_context_found          boolean;
1285 l_ora_db_vers            number; -- db version number for LOW_VOLUME
1286 l_cxt_num                number;
1287 l_par_num                number;
1288 --
1289 ------------------------------- run_db_item -------------------------------
1290 --
1291 begin
1292   g_debug := hr_utility.debug_enabled;
1293   if g_debug then
1294      hr_utility.set_location ('pay_balance_pkg.run_db_item', 1);
1295      hr_utility.trace ('DB_item  = ' || p_database_name);
1296      hr_utility.trace ('bus_grp  = ' || to_char (p_bus_group_id));
1297      hr_utility.trace ('leg_code = ' || p_legislation_code);
1298   end if;
1299   --
1300   -- Get all the data to build up the route
1301   --
1302   select DBI.definition_text,
1303          DBI.data_type,
1304          ENT.user_entity_id,
1305          ENT.creator_type,
1306          ENT.notfound_allowed_flag,
1307          RO.route_id,
1308          RO.text,
1309          RO.optimizer_hint
1310   into   l_definition_text,
1311          l_data_type,
1312          l_user_entity_id,
1313          l_creator_type,
1314          l_notfound_allowed_flag,
1315          l_route_id,
1316          l_text,
1317          l_o_hint
1318   from   ff_database_items         DBI
1319   ,      ff_user_entities          ENT
1320   ,      ff_routes                 RO
1321   where  DBI.user_name           = p_database_name
1322   and    DBI.user_entity_id      = ENT.user_entity_id
1323   and    ( (ENT.legislation_code is null and ENT.business_group_id is null)
1324           or (ENT.business_group_id is null
1325               and p_legislation_code = ENT.legislation_code )
1326           or ENT.business_group_id + 0 = p_bus_group_id
1327          )
1328   and    ENT.route_id            = RO.route_id;
1329   --
1330   --
1334   --
1331   -- The following loop searches through and replaces all the bind variables
1332   -- (Bn) with the actual value for the context.  For a text value, the
1333   -- quotes also need to be inserted.
1335   -- Load the route context cache to avoid re-execution of the cursor
1336   -- when binding values to bind variables
1337   --
1338   route_contexts_cache.sz := 0;
1339   for c1rec in ro_context (l_route_id) loop
1340     route_contexts_cache.sz := route_contexts_cache.sz + 1;
1341     route_contexts_cache.cxt_id(route_contexts_cache.sz) := c1rec.context_id;
1342     l_count := 0;
1343     l_context_found := FALSE;
1344     while (l_count < no_rows_con_tab) loop
1345       if (con_id_tab(l_count) = c1rec.context_id) then
1346         --
1347         -- Found a bind variable
1348         --
1349         l_context_found := TRUE;
1350         l_replace_text := ':' || ltrim(rtrim(con_name_tab(l_count)));
1351         l_text := replace (l_text, '&B'||to_char(c1rec.sequence_no),
1352                                    l_replace_text);
1353         exit;  -- exit while loop
1354       end if;
1355       l_count := l_count + 1;
1356     end loop;  -- end of while loop
1357     if (l_context_found = FALSE) then
1358       --
1359       -- Raise an error, as there are contexts that have not been set up in
1360       -- the pl/sql tables that are required by the route.
1361       --
1362       if g_debug then
1363          hr_utility.set_location ('pay_balance_pkg.run_db_item', 10);
1364       end if;
1365       select context_name
1366       into   l_context_name
1367       from   ff_contexts
1368       where  context_id = c1rec.context_id;
1369       --
1370       hr_utility.set_message(801, 'HR_7271_PAY_CONTEXT_MISSING');
1371       hr_utility.set_message_token ('CONTEXT_NAME', l_context_name);
1372       hr_utility.raise_error;
1373     end if;
1374   end loop; -- end of for c1rec loop
1375   --
1376   -- The following loop searches through and replaces all where clause fillers
1377   -- (Un) with the value from ff_route_parameter_values.  For a text value,
1378   -- the quotes have already pre-inserted into this table.
1379   --
1380   -- Load the route parameters cache to avoid re-execution of the cursor
1381   -- when binding values to bind variables
1382   --
1383   route_parameters_cache.sz := 0;
1384   for c1rec in ro_wclause (l_user_entity_id, l_route_id) loop
1385     route_parameters_cache.sz := route_parameters_cache.sz + 1;
1386     route_parameters_cache.seq_no(route_parameters_cache.sz) := c1rec.sequence_no;
1387     route_parameters_cache.par_val(route_parameters_cache.sz) := c1rec.value;
1388     l_text := replace (l_text, '&U'||to_char(c1rec.sequence_no),
1389                                 ':U'||to_char(c1rec.sequence_no));
1390   end loop;
1391   --
1392   -- Print the route text out for debug purposes.  Normally keep this line
1393   -- commented out, as for a large route it causes an error in the trace
1394   -- utility.
1395   -- hr_utility.trace (l_text);
1396   --
1397   --
1398   -- now build up the full SQL statement:
1399   --
1400   if (l_data_type = 'D') then
1401     if (l_o_hint is null) then
1402          l_text := 'SELECT fnd_date.date_to_canonical(' ||
1403                    l_definition_text || ') FROM ' || l_text;
1404     else
1405          l_text := 'SELECT /*+ '||l_o_hint||
1406                    ' */ fnd_date.date_to_canonical(' ||
1407                    l_definition_text || ') FROM ' || l_text;
1408     end if;
1409   elsif (l_data_type = 'N') then
1410 
1411     if (l_o_hint is null) then
1412       --
1413       -- Use Rule hint on balances if LOW_VOLUME pay_action_paremeter set
1414       --
1415       if (l_creator_type = 'B') then
1416         -- balance dbitem
1417 
1418         if (cached = FALSE) then
1419           cached := TRUE;
1420           l_ora_db_vers := get_oracle_db_version;
1421           if (nvl(l_ora_db_vers, 0) < 9.0) then
1422              g_low_volume := 'Y';
1423           else
1424             begin
1425                select parameter_value
1426                into g_low_volume
1427                from pay_action_parameters
1428                where parameter_name = 'LOW_VOLUME';
1429             exception
1430               when others then
1431                  g_low_volume := 'N';
1432             end;
1433           end if;
1434         end if;
1435 
1436         if (g_low_volume = 'Y') then
1437           l_text := 'SELECT /*+ RULE*/ fnd_number.number_to_canonical(' ||
1438                     l_definition_text || ') FROM ' || l_text;
1439         else
1440           l_text := 'SELECT fnd_number.number_to_canonical(' ||
1441                     l_definition_text || ') FROM ' || l_text;
1442         end if;
1443       else
1444         -- Not a balance dbitem
1445         l_text := 'SELECT fnd_number.number_to_canonical(' ||
1446                   l_definition_text || ') FROM ' || l_text;
1447       end if;
1448     else -- Optimiser hint has bee supplied
1449        l_text := 'SELECT /*+ '||l_o_hint||
1450                  ' */ fnd_number.number_to_canonical(' ||
1451                   l_definition_text || ') FROM ' || l_text;
1452     end if;
1453 --
1454   else
1455     if (l_o_hint is null) then
1456       l_text := 'SELECT ' || l_definition_text || ' FROM ' || l_text;
1457     else
1458       l_text := 'SELECT /*+ ' ||l_o_hint||' */'||
1459                 l_definition_text || ' FROM ' || l_text;
1463   -- Now execute the SQL statement using dynamic pl/sql:
1460     end if;
1461   end if;
1462   --
1464   --
1465   -- Dynamic sql steps:
1466   -- ==================
1467   -- 1. Open dynamic sql cursor
1468   -- 2. Parse dynamic sql
1469   -- 3. bind variables
1470   -- 4. Define dynamic sql columns
1471   -- 5. Execute and fetch dynamic sql
1472   -- 6. Get the sql value (providing there are rows returned)
1473   -- 7. Close the dynamic sql cursor
1474   --
1475   if g_debug then
1476      hr_utility.set_location ('pay_balance_pkg.run_db_item', 15);
1477   end if;
1478   sql_cursor := dbms_sql.open_cursor;                             -- step 1
1479   --
1480   if g_debug then
1481      hr_utility.set_location ('pay_balance_pkg.run_db_item', 20);
1482   end if;
1483   dbms_sql.parse(sql_cursor, l_text, dbms_sql.v7);                -- step 2
1484   --
1485   --                                                              -- step 3
1486   -- At this stage we have parsed the route.  Now bind the
1487   -- variables, starting with the contexts (B values).
1488   --
1489   for l_cxt_num in 1..route_contexts_cache.sz loop
1490     l_count := 0;
1491     l_context_found := FALSE;
1492     while (l_count < no_rows_con_tab) loop
1493       if (con_id_tab(l_count) = route_contexts_cache.cxt_id(l_cxt_num)) then
1494         if g_debug then
1495            hr_utility.trace (con_name_tab (l_count) ||' = '||
1496                              con_value_tab (l_count));
1497         end if;
1498         --
1499         -- Found a bind variable
1500         --
1501         if (con_type_tab (l_count) = 'D') then
1502            dbms_sql.bind_variable (sql_cursor, con_name_tab  (l_count),
1503                                  fnd_date.canonical_to_date(ltrim(rtrim(con_value_tab (l_count)))));
1504         else
1505            dbms_sql.bind_variable (sql_cursor, con_name_tab  (l_count),
1506                                  ltrim(rtrim(con_value_tab (l_count))));
1507         end if;
1508         l_context_found := TRUE;
1509         exit;  -- exit while loop
1510       end if;
1511       l_count := l_count + 1;
1512     end loop;  -- end of while loop
1513     if (l_context_found = FALSE) then
1514       --
1515       -- Raise an error, as there are contexts that have not been set up in
1516       -- the pl/sql tables that are required by the route.
1517       --
1518       if g_debug then
1519          hr_utility.set_location ('pay_balance_pkg.run_db_item', 101);
1520       end if;
1521       select context_name
1522       into   l_context_name
1523       from   ff_contexts
1524       where  context_id = route_contexts_cache.cxt_id(l_cxt_num);
1525       --
1526       hr_utility.set_message(801, 'HR_7271_PAY_CONTEXT_MISSING');
1527       hr_utility.set_message_token ('CONTEXT_NAME', l_context_name);
1528       hr_utility.raise_error;
1529     end if;
1530   end loop; -- end of for route contexts loop
1531   --
1532   -- Now bind the where clause fillers (the 'U' values)
1533   --
1534   for l_par_num in 1..route_parameters_cache.sz loop
1535     dbms_sql.bind_variable (sql_cursor, 'U'||to_char(route_parameters_cache.seq_no(l_par_num)),
1536                                         route_parameters_cache.par_val(l_par_num));
1537   end loop;
1538   --
1539   if g_debug then
1540      hr_utility.set_location ('pay_balance_pkg.run_db_item', 25);
1541   end if;
1542   dbms_sql.define_column (sql_cursor, 1, p_db_output, 240);        -- step 4
1543   --
1544   if g_debug then
1545      hr_utility.set_location ('pay_balance_pkg.run_db_item', 30);
1546   end if;
1547   l_rows := dbms_sql.execute_and_fetch (sql_cursor, false);       -- step 5
1548   --
1549   if (l_rows = 1) then
1550     if g_debug then
1551        hr_utility.set_location ('pay_balance_pkg.run_db_item', 35);
1552     end if;
1553     dbms_sql.column_value (sql_cursor, 1, p_db_output);           -- step 6
1554     --
1555     if g_debug then
1556        hr_utility.set_location ('pay_balance_pkg.run_db_item', 40);
1557     end if;
1558     dbms_sql.close_cursor(sql_cursor);                            -- step 7
1559     --
1560     if g_debug then
1561        hr_utility.trace ('DB value = ' || p_db_output);
1562     end if;
1563     --
1564   elsif (l_rows = 0) then
1565     dbms_sql.close_cursor(sql_cursor);
1566     if (l_notfound_allowed_flag = 'Y') then
1567       --
1568       -- its ok to not find a row
1569       --
1570       if g_debug then
1571          hr_utility.set_location ('pay_balance_pkg.run_db_item', 45);
1572       end if;
1573       p_db_output := null;
1574     else
1575       --
1576       -- Error, no rows found when the entity has been defined to always
1577       -- find a row.
1578       --
1579       hr_utility.set_message(801, 'HR_7272_PAY_NO_ROWS_RETURNED');
1580       hr_utility.set_message_token ('DATABASE_NAME', p_database_name);
1581       hr_utility.raise_error;
1582     end if;
1583   else
1584     --
1585     -- More than 1 row have been returned. We must error as DB items can
1586     -- only return 1 row.
1587     --
1588     dbms_sql.close_cursor(sql_cursor);
1589     hr_utility.set_message(801, 'HR_7273_PAY_MORE_THAN_1_ROW');
1590     hr_utility.raise_error;
1591   end if;
1592   return p_db_output;
1593 exception
1594   --
1595   -- If any other Oracle Error is trapped (e.g. during parse, execute,
1599   When Others Then
1596   -- fetch etc), then we must check to see if the cursor is still open
1597   -- and close down if necessary.
1598   --
1600     l_error_text := sqlerrm;
1601     if g_debug then
1602        Hr_Utility.Set_Location('run_db_item', 100);
1603     end if;
1604     If (dbms_sql.is_open(sql_cursor)) then
1605       if g_debug then
1606          Hr_Utility.Set_Location('run_db_item', 105);
1607       end if;
1608       dbms_sql.close_cursor(sql_cursor);
1609     End If;
1610     hr_utility.set_message(801, 'HR_7276_PAY_FAILED_DB_ITEM');
1611     hr_utility.set_message_token ('ERROR_MESSAGE', l_error_text);
1612     hr_utility.raise_error;
1613 end run_db_item;
1614 --
1615 --
1616 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1617 --
1618 --                            generate_rr_statem                         +
1619 --                                                                        +
1620 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1621 /*
1622 NAME
1623 
1624         generate_rr_statem  -  This creates the Run Result Balance
1625                                 Statement
1626 
1627 DESCRIPTION
1628 
1629       This generates the balance statement to be used to retrieve values.
1630 
1631 */
1632 procedure generate_rr_statem(
1633                        p_batch_mode          in            boolean,
1634                        p_balance_type_id     in            number,
1635                        p_balance_list        in            t_batch_list,
1636                        p_balance_type_column in            varchar2,
1637                        p_retrieval_column    in            varchar2,
1638                        p_decode_required     in            boolean,
1639                        p_from_clause         in            varchar2,
1640                        p_o_hint              in            varchar2,
1641                        p_bal_ptr             in out nocopy number,
1642                        p_statement              out nocopy varchar2
1643                       )
1644 is
1645 --
1646 --  Do not change the length of these without changing MAX_DYN_SQL size.
1647 --
1648 l_select_clause    varchar2(20000);     -- large array for route text
1649 l_from_clause      varchar2(20000);     -- large array for route text
1650 l_select_component varchar2(20000);     -- large array for route text
1651 l_from_component   varchar2(20000);     -- large array for route text
1652 cnt                number;
1653 l_string_full      boolean;
1654 l_from_length      number;
1655 l_comp_length      number;
1656 
1657 begin
1658   --
1659   -- now build up the full SQL statement:
1660   --
1661   -- Select portion of the statement up
1662   --
1663   if (p_retrieval_column is null) then
1664     l_select_clause := 'TARGET.result_value';
1665   else
1666     l_select_clause := p_retrieval_column;
1667   end if;
1668 --
1669   l_select_clause := 'nvl(sum(fnd_number.canonical_to_number('||l_select_clause||') * FEED.scale),0)';
1670   l_from_clause := ' FROM '||p_from_clause;
1671   --
1672   -- Setup the Select and optimiser
1673   --
1674   if (p_o_hint is null) then
1675     if (g_low_volume = 'Y') then
1676       l_select_clause := 'SELECT /*+ RULE*/ fnd_number.number_to_canonical(' ||
1677                          l_select_clause || ')';
1678     else
1679       l_select_clause := 'SELECT fnd_number.number_to_canonical(' ||
1680                          l_select_clause || ')';
1681     end if;
1682   else
1683     l_select_clause := 'SELECT /*+ '||p_o_hint||
1684                        ' */ fnd_number.number_to_canonical(' ||
1685                        l_select_clause || ')';
1686   end if;
1687 --
1688   if g_debug then
1689     hr_utility.trace(l_select_clause);
1690   end if;
1691 --
1692   --
1693   -- Now setup the balance type joining details.
1694   --
1695   if (p_batch_mode = TRUE) then
1696     if (p_balance_type_column is null) then
1697       hr_general.assert_condition(false);
1698     else
1699       declare
1700       l_first_bt boolean;
1701       begin
1702 --
1703         l_select_component := ', '||p_balance_type_column;
1704         if g_debug then
1705           hr_utility.trace(l_select_component);
1706         end if;
1707 --
1708         l_select_clause := l_select_clause||l_select_component;
1709         l_from_clause := l_from_clause||' and '||p_balance_type_column||' in (';
1710         if g_debug then
1711           if length(l_from_clause) <= MAX_TRACE_SIZE then
1712             hr_utility.trace(l_from_clause);
1713           end if;
1714         end if;
1715 --
1716         -- Now put the full statement in the from clause
1717         l_from_clause := l_select_clause||l_from_clause;
1718 --
1719         l_first_bt := TRUE;
1720 --
1721         -- Loop through all the entries in the balance list
1722         -- (or as many as we can)
1723         -- adding them to the statement form batch mode.
1724 --
1725         l_string_full := FALSE;
1726         cnt := p_bal_ptr; -- Start from where we left last time
1727         while (    l_string_full = FALSE
1728                and cnt <= p_balance_list.count) loop
1729 --
1730           if (l_first_bt = TRUE) then
1731 --
1732             l_first_bt := FALSE;
1733             l_from_component := '';
1734 --
1735           else
1736 --
1740 --
1737             l_from_component := ', ';
1738 --
1739           end if;
1741           l_from_component := l_from_component||p_balance_list(cnt).balance_type_id;
1742 --
1743           if(p_decode_required = TRUE) then
1744              l_from_component := l_from_component||' + decode(TARGET.input_value_id, 0, 0, 0)';
1745           end if;
1746 --
1747           -- Only add to the statement if there is space
1748 --
1749           l_from_length := length(l_from_clause);
1750           l_comp_length := length(l_from_component);
1751 --
1752           if ((l_from_length + l_comp_length) <
1753               (MAX_DYN_SQL_SIZE - 50)) then
1754 --
1755             if g_debug then
1756               if l_comp_length <= MAX_TRACE_SIZE then
1757                 hr_utility.trace(l_from_component);
1758               end if;
1759             end if;
1760             l_from_clause := l_from_clause||l_from_component;
1761 --
1762           else
1763 --
1764             l_string_full:= TRUE;
1765 --
1766           end if;
1767 --
1768           cnt := cnt + 1; -- increment pointer
1769 --
1770         end loop;
1771 --
1772         -- Now set the pointer
1773         p_bal_ptr := cnt;
1774 --
1775         l_from_component := ' ) group by '||p_balance_type_column;
1776         if g_debug then
1777           if length(l_from_component) <= MAX_TRACE_SIZE then
1778             hr_utility.trace(l_from_component);
1779           end if;
1780         end if;
1781         l_from_clause := l_from_clause||l_from_component;
1782 --
1783       end;
1784     end if;
1785   else
1786     if (p_balance_type_column is null) then
1787 --
1788        if g_debug then
1789          if length(l_from_clause) <= MAX_TRACE_SIZE then
1790            hr_utility.trace(l_from_clause);
1791          end if;
1792        end if;
1793 --
1794        -- Now put the full statement in the from clause
1795        l_from_clause := l_select_clause||l_from_clause;
1796 --
1797     else
1798        --
1799        -- As above, assume that there is one route parameter.
1800        --
1801 --
1802        if g_debug then
1803          if length(l_from_clause) <= MAX_TRACE_SIZE then
1804            hr_utility.trace(l_from_clause);
1805          end if;
1806        end if;
1807 --
1808        -- Now put the full statement in the from clause
1809        l_from_clause := l_select_clause||l_from_clause;
1810 --
1811        l_from_component := ' and '||p_balance_type_column||' = :U1';
1812        if g_debug then
1813          if length(l_from_component) <= MAX_TRACE_SIZE then
1814            hr_utility.trace(l_from_component);
1815          end if;
1816        end if;
1817        l_from_clause := l_from_clause||l_from_component;
1818 --
1819        if (p_decode_required = TRUE) then
1820           l_from_component := ' + decode(TARGET.input_value_id, 0, 0, 0)';
1821           if g_debug then
1822             if length(l_from_component) <= MAX_TRACE_SIZE then
1823               hr_utility.trace(l_from_component);
1824             end if;
1825           end if;
1826           l_from_clause := l_from_clause||l_from_component;
1827        end if;
1828 --
1829     end if;
1830   end if;
1831 --
1832   -- Now set the output statement
1833 --
1834   p_statement := l_from_clause;
1835 --
1836 end generate_rr_statem;
1837 
1838 --
1839 --
1840 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1841 --
1842 --                            process_balance_statement                   +
1843 --                                                                        +
1844 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1845 /*
1846 NAME
1847 
1848         process_balance_statement  -  Dynamically run the Balance
1849                                       Statement.
1850 
1851 DESCRIPTION
1852 
1853     This dynamically runs the supplied balance statement then sets the
1854     appropreate results.
1855 
1856 */
1857 procedure process_balance_statement(
1858                        p_batch_mode          in            boolean,
1859                        p_statement           in            varchar2,
1860                        p_route_id            in            number,
1861                        p_jur_level_required  in            boolean,
1862                        p_jur_lvl             in            number,
1863                        p_balance_type_id     in            number,
1864                        p_batch_str_ptr       in            number,
1865                        p_batch_end_ptr       in            number,
1866                        p_balance_list        in out nocopy t_batch_list,
1867                        p_balance_value          out nocopy number
1868                       )
1869 is
1870 --
1871 -- Cursor Declarations
1872 -- Used to retrieve the context bind variables for the route.
1873 --
1874 cursor ro_context (p_route_id  number)
1875 is
1876 select context_id,
1877        sequence_no
1878 from   ff_route_context_usages
1879 where  route_id = p_route_id
1880 order  by sequence_no;
1881 --
1882 sql_cursor               integer;
1883 l_rows                   integer;
1884 l_count                  number;
1885 l_context_found          boolean;
1886 l_context_name           ff_contexts.context_name%type;
1890 l_value_retrieved        boolean;
1887 l_balance_type_id        number;
1888 ignore                   number;
1889 l_retrieve               boolean;
1891 l_db_output              varchar(60);
1892 l_error_text             varchar2(200);       -- used for sql error messages
1893 begin
1894   --
1895   -- Now execute the SQL statement using dynamic pl/sql:
1896   --
1897   -- Dynamic sql steps:
1898   -- ==================
1899   -- 1. Open dynamic sql cursor
1900   -- 2. Parse dynamic sql
1901   -- 3. bind variables
1902   -- 4. Define dynamic sql columns
1903   -- 5. Execute and fetch dynamic sql
1904   -- 6. Get the sql value (providing there are rows returned)
1905   -- 7. Close the dynamic sql cursor
1906   --
1907   if g_debug then
1908      hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 35);
1909   end if;
1910   sql_cursor := dbms_sql.open_cursor;                             -- step 1
1911   --
1912   if g_debug then
1913      hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 40);
1914   end if;
1915   dbms_sql.parse(sql_cursor, p_statement, dbms_sql.v7);                -- step 2
1916   --
1917   --                                                              -- step 3
1918   -- At this stage we have parsed the route.  Now bind the
1919   -- variables, starting with the contexts (B values).
1920   --
1921   for c1rec in ro_context (p_route_id) loop
1922     if g_debug then
1923        hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 45);
1924     end if;
1925     l_count := 0;
1926     l_context_found := FALSE;
1927     while (l_count < no_rows_con_tab) loop
1928       if (con_id_tab(l_count) = c1rec.context_id) then
1929         if g_debug then
1930            hr_utility.trace (con_name_tab (l_count) ||' = '||
1931                              con_value_tab (l_count));
1932         end if;
1933         --
1934         -- Found a bind variable
1935         --
1936         if (con_type_tab (l_count) = 'D') then
1937           if g_debug then
1938              hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 50);
1939           end if;
1940           dbms_sql.bind_variable
1941            (sql_cursor,
1942             con_name_tab(l_count),
1943             fnd_date.canonical_to_date(ltrim(rtrim(con_value_tab (l_count)))));
1944         else
1945           if g_debug then
1946              hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 55);
1947           end if;
1948           dbms_sql.bind_variable
1949            (sql_cursor,
1950             con_name_tab(l_count),
1951             ltrim(rtrim(con_value_tab (l_count))));
1952         end if;
1953         l_context_found := TRUE;
1954         exit;  -- exit while loop
1955       end if;
1956       l_count := l_count + 1;
1957     end loop;  -- end of while loop
1958     if (l_context_found = FALSE) then
1959       --
1960       -- Raise an error, as there are contexts that have not been set up in
1961       -- the pl/sql tables that are required by the route.
1962       --
1963       if g_debug then
1964          hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 60);
1965       end if;
1966       select context_name
1967       into   l_context_name
1968       from   ff_contexts
1969       where  context_id = c1rec.context_id;
1970       --
1971       hr_utility.set_message(801, 'HR_7271_PAY_CONTEXT_MISSING');
1972       hr_utility.set_message_token ('CONTEXT_NAME', l_context_name);
1973       hr_utility.raise_error;
1974     end if;
1975   end loop; -- end of for c1rec loop
1976   --
1977   -- Now bind the where clause fillers (the 'U' values)
1978   --
1979   if (p_batch_mode = FALSE) then
1980     if g_debug then
1981        hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 62);
1982        hr_utility.trace('p_balance_type_id = '||p_balance_type_id);
1983     end if;
1984     dbms_sql.bind_variable (sql_cursor, 'U1',
1985                                         p_balance_type_id);
1986   end if;
1987 --
1988   if (p_jur_level_required = TRUE) then
1989     dbms_sql.bind_variable (sql_cursor, 'JURISDICTION_LEVEL',
1990                                         p_jur_lvl);
1991   end if;
1992   --
1993   if g_debug then
1994      hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 65);
1995   end if;
1996   dbms_sql.define_column (sql_cursor, 1, l_db_output, 60);        -- step 4
1997   if (p_batch_mode  = TRUE) then
1998      dbms_sql.define_column (sql_cursor, 2, l_balance_type_id);
1999   end if;
2000   --
2001   if g_debug then
2002      hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 70);
2003   end if;
2004   ignore := dbms_sql.execute(sql_cursor);
2005   --
2006   l_retrieve := TRUE;
2007   l_value_retrieved := FALSE;
2008   while (l_retrieve = TRUE) loop
2009     --
2010     l_rows := dbms_sql.fetch_rows(sql_cursor);
2011     --
2012     if g_debug then
2013        hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 80);
2014     end if;
2015     --
2016     if (l_rows > 0) then
2017       --
2018       if g_debug then
2019          hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 85);
2020       end if;
2021       dbms_sql.column_value (sql_cursor, 1, l_db_output);
2022       if (p_batch_mode  = TRUE) then
2023         dbms_sql.column_value (sql_cursor, 2, l_balance_type_id);
2024         --
2028           if (p_balance_list(cnt).balance_type_id = l_balance_type_id) then
2025         -- Search batch to populate value.
2026         --
2027         for cnt in p_batch_str_ptr..p_batch_end_ptr loop
2029              p_balance_list(cnt).balance_value :=
2030                         fnd_number.canonical_to_number(l_db_output);
2031              if g_debug then
2032                 hr_utility.trace('****Bal = '||l_balance_type_id||' Value = '||
2033                                  l_db_output);
2034              end if;
2035           end if;
2036         end loop;
2037         --
2038       else
2039         --
2040         -- Have we already got a value
2041         --
2042         if g_debug then
2043            hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 87);
2044         end if;
2045         if (l_value_retrieved = TRUE) then
2046           if g_debug then
2047              hr_utility.set_location ('pay_balance_pkg.process_balance_statement', 90);
2048           end if;
2049           dbms_sql.close_cursor(sql_cursor);
2050           hr_utility.set_message(801, 'HR_7273_PAY_MORE_THAN_1_ROW');
2051           hr_utility.raise_error;
2052         end if;
2053         if g_debug then
2054            hr_utility.trace('Value = '||l_db_output);
2055         end if;
2056         p_balance_value := fnd_number.canonical_to_number(l_db_output);
2057         --
2058       end if;
2059       --
2060       l_value_retrieved := TRUE;
2061       --
2062     else
2063       l_retrieve := FALSE;
2064     end if;
2065   end loop;
2066   dbms_sql.close_cursor(sql_cursor);
2067   --
2068   if (l_value_retrieved = FALSE and p_batch_mode  = FALSE) then
2069     --
2070     -- Error, no rows found when the entity has been defined to always
2071     -- find a row.
2072     --
2073     hr_general.assert_condition(false);
2074   end if;
2075   --
2076 exception
2077   --
2078   -- If any other Oracle Error is trapped (e.g. during parse, execute,
2079   -- fetch etc), then we must check to see if the cursor is still open
2080   -- and close down if necessary.
2081   --
2082   When Others Then
2083     l_error_text := sqlerrm;
2084     if g_debug then
2085        Hr_Utility.Set_Location('pay_balance_pkg.process_balance_statement', 100);
2086     end if;
2087     If (dbms_sql.is_open(sql_cursor)) then
2088       if g_debug then
2089          Hr_Utility.Set_Location('pay_balance_pkg.process_balance_statement', 105);
2090       end if;
2091       dbms_sql.close_cursor(sql_cursor);
2092     End If;
2093     hr_utility.set_message(801, 'HR_7276_PAY_FAILED_DB_ITEM');
2094     hr_utility.set_message_token ('ERROR_MESSAGE', l_error_text);
2095     hr_utility.raise_error;
2096     --
2097 end process_balance_statement;
2098 --
2099 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2100 --
2101 --                            run_rr_route                                +
2102 --                                                                        +
2103 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2104 /*
2105 NAME
2106 
2107         run_rr_route  -  Run Run Result Route to get balance value.
2108 
2109 DESCRIPTION
2110 
2111    This procedure is used to retrieve balance values both in batch and
2112    single mode and runs the run result route from the dimension routes
2113    table. Note it is not used to retrieve the route attached to the
2114    balance dimension itself, that is done via run_db_item.
2115 
2116 */
2117 procedure run_rr_route(
2118                        p_batch_mode          in            boolean,
2119                        p_balance_type_id     in            number,
2120                        p_balance_list        in out nocopy t_batch_list,
2121                        p_route_id            in            number,
2122                        p_balance_type_column in            varchar2,
2123                        p_retrieval_column    in            varchar2,
2124                        p_decode_required     in            boolean,
2125                        p_jur_lvl             in            number,
2126                        p_balance_value          out nocopy number
2127                       )
2128 is
2129 -- Cursor Declarations
2130 -- Used to retrieve the context bind variables for the route.
2131 --
2132 cursor ro_context (p_route_id  number)
2133 is
2134 select context_id,
2135        sequence_no
2136 from   ff_route_context_usages
2137 where  route_id = p_route_id
2138 order  by sequence_no;
2139 --
2140 --
2141 l_context_name           ff_contexts.context_name%type;
2142 --
2143 --  Do not change the length of these without changing MAX_DYN_SQL size.
2144 --
2145 l_from_clause            varchar2(20000);     -- large array for route text
2146 l_o_hint                 varchar2(2000);      -- large array for optimiser hint
2147 l_statement              varchar2(20000);     -- large array for route text
2148 l_replace_text           varchar2(80);
2149 l_count                  number;
2150 l_context_found          boolean;
2151 l_jur_level_required     boolean;
2152 l_ora_db_vers            number; -- db version number for LOW_VOLUME
2153 l_bal_ptr                number;
2154 l_start_ptr              number;
2155 --
2156 begin
2157   if g_debug then
2161   -- If we are in batch mode then initialise the returns
2158      hr_utility.set_location ('pay_balance_pkg.run_rr_route', 1);
2159   end if;
2160   --
2162   --
2163   for cnt in 1..p_balance_list.count loop
2164     p_balance_list(cnt).balance_value := 0;
2165   end loop;
2166   --
2167   -- Get all the data to build up the route
2168   -- select definition and data type are hard coded for now
2169   --
2170   select RO.text,
2171          RO.optimizer_hint
2172     into l_from_clause,
2173          l_o_hint
2174     from ff_routes                 RO
2175    where RO.route_id = p_route_id;
2176   --
2177   -- The following loop searches through and replaces all the bind variables
2178   -- (Bn) with the actual value for the context.  For a text value, the
2179   -- quotes also need to be inserted.
2180   --
2181   for c1rec in ro_context (p_route_id) loop
2182     l_count := 0;
2183     l_context_found := FALSE;
2184     while (l_count < no_rows_con_tab) loop
2185       if (con_id_tab(l_count) = c1rec.context_id) then
2186         --
2187         -- Found a bind variable
2188         --
2189         l_context_found := TRUE;
2190         l_replace_text := ':' || ltrim(rtrim(con_name_tab(l_count)));
2191         l_from_clause := replace (l_from_clause, '&B'||to_char(c1rec.sequence_no),
2192                                    l_replace_text);
2193         exit;  -- exit while loop
2194       end if;
2195       l_count := l_count + 1;
2196     end loop;  -- end of while loop
2197     if (l_context_found = FALSE) then
2198       --
2199       -- Raise an error, as there are contexts that have not been set up in
2200       -- the pl/sql tables that are required by the route.
2201       --
2202       if g_debug then
2203          hr_utility.set_location ('pay_balance_pkg.run_rr_route', 10);
2204       end if;
2205       select context_name
2206       into   l_context_name
2207       from   ff_contexts
2208       where  context_id = c1rec.context_id;
2209       --
2210       hr_utility.set_message(801, 'HR_7271_PAY_CONTEXT_MISSING');
2211       hr_utility.set_message_token ('CONTEXT_NAME', l_context_name);
2212       hr_utility.raise_error;
2213     end if;
2214   end loop; -- end of for c1rec loop
2215   --
2216   -- The following loop searches through and replaces all where clause fillers
2217   -- (Un) with the value from ff_route_parameter_values.
2218   -- NOTE. This is a run result route, assume that only balance type
2219   -- is the parameter and only in non batch mode.
2220   --
2221   if (p_balance_type_column is null) then
2222     l_from_clause := replace (l_from_clause, '&U1',
2223                                 ':U1');
2224   end if;
2225   --
2226   -- Print the route text out for debug purposes.  Normally keep this line
2227   -- commented out, as for a large route it causes an error in the trace
2228   -- utility.
2229   -- hr_utility.trace (l_from_clause);
2230   --
2231   --
2232   -- Get action Parameter.
2233   --
2234   if (cached = FALSE) then
2235     cached := TRUE;
2236     if g_debug then
2237        hr_utility.set_location('pay_balance_pkg.run_rr_route', 20);
2238     end if;
2239     l_ora_db_vers := get_oracle_db_version;
2240     if (nvl(l_ora_db_vers, 0) < 9.0) then
2241        g_low_volume := 'Y';
2242     else
2243       begin
2244         select parameter_value
2245         into g_low_volume
2246         from pay_action_parameters
2247         where parameter_name = 'LOW_VOLUME';
2248       exception
2249         when others then
2250            g_low_volume := 'N';
2251       end;
2252     end if;
2253   end if;
2254 --
2255   -- Set up the Jurisdiction level flag.
2256   l_jur_level_required := FALSE;
2257   if (instr(l_from_clause, ':JURISDICTION_LEVEL') <> 0) then
2258      l_jur_level_required := TRUE;
2259   end if;
2260 --
2261   -- If batch mode is false then we know that we are only
2262   -- retrieving a single value. However in batch mode
2263   -- it may take several passes to retrieve all the
2264   -- values.
2265 --
2266   if (p_batch_mode = TRUE) then
2267 --
2268     l_bal_ptr := 1;
2269 --
2270     while (l_bal_ptr <= p_balance_list.count) loop
2271 --
2272       l_start_ptr := l_bal_ptr;
2273 --
2274       -- Now call the procedure that builds the SQL statement
2275 --
2276       generate_rr_statem(p_batch_mode,
2277                           p_balance_type_id,
2278                           p_balance_list,
2279                           p_balance_type_column,
2280                           p_retrieval_column,
2281                           p_decode_required,
2282                           l_from_clause,
2283                           l_o_hint,
2284                           l_bal_ptr,
2285                           l_statement
2286                          );
2287 --
2288       -- Now get the balances
2289 --
2290       process_balance_statement(
2291                            p_batch_mode,
2292                            l_statement,
2293                            p_route_id,
2294                            l_jur_level_required,
2295                            p_jur_lvl,
2296                            p_balance_type_id,
2297                            l_start_ptr,
2298                            l_bal_ptr - 1, -- its set to the next position.
2299                            p_balance_list,
2300                            p_balance_value
2301                           );
2305     -- Now call the procedure that builds the SQL statement
2302     end loop;
2303   else
2304 --
2306 --
2307     generate_rr_statem(p_batch_mode,
2308                         p_balance_type_id,
2309                         p_balance_list,
2310                         p_balance_type_column,
2311                         p_retrieval_column,
2312                         p_decode_required,
2313                         l_from_clause,
2314                         l_o_hint,
2315                         l_bal_ptr,
2316                         l_statement
2317                        );
2318 --
2319     -- Now get the balances
2320 --
2321     process_balance_statement(
2322                          p_batch_mode,
2323                          l_statement,
2324                          p_route_id,
2325                          l_jur_level_required,
2326                          p_jur_lvl,
2327                          p_balance_type_id,
2328                          1,
2329                          1,
2330                          p_balance_list,
2331                          p_balance_value
2332                         );
2333   end if;
2334 --
2335 end run_rr_route;
2336 ---------------------------------------------------------------------------
2337 -- function get_run_balance
2338 ---------------------------------------------------------------------------
2339 function get_run_balance
2340 (p_user_name         in varchar2
2341 ,p_business_group_id in number
2342 ,p_legislation_code  in varchar2
2343 ,p_route_type        in varchar2
2344 ) return varchar2
2345 is
2346 p_db_output varchar2(80);
2347 --
2348 -- Cursor Declarations
2349 -- Used to retrieve the context bind variables for the route.
2350 --
2351 cursor ro_context (p_route_id  number)
2352 is
2353 select context_id,
2354        sequence_no
2355 from   ff_route_context_usages
2356 where  route_id = p_route_id
2357 order  by sequence_no;
2358 --
2359 -- Used to retrieve the where clause fillers for the route
2360 --
2361 cursor ro_wclause (p_user_entity_id   number,
2362                    p_route_id         number)
2363 is
2364 select RP.sequence_no,
2365        RPV.value
2366 from   ff_route_parameter_values    RPV
2367 ,      ff_route_parameters          RP
2368 where  RPV.user_entity_id         = p_user_entity_id
2369 and    RP.route_id                = p_route_id
2370 and    RPV.route_parameter_id     = RP.route_parameter_id
2371 order  by RP.sequence_no;
2372 --
2373 sql_cursor               integer;
2374 l_rows                   integer;
2375 l_definition_text        varchar2(240);
2376 --l_data_type              ff_database_items.data_type%type;
2377 l_data_type              varchar2(1) := 'N';
2378 l_user_entity_id         ff_user_entities.user_entity_id%type;
2379 l_creator_type           ff_user_entities.creator_type%type;
2380 l_notfound_allowed_flag  ff_user_entities.notfound_allowed_flag%type;
2381 l_route_id               ff_routes.route_id%type;
2382 l_context_name           ff_contexts.context_name%type;
2383 l_text                   varchar2(20000);     -- large array for route text
2384 l_replace_text           varchar2(80);
2385 l_error_text             varchar2(200);       -- used for sql error messages
2386 l_o_hint                 varchar2(2000);      -- optimiser hint
2387 l_count                  number;
2388 l_context_found          boolean;
2389 l_cxt_num                number;
2390 l_par_num                number;
2391 --
2392 BEGIN
2393   if g_debug then
2394      hr_utility.set_location ('pay_balance_pkg.get_run_balance', 1);
2395      hr_utility.trace ('User name: '|| p_user_name);
2396      hr_utility.trace ('bus_grp: '  || to_char (p_business_group_id));
2397      hr_utility.trace ('leg_code: ' || p_legislation_code);
2398   end if;
2399   --
2400   -- Get all the data to build up the route
2401   -- select definition and data type are hard coded for now
2402   --
2403   select ENT.user_entity_id,
2404          ENT.creator_type,
2405          ENT.notfound_allowed_flag,
2406          RO.route_id,
2407          RO.text,
2408          RO.optimizer_hint
2409   into   l_user_entity_id,
2410          l_creator_type,
2411          l_notfound_allowed_flag,
2412          l_route_id,
2413          l_text,
2414          l_o_hint
2415   from   ff_user_entities          ENT
2416   ,      ff_routes                 RO
2417   where  ent.user_entity_name = p_user_name
2418   and    ( (ENT.legislation_code is null and ENT.business_group_id is null)
2419           or (ENT.business_group_id is null
2420               and p_legislation_code = ENT.legislation_code )
2421           or ENT.business_group_id + 0 = p_business_group_id
2422          )
2423   and    ENT.route_id            = RO.route_id;
2424   --
2425   -- The following loop searches through and replaces all the bind variables
2426   -- (Bn) with the actual value for the context.  For a text value, the
2427   -- quotes also need to be inserted.
2428   --
2429   -- Load the route context cache to avoid re-execution of the cursor
2430   -- when binding values to bind variables
2431   --
2432   route_contexts_cache.sz := 0;
2433   for c1rec in ro_context (l_route_id) loop
2434     route_contexts_cache.sz := route_contexts_cache.sz + 1;
2435     route_contexts_cache.cxt_id(route_contexts_cache.sz) := c1rec.context_id;
2436     l_count := 0;
2440         --
2437     l_context_found := FALSE;
2438     while (l_count < no_rows_con_tab) loop
2439       if (con_id_tab(l_count) = c1rec.context_id) then
2441         -- Found a bind variable
2442         --
2443         l_context_found := TRUE;
2444         l_replace_text := ':' || ltrim(rtrim(con_name_tab(l_count)));
2445         l_text := replace (l_text, '&B'||to_char(c1rec.sequence_no),
2446                                    l_replace_text);
2447         exit;  -- exit while loop
2448       end if;
2449       l_count := l_count + 1;
2450     end loop;  -- end of while loop
2451     if (l_context_found = FALSE) then
2452       --
2453       -- Raise an error, as there are contexts that have not been set up in
2454       -- the pl/sql tables that are required by the route.
2455       --
2456       if g_debug then
2457          hr_utility.set_location ('pay_balance_pkg.get_run_balance', 10);
2458       end if;
2459       select context_name
2460       into   l_context_name
2461       from   ff_contexts
2462       where  context_id = c1rec.context_id;
2463       --
2464       hr_utility.set_message(801, 'HR_7271_PAY_CONTEXT_MISSING');
2465       hr_utility.set_message_token ('CONTEXT_NAME', l_context_name);
2466       hr_utility.raise_error;
2467     end if;
2468   end loop; -- end of for c1rec loop
2469   --
2470   -- The following loop searches through and replaces all where clause fillers
2471   -- (Un) with the value from ff_route_parameter_values.  For a text value,
2472   -- the quotes have already pre-inserted into this table.
2473   --
2474   -- Load the route parameters cache to avoid re-execution of the cursor
2475   -- when binding values to bind variables
2476   --
2477   route_parameters_cache.sz := 0;
2478   for c1rec in ro_wclause (l_user_entity_id, l_route_id) loop
2479     route_parameters_cache.sz := route_parameters_cache.sz + 1;
2480     route_parameters_cache.seq_no(route_parameters_cache.sz) := c1rec.sequence_no;
2481     route_parameters_cache.par_val(route_parameters_cache.sz) := c1rec.value;
2482     l_text := replace (l_text, '&U'||to_char(c1rec.sequence_no),
2483                                 ':U'||to_char(c1rec.sequence_no));
2484   end loop;
2485   --
2486   -- Print the route text out for debug purposes.  Normally keep this line
2487   -- commented out, as for a large route it causes an error in the trace
2488   -- utility.
2489   -- hr_utility.trace (l_text);
2490   --
2491   --
2492   -- now build up the full SQL statement:
2493   --
2494   -- If route_type is SRB then in the first phase hardcoding the datatype
2495   -- (to 'N') and the select definition. If route_type is RR, then select the
2496   -- appropriate select definition.
2497   --
2498   if p_route_type = 'SRB' then
2499 --
2500     if (l_o_hint is null) then
2501       l_text := 'SELECT NVL(SUM(prb.balance_value),0) FROM ' || l_text;
2502     else
2503       l_text := 'SELECT /*+ '||l_o_hint||
2504                 ' */ NVL(SUM(prb.balance_value),0) FROM ' || l_text;
2505     end if;
2506 --
2507     if g_debug then
2508        hr_utility.set_location('pay_balance_pkg.get_run_balance', 15);
2509     end if;
2510 --
2511   elsif p_route_type = 'RR' then
2512     --
2513     -- Should never get here since all RRs are handled by run_rr_route now.
2514     --
2515     if g_debug then
2516        hr_utility.set_location('pay_balance_pkg.get_run_balance', 20);
2517     end if;
2518 --
2519     hr_general.assert_condition(false);
2520 --
2521   end if;
2522   --
2523   -- Now execute the SQL statement using dynamic pl/sql:
2524   --
2525   -- Dynamic sql steps:
2526   -- ==================
2527   -- 1. Open dynamic sql cursor
2528   -- 2. Parse dynamic sql
2529   -- 3. bind variables
2530   -- 4. Define dynamic sql columns
2531   -- 5. Execute and fetch dynamic sql
2532   -- 6. Get the sql value (providing there are rows returned)
2533   -- 7. Close the dynamic sql cursor
2534   --
2535   if g_debug then
2536      hr_utility.set_location ('pay_balance_pkg.get_run_balance', 35);
2537   end if;
2538   sql_cursor := dbms_sql.open_cursor;                             -- step 1
2539   --
2540   if g_debug then
2541      hr_utility.set_location ('pay_balance_pkg.get_run_balance', 40);
2542   end if;
2543   dbms_sql.parse(sql_cursor, l_text, dbms_sql.v7);                -- step 2
2544   --
2545   --                                                              -- step 3
2546   -- At this stage we have parsed the route.  Now bind the
2547   -- variables, starting with the contexts (B values).
2548   --
2549   for l_cxt_num in 1..route_contexts_cache.sz loop
2550     if g_debug then
2551        hr_utility.set_location ('pay_balance_pkg.get_run_balance', 45);
2552     end if;
2553     l_count := 0;
2554     l_context_found := FALSE;
2555     while (l_count < no_rows_con_tab) loop
2556       if (con_id_tab(l_count) = route_contexts_cache.cxt_id(l_cxt_num)) then
2557         if g_debug then
2558            hr_utility.trace (con_name_tab (l_count) ||' = '||
2559                              con_value_tab (l_count));
2560         end if;
2561         --
2562         -- Found a bind variable
2563         --
2564         if (con_type_tab (l_count) = 'D') then
2565           if g_debug then
2566              hr_utility.set_location ('pay_balance_pkg.get_run_balance', 50);
2567           end if;
2568           dbms_sql.bind_variable
2569            (sql_cursor,
2573           if g_debug then
2570             con_name_tab(l_count),
2571             fnd_date.canonical_to_date(ltrim(rtrim(con_value_tab (l_count)))));
2572         else
2574              hr_utility.set_location ('pay_balance_pkg.get_run_balance', 55);
2575           end if;
2576           dbms_sql.bind_variable
2577            (sql_cursor,
2578             con_name_tab(l_count),
2579             ltrim(rtrim(con_value_tab (l_count))));
2580         end if;
2581         l_context_found := TRUE;
2582         exit;  -- exit while loop
2583       end if;
2584       l_count := l_count + 1;
2585     end loop;  -- end of while loop
2586     if (l_context_found = FALSE) then
2587       --
2588       -- Raise an error, as there are contexts that have not been set up in
2589       -- the pl/sql tables that are required by the route.
2590       --
2591       if g_debug then
2592          hr_utility.set_location ('pay_balance_pkg.get_run_balance', 60);
2593       end if;
2594       select context_name
2595       into   l_context_name
2596       from   ff_contexts
2597       where  context_id = route_contexts_cache.cxt_id(l_cxt_num);
2598       --
2599       hr_utility.set_message(801, 'HR_7271_PAY_CONTEXT_MISSING');
2600       hr_utility.set_message_token ('CONTEXT_NAME', l_context_name);
2601       hr_utility.raise_error;
2602     end if;
2603   end loop; -- end of for route context loop
2604   --
2605   -- Now bind the where clause fillers (the 'U' values)
2606   --
2607   for l_par_num in 1..route_parameters_cache.sz loop
2608   if g_debug then
2609      hr_utility.set_location ('pay_balance_pkg.get_run_balance', 62);
2610   end if;
2611     dbms_sql.bind_variable (sql_cursor, 'U'||to_char(route_parameters_cache.seq_no(l_par_num)),
2612                                         route_parameters_cache.par_val(l_par_num));
2613   end loop;
2614   --
2615   if g_debug then
2616      hr_utility.set_location ('pay_balance_pkg.get_run_balance', 65);
2617   end if;
2618   dbms_sql.define_column (sql_cursor, 1, p_db_output, 80);        -- step 4
2619   --
2620   if g_debug then
2621      hr_utility.set_location ('pay_balance_pkg.get_run_balance', 70);
2622   end if;
2623   l_rows := dbms_sql.execute_and_fetch (sql_cursor, false);       -- step 5
2624   --
2625   if (l_rows = 1) then
2626     if g_debug then
2627        hr_utility.set_location ('pay_balance_pkg.get_run_balance', 75);
2628     end if;
2629     dbms_sql.column_value (sql_cursor, 1, p_db_output);           -- step 6
2630     --
2631     if g_debug then
2632        hr_utility.set_location ('pay_balance_pkg.get_run_balance', 80);
2633     end if;
2634     dbms_sql.close_cursor(sql_cursor);                            -- step 7
2635     --
2636     if g_debug then
2637        hr_utility.trace ('DB value = ' || p_db_output);
2638     end if;
2639     --
2640   elsif (l_rows = 0) then
2641     dbms_sql.close_cursor(sql_cursor);
2642     if (l_notfound_allowed_flag = 'Y') then
2643       --
2644       -- its ok to not find a row
2645       --
2646       if g_debug then
2647          hr_utility.set_location ('pay_balance_pkg.get_run_balance', 85);
2648       end if;
2649       p_db_output := null;
2650     else
2651       --
2652       -- Error, no rows found when the entity has been defined to always
2653       -- find a row.
2654       --
2655       hr_utility.set_message(801, 'HR_7272_PAY_NO_ROWS_RETURNED');
2656       hr_utility.set_message_token ('USER_ENTITY_NAME', p_user_name);
2657       hr_utility.raise_error;
2658     end if;
2659   else
2660     --
2661     -- More than 1 row have been returned. We must error as DB items can
2662     -- only return 1 row.
2663     --
2664     if g_debug then
2665        hr_utility.set_location ('pay_balance_pkg.get_run_balance', 95);
2666     end if;
2667     dbms_sql.close_cursor(sql_cursor);
2668     hr_utility.set_message(801, 'HR_7273_PAY_MORE_THAN_1_ROW');
2669     hr_utility.raise_error;
2670   end if;
2671   return p_db_output;
2672 exception
2673   --
2674   -- If any other Oracle Error is trapped (e.g. during parse, execute,
2675   -- fetch etc), then we must check to see if the cursor is still open
2676   -- and close down if necessary.
2677   --
2678   When Others Then
2679     l_error_text := sqlerrm;
2680     if g_debug then
2681        Hr_Utility.Set_Location('pay_balance_pkg.get_run_balance', 100);
2682     end if;
2683     If (dbms_sql.is_open(sql_cursor)) then
2684       if g_debug then
2685          Hr_Utility.Set_Location('pay_balance_pkg.get_run_balance', 105);
2686       end if;
2687       dbms_sql.close_cursor(sql_cursor);
2688     End If;
2689     hr_utility.set_message(801, 'HR_7276_PAY_FAILED_DB_ITEM');
2690     hr_utility.set_message_token ('ERROR_MESSAGE', l_error_text);
2691     hr_utility.raise_error;
2692     --
2693 end get_run_balance;
2694 ---------------------------------------------------------------------------
2695 -- function run_db_item
2696 ---------------------------------------------------------------------------
2697 -- Description
2698 -- Previously a sql stmt was fired for every call to get_value, to return
2699 -- the dbi user_name, business_group_id and legislation_code, to be used
2700 -- in the call to run_db_item. run_db_item is only called when the balance
2701 -- value has to be derived, so hopefully not often. Thus this overloaded
2702 -- version of run_db_item has been created so that the sql statement, only
2703 -- need be run as and when required.
2707 --
2704 --
2705 function run_db_item (p_def_bal_id in number)
2706 return varchar2 is
2708 -- get the information required to retrieve the DB item.  It is assumed
2709 -- here that there is a 1-to-1 mapping of database item and user entities.
2710 -- If either legislation code or business group is null then any value
2711 -- may be used in order to satisfy the parameters supplied to function
2712 -- run_db_item.
2713 --
2714 cursor get_vals (p_def_bal number)
2715 is
2716 select dbi.user_name
2717 ,      nvl(ent.business_group_id, -1)
2718 ,      nvl(ent.legislation_code, ' ')
2719 from   ff_database_items dbi
2720 ,      ff_user_entities  ent
2721 where  ent.creator_id = p_def_bal
2722 and    ent.creator_type = 'B'
2723 and    ent.user_entity_id = dbi.user_entity_id;
2724 --
2725 l_balance_val       varchar2(240);
2726 l_user_name         ff_database_items.user_name%type;
2727 l_business_group_id ff_user_entities.business_group_id%type;
2728 l_legislation_code  ff_user_entities.legislation_code%type;
2729 --
2730 BEGIN
2731 open  get_vals(p_def_bal_id);
2732 fetch get_vals into l_user_name, l_business_group_id, l_legislation_code;
2733 close get_vals;
2734 --
2735 l_balance_val := run_db_item(l_user_name
2736                             ,l_business_group_id
2737                             ,l_legislation_code);
2738 return l_balance_val;
2739 --
2740 END run_db_item;
2741 --------------------------------------------------------------------------
2742 -- function get_run_balance
2743 ---------------------------------------------------------------------------
2744 -- Description
2745 -- Previously a sql stmt was fired for every call to get_value, to return
2746 -- the dbi user_name, business_group_id and legislation_code, to be used
2747 -- in the call to run_db_item. run_db_item is only called when the balance
2748 -- value has to be derived, so hopefully not often. Thus this overloaded
2749 -- version of run_db_item has been created so that the sql statement, only
2750 -- need be run as and when required.
2751 --
2752 function get_run_balance (p_def_bal_id in number
2753                          ,p_priority   in number
2754                          ,p_route_type in varchar2)
2755 return number is
2756 --
2757 -- get the information required to retrieve the DB item.  It is assumed
2758 -- here that there is a 1-to-1 mapping of database item and user entities.
2759 -- If either legislation code or business group is null then any value
2760 -- may be used in order to satisfy the parameters supplied to function
2761 -- run_db_item.
2762 --
2763 cursor get_vals (p_def_bal number
2764                 ,p_prty    number)
2765 is
2766 select fue.user_entity_name
2767 ,      nvl(fue.business_group_id, -1)
2768 ,      nvl(fue.legislation_code, ' ')
2769 from   ff_user_entities fue
2770 ,      ff_user_entities fue_b
2771 where  fue.creator_id = p_def_bal
2772 and    fue_b.creator_type = 'B'
2773 and    fue.creator_id = fue_b.creator_id
2774 and    fue.creator_type = 'RB'
2775 and    fue.user_entity_name = fue_b.user_entity_name||'_'||to_char(p_prty)
2776 ;
2777 --
2778 l_balance_val       number;
2779 l_user_name         ff_user_entities.user_entity_name%type;
2780 l_business_group_id ff_user_entities.business_group_id%type;
2781 l_legislation_code  ff_user_entities.legislation_code%type;
2782 l_balance_list      t_batch_list;
2783 l_balance_type_id   pay_defined_balances.balance_type_id%type;
2784 l_route_id          pay_dimension_routes.route_id%type;
2785 l_balance_type_column pay_dimension_routes.balance_type_column%type;
2786 l_retrieval_column    pay_dimension_routes.retrieval_column%type;
2787 l_decode_required     boolean;
2788 l_decode_required_chr pay_dimension_routes.decode_required%type;
2789 l_jur_lvl             pay_balance_types.jurisdiction_level%type;
2790 --
2791 BEGIN
2792   if g_debug then
2793      hr_utility.set_location('Entering pay_balance_pkg.get_run_bal', 10);
2794   end if;
2795 --
2796   if (p_route_type = 'RR') then
2797 --
2798     if g_debug then
2799        hr_utility.set_location('Entering pay_balance_pkg.get_run_bal', 15);
2800     end if;
2801     select pdb.balance_type_id,
2802            pdr.route_id,
2803            pdr.balance_type_column,
2804            pdr.retrieval_column,
2805            nvl(pdr.decode_required, 'N'),
2806            nvl(jurisdiction_level, 0)
2807       into l_balance_type_id,
2808            l_route_id,
2809            l_balance_type_column,
2810            l_retrieval_column,
2811            l_decode_required_chr,
2812            l_jur_lvl
2813       from pay_defined_balances pdb,
2814            pay_dimension_routes pdr,
2815            pay_balance_types    pbt
2816      where pdb.defined_balance_id = p_def_bal_id
2817        and pdb.balance_dimension_id = pdr.balance_dimension_id
2818        and pbt.balance_type_id = pdb.balance_type_id
2819        and pdr.priority = p_priority;
2820 --
2821     l_decode_required := FALSE;
2822     if (l_decode_required_chr = 'Y') then
2823       l_decode_required := TRUE;
2824     end if;
2825     l_balance_list.delete;
2826     run_rr_route(
2827                  FALSE,
2828                  l_balance_type_id,
2829                  l_balance_list,
2830                  l_route_id,
2831                  l_balance_type_column,
2832                  l_retrieval_column,
2833                  l_decode_required,
2834                  l_jur_lvl,
2835                  l_balance_val
2839 --
2836                 );
2837 --
2838   else
2840     open  get_vals(p_def_bal_id, p_priority);
2841     fetch get_vals into l_user_name, l_business_group_id, l_legislation_code;
2842     close get_vals;
2843 --
2844     if g_debug then
2845        hr_utility.set_location('pay_balance_pkg.get_run_bal', 20);
2846        hr_utility.trace('l_user_entity_name is: '||l_user_name);
2847     end if;
2848     l_balance_val := get_run_balance(l_user_name
2849                                     ,l_business_group_id
2850                                     ,l_legislation_code
2851                                     ,p_route_type);
2852   end if;
2853   if g_debug then
2854      hr_utility.trace('Val = '||l_balance_val);
2855   end if;
2856   return l_balance_val;
2857 --
2858 END get_run_balance;
2859 --
2860 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2861 --
2862 --                            get_rr_value                                +
2863 --                                                                        +
2864 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2865 /*
2866 NAME
2867 
2868         get_rr_value  -  Get the balance value based on Run Results
2869 
2870 DESCRIPTION
2871 
2872 */
2873 procedure get_rr_value(p_defined_balance_id in         number,
2874                        p_balance_value      out nocopy number)
2875 is
2876 --
2877 l_balance_value  number;
2878 l_route_count    number;
2879 l_defbal_rec            t_def_bal_rec;
2880 --
2881 begin
2882   if g_debug then
2883      hr_utility.set_location ('Entering: pay_balance_pkg.get_rr_value', 5);
2884   end if;
2885 --
2886   -- First load the cache
2887   load_defbal_cache(p_defined_balance_id,
2888                     l_defbal_rec);
2889 --
2890   if l_defbal_rec.rr_ptr is not null then
2891     if g_debug then
2892        hr_utility.set_location ('Entering: pay_balance_pkg.get_rr_value', 10);
2893     end if;
2894     l_balance_value := get_run_balance(p_defined_balance_id
2895                                       ,g_dimrou_cache(l_defbal_rec.rr_ptr).priority
2896                                       ,g_dimrou_cache(l_defbal_rec.rr_ptr).route_type);
2897   else
2898 --
2899     if g_debug then
2900        hr_utility.set_location ('Entering: pay_balance_pkg.get_rr_value', 20);
2901     end if;
2902     if (l_defbal_rec.start_rb_ptr is not null) then
2903        hr_general.assert_condition(false);
2904     else
2905        if g_debug then
2906           hr_utility.set_location ('Entering: pay_balance_pkg.get_rr_value', 30);
2907        end if;
2908        l_balance_value := fnd_number.canonical_to_number(run_db_item(p_defined_balance_id));
2909     end if;
2910   end if;
2911   --
2912   -- Setup the return values
2913   --
2914   p_balance_value := l_balance_value;
2915   if g_debug then
2916      hr_utility.set_location ('Leaving: pay_balance_pkg.get_rr_value', 45);
2917   end if;
2918 --
2919 end get_rr_value;
2920 --
2921 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2922 --
2923 --                            get_rb_status                               +
2924 --                                                                        +
2925 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2926 /*
2927 NAME
2928 
2929         get_rb_status  -  Work out the Run Balance Status for the
2930                           Balance being retrieved.
2931 
2932 DESCRIPTION
2933 
2934 */
2935 --
2936 procedure get_rb_status (p_retreival_db_id in            number,
2937                          p_run_db_id       in            number,
2938                          p_asg_action_id   in            number,
2939                          p_status             out nocopy varchar2)
2940 is
2941 --
2942 l_run_bal_status  pay_balance_validation.run_balance_status%type;
2943 l_load_date       pay_balance_validation.balance_load_date%type;
2944 l_retrieval_date  pay_payroll_actions.effective_date%type;
2945 l_bus_grp_id      pay_payroll_actions.business_group_id%type;
2946 l_payroll_id      pay_payroll_actions.payroll_id%type;
2947 l_period_type     pay_balance_dimensions.period_type%type;
2948 l_start_date_code pay_balance_dimensions.start_date_code%type;
2949 l_action_type     pay_payroll_actions.action_type%type;
2950 l_start_date      date;
2951 --
2952 begin
2953 --
2954   -- Use cache on the payroll action information for this assignment
2955   -- action.  Bug 4221840.
2956   if p_asg_action_id <> nvl(g_aa_id, -p_asg_action_id) Then
2957    select ppa.effective_date,
2958           ppa.business_group_id,
2959           ppa.payroll_id,
2960           ppa.action_type
2961      into g_retrieval_date,
2962           g_bus_grp_id,
2963           g_payroll_id,
2964           g_action_type
2965      from pay_payroll_actions    ppa,
2966           pay_assignment_actions paa
2967     where paa.assignment_action_id = p_asg_action_id
2968       and paa.payroll_action_id = ppa.payroll_action_id;
2969 
2970     g_aa_id := p_asg_action_id;
2971   end if;
2972 --
2973   l_retrieval_date := g_retrieval_date;
2974   l_bus_grp_id := g_bus_grp_id;
2975   l_payroll_id := g_payroll_id;
2976   l_action_type := g_action_type;
2977 --
2978   select pbv.run_balance_status, pbv.balance_load_date
2979      into l_run_bal_status, l_load_date
2983 --
2980      from pay_balance_validation pbv
2981     where pbv.defined_balance_id = p_run_db_id
2982       and pbv.business_group_id = l_bus_grp_id;
2984    if (l_run_bal_status = 'V') then
2985     if (l_load_date is not null) then
2986 --
2987       --
2988       -- OK, the balances have been loaded from a
2989       -- specific date. We need to workout whether
2990       -- we can use the run balances.
2991       --
2992       select nvl(pbd.period_type, 'NULL'),
2993              pbd.start_date_code
2994         into l_period_type,
2995              l_start_date_code
2996         from pay_balance_dimensions pbd,
2997              pay_defined_balances   pdb
2998        where pdb.defined_balance_id = p_retreival_db_id
2999          and pdb.balance_dimension_id = pbd.balance_dimension_id;
3000 
3001 --
3002       -- If we don't know the retrieval period type
3003       -- then we must assume we cannot use run balances.
3004       if (l_period_type = 'NULL') then
3005         l_run_bal_status := 'I';
3006       else
3007         get_period_type_start(l_period_type,
3008                               l_retrieval_date,
3009                               l_start_date,
3010                               l_start_date_code,
3011                               l_payroll_id,
3012                               l_bus_grp_id,
3013                               l_action_type,
3014                               p_asg_action_id);
3015 --
3016         if (l_start_date < l_load_date) then
3017           l_run_bal_status := 'I';
3018         end if;
3019       end if;
3020     end if;
3021    end if;
3022 --
3023    p_status := l_run_bal_status;
3024 --
3025 exception
3026 --
3027    when no_data_found then
3028 --
3029       p_status := 'I'; -- Invalid
3030 --
3031 end get_rb_status;
3032 --
3033 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3034 --
3035 --                            get_rb_value                                +
3036 --                                                                        +
3037 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3038 /*
3039 NAME
3040 
3041         get_rb_value  -  Get the balance value based on Run Balances
3042 
3043 DESCRIPTION
3044 
3045 */
3046 procedure get_rb_value(p_defined_balance_id in         number,
3047                        p_asg_action_id      in         number,
3048                        p_found              out nocopy boolean,
3049                        p_balance_value      out nocopy number)
3050 is
3051 --
3052 CURSOR get_rb_routes(p_def_bal_id number)
3053 IS
3054 select pdr.run_dimension_id
3055 ,      pdr.priority
3056 ,      pdr.route_type
3057 ,      rdb.defined_balance_id run_def_bal_id
3058 from   pay_dimension_routes pdr
3059 ,      pay_defined_balances pdb -- balance defined balance
3060 ,      pay_defined_balances rdb -- run defined balance
3061 where pdb.balance_dimension_id = pdr.balance_dimension_id
3062 and    pdb.defined_balance_id = p_def_bal_id
3063 and    rdb.balance_type_id = pdb.balance_type_id
3064 and    rdb.balance_dimension_id = pdr.run_dimension_id
3065 and    pdr.route_type = 'SRB'
3066 order by 2;
3067 --
3068 l_balval_found   boolean;
3069 l_valid          pay_defined_balances.run_balance_status%type;
3070 l_balance_value  number;
3071 l_defbal_rec            t_def_bal_rec;
3072 l_position       number;
3073 --
3074 begin
3075   if g_debug then
3076      hr_utility.set_location ('Entering: pay_balance_pkg.get_rb_value', 5);
3077   end if;
3078 --
3079   -- First load the cache
3080   load_defbal_cache(p_defined_balance_id,
3081                     l_defbal_rec);
3082   l_balval_found := FALSE;
3083   l_position     := l_defbal_rec.start_rb_ptr;
3084 --
3085   if (l_defbal_rec.start_rb_ptr is not null) then
3086     while (l_balval_found = FALSE
3087           and l_position <= l_defbal_rec.end_rb_ptr) loop
3088       --
3089       -- see if route definition can be used i.e.defined balance has
3090       -- run_balance_status of V(ALID) and saved_to_run_balances flag is 'Y'
3091       --
3092 --
3093       get_rb_status(p_defined_balance_id,
3094                     g_dimrou_cache(l_position).run_def_bal_id,
3095                     p_asg_action_id,
3096                     l_valid);
3097       --
3098       if g_debug then
3099          hr_utility.set_location ('pay_balance_pkg.get_rb_value', 20);
3100       end if;
3101       --
3102       -- now know that run balance exists and is valid, so call run_db_item to
3103       -- return the balance_value.
3104       --
3105       if l_valid = 'V' then
3106         if g_debug then
3107            hr_utility.set_location ('pay_balance_pkg.get_rb_value', 25);
3108         end if;
3109         l_balance_value := get_run_balance(p_defined_balance_id
3110                                           ,g_dimrou_cache(l_position).priority
3111                                           ,g_dimrou_cache(l_position).route_type);
3112         l_balval_found := TRUE;
3113       end if;
3114       --
3115       l_position := l_position + 1;
3116       --
3117     end loop;
3118   end if;
3119   --
3120   if g_debug then
3121      hr_utility.set_location ('pay_balance_pkg.get_rb_value', 35);
3122   end if;
3123 --
3124   --
3128   p_balance_value := l_balance_value;
3125   -- Setup the return values
3126   --
3127   p_found := l_balval_found;
3129   if g_debug then
3130      hr_utility.set_location ('Leaving: pay_balance_pkg.get_rb_value', 45);
3131   end if;
3132 --
3133 end get_rb_value;
3134 --
3135 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3136 --                                                                        +
3137 --                        check_bal_expiry_internal                       +
3138 --                                                                        +
3139 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3140 --
3141 /*
3142 NAME
3143   check_bal_expiry_internal  - See if the balance has expired
3144 --
3145 DESCRIPTION
3146   Checks whether a balance has expired and returns a mode value:
3147   --
3148   0   : Balance has not expired (Current).
3149   1   : Balance expired
3150   2   : Previous Period Balance Value
3151   3   : Current Period Balance Value
3152   4   : Balance has Rolled over.
3153   --
3154   The pl/sql function name that checks whether the balance has expired is
3155   passed to this routine as 'p_expiry_checking_code'.  The call to the
3156   function is done using dynamic pl/sql.
3157 */
3158 function check_bal_expiry_internal
3159 (
3160    p_bal_owner_asg_action       in     number,    -- assact created balance.
3161    p_assignment_action_id       in     number,    -- current assact..
3162    p_dimension_name             in     varchar2,  -- balance dimension name.
3163    p_expiry_checking_level      in     varchar2,
3164    p_expiry_checking_code       in     varchar2,
3165    p_bal_context_str            in     varchar2   -- list of context values.
3166 ) return number is
3167 p_balance_expired number;
3168 --
3169 l_bal_owner_pay_action  pay_payroll_actions.payroll_action_id%type;
3170 l_bal_owner_eff_date    pay_payroll_actions.effective_date%type;
3171 l_payroll_action        pay_payroll_actions.payroll_action_id%type;
3172 l_effective_date        pay_payroll_actions.effective_date%type;
3173 l_jul_effect_date       number;          -- Julian value of effective date
3174 l_expiry_chk_str        varchar2(2000);  -- used with dynamic pl/sql
3175 l_expiry_information    number;
3176 sql_cursor              integer;
3177 l_rows                  integer;
3178 l_error_text            varchar2(200);   -- used for sql error messages
3179 l_value_found           number;
3180 begin
3181   if g_debug then
3182      hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 1);
3183      hr_utility.trace ('Expiry check level   = ' || p_expiry_checking_level);
3184      hr_utility.trace ('Expiry checking code = ' || p_expiry_checking_code);
3185   end if;
3186   if (p_expiry_checking_level = 'N') then
3187     --
3188     -- There is no expiry check code to run, so balance can't expire
3189     --
3190     p_balance_expired := BALANCE_NOT_EXPIRED;
3191     return p_balance_expired;
3192   end if;
3193   --
3194   -- get the payroll_action_id for the balance (ie. owner):
3195   --
3196   if g_debug then
3197      hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 10);
3198   end if;
3199   select PAY.payroll_action_id,
3200          PAY.effective_date
3201   into   l_bal_owner_pay_action,
3202          l_bal_owner_eff_date
3203   from   pay_assignment_actions        ASG
3204   ,      pay_payroll_actions           PAY
3205   where  ASG.assignment_action_id    = p_bal_owner_asg_action
3206   and    PAY.payroll_action_id       = ASG.payroll_action_id;
3207   --
3208   -- get the actual payroll information for this assignment action:
3209   --
3210   if g_debug then
3211      hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 15);
3212   end if;
3213   select PAY.payroll_action_id,
3214          PAY.effective_date
3215   into   l_payroll_action,
3216          l_effective_date
3217   from   pay_assignment_actions        ASG
3218   ,      pay_payroll_actions           PAY
3219   where  ASG.assignment_action_id    = p_assignment_action_id
3220   and    PAY.payroll_action_id       = ASG.payroll_action_id;
3221   --
3222   -- before building up the sql string to call the expiry checking pl/sql
3223   -- procedure, if the expiry_checking_level is P or E, then check the
3224   -- expiry checking cache.
3225   --
3226   if p_expiry_checking_level in ('P', 'E') then
3227   --
3228     for each_row in 1..t_own_pay_action.count loop
3229     --
3230       if t_own_pay_action(each_row) = l_bal_owner_pay_action
3231       and
3232          t_usr_pay_action(each_row) = l_payroll_action
3233       and
3234          t_dim_nm(each_row) = p_dimension_name
3235       then
3236         l_value_found := t_expiry(each_row);
3237       end if;
3238     end loop;
3239   end if; -- if p_expiry_checking_level in ('P', 'E')
3240   --
3241   -- if l_value_found is null, then the value for the current row has not
3242   -- yet been cached, or the expiry_checking_level is not P or E, so figure
3243   -- out what it should be, then cache the new row, i.e. return to original
3244   -- code, before caching introduced
3245   --
3246   IF l_value_found IS NULL THEN
3247   --
3248   -- we now have all the data required to build up the sql string to call
3249   -- the expiry checking pl/sql procedure:
3250   --
3251   l_expiry_chk_str := 'begin ' || p_expiry_checking_code || ' (';
3252   l_expiry_chk_str := l_expiry_chk_str || ':l_bal_owner_pay_action, ';
3256   l_expiry_chk_str := l_expiry_chk_str || ':l_bal_owner_eff_date, ';
3253   l_expiry_chk_str := l_expiry_chk_str || ':l_payroll_action, ';
3254   l_expiry_chk_str := l_expiry_chk_str || ':p_bal_owner_asg_action, ';
3255   l_expiry_chk_str := l_expiry_chk_str || ':p_assignment_action_id, ';
3257   l_expiry_chk_str := l_expiry_chk_str || ':l_effective_date, ';
3258   l_expiry_chk_str := l_expiry_chk_str || ':p_dimension_name, ';
3259   --
3260   if (p_expiry_checking_level in ('A', 'D')) then
3261     l_expiry_chk_str := l_expiry_chk_str || ':p_bal_context_str, ';
3262   end if;
3263   --
3264   l_expiry_chk_str := l_expiry_chk_str || ':l_expiry_information); end;';
3265   --
3266   -- now execute the SQL statement using dynamic pl/sql:
3267   --
3268   -- Dynamic sql steps:
3269   -- ==================
3270   -- 1. Open dynamic sql cursor
3271   -- 2. Parse dynamic sql
3272   -- 3. bind variables
3273   -- 4. Execute dynamic sql
3274   -- 5. Get the variable value (providing there are rows returned)
3275   -- 6. Close the dynamic sql cursor
3276   --
3277   if g_debug then
3278      hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 20);
3279   end if;
3280   sql_cursor := dbms_sql.open_cursor;                             -- step 1
3281   --
3282   if g_debug then
3283      hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 25);
3284   end if;
3285   dbms_sql.parse(sql_cursor, l_expiry_chk_str, dbms_sql.v7);      -- step 2
3286   --
3287   if g_debug then
3288      hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 30);
3289   end if;
3290   dbms_sql.bind_variable(sql_cursor, 'l_bal_owner_pay_action',    -- step 3:
3291                                       l_bal_owner_pay_action);
3292   --
3293   dbms_sql.bind_variable(sql_cursor, 'l_payroll_action',
3294                                       l_payroll_action);
3295   --
3296   dbms_sql.bind_variable(sql_cursor, 'p_bal_owner_asg_action',
3297                                       p_bal_owner_asg_action);
3298   --
3299   dbms_sql.bind_variable(sql_cursor, 'p_assignment_action_id',
3300                                       p_assignment_action_id);
3301   --
3302   dbms_sql.bind_variable(sql_cursor, 'l_bal_owner_eff_date',
3303                                       l_bal_owner_eff_date);
3304   --
3305   dbms_sql.bind_variable(sql_cursor, 'l_effective_date',
3306                                       l_effective_date);
3307   --
3308   dbms_sql.bind_variable(sql_cursor, 'p_dimension_name',
3309                                       p_dimension_name);
3310   --
3311   if (p_expiry_checking_level in ('A', 'D')) then
3312     dbms_sql.bind_variable(sql_cursor, 'p_bal_context_str',
3313                                         p_bal_context_str);
3314   end if;
3315   --
3316   dbms_sql.bind_variable(sql_cursor, 'l_expiry_information',
3317                                       l_expiry_information);
3318   --
3319   if g_debug then
3320      hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 35);
3321   end if;
3322   l_rows := dbms_sql.execute (sql_cursor);                        -- step 4
3323   --
3324   if (l_rows = 1) then
3325     if g_debug then
3326        hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 40);
3327     end if;
3328     dbms_sql.variable_value(sql_cursor, 'l_expiry_information',   -- step 5
3329                                          l_expiry_information);
3330     --
3331     if g_debug then
3332        hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 45);
3333     end if;
3334     dbms_sql.close_cursor(sql_cursor);                            -- step 6
3335   elsif (l_rows = 0) then
3336     dbms_sql.close_cursor(sql_cursor);
3337       hr_utility.set_message(801, 'HR_7274_PAY_NO_EXPIRY_CODE');
3338       hr_utility.set_message_token ('EXPIRY_CODE', p_expiry_checking_code);
3339       hr_utility.raise_error;
3340   else
3341     --
3342     -- More than 1 row has been returned. We must error as package call can
3343     -- only return 1 row, so this condition should never occur !
3344     --
3345     if g_debug then
3346        hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 111);
3347     end if;
3348     dbms_sql.close_cursor(sql_cursor);
3349     hr_utility.raise_error;
3350   end if;
3351   --
3352   -- At this point we have executed the dynamic pl/sql, 1 row was returned
3353   -- with the expiry information in the value 'l_expiry_information'.
3354   -- If the expiry checking level is Assignment, Payroll action or Enhanced
3355   -- the expiry value is returned.  For a date level expiry checking, a julian
3356   -- date is returned which has to be compared with the effective date of the
3357   -- payroll action.
3358   --
3359   if (p_expiry_checking_level in ('A', 'P', 'E')) then
3360     if g_debug then
3361        hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 50);
3362     end if;
3363     p_balance_expired := l_expiry_information;
3364   else                                 -- date level
3365     if g_debug then
3366        hr_utility.set_location ('pay_balance_pkg.check_bal_expiry_internal', 55);
3367     end if;
3368     l_jul_effect_date := to_number (to_char (l_effective_date, 'J'));
3369     --
3370     if (l_expiry_information < l_jul_effect_date) then
3371       p_balance_expired := BALANCE_EXPIRED;       -- The balance has expired
3372     else
3376   --
3373       p_balance_expired := BALANCE_NOT_EXPIRED;   -- The balance has not expired
3374     end if;
3375   end if;
3377   -- Now have the expired value so cache the new row, if the expiry_checking_
3378   -- level is P or E.
3379   --
3380   if p_expiry_checking_level in ('P', 'E') then
3381   --
3382     t_own_pay_action(t_own_pay_action.count + 1) := l_bal_owner_pay_action;
3383     t_usr_pay_action(t_usr_pay_action.count + 1) := l_payroll_action;
3384     t_dim_nm(t_dim_nm.count + 1) := p_dimension_name;
3385     t_expiry(t_expiry.count + 1) := p_balance_expired;
3386     --
3387     if g_debug then
3388        hr_utility.trace('cached own pay action: '||
3389                        to_char(t_own_pay_action(t_own_pay_action.count)));
3390        hr_utility.trace('cached usr pay action: '||
3391                        to_char(t_usr_pay_action(t_usr_pay_action.count)));
3392        hr_utility.trace('cached dim name: '||t_dim_nm(t_dim_nm.count));
3393        hr_utility.trace('cached bal expired: '||t_expiry(t_expiry.count));
3394     end if;
3395     --
3396   end if;
3397   --
3398   return p_balance_expired;
3399   --
3400   ELSE
3401     return l_value_found;
3402   END IF; -- if l_value_found is null
3403   --
3404 exception
3405   --
3406   -- If any other Oracle Error is trapped (e.g. during parse, execute,
3407   -- fetch etc), then we must check to see if the cursor is still open
3408   -- and close down if necessary.
3409   --
3410   When Others Then
3411     l_error_text := sqlerrm;
3412     if g_debug then
3413        Hr_Utility.Set_Location('check_bal_expiry_internal', 115);
3414     end if;
3415     If (dbms_sql.is_open(sql_cursor)) then
3416       if g_debug then
3417          Hr_Utility.Set_Location('check_bal_expiry_internal', 120);
3418       end if;
3419       dbms_sql.close_cursor(sql_cursor);
3420     End If;
3421     hr_utility.set_message(801, 'HR_7275_PAY_FAILED_IN_EXPIRY');
3422     hr_utility.set_message_token ('ERROR_MESSAGE', l_error_text);
3423     hr_utility.raise_error;
3424 end check_bal_expiry_internal;
3425 --
3426 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3427 --                                                                        +
3428 --                        check_bal_expiry                                +
3429 --                                                                        +
3430 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3431 --
3432 /*
3433 NAME
3434   check_bal_expiry  - See if the balance has expired
3435 --
3436 DESCRIPTION
3437   Checks whether a balance has expired and returns a boolean value:
3438   --
3439   TRUE   : Balance expired
3440   FALSE  : Balance has not expired.
3441   --
3442   The pl/sql function name that checks whether the balance has expired is
3443   passed to this routine as 'p_expiry_checking_code'.  The call to the
3444   function is done using dynamic pl/sql.
3445 */
3446 function check_bal_expiry
3447 (
3448    p_bal_owner_asg_action       in     number,    -- assact created balance.
3449    p_assignment_action_id       in     number,    -- current assact..
3450    p_dimension_name             in     varchar2,  -- balance dimension name.
3451    p_expiry_checking_level      in     varchar2,
3452    p_expiry_checking_code       in     varchar2,
3453    p_bal_context_str            in     varchar2   -- list of context values.
3454 ) return boolean is
3455 p_balance_expired boolean;
3456 l_exp_code number;
3457 --
3458 begin
3459    g_debug := hr_utility.debug_enabled;
3460 --
3461    l_exp_code := check_bal_expiry_internal(p_bal_owner_asg_action,
3462                                  p_assignment_action_id,
3463                                  p_dimension_name,
3464                                  p_expiry_checking_level,
3465                                  p_expiry_checking_code,
3466                                  p_bal_context_str);
3467   if (l_exp_code = BALANCE_NOT_EXPIRED) then
3468      p_balance_expired := FALSE;
3469   else
3470      p_balance_expired := TRUE;
3471   end if;
3472 --
3473   return p_balance_expired;
3474 --
3475 end check_bal_expiry;
3476 --
3477 --
3478 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3479 --
3480 --                            get_old_latest_bal_value                    +
3481 --                                                                        +
3482 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3483 /*
3484 NAME
3485 
3486         get_old_latest_bal_value  -  Get the balance value based on Latest
3487                                      Balances
3488 
3489 DESCRIPTION
3490 
3491 */
3492 procedure get_old_latest_bal_value(p_defined_balance_id   in         number,
3493                                p_assignment_action_id in         number,
3494                                p_defbal_rec           in         t_def_bal_rec,
3495                                p_balance_value        out nocopy number,
3496                                p_found                out nocopy boolean)
3497 is
3498 --
3499 -- The following cursor is used to determine whether there is a latest balance
3500 -- available for the requested assignment action.  This involves checking that
3501 -- the balance is available for the particular assignment_id or person_id, AND
3502 -- that the date of the requested balance is the greater or equal to the date
3506 -- done by comparing the effective date on the payroll actions table,
3503 -- for the latest balance.  (This stops the case where a balance is requested
3504 -- for a date/ action that is in the past, obviously we can't use the
3505 -- 'latest' balance value).  In prevous versions (40.4 and earlier) this was
3507 -- (and PACT.effective_date >= OWN_PACT.effective_date).  However this fails
3508 -- for payroll runs on the same day.  The better comparision is to use
3509 -- assignment sequence.
3510 -- note : The OWN... aliases are the assignment actions that own the balance.
3511 --
3512 cursor bal_contexts_asg
3513 is
3514 select ASGBAL.latest_balance_id,
3515        ASGBAL.assignment_action_id,
3516        ASGBAL.value,
3517        ACT.action_sequence      current_action_seq,
3518        OWN_ACT.action_sequence  owner_action_seq,
3519        ASGBAL.expired_assignment_action_id,
3520        ASGBAL.expired_value,
3521        ASGBAL.prev_assignment_action_id,
3522        ASGBAL.prev_balance_value
3523 from   pay_assignment_latest_balances  ASGBAL
3524 ,      pay_assignment_actions          ACT
3525 ,      pay_assignment_actions          OWN_ACT
3526 where  ASGBAL.defined_balance_id     = p_defined_balance_id
3527 and    ACT.assignment_action_id      = p_assignment_action_id
3528 and    ASGBAL.assignment_id          = ACT.assignment_id
3529 and    OWN_ACT.assignment_action_id  = ASGBAL.assignment_action_id
3530 --and    ACT.action_sequence          >= OWN_ACT.action_sequence
3531 order  by ASGBAL.latest_balance_id;
3532 --
3533 cursor bal_contexts_per
3534 is
3535 select PERBAL.latest_balance_id,
3536        PERBAL.assignment_action_id,
3537        PERBAL.value,
3538        ACT.action_sequence      current_action_seq,
3539        OWN_ACT.action_sequence  owner_action_seq,
3540        PERBAL.expired_assignment_action_id,
3541        PERBAL.expired_value,
3542        PERBAL.prev_assignment_action_id,
3543        PERBAL.prev_balance_value
3544 from   pay_person_latest_balances      PERBAL
3545 ,      per_all_assignments_f           ASSIGN
3546 ,      pay_assignment_actions          ACT
3547 ,      pay_payroll_actions             PACT
3548 ,      pay_assignment_actions          OWN_ACT
3549 where  PERBAL.defined_balance_id     = p_defined_balance_id
3550 and    ACT.assignment_action_id      = p_assignment_action_id
3551 and    ASSIGN.assignment_id          = ACT.assignment_id
3552 and    PERBAL.person_id              = ASSIGN.person_id
3553 and    PACT.payroll_action_id        = ACT.payroll_action_id
3554 and    PACT.effective_date     between ASSIGN.effective_start_date
3555                                    and ASSIGN.effective_end_date
3556 and    OWN_ACT.assignment_action_id  = PERBAL.assignment_action_id
3557 --and    ACT.action_sequence          >= OWN_ACT.action_sequence
3558 order  by PERBAL.latest_balance_id;
3559 --
3560 -- The following cursor is used to get the contexts that are used for a
3561 -- particular latest balance.
3562 --
3563 cursor bal_context_values (p_latest_balance_id number)
3564 is
3565 select CONVAL.context_id,
3566        CONVAL.value,
3567        CON.context_name
3568 from   pay_balance_context_values     CONVAL
3569 ,      ff_contexts                    CON
3570 where  latest_balance_id            = p_latest_balance_id
3571 and    CON.context_id               = CONVAL.context_id
3572 order  by 1;
3573 --
3574 l_balance_value         pay_person_latest_balances.value%type;
3575 l_bal_owner_asg_action  pay_person_latest_balances.assignment_action_id%type;
3576 l_bal_owner_act_seq     pay_assignment_actions.action_sequence%type;
3577 l_bal_asg_act_seq       pay_assignment_actions.action_sequence%type;
3578 l_bal_expired_action
3579                  pay_person_latest_balances.expired_assignment_action_id%type;
3580 l_bal_expired_value     pay_person_latest_balances.expired_value%type;
3581 l_prev_action    pay_person_latest_balances.prev_assignment_action_id%type;
3582 l_prev_value            pay_person_latest_balances.prev_balance_value%type;
3583 l_latest_balance_id     pay_person_latest_balances.latest_balance_id%type;
3584 --
3585 l_num_of_runs           number;
3586 l_bal_con_str           varchar2(500);   -- Balance context string
3587 l_balance_found         boolean;
3588 l_fnd_con_and_val       boolean;
3589 l_found_all_contexts    boolean;
3590 l_balance_expired       number;
3591 l_cursor_executed       boolean;
3592 l_count                 binary_integer;
3593 loc_value_tab           con_val_array;
3594 l_found                 boolean;
3595 l_expiry_needed         boolean;
3596 --
3597 begin
3598 --
3599   l_found := FALSE;
3600   l_balance_value := 0;
3601   l_expiry_needed := TRUE;
3602   --
3603   -- There could be a latest balance available.  Take a copy of the context
3604   -- value table, and if jurisdiction code is present, apply the substr
3605   -- function. (The values stored in the balance context values table have
3606   -- already been substr'ed for jurisdiction code).
3607   --
3608   loc_value_tab := con_value_tab;
3609   --
3610   l_count := 0;
3611   while (l_count < no_rows_con_tab) loop
3612     if (con_name_tab (l_count) = 'JURISDICTION_CODE') then
3613        --
3614        if g_debug then
3615           hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 15)
3616 ;
3617        end if;
3618        loc_value_tab (l_count) := substr (loc_value_tab(l_count), 1,
3619                                           p_defbal_rec.jurisdiction_lvl);
3620        if g_debug then
3624     end if;
3621           hr_utility.trace ('substr jur. code = ' || loc_value_tab (l_count));
3622        end if;
3623        exit;   -- exit while loop
3625     l_count := l_count + 1;
3626   end loop;
3627   --
3628   -- The following cursor loops are the final search for a latest balance.
3629   -- If both the context name and context values match, then there is a
3630   -- latest balance present.
3631   --
3632   l_balance_found := FALSE;
3633   ---
3634   IF p_defbal_rec.dimension_type = 'A' THEN
3635     open bal_contexts_asg;
3636   ELSE
3637     OPEN bal_contexts_per;
3638   END IF;
3639   --
3640   LOOP
3641     if g_debug then
3642        hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 20);
3643     end if;
3644     --
3645     IF p_defbal_rec.dimension_type = 'A' THEN
3646       FETCH bal_contexts_asg INTO l_latest_balance_id,
3647                                   l_bal_owner_asg_action,
3648                                   l_balance_value,
3649                                   l_bal_asg_act_seq,
3650                                   l_bal_owner_act_seq,
3651                                   l_bal_expired_action,
3652                                   l_bal_expired_value,
3653                                   l_prev_action,
3654                                   l_prev_value;
3655       EXIT WHEN bal_contexts_asg%NOTFOUND;
3656     ELSE
3657       FETCH bal_contexts_per INTO l_latest_balance_id,
3658                                   l_bal_owner_asg_action,
3659                                   l_balance_value,
3660                                   l_bal_asg_act_seq,
3661                                   l_bal_owner_act_seq,
3662                                   l_bal_expired_action,
3663                                   l_bal_expired_value,
3664                                   l_prev_action,
3665                                   l_prev_value;
3666       EXIT WHEN bal_contexts_per%NOTFOUND;
3667     END IF;
3668     --
3669     -- initialise variables:
3670     --
3671     l_found_all_contexts := FALSE;
3672     l_bal_con_str        := null;
3673     l_cursor_executed    := FALSE;
3674     for c2rec in bal_context_values (l_latest_balance_id) loop
3675       if g_debug then
3676          hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 25);
3677       end if;
3678       --
3679       -- Try to find the context:
3680       --
3681       l_fnd_con_and_val := FALSE;
3682       l_cursor_executed := TRUE;
3683       l_count := 0;
3684       if g_debug then
3685          hr_utility.trace ('Trying to match ' || c2rec.context_name);
3686       end if;
3687       --
3688       while (l_count < no_rows_con_tab) loop
3689         if ((con_id_tab    (l_count) = c2rec.context_id) AND
3690             (ltrim(rtrim(loc_value_tab (l_count))) = c2rec.value)) then
3691           --
3692           -- found a matching context name AND value
3693           --
3694           if g_debug then
3695              hr_utility.trace ('match found');
3696           end if;
3697           l_fnd_con_and_val := TRUE;
3698           --
3699           -- Since we have the context name and value at this point, build
3700           -- up the context string for possible use later in the expiry
3701           -- checking code.  (note, we only need this string for expiry
3702           -- checking levels of assignment or date, not payroll action).
3703           --
3704           if (p_defbal_rec.expiry_check_lvl in ('A', 'D')) then
3705             l_bal_con_str := l_bal_con_str || con_name_tab(l_count);
3706             l_bal_con_str := l_bal_con_str || '=';
3707             l_bal_con_str := l_bal_con_str || c2rec.value;
3708             l_bal_con_str := l_bal_con_str || ' ';
3709           end if;
3710           exit;   -- exit while loop
3711         end if;
3712         l_count := l_count + 1;
3713       end loop;                                             -- end while
3714       --
3715       if (l_fnd_con_and_val = TRUE) then
3716         --
3717         -- Found all contexts so far.  set flag, and check next context.
3718         --
3719         l_found_all_contexts := TRUE;
3720       else
3721         --
3722         -- Failed to find a matching context name and value, so clear
3723         -- flags and exit inner context cursor loop (c2rec)
3724         --
3725         if g_debug then
3726            hr_utility.trace ('match failed');
3727         end if;
3728         l_found_all_contexts := FALSE;
3729         exit; -- exit c2rec
3730       end if;
3731     end loop;                                               -- end c2rec
3732     --
3733     -- As this point (the exit of c2rec cursor) we have checked through
3734     -- for matching contexts for a particular latest_balance_id.  If we
3735     -- have found all the contexts AND values match then we have found the
3736     -- balance.  Also if the c2rec cursor was never executed then there
3737     -- were no rows in the context table, ie. the route does not use any
3738     -- contexts (apart from assignment action id). Therefore we have a
3739     -- match.
3740     -- Else carry on to the next latest_balance_id.
3741     --
3742     if ((l_found_all_contexts = TRUE) OR (l_cursor_executed = FALSE)) then
3743       if g_debug then
3744          hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 35);
3745       end if;
3746       l_balance_found := TRUE;
3747       exit; -- exit c1rec
3748     end if;
3749   end loop;                                                 -- end asg/per loop
3750   --
3751   IF p_defbal_rec.dimension_type = 'A' THEN
3752     CLOSE bal_contexts_asg;
3753   ELSE
3754     CLOSE bal_contexts_per;
3755   END IF;
3756   --
3757   -- If we could not find a latest balance, then derive from ff_routes
3758   --
3759   if g_debug then
3760      hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 37);
3761   end if;
3762   if (l_balance_found = TRUE) then
3763     if g_debug then
3764        hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 38);
3765        hr_utility.trace('OWN SEQ '||l_bal_owner_act_seq);
3766        hr_utility.trace('CURR SEQ '||l_bal_asg_act_seq);
3767     end if;
3768     --
3769     -- Check that the current assignment_action is the same as the
3770     -- owner action, if so there is no need for expiry checking.
3771     --
3772     if (p_assignment_action_id = l_bal_owner_asg_action) then
3773        l_found := TRUE;
3774        l_expiry_needed := FALSE;
3775     elsif (p_assignment_action_id = l_bal_expired_action) then
3776        l_balance_value := l_bal_expired_value;
3777        l_found := TRUE;
3778        l_expiry_needed := FALSE;
3779     elsif (p_assignment_action_id = l_prev_action) then
3780        l_balance_value := l_prev_value;
3781        l_found := TRUE;
3782        l_expiry_needed := FALSE;
3783     --
3784     -- A latest balance row has been found but can it be used for this
3785     -- effective date. Must check the action sequences of the owning
3786     -- assignment_action.
3787     -- Or is it a special balance expiry level 'E'
3788     --
3789     elsif ((l_bal_owner_act_seq > l_bal_asg_act_seq) AND
3790         (p_defbal_rec.expiry_check_lvl <> 'E')) then
3791        --
3792        -- O.K. We cannot use the latest balance value, now check if the
3793        -- expired details can by used.
3794        --
3795        if g_debug then
3796           hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 140
3797 );
3798        end if;
3799        select count(*)
3800        into   l_num_of_runs
3801        from
3802             pay_payroll_actions     ppa2,
3803             pay_assignment_actions  paa2,
3804             pay_assignment_actions  paa
3805        where paa.assignment_action_id = l_bal_expired_action
3806        and   paa2.assignment_id       = paa.assignment_id
3807        and   ppa2.payroll_action_id   = paa2.payroll_action_id
3808        and   ppa2.action_type        in ('R', 'Q', 'B', 'V')
3809        and   paa2.action_sequence between paa.action_sequence
3810                                       and l_bal_asg_act_seq;
3811        --
3812        -- Check if the expired assignment action is the only run action
3813        -- for this assignment upto the effective date. If none are found
3814        -- then the expired assignment action must be the dummy value -9999.
3815        -- If more than 1 are found then there must be other payroll runs
3816        -- in the specified period.
3817        --
3818        if l_num_of_runs = 1 then
3819           if g_debug then
3820              hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value',
3821 150);
3822           end if;
3823           l_bal_owner_asg_action := l_bal_expired_action;
3824           l_balance_value        := l_bal_expired_value;
3825           l_expiry_needed        := TRUE;
3826        else
3827          -- check if can use previous balance value
3828           if g_debug then
3829              hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value',
3830 160);
3831           end if;
3832           select count(*)
3833           into   l_num_of_runs
3834           from
3835               pay_payroll_actions     ppa2,
3836               pay_assignment_actions  paa2,
3837               pay_assignment_actions  paa
3838           where paa.assignment_action_id = l_prev_action
3839           and   paa2.assignment_id       = paa.assignment_id
3840           and   ppa2.payroll_action_id   = paa2.payroll_action_id
3841           and   ppa2.action_type        in ('R', 'Q', 'B', 'V')
3842           and   paa2.action_sequence between paa.action_sequence
3843                                       and l_bal_asg_act_seq;
3844 --
3845           if l_num_of_runs = 1 then
3846             if g_debug then
3847                hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value'
3848 , 170);
3849             end if;
3850             l_bal_owner_asg_action := l_prev_action;
3851             l_balance_value        := l_prev_value;
3852             l_expiry_needed        := TRUE;
3853           else
3854             l_expiry_needed        := FALSE;
3855             --
3856             -- call monitoring balance retrieval code
3857             --
3858             pay_monitor_balance_retrieval.monitor_balance_retrieval
3859                                 (p_defined_balance_id
3860                                 ,p_assignment_action_id
3861                                 ,'Core Balance pkg - Latest balance expired');
3862           end if;
3863        end if;
3864 --
3865     end if;
3866     if g_debug then
3867        hr_utility.set_location ('pay_balance_pkg.get_old_latest_bal_value', 55);
3868     end if;
3869 --
3870     if (l_expiry_needed = TRUE) then
3871       --
3872       -- If we have reached here then there is a latest balance/expired balance
3873       -- value present on the database that we can use.  Now see if the balance
3874       -- has expired by running the procedure associated with the balance - this
3875       -- requires dynamic pl/sql.
3876       -- If the balance has expired then a zero value will be returned.
3877       --
3878       l_balance_expired := check_bal_expiry_internal (l_bal_owner_asg_action,
3879                                              p_assignment_action_id,
3880                                              p_defbal_rec.dimension_name,
3881                                              p_defbal_rec.expiry_check_lvl,
3882                                              p_defbal_rec.expiry_check_code,
3883                                              l_bal_con_str);
3884       --
3885       -- OK, whats the checking level, we might have a special
3886       -- case balance
3887       --
3888       if (p_defbal_rec.expiry_check_lvl = 'E') then
3889 --
3890        -- Yep its a special case.
3891         if (l_balance_expired = BALANCE_EXPIRED) then
3892           if g_debug then
3893              hr_utility.trace ('Balance expired');
3894           end if;
3895           l_balance_value := 0;
3896           l_found := TRUE;
3897         elsif (l_balance_expired = BALANCE_NOT_EXPIRED) then
3898           if g_debug then
3899              hr_utility.trace ('Balance not expired');
3900           end if;
3901           l_found := TRUE;
3902         else
3903            --
3904            -- call monitoring balance retrieval code
3905            --
3906            pay_monitor_balance_retrieval.monitor_balance_retrieval
3907                         (p_defined_balance_id
3908                         ,p_assignment_action_id
3909                         ,'Core Balance pkg - still no value after expiry checking'
3910 );
3911         end if; --BALANCE_EXPIRED
3912 
3913       else
3914         if (l_balance_expired = BALANCE_EXPIRED) then
3915           if g_debug then
3916              hr_utility.trace ('Balance expired');
3917           end if;
3918           l_balance_value := 0;
3919           l_found := TRUE;
3920         else
3921           if g_debug then
3922              hr_utility.trace ('Balance not expired');
3923           end if;
3924           l_found := TRUE;
3925         end if; --BALANCE_EXPIRED
3926       end if;   --p_defbal_rec.expiry_check_lvl = 'E'
3927     end if;     -- l_found = FALSE
3928   else
3929     --
3930     -- call monitoring balance retrieval code
3931     --
3932     pay_monitor_balance_retrieval.monitor_balance_retrieval
3933                                (p_defined_balance_id
3934                                ,p_assignment_action_id
3935                                ,'Core Balance pkg - No latest balance');
3936   end if; -- l_balance_found = FALSE
3937 --
3938 -- Set up the return values
3939 --
3940    p_found := l_found;
3941    p_balance_value := l_balance_value;
3942 --
3943 end get_old_latest_bal_value;
3944 --
3945 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3946 --
3947 --                            get_new_latest_bal_value                    +
3948 --                                                                        +
3949 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3950 /*
3951 NAME
3952 
3953         get_new_latest_bal_value  -  Get the balance value based on Latest
3954                                      Balances
3955 
3956 DESCRIPTION
3957 
3958 */
3959 procedure get_new_latest_bal_value(p_defined_balance_id   in         number,
3960                                p_assignment_action_id in         number,
3961                                p_defbal_rec           in         t_def_bal_rec,
3962                                p_balance_value        out nocopy number,
3963                                p_found                out nocopy boolean)
3964 is
3965 --
3966 cursor crs_latest_bal
3967 is
3968 select plb.latest_balance_id,
3969        plb.assignment_action_id,
3970        plb.value,
3971        plb.expiry_date,
3972        ACT.action_sequence      current_action_seq,
3973        OWN_ACT.action_sequence  owner_action_seq,
3974        PACT.effective_date      current_effective_date,
3975        plb.expired_assignment_action_id,
3976        plb.expired_value,
3977        plb.expired_date,
3978        plb.prev_assignment_action_id,
3979        plb.prev_balance_value,
3980        plb.prev_expiry_date,
3981        plb.tax_unit_id,
3982        plb.jurisdiction_code,
3983        plb.original_entry_id,
3984        plb.source_id,
3985        plb.source_text,
3986        plb.source_text2,
3987        plb.source_number,
3988        plb.tax_group,
3989        plb.payroll_id,
3990        plb.local_unit_id,
3991        plb.organization_id,
3992        plb.source_number2
3993 from   pay_latest_balances             plb
3994 ,      per_all_assignments_f           ASSIGN
3995 ,      pay_assignment_actions          ACT
3996 ,      pay_payroll_actions             PACT
3997 ,      pay_assignment_actions          OWN_ACT
3998 where  plb.defined_balance_id        = p_defined_balance_id
3999 and    ACT.assignment_action_id      = p_assignment_action_id
4000 and    ASSIGN.assignment_id          = ACT.assignment_id
4001 and    exists (select ''
4002                  from pay_object_groups               POG
4003                 where POG.source_id (+)             = ACT.assignment_id
4004                 and   POG.source_type (+)           = 'PAF'
4005                 and nvl(plb.process_group_id,
4006                         nvl(POG.parent_object_group_id, -1)) =
4007                     nvl(POG.parent_object_group_id,-1)
4008               )
4009 and    plb.person_id                 = ASSIGN.person_id
4010 and    nvl(plb.assignment_id, ACT.assignment_id) = ACT.assignment_id
4011 and    PACT.payroll_action_id        = ACT.payroll_action_id
4012 and    PACT.effective_date     between ASSIGN.effective_start_date
4013                                    and ASSIGN.effective_end_date
4014 and    OWN_ACT.assignment_action_id  = plb.assignment_action_id
4015 --and    ACT.action_sequence          >= OWN_ACT.action_sequence
4016 and    nvl(plb.tax_unit_id, nvl(g_con_tax_unit_id, -1))
4017                              = nvl(g_con_tax_unit_id, -1)
4018 and    nvl(substr(nvl(plb.jurisdiction_code,
4019                   nvl(g_con_jurisdiction_code, -1)),
4020                   1, p_defbal_rec.jurisdiction_lvl),
4021            -1)
4022                              = nvl(
4023                                 substr(nvl(g_con_jurisdiction_code, -1),
4024                                        1, p_defbal_rec.jurisdiction_lvl),
4025                                    -1)
4026 and    nvl(plb.original_entry_id, nvl(g_con_original_entry_id, -1))
4027                              = nvl(g_con_original_entry_id, -1)
4028 and    nvl(plb.source_id, nvl(g_con_source_id, -1))
4029                              = nvl(g_con_source_id, -1)
4030 and    nvl(plb.source_text, nvl(g_con_source_text, -1))
4031                              = nvl(g_con_source_text, -1)
4032 and    nvl(plb.source_text2, nvl(g_con_source_text2, -1))
4033                              = nvl(g_con_source_text2, -1)
4034 and    nvl(plb.source_number, nvl(g_con_source_number, -1))
4035                              = nvl(g_con_source_number, -1)
4036 and    nvl(plb.tax_group, nvl(g_con_tax_group, -1))
4037                              = nvl(g_con_tax_group, -1)
4038 and    nvl(plb.payroll_id, nvl(g_con_payroll_id, -1))
4039                              = nvl(g_con_payroll_id, -1)
4040 and    nvl(plb.local_unit_id, nvl(g_con_local_unit_id, -1))
4041                              = nvl(g_con_local_unit_id, -1)
4042 and    nvl(plb.organization_id, nvl(g_con_organization_id, -1))
4043                              = nvl(g_con_organization_id, -1)
4044 and    nvl(plb.source_number2, nvl(g_con_source_number2, -1))
4045                              = nvl(g_con_source_number2, -1)
4046 order  by plb.latest_balance_id;
4047 --
4048 l_balance_value         pay_latest_balances.value%type;
4049 l_bal_owner_asg_action  pay_latest_balances.assignment_action_id%type;
4050 --
4051 l_num_of_runs           number;
4052 l_bal_con_str           varchar2(500);   -- Balance context string
4053 l_balance_expired       number;
4054 l_found                 boolean;
4055 l_expiry_needed         boolean;
4056 --
4057 l_cnt number;
4058 l_expiry_date date;
4059 begin
4060 --
4061  l_expiry_date := NULL;
4062  l_cnt         := 0;
4063  l_bal_con_str := ' ';
4064  l_found       := FALSE;
4065  for lbrec in crs_latest_bal loop
4066 --
4067    l_cnt := l_cnt + 1;
4068 --
4069     if g_debug then
4070        hr_utility.set_location ('pay_balance_pkg.get_new_latest_bal_value', 38);
4071        hr_utility.trace('OWN SEQ '||lbrec.owner_action_seq);
4072        hr_utility.trace('CURR SEQ '||lbrec.current_action_seq);
4073     end if;
4074     --
4075     -- Check that the current assignment_action is the same as the
4076     -- owner action, if so there is no need for expiry checking.
4077     --
4078     if (p_assignment_action_id = lbrec.assignment_action_id) then
4079        l_balance_value := lbrec.value;
4080        l_found := TRUE;
4081        l_expiry_needed := FALSE;
4082     elsif (p_assignment_action_id = lbrec.expired_assignment_action_id) then
4083        l_balance_value := lbrec.expired_value;
4084        l_found := TRUE;
4085        l_expiry_needed := FALSE;
4086     elsif (p_assignment_action_id = lbrec.prev_assignment_action_id) then
4087        l_balance_value := lbrec.prev_balance_value;
4088        l_found := TRUE;
4089        l_expiry_needed := FALSE;
4090     --
4091     -- A latest balance row has been found but can it be used for this
4092     -- effective date. Must check the action sequences of the owning
4093     -- assignment_action.
4094     -- Or is it a special balance expiry level 'E'
4095     --
4096     elsif ((lbrec.owner_action_seq > lbrec.current_action_seq) AND
4097         (p_defbal_rec.expiry_check_lvl <> 'E')) then
4098        --
4099        -- O.K. We cannot use the latest balance value, now check if the
4100        -- expired details can by used.
4101        --
4102        if g_debug then
4103           hr_utility.set_location ('pay_balance_pkg.get_new_latest_bal_value', 140
4104 );
4105        end if;
4106        select count(*)
4107        into   l_num_of_runs
4108        from
4109             pay_payroll_actions     ppa2,
4110             pay_assignment_actions  paa2,
4111             pay_assignment_actions  paa
4112        where paa.assignment_action_id = lbrec.expired_assignment_action_id
4113        and   paa2.assignment_id       = paa.assignment_id
4114        and   ppa2.payroll_action_id   = paa2.payroll_action_id
4115        and   ppa2.action_type        in ('R', 'Q', 'B', 'V')
4116        and   paa2.action_sequence between paa.action_sequence
4117                                       and lbrec.current_action_seq;
4118        --
4119        -- Check if the expired assignment action is the only run action
4120        -- for this assignment upto the effective date. If none are found
4121        -- then the expired assignment action must be the dummy value -9999.
4122        -- If more than 1 are found then there must be other payroll runs
4123        -- in the specified period.
4124        --
4125        if l_num_of_runs = 1 then
4126           if g_debug then
4127              hr_utility.set_location ('pay_balance_pkg.get_new_latest_bal_value',
4128 150);
4129           end if;
4130           l_bal_owner_asg_action := lbrec.expired_assignment_action_id;
4131           l_balance_value        := lbrec.expired_value;
4132           l_expiry_date          := lbrec.expired_date;
4133           l_expiry_needed        := TRUE;
4134        else
4135          -- check if can use previous balance value
4136           if g_debug then
4137              hr_utility.set_location ('pay_balance_pkg.get_new_latest_bal_value',
4138 160);
4139           end if;
4140           select count(*)
4141           into   l_num_of_runs
4142           from
4143               pay_payroll_actions     ppa2,
4144               pay_assignment_actions  paa2,
4145               pay_assignment_actions  paa
4146           where paa.assignment_action_id = lbrec.prev_assignment_action_id
4147           and   paa2.assignment_id       = paa.assignment_id
4148           and   ppa2.payroll_action_id   = paa2.payroll_action_id
4149           and   ppa2.action_type        in ('R', 'Q', 'B', 'V')
4150           and   paa2.action_sequence between paa.action_sequence
4151                                       and lbrec.current_action_seq;
4152 --
4153           if l_num_of_runs = 1 then
4154             if g_debug then
4155                hr_utility.set_location ('pay_balance_pkg.get_new_latest_bal_value'
4156 , 170);
4157             end if;
4158             l_bal_owner_asg_action := lbrec.prev_assignment_action_id;
4159             l_balance_value        := lbrec.prev_balance_value;
4160             l_expiry_needed        := TRUE;
4161             l_expiry_date          := lbrec.prev_expiry_date;
4162           else
4163             l_expiry_needed        := FALSE;
4164             l_found                := FALSE;
4165             --
4166             -- call monitoring balance retrieval code
4167             --
4168             pay_monitor_balance_retrieval.monitor_balance_retrieval
4169                                 (p_defined_balance_id
4170                                 ,p_assignment_action_id
4171                                 ,'Core Balance pkg - Latest balance expired');
4172           end if;
4173        end if;
4174 --
4175     else
4176        /* Current latest balance can be used, we just need to perform expiry check
4177 ing*/
4178 --
4179        l_bal_owner_asg_action := lbrec.assignment_action_id;
4180        l_balance_value        := lbrec.value;
4181        l_expiry_needed        := TRUE;
4182        l_expiry_date          := lbrec.expiry_date;
4183 --
4184     end if;
4185     if g_debug then
4186        hr_utility.set_location ('pay_balance_pkg.get_new_latest_bal_value', 55);
4187     end if;
4188 --
4189     if (l_expiry_needed = TRUE) then
4190       --
4191       -- If we have reached here then there is a latest balance/expired balance
4192       -- value present on the database that we can use.  Now see if the balance
4193       -- has expired by running the procedure associated with the balance - this
4194       -- requires dynamic pl/sql.
4195       -- If the balance has expired then a zero value will be returned.
4196       --
4197       if (l_expiry_date is not null and p_defbal_rec.expiry_check_lvl <> 'E') then
4198          if (lbrec.current_effective_date > l_expiry_date) then
4199            l_balance_value := 0;
4200          end if;
4201          l_found := TRUE;
4202       else
4203 --
4204         -- OK, we need to do the expiry checking the hard way
4205 --
4206         if (p_defbal_rec.expiry_check_lvl in ('A', 'D')) then
4207 --
4208           if (lbrec.TAX_UNIT_ID is not null) then
4209             l_bal_con_str := l_bal_con_str||'TAX_UNIT_ID='||lbrec.TAX_UNIT_ID||' ';
4210           end if;
4211 --
4212           if (lbrec.JURISDICTION_CODE is not null) then
4213             l_bal_con_str := l_bal_con_str||'JURISDICTION_CODE='||lbrec.JURISDICTION_CODE||' ';
4214           end if;
4215 --
4216           if (lbrec.ORIGINAL_ENTRY_ID is not null) then
4217             l_bal_con_str := l_bal_con_str||'ORIGINAL_ENTRY_ID='||lbrec.ORIGINAL_ENTRY_ID||' ';
4218           end if;
4219 --
4220           if (lbrec.SOURCE_ID is not null) then
4221             l_bal_con_str := l_bal_con_str||'SOURCE_ID='||lbrec.SOURCE_ID||' ';
4222           end if;
4223 --
4224           if (lbrec.SOURCE_TEXT is not null) then
4225             l_bal_con_str := l_bal_con_str||'SOURCE_TEXT='||lbrec.SOURCE_TEXT||' ';
4226           end if;
4227 --
4228           if (lbrec.SOURCE_TEXT2 is not null) then
4229             l_bal_con_str := l_bal_con_str||'SOURCE_TEXT2='||lbrec.SOURCE_TEXT2||' ';
4230           end if;
4231 --
4232           if (lbrec.SOURCE_NUMBER is not null) then
4233             l_bal_con_str := l_bal_con_str||'SOURCE_NUMBER='||lbrec.SOURCE_NUMBER||' ';
4234           end if;
4235 --
4236           if (lbrec.TAX_GROUP is not null) then
4237             l_bal_con_str := l_bal_con_str||'TAX_GROUP='||lbrec.TAX_GROUP||' ';
4238           end if;
4239 --
4240           if (lbrec.PAYROLL_ID is not null) then
4241             l_bal_con_str := l_bal_con_str||'PAYROLL_ID='||lbrec.PAYROLL_ID||' ';
4242           end if;
4243 --
4244           if (lbrec.LOCAL_UNIT_ID is not null) then
4245             l_bal_con_str := l_bal_con_str||'LOCAL_UNIT_ID='||lbrec.LOCAL_UNIT_ID||' ';
4246           end if;
4247 --
4248           if (lbrec.ORGANIZATION_ID is not null) then
4249             l_bal_con_str := l_bal_con_str||'ORGANIZATION_ID='||lbrec.ORGANIZATION_ID||' ';
4250           end if;
4251 --
4252           if (lbrec.SOURCE_NUMBER2 is not null) then
4253             l_bal_con_str := l_bal_con_str||'SOURCE_NUMBER2='||lbrec.SOURCE_NUMBER2||' ';
4254           end if;
4255 --
4256         end if;
4257 --
4258         l_balance_expired := check_bal_expiry_internal (l_bal_owner_asg_action,
4259                                                p_assignment_action_id,
4260                                                p_defbal_rec.dimension_name,
4261                                                p_defbal_rec.expiry_check_lvl,
4262                                                p_defbal_rec.expiry_check_code,
4263                                                l_bal_con_str);
4264 --
4265         --
4266         -- OK, whats the checking level, we might have a special
4267         -- case balance
4268         --
4269         if (p_defbal_rec.expiry_check_lvl = 'E') then
4270 --
4271          -- Yep its a special case.
4272           if (l_balance_expired = BALANCE_EXPIRED) then
4273             if g_debug then
4274                hr_utility.trace ('Balance expired');
4275             end if;
4276             l_balance_value := 0;
4277             l_found := TRUE;
4278           elsif (l_balance_expired = BALANCE_NOT_EXPIRED) then
4279             if g_debug then
4280                hr_utility.trace ('Balance not expired');
4281             end if;
4282             l_found := TRUE;
4283           else
4284              --
4285              -- call monitoring balance retrieval code
4286              --
4287              pay_monitor_balance_retrieval.monitor_balance_retrieval
4288                           (p_defined_balance_id
4289                           ,p_assignment_action_id
4290                           ,'Core Balance pkg - still no value after expiry checkin
4291 g');
4292           end if; --BALANCE_EXPIRED
4293         else
4294           if (l_balance_expired = BALANCE_EXPIRED) then
4295             if g_debug then
4296                hr_utility.trace ('Balance expired');
4297             end if;
4298             l_balance_value := 0;
4299             l_found := TRUE;
4300           else
4301             if g_debug then
4302                hr_utility.trace ('Balance not expired');
4303             end if;
4304             l_found := TRUE;
4305           end if; --BALANCE_EXPIRED
4306         end if;   --p_defbal_rec.expiry_check_lvl = 'E'
4307       end if;
4308     end if;     -- l_found = FALSE
4309  end loop;
4310 --
4311  if (l_cnt = 0) then
4312    p_found := FALSE;
4313  elsif (l_cnt = 1) then
4314    p_found := l_found;
4315    p_balance_value := l_balance_value;
4316  else
4317     pay_core_utils.assert_condition('get_new_latest_bal_value:1', false);
4318  end if;
4319 --
4320 end get_new_latest_bal_value;
4321 --
4322 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4323 --
4324 --                            get_latest_bal_value                        +
4325 --                                                                        +
4326 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4327 /*
4328 NAME
4329 
4330         get_latest_bal_value  -  Get the balance value based on Latest
4331                                  Balances
4332 
4333 DESCRIPTION
4334 
4335 */
4336 procedure get_latest_bal_value(p_defined_balance_id   in         number,
4337                                p_assignment_action_id in         number,
4338                                p_defbal_rec           in         t_def_bal_rec,
4339                                p_balance_value        out nocopy number,
4340                                p_found                out nocopy boolean)
4341 is
4342 --
4343 l_business_group_id per_business_groups.business_group_id%type;
4344 l_status            pay_upgrade_status.status%type;
4345 begin
4346 --
4347   select ppa.business_group_id
4348     into l_business_group_id
4349     from pay_payroll_actions ppa,
4350          pay_assignment_actions paa
4351    where ppa.payroll_action_id = paa.payroll_action_id
4352      and paa.assignment_action_id = p_assignment_action_id;
4353 --
4354   pay_core_utils.get_upgrade_status(l_business_group_id,
4355                                     'SINGLE_BAL_TABLE',
4356                                     l_status);
4357 --
4358   if (l_status = 'Y') then
4359     get_new_latest_bal_value(p_defined_balance_id,
4360                              p_assignment_action_id,
4361                              p_defbal_rec,
4362                              p_balance_value,
4363                              p_found);
4364   else
4365     get_old_latest_bal_value(p_defined_balance_id,
4366                              p_assignment_action_id,
4367                              p_defbal_rec,
4368                              p_balance_value,
4369                              p_found);
4370   end if;
4371 end get_latest_bal_value;
4372 --
4373 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4374 --                                                                        +
4375 --               get_value_internal                                       +
4376 --                                                                        +
4377 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4378 --
4379 /*
4380 NAME
4381   get_value_internal        - Get Value of a balance
4382 --
4383 DESCRIPTION
4384    This procedure retrieves the value of a balance, and is the
4385    main procedure for returning a single value.
4386    NOTE:- It does do not retrieve the balance value if the
4387           Run Result route is to be used, and that route is
4388           batch enabled.
4389 */
4390 procedure get_value_internal (
4391                               p_defined_balance_id   in            number,
4392                               p_defbal_rec           in            t_def_bal_rec,
4393                               p_assignment_action_id in            number,
4394                               p_get_rr_route         in            boolean,
4395                               p_get_rb_route         in            boolean,
4396                               p_found                   out nocopy boolean,
4397                               p_balance_value           out nocopy number)
4398 is
4399 l_balance_value         number;
4400 l_found                 boolean;
4401 begin
4402 --
4403   l_found := FALSE;
4404   l_balance_value := 0;
4405 --
4406   if (p_defbal_rec.dimension_type not in ('N', 'F', 'R')) then
4407      if (p_get_rb_route = FALSE
4408        and p_get_rr_route = FALSE) then
4409          get_latest_bal_value(p_defined_balance_id,
4410                               p_assignment_action_id,
4411                               p_defbal_rec,
4412                               l_balance_value,
4413                               l_found);
4414      else
4415          --
4416          -- call monitoring balance retrieval code
4417          --
4418          pay_monitor_balance_retrieval.monitor_balance_retrieval
4419                        (p_defined_balance_id
4420                        ,p_assignment_action_id
4421                        ,'Core Balance pkg - Run Result/Balance override flag set');
4422      end if;
4423    else
4424    --
4425    -- call monitoring balance retrieval code
4426    --
4427    pay_monitor_balance_retrieval.monitor_balance_retrieval
4428                         (p_defined_balance_id
4429                         ,p_assignment_action_id
4430                         ,'Core Balance pkg - Dimension_type is NOT A or P. '
4431                         ||'Latest balances are only availbale for assignment '
4432                         ||'or person level balances, that are not also Run '
4433                         ||'level balances. The level of the balance is set '
4434                         ||'with the dimension_type on pay_balance_dimensions. '
4435                         ||'Dimension types for seeded data must not be '
4436                         ||'updated.');
4437    end if;
4438 --
4439    if (l_found = FALSE
4440      and p_get_rr_route = FALSE) then
4441      get_rb_value(p_defined_balance_id,
4442                   p_assignment_action_id,
4443                   l_found,
4444                   l_balance_value);
4445    end if;
4446 --
4447    -- If this balance can not be retrieved in batch mode then
4448    -- get it via run results single mode.
4449    if (l_found = FALSE) then
4450      if (p_defbal_rec.balance_type_column is null) then
4451        get_rr_value(p_defined_balance_id,
4452                     l_balance_value);
4453        l_found := TRUE;
4454      end if;
4455    end if;
4456 --
4457 -- Setup the return values
4458 --
4459    p_found         := l_found;
4460    p_balance_value := l_balance_value;
4461 --
4462 end get_value_internal;
4463 --
4464 --
4465 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4466 --                                                                        +
4467 --               get_value     (assignment action mode)                   +
4468 --                                                                        +
4469 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4470 --
4471 /*
4472 NAME
4473   get_value        - Retrieve a balance (assignment action mode)
4474 --
4475 DESCRIPTION
4476   This is the main routine that derives a balance for an assignment action,
4477   and is also called by the date mode get_value function.  If the parameter
4478   'p_always_get_db_item' is set to TRUE the routine will derive the balance
4479   from the route, and not even look for a latest balance.  This parameter is
4480   used for testing purposes.
4481   --
4482   There are 2 overloaded versions of the get_value function below.  The first
4483   one is called by the forms/ reports.  The second has the option of setting
4484   the 'p_always_get_db_item' value.  This is necessary since the forms do not
4485   support default parameters.
4486 */
4487 procedure get_value_int_batch (p_assignment_action_id in            number,
4488                      p_context_lst          in            t_context_tab,
4489                      p_get_rr_route         in            boolean default FALSE,
4490                      p_get_rb_route         in            boolean default FALSE,
4491                      p_output_table         in out nocopy t_detailed_bal_out_tab)
4492 is
4493 l_retrieval_list         t_balance_value_tab;
4494 l_retrieval_cnt          number;
4495 --
4496 l_cache_cnt              number;
4497 prev_def_bal_id          number;
4498 l_add_balance            boolean;
4499 start_def_bal_ptr        number;
4500 pos_indx                 number_tab;
4501 bal_ju_code              pay_run_results.jurisdiction_code%type;
4502 con_ju_code              pay_run_results.jurisdiction_code%type;
4503 begin
4504 --
4505 -- Now get the balances
4506 --
4507    for con_cnt in 1..p_context_lst.count loop
4508 --
4509      l_retrieval_list.delete;
4510 --
4511      -- Set the contexts
4512      if (p_context_lst(con_cnt).tax_unit_id is not null) then
4513        set_context('TAX_UNIT_ID', p_context_lst(con_cnt).tax_unit_id);
4514      end if;
4515      if (p_context_lst(con_cnt).jurisdiction_code is not null) then
4516        set_context('JURISDICTION_CODE', p_context_lst(con_cnt).jurisdiction_code);
4517      end if;
4518      if (p_context_lst(con_cnt).source_id is not null) then
4519        set_context('SOURCE_ID', p_context_lst(con_cnt).source_id);
4520      end if;
4521      if (p_context_lst(con_cnt).source_text is not null) then
4522        set_context('SOURCE_TEXT', p_context_lst(con_cnt).source_text);
4523      end if;
4524      if (p_context_lst(con_cnt).source_number is not null) then
4525        set_context('SOURCE_NUMBER', p_context_lst(con_cnt).source_number);
4526      end if;
4527      if (p_context_lst(con_cnt).source_text2 is not null) then
4528        set_context('SOURCE_TEXT2', p_context_lst(con_cnt).source_text2);
4529      end if;
4530      if (p_context_lst(con_cnt).time_def_id  is not null) then
4531        set_context('TIME_DEFINITION_ID', p_context_lst(con_cnt).time_def_id);
4532      end if;
4533      if (p_context_lst(con_cnt).balance_date  is not null) then
4534        set_context('BALANCE_DATE', to_char(p_context_lst(con_cnt).balance_date,
4535                                            'YYYY/MM/DD HH24:MI:SS'));
4536      end if;
4537      if (p_context_lst(con_cnt).local_unit_id  is not null) then
4538        set_context('LOCAL_UNIT_ID', p_context_lst(con_cnt).local_unit_id);
4539      end if;
4540      if (p_context_lst(con_cnt).source_number2  is not null) then
4541        set_context('SOURCE_NUMBER2', p_context_lst(con_cnt).source_number2);
4542      end if;
4543      if (p_context_lst(con_cnt).organization_id  is not null) then
4544        set_context('ORGANIZATION_ID', p_context_lst(con_cnt).organization_id);
4545      end if;
4546 --
4547      -- Now loop through the balances to retrieve.
4548      l_retrieval_cnt := 1;
4549      for db_cnt in 1..p_output_table.count loop
4550 --
4551        if (p_output_table(db_cnt).balance_found = FALSE) then
4552 --
4553          bal_ju_code := substr(p_output_table(db_cnt).jurisdiction_code,
4554                               1,
4555                               p_output_table(db_cnt).jurisdiction_lvl);
4556          con_ju_code := substr(p_context_lst(con_cnt).jurisdiction_code,
4557                             1,
4558                             p_output_table(db_cnt).jurisdiction_lvl);
4559 --
4560          if (nvl(p_context_lst(con_cnt).tax_unit_id, -999) =
4561                    nvl(p_output_table(db_cnt).tax_unit_id,
4562                        nvl(p_context_lst(con_cnt).tax_unit_id, -999))
4563              and nvl(con_ju_code, '<NULL>') =
4564                    nvl(bal_ju_code, nvl(con_ju_code, '<NULL>'))
4565              and nvl(p_context_lst(con_cnt).source_text, '<NULL>') =
4566                    nvl(p_output_table(db_cnt).source_text,
4567                        nvl(p_context_lst(con_cnt).source_text, '<NULL>'))
4568              and nvl(p_context_lst(con_cnt).source_number, -999) =
4569                    nvl(p_output_table(db_cnt).source_number,
4570                        nvl(p_context_lst(con_cnt).source_number, -999))
4571              and nvl(p_context_lst(con_cnt).source_number2, -999) =
4572                    nvl(p_output_table(db_cnt).source_number2,
4573                        nvl(p_context_lst(con_cnt).source_number2, -999))
4574              and nvl(p_context_lst(con_cnt).local_unit_id, -999) =
4575                    nvl(p_output_table(db_cnt).local_unit_id,
4576                        nvl(p_context_lst(con_cnt).local_unit_id, -999))
4577              and nvl(p_context_lst(con_cnt).organization_id, -999) =
4578                    nvl(p_output_table(db_cnt).organization_id,
4579                        nvl(p_context_lst(con_cnt).organization_id, -999))
4580              and nvl(p_context_lst(con_cnt).source_text2, '<NULL>') =
4581                    nvl(p_output_table(db_cnt).source_text2,
4582                        nvl(p_context_lst(con_cnt).source_text2, '<NULL>'))
4583              and nvl(p_context_lst(con_cnt).source_id, -999) =
4584                    nvl(p_output_table(db_cnt).source_id,
4585                        nvl(p_context_lst(con_cnt).source_id, -999))
4586              and nvl(p_context_lst(con_cnt).time_def_id, -999) =
4587                    nvl(p_output_table(db_cnt).time_def_id,
4588                        nvl(p_context_lst(con_cnt).time_def_id, -999))
4589              and nvl(p_context_lst(con_cnt).balance_date, to_date('0001/01/01', 'YYYY/MM/DD')) =
4590                    nvl(p_output_table(db_cnt).balance_date,
4591                        nvl(p_context_lst(con_cnt).balance_date, to_date('0001/01/01', 'YYYY/MM/DD')))
4592            ) then
4593            --
4594            l_retrieval_list(l_retrieval_cnt).defined_balance_id :=
4595                        p_output_table(db_cnt).defined_balance_id;
4596            pos_indx(l_retrieval_cnt) := db_cnt;
4597            l_retrieval_cnt := l_retrieval_cnt + 1;
4598            --
4599          end if;
4600        end if;
4601 --
4602      end loop;
4603      -- Now do the retrieval
4604      get_value(p_assignment_action_id,
4605                l_retrieval_list,
4606                p_get_rr_route,
4607                p_get_rb_route);
4608 --
4609      -- Put the returned values into the cache table.
4610      for ret_cnt in 1..l_retrieval_list.count loop
4611        p_output_table(pos_indx(ret_cnt)).balance_value :=
4612                           l_retrieval_list(ret_cnt).balance_value;
4613        p_output_table(pos_indx(ret_cnt)).balance_found := TRUE;
4614      end loop;
4615 --
4616    end loop;
4617 --
4618 end;
4619 
4620 procedure get_value (p_assignment_action_id in            number,
4621                      p_defined_balance_lst  in            t_balance_value_tab,
4622                      p_context_lst          in            t_context_tab,
4623                      p_get_rr_route         in            boolean default FALSE,
4624                      p_get_rb_route         in            boolean default FALSE,
4625                      p_output_table            out nocopy t_detailed_bal_out_tab)
4626 is
4627 --
4628 l_retrieval_list         t_balance_value_tab;
4629 l_transform_cache        t_int_bal_cache;
4630 l_batch_bal_cache        t_int_bal_cache;
4631 l_cache_cnt              number;
4632 l_defbal_rec             t_def_bal_rec;
4633 prev_def_bal_id          number;
4634 l_add_balance            boolean;
4635 start_def_bal_ptr        number;
4636 l_retrieval_cnt          number;
4637 pos_indx                 number_tab;
4638 bal_ju_code              pay_run_results.jurisdiction_code%type;
4639 con_ju_code              pay_run_results.jurisdiction_code%type;
4640 --
4641 begin
4642    g_debug := hr_utility.debug_enabled;
4643 --
4644    l_cache_cnt := 1; -- The next free position
4645 --
4646 -- Create the super set of balances to be calculated.
4647 --
4648    for def_cnt in 1..p_defined_balance_lst.count loop
4649 --
4650       load_defbal_cache(p_defined_balance_lst(def_cnt).defined_balance_id,
4651                         l_defbal_rec);
4652 --
4653       for con_cnt in 1..p_context_lst.count loop
4654 --
4655         -- Only add a row if we have all the contexts needed
4656         if ((l_defbal_rec.tu_needed and p_context_lst(con_cnt).tax_unit_id is null)
4657           or (l_defbal_rec.jc_needed and p_context_lst(con_cnt).jurisdiction_code is null)
4658           or (l_defbal_rec.si_needed and p_context_lst(con_cnt).source_id is null)
4659           or (l_defbal_rec.sn_needed and p_context_lst(con_cnt).source_number is null)
4660           or (l_defbal_rec.st2_needed and p_context_lst(con_cnt).source_text2 is null)
4661           or (l_defbal_rec.st_needed and p_context_lst(con_cnt).source_text is null)
4662           or (l_defbal_rec.td_needed and p_context_lst(con_cnt).time_def_id is null)
4663           or (l_defbal_rec.lu_needed and p_context_lst(con_cnt).local_unit_id is null)
4664           or (l_defbal_rec.sn2_needed and p_context_lst(con_cnt).source_number2 is null)
4665           or (l_defbal_rec.org_needed and p_context_lst(con_cnt).organization_id is null)
4666           or (l_defbal_rec.bd_needed and p_context_lst(con_cnt).balance_date is null)
4667            ) then
4668 --
4669            if g_debug then
4670               hr_utility.trace('Not a valid context combination for this balance');
4671            end if;
4672 --
4673         else
4674 --
4675           l_transform_cache(l_cache_cnt).defined_balance_id :=
4676                                   p_defined_balance_lst(def_cnt).defined_balance_id;
4677           l_transform_cache(l_cache_cnt).balance_found := FALSE;
4678 --
4679 --      Set up the contexts
4680 --
4681           if (l_defbal_rec.tu_needed
4682               and p_context_lst(con_cnt).tax_unit_id is not null) then
4683              l_transform_cache(l_cache_cnt).tax_unit_id :=
4684                                      p_context_lst(con_cnt).tax_unit_id;
4685           else
4686              l_transform_cache(l_cache_cnt).tax_unit_id := null;
4687           end if;
4688 --
4689           if (l_defbal_rec.jc_needed
4690              and p_context_lst(con_cnt).jurisdiction_code is not null) then
4691              l_transform_cache(l_cache_cnt).jurisdiction_code :=
4692                                      substr(p_context_lst(con_cnt).jurisdiction_code,
4693                                             1,
4694                                             l_defbal_rec.jurisdiction_lvl);
4695           else
4696              l_transform_cache(l_cache_cnt).jurisdiction_code := null;
4697           end if;
4698 --
4699           if (l_defbal_rec.si_needed
4700              and p_context_lst(con_cnt).source_id is not null) then
4701              l_transform_cache(l_cache_cnt).source_id :=
4702                                      p_context_lst(con_cnt).source_id;
4703           else
4704              l_transform_cache(l_cache_cnt).source_id := null;
4705           end if;
4706 --
4707           if (l_defbal_rec.st_needed
4708               and p_context_lst(con_cnt).source_text is not null) then
4709              l_transform_cache(l_cache_cnt).source_text :=
4710                                      p_context_lst(con_cnt).source_text;
4711           else
4712              l_transform_cache(l_cache_cnt).source_text := null;
4713           end if;
4714 --
4715           if (l_defbal_rec.sn_needed
4716               and p_context_lst(con_cnt).source_number is not null) then
4717              l_transform_cache(l_cache_cnt).source_number :=
4718                                      p_context_lst(con_cnt).source_number;
4719           else
4720              l_transform_cache(l_cache_cnt).source_number := null;
4721           end if;
4722 --
4723           if (l_defbal_rec.st2_needed
4724               and p_context_lst(con_cnt).source_text2 is not null) then
4725              l_transform_cache(l_cache_cnt).source_text2 :=
4726                                      p_context_lst(con_cnt).source_text2;
4727           else
4728              l_transform_cache(l_cache_cnt).source_text2 := null;
4729           end if;
4730 --
4731           if (l_defbal_rec.td_needed
4732               and p_context_lst(con_cnt).time_def_id is not null) then
4733              l_transform_cache(l_cache_cnt).time_def_id :=
4734                                      p_context_lst(con_cnt).time_def_id;
4735           else
4736              l_transform_cache(l_cache_cnt).time_def_id := null;
4737           end if;
4738 --
4739           if (l_defbal_rec.bd_needed
4740               and p_context_lst(con_cnt).balance_date is not null) then
4741              l_transform_cache(l_cache_cnt).balance_date :=
4742                                      p_context_lst(con_cnt).balance_date;
4743           else
4744              l_transform_cache(l_cache_cnt).balance_date := null;
4745           end if;
4746 --
4747           if (l_defbal_rec.lu_needed
4748               and p_context_lst(con_cnt).local_unit_id is not null) then
4749              l_transform_cache(l_cache_cnt).local_unit_id :=
4750                                      p_context_lst(con_cnt).local_unit_id;
4751           else
4752              l_transform_cache(l_cache_cnt).local_unit_id := null;
4753           end if;
4754 --
4755           if (l_defbal_rec.sn2_needed
4756               and p_context_lst(con_cnt).source_number2 is not null) then
4757              l_transform_cache(l_cache_cnt).source_number2 :=
4758                                      p_context_lst(con_cnt).source_number2;
4759           else
4760              l_transform_cache(l_cache_cnt).source_number2 := null;
4761           end if;
4762 --
4763           if (l_defbal_rec.org_needed
4764               and p_context_lst(con_cnt).organization_id is not null) then
4765              l_transform_cache(l_cache_cnt).organization_id :=
4766                                      p_context_lst(con_cnt).organization_id;
4767           else
4768              l_transform_cache(l_cache_cnt).organization_id := null;
4769           end if;
4770 --
4771           -- Next free position.
4772           l_cache_cnt := l_cache_cnt + 1;
4773 --
4774         end if;
4775 --
4776       end loop;
4777 --
4778    end loop;
4779 --
4780 -- Remove the duplicates
4781 --
4782    l_cache_cnt := 1;
4783    start_def_bal_ptr := -999;
4784    prev_def_bal_id := -1;
4785    for cnt in 1..l_transform_cache.count loop
4786 --
4787      hr_utility.trace('Time 1 '||to_char(sysdate, 'HH24:MI:SS'));
4788      if l_transform_cache(cnt).defined_balance_id = prev_def_bal_id then
4789 --
4790        l_add_balance := TRUE;
4791        if g_debug then
4792           hr_utility.trace('Start index '||start_def_bal_ptr||' to '||l_batch_bal_cache.count);
4793        end if;
4794      hr_utility.trace('Time 2 '||to_char(sysdate, 'HH24:MI:SS'));
4795        for dup_cnt in start_def_bal_ptr..l_batch_bal_cache.count loop
4796 --
4797      hr_utility.trace('Time 3 '||to_char(sysdate, 'HH24:MI:SS'));
4798           if g_debug then
4799              hr_utility.trace('Comparing...');
4800              hr_utility.trace(l_transform_cache(cnt).defined_balance_id||' '||
4801                               l_batch_bal_cache(dup_cnt).defined_balance_id);
4802              hr_utility.trace(nvl(l_transform_cache(cnt).tax_unit_id, -999)||' '||
4803                               nvl(l_batch_bal_cache(dup_cnt).tax_unit_id, -999));
4804              hr_utility.trace(nvl(l_transform_cache(cnt).jurisdiction_code, '<NULL>')||' '||
4805                               nvl(l_batch_bal_cache(dup_cnt).jurisdiction_code, '<NULL>'));
4806              hr_utility.trace(nvl(l_transform_cache(cnt).source_id, -999)||' '||
4807                               nvl(l_batch_bal_cache(dup_cnt).source_id, -999));
4808              hr_utility.trace(nvl(l_transform_cache(cnt).source_text, '<NULL>')||' '||
4809                               nvl(l_batch_bal_cache(dup_cnt).source_text, '<NULL>'));
4810           end if;
4811 --
4812      hr_utility.trace('Time 4 '||to_char(sysdate, 'HH24:MI:SS'));
4813           if (l_transform_cache(cnt).defined_balance_id  = l_batch_bal_cache(dup_cnt).defined_balance_id
4814             and nvl(l_transform_cache(cnt).tax_unit_id, -999)
4815                             = nvl(l_batch_bal_cache(dup_cnt).tax_unit_id, -999)
4816             and nvl(l_transform_cache(cnt).jurisdiction_code, '<NULL>')
4817                             = nvl(l_batch_bal_cache(dup_cnt).jurisdiction_code, '<NULL>')
4818             and nvl(l_transform_cache(cnt).source_id, -999)
4819                             = nvl(l_batch_bal_cache(dup_cnt).source_id, -999)
4820             and nvl(l_transform_cache(cnt).source_text, '<NULL>')
4821                             = nvl(l_batch_bal_cache(dup_cnt).source_text, '<NULL>')
4822             and nvl(l_transform_cache(cnt).source_number, -999)
4823                             = nvl(l_batch_bal_cache(dup_cnt).source_number, -999)
4824             and nvl(l_transform_cache(cnt).source_text2, '<NULL>')
4825                             = nvl(l_batch_bal_cache(dup_cnt).source_text2, '<NULL>')
4826             and nvl(l_transform_cache(cnt).time_def_id, -999)
4827                             = nvl(l_batch_bal_cache(dup_cnt).time_def_id, -999)
4828             and nvl(l_transform_cache(cnt).local_unit_id, -999)
4829                             = nvl(l_batch_bal_cache(dup_cnt).local_unit_id, -999)
4830             and nvl(l_transform_cache(cnt).source_number2, -999)
4831                             = nvl(l_batch_bal_cache(dup_cnt).source_number2, -999)
4832             and nvl(l_transform_cache(cnt).organization_id, -999)
4833                             = nvl(l_batch_bal_cache(dup_cnt).organization_id, -999)
4834             and nvl(l_transform_cache(cnt).balance_date, to_date('0001/01/01', 'YYYY/MM/DD'))
4835                             = nvl(l_batch_bal_cache(dup_cnt).balance_date, to_date('0001/01/01', 'YYYY/MM/DD'))
4836               ) then
4837             l_add_balance := FALSE;
4838           end if;
4839 --
4840        end loop;
4841 --
4842      hr_utility.trace('Time 5 '||to_char(sysdate, 'HH24:MI:SS'));
4843        if (l_add_balance = TRUE) then
4844      hr_utility.trace('Time 6 '||to_char(sysdate, 'HH24:MI:SS'));
4845          l_batch_bal_cache(l_cache_cnt) := l_transform_cache(cnt);
4846          l_cache_cnt := l_cache_cnt + 1;
4847        end if;
4848      hr_utility.trace('Time 7 '||to_char(sysdate, 'HH24:MI:SS'));
4849 --
4850      else
4851      hr_utility.trace('Time 8 '||to_char(sysdate, 'HH24:MI:SS'));
4852        l_batch_bal_cache(l_cache_cnt) := l_transform_cache(cnt);
4853        prev_def_bal_id := l_batch_bal_cache(l_cache_cnt).defined_balance_id;
4854        start_def_bal_ptr := l_cache_cnt;
4855        l_cache_cnt := l_cache_cnt + 1;
4856      end if;
4857      hr_utility.trace('Time 9 '||to_char(sysdate, 'HH24:MI:SS'));
4858 --
4859    end loop;
4860 --
4861 -- Now get the balances
4862 --
4863    for con_cnt in 1..p_context_lst.count loop
4864 --
4865      l_retrieval_list.delete;
4866 --
4867      -- Set the contexts
4868      if (p_context_lst(con_cnt).tax_unit_id is not null) then
4869        set_context('TAX_UNIT_ID', p_context_lst(con_cnt).tax_unit_id);
4870      end if;
4871      if (p_context_lst(con_cnt).jurisdiction_code is not null) then
4872        set_context('JURISDICTION_CODE', p_context_lst(con_cnt).jurisdiction_code);
4873      end if;
4874      if (p_context_lst(con_cnt).source_id is not null) then
4875        set_context('SOURCE_ID', p_context_lst(con_cnt).source_id);
4876      end if;
4877      if (p_context_lst(con_cnt).source_text is not null) then
4878        set_context('SOURCE_TEXT', p_context_lst(con_cnt).source_text);
4879      end if;
4880      if (p_context_lst(con_cnt).source_number is not null) then
4881        set_context('SOURCE_NUMBER', p_context_lst(con_cnt).source_number);
4882      end if;
4883      if (p_context_lst(con_cnt).source_text2 is not null) then
4884        set_context('SOURCE_TEXT2', p_context_lst(con_cnt).source_text2);
4885      end if;
4886      if (p_context_lst(con_cnt).time_def_id is not null) then
4887        set_context('TIME_DEFINITION_ID', p_context_lst(con_cnt).time_def_id);
4888      end if;
4889      if (p_context_lst(con_cnt).balance_date is not null) then
4890        set_context('BALANCE_DATE', to_char(p_context_lst(con_cnt).balance_date,
4891                                            'YYYY/MM/DD HH24:MI:SS'));
4892      end if;
4893      if (p_context_lst(con_cnt).local_unit_id is not null) then
4894        set_context('LOCAL_UNIT_ID', p_context_lst(con_cnt).local_unit_id);
4895      end if;
4896      if (p_context_lst(con_cnt).source_number2 is not null) then
4897        set_context('SOURCE_NUMBER2', p_context_lst(con_cnt).source_number2);
4898      end if;
4899      if (p_context_lst(con_cnt).organization_id is not null) then
4900        set_context('ORGANIZATION_ID', p_context_lst(con_cnt).organization_id);
4901      end if;
4902 --
4903      -- Now loop through the balances to retrieve.
4904      l_retrieval_cnt := 1;
4905      for db_cnt in 1..l_batch_bal_cache.count loop
4906 --
4907        if (l_batch_bal_cache(db_cnt).balance_found = FALSE) then
4908 --
4909          load_defbal_cache(l_batch_bal_cache(db_cnt).defined_balance_id,
4910                            l_defbal_rec);
4911 --
4912          bal_ju_code := substr(l_batch_bal_cache(db_cnt).jurisdiction_code,
4913                               1,
4914                               l_defbal_rec.jurisdiction_lvl);
4915          con_ju_code := substr(p_context_lst(con_cnt).jurisdiction_code,
4916                             1,
4917                             l_defbal_rec.jurisdiction_lvl);
4918 --
4919          if (nvl(p_context_lst(con_cnt).tax_unit_id, -999) =
4920                    nvl(l_batch_bal_cache(db_cnt).tax_unit_id,
4921                        nvl(p_context_lst(con_cnt).tax_unit_id, -999))
4922              and nvl(con_ju_code, '<NULL>') =
4923                    nvl(bal_ju_code, nvl(con_ju_code, '<NULL>'))
4924              and nvl(p_context_lst(con_cnt).source_text, '<NULL>') =
4925                    nvl(l_batch_bal_cache(db_cnt).source_text,
4926                        nvl(p_context_lst(con_cnt).source_text, '<NULL>'))
4927              and nvl(p_context_lst(con_cnt).source_number, -999) =
4928                    nvl(l_batch_bal_cache(db_cnt).source_number,
4929                        nvl(p_context_lst(con_cnt).source_number, -999))
4930              and nvl(p_context_lst(con_cnt).source_text2, '<NULL>') =
4931                    nvl(l_batch_bal_cache(db_cnt).source_text2,
4932                        nvl(p_context_lst(con_cnt).source_text2, '<NULL>'))
4933              and nvl(p_context_lst(con_cnt).source_id, -999) =
4934                    nvl(l_batch_bal_cache(db_cnt).source_id,
4935                        nvl(p_context_lst(con_cnt).source_id, -999))
4936              and nvl(p_context_lst(con_cnt).time_def_id, -999) =
4937                    nvl(l_batch_bal_cache(db_cnt).time_def_id,
4938                        nvl(p_context_lst(con_cnt).time_def_id, -999))
4939              and nvl(p_context_lst(con_cnt).local_unit_id, -999) =
4940                    nvl(l_batch_bal_cache(db_cnt).local_unit_id,
4941                        nvl(p_context_lst(con_cnt).local_unit_id, -999))
4942              and nvl(p_context_lst(con_cnt).source_number2, -999) =
4943                    nvl(l_batch_bal_cache(db_cnt).source_number2,
4944                        nvl(p_context_lst(con_cnt).source_number2, -999))
4945              and nvl(p_context_lst(con_cnt).organization_id, -999) =
4946                    nvl(l_batch_bal_cache(db_cnt).organization_id,
4947                        nvl(p_context_lst(con_cnt).organization_id, -999))
4948              and nvl(p_context_lst(con_cnt).balance_date, to_date('0001/01/01', 'YYYY/MM/DD')) =
4949                    nvl(l_batch_bal_cache(db_cnt).balance_date,
4950                        nvl(p_context_lst(con_cnt).balance_date, to_date('0001/01/01', 'YYYY/MM/DD')))
4951            ) then
4952            --
4953            l_retrieval_list(l_retrieval_cnt).defined_balance_id :=
4954                        l_batch_bal_cache(db_cnt).defined_balance_id;
4955            pos_indx(l_retrieval_cnt) := db_cnt;
4956            l_retrieval_cnt := l_retrieval_cnt + 1;
4957            --
4958          end if;
4959        end if;
4960 --
4961      end loop;
4962 --
4963      -- Now do the retrieval
4964      get_value(p_assignment_action_id,
4965                l_retrieval_list,
4966                p_get_rr_route,
4967                p_get_rb_route);
4968 --
4969      -- Put the returned values into the cache table.
4970      for ret_cnt in 1..l_retrieval_list.count loop
4971        l_batch_bal_cache(pos_indx(ret_cnt)).balance_value :=
4972                           l_retrieval_list(ret_cnt).balance_value;
4973        l_batch_bal_cache(pos_indx(ret_cnt)).balance_found := TRUE;
4974      end loop;
4975 --
4976    end loop;
4977 --
4978    for db_cnt in 1..l_batch_bal_cache.count loop
4979      p_output_table(db_cnt).defined_balance_id := l_batch_bal_cache(db_cnt).defined_balance_id;
4980      p_output_table(db_cnt).tax_unit_id        := l_batch_bal_cache(db_cnt).tax_unit_id;
4981      p_output_table(db_cnt).jurisdiction_code  := l_batch_bal_cache(db_cnt).jurisdiction_code;
4982      p_output_table(db_cnt).source_id          := l_batch_bal_cache(db_cnt).source_id;
4983      p_output_table(db_cnt).source_text        := l_batch_bal_cache(db_cnt).source_text;
4984      p_output_table(db_cnt).source_number      := l_batch_bal_cache(db_cnt).source_number;
4985      p_output_table(db_cnt).source_text2       := l_batch_bal_cache(db_cnt).source_text2;
4986      p_output_table(db_cnt).time_def_id        := l_batch_bal_cache(db_cnt).time_def_id;
4987      p_output_table(db_cnt).local_unit_id      := l_batch_bal_cache(db_cnt).local_unit_id;
4988      p_output_table(db_cnt).source_number2     := l_batch_bal_cache(db_cnt).source_number2;
4989      p_output_table(db_cnt).organization_id    := l_batch_bal_cache(db_cnt).organization_id;
4990      p_output_table(db_cnt).balance_date       := l_batch_bal_cache(db_cnt).balance_date;
4991      p_output_table(db_cnt).balance_value      := l_batch_bal_cache(db_cnt).balance_value;
4992    end loop;
4993 --
4994 l_retrieval_list.delete;
4995 l_transform_cache.delete;
4996 l_batch_bal_cache.delete;
4997 --
4998 end get_value;
4999 --
5000 procedure get_value (p_assignment_action_id in            number,
5001                      p_defined_balance_lst  in out nocopy t_balance_value_tab,
5002                      p_get_rr_route         in            boolean default FALSE,
5003                      p_get_rb_route         in            boolean default FALSE)
5004 is
5005 --
5006 l_defbal_rec            t_def_bal_rec;
5007 l_balance_value         number;
5008 l_found                 boolean;
5009 l_balance_cache         t_int_bal_cache;
5010 l_batch_bd_id           pay_balance_dimensions.balance_dimension_id%type;
5011 l_batch_list            t_batch_list;
5012 l_batch_cnt             number;
5013 l_dummy_value           number;
5014 l_decode_required       boolean;
5015 l_jur_lvl               pay_balance_types.jurisdiction_level%type;
5016 --
5017 begin
5018    g_debug := hr_utility.debug_enabled;
5019 --
5020    set_context ('ASSIGNMENT_ACTION_ID', p_assignment_action_id);
5021 --
5022    -- Ensure the internal cache is empty.
5023    l_balance_cache.delete;
5024 --
5025    for cnt in 1..p_defined_balance_lst.count loop
5026 --
5027      load_defbal_cache(p_defined_balance_lst(cnt).defined_balance_id,
5028                        l_defbal_rec);
5029 --
5030      get_value_internal (p_defined_balance_lst(cnt).defined_balance_id,
5031                          l_defbal_rec,
5032                          p_assignment_action_id,
5033                          p_get_rr_route,
5034                          p_get_rb_route,
5035                          l_found,
5036                          l_balance_value);
5037 --
5038      l_balance_cache(cnt).balance_found := l_found;
5039      l_balance_cache(cnt).balance_value := l_balance_value;
5040      l_balance_cache(cnt).balance_dimension_id := l_defbal_rec.balance_dimension_id;
5041      l_balance_cache(cnt).balance_type_id := l_defbal_rec.balance_type_id;
5042      l_balance_cache(cnt).jurisdiction_level := l_defbal_rec.jurisdiction_lvl;
5043 --
5044    end loop;
5045 --
5046    -- any balance that reaches this point that is not found
5047    -- must be capable of being retrieved by batch.
5048    --
5049    for cnt in 1..p_defined_balance_lst.count loop
5050 --
5051      -- Look for any balances that are not found
5052 --
5053      if (l_balance_cache(cnt).balance_found = FALSE) then
5054 --
5055         load_defbal_cache(p_defined_balance_lst(cnt).defined_balance_id,
5056                           l_defbal_rec);
5057 --
5058         l_batch_bd_id := l_balance_cache(cnt).balance_dimension_id;
5059         l_batch_list.delete;
5060         l_batch_cnt := 1;
5061         l_jur_lvl := l_defbal_rec.jurisdiction_lvl;
5062         l_batch_list(l_batch_cnt).balance_type_id := l_balance_cache(cnt).balance_type_id;
5063         l_batch_list(l_batch_cnt).source_index := cnt;
5064         for bd_cnt in cnt+1..p_defined_balance_lst.count loop
5065 --
5066           if (   l_balance_cache(bd_cnt).balance_dimension_id = l_batch_bd_id
5067              and l_balance_cache(bd_cnt).jurisdiction_level = l_jur_lvl
5068              and l_balance_cache(bd_cnt).balance_found = FALSE) then
5069              l_batch_cnt := l_batch_cnt + 1;
5070              l_batch_list(l_batch_cnt).balance_type_id :=
5071                              l_balance_cache(bd_cnt).balance_type_id;
5072              l_batch_list(l_batch_cnt).source_index := bd_cnt;
5073           end if;
5074 --
5075         end loop;
5076 --
5077         -- Now we have set up the batch get the values
5078 --
5079         l_decode_required := FALSE;
5080         if (l_defbal_rec.decode_required = 'Y') then
5081             l_decode_required := TRUE;
5082         end if;
5083 --
5084         run_rr_route(
5085                  TRUE,
5086         --         null,
5087                  1,
5088                  l_batch_list,
5089                  l_defbal_rec.dim_rou_rr_route_id,
5090                  l_defbal_rec.balance_type_column,
5091                  g_dimrou_cache(l_defbal_rec.rr_ptr).retrieval_column,
5092                  l_decode_required,
5093                  l_jur_lvl,
5094                  l_dummy_value
5095                 );
5096         --
5097         -- Now match the results up
5098         --
5099         -- Use the source index pointer to point back to
5100         -- the original entry.
5101 --
5102         for l_batch_cnt in 1..l_batch_list.count loop
5103           l_balance_cache(l_batch_list(l_batch_cnt).source_index).balance_value
5104                 := l_batch_list(l_batch_cnt).balance_value;
5105           l_balance_cache(l_batch_list(l_batch_cnt).source_index).balance_found
5106                 := TRUE;
5107         end loop;
5108 --
5109      end if;
5110 --
5111    end loop;
5112 --
5113 -- Now copy the results into the output cache
5114 --
5115    for cnt in 1..p_defined_balance_lst.count loop
5116      p_defined_balance_lst(cnt).balance_value := l_balance_cache(cnt).balance_value;
5117    end loop;
5118 --
5119 end get_value;
5120 --
5121 function get_value
5122 (
5123     p_defined_balance_id   in number,
5124     p_assignment_action_id in number,
5125     p_tax_unit_id          in number,
5126     p_jurisdiction_code    in varchar2,
5127     p_source_id            in number,
5128     p_tax_group            in varchar2,
5129     p_date_earned          in date
5130 ) return number is
5131 p_balance_result number;
5132 begin
5133    g_debug := hr_utility.debug_enabled;
5134 --
5135    if p_tax_unit_id is not null then
5136       set_context('TAX_UNIT_ID', p_tax_unit_id);
5137    end if;
5138    if p_jurisdiction_code is not null then
5139       set_context('JURISDICTION_CODE', p_jurisdiction_code);
5140    end if;
5141    if p_source_id is not null then
5142       set_context('SOURCE_ID', p_source_id);
5143    end if;
5144    if p_tax_group is not null then
5145       set_context('TAX_GROUP', p_tax_group);
5146    end if;
5147    if p_date_earned is not null then
5148       set_context('DATE_EARNED', to_char(p_date_earned, 'YYYY/MM/DD HH24:MI:SS'));
5149    end if;
5150 --
5151    p_balance_result := get_value (p_defined_balance_id,
5152                                   p_assignment_action_id,
5153                                   false);  -- look for a latest balance first
5154    return p_balance_result;
5155 end get_value;
5156 --
5157 function get_value
5158 (
5159     p_defined_balance_id   in number,
5160     p_assignment_action_id in number,
5161     p_tax_unit_id          in number,
5162     p_jurisdiction_code    in varchar2,
5163     p_source_id            in number,
5164     p_source_text          in varchar2,
5165     p_tax_group            in varchar2,
5166     p_date_earned          in date
5167 ) return number is
5168 p_balance_result number;
5169 begin
5170    g_debug := hr_utility.debug_enabled;
5171 --
5172    if p_tax_unit_id is not null then
5173       set_context('TAX_UNIT_ID', p_tax_unit_id);
5174    end if;
5175    if p_jurisdiction_code is not null then
5176       set_context('JURISDICTION_CODE', p_jurisdiction_code);
5177    end if;
5178    if p_source_id is not null then
5179       set_context('SOURCE_ID', p_source_id);
5180    end if;
5181    if p_source_text is not null then
5182       set_context('SOURCE_TEXT', p_source_text);
5183    end if;
5184    if p_tax_group is not null then
5185       set_context('TAX_GROUP', p_tax_group);
5186    end if;
5187    if p_date_earned is not null then
5188       set_context('DATE_EARNED', to_char(p_date_earned, 'YYYY/MM/DD HH24:MI:SS'));
5189    end if;
5190 --
5191    p_balance_result := get_value (p_defined_balance_id,
5192                                   p_assignment_action_id,
5193                                   false);  -- look for a latest balance first
5194    return p_balance_result;
5195 end get_value;
5196 --
5197 -- Added to support original entri id context.
5198 --
5199 function get_value
5200 (
5201     p_defined_balance_id   in number,
5202     p_assignment_action_id in number,
5203     p_tax_unit_id          in number,
5204     p_jurisdiction_code    in varchar2,
5205     p_source_id            in number,
5206     p_source_text          in varchar2,
5207     p_tax_group            in varchar2,
5208     p_original_entry_id    in number,
5209     p_date_earned          in date
5210 ) return number is
5211 p_balance_result number;
5212 begin
5213    g_debug := hr_utility.debug_enabled;
5214 --
5215    if p_tax_unit_id is not null then
5216       set_context('TAX_UNIT_ID', p_tax_unit_id);
5217    end if;
5218    if p_jurisdiction_code is not null then
5219       set_context('JURISDICTION_CODE', p_jurisdiction_code);
5220    end if;
5221    if p_source_id is not null then
5222       set_context('SOURCE_ID', p_source_id);
5223    end if;
5224    if p_source_text is not null then
5225       set_context('SOURCE_TEXT', p_source_text);
5226    end if;
5227    if p_tax_group is not null then
5228       set_context('TAX_GROUP', p_tax_group);
5229    end if;
5230    if p_original_entry_id is not null then
5231       set_context('ORIGINAL_ENTRY_ID', p_original_entry_id);
5232    end if;
5233    if p_date_earned is not null then
5234       set_context('DATE_EARNED', to_char(p_date_earned, 'YYYY/MM/DD HH24:MI:SS'));
5235    end if;
5236 --
5237    p_balance_result := get_value (p_defined_balance_id,
5238                                   p_assignment_action_id,
5239                                   false);  -- look for a latest balance first
5240    return p_balance_result;
5241 end get_value;
5242 --
5243 function get_value
5244 (
5245     p_defined_balance_id   in number,
5246     p_assignment_action_id in number
5247 ) return number is
5248 p_balance_result number;
5249 begin
5250    g_debug := hr_utility.debug_enabled;
5251    p_balance_result := get_value (p_defined_balance_id,
5252                                   p_assignment_action_id,
5253                                   false);  -- look for a latest balance first
5254    return p_balance_result;
5255 end get_value;
5256 --
5257 function get_value
5258 (
5259     p_defined_balance_id   in number,
5260     p_assignment_action_id in number,
5261     p_always_get_db_item   in boolean
5262 ) return number is
5263 p_balance_result number;
5264 begin
5265   g_debug := hr_utility.debug_enabled;
5266   p_balance_result := get_value(p_defined_balance_id   => p_defined_balance_id
5267                                ,p_assignment_action_id => p_assignment_action_id
5268                                ,p_get_rr_route         => p_always_get_db_item
5269                                ,p_get_rb_route         => false
5270                                );
5271   return p_balance_result;
5272 end get_value;
5273 --
5274 function get_value
5275 (p_defined_balance_id   in number
5276 ,p_assignment_action_id in number
5277 ,p_tax_unit_id          in number
5278 ,p_jurisdiction_code    in varchar2
5279 ,p_source_id            in number
5280 ,p_source_text          in varchar2
5281 ,p_tax_group            in varchar2
5282 ,p_date_earned          in date
5283 ,p_get_rr_route         in varchar2
5284 ,p_get_rb_route         in varchar2
5285 ,p_source_text2         in varchar2 default null
5286 ,p_source_number        in number   default null
5287 ,p_time_def_id          in number   default null
5288 ,p_balance_date         in date     default null
5289 ,p_payroll_id           in number   default null
5290 ,p_original_entry_id    in number   default null
5291 ,p_local_unit_id        in number   default null
5292 ,p_source_number2       in number   default null
5293 ,p_organization_id      in number   default null
5294 ) return number is
5295 p_balance_result number;
5296 l_get_rr_route   boolean;
5297 l_get_rb_route   boolean;
5298 begin
5299    g_debug := hr_utility.debug_enabled;
5300 --
5301    -- p_get_rr_route and p_get_rb_route have been set to number rather than
5302    -- boolean, so that can be called in a select statement.
5303 --
5304    if nvl(p_get_rr_route, 'FALSE') = 'TRUE' then
5305      l_get_rr_route := true;
5306    elsif nvl(p_get_rr_route, 'FALSE') = 'FALSE' then
5307      l_get_rr_route := false;
5308    else
5309      l_get_rr_route := false;
5310    end if;
5311 --
5312    if nvl(p_get_rb_route, 'FALSE') = 'TRUE' then
5313      l_get_rb_route := true;
5314    elsif nvl(p_get_rb_route, 'FALSE') = 'FALSE' then
5315      l_get_rb_route := false;
5316    else
5317      l_get_rb_route := false;
5318    end if;
5319 --
5320    if p_tax_unit_id is not null then
5321       set_context('TAX_UNIT_ID', p_tax_unit_id);
5322    end if;
5323    if p_jurisdiction_code is not null then
5324       set_context('JURISDICTION_CODE', p_jurisdiction_code);
5325    end if;
5326    if p_source_id is not null then
5327       set_context('SOURCE_ID', p_source_id);
5328    end if;
5329    if p_source_text is not null then
5330       set_context('SOURCE_TEXT', p_source_text);
5331    end if;
5332    if p_tax_group is not null then
5333       set_context('TAX_GROUP', p_tax_group);
5334    end if;
5335    if p_date_earned is not null then
5336       set_context('DATE_EARNED', to_char(p_date_earned, 'YYYY/MM/DD HH24:MI:SS'));
5337    end if;
5338    if p_source_text2 is not null then
5339       set_context('SOURCE_TEXT2', p_source_text2);
5340    end if;
5341    if p_source_number is not null then
5342       set_context('SOURCE_NUMBER', p_source_number);
5343    end if;
5344    if p_time_def_id is not null then
5345       set_context('TIME_DEFINITION_ID', p_time_def_id);
5346    end if;
5347    if p_balance_date is not null then
5348       set_context('BALANCE_DATE', to_char(p_balance_date, 'YYYY/MM/DD HH24:MI:SS'));
5349    end if;
5350    if p_payroll_id is not null then
5351       set_context('PAYROLL_ID', p_payroll_id);
5352    end if;
5353    if p_original_entry_id is not null then
5354       set_context('ORIGINAL_ENTRY_ID', p_original_entry_id);
5355    end if;
5356    if p_local_unit_id is not null then
5357       set_context('LOCAL_UNIT_ID', p_local_unit_id);
5358    end if;
5359    if p_source_number2 is not null then
5360       set_context('SOURCE_NUMBER2', p_source_number2);
5361    end if;
5362    if p_organization_id is not null then
5363       set_context('ORGANIZATION_ID', p_organization_id);
5364    end if;
5365 --
5366    p_balance_result := get_value
5367                         (p_defined_balance_id   => p_defined_balance_id
5368                         ,p_assignment_action_id => p_assignment_action_id
5369                         ,p_get_rr_route         => l_get_rr_route
5370                         ,p_get_rb_route         => l_get_rb_route
5371                         );
5372   return p_balance_result;
5373 end get_value;
5374 --
5375 function get_value
5376 (
5377     p_defined_balance_id   in number
5378 ,   p_assignment_action_id in number
5379 ,   p_get_rr_route         in boolean
5380 ,   p_get_rb_route         in boolean
5381 ) return number is p_balance_result number;
5382 --
5383 l_balance_value         pay_person_latest_balances.value%type;
5384 l_found                 boolean;
5385 l_defbal_rec            t_def_bal_rec;
5386 --
5387 ------------------------- get_value (action mode) -------------------------
5388 --
5389 begin
5390   g_debug := hr_utility.debug_enabled;
5391   if g_debug then
5392      hr_utility.set_location ('pay_balance_pkg.get_value - ACTION MODE', 1);
5393      hr_utility.trace ('def_bal_id    = ' || to_char(p_defined_balance_id));
5394      hr_utility.trace ('asg_action_id = ' || to_char(p_assignment_action_id));
5395   end if;
5396   --
5397   -- Set up the context of assignment action:
5398   --
5399   set_context ('ASSIGNMENT_ACTION_ID', p_assignment_action_id);
5400   --
5401   load_defbal_cache(p_defined_balance_id,
5402                     l_defbal_rec);
5403   --
5404   get_value_internal (p_defined_balance_id,
5405                       l_defbal_rec,
5406                       p_assignment_action_id,
5407                       p_get_rr_route,
5408                       p_get_rb_route,
5409                       l_found,
5410                       l_balance_value);
5411   --
5412   -- The only time get_value_internal does not return a
5413   -- balance value is when it is a batch enabled RR route.
5414   --
5415   if (l_found = FALSE) then
5416     get_rr_value(p_defined_balance_id,
5417                  l_balance_value);
5418   end if;
5419 --
5420   p_balance_result := l_balance_value;
5421   return p_balance_result;
5422 end get_value;  -- assignment action mode
5423 --
5424 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5425 --                                                                        +
5426 --                     get_value (date mode)                              +
5427 --                                                                        +
5428 --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5429 --
5430 /*
5431 NAME
5432   get_value        - Retrieve a balance (date mode)
5433 --
5434 DESCRIPTION
5435   The balance routes are all driven off assignment action id.  So, when a
5436   balance is required for a particular date, a payroll action and assignment
5437   action are temporarily created.  This is done by setting a savepoint,
5438   inserting a dummy payroll action for the required date, and calling the
5439   hrassact.inassact procedure to insert the corresponding assignment action
5440   and shuffle any later assignment actions along.  The get_value
5441   function is then called with the dummy assignment action, which returns the
5442   balance value for the required date.  A rollback then removes the temporary
5443   payroll action and assignment action.
5444   --
5445   The parameter 'p_always_get_db_item' if set to TRUE will always derive the
5446   balance from the route and not even try to find a latest balance value.
5447   This parameter is used for testing purposes, to verify the latest balance
5448   value.
5449   --
5450   There are 2 overloaded versions of the get_value function below.  The first
5451   one is called by the forms/ reports.  The second has the option of setting
5452   the 'p_always_get_db_item' value.  This is necessary since the forms do not
5453   support default parameters.
5454 */
5455 function get_value
5456 (
5457     p_defined_balance_id   in number,
5458     p_assignment_id        in number,
5459     p_virtual_date         in date
5460 ) return number is
5461 p_balance_result number;
5462 begin
5463   g_debug := hr_utility.debug_enabled;
5464   p_balance_result := get_value_lock (p_defined_balance_id,
5465                                       p_assignment_id,
5466                                       p_virtual_date,
5467                                       false, -- look for a latest balance first
5468                                       'Y');
5469   return p_balance_result;
5470 end get_value;
5471 --
5472 function get_value
5473 (
5474     p_defined_balance_id   in number,
5475     p_assignment_id        in number,
5476     p_virtual_date         in date,
5477     p_always_get_db_item   in boolean
5478 ) return number is
5479 p_balance_result number;
5480 begin
5481   g_debug := hr_utility.debug_enabled;
5482   p_balance_result := get_value_lock (p_defined_balance_id,
5483                                       p_assignment_id,
5484                                       p_virtual_date,
5485                                       p_always_get_db_item,
5486                                       'Y');
5487   return p_balance_result;
5488 end get_value;
5489 --
5490 function get_value_lock
5491 (
5492     p_defined_balance_id   in number,
5493     p_assignment_id        in number,
5494     p_virtual_date         in date,
5495     p_asg_lock             in varchar2
5496 ) return number is
5497 p_balance_result number;
5498 begin
5499   g_debug := hr_utility.debug_enabled;
5500   p_balance_result := get_value_lock (p_defined_balance_id,
5501                                       p_assignment_id,
5502                                       p_virtual_date,
5503                                       false, -- look for a latest balance first
5504                                       p_asg_lock);
5505   return p_balance_result;
5506 end get_value_lock;
5507 --
5508 function get_value_lock_internal
5509 (
5510     p_defined_balance_id   in number,
5511     p_assignment_id        in number,
5512     p_virtual_date         in date,
5513     p_always_get_db_item   in boolean,
5514     p_asg_lock             in varchar2
5515 ) return number is
5516 p_balance_result number;
5517 --
5518 l_payroll_id     per_all_assignments_f.payroll_id%type;
5519 l_bus_grp_id     per_all_assignments_f.business_group_id%type;
5520 l_consol_set_id  pay_all_payrolls_f.payroll_id%type;
5521 l_ass_action_id  pay_assignment_actions.assignment_action_id%type;
5522 l_pay_action_id  pay_payroll_actions.payroll_action_id%type;
5523 l_time_period_id per_time_periods.time_period_id%type;
5524 l_asg_lock       boolean;
5525 --
5526 begin
5527   g_debug := hr_utility.debug_enabled;
5528   if g_debug then
5529      hr_utility.set_location ('pay_balance_pkg.get_value - DATE MODE', 1);
5530      hr_utility.trace ('def_bal_id = ' || to_char(p_defined_balance_id));
5531      hr_utility.trace ('Assign_id  = ' || to_char(p_assignment_id));
5532      hr_utility.trace ('V_date     = ' || to_char (p_virtual_date));
5533   end if;
5534   --
5535   -- Set the assignment locking flag.
5536   --
5537   l_asg_lock := FALSE;
5538   if (p_asg_lock = 'Y') then
5539      l_asg_lock := TRUE;
5540   end if;
5541   --
5542   SAVEPOINT bal_date_mode;
5543   --
5544   -- get the payroll information
5545   --
5546   if g_debug then
5547      hr_utility.set_location ('pay_balance_pkg.get_value', 10);
5548   end if;
5549   select ASSIGN.payroll_id,
5550          ASSIGN.business_group_id,
5551          PAYROLL.consolidation_set_id
5552   into   l_payroll_id,
5553          l_bus_grp_id,
5554          l_consol_set_id
5555   from   per_all_assignments_f   ASSIGN
5556   ,      pay_all_payrolls_f      PAYROLL
5557   where  ASSIGN.assignment_id  = p_assignment_id
5558   and    p_virtual_date  between ASSIGN.effective_start_date
5559                              and ASSIGN.effective_end_date
5560   and    PAYROLL.payroll_id    = ASSIGN.payroll_id
5561   and    p_virtual_date  between PAYROLL.effective_start_date
5562                              and PAYROLL.effective_end_date;
5563   --
5564   -- If there is a time period id, then get it, else use a null value:
5565   --
5566   begin
5567     if g_debug then
5568        hr_utility.set_location ('pay_balance_pkg.get_value', 12);
5569     end if;
5570     select TIMEP.time_period_id
5571     into   l_time_period_id
5572     from   per_time_periods        TIMEP
5573     where  TIMEP.payroll_id      = l_payroll_id
5574     and    p_virtual_date  between TIMEP.start_date
5575                                and TIMEP.end_date;
5576   exception
5577     when no_data_found then
5578       if g_debug then
5579          hr_utility.set_location ('pay_balance_pkg.get_value', 13);
5580       end if;
5581       l_time_period_id := null;
5582   end;
5583   --
5584   -- get the next value for payroll action id
5585   --
5586   select pay_payroll_actions_s.nextval
5587   into   l_pay_action_id
5588   from   dual;
5589   --
5590   -- insert a temporary row into pay_payroll_actions
5591   --
5592   if g_debug then
5593      hr_utility.set_location ('pay_balance_pkg.get_value', 20);
5594   end if;
5595   insert into pay_payroll_actions
5596   (payroll_action_id,
5597    action_type,
5598    business_group_id,
5599    consolidation_set_id,
5600    payroll_id,
5601    action_population_status,
5602    action_status,
5603    effective_date,
5604    date_earned,
5605    time_period_id,
5606    object_version_number)
5607   values
5608   (l_pay_action_id,
5609    'N',                           -- not tracked action type
5610    l_bus_grp_id,
5611    l_consol_set_id,
5612    l_payroll_id,
5613    'U',
5614    'U',
5615    p_virtual_date,
5616    p_virtual_date,
5617    l_time_period_id,
5618    1);
5619   --
5620   -- now insert the assignment action:
5621   --
5622   if g_debug then
5623      hr_utility.set_location ('pay_balance_pkg.get_value', 25);
5624   end if;
5625   hrassact.inassact (pactid     => l_pay_action_id,
5626                      asgid      => p_assignment_id,
5627                      p_asg_lock => l_asg_lock);
5628   --
5629   -- retrieve the assignment action id:
5630   --
5631   if g_debug then
5632      hr_utility.set_location ('pay_balance_pkg.get_value', 30);
5633   end if;
5634   select assignment_action_id
5635   into   l_ass_action_id
5636   from   pay_assignment_actions
5637   where  payroll_action_id = l_pay_action_id;
5638   --
5639   if g_debug then
5640      hr_utility.trace ('Assignment action id = ' || to_char (l_ass_action_id));
5641   end if;
5642   --
5643   -- Now retrieve the balance for this temp. assignment action:
5644   --
5645   p_balance_result := get_value (p_defined_balance_id,
5646                                  l_ass_action_id,
5647                                  p_always_get_db_item);
5648   rollback to bal_date_mode;
5649   return p_balance_result;
5650 --
5651 exception
5652   when others then
5653     rollback to bal_date_mode;
5654     raise;
5655 end get_value_lock_internal;
5656 --
5657 -- This function returns the latest assignment action ID given an assignment
5658 -- and effective date. This is called from all Date Mode functions.
5659 --
5660 FUNCTION get_latest_action_id (p_mode           IN VARCHAR2,
5661                                p_assignment_id  IN NUMBER,
5662                                p_effective_date IN DATE)
5663 RETURN NUMBER IS
5664 --
5665    l_assignment_action_id       NUMBER;
5666 --
5667 cursor get_latest_id (c_assignment_id IN NUMBER,
5668                       c_effective_date IN DATE) is
5669     SELECT /*+ USE_NL(paa, ppa) */
5670          fnd_number.canonical_to_number(substr(max(lpad(paa.action_sequence,15,'
5671 0')||
5672          paa.assignment_action_id),16))
5673     FROM pay_assignment_actions paa,
5674          pay_payroll_actions    ppa
5675     WHERE
5676          paa.assignment_id = c_assignment_id
5677     AND  ppa.payroll_action_id = paa.payroll_action_id
5678     AND  ppa.effective_date <= c_effective_date
5679     AND  ppa.action_type        in ('R', 'Q', 'I', 'V', 'B');
5680 --
5681 cursor get_per_latest_id (c_assignment_id IN NUMBER,
5682                           c_effective_date IN DATE) is
5683     SELECT /*+ INDEX(PAF1 PER_ASSIGNMENTS_F_PK)
5684                INDEX(PAF2 PER_ASSIGNMENTS_F_N12)
5685                INDEX(PAA PAY_ASSIGNMENT_ACTIONS_N51)
5686                INDEX(PPA PAY_PAYROLL_ACTIONS_PK)
5687                USE_NL(paa, ppa) */
5688          fnd_number.canonical_to_number(substr(max(lpad(paa.action_sequence,15,'0')||
5689          paa.assignment_action_id),16))
5690     FROM pay_assignment_actions paa,
5691          pay_payroll_actions    ppa,
5692          per_all_assignments_f  paf1,
5693          per_all_assignments_f  paf2
5694     WHERE
5695          paf1.assignment_id = c_assignment_id
5696     AND  c_effective_date between paf1.effective_start_date
5697                               and paf1.effective_end_date
5698     AND  paf2.person_id     = paf1.person_id
5699     AND  paf2.effective_start_date = (select max(paf3.effective_start_date)
5700                                         from per_all_assignments_f paf3
5701                                        where paf3.assignment_id = paf2.assignment_id
5702                                          and paf3.effective_start_date <= c_effective_date)
5703     AND  paf2.assignment_id = paa.assignment_id
5704     AND  ppa.payroll_action_id = paa.payroll_action_id
5705     AND  ppa.effective_date <= c_effective_date
5706     AND  ppa.action_type        in ('R', 'Q', 'I', 'V', 'B');
5707 --
5708 BEGIN
5709 --
5710     if (p_mode = 'ASG') then
5711 --
5712       open get_latest_id(p_assignment_id, p_effective_date);
5713       fetch get_latest_id into l_assignment_action_id;
5714       close get_latest_id;
5715 --
5716     elsif (p_mode = 'PER') then
5717 --
5718       open get_per_latest_id(p_assignment_id, p_effective_date);
5719       fetch get_per_latest_id into l_assignment_action_id;
5720       close get_per_latest_id;
5721 --
5722     end if;
5723 --
5724 RETURN l_assignment_action_id;
5725 --
5726 END get_latest_action_id;
5727 --
5728 procedure get_value_seq_internal
5729 (
5730     p_defined_balance_id   in     number,
5731     p_assignment_id        in     number,
5732     p_virtual_date         in     date,
5733     p_always_get_db_item   in     boolean,
5734     p_defbal_rec           in     t_def_bal_rec,
5735     p_balance_value           out nocopy number
5736 )
5737 is
5738 l_assignment_action_id pay_assignment_actions.assignment_action_id%type;
5739 l_pact_payroll_id      pay_payroll_actions.payroll_action_id%type;
5740 l_pact_eff_date        date;
5741 l_expiry_date          date;
5742 l_balance_value        number := null;
5743 l_found                boolean;
5744 begin
5745 --
5746    if (p_defbal_rec.dimension_lvl = 'PER') then
5747       l_assignment_action_id := get_latest_action_id(
5748                                                      'PER',
5749                                                      p_assignment_id,
5750                                                      p_virtual_date);
5751    elsif (p_defbal_rec.dimension_lvl = 'ASG') then
5752       l_assignment_action_id := get_latest_action_id(
5753                                                      'ASG',
5754                                                      p_assignment_id,
5755                                                      p_virtual_date);
5756    end if;
5757 --
5758    /* If we have an assignment action we can use get value */
5759    if (l_assignment_action_id is not null) then
5760 --
5761       set_context ('ASSIGNMENT_ACTION_ID', l_assignment_action_id);
5762 --
5763       get_value_internal (p_defined_balance_id,
5764                           p_defbal_rec,
5765                           l_assignment_action_id,
5766                           p_always_get_db_item,
5767                           FALSE,
5768                           l_found,
5769                           l_balance_value);
5770       --
5771       -- The only time get_value_internal does not return a
5772       -- balance value is when it is a batch enabled RR route.
5773       --
5774       if (l_found = FALSE) then
5775         get_rr_value(p_defined_balance_id,
5776                      l_balance_value);
5777       end if;
5778 --
5779       /* OK now we have the value we need to do the expiry checking
5780       */
5781 --
5782       select ppa.effective_date,
5783              ppa.payroll_id
5784         into l_pact_eff_date,
5785              l_pact_payroll_id
5786         from pay_payroll_actions ppa,
5787              pay_assignment_actions paa
5788        where paa.assignment_action_id = l_assignment_action_id
5789          and paa.payroll_action_id = ppa.payroll_action_id;
5790 --
5791       if (p_defbal_rec.period_type is not null
5792          and p_defbal_rec.period_type in ('YEAR',
5793                                           'QUARTER',
5794                                           'PERIOD',
5795                                           'MONTH')
5796         ) then
5797 
5798             get_period_type_end(p_period_type => p_defbal_rec.period_type,
5799                                 p_effective_date  => l_pact_eff_date,
5800                                 p_end_date        => l_expiry_date,
5801                                 p_payroll_id      => l_pact_payroll_id);
5802 --
5803             if (l_expiry_date < p_virtual_date) then
5804 --
5805               /* Balance value has expired */
5806               l_balance_value := 0;
5807             end if;
5808 --
5809       elsif (nvl(p_defbal_rec.expiry_check_lvl, 'N') = 'N') then
5810 --
5811             /* Never expires, hence use value */
5812             null;
5813 --
5814       end if;
5815 --
5816   end if;
5817 --
5818   p_balance_value := l_balance_value;
5819 --
5820 end get_value_seq_internal;
5821 --
5822 function get_value_lock
5823 (
5824     p_defined_balance_id   in number,
5825     p_assignment_id        in number,
5826     p_virtual_date         in date,
5827     p_always_get_db_item   in boolean,
5828     p_asg_lock             in varchar2
5829 ) return number is
5830 --
5831 l_balance_value number;
5832 l_defbal_rec    t_def_bal_rec;
5833 use_date_get_val boolean;
5834 use_seq_get_val boolean;
5835 --
5836 begin
5837 --
5838   use_date_get_val := TRUE;
5839   use_seq_get_val := FALSE;
5840 --
5841   load_defbal_cache(p_defined_balance_id,
5842                     l_defbal_rec);
5843 --
5844   /* If its a group level balance forget it, use the date get value
5845   */
5846   if (    l_defbal_rec.dimension_lvl is not null
5847       and l_defbal_rec.dimension_lvl <> 'GRP') then
5848 --
5849      /* OK if it is a Run or Payment balance then it
5850         will always be 0 in date mode
5851      */
5852      if (l_defbal_rec.dimension_type = 'R') then
5853 --
5854          l_balance_value := 0;
5855          use_date_get_val := FALSE;
5856 --
5857      elsif (l_defbal_rec.period_type is not null
5858             and l_defbal_rec.period_type in ('PAYMENT', 'RUN')) then
5859 --
5860          l_balance_value := 0;
5861          use_date_get_val := FALSE;
5862 --
5863      /* If it's not a complex balance we may be able to get the value
5864      */
5865      elsif (l_defbal_rec.expiry_check_lvl <> 'E') then
5866 --
5867         /* At the moment we can only support certain periods
5868         */
5869         if (l_defbal_rec.period_type is not null
5870             and l_defbal_rec.period_type in ('YEAR',
5871                                              'QUARTER',
5872                                              'PERIOD',
5873                                              'MONTH')
5874            ) then
5875 --
5876                use_seq_get_val := TRUE;
5877 --
5878         elsif (nvl(l_defbal_rec.expiry_check_lvl, 'N') = 'N') then
5879 --
5880                use_seq_get_val := TRUE;
5881 --
5882         end if;
5883 --
5884         if (use_seq_get_val = TRUE) then
5885 --
5886             get_value_seq_internal
5887             (
5888                 p_defined_balance_id   => p_defined_balance_id,
5889                 p_assignment_id        => p_assignment_id,
5890                 p_virtual_date         => p_virtual_date,
5891                 p_always_get_db_item   => p_always_get_db_item,
5892                 p_defbal_rec           => l_defbal_rec,
5893                 p_balance_value        => l_balance_value
5894             );
5895 --
5896             if (l_balance_value is not null) then
5897                 use_date_get_val := FALSE;
5898             end if;
5899 --
5900         end if;
5901 --
5902      end if;
5903 --
5904   end if;
5905 --
5906   if (use_date_get_val = TRUE) then
5907 --
5908       l_balance_value := get_value_lock_internal
5909                           (
5910                               p_defined_balance_id   => p_defined_balance_id,
5911                               p_assignment_id        => p_assignment_id,
5912                               p_virtual_date         => p_virtual_date,
5913                               p_always_get_db_item   => p_always_get_db_item,
5914                               p_asg_lock             => p_asg_lock
5915                           );
5916   end if;
5917 --
5918   return l_balance_value;
5919 --
5920 end get_value_lock;
5921 --
5922 --------------------------------------------------------------------------
5923 -- procedure invalidate_run_balances
5924 -- Bug 3397712 - removed the filter on run balances from the cursor
5925 -- get_def_bals, as this was preventing rows being returned that should be
5926 -- invalidated when a feed is added to a balance.
5927 --------------------------------------------------------------------------
5928 procedure invalidate_run_balances(p_balance_type_id in number,
5929                                   p_input_value_id  in number,
5930                                   p_invalid_date    in date)
5931 is
5932 --
5933 cursor get_def_bals(p_bal_id number,
5934                     p_iv_id  number)
5935 is
5936 select pdb.defined_balance_id
5937 from   pay_defined_balances pdb,
5938        pay_balance_validation pbv
5939 where  pdb.balance_type_id = p_bal_id
5940 and    pdb.save_run_balance = 'Y'
5941 and    pbv.defined_balance_id = pdb.defined_balance_id
5942 and    pbv.run_balance_status in ('P', 'V');
5943 
5944 -- Select if run result value exists for input value
5945 cursor ivchk is
5946 select '1' from dual
5947  where exists
5948     (select /*+ ORDERED INDEX(RRV PAY_RUN_RESULT_VALUES_PK)
5949                         INDEX(PRR PAY_RUN_RESULTS_PK) */
5950             1
5951        from pay_run_result_values rrv,
5952             pay_run_results prr
5953       where rrv.input_value_id = p_input_value_id
5954         and prr.run_result_id  = rrv.run_result_id
5955         and prr.status         in ('P', 'PA')
5956         and nvl(rrv.result_value, '0') <> '0');
5957 
5958 l_ivchk varchar2(2);
5959 l_rrv_found number := -1;
5960 --
5961 BEGIN
5962   g_debug := hr_utility.debug_enabled;
5963   if g_debug then
5964      hr_utility.set_location('Entering: pay_balance_pkg.invalidate_run_balances',5);
5965   end if;
5966 --
5967   if HRASSACT.CHECK_LATEST_BALANCES = TRUE then
5968 --
5969     for each_row in get_def_bals(p_balance_type_id, p_input_value_id) loop
5970 --
5971       if l_rrv_found = -1 then
5972          open ivchk;
5973 
5974          fetch ivchk
5975          into l_ivchk;
5976 
5977          if ivchk%FOUND then
5978             l_rrv_found := 1;
5979          else
5980             l_rrv_found := 0;
5981             close ivchk;
5982             exit;
5983          end if;
5984          close ivchk;
5985       end if;
5986 
5987       if l_rrv_found = 1 then
5988          update pay_balance_validation
5989          set    run_balance_status = 'I'
5990          where   defined_balance_id = each_row.defined_balance_id;
5991          if g_debug then
5992             hr_utility.set_location('pay_balance_pkg.invalidate_run_balances', 15);
5993          end if;
5994       end if;
5995    end loop;
5996    if g_debug then
5997        hr_utility.set_location('Leaving: pay_balance_pkg.invalidate_run_balances', 20);
5998     end if;
5999 --
6000   end if;
6001 --
6002 END invalidate_run_balances;
6003 --------------------------------------------------------------------------
6004 -- procedure invalidate_run_balances
6005 --------------------------------------------------------------------------
6006 procedure invalidate_run_balances(p_balance_type_id in number,
6007                                   p_invalid_date    in date)
6008 is
6009 --
6010 cursor get_def_bals(p_bal_id number)
6011 is
6012 select pdb.defined_balance_id
6013 from   pay_defined_balances pdb
6014 where  pdb.balance_type_id = p_bal_id
6015 and    pdb.save_run_balance = 'Y';
6016 --
6017 BEGIN
6018   g_debug := hr_utility.debug_enabled;
6019   if g_debug then
6020      hr_utility.set_location('Entering: pay_balance_pkg.invalidate_run_balances',5);
6021   end if;
6022   for each_row in get_def_bals(p_balance_type_id) loop
6023 --
6024     update pay_balance_validation
6025     set    run_balance_status = 'I'
6026     where   defined_balance_id = each_row.defined_balance_id;
6027     if g_debug then
6028        hr_utility.set_location('pay_balance_pkg.invalidate_run_balances', 15);
6029     end if;
6030   end loop;
6031   if g_debug then
6032      hr_utility.set_location('Leaving: pay_balance_pkg.invalidate_run_balances', 20);
6033   end if;
6034 END invalidate_run_balances;
6035 --------------------------------------------------------------------------
6036 -- get_run_result_info
6037 --------------------------------------------------------------------------
6038 -- Description: This procedure returns the information related to the
6039 --              specified run result ID.
6040 --
6041 procedure get_run_result_info
6042   (p_run_result_id  in         number
6043   ,p_run_result_rec out nocopy t_run_result_rec
6044   )
6045 is
6046   --
6047   l_rr_rec         t_run_result_rec;
6048   --
6049   cursor csr_rr
6050   is
6051   select
6052     prr.run_result_id
6053    ,prr.element_type_id
6054    ,prr.jurisdiction_code
6055    ,paa.assignment_action_id
6056    ,paa.assignment_id
6057    ,paa.tax_unit_id
6058    ,paa.payroll_action_id
6059    ,prr.time_definition_id
6060    ,prr.end_date
6061    ,prr.local_unit_id
6062   from
6063     pay_assignment_actions   paa
6064    ,pay_run_results          prr
6065   where
6066       paa.assignment_action_id = prr.assignment_action_id
6067   and prr.run_result_id        = p_run_result_id
6068   ;
6069 
6070   cursor csr_ppa(p_payroll_action_id number)
6071   is
6072   select
6073     ppa.effective_date
6074    ,pbg.legislation_code
6075    ,ppa.business_group_id
6076   from
6077     per_business_groups_perf pbg
6078    ,pay_payroll_actions      ppa
6079   where
6080       pbg.business_group_id    = ppa.business_group_id
6081   and ppa.payroll_action_id    = p_payroll_action_id
6082   ;
6083 
6084 begin
6085   --
6086   -- Check if cache exists.
6087   --
6088   if p_run_result_id = g_run_result_rec.run_result_id then
6089     p_run_result_rec := g_run_result_rec;
6090   else
6091     --
6092     -- Get the run result info.
6093     --
6094     open csr_rr;
6095     fetch csr_rr into l_rr_rec.run_result_id
6096                      ,l_rr_rec.element_type_id
6097                      ,l_rr_rec.jurisdiction_code
6098                      ,l_rr_rec.assignment_action_id
6099                      ,l_rr_rec.assignment_id
6100                      ,l_rr_rec.tax_unit_id
6101                      ,l_rr_rec.payroll_action_id
6102                      ,l_rr_rec.time_def_id
6103                      ,l_rr_rec.balance_date
6104                      ,l_rr_rec.local_unit_id;
6105     close csr_rr;
6106     --
6107     -- Check if the old cache holds the same payroll action info.
6108     --
6109     if l_rr_rec.payroll_action_id = g_run_result_rec.payroll_action_id then
6110       --
6111       l_rr_rec.effective_date   := g_run_result_rec.effective_date;
6112       l_rr_rec.legislation_code := g_run_result_rec.legislation_code;
6113       l_rr_rec.business_group_id:= g_run_result_rec.business_group_id;
6114       --
6115     else
6116       --
6117       open csr_ppa(l_rr_rec.payroll_action_id);
6118       fetch csr_ppa into l_rr_rec.effective_date
6119                         ,l_rr_rec.legislation_code
6120                         ,l_rr_rec.business_group_id;
6121       close csr_ppa;
6122       --
6123     end if;
6124     -- reset the global cache.
6125     g_run_result_rec := l_rr_rec;
6126     p_run_result_rec := l_rr_rec;
6127     --
6128   end if;
6129 
6130 end get_run_result_info;
6131 --
6132 --------------------------------------------------------------------------
6133 -- find_context
6134 --------------------------------------------------------------------------
6135 function find_context(p_context_name in varchar2,
6136                       p_context_id   in number) return varchar2
6137 is
6138 --
6139 cursor get_sval(p_rr_id in number,
6140                 p_iv_name in varchar2,
6141                 p_ele_id  in number,
6142                 p_effdate in date) is
6143 select prrv.result_value
6144   from pay_run_result_values  prrv,
6145        pay_input_values_f     piv
6146  where prrv.run_result_id = p_rr_id
6147    and piv.name = p_iv_name
6148    and piv.input_value_id = prrv.input_value_id
6149    and piv.element_type_id = p_ele_id
6150    and p_effdate between piv.effective_start_date
6151                      and piv.effective_end_date;
6152 --
6153 cnt_value pay_run_result_values.result_value%type;
6154 l_rr_rec           t_run_result_rec;
6155 l_inp_val_name pay_input_values_f.name%type;
6156 l_found boolean;
6157 --
6158 begin
6159 --
6160    get_run_result_info(p_context_id, l_rr_rec);
6161 --
6162    pay_core_utils.get_leg_context_iv_name(p_context_name,
6163                                           l_rr_rec.legislation_code,
6164                                           l_inp_val_name,
6165                                           l_found
6166                                          );
6167 --
6168    if (l_found = TRUE) then
6169 --
6170      cnt_value := null;
6171      open get_sval(p_context_id,
6172                    l_inp_val_name,
6173                    l_rr_rec.element_type_id,
6174                    l_rr_rec.effective_date);
6175      fetch get_sval into cnt_value;
6176      close get_sval;
6177 --
6178      return cnt_value;
6179 --
6180    else
6181      return null;
6182    end if;
6183 --
6184 end find_context;
6185 --
6186 --------------------------------------------------------------------------
6187 -- search_rb_cache
6188 -- Description:
6189 --    Search a run balance cache for a specified run balance.
6190 --------------------------------------------------------------------------
6191 procedure search_rb_cache(
6192                           p_defined_balanceid  in            number,
6193                           p_payroll_action_id  in            number,
6194                           p_tax_unit_id        in            number,
6195                           p_jur_code           in            varchar2,
6196                           p_src_id             in            number,
6197                           p_src_txt            in            varchar2,
6198                           p_src_num            in            number,
6199                           p_src_txt2           in            varchar2,
6200                           p_time_def_id        in            number,
6201                           p_balance_date       in            date,
6202                           p_local_unit_id      in            number,
6203                           p_source_number2     in            number,
6204                           p_organization_id    in            number,
6205                           p_int_mlt_thrd_cache in            t_int_rb_cache,
6206                           p_grp_rb_ptr_list    in            number_array,
6207                           p_current_ptr           out nocopy number,
6208                           p_previous_ptr          out nocopy number,
6209                           p_found                 out nocopy boolean
6210                          )
6211 is
6212 l_found boolean;
6213 prev_ptr number;
6214 current_ptr number;
6215 begin
6216 --
6217   if g_debug then
6218      hr_utility.set_location('Entering: pay_balance_pkg.search_rb_cache', 5);
6219   end if;
6220   if (p_grp_rb_ptr_list.exists(p_defined_balanceid)) then
6221 --
6222     current_ptr := p_grp_rb_ptr_list(p_defined_balanceid);
6223     prev_ptr := null;
6224     l_found := FALSE;
6225     while (current_ptr is not null and
6226            l_found = FALSE) loop
6227 --
6228       if        p_int_mlt_thrd_cache(current_ptr).defined_balance_id         = p_defined_balanceid
6229         and     p_int_mlt_thrd_cache(current_ptr).payroll_action_id          = p_payroll_action_id
6230         and nvl(p_int_mlt_thrd_cache(current_ptr).tax_unit_id, -1)           = nvl(p_tax_unit_id, -1)
6231         and nvl(p_int_mlt_thrd_cache(current_ptr).jurisdiction_code, 'null') = nvl(p_jur_code, 'null')
6232         and nvl(p_int_mlt_thrd_cache(current_ptr).source_id, -1)             = nvl(p_src_id, -1)
6233         and nvl(p_int_mlt_thrd_cache(current_ptr).source_text, 'null')       = nvl(p_src_txt, 'null')
6234         and nvl(p_int_mlt_thrd_cache(current_ptr).source_number, -1)         = nvl(p_src_num, -1)
6235         and nvl(p_int_mlt_thrd_cache(current_ptr).source_text2, 'null')      = nvl(p_src_txt2, 'null')
6236         and nvl(p_int_mlt_thrd_cache(current_ptr).time_def_id, -1)           = nvl(p_time_def_id, -1)
6237         and nvl(p_int_mlt_thrd_cache(current_ptr).balance_date, to_date('0001/01/01', 'YYYY/MM/DD'))
6238                                        = nvl(p_balance_date, to_date('0001/01/01', 'YYYY/MM/DD'))
6239         and nvl(p_int_mlt_thrd_cache(current_ptr).local_unit_id, -1)         = nvl(p_local_unit_id, -1)
6240         and nvl(p_int_mlt_thrd_cache(current_ptr).source_number2, -1)        = nvl(p_source_number2, -1)
6241         and nvl(p_int_mlt_thrd_cache(current_ptr).organization_id, -1)       = nvl(p_organization_id, -1)
6242       then
6243 --
6244          l_found := TRUE;
6245          if g_debug then
6246             hr_utility.set_location('pay_balance_pkg.search_rb_cache', 10);
6247          end if;
6248 --
6249       else
6250 --
6251         prev_ptr := current_ptr;
6252         current_ptr := p_int_mlt_thrd_cache(current_ptr).next;
6253         if g_debug then
6254            hr_utility.set_location('pay_balance_pkg.search_rb_cache', 15);
6255         end if;
6256 --
6257       end if;
6258 --
6259     end loop;
6260 --
6261   else
6262     prev_ptr := null;
6263     current_ptr := null;
6264     l_found      := FALSE;
6265   end if;
6266 --
6267   p_found := l_found;
6268   p_current_ptr := current_ptr;
6269   p_previous_ptr := prev_ptr;
6270 --
6271   if g_debug then
6272      hr_utility.set_location('Leaving: pay_balance_pkg.search_rb_cache', 20);
6273   end if;
6274 --
6275 end search_rb_cache;
6276 --
6277 --------------------------------------------------------------------------
6278 -- subtract_from_grp_bal
6279 -- Description:
6280 --     Subtract a given amount from a group balance
6281 --------------------------------------------------------------------------
6282 procedure subtract_from_grp_bal(
6283                                 p_grp_def_bal_id     in            number,
6284                                 p_payroll_action_id  in            number,
6285                                 p_tax_unit_id        in            number,
6286                                 p_jur_code           in            varchar2,
6287                                 p_src_id             in            number,
6288                                 p_src_txt            in            varchar2,
6289                                 p_src_num            in            number,
6290                                 p_src_txt2           in            varchar2,
6291                                 p_time_def_id        in            number,
6292                                 p_balance_date       in            date,
6293                                 p_local_unit_id      in            number,
6294                                 p_source_number2     in            number,
6295                                 p_organization_id    in            number,
6296                                 p_amount             in            number,
6297                                 p_effective_date     in            date,
6298                                 p_int_mlt_thrd_cache in out nocopy t_int_rb_cache,
6299                                 p_grp_rb_ptr_list    in out nocopy number_array,
6300                                 p_multi_thread       in            boolean default TRUE
6301                                )
6302 is
6303 --
6304 cursor get_row_to_update(p_defined_balance_id number
6305                         ,p_pact_id            number
6306                         ,p_gre                number
6307                         ,p_jd                 varchar2
6308                         ,p_src_id             number
6309                         ,p_src_tx             varchar2
6310                         ,p_src_num            number
6311                         ,p_src_txt2           varchar2
6312                         ,p_time_def_id        number
6313                         ,p_balance_date       date
6314                         ,p_local_unit_id      number
6315                         ,p_source_number2     number
6316                         ,p_organization_id    number)
6317 is
6318 select run_balance_id
6319 ,      balance_value
6320 from pay_run_balances
6321 where defined_balance_id             = p_defined_balance_id
6322 and   payroll_action_id              = p_pact_id
6323 and   nvl(tax_unit_id, -1)           = nvl(p_gre, -1)
6324 and   nvl(jurisdiction_code, 'null') = nvl(p_jd, 'null')
6325 and   nvl(source_id, -1)             = nvl(p_src_id, -1)
6326 and   nvl(source_text, 'null')       = nvl(p_src_tx, 'null')
6327 and   nvl(source_number, -1)         = nvl(p_src_num, -1)
6328 and   nvl(source_text2, 'null')      = nvl(p_src_txt2, 'null')
6329 and   nvl(time_definition_id, -1)    = nvl(p_time_def_id, -1)
6330 and   nvl(local_unit_id, -1)         = nvl(p_local_unit_id, -1)
6331 and   nvl(source_number2, -1)        = nvl(p_source_number2, -1)
6332 and   nvl(organization_id, -1)       = nvl(p_organization_id, -1)
6333 and   nvl(balance_date, to_date('0001/01/01', 'YYYY/MM/DD'))
6334                                      = nvl(p_balance_date, to_date('0001/01/01', 'YYYY/MM/DD'))
6335 and   rownum = 1;
6336 --
6337 l_grp_run_bal_id  pay_run_balances.run_balance_id%type;
6338 l_grp_run_bal_val pay_run_balances.balance_value%type;
6339 l_cache_ct        number;
6340 l_found           boolean;
6341 l_jur1            pay_run_balances.jurisdiction_comp1%type;
6342 l_jur2            pay_run_balances.jurisdiction_comp2%type;
6343 l_jur3            pay_run_balances.jurisdiction_comp3%type;
6344 l_current_ptr     number;
6345 l_previous_ptr     number;
6346 --
6347 begin
6348 --
6349   if p_amount <> 0 then
6350   --
6351     if g_debug then
6352        hr_utility.set_location('pay_balance_pkg.subtract_from_grp_bal', 25);
6353     end if;
6354     --
6355     if not p_multi_thread then -- i.e. if in single thread mode
6356       --
6357       -- update existing row, by subtracting the contributing amount
6358       --
6359       if g_debug then
6360          hr_utility.trace('p_grp_def_bal_id: '||p_grp_def_bal_id);
6361          hr_utility.trace('pactid: '||p_payroll_action_id);
6362       end if;
6363         open  get_row_to_update(p_grp_def_bal_id
6364                                ,p_payroll_action_id
6365                                ,p_tax_unit_id
6366                                ,p_jur_code
6367                                ,p_src_id
6368                                ,p_src_txt
6369                                ,p_src_num
6370                                ,p_src_txt2
6371                                ,p_time_def_id
6372                                ,p_balance_date
6373                                ,p_local_unit_id
6374                                ,p_source_number2
6375                                ,p_organization_id
6376                                );
6377         fetch get_row_to_update into l_grp_run_bal_id, l_grp_run_bal_val;
6378 --
6379         if get_row_to_update%notfound then
6380         --
6381         -- error as should find a row for updating
6382         --
6383           close get_row_to_update;
6384           hr_general.assert_condition(false);
6385         --
6386         else
6387           close get_row_to_update;
6388           if g_debug then
6389              hr_utility.set_location('pay_balance_pkg.subtract_from_grp_bal', 40);
6390              hr_utility.trace('p_grp_def_bal_id: '||to_char(p_grp_def_bal_id));
6391              hr_utility.trace('l_grp_run_bal_val: '||to_char(l_grp_run_bal_val));
6392              hr_utility.trace('contrib amt: '||to_char(p_amount));
6393           end if;
6394           --
6395           update pay_run_balances
6396           set    balance_value = (balance_value - p_amount)
6397           where  run_balance_id = l_grp_run_bal_id;
6398           --
6399         end if;
6400         --
6401     else  -- is multi threaded mode
6402       --
6403       -- if a row for this balance exists in the cache, then update that row
6404       -- else, create a row and add it to the cache
6405       --
6406       search_rb_cache( p_grp_def_bal_id,
6407                        p_payroll_action_id,
6408                        p_tax_unit_id,
6409                        p_jur_code,
6410                        p_src_id,
6411                        p_src_txt,
6412                        p_src_num,
6413                        p_src_txt2,
6414                        p_time_def_id,
6415                        p_balance_date,
6416                        p_local_unit_id,
6417                        p_source_number2,
6418                        p_organization_id,
6419                        p_int_mlt_thrd_cache,
6420                        p_grp_rb_ptr_list,
6421                        l_current_ptr,
6422                        l_previous_ptr,
6423                        l_found
6424                       );
6425 --
6426       if (l_found = TRUE) then
6427       --
6428         if g_debug then
6429            hr_utility.set_location('pay_balance_pkg.subtract_from_grp_bal', 50);
6430            hr_utility.trace('MULTI THREADED UPDATE');
6431         end if;
6432         update pay_run_balances
6433         set    balance_value = balance_value - p_amount
6434         where  run_balance_id =  p_int_mlt_thrd_cache(l_current_ptr).run_balance_id;
6435         --
6436       else  -- no row for this balance
6437         if g_debug then
6438            hr_utility.trace('MULTI THREADED INSERT');
6439            hr_utility.set_location('pay_balance_pkg.subtract_from_grp_bal', 55);
6440         end if;
6441         --
6442         --
6443         select pay_run_balances_s.nextval
6444         into l_grp_run_bal_id
6445         from dual;
6446         --
6447         split_jurisdiction(p_jur_code,
6448                            l_jur1,
6449                            l_jur2,
6450                            l_jur3);
6451 --
6452         insert into pay_run_balances
6453         (run_balance_id
6454         ,defined_balance_id
6455         ,payroll_action_id
6456         ,effective_date
6457         ,balance_value
6458         ,tax_unit_id
6459         ,jurisdiction_code
6460         ,jurisdiction_comp1
6461         ,jurisdiction_comp2
6462         ,jurisdiction_comp3
6463         ,source_id
6464         ,source_text
6465         ,source_number
6466         ,source_text2
6467         ,time_definition_id
6468         ,balance_date
6469         ,local_unit_id
6470         ,source_number2
6471         ,organization_id
6472         )
6473         values
6474         (l_grp_run_bal_id
6475         ,p_grp_def_bal_id
6476         ,p_payroll_action_id
6477         ,p_effective_date
6478         ,- p_amount
6479         ,p_tax_unit_id
6480         ,p_jur_code
6481         ,l_jur1
6482         ,l_jur2
6483         ,l_jur3
6484         ,p_src_id
6485         ,p_src_txt
6486         ,p_src_num
6487         ,p_src_txt2
6488         ,p_time_def_id
6489         ,p_balance_date
6490         ,p_local_unit_id
6491         ,p_source_number2
6492         ,p_organization_id
6493         );
6494         if g_debug then
6495            hr_utility.set_location('pay_balance_pkg.subtract_from_grp_bal', 60);
6496         end if;
6497         --
6498         -- cache the row details
6499         --
6500         --
6501         -- cache the row details
6502         --
6503         l_cache_ct := p_int_mlt_thrd_cache.count + 1;
6504         --
6505         p_int_mlt_thrd_cache(l_cache_ct).run_balance_id     := l_grp_run_bal_id;
6506         p_int_mlt_thrd_cache(l_cache_ct).defined_balance_id := p_grp_def_bal_id;
6507         p_int_mlt_thrd_cache(l_cache_ct).payroll_action_id  := p_payroll_action_id;
6508         p_int_mlt_thrd_cache(l_cache_ct).tax_unit_id        := p_tax_unit_id;
6509         p_int_mlt_thrd_cache(l_cache_ct).jurisdiction_code  := p_jur_code;
6510         p_int_mlt_thrd_cache(l_cache_ct).source_id          := p_src_id;
6511         p_int_mlt_thrd_cache(l_cache_ct).source_text        := p_src_txt;
6512         p_int_mlt_thrd_cache(l_cache_ct).source_number      := p_src_num;
6513         p_int_mlt_thrd_cache(l_cache_ct).source_text2       := p_src_txt2;
6514         p_int_mlt_thrd_cache(l_cache_ct).time_def_id        := p_time_def_id;
6515         p_int_mlt_thrd_cache(l_cache_ct).balance_date       := p_balance_date;
6516         p_int_mlt_thrd_cache(l_cache_ct).local_unit_id      := p_local_unit_id;
6517         p_int_mlt_thrd_cache(l_cache_ct).source_number2     := p_source_number2;
6518         p_int_mlt_thrd_cache(l_cache_ct).organization_id    := p_organization_id;
6519         p_int_mlt_thrd_cache(l_cache_ct).next               := null;
6520 --
6521         if (l_previous_ptr is null) then
6522           p_grp_rb_ptr_list(p_grp_def_bal_id):= l_cache_ct;
6523         else
6524           p_int_mlt_thrd_cache(l_previous_ptr).next := l_cache_ct;
6525         end if;
6526         --
6527         if g_debug then
6528            hr_utility.set_location('pay_balance_pkg.subtract_from_grp_bal', 65);
6529         end if;
6530         --
6531       end if;
6532       --
6533     end if;
6534   else -- not > 0
6535     if g_debug then
6536        hr_utility.set_location('pay_balance_pkg.subtract_from_grp_bal', 70);
6537     end if;
6538   end if;
6539 --
6540 end subtract_from_grp_bal;
6541 --
6542 --------------------------------------------------------------------------
6543 -- remove_asg_contribs
6544 -- Description: Removes assignment contributions to a run balance group balance
6545 --              from the run balance group balance, i.e. when an assignment is
6546 --              rolled back, the group balance needs to redueced by the
6547 --              amount contributed by that assignment.
6548 --------------------------------------------------------------------------
6549 procedure remove_asg_contribs
6550 (p_payroll_action_id   in number
6551 ,p_assignment_action_id in number
6552 ,p_multi_thread in boolean default false
6553 ) is
6554 --
6555 -- Get all the group defined_balances
6556 --
6557 cursor get_dbs(p_pact number) is
6558 select distinct defined_balance_id
6559   from pay_run_balances
6560  where payroll_action_id = p_pact;
6561 --
6562 cursor get_contexts(p_asg_action    in number,
6563                     p_si_needed  in varchar2,
6564                     p_st_needed  in varchar2,
6565                     p_sn_needed  in varchar2,
6566                     p_st2_needed  in varchar2,
6567                     p_sn2_needed  in varchar2,
6568                     p_org_needed  in varchar2) is
6569 select /*+ ORDERED USE_NL(prr) INDEX(prr pay_run_results_n50)*/
6570        distinct
6571        paa.tax_unit_id                                         tax_unit_id
6572 ,      prr.jurisdiction_code                                   jurisdiction_code
6573 ,      decode(p_si_needed,
6574               'Y', find_context('SOURCE_ID', prr.run_result_id),
6575               null)                                            source_id
6576 ,      decode(p_st_needed,
6577               'Y', find_context('SOURCE_TEXT', prr.run_result_id),
6578               null)                                            source_text
6579 ,      decode(p_sn_needed,
6580               'Y', find_context('SOURCE_NUMBER', prr.run_result_id),
6581               null)                                            source_number
6582 ,      decode(p_st2_needed,
6583               'Y', find_context('SOURCE_TEXT2', prr.run_result_id),
6584               null)                                            source_text2
6585 ,      decode(p_sn2_needed,
6586               'Y', find_context('SOURCE_NUMBER2', prr.run_result_id),
6587               null)                                            source_number2
6588 ,      decode(p_org_needed,
6589               'Y', find_context('ORGANIZATION_ID', prr.run_result_id),
6590               null)                                            organization_id
6591 ,      prr.time_definition_id
6592 ,      nvl(prr.end_date, ptp.end_date)                         balance_date
6593 ,      prr.local_unit_id                                       local_unit_id
6594 ,      ppa.effective_date
6595   from pay_assignment_actions paa,
6596        pay_payroll_actions    ppa,
6597        per_time_periods       ptp,
6598        pay_run_results        prr
6599  where paa.assignment_action_id = p_asg_action
6600    and ppa.payroll_action_id = paa.payroll_action_id
6601    and paa.assignment_action_id = prr.assignment_action_id
6602    and ptp.payroll_id = ppa.payroll_id
6603    and ppa.date_earned between ptp.start_date
6604                            and ptp.end_date
6605   order by 1, 2, 3, 4;
6606 --
6607 cursor get_bg (pactid number) is
6608 select pbg.business_group_id, pbg.legislation_code
6609   from pay_payroll_actions ppa,
6610        per_business_groups_perf pbg
6611  where payroll_action_id = pactid
6612    and pbg.business_group_id = ppa.business_group_id;
6613 --
6614 l_output_list        t_detailed_bal_out_tab;
6615 l_contexts           t_context_tab;
6616 l_cnt                number;
6617 l_eff_date            date;
6618 l_asg_def_bal_id     number;
6619 l_found              boolean;
6620 l_asgbal_cnt         number;
6621 l_inp_val_name       pay_input_values_f.name%type;
6622 --
6623 BEGIN
6624   g_debug := hr_utility.debug_enabled;
6625   if g_debug then
6626      hr_utility.set_location('Entering: pay_balance_pkg.remove_asg_contribs', 5);
6627   end if;
6628 --
6629   if (p_payroll_action_id <> g_payroll_action) then
6630 --
6631     /* First get the legislation rule */
6632     begin
6633        select plr.rule_mode
6634          into g_save_run_bals
6635          from pay_legislation_rules plr,
6636               per_business_groups_perf pbg,
6637               pay_payroll_actions ppa
6638         where ppa.payroll_action_id = p_payroll_action_id
6639           and ppa.business_group_id = pbg.business_group_id
6640           and pbg.legislation_code = plr.legislation_code
6641           and plr.rule_type = 'SAVE_RUN_BAL';
6642     exception
6643        when no_data_found then
6644          g_save_run_bals := 'N';
6645     end;
6646     --
6647     --
6648 --
6649     l_cnt := 1;
6650 --
6651     if (g_save_run_bals = 'Y') then
6652 --
6653     -- OK setup the balances to use
6654 --
6655       -- Reset everything
6656       g_rlb_grp_defbals.delete;
6657       g_rlb_asg_defbals.delete;
6658 --
6659       for dbrec in get_dbs(p_payroll_action_id) loop
6660       --
6661         g_rlb_grp_defbals(l_cnt).defined_balance_id := dbrec.defined_balance_id;
6662 --
6663         -- Now maintain the assignment ones
6664         select pdb.defined_balance_id
6665           into g_rlb_asg_defbals(l_cnt).defined_balance_id
6666           from pay_defined_balances pdb,
6667                pay_defined_balances pdb_grp,
6668                pay_balance_dimensions pbd
6669          where pdb_grp.defined_balance_id = g_rlb_grp_defbals(l_cnt).defined_balance_id
6670            and pdb_grp.balance_dimension_id = pbd.balance_dimension_id
6671            and pdb.balance_type_id = pdb_grp.balance_type_id
6672            and pdb.balance_dimension_id = pbd.asg_action_balance_dim_id;
6673 --
6674         l_cnt := l_cnt + 1;
6675       --
6676       end loop;
6677 --
6678       -- Check whether the SOURCE_ID, SOURCE_TEXT contexts are used.
6679       g_si_needed_chr := 'N';
6680       g_st_needed_chr := 'N';
6681       g_sn_needed_chr := 'N';
6682       g_st2_needed_chr := 'N';
6683       g_sn2_needed_chr := 'N';
6684       g_org_needed_chr := 'N';
6685       for bgrec in get_bg(p_payroll_action_id) loop
6686 --
6687         pay_core_utils.get_leg_context_iv_name('SOURCE_ID',
6688                                                bgrec.legislation_code,
6689                                                l_inp_val_name,
6690                                                l_found
6691                                               );
6692        if (l_found = TRUE) then
6693          g_si_needed_chr := 'Y';
6694        end if;
6695 --
6696         pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT',
6697                                                bgrec.legislation_code,
6698                                                l_inp_val_name,
6699                                                l_found
6700                                               );
6701        if (l_found = TRUE) then
6702          g_st_needed_chr := 'Y';
6703        end if;
6704 --
6705         pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER',
6706                                                bgrec.legislation_code,
6707                                                l_inp_val_name,
6708                                                l_found
6709                                               );
6710        if (l_found = TRUE) then
6711          g_sn_needed_chr := 'Y';
6712        end if;
6713 --
6714         pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT2',
6715                                                bgrec.legislation_code,
6716                                                l_inp_val_name,
6717                                                l_found
6718                                               );
6719        if (l_found = TRUE) then
6720          g_st2_needed_chr := 'Y';
6721        end if;
6722 --
6723         pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER2',
6724                                                bgrec.legislation_code,
6725                                                l_inp_val_name,
6726                                                l_found
6727                                               );
6728        if (l_found = TRUE) then
6729          g_sn2_needed_chr := 'Y';
6730        end if;
6731 --
6732         pay_core_utils.get_leg_context_iv_name('ORGANIZATION_ID',
6733                                                bgrec.legislation_code,
6734                                                l_inp_val_name,
6735                                                l_found
6736                                               );
6737        if (l_found = TRUE) then
6738          g_org_needed_chr := 'Y';
6739        end if;
6740 --
6741       end loop;
6742 --
6743       g_payroll_action := p_payroll_action_id;
6744 --
6745       -- The Run balances to maintain will be different since its a
6746       -- new payroll action.
6747 --
6748       g_grp_maintained_rb.delete;
6749       g_grp_rb_ptr_list.delete;
6750 --
6751     end if;
6752 --
6753   end if;
6754   --
6755   -- Is run Balances set
6756   --
6757   if (g_save_run_bals = 'Y') then
6758     --
6759     -- Only need to do somethinf if there are group balances
6760     --
6761     if (g_rlb_grp_defbals.count > 0) then
6762       --
6763       -- Now get the contexts
6764       --
6765       l_cnt := 1;
6766       for ctxrec in get_contexts(p_assignment_action_id,
6767                                  g_si_needed_chr,
6768                                  g_st_needed_chr,
6769                                  g_sn_needed_chr,
6770                                  g_st2_needed_chr,
6771                                  g_sn2_needed_chr,
6772                                  g_org_needed_chr) loop
6773 --
6774         l_contexts(l_cnt).tax_unit_id        := ctxrec.tax_unit_id;
6775         l_contexts(l_cnt).jurisdiction_code  := ctxrec.jurisdiction_code;
6776         l_contexts(l_cnt).source_id          := ctxrec.source_id;
6777         l_contexts(l_cnt).source_text        := ctxrec.source_text;
6778         l_contexts(l_cnt).source_number      := ctxrec.source_number;
6779         l_contexts(l_cnt).source_text2       := ctxrec.source_text2;
6780         l_contexts(l_cnt).time_def_id        := ctxrec.time_definition_id;
6781         l_contexts(l_cnt).local_unit_id      := ctxrec.local_unit_id;
6785         l_eff_date                           := ctxrec.effective_date;
6782         l_contexts(l_cnt).source_number2     := ctxrec.source_number2;
6783         l_contexts(l_cnt).organization_id    := ctxrec.organization_id;
6784         l_contexts(l_cnt).balance_date       := ctxrec.balance_date;
6786         l_cnt := l_cnt + 1;
6787 --
6788       end loop;
6789 --
6790       pay_balance_pkg.get_value (p_assignment_action_id,
6791                                  g_rlb_asg_defbals,
6792                                  l_contexts,
6793                                  FALSE,
6794                                  FALSE,
6795                                  l_output_list);
6796 --
6797       for l_cnt in 1..l_output_list.count loop
6798 --
6799        --
6800        -- First find the group equivolent
6801        --
6802        l_found := FALSE;
6803        l_asgbal_cnt := 0;
6804        while (l_found = FALSE and l_asgbal_cnt < g_rlb_asg_defbals.count) loop
6805 --
6806           l_asgbal_cnt := l_asgbal_cnt + 1;
6807           if (g_rlb_asg_defbals(l_asgbal_cnt).defined_balance_id =
6808                         l_output_list(l_cnt).defined_balance_id) then
6809 --
6810             l_found := TRUE;
6811 --
6812           end if;
6813 --
6814        end loop;
6815 --
6816        -- Now make the adjustment
6817        subtract_from_grp_bal(
6818                                  g_rlb_grp_defbals(l_asgbal_cnt).defined_balance_id,
6819                                  p_payroll_action_id,
6820                                  l_output_list(l_cnt).tax_unit_id,
6821                                  l_output_list(l_cnt).jurisdiction_code,
6822                                  l_output_list(l_cnt).source_id,
6823                                  l_output_list(l_cnt).source_text,
6824                                  l_output_list(l_cnt).source_number,
6825                                  l_output_list(l_cnt).source_text2,
6826                                  l_output_list(l_cnt).time_def_id,
6827                                  l_output_list(l_cnt).balance_date,
6828                                  l_output_list(l_cnt).local_unit_id,
6829                                  l_output_list(l_cnt).source_number2,
6830                                  l_output_list(l_cnt).organization_id,
6831                                  l_output_list(l_cnt).balance_value,
6832                                  l_eff_date,
6833                                  g_grp_maintained_rb,
6834                                  g_grp_rb_ptr_list,
6835                                  p_multi_thread
6836                                 );
6837 --
6838       end loop;
6839 --
6840     end if;
6841 --
6842   end if;
6843   if g_debug then
6844      hr_utility.set_location('Leaving: pay_balance_pkg.remove_asg_contribs', 80);
6845   end if;
6846 END remove_asg_contribs;
6847 --
6848 --------------------------------------------------------------------------
6849 -- procedure ins_run_balance
6850 --------------------------------------------------------------------------
6851 procedure ins_run_balance (p_defined_balance_id in number,
6852                            p_eff_date           in date,
6853                            p_bal_val            in number,
6854                            p_asg_act_id         in number default null,
6855                            p_payroll_act_id     in number default null,
6856                            p_asg_id             in number default null,
6857                            p_act_seq            in number default null,
6858                            p_tax_unit           in number default null,
6859                            p_jurisdiction       in varchar2 default null,
6860                            p_source_id          in number default null,
6861                            p_source_text        in varchar2 default null,
6862                            p_source_number      in varchar2 default null,
6863                            p_source_text2       in varchar2 default null,
6864                            p_time_def_id        in number   default null,
6865                            p_balance_date       in date     default null,
6866                            p_local_unit_id      in number   default null,
6867                            p_source_number2     in number   default null,
6868                            p_organization_id    in number   default null,
6869                            p_tax_group          in varchar2 default null
6870                           )
6871 is
6872 l_jur1 pay_run_balances.jurisdiction_comp1%type;
6873 l_jur2 pay_run_balances.jurisdiction_comp2%type;
6874 l_jur3 pay_run_balances.jurisdiction_comp3%type;
6875 begin
6876 --
6877   split_jurisdiction(p_jurisdiction,
6878                      l_jur1,
6879                      l_jur2,
6880                      l_jur3);
6881 --
6882   insert into pay_run_balances
6883            (
6884              run_balance_id,
6885              defined_balance_id,
6886              assignment_action_id,
6887              payroll_action_id,
6888              assignment_id,
6889              action_sequence,
6890              effective_date,
6891              balance_value,
6892              tax_unit_id,
6893              jurisdiction_code,
6894              jurisdiction_comp1,
6895              jurisdiction_comp2,
6896              jurisdiction_comp3,
6897              source_id,
6898              source_text,
6899              source_number,
6900              source_text2,
6904              source_number2,
6901              time_definition_id,
6902              balance_date,
6903              local_unit_id,
6905              organization_id,
6906              tax_group
6907             )
6908        values (
6909              pay_run_balances_s.nextval,
6910              p_defined_balance_id,
6911              p_asg_act_id,
6912              p_payroll_act_id,
6913              p_asg_id,
6914              p_act_seq,
6915              p_eff_date,
6916              p_bal_val,
6917              p_tax_unit,
6918              p_jurisdiction,
6919              l_jur1,
6920              l_jur2,
6921              l_jur3,
6922              p_source_id,
6923              p_source_text,
6924              p_source_number,
6925              p_source_text2,
6926              p_time_def_id,
6927              p_balance_date,
6928              p_local_unit_id,
6929              p_source_number2,
6930              p_organization_id,
6931              p_tax_group
6932             );
6933 --
6934 end ins_run_balance;
6935 --
6936 procedure create_run_balance (
6937                               p_batch_mode           in            boolean default FALSE,
6938                               p_def_bal_id           in            number,
6939                               p_mode                 in            varchar2,
6940                               p_asg_act              in            number,
6941                               p_pactid               in            number,
6942                               p_effective_date       in            date,
6943                               p_contexts             in out nocopy t_context_details_rec,
6944                               p_defined_balance_lst  in out nocopy t_balance_value_tab
6945                              )
6946 is
6947 --
6948 cursor get_aa_info(p_asg_act_id in number) is
6949 select assignment_id
6950 ,      action_sequence
6951 from   pay_assignment_actions
6952 where  assignment_action_id = p_asg_act_id;
6953 --
6954   bal_val        number;
6955   l_tx_ut        pay_assignment_actions.tax_unit_id%type;
6956   l_asg_id       pay_assignment_actions.assignment_id%type;
6957   l_act_seq      pay_assignment_actions.action_sequence%type;
6958   l_asgact_id    pay_assignment_actions.assignment_action_id%type;
6959   l_pactid       pay_payroll_actions.payroll_action_id%type;
6960 begin
6961 --
6962 --
6963    if g_debug then
6964       hr_utility.set_location('pay_balance_pkg.create_run_balance', 30);
6965       hr_utility.trace('Getting Balance'||p_def_bal_id);
6966    end if;
6967    /* First setup the contexts */
6968    if (nvl(p_contexts.tax_unit_id, -999) <> nvl(p_contexts.prv_tax_unit_id, -999)) then
6969       pay_balance_pkg.set_context('TAX_UNIT_ID', p_contexts.tax_unit_id);
6970       p_contexts.prv_tax_unit_id := p_contexts.tax_unit_id;
6971       if p_contexts.tax_unit_id is null then
6972          p_contexts.tu_set    := FALSE;
6973       else
6974          p_contexts.tu_set    := TRUE;
6975       end if;
6976    end if;
6977    if (nvl(p_contexts.jurisdiction_code, 'NULL') <> nvl(p_contexts.prv_jurisdiction_code, 'NULL'))
6978      then
6979      pay_balance_pkg.set_context('JURISDICTION_CODE',p_contexts.jurisdiction_code);
6980      p_contexts.prv_jurisdiction_code := p_contexts.jurisdiction_code;
6981       if p_contexts.jurisdiction_code is null then
6982          p_contexts.jc_set    := FALSE;
6983       else
6984          p_contexts.jc_set    := TRUE;
6985       end if;
6986    end if;
6987    if (nvl(p_contexts.source_id, -999) <> nvl(p_contexts.prv_source_id, -999)) then
6988       pay_balance_pkg.set_context('SOURCE_ID', p_contexts.source_id);
6989       p_contexts.prv_source_id := p_contexts.source_id;
6990       if p_contexts.source_id is null then
6991          p_contexts.si_set    := FALSE;
6992       else
6993          p_contexts.si_set    := TRUE;
6994       end if;
6995    end if;
6996    if (nvl(p_contexts.source_text, 'NULL') <> nvl(p_contexts.prv_source_text, 'NULL')) then
6997       pay_balance_pkg.set_context('SOURCE_TEXT', p_contexts.source_text);
6998       p_contexts.prv_source_text := p_contexts.source_text;
6999       if p_contexts.source_text is null then
7000          p_contexts.st_set    := FALSE;
7001       else
7002          p_contexts.st_set    := TRUE;
7003       end if;
7004    end if;
7005    if (nvl(p_contexts.source_number, 'NULL') <> nvl(p_contexts.prv_source_number, 'NULL')) then
7006       pay_balance_pkg.set_context('SOURCE_NUMBER', p_contexts.source_number);
7007       p_contexts.prv_source_number := p_contexts.source_number;
7008       if p_contexts.source_number is null then
7009          p_contexts.sn_set    := FALSE;
7010       else
7011          p_contexts.sn_set    := TRUE;
7012       end if;
7013    end if;
7014    if (nvl(p_contexts.source_text2, 'NULL') <> nvl(p_contexts.prv_source_text2, 'NULL')) then
7015       pay_balance_pkg.set_context('SOURCE_TEXT2', p_contexts.source_text2);
7016       p_contexts.prv_source_text2 := p_contexts.source_text2;
7017       if p_contexts.source_text2 is null then
7018          p_contexts.st2_set    := FALSE;
7019       else
7020          p_contexts.st2_set    := TRUE;
7021       end if;
7022    end if;
7023    if (nvl(p_contexts.time_def_id, -999) <> nvl(p_contexts.prv_time_def_id, -999)) then
7024       pay_balance_pkg.set_context('TIME_DEFINITION_ID', p_contexts.time_def_id);
7025       p_contexts.prv_time_def_id := p_contexts.time_def_id;
7029          p_contexts.td_set    := TRUE;
7026       if p_contexts.time_def_id is null then
7027          p_contexts.td_set    := FALSE;
7028       else
7030       end if;
7031    end if;
7032    if (nvl(p_contexts.balance_date, to_date('0001/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS')) <> nvl(p_contexts.prv_balance_date, to_date('0001/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS'))) then
7033       set_context('BALANCE_DATE'
7034                  ,fnd_date.date_to_canonical(p_contexts.balance_date));
7035       p_contexts.prv_balance_date := p_contexts.balance_date;
7036       if p_contexts.balance_date is null then
7037          p_contexts.bd_set    := FALSE;
7038       else
7039          p_contexts.bd_set    := TRUE;
7040       end if;
7041    end if;
7042    if (nvl(p_contexts.local_unit_id, -999) <> nvl(p_contexts.prv_local_unit_id, -999)) then
7043       pay_balance_pkg.set_context('LOCAL_UNIT_ID', p_contexts.local_unit_id);
7044       p_contexts.prv_local_unit_id := p_contexts.local_unit_id;
7045       if p_contexts.local_unit_id is null then
7046          p_contexts.lu_set    := FALSE;
7047       else
7048          p_contexts.lu_set    := TRUE;
7049       end if;
7050    end if;
7051    if (nvl(p_contexts.source_number2, -999) <> nvl(p_contexts.prv_source_number2, -999)) then
7052       pay_balance_pkg.set_context('SOURCE_NUMBER2', p_contexts.source_number2);
7053       p_contexts.prv_source_number2 := p_contexts.source_number2;
7054       if p_contexts.source_number2 is null then
7055          p_contexts.sn2_set    := FALSE;
7056       else
7057          p_contexts.sn2_set    := TRUE;
7058       end if;
7059    end if;
7060    if (nvl(p_contexts.organization_id, -999) <> nvl(p_contexts.prv_organization_id, -999)) then
7061       pay_balance_pkg.set_context('ORGANIZATION_ID', p_contexts.organization_id);
7062       p_contexts.prv_organization_id := p_contexts.organization_id;
7063       if p_contexts.organization_id is null then
7064          p_contexts.org_set    := FALSE;
7065       else
7066          p_contexts.org_set    := TRUE;
7067       end if;
7068    end if;
7069 --
7070    if ((p_contexts.tu_needed and p_contexts.tu_set = FALSE) or
7071        (p_contexts.jc_needed and p_contexts.jc_set = FALSE) or
7072        (p_contexts.si_needed and p_contexts.si_set = FALSE) or
7073        (p_contexts.sn_needed and p_contexts.sn_set = FALSE) or
7074        (p_contexts.st2_needed and p_contexts.st2_set = FALSE) or
7075        (p_contexts.st_needed and p_contexts.st_set = FALSE) or
7076        (p_contexts.td_needed and p_contexts.td_set = FALSE) or
7077        (p_contexts.lu_needed and p_contexts.lu_set = FALSE) or
7078        (p_contexts.sn2_needed and p_contexts.sn2_set = FALSE) or
7079        (p_contexts.org_needed and p_contexts.org_set = FALSE) or
7080        (p_contexts.bd_needed and p_contexts.bd_set = FALSE)) then
7081      --
7082      -- Do nothing, not all the contexts are set.
7083      --
7084      null;
7085    else
7086    --
7087    -- set the tax unit id, if it is not needed then it should be null
7088    -- even if the cursor brought a value back;
7089    --
7090      if (p_mode = 'ASG') then
7091          l_asgact_id := p_asg_act;
7092          open get_aa_info(l_asgact_id);
7093          fetch get_aa_info into l_asg_id, l_act_seq;
7094          close get_aa_info;
7095          l_pactid := null;
7096      else
7097          l_asg_id := null;
7098          l_act_seq := null;
7099          l_asgact_id := null;
7100          l_pactid := p_pactid;
7101      end if;
7102 --
7103      if not p_contexts.tu_needed then
7104        l_tx_ut := null;
7105      else
7106        l_tx_ut := p_contexts.tax_unit_id;
7107      end if;
7108    --
7109      /* Now get the balance value */
7110      bal_val := pay_balance_pkg.get_value
7111                    (p_defined_balance_id    => p_def_bal_id
7112                    ,p_assignment_action_id  => p_asg_act
7113                    ,p_get_rr_route          => true
7114                    ,p_get_rb_route          => false);
7115 --
7116      if (bal_val <> 0) then
7117        if g_debug then
7118           hr_utility.set_location('pay_balance_pkg.create_run_balance', 35);
7119        end if;
7120        ins_run_balance (p_defined_balance_id => p_def_bal_id,
7121                         p_eff_date           => p_effective_date,
7122                         p_bal_val            => bal_val,
7123                         p_payroll_act_id     => l_pactid,
7124                         p_asg_act_id         => l_asgact_id,
7125                         p_asg_id             => l_asg_id,
7126                         p_act_seq            => l_act_seq,
7127                         p_tax_unit           => l_tx_ut,
7128                         p_jurisdiction       => p_contexts.jurisdiction_code,
7129                         p_source_id          => p_contexts.source_id,
7130                         p_source_text        => p_contexts.source_text,
7131                         p_source_number      => p_contexts.source_number,
7132                         p_source_text2       => p_contexts.source_text2,
7133                         p_time_def_id        => p_contexts.time_def_id,
7134                         p_balance_date       => p_contexts.balance_date,
7135                         p_local_unit_id      => p_contexts.local_unit_id,
7136                         p_source_number2     => p_contexts.source_number2,
7137                         p_organization_id    => p_contexts.organization_id
7138                        );
7139      end if;
7140    end if;
7144    end if;
7141 --
7142    if g_debug then
7143       hr_utility.set_location('pay_balance_pkg.create_run_balance', 40);
7145 --
7146 end create_run_balance;
7147 --
7148 --------------------------------------------------------------------------
7149 -- procedure check_defbal_context
7150 --------------------------------------------------------------------------
7151 -- Description: This procedure assesses the capability of the contexts for
7152 --              the specified defined balance and returns the context
7153 --              record whose unnecessary context values are removed.
7154 --
7155 procedure check_defbal_context
7156   (p_defined_balance_id   in     number
7157   ,p_context              in out nocopy t_context_rec
7158   ,p_defbal_rec              out nocopy t_def_bal_rec
7159   ,p_valid_contexts          out nocopy boolean
7160   )
7161 is
7162 begin
7163 
7164   --
7165   -- Derive context usages for the defined balance.
7166   --
7167   load_defbal_cache(p_defined_balance_id, p_defbal_rec);
7168 
7169   --
7170   -- Nullify unnecessary context values.
7171   --
7172   if not p_defbal_rec.tu_needed then
7173     p_context.tax_unit_id       := null;
7174   end if;
7175   if not p_defbal_rec.jc_needed then
7176     p_context.jurisdiction_code := null;
7177   end if;
7178   if not p_defbal_rec.si_needed then
7179     p_context.source_id := null;
7180   end if;
7181   if not p_defbal_rec.st_needed then
7182     p_context.source_text := null;
7183   end if;
7184   if not p_defbal_rec.st2_needed then
7185     p_context.source_text2 := null;
7186   end if;
7187   if not p_defbal_rec.sn_needed then
7188     p_context.source_number := null;
7189   end if;
7190   if not p_defbal_rec.td_needed then
7191     p_context.time_def_id := null;
7192   end if;
7193   if not p_defbal_rec.bd_needed then
7194     p_context.balance_date := null;
7195   end if;
7196   if not p_defbal_rec.lu_needed then
7197     p_context.local_unit_id := null;
7198   end if;
7199   if not p_defbal_rec.sn2_needed then
7200     p_context.source_number2 := null;
7201   end if;
7202   if not p_defbal_rec.org_needed then
7203     p_context.organization_id := null;
7204   end if;
7205 
7206   --
7207   -- Check to see if necessary contexts are set.
7208   --
7209   if   (p_defbal_rec.tu_needed and p_context.tax_unit_id is null)
7210     or (p_defbal_rec.jc_needed and p_context.jurisdiction_code is null)
7211     or (p_defbal_rec.si_needed and p_context.source_id is null)
7212     or (p_defbal_rec.st_needed and p_context.source_text is null)
7213     or (p_defbal_rec.st2_needed and p_context.source_text2 is null)
7214     or (p_defbal_rec.sn_needed and p_context.source_number is null)
7215     or (p_defbal_rec.td_needed and p_context.time_def_id is null)
7216     or (p_defbal_rec.lu_needed and p_context.local_unit_id is null)
7217     or (p_defbal_rec.sn2_needed and p_context.source_number2 is null)
7218     or (p_defbal_rec.org_needed and p_context.organization_id is null)
7219     or (p_defbal_rec.bd_needed and p_context.balance_date is null)
7220   then
7221     p_valid_contexts := FALSE;
7222   else
7223     p_valid_contexts := TRUE;
7224   end if;
7225 
7226 end check_defbal_context;
7227 --
7228 --------------------------------------------------------------------------
7229 -- procedure create_rr_asg_balances
7230 --------------------------------------------------------------------------
7231 -- Description: This procedure creates assignment level run balances
7232 --              based on the specified run result id.
7233 --              This is intended to be called from (batch) balance
7234 --              adjustment so that it can process assignment run balances
7235 --              per adjustment.
7236 --
7237 procedure create_rr_asg_balances
7238   (p_run_result_id    in number
7239   )
7240 is
7241 --
7242   l_proc            varchar2(80):= ' pay_balance_pkg.create_rr_asg_balances';
7243 
7244   l_legrule_found     boolean      := FALSE;
7245   l_save_run_bal_flag varchar2(30) := 'N';
7246   l_rr_ctx            t_context_rec;         -- Run result level context
7247   l_bal_ctx           t_context_rec;         -- Run result level context
7248   l_context           t_context_rec;
7249   l_defbal_rec        t_def_bal_rec;         -- Defined balance context usage
7250   l_defbal_ctx        t_context_details_rec; -- Defined balance level context
7251   l_defbal_list       t_balance_value_tab;
7252   l_valid_contexts    boolean;
7253 
7254   l_rr_info           t_run_result_rec;
7255 
7256   --
7257   -- Cursor to retrieve defined balances that could be connected
7258   -- with assignment run balances.
7259   --
7260   -- #6151064. Ensuring the defined balances are for the processing
7261   --           business group.
7262   --
7263   cursor csr_defbal
7264     (p_rr_id          in number
7265     ,p_ele_id         in number
7266     ,p_effective_date in date
7267     ,p_bus_grp_id     in number
7268     ,p_leg_code       in varchar2)
7269   is
7270   select
7271     /*+ ORDERED USE_NL(pbf, pdb, pbd, prrv) */
7272     distinct pdb.defined_balance_id
7273   from
7274     pay_input_values_f     piv
7275    ,pay_balance_feeds_f    pbf
7276    ,pay_defined_balances   pdb
7277    ,pay_balance_dimensions pbd
7278    ,pay_run_result_values  prrv
7279   where
7280       pbd.dimension_level = 'ASG'
7284        or (pdb.legislation_code = p_leg_code
7281   and pdb.save_run_balance = 'Y'
7282   and (   (pdb.business_group_id = p_bus_grp_id
7283            and pdb.legislation_code is null)
7285            and pdb.business_group_id is null)
7286        or (pdb.legislation_code is null
7287            and pdb.business_group_id is null))
7288   and pdb.balance_dimension_id = pbd.balance_dimension_id
7289   and pdb.balance_type_id = pbf.balance_type_id
7290   and p_effective_date between pbf.effective_start_date
7291                            and pbf.effective_end_date
7292   and pbf.input_value_id = piv.input_value_id
7293   and piv.element_type_id = p_ele_id
7294   and p_effective_date between piv.effective_start_date
7295                            and piv.effective_end_date
7296   and prrv.input_value_id = piv.input_value_id
7297   and prrv.run_result_id = p_rr_id
7298   and prrv.result_value is not null
7299   ;
7300 
7301 begin
7302 --
7303   g_debug := hr_utility.debug_enabled;
7304   if g_debug then
7305     hr_utility.set_location('Entering:'||l_proc, 5);
7306   end if;
7307   --
7308   -- Derive information for the specified run result.
7309   --
7310   get_run_result_info(p_run_result_id, l_rr_info);
7311 
7312   --
7313   -- Check SAVE_ASG_RUN_BAL legislation rule.
7314   --
7315   pay_core_utils.get_legislation_rule
7316     (p_legrul_name   => 'SAVE_ASG_RUN_BAL'
7317     ,p_legislation   => l_rr_info.legislation_code
7318     ,p_legrul_value  => l_save_run_bal_flag
7319     ,p_found         => l_legrule_found
7320     );
7321 
7322   if NOT (l_legrule_found and l_save_run_bal_flag = 'Y') then
7323 
7324     if g_debug then
7325       hr_utility.set_location(l_proc, 10);
7326     end if;
7327     -- Saving run balance is not supported, exit the process.
7328     return;
7329   end if;
7330 
7331   if g_debug then
7332     hr_utility.set_location(l_proc, 20);
7333   end if;
7334   --
7335   -- Setup run result level contexts.
7336   --
7337   l_rr_ctx.tax_unit_id       := l_rr_info.tax_unit_id;
7338   l_rr_ctx.jurisdiction_code := l_rr_info.jurisdiction_code;
7339   l_rr_ctx.source_id     := find_context('SOURCE_ID', p_run_result_id);
7340   l_rr_ctx.source_text   := find_context('SOURCE_TEXT', p_run_result_id);
7341   l_rr_ctx.source_number := find_context('SOURCE_NUMBER', p_run_result_id);
7342   l_rr_ctx.source_text2  := find_context('SOURCE_TEXT2', p_run_result_id);
7343   l_rr_ctx.time_def_id   := l_rr_info.time_def_id;
7344   l_rr_ctx.local_unit_id := l_rr_info.local_unit_id;
7345   l_rr_ctx.source_number2 := find_context('SOURCE_NUMBER2', p_run_result_id);
7346   l_rr_ctx.organization_id := find_context('ORGANIZATION_ID', p_run_result_id);
7347   l_rr_ctx.balance_date  := l_rr_info.balance_date;
7348 
7349   --
7350   -- Process defined balances to save.
7351   --
7352   for l_rec in csr_defbal(p_run_result_id
7353                          ,l_rr_info.element_type_id
7354                          ,l_rr_info.effective_date
7355                          ,l_rr_info.business_group_id
7356                          ,l_rr_info.legislation_code
7357                          )
7358   loop
7359 
7360     if g_debug then
7361       hr_utility.set_location(l_proc, 25);
7362     end if;
7363 
7364     -- copy the run result contexts
7365     l_context := l_rr_ctx;
7366 
7367     --
7368     -- Check the contexts and truncate unnecessary context values.
7369     --
7370     check_defbal_context
7371       (p_defined_balance_id  => l_rec.defined_balance_id
7372       ,p_context             => l_context
7373       ,p_defbal_rec          => l_defbal_rec
7374       ,p_valid_contexts      => l_valid_contexts
7375       );
7376 
7377     --
7378     -- Setup defined balance context record for create_run_balance().
7379     --
7380     l_defbal_ctx.tu_needed  := l_defbal_rec.tu_needed;
7381     l_defbal_ctx.jc_needed  := l_defbal_rec.jc_needed;
7382     l_defbal_ctx.si_needed  := l_defbal_rec.si_needed;
7383     l_defbal_ctx.st_needed  := l_defbal_rec.st_needed;
7384     l_defbal_ctx.st2_needed := l_defbal_rec.st2_needed;
7385     l_defbal_ctx.sn_needed  := l_defbal_rec.sn_needed;
7386     l_defbal_ctx.td_needed  := l_defbal_rec.td_needed;
7387     l_defbal_ctx.bd_needed  := l_defbal_rec.bd_needed;
7388     l_defbal_ctx.lu_needed  := l_defbal_rec.lu_needed;
7389     l_defbal_ctx.sn2_needed := l_defbal_rec.sn2_needed;
7390     l_defbal_ctx.org_needed := l_defbal_rec.org_needed;
7391 
7392     l_defbal_ctx.tax_unit_id       := l_context.tax_unit_id;
7393     l_defbal_ctx.jurisdiction_code := l_context.jurisdiction_code;
7394     l_defbal_ctx.source_id         := l_context.source_id;
7395     l_defbal_ctx.source_text       := l_context.source_text;
7396     l_defbal_ctx.source_text2      := l_context.source_text2;
7397     l_defbal_ctx.source_number     := l_context.source_number;
7398     l_defbal_ctx.time_def_id       := l_context.time_def_id;
7399     l_defbal_ctx.balance_date      := l_context.balance_date;
7400     l_defbal_ctx.local_unit_id     := l_context.local_unit_id;
7401     l_defbal_ctx.source_number2    := l_context.source_number2;
7402     l_defbal_ctx.organization_id   := l_context.organization_id;
7403 
7404     --
7405     -- Process only when necessary contexts are set.
7406     --
7407     if l_valid_contexts then
7408 
7409       if g_debug then
7410         hr_utility.set_location(l_proc, 30);
7411         hr_utility.trace(' Defined Balance ID: '
7415       -- Delete run balance records that may conflict with new one.
7412                          || l_rec.defined_balance_id);
7413       end if;
7414       --
7416       --
7417       delete from pay_run_balances
7418       where
7419           defined_balance_id = l_rec.defined_balance_id
7420       and assignment_action_id = l_rr_info.assignment_action_id
7421       and payroll_action_id is null
7422       and (   (l_defbal_ctx.tax_unit_id is null)
7423            or (tax_unit_id = l_defbal_ctx.tax_unit_id))
7424       and (   (l_defbal_ctx.jurisdiction_code is null)
7425            or (substr(jurisdiction_code, 1, l_defbal_rec.jurisdiction_lvl))
7426                 = substr(l_defbal_ctx.jurisdiction_code
7427                         ,1, l_defbal_rec.jurisdiction_lvl))
7428       and (   (l_defbal_ctx.source_id is null)
7429            or (source_id = l_defbal_ctx.source_id))
7430       and (   (l_defbal_ctx.source_text is null)
7431            or (source_text = l_defbal_ctx.source_text))
7432       and (   (l_defbal_ctx.source_text2 is null)
7433            or (source_text2 = l_defbal_ctx.source_text2))
7434       and (   (l_defbal_ctx.source_number is null)
7435            or (source_number = l_defbal_ctx.source_number))
7436       and (   (l_defbal_ctx.time_def_id is null)
7437            or (time_definition_id = l_defbal_ctx.time_def_id))
7438       and (   (l_defbal_ctx.local_unit_id is null)
7439            or (local_unit_id = l_defbal_ctx.local_unit_id))
7440       and (   (l_defbal_ctx.source_number2 is null)
7441            or (source_number2 = l_defbal_ctx.source_number2))
7442       and (   (l_defbal_ctx.organization_id is null)
7443            or (organization_id = l_defbal_ctx.organization_id))
7444       and (   (l_defbal_ctx.balance_date is null)
7445            or (balance_date = l_defbal_ctx.balance_date))
7446       ;
7447       --
7448       create_run_balance
7449         (p_def_bal_id          => l_rec.defined_balance_id
7450         ,p_mode                => 'ASG'
7451         ,p_asg_act             => l_rr_info.assignment_action_id
7452         ,p_pactid              => null
7453         ,p_effective_date      => l_rr_info.effective_date
7454         ,p_contexts            => l_defbal_ctx
7455         ,p_defined_balance_lst => l_defbal_list
7456         );
7457 
7458     end if;
7459 
7460   end loop;
7461   --
7462   if g_debug then
7463     hr_utility.set_location('Leaving: '||l_proc, 50);
7464   end if;
7465 end create_rr_asg_balances;
7466 --
7467 --------------------------------------------------------------------------
7468 -- procedure create_set_asg_balance
7469 --------------------------------------------------------------------------
7470 procedure create_set_asg_balance(p_defined_balance_lst  in out nocopy t_balance_value_tab,
7471                                  p_asgact_id            in            number,
7472                                  p_load_type            in            varchar2 default 'NORMAL')
7473 is
7474 --
7475 cursor get_contexts(asgact    in number,
7476                     p_si_needed  in varchar2,
7477                     p_st_needed  in varchar2,
7478                     p_sn_needed  in varchar2,
7479                     p_st2_needed in varchar2,
7480                     p_sn2_needed in varchar2,
7481                     p_org_needed in varchar2) is
7482 select /*+ ORDERED USE_NL(prr) INDEX(prr pay_run_results_n50)*/
7483        distinct
7484        paa.tax_unit_id                                         tax_unit_id
7485 ,      prr.jurisdiction_code                                   jurisdiction_code
7486 ,      decode(p_si_needed,
7487               'Y', find_context('SOURCE_ID', prr.run_result_id),
7488               null)                                            source_id
7489 ,      decode(p_st_needed,
7490               'Y', find_context('SOURCE_TEXT', prr.run_result_id),
7491               null)                                            source_text
7492 ,      decode(p_sn_needed,
7493               'Y', find_context('SOURCE_NUMBER', prr.run_result_id),
7494               null)                                            source_number
7495 ,      decode(p_st2_needed,
7496               'Y', find_context('SOURCE_TEXT2', prr.run_result_id),
7497               null)                                            source_text2
7498 ,      decode(p_sn2_needed,
7499               'Y', find_context('SOURCE_NUMBER2', prr.run_result_id),
7500               null)                                            source_number2
7501 ,      decode(p_org_needed,
7502               'Y', find_context('ORGANIZATION_ID', prr.run_result_id),
7503               null)                                            organization_id
7504 ,      prr.local_unit_id
7505 ,      prr.time_definition_id
7506 ,      nvl(prr.end_date, ptp.end_date)                         balance_date
7507   from pay_assignment_actions paa,
7508        pay_payroll_actions    ppa,
7509        per_time_periods       ptp,
7510        pay_run_results        prr
7511  where paa.assignment_action_id = asgact
7512    and paa.assignment_action_id = prr.assignment_action_id
7513    and ppa.payroll_action_id = paa.payroll_action_id
7514    and ppa.payroll_id = ptp.payroll_id
7515    and ppa.date_earned between ptp.start_date
7516                            and ptp.end_date
7517   order by 1, 2, 3, 4;
7518 --
7519 cursor get_aa (asgact    in number) is
7520 select paa.assignment_action_id,
7521        ppa.effective_date,
7522        paa.assignment_id,
7523        paa.action_sequence
7524   from pay_assignment_actions paa,
7528 --
7525        pay_payroll_actions    ppa
7526  where ppa.payroll_action_id = paa.payroll_action_id
7527    and paa.assignment_action_id = asgact;
7529 cursor get_bg (aaid number) is
7530 select pbg.business_group_id, pbg.legislation_code
7531   from pay_payroll_actions ppa,
7532        pay_assignment_actions paa,
7533        per_business_groups_perf pbg
7534  where ppa.payroll_action_id = paa.payroll_action_id
7535    and paa.assignment_action_id = aaid
7536    and pbg.business_group_id = ppa.business_group_id;
7537 --
7538   l_si_needed_chr varchar2(10);
7539   l_st_needed_chr varchar2(10);
7540   l_sn_needed_chr varchar2(10);
7541   l_st2_needed_chr varchar2(10);
7542   l_sn2_needed_chr varchar2(10);
7543   l_org_needed_chr varchar2(10);
7544   l_context_lst   t_context_tab;
7545   l_cnt           number;
7546   l_output_list   t_detailed_bal_out_tab;
7547   l_inp_val_name  pay_input_values_f.name%type;
7548   l_found         boolean;
7549 --
7550 begin
7551 --
7552    if g_debug then
7553       hr_utility.set_location('Entering: pay_balance_pkg.create_set_asg_balance', 5);
7554    end if;
7555 --
7556    -- Check whether the SOURCE_ID, SOURCE_TEXT contexts are used.
7557    l_si_needed_chr := 'N';
7558    l_st_needed_chr := 'N';
7559    l_sn_needed_chr := 'N';
7560    l_st2_needed_chr := 'N';
7561    l_sn2_needed_chr := 'N';
7562    l_org_needed_chr := 'N';
7563    for bgrec in get_bg(p_asgact_id) loop
7564 --
7565      pay_core_utils.get_leg_context_iv_name('SOURCE_ID',
7566                                             bgrec.legislation_code,
7567                                             l_inp_val_name,
7568                                             l_found
7569                                            );
7570     if (l_found = TRUE) then
7571       l_si_needed_chr := 'Y';
7572     end if;
7573 --
7574      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT',
7575                                             bgrec.legislation_code,
7576                                             l_inp_val_name,
7577                                             l_found
7578                                            );
7579     if (l_found = TRUE) then
7580       l_st_needed_chr := 'Y';
7581     end if;
7582 --
7583      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER',
7584                                             bgrec.legislation_code,
7585                                             l_inp_val_name,
7586                                             l_found
7587                                            );
7588     if (l_found = TRUE) then
7589       l_sn_needed_chr := 'Y';
7590     end if;
7591 --
7592      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT2',
7593                                             bgrec.legislation_code,
7594                                             l_inp_val_name,
7595                                             l_found
7596                                            );
7597     if (l_found = TRUE) then
7598       l_st2_needed_chr := 'Y';
7599     end if;
7600 --
7601      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER2',
7602                                             bgrec.legislation_code,
7603                                             l_inp_val_name,
7604                                             l_found
7605                                            );
7606     if (l_found = TRUE) then
7607       l_sn2_needed_chr := 'Y';
7608     end if;
7609 --
7610      pay_core_utils.get_leg_context_iv_name('ORGANIZATION_ID',
7611                                             bgrec.legislation_code,
7612                                             l_inp_val_name,
7613                                             l_found
7614                                            );
7615     if (l_found = TRUE) then
7616       l_org_needed_chr := 'Y';
7617     end if;
7618 --
7619    end loop;
7620 --
7621 -- Validate/Delete the pay_run_balances for this mode.
7622 --
7623    for cnt in 1..p_defined_balance_lst.count loop
7624 --
7625      if (p_load_type = 'FORCE') then
7626        if g_debug then
7627           hr_utility.set_location('pay_balance_pkg.create_set_asg_balance', 20);
7628        end if;
7629        delete from pay_run_balances
7630         where defined_balance_id = p_defined_balance_lst(cnt).defined_balance_id
7631           and assignment_action_id = p_asgact_id;
7632      elsif (p_load_type = 'TRUSTED') then
7633        null;
7634      else
7635        if g_debug then
7636           hr_utility.set_location('pay_balance_pkg.create_set_asg_balance', 25);
7637        end if;
7638         declare
7639            l_dummy number;
7640         begin
7641 --
7642            select 1
7643              into l_dummy
7644              from dual
7645             where exists (select ''
7646                             from pay_run_balances
7647                            where defined_balance_id = p_defined_balance_lst(cnt).defined_balance_id
7648                              and assignment_action_id = p_asgact_id
7649                              and balance_value <> 0);
7650 --
7651            /* Error, there should be no rows in this mode */
7652            hr_utility.set_message(801,'HR_34723_NO_ROWS_NORMAL_MODE');
7653            hr_utility.raise_error;
7654 --
7655         exception
7656            when no_data_found then
7657                null;
7658         end;
7659      end if;
7663 --
7660    end loop;
7661 --
7662 -- Generate the context list
7664    l_cnt := 1;
7665    if g_debug then
7666       hr_utility.set_location('pay_balance_pkg.create_set_asg_balance', 27);
7667    end if;
7668    for cxt in get_contexts(p_asgact_id,
7669                            l_si_needed_chr,
7670                            l_st_needed_chr,
7671                            l_sn_needed_chr,
7672                            l_st2_needed_chr,
7673                            l_sn2_needed_chr,
7674                            l_org_needed_chr) loop
7675       if g_debug then
7676          hr_utility.set_location('pay_balance_pkg.create_set_asg_balance', 28);
7677       end if;
7678       l_context_lst(l_cnt).tax_unit_id := cxt.tax_unit_id;
7679       l_context_lst(l_cnt).jurisdiction_code := cxt.jurisdiction_code;
7680       l_context_lst(l_cnt).source_id := cxt.source_id;
7681       l_context_lst(l_cnt).source_text := cxt.source_text;
7682       l_context_lst(l_cnt).source_number := cxt.source_number;
7683       l_context_lst(l_cnt).source_text2 := cxt.source_text2;
7684       l_context_lst(l_cnt).time_def_id := cxt.time_definition_id;
7685       l_context_lst(l_cnt).balance_date := cxt.balance_date;
7686       l_context_lst(l_cnt).local_unit_id := cxt.local_unit_id;
7687       l_context_lst(l_cnt).source_number2 := cxt.source_number2;
7688       l_context_lst(l_cnt).organization_id := cxt.organization_id;
7689 --
7690       l_cnt := l_cnt + 1;
7691    end loop;
7692 --
7693 -- Go Get the balance values
7694 --
7695    if g_debug then
7696       hr_utility.set_location('pay_balance_pkg.create_set_asg_balance', 30);
7697    end if;
7698    for aarec in get_aa (p_asgact_id) loop
7699       pay_balance_pkg.get_value (p_asgact_id,
7700                                  p_defined_balance_lst,
7701                                  l_context_lst,
7702                                  TRUE,
7703                                  FALSE,
7704                                  l_output_list);
7705 --
7706 --   Insert the results in the run_balance table.
7707 --
7708      if g_debug then
7709         hr_utility.set_location('pay_balance_pkg.create_set_asg_balance', 35);
7710      end if;
7711      for cnt in 1..l_output_list.count loop
7712        if (l_output_list(cnt).balance_value <> 0) then
7713 --
7714          if g_debug then
7715             hr_utility.set_location('pay_balance_pkg.create_set_asg_balance', 40);
7716          end if;
7717          ins_run_balance (p_defined_balance_id => l_output_list(cnt).defined_balance_id,
7718                           p_eff_date           => aarec.effective_date,
7719                           p_bal_val            => l_output_list(cnt).balance_value,
7720                           p_payroll_act_id     => null,
7721                           p_asg_act_id         => p_asgact_id,
7722                           p_asg_id             => aarec.assignment_id,
7723                           p_act_seq            => aarec.action_sequence,
7724                           p_tax_unit           => l_output_list(cnt).tax_unit_id,
7725                           p_jurisdiction       => l_output_list(cnt).jurisdiction_code,
7726                           p_source_id          => l_output_list(cnt).source_id,
7727                           p_source_text        => l_output_list(cnt).source_text,
7728                           p_source_number      => l_output_list(cnt).source_number,
7729                           p_source_text2       => l_output_list(cnt).source_text2,
7730                           p_time_def_id        => l_output_list(cnt).time_def_id,
7731                           p_balance_date       => l_output_list(cnt).balance_date,
7732                           p_local_unit_id      => l_output_list(cnt).local_unit_id,
7733                           p_source_number2     => l_output_list(cnt).source_number2,
7734                           p_organization_id    => l_output_list(cnt).organization_id
7735                          );
7736 --
7737        end if;
7738      end loop;
7739    end loop;
7740 --
7741    if g_debug then
7742       hr_utility.set_location('Leaving: pay_balance_pkg.create_set_asg_balance', 50);
7743    end if;
7744 end create_set_asg_balance;
7745 --
7746 procedure add_grpbal_to_list(p_def_bal_id  in     number,
7747                              p_load_type   in     varchar2,
7748                              p_pactid      in     number,
7749                              p_output_list in out nocopy t_detailed_bal_out_tab,
7750                              p_next_free   in out nocopy number
7751                             )
7752 is
7753 --
7754 cursor get_contexts(p_pact_id    in number,
7755                     p_jur_lvl    in number,
7756                     p_tu_needed  in varchar2,
7757                     p_jc_needed  in varchar2,
7758                     p_si_needed  in varchar2,
7759                     p_st_needed  in varchar2,
7760                     p_sn_needed  in varchar2,
7761                     p_st2_needed in varchar2,
7762                     p_td_needed  in varchar2,
7763                     p_bd_needed  in varchar2,
7764                     p_lu_needed  in varchar2,
7765                     p_sn2_needed in varchar2,
7766                     p_org_needed in varchar2) is
7767 select /*+ ORDERED USE_NL (prr) INDEX(prr pay_run_results_n50)*/
7768        distinct
7769        decode(p_tu_needed,
7770               'Y', paa.tax_unit_id,
7771               null)                                           tax_unit_id
7772 ,      decode(p_jc_needed,
7776               'Y', find_context('SOURCE_ID', prr.run_result_id),
7773               'Y', substr(prr.jurisdiction_code, 1, p_jur_lvl),
7774               null)                                           jurisdiction_code
7775 ,      decode(p_si_needed,
7777               null)                                            source_id
7778 ,      decode(p_st_needed,
7779               'Y', find_context('SOURCE_TEXT', prr.run_result_id),
7780               null)                                            source_text
7781 ,      decode(p_sn_needed,
7782               'Y', find_context('SOURCE_NUMBER', prr.run_result_id),
7783               null)                                            source_number
7784 ,      decode(p_st2_needed,
7785               'Y', find_context('SOURCE_TEXT2', prr.run_result_id),
7786               null)                                            source_text2
7787 ,      decode(p_td_needed,
7788               'Y', prr.time_definition_id,
7789               null)                                            time_definition_id
7790 ,      decode(p_bd_needed,
7791               'Y', nvl(prr.end_date, ptp.end_date),
7792               null)                                            balance_date
7793 ,      decode(p_lu_needed,
7794               'Y', prr.local_unit_id,
7795               null)                                            local_unit_id
7796 ,      decode(p_sn2_needed,
7797               'Y', find_context('SOURCE_NUMBER2', prr.run_result_id),
7798               null)                                            source_number2
7799 ,      decode(p_org_needed,
7800               'Y', find_context('ORGANIZATION_ID', prr.run_result_id),
7801               null)                                            organization_id
7802   from pay_assignment_actions paa,
7803        pay_payroll_actions    ppa,
7804        per_time_periods       ptp,
7805        pay_run_results        prr
7806  where paa.payroll_action_id = p_pact_id
7807    and paa.assignment_action_id = prr.assignment_action_id
7808    and ppa.payroll_action_id = paa.payroll_action_id
7809    and ppa.payroll_id = ptp.payroll_id
7810    and ppa.date_earned between ptp.start_date
7811                            and ptp.end_date
7812   order by 1, 2, 3, 4;
7813 
7814 --
7815 l_defbal_rec           t_def_bal_rec;
7816 l_tu_needed_chr        pay_legislation_rules.rule_mode%type;
7817 l_jc_needed_chr        pay_legislation_rules.rule_mode%type;
7818 l_si_needed_chr        pay_legislation_rules.rule_mode%type;
7819 l_st_needed_chr        pay_legislation_rules.rule_mode%type;
7820 l_sn_needed_chr        pay_legislation_rules.rule_mode%type;
7821 l_st2_needed_chr       pay_legislation_rules.rule_mode%type;
7822 l_td_needed_chr        pay_legislation_rules.rule_mode%type;
7823 l_bd_needed_chr        pay_legislation_rules.rule_mode%type;
7824 l_lu_needed_chr        pay_legislation_rules.rule_mode%type;
7825 l_sn2_needed_chr       pay_legislation_rules.rule_mode%type;
7826 l_org_needed_chr       pay_legislation_rules.rule_mode%type;
7827 --
7828 begin
7829 --
7830 if g_debug then
7831   hr_utility.set_location('Entering pay_balance_pkg.add_grpbal_to_list',10);
7832   hr_utility.trace('p_load_type: '||p_load_type);
7833 end if;
7834 --
7835       l_jc_needed_chr := 'N';
7836       l_si_needed_chr := 'N';
7837       l_st_needed_chr := 'N';
7838       l_sn_needed_chr := 'N';
7839       l_tu_needed_chr := 'N';
7840       l_st2_needed_chr := 'N';
7841       l_td_needed_chr := 'N';
7842       l_bd_needed_chr := 'N';
7843       l_lu_needed_chr := 'N';
7844       l_sn2_needed_chr := 'N';
7845       l_org_needed_chr := 'N';
7846       load_defbal_cache(p_def_bal_id,
7847                           l_defbal_rec);
7848 --
7849       if (l_defbal_rec.jc_needed = TRUE) then
7850          l_jc_needed_chr := 'Y';
7851       end if;
7852       if (l_defbal_rec.si_needed = TRUE) then
7853          l_si_needed_chr := 'Y';
7854       end if;
7855       if (l_defbal_rec.st_needed = TRUE) then
7856          l_st_needed_chr := 'Y';
7857       end if;
7858       if (l_defbal_rec.sn_needed = TRUE) then
7859          l_sn_needed_chr := 'Y';
7860       end if;
7861       if (l_defbal_rec.tu_needed = TRUE) then
7862          l_tu_needed_chr := 'Y';
7863       end if;
7864       if (l_defbal_rec.st2_needed = TRUE) then
7865          l_st2_needed_chr := 'Y';
7866       end if;
7867       if (l_defbal_rec.td_needed = TRUE) then
7868          l_td_needed_chr := 'Y';
7869       end if;
7870       if (l_defbal_rec.bd_needed = TRUE) then
7871          l_bd_needed_chr := 'Y';
7872       end if;
7873       if (l_defbal_rec.lu_needed = TRUE) then
7874          l_lu_needed_chr := 'Y';
7875       end if;
7876       if (l_defbal_rec.sn2_needed = TRUE) then
7877          l_sn2_needed_chr := 'Y';
7878       end if;
7879       if (l_defbal_rec.org_needed = TRUE) then
7880          l_org_needed_chr := 'Y';
7881       end if;
7882 --
7883      if (p_load_type = 'FORCE') then
7884        if g_debug then
7885           hr_utility.set_location('pay_balance_pkg.add_grpbal_to_list', 20);
7886        end if;
7887        delete from pay_run_balances
7888         where defined_balance_id = p_def_bal_id
7889           and payroll_action_id = p_pactid;
7890      elsif (p_load_type = 'TRUSTED') then
7891        null;
7892      else
7893        if g_debug then
7894           hr_utility.set_location('pay_balance_pkg.add_grpbal_to_list', 25);
7895        end if;
7896         declare
7900              into l_dummy
7897            l_dummy number;
7898         begin
7899            select 1
7901              from dual
7902             where exists (select ''
7903                             from pay_run_balances
7904                            where defined_balance_id = p_def_bal_id
7905                              and payroll_action_id = p_pactid
7906                              and balance_value <> 0);
7907 --
7908            -- Error, there should be no rows in this mode
7909            hr_utility.set_message(801,'HR_34723_NO_ROWS_NORMAL_MODE');
7910            hr_utility.raise_error;
7911 --
7912         exception
7913            when no_data_found then
7914                null;
7915         end;
7916      end if;
7917 --
7918       for ctxrec in get_contexts(p_pactid,
7919                                  l_defbal_rec.jurisdiction_lvl,
7920                                  l_tu_needed_chr,
7921                                  l_jc_needed_chr,
7922                                  l_si_needed_chr,
7923                                  l_st_needed_chr,
7924                                  l_sn_needed_chr,
7925                                  l_st2_needed_chr,
7926                                  l_td_needed_chr,
7927                                  l_bd_needed_chr,
7928                                  l_lu_needed_chr,
7929                                  l_sn2_needed_chr,
7930                                  l_org_needed_chr) loop
7931 --
7932 --       Only add to the list if all the context values are known
7933 --
7934          if (    ((    l_defbal_rec.st2_needed = TRUE
7935                    and ctxrec.source_text2 is not null)
7936                   or l_defbal_rec.st2_needed = FALSE)
7937              and ((    l_defbal_rec.jc_needed = TRUE
7938                    and ctxrec.jurisdiction_code is not null)
7939                   or l_defbal_rec.jc_needed = FALSE)
7940              and ((    l_defbal_rec.tu_needed = TRUE
7941                    and ctxrec.tax_unit_id is not null)
7942                   or l_defbal_rec.tu_needed = FALSE)
7943              and ((    l_defbal_rec.si_needed = TRUE
7944                    and ctxrec.source_id is not null)
7945                   or l_defbal_rec.si_needed = FALSE)
7946              and ((    l_defbal_rec.st_needed = TRUE
7947                    and ctxrec.source_text is not null)
7948                   or l_defbal_rec.st_needed = FALSE)
7949              and ((    l_defbal_rec.sn_needed = TRUE
7950                    and ctxrec.source_number is not null)
7951                   or l_defbal_rec.sn_needed = FALSE)
7952              and ((    l_defbal_rec.td_needed = TRUE
7953                    and ctxrec.time_definition_id is not null)
7954                   or l_defbal_rec.td_needed = FALSE)
7955              and ((    l_defbal_rec.bd_needed = TRUE
7956                    and ctxrec.balance_date is not null)
7957                   or l_defbal_rec.bd_needed = FALSE)
7958              and ((    l_defbal_rec.lu_needed = TRUE
7959                    and ctxrec.local_unit_id is not null)
7960                   or l_defbal_rec.lu_needed = FALSE)
7961              and ((    l_defbal_rec.sn2_needed = TRUE
7962                    and ctxrec.source_number2 is not null)
7963                   or l_defbal_rec.sn2_needed = FALSE)
7964              and ((    l_defbal_rec.org_needed = TRUE
7965                    and ctxrec.organization_id is not null)
7966                   or l_defbal_rec.org_needed = FALSE)
7967             ) then
7968 --
7969            p_output_list(p_next_free).defined_balance_id:= p_def_bal_id;
7970            p_output_list(p_next_free).tax_unit_id := ctxrec.tax_unit_id;
7971            p_output_list(p_next_free).jurisdiction_code := ctxrec.jurisdiction_code;
7972            p_output_list(p_next_free).source_id := ctxrec.source_id;
7973            p_output_list(p_next_free).source_text := ctxrec.source_text;
7974            p_output_list(p_next_free).source_number := ctxrec.source_number;
7975            p_output_list(p_next_free).source_text2 := ctxrec.source_text2;
7976            p_output_list(p_next_free).time_def_id := ctxrec.time_definition_id;
7977            p_output_list(p_next_free).balance_date := ctxrec.balance_date;
7978            p_output_list(p_next_free).local_unit_id := ctxrec.local_unit_id;
7979            p_output_list(p_next_free).source_number2 := ctxrec.source_number2;
7980            p_output_list(p_next_free).organization_id := ctxrec.organization_id;
7981            p_output_list(p_next_free).balance_value := 0;
7982            p_output_list(p_next_free).balance_found := FALSE;
7983            p_output_list(p_next_free).jurisdiction_lvl := l_defbal_rec.jurisdiction_lvl;
7984            p_next_free := p_next_free + 1;
7985          end if;
7986 --
7987       end loop;
7988 --
7989 if g_debug then
7990   hr_utility.set_location('Leaving: pay_balance_pkg.add_grpbal_to_list', 40);
7991 end if;
7992 --
7993 end add_grpbal_to_list;
7994 -----------------------------------------------------------------------------
7995 -- procedure create_all_grp_balances_full
7996 -----------------------------------------------------------------------------
7997 procedure create_all_grp_balances_full(p_pact_id  in number,
7998                                   p_bal_list   in varchar2 default 'ALL',
7999                                   p_load_type  in varchar2 default 'NORMAL',
8000                                   p_def_bal    in number   default null
8004 is
8001                                  ,p_eff_date   in date     default null
8002                                  ,p_delta      in varchar2 default null
8003                                  )
8005 --
8006 cursor crs_asgact (p_pact_id number,
8007                    p_eff_date   date,
8008                    p_bus_grp_id number,
8009                    p_leg_code   varchar2,
8010                    p_bal_list   varchar2,
8011                    p_def_bal    number)
8012 is
8013 select  /*+ ORDERED USE_NL(piv pbf pbt pdb pbd prrv)
8014             INDEX (prrv PAY_RUN_RESULT_VALUES_PK)*/
8015        distinct  pdb.defined_balance_id,
8016        pbt.jurisdiction_level,
8017        pbt.balance_type_id,
8018        pbd.balance_dimension_id
8019   from
8020        pay_assignment_actions paa,
8021        pay_run_results        prr,
8022        pay_input_values_f     piv,
8023        pay_balance_feeds_f    pbf,
8024        pay_balance_types      pbt,
8025        pay_defined_balances   pdb,
8026        pay_balance_dimensions pbd,
8027        pay_run_result_values  prrv
8028  where pbd.dimension_level = 'GRP'
8029    and pdb.save_run_balance|| decode (pbt.balance_type_id, 0, '', '')= 'Y'
8030    and pdb.balance_dimension_id = pbd.balance_dimension_id
8031    and pdb.balance_type_id = pbt.balance_type_id
8032    and ((pdb.business_group_id = p_bus_grp_id
8033           and pdb.legislation_code is null)
8034         or
8035          (pdb.legislation_code = p_leg_code
8036           and pdb.business_group_id is null)
8037         or
8038          (pdb.legislation_code is null
8039           and pdb.business_group_id is null)
8040        )
8041    and (    p_bal_list <> 'INVALID'
8042          or (    p_bal_list = 'INVALID'
8043              and exists (select ''
8044                            from pay_balance_validation pbv
8045                           where pbv.defined_balance_id = pdb.defined_balance_id
8046                             and pbv.business_group_id = p_bus_grp_id
8047                             and pbv.run_balance_status = 'P')
8048             )
8049        )
8050    and (   p_def_bal is null
8051         or p_def_bal = pdb.defined_balance_id
8052        )
8053    and paa.payroll_action_id = p_pact_id
8054    and prr.assignment_action_id = paa.assignment_action_id
8055    and piv.element_type_id = prr.element_type_id
8056    and p_eff_date between piv.effective_start_date
8057                       and piv.effective_end_date
8058    and pbf.input_value_id = piv.input_value_id
8059    and p_eff_date between pbf.effective_start_date
8060                       and pbf.effective_end_date
8061    and pbt.balance_type_id = pbf.balance_type_id
8062    and prrv.run_result_id = prr.run_result_id
8063    and prrv.input_value_id = piv.input_value_id
8064    and prrv.result_value is not null;
8065 
8066 --   and exists (select /*+ ORDERED */ ''
8067 --                 from pay_run_results prr,
8068 --                      pay_run_result_values prrv,
8069 --                      pay_balance_feeds_f pbf
8070 --                where prr.assignment_action_id = p_asg_act_id
8071 --                  and prr.run_result_id = prrv.run_result_id
8072 --                  and prrv.input_value_id = pbf.input_value_id
8073 --                  and pbf.balance_type_id = pbt.balance_type_id);
8074 --
8075 --
8076 cursor crs_balatt (p_pact_id number
8077                   ,p_bal_list   varchar2
8078                   ,p_def_bal_id number
8079                    )
8080 is
8081 select /*+ ORDERED */
8082        pdb.defined_balance_id,
8083        pdb.balance_type_id,
8084        pdb.balance_dimension_id
8085   from
8086        pay_bal_attribute_definitions pbad,
8087        pay_balance_attributes        pba,
8088        pay_defined_balances          pdb,
8089        pay_balance_dimensions        pbd
8090  where
8091        pbad.attribute_name = p_bal_list
8092    and pbad.attribute_id   = pba.attribute_id
8093    and pba.defined_balance_id = pdb.defined_balance_id
8094    and pdb.balance_dimension_id = pbd.balance_dimension_id
8095    and pbd.dimension_level = 'GRP'
8096    and p_def_bal_id = pba.defined_balance_id
8097    and exists (select /*+ ORDERED */ ''
8098                  from pay_assignment_actions paa,
8099                       pay_run_results prr,
8100                       pay_run_result_values prrv,
8101                       pay_balance_feeds_f pbf
8102                 where paa.payroll_action_id = p_pact_id
8103                   and prr.assignment_action_id = paa.assignment_action_id
8104                   and prr.run_result_id = prrv.run_result_id
8105                   and prrv.input_value_id = pbf.input_value_id
8106                   and pdb.balance_type_id = pbf.balance_type_id
8107               )
8108 UNION ALL
8109 select pdb.defined_balance_id,
8110        pdb.balance_type_id,
8111        pdb.balance_dimension_id
8112   from
8113        pay_bal_attribute_definitions pbad,
8114        pay_balance_attributes        pba,
8115        pay_defined_balances          pdb,
8116        pay_balance_dimensions        pbd
8117  where
8118        pbad.attribute_name = p_bal_list
8119    and pbad.attribute_id   = pba.attribute_id
8120    and pba.defined_balance_id = pdb.defined_balance_id
8121    and pdb.balance_dimension_id = pbd.balance_dimension_id
8122    and pbd.dimension_level = 'GRP'
8123    and p_def_bal_id is null
8124    and pdb.balance_type_id in
8128                       pay_run_results prr,
8125                 (select /*+ USE_NL(paa,prr,prrv,pbf) ORDERED */
8126                       distinct pbf.balance_type_id
8127                  from pay_assignment_actions paa,
8129                       pay_run_result_values prrv,
8130                       pay_balance_feeds_f pbf
8131                 where paa.payroll_action_id = p_pact_id
8132                   and prr.assignment_action_id = paa.assignment_action_id
8133                   and prr.run_result_id = prrv.run_result_id
8134                   and prrv.input_value_id = pbf.input_value_id
8135               );
8136 --
8137 cursor crs_delta_balatt(p_pact_id    number
8138                        ,p_bal_list   varchar2
8139                        ,p_bus_grp_id number
8140                        ,p_eff_date   date
8141                    )
8142 is
8143 select /*+ ORDERED */
8144        pdb.defined_balance_id,
8145        pdb.balance_type_id,
8146        pdb.balance_dimension_id
8147   from
8148        pay_bal_attribute_definitions pbad,
8149        pay_balance_attributes        pba,
8150        pay_defined_balances          pdb,
8151        pay_balance_dimensions        pbd
8152  where
8153        pbad.attribute_name = p_bal_list
8154    and pbad.attribute_id   = pba.attribute_id
8155    and pba.defined_balance_id = pdb.defined_balance_id
8156    and pdb.balance_dimension_id = pbd.balance_dimension_id
8157    and pbd.dimension_level = 'GRP'
8158    and exists (select ''
8159                from pay_balance_validation pbv
8160                where pdb.defined_balance_id + decode(PDB.LEGISLATION_SUBGROUP, ' ', 0, 0) = pbv.defined_balance_id
8161                and   pbv.run_balance_status = 'V'
8162                and   pbv.business_group_id = p_bus_grp_id
8163                and   pbv.balance_load_date is not null
8164                and   pbv.balance_load_date > p_eff_date)
8165    and pdb.balance_type_id in
8166                 (select /*+ USE_NL(paa,prr,prrv,pbf) ORDERED */
8167                       distinct pbf.balance_type_id
8168                  from pay_assignment_actions paa,
8169                       pay_run_results prr,
8170                       pay_run_result_values prrv,
8171                       pay_balance_feeds_f pbf
8172                 where paa.payroll_action_id = p_pact_id
8173                   and prr.assignment_action_id = paa.assignment_action_id
8174                   and prr.run_result_id = prrv.run_result_id
8175                   and prrv.input_value_id = pbf.input_value_id
8176                 );
8177 
8178 
8179 cursor get_aa (p_pact_id    in number) is
8180 select paa.assignment_action_id,
8181        ppa.effective_date,
8182        paa.assignment_id,
8183        paa.action_sequence
8184   from pay_assignment_actions paa,
8185        pay_payroll_actions    ppa
8186  where ppa.payroll_action_id = paa.payroll_action_id
8187    and ppa.payroll_action_id = p_pact_id
8188    and rownum = 1;
8189 --
8190 --
8191 cursor get_contexts_2(p_pact_id    in number,
8192                     p_si_needed  in varchar2,
8193                     p_st_needed  in varchar2,
8194                     p_sn_needed  in varchar2,
8195                     p_st2_needed in varchar2,
8196                     p_sn2_needed in varchar2,
8197                     p_org_needed in varchar2) is
8198 select /*+ ORDERED USE_NL (prr) INDEX(prr pay_run_results_n50)*/
8199        distinct
8200        paa.tax_unit_id                                         tax_unit_id
8201 ,      prr.jurisdiction_code                                   jurisdiction_code
8202 ,      decode(p_si_needed,
8203               'Y', find_context('SOURCE_ID', prr.run_result_id),
8204               null)                                            source_id
8205 ,      decode(p_st_needed,
8206               'Y', find_context('SOURCE_TEXT', prr.run_result_id),
8207               null)                                            source_text
8208 ,      decode(p_sn_needed,
8209               'Y', find_context('SOURCE_NUMBER', prr.run_result_id),
8210               null)                                            source_number
8211 ,      decode(p_st2_needed,
8212               'Y', find_context('SOURCE_TEXT2', prr.run_result_id),
8213               null)                                            source_text2
8214 ,      decode(p_sn2_needed,
8215               'Y', find_context('SOURCE_NUMBER2', prr.run_result_id),
8216               null)                                            source_number2
8217 ,      decode(p_org_needed,
8218               'Y', find_context('ORGANIZATION_ID', prr.run_result_id),
8219               null)                                            organization_id
8220 ,      prr.time_definition_id
8221 ,      nvl(prr.end_date, ptp.end_date)                         balance_date
8222 ,      prr.local_unit_id                                       local_unit_id
8223   from pay_assignment_actions paa,
8224        pay_payroll_actions    ppa,
8225        per_time_periods       ptp,
8226        pay_run_results        prr
8227  where paa.payroll_action_id = p_pact_id
8228    and paa.assignment_action_id = prr.assignment_action_id
8229    and ppa.payroll_action_id = paa.payroll_action_id
8230    and ppa.payroll_id = ptp.payroll_id
8231    and ppa.date_earned between ptp.start_date
8232                            and ptp.end_date
8233   order by 1, 2, 3, 4;
8234 --
8235 cursor get_bg (p_pact_id number) is
8236 select pbg.business_group_id, pbg.legislation_code
8237   from pay_payroll_actions ppa,
8241 --
8238        per_business_groups_perf pbg
8239  where ppa.payroll_action_id = p_pact_id
8240    and pbg.business_group_id = ppa.business_group_id;
8242 l_defbal_rec           t_def_bal_rec;
8243 l_tu_needed_chr        pay_legislation_rules.rule_mode%type;
8244 l_jc_needed_chr        pay_legislation_rules.rule_mode%type;
8245 l_si_needed_chr        pay_legislation_rules.rule_mode%type;
8246 l_st_needed_chr        pay_legislation_rules.rule_mode%type;
8247 l_sn_needed_chr        pay_legislation_rules.rule_mode%type;
8248 l_st2_needed_chr       pay_legislation_rules.rule_mode%type;
8249 l_td_needed_chr        pay_legislation_rules.rule_mode%type;
8250 l_bd_needed_chr        pay_legislation_rules.rule_mode%type;
8251 l_lu_needed_chr        pay_legislation_rules.rule_mode%type;
8252 l_sn2_needed_chr       pay_legislation_rules.rule_mode%type;
8253 l_org_needed_chr       pay_legislation_rules.rule_mode%type;
8254   l_context_lst   t_context_tab;
8255   l_cnt           number;
8256   l_output_list   t_detailed_bal_out_tab;
8257   l_inp_val_name  pay_input_values_f.name%type;
8258   l_found         boolean;
8259   l_next_free number;
8260   l_bg_id     per_business_groups.business_group_id%type;
8261   l_leg_code  per_business_groups.legislation_code%type;
8262   l_eff_date  date;
8263 begin
8264 --
8265 if g_debug then
8266   hr_utility.set_location(
8267             'Entering:pay_balance_pkg.create_all_gre_balances_full',10);
8268 end if;
8269    --
8270    select pbg.business_group_id,
8271           pbg.legislation_code,
8272           ppa.effective_date
8273      into l_bg_id,
8274           l_leg_code,
8275           l_eff_date
8276      from per_business_groups_perf pbg,
8277           pay_payroll_actions ppa
8278     where ppa.payroll_action_id = p_pact_id
8279       and ppa.business_group_id = pbg.business_group_id;
8280 --
8281    l_next_free := 1;
8282 --
8283    if (    p_bal_list <> 'ALL'
8284        and p_bal_list <> 'INVALID'
8285        and p_bal_list <> 'SINGLE') then
8286 --
8287        /* OK we must be balance attribute */
8288        -- REC 13/10/06 Balance attribute mode can now be used for SINGLE
8289        -- but SINGLE calls this procedure in FORCE mode. So, if p_def_bal
8290        -- is not null and p_bal_list is a GEN_BAL<pact_id) value, then FORCE
8291        -- can be allowed. The run balances will be deleted in procedure
8292        -- add_grpbal_to_list.
8293        --
8294      if (p_delta = 'N'
8295          or p_delta is null) then -- if NOT in DELTA mode
8296      --
8297        if (p_load_type = 'TRUSTED') then
8298        --
8299          if g_debug then
8300            hr_utility.set_location(
8301                      'pay_balance_pkg.create_all_grp_balances_full', 20);
8302          end if;
8303          --
8304          for dbarec in crs_balatt(p_pact_id
8305                                  ,p_bal_list
8306                                  ,p_def_bal
8307                                  ) loop
8308             add_grpbal_to_list(p_def_bal_id  => dbarec.defined_balance_id,
8309                                p_load_type   => p_load_type,
8310                                p_pactid      => p_pact_id,
8311                                p_output_list => l_output_list,
8312                                p_next_free   => l_next_free
8313                               );
8314          end loop;
8315          --
8316        elsif (p_load_type = 'FORCE'
8317               and p_def_bal is not null) then
8318        --
8319          if g_debug then
8320            hr_utility.set_location(
8321                      'pay_balance_pkg.create_all_grp_balances_full', 30);
8322          end if;
8323          --
8324          for dbsarec in crs_balatt(p_pact_id
8325                                   ,p_bal_list
8326                                   ,p_def_bal
8327                                   ) loop
8328             add_grpbal_to_list(p_def_bal_id  => dbsarec.defined_balance_id,
8329                                p_load_type   => p_load_type,
8330                                p_pactid      => p_pact_id,
8331                                p_output_list => l_output_list,
8332                                p_next_free   => l_next_free
8333                               );
8334          end loop;
8335          --
8336       else -- p_load_type <> TRUSTED
8337       --
8338          pay_core_utils.assert_condition('create_all_grp_balances_full:1', false);
8339       end if;
8340     else -- p_delta is Y thus in Delta mode
8341     --
8342      if g_debug then
8343        hr_utility.set_location(
8344                  'pay_balance_pkg.create_all_grp_balances_full', 35);
8345      end if;
8346      --
8347      for dbdrec in crs_delta_balatt(p_pact_id
8348                                    ,p_bal_list
8349                                    ,l_bg_id
8350                                    ,l_eff_date
8351                                    ) loop
8352             add_grpbal_to_list(p_def_bal_id  => dbdrec.defined_balance_id,
8353                                p_load_type   => p_load_type,
8354                                p_pactid      => p_pact_id,
8355                                p_output_list => l_output_list,
8356                                p_next_free   => l_next_free
8357                               );
8358      end loop;
8359     end if;
8360    --
8361    else
8362      if g_debug then
8366      --
8363        hr_utility.set_location(
8364                  'pay_balance_pkg.create_all_grp_balances_full', 40);
8365      end if;
8367       for dbrec in crs_asgact(p_pact_id,
8368                               l_eff_date,
8369                               l_bg_id,
8370                               l_leg_code,
8371                               p_bal_list,
8372                               p_def_bal) loop
8373          add_grpbal_to_list(p_def_bal_id  => dbrec.defined_balance_id,
8374                             p_load_type   => p_load_type,
8375                             p_pactid      => p_pact_id,
8376                             p_output_list => l_output_list,
8377                             p_next_free   => l_next_free
8378                            );
8379       end loop;
8380    end if;
8381    --
8382    -- Bug 4318391.
8383    -- Ensure if any defind balances found.
8384    --
8385    if l_output_list.count = 0 then
8386      hr_utility.set_location('pay_balance_pkg.create_all_grp_balances_full', 50);
8387      --
8388      -- Exit this procedure.
8389      --
8390      return;
8391    end if;
8392 
8393 --
8394 --
8395 -- Generate the context list
8396 --
8397    -- Check whether the SOURCE_ID, SOURCE_TEXT contexts are used.
8398    l_si_needed_chr := 'N';
8399    l_st_needed_chr := 'N';
8400    l_sn_needed_chr := 'N';
8401    l_st2_needed_chr := 'N';
8402    l_sn2_needed_chr := 'N';
8403    l_org_needed_chr := 'N';
8404    for bgrec in get_bg(p_pact_id) loop
8405 --
8406      pay_core_utils.get_leg_context_iv_name('SOURCE_ID',
8407                                             bgrec.legislation_code,
8408                                             l_inp_val_name,
8409                                             l_found
8410                                            );
8411     if (l_found = TRUE) then
8412       l_si_needed_chr := 'Y';
8413     end if;
8414 --
8415      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT',
8416                                             bgrec.legislation_code,
8417                                             l_inp_val_name,
8418                                             l_found
8419                                            );
8420     if (l_found = TRUE) then
8421       l_st_needed_chr := 'Y';
8422     end if;
8423 --
8424      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER',
8425                                             bgrec.legislation_code,
8426                                             l_inp_val_name,
8427                                             l_found
8428                                            );
8429     if (l_found = TRUE) then
8430       l_sn_needed_chr := 'Y';
8431     end if;
8432 --
8433      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT2',
8434                                             bgrec.legislation_code,
8435                                             l_inp_val_name,
8436                                             l_found
8437                                            );
8438     if (l_found = TRUE) then
8439       l_st2_needed_chr := 'Y';
8440     end if;
8441 --
8442      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER2',
8443                                             bgrec.legislation_code,
8444                                             l_inp_val_name,
8445                                             l_found
8446                                            );
8447     if (l_found = TRUE) then
8448       l_sn2_needed_chr := 'Y';
8449     end if;
8450 --
8451      pay_core_utils.get_leg_context_iv_name('ORGANIZATION_ID',
8452                                             bgrec.legislation_code,
8453                                             l_inp_val_name,
8454                                             l_found
8455                                            );
8456     if (l_found = TRUE) then
8457       l_org_needed_chr := 'Y';
8458     end if;
8459 --
8460    end loop;
8461 --
8462    l_cnt := 1;
8463    if g_debug then
8464       hr_utility.set_location(
8465                 'pay_balance_pkg.create_all_grp_balances_full', 60);
8466    end if;
8467    --
8468    for cxt in get_contexts_2(p_pact_id,
8469                            l_si_needed_chr,
8470                            l_st_needed_chr,
8471                            l_sn_needed_chr,
8472                            l_st2_needed_chr,
8473                            l_sn2_needed_chr,
8474                            l_org_needed_chr) loop
8475       if g_debug then
8476          hr_utility.set_location(
8477                    'pay_balance_pkg.create_all_grp_balances_full', 70);
8478       end if;
8479       --
8480       l_context_lst(l_cnt).tax_unit_id := cxt.tax_unit_id;
8481       l_context_lst(l_cnt).jurisdiction_code := cxt.jurisdiction_code;
8482       l_context_lst(l_cnt).source_id := cxt.source_id;
8483       l_context_lst(l_cnt).source_text := cxt.source_text;
8484       l_context_lst(l_cnt).source_number := cxt.source_number;
8485       l_context_lst(l_cnt).source_text2 := cxt.source_text2;
8486       l_context_lst(l_cnt).time_def_id := cxt.time_definition_id;
8487       l_context_lst(l_cnt).balance_date := cxt.balance_date;
8488       l_context_lst(l_cnt).local_unit_id := cxt.local_unit_id;
8489       l_context_lst(l_cnt).source_number2 := cxt.source_number2;
8490       l_context_lst(l_cnt).organization_id := cxt.organization_id;
8491 --
8492       l_cnt := l_cnt + 1;
8496 --
8493    end loop;
8494 --
8495 -- Go Get the balance values
8497    if g_debug then
8498       hr_utility.set_location(
8499                 'pay_balance_pkg.create_all_grp_balances_full', 80);
8500    end if;
8501 --
8502    for aarec in get_aa (p_pact_id) loop
8503       pay_balance_pkg.get_value_int_batch (aarec.assignment_action_id,
8504                                  l_context_lst,
8505                                  TRUE,
8506                                  FALSE,
8507                                  l_output_list);
8508 --
8509 --   Insert the results in the run_balance table.
8510 --
8511      if g_debug then
8512         hr_utility.set_location(
8513                   'pay_balance_pkg.create_all_grp_balances_full', 85);
8514      end if;
8515      for cnt in 1..l_output_list.count loop
8516        if (l_output_list(cnt).balance_value <> 0) then
8517 --
8518          if g_debug then
8519             hr_utility.set_location(
8520                       'pay_balance_pkg.create_all_grp_balances_full', 90);
8521          end if;
8522          --
8523          ins_run_balance (p_defined_balance_id => l_output_list(cnt).defined_balance_id,
8524                           p_eff_date           => aarec.effective_date,
8525                           p_bal_val            => l_output_list(cnt).balance_value,
8526                           p_payroll_act_id     => p_pact_id,
8527                           p_asg_act_id         => null,
8528                           p_asg_id             => null,
8529                           p_act_seq            => null,
8530                           p_tax_unit           => l_output_list(cnt).tax_unit_id,
8531                           p_jurisdiction       => l_output_list(cnt).jurisdiction_code,
8532                           p_source_id          => l_output_list(cnt).source_id,
8533                           p_source_text        => l_output_list(cnt).source_text,
8534                           p_source_number      => l_output_list(cnt).source_number,
8535                           p_source_text2       => l_output_list(cnt).source_text2,
8536                           p_time_def_id        => l_output_list(cnt).time_def_id,
8537                           p_balance_date       => l_output_list(cnt).balance_date,
8538                           p_local_unit_id      => l_output_list(cnt).local_unit_id,
8539                           p_source_number2     => l_output_list(cnt).source_number2,
8540                           p_organization_id    => l_output_list(cnt).organization_id
8541                          );
8542 --
8543        end if;
8544      end loop;
8545    end loop;
8546 --
8547    if g_debug then
8548       hr_utility.set_location(
8549                 'Leaving: pay_balance_pkg.create_all_grp_balances_full', 95);
8550    end if;
8551 --
8552 end create_all_grp_balances_full;
8553 -----------------------------------------------------------------------------
8554 -- procedure add_asgbal_to_list
8555 -----------------------------------------------------------------------------
8556 procedure add_asgbal_to_list(p_def_bal_id  in     number,
8557                              p_load_type   in     varchar2,
8558                              p_asgact_id   in     number,
8559                              p_output_list in out nocopy t_detailed_bal_out_tab,
8560                              p_next_free   in out nocopy number
8561                             )
8562 is
8563 cursor get_contexts(asgact    in number,
8564                     p_jur_lvl    in number,
8565                     p_tu_needed  in varchar2,
8566                     p_jc_needed  in varchar2,
8567                     p_si_needed  in varchar2,
8568                     p_st_needed  in varchar2,
8569                     p_sn_needed  in varchar2,
8570                     p_st2_needed in varchar2,
8571                     p_td_needed  in varchar2,
8572                     p_bd_needed  in varchar2,
8573                     p_lu_needed in varchar2,
8574                     p_sn2_needed in varchar2,
8575                     p_org_needed in varchar2) is
8576 select /*+ ORDERED USE_NL(prr) INDEX(prr pay_run_results_n50)*/
8577        distinct
8578        decode(p_tu_needed,
8579               'Y', paa.tax_unit_id,
8580               null)                                           tax_unit_id
8581 ,      decode(p_jc_needed,
8582               'Y', substr(prr.jurisdiction_code, 1, p_jur_lvl),
8583               null)                                           jurisdiction_code
8584 ,      decode(p_si_needed,
8585               'Y', find_context('SOURCE_ID', prr.run_result_id),
8586               null)                                            source_id
8587 ,      decode(p_st_needed,
8588               'Y', find_context('SOURCE_TEXT', prr.run_result_id),
8589               null)                                            source_text
8590 ,      decode(p_sn_needed,
8591               'Y', find_context('SOURCE_NUMBER', prr.run_result_id),
8592               null)                                            source_number
8593 ,      decode(p_st2_needed,
8594               'Y', find_context('SOURCE_TEXT2', prr.run_result_id),
8595               null)                                            source_text2
8596 ,      decode(p_sn2_needed,
8597               'Y', find_context('SOURCE_NUMBER2', prr.run_result_id),
8598               null)                                            source_number2
8599 ,      decode(p_org_needed,
8603               'Y', prr.time_definition_id,
8600               'Y', find_context('ORGANIZATION_ID', prr.run_result_id),
8601               null)                                            organization_id
8602 ,      decode(p_td_needed,
8604               null)                                            time_definition_id
8605 ,      decode(p_bd_needed,
8606               'Y', nvl(prr.end_date, ptp.end_date),
8607               null)                                            balance_date
8608 ,      decode(p_lu_needed,
8609               'Y', prr.local_unit_id,
8610               null)                                            local_unit_id
8611   from pay_assignment_actions paa,
8612        pay_payroll_actions    ppa,
8613        per_time_periods       ptp,
8614        pay_run_results        prr
8615  where paa.assignment_action_id = asgact
8616    and paa.assignment_action_id = prr.assignment_action_id
8617    and ppa.payroll_action_id = paa.payroll_action_id
8618    and ppa.payroll_id = ptp.payroll_id
8619    and ppa.date_earned between ptp.start_date
8620                            and ptp.end_date
8621   order by 1, 2, 3, 4;
8622 --
8623 l_defbal_rec           t_def_bal_rec;
8624 l_tu_needed_chr        pay_legislation_rules.rule_mode%type;
8625 l_jc_needed_chr        pay_legislation_rules.rule_mode%type;
8626 l_si_needed_chr        pay_legislation_rules.rule_mode%type;
8627 l_st_needed_chr        pay_legislation_rules.rule_mode%type;
8628 l_sn_needed_chr        pay_legislation_rules.rule_mode%type;
8629 l_st2_needed_chr       pay_legislation_rules.rule_mode%type;
8630 l_td_needed_chr        pay_legislation_rules.rule_mode%type;
8631 l_bd_needed_chr        pay_legislation_rules.rule_mode%type;
8632 l_lu_needed_chr        pay_legislation_rules.rule_mode%type;
8633 l_sn2_needed_chr       pay_legislation_rules.rule_mode%type;
8634 l_org_needed_chr       pay_legislation_rules.rule_mode%type;
8635 --
8636 begin
8637 --
8638 if g_debug then
8639   hr_utility.set_location('Entering pay_balance_pkg.add_asgbal_to_list',10);
8640   hr_utility.trace('p_load_type: '||p_load_type);
8641 end if;
8642       l_jc_needed_chr := 'N';
8643       l_si_needed_chr := 'N';
8644       l_st_needed_chr := 'N';
8645       l_sn_needed_chr := 'N';
8646       l_tu_needed_chr := 'N';
8647       l_st2_needed_chr := 'N';
8648       l_td_needed_chr := 'N';
8649       l_bd_needed_chr := 'N';
8650       l_lu_needed_chr := 'N';
8651       l_sn2_needed_chr := 'N';
8652       l_org_needed_chr := 'N';
8653       load_defbal_cache(p_def_bal_id,
8654                           l_defbal_rec);
8655 --
8656       if (l_defbal_rec.jc_needed = TRUE) then
8657          l_jc_needed_chr := 'Y';
8658       end if;
8659       if (l_defbal_rec.si_needed = TRUE) then
8660          l_si_needed_chr := 'Y';
8661       end if;
8662       if (l_defbal_rec.st_needed = TRUE) then
8663          l_st_needed_chr := 'Y';
8664       end if;
8665       if (l_defbal_rec.sn_needed = TRUE) then
8666          l_sn_needed_chr := 'Y';
8667       end if;
8668       if (l_defbal_rec.tu_needed = TRUE) then
8669          l_tu_needed_chr := 'Y';
8670       end if;
8671       if (l_defbal_rec.st2_needed = TRUE) then
8672          l_st2_needed_chr := 'Y';
8673       end if;
8674       if (l_defbal_rec.td_needed = TRUE) then
8675          l_td_needed_chr := 'Y';
8676       end if;
8677       if (l_defbal_rec.bd_needed = TRUE) then
8678          l_bd_needed_chr := 'Y';
8679       end if;
8680       if (l_defbal_rec.lu_needed = TRUE) then
8681          l_lu_needed_chr := 'Y';
8682       end if;
8683       if (l_defbal_rec.sn2_needed = TRUE) then
8684          l_sn2_needed_chr := 'Y';
8685       end if;
8686       if (l_defbal_rec.org_needed = TRUE) then
8687          l_org_needed_chr := 'Y';
8688       end if;
8689 --
8690      if (p_load_type = 'FORCE') then
8691        if g_debug then
8692           hr_utility.set_location('pay_balance_pkg.add_asgbal_to_list', 20);
8693        end if;
8694        delete from pay_run_balances
8695         where defined_balance_id = p_def_bal_id
8696           and assignment_action_id = p_asgact_id;
8697      elsif (p_load_type = 'TRUSTED') then
8698        null;
8699      else
8700        if g_debug then
8701           hr_utility.set_location('pay_balance_pkg.add_asgbal_to_list', 25);
8702        end if;
8703         declare
8704            l_dummy number;
8705         begin
8706            select 1
8707              into l_dummy
8708              from dual
8709             where exists (select ''
8710                             from pay_run_balances
8711                            where defined_balance_id = p_def_bal_id
8712                              and assignment_action_id = p_asgact_id
8713                              and balance_value <> 0);
8714 --
8715            -- Error, there should be no rows in this mode
8716            hr_utility.set_message(801,'HR_34723_NO_ROWS_NORMAL_MODE');
8717            hr_utility.raise_error;
8718 --
8719         exception
8720            when no_data_found then
8721                null;
8722         end;
8723      end if;
8724 --
8725       for ctxrec in get_contexts(p_asgact_id,
8726                                  l_defbal_rec.jurisdiction_lvl,
8727                                  l_tu_needed_chr,
8728                                  l_jc_needed_chr,
8729                                  l_si_needed_chr,
8733                                  l_td_needed_chr,
8730                                  l_st_needed_chr,
8731                                  l_sn_needed_chr,
8732                                  l_st2_needed_chr,
8734                                  l_bd_needed_chr,
8735                                  l_lu_needed_chr,
8736                                  l_sn2_needed_chr,
8737                                  l_org_needed_chr) loop
8738 --
8739 --       Only add to the list if all the context values are known
8740 --
8741          if (    ((    l_defbal_rec.st2_needed = TRUE
8742                    and ctxrec.source_text2 is not null)
8743                   or l_defbal_rec.st2_needed = FALSE)
8744              and ((    l_defbal_rec.jc_needed = TRUE
8745                    and ctxrec.jurisdiction_code is not null)
8746                   or l_defbal_rec.jc_needed = FALSE)
8747              and ((    l_defbal_rec.tu_needed = TRUE
8748                    and ctxrec.tax_unit_id is not null)
8749                   or l_defbal_rec.tu_needed = FALSE)
8750              and ((    l_defbal_rec.si_needed = TRUE
8751                    and ctxrec.source_id is not null)
8752                   or l_defbal_rec.si_needed = FALSE)
8753              and ((    l_defbal_rec.st_needed = TRUE
8754                    and ctxrec.source_text is not null)
8755                   or l_defbal_rec.st_needed = FALSE)
8756              and ((    l_defbal_rec.sn_needed = TRUE
8757                    and ctxrec.source_number is not null)
8758                   or l_defbal_rec.sn_needed = FALSE)
8759              and ((    l_defbal_rec.td_needed = TRUE
8760                    and ctxrec.time_definition_id is not null)
8761                   or l_defbal_rec.td_needed = FALSE)
8762              and ((    l_defbal_rec.bd_needed = TRUE
8763                    and ctxrec.balance_date is not null)
8764                   or l_defbal_rec.bd_needed = FALSE)
8765              and ((    l_defbal_rec.lu_needed = TRUE
8766                    and ctxrec.local_unit_id is not null)
8767                   or l_defbal_rec.lu_needed = FALSE)
8768              and ((    l_defbal_rec.sn2_needed = TRUE
8769                    and ctxrec.source_number2 is not null)
8770                   or l_defbal_rec.sn2_needed = FALSE)
8771              and ((    l_defbal_rec.org_needed = TRUE
8772                    and ctxrec.organization_id is not null)
8773                   or l_defbal_rec.org_needed = FALSE)
8774             ) then
8775 --
8776            p_output_list(p_next_free).defined_balance_id:= p_def_bal_id;
8777            p_output_list(p_next_free).tax_unit_id := ctxrec.tax_unit_id;
8778            p_output_list(p_next_free).jurisdiction_code := ctxrec.jurisdiction_code;
8779            p_output_list(p_next_free).source_id := ctxrec.source_id;
8780            p_output_list(p_next_free).source_text := ctxrec.source_text;
8781            p_output_list(p_next_free).source_number := ctxrec.source_number;
8782            p_output_list(p_next_free).source_text2 := ctxrec.source_text2;
8783            p_output_list(p_next_free).time_def_id := ctxrec.time_definition_id;
8784            p_output_list(p_next_free).balance_date := ctxrec.balance_date;
8785            p_output_list(p_next_free).local_unit_id := ctxrec.local_unit_id;
8786            p_output_list(p_next_free).source_number2 := ctxrec.source_number2;
8787            p_output_list(p_next_free).organization_id := ctxrec.organization_id;
8788            p_output_list(p_next_free).balance_value := 0;
8789            p_output_list(p_next_free).balance_found := FALSE;
8790            p_output_list(p_next_free).jurisdiction_lvl := l_defbal_rec.jurisdiction_lvl;
8791            p_next_free := p_next_free + 1;
8792          end if;
8793 --
8794       end loop;
8795 --
8796 if g_debug then
8797   hr_utility.set_location('Leaving: pay_balance_pkg.add_asgbal_to_list', 40);
8798 end if;
8799 --
8800 end add_asgbal_to_list;
8801 --
8802 procedure create_all_asg_balances_full(p_asgact_id  in number,
8803                                   p_bal_list   in varchar2 default 'ALL',
8804                                   p_load_type  in varchar2 default 'NORMAL',
8805                                   p_def_bal    in number   default null,
8806                                   p_eff_date   in date     default null,
8807                                   p_delta      in varchar2 default null
8808                                  )
8809 is
8810 --
8811 cursor crs_asgact (p_asg_act_id number,
8812                    p_bus_grp_id number,
8813                    p_leg_code   varchar2,
8814                    p_bal_list   varchar2,
8815                    p_def_bal    number)
8816 is
8817 select  /*+ ORDERED USE_NL(pbf pbt pdb pbd prrv)
8818             INDEX(prrv PAY_RUN_RESULT_VALUES_N50)*/
8819        distinct  pdb.defined_balance_id,
8820        pbt.jurisdiction_level,
8821        pbt.balance_type_id,
8822        pbd.balance_dimension_id
8823   from
8824        pay_run_results      prr,
8825        pay_run_result_values prrv,
8826        pay_balance_feeds_f  pbf,
8827        pay_balance_types    pbt,
8828        pay_defined_balances pdb,
8829        pay_balance_dimensions pbd
8830  where pbd.dimension_level = 'ASG'
8831    and pdb.save_run_balance|| decode (pbt.balance_type_id, 0, '', '')= 'Y'
8832    and pdb.balance_dimension_id = pbd.balance_dimension_id
8833    and pdb.balance_type_id = pbt.balance_type_id
8834    and (    p_bal_list <> 'INVALID'
8835          or (    p_bal_list = 'INVALID'
8839                             and pbv.business_group_id = p_bus_grp_id
8836              and exists (select ''
8837                            from pay_balance_validation pbv
8838                           where pbv.defined_balance_id = pdb.defined_balance_id
8840                             and pbv.run_balance_status = 'P')
8841             )
8842        )
8843    and (   p_def_bal is null
8844         or p_def_bal = pdb.defined_balance_id
8845        )
8846    and ((pdb.business_group_id = p_bus_grp_id
8847           and pdb.legislation_code is null)
8848         or
8849          (pdb.legislation_code = p_leg_code
8850           and pdb.business_group_id is null)
8851         or
8852          (pdb.legislation_code is null
8853           and pdb.business_group_id is null)
8854        )
8855    and prr.assignment_action_id = p_asg_act_id
8856    and prr.run_result_id = prrv.run_result_id
8857    and prrv.input_value_id = pbf.input_value_id
8858    and pbt.balance_type_id = pbf.balance_type_id;
8859 --   and exists (select /*+ ORDERED */ ''
8860 --                 from pay_run_results prr,
8861 --                      pay_run_result_values prrv,
8862 --                      pay_balance_feeds_f pbf
8863 --                where prr.assignment_action_id = p_asg_act_id
8864 --                  and prr.run_result_id = prrv.run_result_id
8865 --                  and prrv.input_value_id = pbf.input_value_id
8866 --                  and pbf.balance_type_id = pbt.balance_type_id);
8867 --
8868 cursor crs_balatt (p_asg_act_id number
8869                   ,p_bal_list   varchar2
8870                   ,p_def_bal_id number
8871                    )
8872 is
8873 select /*+ ORDERED */
8874        pdb.defined_balance_id,
8875        pdb.balance_type_id,
8876        pdb.balance_dimension_id
8877   from
8878        pay_bal_attribute_definitions pbad,
8879        pay_balance_attributes        pba,
8880        pay_defined_balances          pdb,
8881        pay_balance_dimensions        pbd
8882 
8883  where
8884        pbad.attribute_name = p_bal_list
8885    and pbad.attribute_id   = pba.attribute_id
8886    and pba.defined_balance_id = pdb.defined_balance_id
8887    and pdb.balance_dimension_id = pbd.balance_dimension_id
8888    and pbd.dimension_level = 'ASG'
8889    and p_def_bal_id = pba.defined_balance_id
8890    and exists (select /*+ ORDERED */ ''
8891                  from pay_run_results prr,
8892                       pay_run_result_values prrv,
8893                       pay_balance_feeds_f pbf
8894                 where prr.assignment_action_id = p_asg_act_id
8895                   and prr.run_result_id = prrv.run_result_id
8896                   and prrv.input_value_id = pbf.input_value_id
8897                   and pdb.balance_type_id = pbf.balance_type_id
8898               )
8899 union all
8900 select
8901        pdb.defined_balance_id,
8902        pdb.balance_type_id,
8903        pdb.balance_dimension_id
8904   from
8905        pay_bal_attribute_definitions pbad,
8906        pay_balance_attributes        pba,
8907        pay_defined_balances          pdb,
8908        pay_balance_dimensions        pbd
8909  where
8910        pbad.attribute_name = p_bal_list
8911    and pbad.attribute_id   = pba.attribute_id
8912    and pba.defined_balance_id = pdb.defined_balance_id
8913    and pdb.balance_dimension_id = pbd.balance_dimension_id
8914    and pbd.dimension_level = 'ASG'
8915    and p_def_bal_id is null
8916    and pdb.balance_type_id in
8917                    (select /*+ USE_NL(paa,prr,prrv,pbf) ORDERED */
8918                            distinct pbf.balance_type_id
8919                     from pay_run_results prr,
8920                          pay_run_result_values prrv,
8921                          pay_balance_feeds_f pbf
8922                    where prr.assignment_action_id = p_asg_act_id
8923                      and prr.run_result_id = prrv.run_result_id
8924                      and prrv.input_value_id = pbf.input_value_id);
8925 --
8926 --cursor crs_delta_balatt (p_asg_act_id number
8927 --                        ,p_bal_list   varchar2
8928 --                        ,p_def_bal_id number
8929 --                        ,p_bus_grp_id number
8930 --                        ,p_eff_date   date
8931 --                        )
8932 --is
8933 --select /*+ ORDERED */
8934 --       pdb.defined_balance_id,
8935 --       pdb.balance_type_id,
8936 --       pdb.balance_dimension_id
8937 --  from
8938 --       pay_bal_attribute_definitions pbad,
8939 --       pay_balance_attributes        pba,
8940 --       pay_defined_balances          pdb,
8941 --       pay_balance_dimensions        pbd
8942 -- where
8943 --       pbad.attribute_name = p_bal_list
8944 --   and pbad.attribute_id   = pba.attribute_id
8945 --   and pba.defined_balance_id = pdb.defined_balance_id
8946 --   and pdb.balance_dimension_id = pbd.balance_dimension_id
8947 --   and pbd.dimension_level = 'ASG'
8948 --   and exists (select ''
8949 --               from pay_balance_validation pbv
8950 --               where pdb.defined_balance_id + decode(PDB.LEGISLATION_SUBGROUP, ' ', 0, 0) = pbv.defined_balance_id
8951 --               and   pbv.run_balance_status = 'V'
8952 --               and   pbv.business_group_id = p_bus_grp_id
8953 --               and   pbv.balance_load_date is not null
8954 --               and   pbv.balance_load_date > p_eff_date)
8955 --   and pdb.balance_type_id in
8959 --                      pay_run_result_values prrv,
8956 --                (select /*+ USE_NL(paa,prr,prrv,pbf) ORDERED */
8957 --                      distinct pbf.balance_type_id
8958 --                 from pay_run_results prr,
8960 --                      pay_balance_feeds_f pbf
8961 --                where prr.assignment_action_id = p_asg_act_id
8962 --                  and prr.run_result_id = prrv.run_result_id
8963 --                  and prrv.input_value_id = pbf.input_value_id
8964 --              );
8965 --
8966 --
8967 -- Bug 5947296. Changed the sql statement not to scan through run results
8968 --              more than once.
8969 --
8970 l_bal_attid number; -- balance attribute id.
8971 
8972 cursor crs_delta_balatt (p_asg_act_id number
8973                         ,p_bal_list   varchar2
8974                         ,p_def_bal_id number
8975                         ,p_bus_grp_id number
8976                         ,p_eff_date   date
8977                         ,p_att_id     number
8978                         )
8979 is
8980   select /*+ ORDERED
8981              use_nl(piv pbf pdb pbd pbv prrv)
8982           */
8983          distinct
8984          pdb.defined_balance_id,
8985          pdb.balance_type_id,
8986          pdb.balance_dimension_id
8987   from
8988     pay_run_results        prr,
8989     pay_input_values_f     piv,
8990     pay_balance_feeds_f    pbf,
8991     pay_defined_balances   pdb,
8992     pay_balance_attributes pba,
8993     pay_balance_dimensions pbd,
8994     pay_balance_validation pbv,
8995     pay_run_result_values  prrv
8996   where
8997       prr.assignment_action_id = p_asg_act_id
8998   and piv.element_type_id = prr.element_type_id
8999   and p_eff_date between piv.effective_start_date
9000                      and piv.effective_end_date
9001   and pbf.input_value_id = piv.input_value_id
9002   and p_eff_date between pbf.effective_start_date
9003                      and pbf.effective_end_date
9004   and pdb.balance_type_id = pbf.balance_type_id
9005   and pdb.save_run_balance = 'Y'
9006   and pba.attribute_id = p_att_id
9007   and pba.defined_balance_id = pdb.defined_balance_id
9008   and pdb.balance_dimension_id = pbd.balance_dimension_id
9009   and pbd.dimension_level = 'ASG'
9010   and pbv.defined_balance_id = pdb.defined_balance_id
9011   and pbv.run_balance_status = 'V'
9012   and pbv.business_group_id = p_bus_grp_id
9013   and pbv.balance_load_date > p_eff_date
9014   and prrv.run_result_id = prr.run_result_id
9015   and prrv.input_value_id = pbf.input_value_id
9016   and nvl(prrv.result_value,'0') <> '0'
9017   ;
9018 
9019 --
9020 cursor get_aa (asgact    in number) is
9021 select paa.assignment_action_id,
9022        ppa.effective_date,
9023        paa.assignment_id,
9024        paa.action_sequence
9025   from pay_assignment_actions paa,
9026        pay_payroll_actions    ppa
9027  where ppa.payroll_action_id = paa.payroll_action_id
9028    and paa.assignment_action_id = asgact;
9029 --
9030 cursor get_contexts_2(asgact    in number,
9031                     p_si_needed  in varchar2,
9032                     p_st_needed  in varchar2,
9033                     p_sn_needed  in varchar2,
9034                     p_st2_needed in varchar2,
9035                     p_sn2_needed  in varchar2,
9036                     p_org_needed  in varchar2) is
9037 select /*+ ORDERED USE_NL(prr) INDEX(prr pay_run_results_n50)*/
9038        distinct
9039        paa.tax_unit_id                                         tax_unit_id
9040 ,      prr.jurisdiction_code                                   jurisdiction_code
9041 ,      decode(p_si_needed,
9042               'Y', find_context('SOURCE_ID', prr.run_result_id),
9043               null)                                            source_id
9044 ,      decode(p_st_needed,
9045               'Y', find_context('SOURCE_TEXT', prr.run_result_id),
9046               null)                                            source_text
9047 ,      decode(p_sn_needed,
9048               'Y', find_context('SOURCE_NUMBER', prr.run_result_id),
9049               null)                                            source_number
9050 ,      decode(p_st2_needed,
9051               'Y', find_context('SOURCE_TEXT2', prr.run_result_id),
9052               null)                                            source_text2
9053 ,      decode(p_sn2_needed,
9054               'Y', find_context('SOURCE_NUMBER2', prr.run_result_id),
9055               null)                                            source_number2
9056 ,      decode(p_org_needed,
9057               'Y', find_context('ORGANIZATION_ID', prr.run_result_id),
9058               null)                                            organization_id
9059 ,      prr.time_definition_id
9060 ,      nvl(prr.end_date, ptp.end_date)                         balance_date
9061 ,      prr.local_unit_id
9062   from pay_assignment_actions paa,
9063        pay_payroll_actions    ppa,
9064        per_time_periods       ptp,
9065        pay_run_results        prr
9066  where paa.assignment_action_id = asgact
9067    and paa.assignment_action_id = prr.assignment_action_id
9068    and ppa.payroll_action_id = paa.payroll_action_id
9069    and ppa.payroll_id = ptp.payroll_id
9070    and ppa.date_earned between ptp.start_date
9071                            and ptp.end_date
9072   order by 1, 2, 3, 4;
9073 --
9074 cursor get_bg (aaid number) is
9075 select pbg.business_group_id, pbg.legislation_code
9079  where ppa.payroll_action_id = paa.payroll_action_id
9076   from pay_payroll_actions ppa,
9077        pay_assignment_actions paa,
9078        per_business_groups_perf pbg
9080    and paa.assignment_action_id = aaid
9081    and pbg.business_group_id = ppa.business_group_id;
9082 --
9083 l_defbal_rec           t_def_bal_rec;
9084 l_tu_needed_chr        pay_legislation_rules.rule_mode%type;
9085 l_jc_needed_chr        pay_legislation_rules.rule_mode%type;
9086 l_si_needed_chr        pay_legislation_rules.rule_mode%type;
9087 l_st_needed_chr        pay_legislation_rules.rule_mode%type;
9088 l_sn_needed_chr        pay_legislation_rules.rule_mode%type;
9089 l_st2_needed_chr       pay_legislation_rules.rule_mode%type;
9090 l_td_needed_chr        pay_legislation_rules.rule_mode%type;
9091 l_bd_needed_chr        pay_legislation_rules.rule_mode%type;
9092 l_lu_needed_chr        pay_legislation_rules.rule_mode%type;
9093 l_sn2_needed_chr       pay_legislation_rules.rule_mode%type;
9094 l_org_needed_chr       pay_legislation_rules.rule_mode%type;
9095   l_context_lst   t_context_tab;
9096   l_cnt           number;
9097   l_output_list   t_detailed_bal_out_tab;
9098   l_inp_val_name  pay_input_values_f.name%type;
9099   l_found         boolean;
9100   l_next_free number;
9101   l_bg_id     per_business_groups.business_group_id%type;
9102   l_leg_code  per_business_groups.legislation_code%type;
9103 begin
9104 --
9105 if g_debug then
9106   hr_utility.set_location(
9107             'Entering:pay_balance_pkg.create_all_asg_balances_full',10);
9108 end if;
9109    --
9110    select pbg.business_group_id,
9111           pbg.legislation_code
9112      into l_bg_id,
9113           l_leg_code
9114      from per_business_groups_perf pbg,
9115           pay_payroll_actions ppa,
9116           pay_assignment_actions paa
9117     where paa.assignment_action_id = p_asgact_id
9118       and ppa.payroll_action_id = paa.payroll_action_id
9119       and ppa.business_group_id = pbg.business_group_id;
9120 --
9121    l_next_free := 1;
9122 --
9123    if (    p_bal_list <> 'ALL'
9124        and p_bal_list <> 'INVALID'
9125        and p_bal_list <> 'SINGLE') then
9126 --
9127        /* OK we must be balance attribute */
9128        -- REC 13/10/06 Balance attribute mode can now be used for SINGLE
9129        -- but SINGLE calls this procedure in FORCE mode. So, if p_def_bal
9130        -- is not null and p_bal_list is a GEN_BAL<pact_id) value, then FORCE
9131        -- can be allowed. The run balances will be deleted in procedure
9132        -- add_asgbal_to_list.
9133        --
9134      hr_utility.trace('p_delta is: '||p_delta);
9135      if (p_delta = 'N'
9136          or p_delta is null) then  -- if NOT in  DELTA MODE
9137      --
9138        if (p_load_type = 'TRUSTED') then
9139        --
9140          if g_debug then
9141            hr_utility.set_location(
9142                      'pay_balance_pkg.create_all_asg_balances_full', 20);
9143          end if;
9144          --
9145          for dbarec in crs_balatt(p_asgact_id
9146                                  ,p_bal_list
9147                                  ,p_def_bal
9148                                  ) loop
9149             add_asgbal_to_list(p_def_bal_id  => dbarec.defined_balance_id,
9150                                p_load_type   => p_load_type,
9151                                p_asgact_id   => p_asgact_id,
9152                                p_output_list => l_output_list,
9153                                p_next_free   => l_next_free
9154                               );
9155          end loop;
9156          --
9157        elsif (p_load_type = 'FORCE'
9158               and p_def_bal is not null) then
9159        --
9160        if g_debug then
9161          hr_utility.set_location(
9162                    'pay_balance_pkg.create_all_asg_balances_full', 30);
9163        end if;
9164        --
9165          for dbsarec in crs_balatt(p_asgact_id
9166                                   ,p_bal_list
9167                                   ,p_def_bal
9168                                   ) loop
9169             add_asgbal_to_list(p_def_bal_id  => dbsarec.defined_balance_id,
9170                                p_load_type   => p_load_type,
9171                                p_asgact_id   => p_asgact_id,
9172                                p_output_list => l_output_list,
9173                                p_next_free   => l_next_free
9174                               );
9175          end loop;
9176          --
9177        else -- p_load_type <> TRUSTED
9178 --
9179         pay_core_utils.assert_condition('create_all_asg_balances_full:1', false);
9180       end if;
9181     else -- p_delta is Y thus in DELTA mode
9182     --
9183       if g_debug then
9184          hr_utility.set_location(
9185                    'pay_balance_pkg.create_all_asg_balances_full', 35);
9186        end if;
9187        --
9188        --
9189        -- Obtain the bal attribute id.
9190        --
9191        select attribute_id into l_bal_attid
9192        from pay_bal_attribute_definitions
9193        where attribute_name = p_bal_list;
9194 
9195        --
9196        for dbdrec in crs_delta_balatt(p_asgact_id
9197                                      ,p_bal_list
9198                                      ,p_def_bal
9199                                      ,l_bg_id
9200                                      ,p_eff_date
9204                                p_load_type   => p_load_type,
9201                                      ,l_bal_attid
9202                                      ) loop
9203             add_asgbal_to_list(p_def_bal_id  => dbdrec.defined_balance_id,
9205                                p_asgact_id   => p_asgact_id,
9206                                p_output_list => l_output_list,
9207                                p_next_free   => l_next_free
9208                               );
9209        end loop;
9210     end if;
9211      --
9212    else
9213      if g_debug then
9214        hr_utility.set_location(
9215                  'pay_balance_pkg.create_all_asg_balances_full', 40);
9216      end if;
9217      --
9218       for dbrec in crs_asgact(p_asgact_id,
9219                               l_bg_id,
9220                               l_leg_code,
9221                               p_bal_list,
9222                               p_def_bal) loop
9223          add_asgbal_to_list(p_def_bal_id  => dbrec.defined_balance_id,
9224                             p_load_type   => p_load_type,
9225                             p_asgact_id   => p_asgact_id,
9226                             p_output_list => l_output_list,
9227                             p_next_free   => l_next_free
9228                            );
9229       end loop;
9230    end if;
9231 --
9232 --
9233 -- Only need to do the rest of this procedure if balances existed for the
9234 -- people - i.e. if rows returned either from crs_balatt or crs_asgact.
9235 --
9236 if l_output_list.count > 0 then
9237 --
9238 -- Generate the context list
9239 --
9240    -- Check whether the SOURCE_ID, SOURCE_TEXT contexts are used.
9241    l_si_needed_chr := 'N';
9242    l_st_needed_chr := 'N';
9243    l_sn_needed_chr := 'N';
9244    l_st2_needed_chr := 'N';
9245    l_sn2_needed_chr := 'N';
9246    l_org_needed_chr := 'N';
9247    for bgrec in get_bg(p_asgact_id) loop
9248 --
9249      pay_core_utils.get_leg_context_iv_name('SOURCE_ID',
9250                                             bgrec.legislation_code,
9251                                             l_inp_val_name,
9252                                             l_found
9253                                            );
9254     if (l_found = TRUE) then
9255       l_si_needed_chr := 'Y';
9256     end if;
9257 --
9258      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT',
9259                                             bgrec.legislation_code,
9260                                             l_inp_val_name,
9261                                             l_found
9262                                            );
9263     if (l_found = TRUE) then
9264       l_st_needed_chr := 'Y';
9265     end if;
9266 --
9267      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER',
9268                                             bgrec.legislation_code,
9269                                             l_inp_val_name,
9270                                             l_found
9271                                            );
9272     if (l_found = TRUE) then
9273       l_sn_needed_chr := 'Y';
9274     end if;
9275 --
9276      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT2',
9277                                             bgrec.legislation_code,
9278                                             l_inp_val_name,
9279                                             l_found
9280                                            );
9281     if (l_found = TRUE) then
9282       l_st2_needed_chr := 'Y';
9283     end if;
9284 --
9285      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER2',
9286                                             bgrec.legislation_code,
9287                                             l_inp_val_name,
9288                                             l_found
9289                                            );
9290     if (l_found = TRUE) then
9291       l_sn2_needed_chr := 'Y';
9292     end if;
9293 --
9294      pay_core_utils.get_leg_context_iv_name('ORGANIZATION_ID',
9295                                             bgrec.legislation_code,
9296                                             l_inp_val_name,
9297                                             l_found
9298                                            );
9299     if (l_found = TRUE) then
9300       l_org_needed_chr := 'Y';
9301     end if;
9302 --
9303    end loop;
9304    l_cnt := 1;
9305    if g_debug then
9306       hr_utility.set_location(
9307                 'pay_balance_pkg.create_all_asg_balances_full', 50);
9308    end if;
9309    for cxt in get_contexts_2(p_asgact_id,
9310                            l_si_needed_chr,
9311                            l_st_needed_chr,
9312                            l_sn_needed_chr,
9313                            l_st2_needed_chr,
9314                            l_sn2_needed_chr,
9315                            l_org_needed_chr) loop
9316       if g_debug then
9317          hr_utility.set_location(
9318                    'pay_balance_pkg.create_all_asg_balances_full', 60);
9319       end if;
9320       l_context_lst(l_cnt).tax_unit_id := cxt.tax_unit_id;
9321       l_context_lst(l_cnt).jurisdiction_code := cxt.jurisdiction_code;
9322       l_context_lst(l_cnt).source_id := cxt.source_id;
9323       l_context_lst(l_cnt).source_text := cxt.source_text;
9324       l_context_lst(l_cnt).source_number := cxt.source_number;
9325       l_context_lst(l_cnt).source_text2 := cxt.source_text2;
9329       l_context_lst(l_cnt).organization_id := cxt.organization_id;
9326       l_context_lst(l_cnt).time_def_id := cxt.time_definition_id;
9327       l_context_lst(l_cnt).local_unit_id := cxt.local_unit_id;
9328       l_context_lst(l_cnt).source_number2 := cxt.source_number2;
9330       l_context_lst(l_cnt).balance_date := cxt.balance_date;
9331 --
9332       l_cnt := l_cnt + 1;
9333    end loop;
9334 --
9335 -- Go Get the balance values
9336 --
9337    if g_debug then
9338       hr_utility.set_location(
9339                 'pay_balance_pkg.create_all_asg_balances_full', 70);
9340    end if;
9341    for aarec in get_aa (p_asgact_id) loop
9342       pay_balance_pkg.get_value_int_batch (p_asgact_id,
9343                                  l_context_lst,
9344                                  TRUE,
9345                                  FALSE,
9346                                  l_output_list);
9347 --
9348 --   Insert the results in the run_balance table.
9349 --
9350      if g_debug then
9351         hr_utility.set_location(
9352                   'pay_balance_pkg.create_all_asg_balances_full', 80);
9353      end if;
9354      for cnt in 1..l_output_list.count loop
9355        if (l_output_list(cnt).balance_value <> 0) then
9356 --
9357          if g_debug then
9358             hr_utility.set_location(
9359                       'pay_balance_pkg.create_all_asg_balances_full', 90);
9360          end if;
9361          ins_run_balance (p_defined_balance_id => l_output_list(cnt).defined_balance_id,
9362                           p_eff_date           => aarec.effective_date,
9363                           p_bal_val            => l_output_list(cnt).balance_value,
9364                           p_payroll_act_id     => null,
9365                           p_asg_act_id         => p_asgact_id,
9366                           p_asg_id             => aarec.assignment_id,
9367                           p_act_seq            => aarec.action_sequence,
9368                           p_tax_unit           => l_output_list(cnt).tax_unit_id,
9369                           p_jurisdiction       => l_output_list(cnt).jurisdiction_code,
9370                           p_source_id          => l_output_list(cnt).source_id,
9371                           p_source_text        => l_output_list(cnt).source_text,
9372                           p_source_number      => l_output_list(cnt).source_number,
9373                           p_source_text2       => l_output_list(cnt).source_text2,
9374                           p_time_def_id        => l_output_list(cnt).time_def_id,
9375                           p_balance_date       => l_output_list(cnt).balance_date,
9376                           p_local_unit_id      => l_output_list(cnt).local_unit_id,
9377                           p_source_number2     => l_output_list(cnt).source_number2,
9378                           p_organization_id    => l_output_list(cnt).organization_id
9379                          );
9380 --
9381        end if;
9382      end loop;
9383    end loop;
9384 --
9385 else -- no balances returned from cursors crs_balatt or crs_asgact, so do
9386      -- nothing
9387   hr_utility.set_location('pay_balance_pkg.create_all_asg_balances_full',98);
9388 end if;
9389 --
9390 if g_debug then
9391   hr_utility.set_location(
9392             'Leaving: pay_balance_pkg.create_all_asg_balances_full', 100);
9393 end if;
9394 --
9395 end create_all_asg_balances_full;
9396 --------------------------------------------------------------------------
9397 -- procedure create_all_asg_balances
9398 --------------------------------------------------------------------------
9399 procedure create_all_asg_balances(p_asgact_id  in number,
9400                                   p_bal_list   in varchar2 default 'ALL',
9401                                   p_load_type  in varchar2 default 'NORMAL',
9402                                   p_eff_date   in date     default null,
9403                                   p_delta      in varchar2  default null
9404                                  )
9405 is
9406 --
9407 cursor crs_asgact (p_asg_act_id number)
9408 is
9409 select pdb.defined_balance_id
9410   from pay_defined_balances pdb,
9411        pay_balance_dimensions pbd,
9412        pay_assignment_actions paa,
9413        pay_payroll_actions    ppa,
9414        per_business_groups_perf pbg
9415  where pbd.dimension_level = 'ASG'
9416    and pdb.save_run_balance = 'Y'
9417    and pdb.balance_dimension_id = pbd.balance_dimension_id
9418    and paa.assignment_action_id = p_asg_act_id
9419    and paa.payroll_action_id = ppa.payroll_action_id
9420    and ppa.business_group_id = pbg.business_group_id
9421    and ((pdb.business_group_id = pbg.business_group_id
9422           and pdb.legislation_code is null)
9423         or
9424          (pdb.legislation_code = pbg.legislation_code
9425           and pdb.business_group_id is null)
9426         or
9427          (pdb.legislation_code is null
9428           and pdb.business_group_id is null)
9429        );
9430 --
9431 cursor crs_invalid (p_asg_act_id number)
9432 is
9433 select /*+ ORDERED */
9434        pdb.defined_balance_id
9435   from pay_assignment_actions paa,
9436        pay_payroll_actions    ppa,
9437        per_business_groups_perf pbg,
9438        pay_balance_validation pbv,
9439        pay_defined_balances pdb,
9440        pay_balance_dimensions pbd
9441  where pbd.dimension_level = 'ASG'
9442    and pdb.save_run_balance = 'Y'
9443    and pdb.balance_dimension_id = pbd.balance_dimension_id
9444    and paa.assignment_action_id = p_asg_act_id
9445    and paa.payroll_action_id = ppa.payroll_action_id
9446    and ppa.business_group_id = pbg.business_group_id
9447    and pbv.defined_balance_id = pdb.defined_balance_id
9448    and pbv.business_group_id  = ppa.business_group_id
9449    and pbv.run_balance_status <> 'V'
9450    and ((pdb.business_group_id = pbg.business_group_id
9451           and pdb.legislation_code is null)
9452         or
9453          (pdb.legislation_code = pbg.legislation_code
9454           and pdb.business_group_id is null)
9455         or
9456          (pdb.legislation_code is null
9457           and pdb.business_group_id is null)
9458        );
9459 --
9460 save_run_bals pay_legislation_rules.rule_mode%type;
9461 bal_ret_buffer_sz number;
9462 l_param_value     pay_action_parameters.parameter_value%type;
9463 l_found       boolean;
9464 l_balance_lst t_balance_value_tab;
9465 l_cnt         number;
9466 l_delta       boolean;
9467 --
9468 begin
9469 g_debug := hr_utility.debug_enabled;
9470 if g_debug then
9471    hr_utility.set_location('Entering: pay_balance_pkg.create_all_asg_balances', 5);
9472 end if;
9473 --
9474    l_balance_lst.delete;
9475 --
9476    /* Get the buffer size */
9477    pay_core_utils.get_action_parameter('BAL_RETRIEVAL_BUFFER_SIZE',
9478                                        l_param_value,
9479                                        l_found);
9480    if (l_found = TRUE) then
9481      bal_ret_buffer_sz := to_number(l_param_value);
9482      if (bal_ret_buffer_sz > 1000) then
9483       bal_ret_buffer_sz := 1000;
9484      end if;
9485    else
9486      bal_ret_buffer_sz := 100;
9487    end if;
9488    hr_utility.trace('Action Parameter BAL_RETRIEVAL_BUFFER_SIZE = '||bal_ret_buffer_sz);
9489 --
9490    /* First get the legislation rule */
9491    begin
9492       select plr.rule_mode
9493         into save_run_bals
9494         from pay_legislation_rules plr,
9495              per_business_groups_perf pbg,
9496              pay_payroll_actions ppa,
9497              pay_assignment_actions paa
9498        where ppa.payroll_action_id = paa.payroll_action_id
9499          and paa.assignment_action_id = p_asgact_id
9500          and ppa.business_group_id = pbg.business_group_id
9501          and pbg.legislation_code = plr.legislation_code
9502          and plr.rule_type = 'SAVE_ASG_RUN_BAL';
9503    exception
9504       when no_data_found then
9505         save_run_bals := 'N';
9506    end;
9507 --
9508    l_cnt := 1;
9509    if (save_run_bals = 'Y') then
9510 --
9511      if g_debug then
9512         hr_utility.set_location('pay_balance_pkg.create_all_asg_balances', 25);
9513      end if;
9514 --
9515      if (p_bal_list = 'ALL'
9516          or (    p_bal_list = 'INVALID'
9517              and p_load_type = 'TRUSTED')
9518          or (    p_bal_list <> 'ALL'
9519              and p_bal_list <> 'INVALID'
9520              and p_bal_list <> 'SINGLE')) then
9521      --
9522        create_all_asg_balances_full(p_asgact_id => p_asgact_id
9523                                    ,p_bal_list  => p_bal_list
9524                                    ,p_load_type => p_load_type
9525                                    ,p_eff_date  => p_eff_date
9526                                    ,p_delta     => p_delta
9527                                  );
9528 --
9529      elsif (p_bal_list = 'INVALID') then
9530 --
9531        for fullrec in crs_invalid(p_asgact_id) loop
9532 --
9533          l_balance_lst(l_cnt).defined_balance_id := fullrec.defined_balance_id;
9534          if g_debug then
9535             hr_utility.trace('Added Def Bal ID '||
9536                               l_balance_lst(l_cnt).defined_balance_id);
9537          end if;
9538 --
9539          /* Have we reached the buffer limit */
9540          if (l_cnt = bal_ret_buffer_sz) then
9541 --
9542            create_set_asg_balance(l_balance_lst,
9543                                   p_asgact_id,
9544                                   p_load_type);
9545            l_balance_lst.delete;
9546            l_cnt := 1;
9547          else
9548            l_cnt := l_cnt + 1;
9549          end if;
9550 --
9551        end loop;
9552 --
9553 --
9554        /* Do we still have values in the buffer to process */
9555        if (l_cnt <> 1) then
9556          create_set_asg_balance(l_balance_lst,
9557                                 p_asgact_id,
9558                                 p_load_type);
9559          l_balance_lst.delete;
9560        end if;
9561 --
9562      end if;
9563 --
9564    end if;
9565 --
9566 if g_debug then
9567    hr_utility.set_location('Leaving: pay_balance_pkg.create_all_asg_balances', 30);
9568 end if;
9569 end create_all_asg_balances;
9570 --
9571 --------------------------------------------------------------------------
9572 -- procedure create_set_group_balance
9573 --------------------------------------------------------------------------
9574 procedure create_set_group_balance(p_defined_balance_lst  in out nocopy t_balance_value_tab,
9575                                    p_pact_id              in            number,
9576                                    p_load_type            in            varchar2 default 'NORMAL')
9577 is
9578 --
9579 cursor get_contexts(p_pact_id    in number,
9580                     p_si_needed  in varchar2,
9581                     p_st_needed  in varchar2,
9582                     p_sn_needed  in varchar2,
9583                     p_st2_needed  in varchar2,
9584                     p_sn2_needed  in varchar2,
9585                     p_org_needed  in varchar2) is
9586 select distinct
9587        paa.tax_unit_id                                         tax_unit_id
9588 ,      prr.jurisdiction_code                                   jurisdiction_code
9589 ,      decode(p_si_needed,
9590               'Y', find_context('SOURCE_ID', prr.run_result_id),
9591               null)                                            source_id
9592 ,      decode(p_st_needed,
9593               'Y', find_context('SOURCE_TEXT', prr.run_result_id),
9594               null)                                            source_text
9595 ,      decode(p_sn_needed,
9596               'Y', find_context('SOURCE_NUMBER', prr.run_result_id),
9597               null)                                            source_number
9598 ,      decode(p_st2_needed,
9599               'Y', find_context('SOURCE_TEXT2', prr.run_result_id),
9600               null)                                            source_text2
9601 ,      decode(p_sn2_needed,
9602               'Y', find_context('SOURCE_NUMBER2', prr.run_result_id),
9603               null)                                            source_number2
9604 ,      decode(p_org_needed,
9605               'Y', find_context('ORGANIZATION_ID', prr.run_result_id),
9606               null)                                            organization_id
9607 ,      prr.time_definition_id
9608 ,      nvl(prr.end_date, ptp.end_date)                         balance_date
9609 ,      prr.local_unit_id
9610   from pay_assignment_actions paa,
9611        pay_run_results        prr,
9612        pay_payroll_actions    ppa,
9613        per_time_periods       ptp
9614  where ppa.payroll_action_id = p_pact_id
9615    and paa.payroll_action_id = ppa.payroll_action_id
9616    and paa.assignment_action_id = prr.assignment_action_id
9617    and ptp.payroll_id = ppa.payroll_id
9618    and ppa.date_earned between ptp.start_date
9619                            and ptp.end_date
9620   order by 1, 2, 3, 4;
9621 --
9622 cursor get_aa (p_pact_id    in number) is
9623 select paa.assignment_action_id,
9624        ppa.effective_date
9625   from pay_assignment_actions paa,
9626        pay_payroll_actions    ppa
9627  where ppa.payroll_action_id = p_pact_id
9628    and ppa.payroll_action_id = paa.payroll_action_id
9629    and rownum = 1;
9630 --
9631 cursor get_bg (pactid number) is
9632 select pbg.business_group_id, pbg.legislation_code
9633   from pay_payroll_actions ppa,
9634        per_business_groups_perf pbg
9635  where payroll_action_id = pactid
9636    and pbg.business_group_id = ppa.business_group_id;
9637 --
9638   l_si_needed_chr varchar2(10);
9639   l_st_needed_chr varchar2(10);
9640   l_sn_needed_chr varchar2(10);
9641   l_sn2_needed_chr varchar2(10);
9642   l_org_needed_chr varchar2(10);
9643   l_st2_needed_chr varchar2(10);
9644   l_context_lst   t_context_tab;
9645   l_cnt           number;
9646   l_output_list   t_detailed_bal_out_tab;
9647   l_inp_val_name  pay_input_values_f.name%type;
9648   l_found         boolean;
9649 --
9650 begin
9651 --
9652    if g_debug then
9653       hr_utility.set_location('Entering: pay_balance_pkg.create_set_group_balance', 5);
9654    end if;
9655 --
9656    -- Check whether the SOURCE_ID, SOURCE_TEXT contexts are used.
9657    l_si_needed_chr := 'N';
9658    l_st_needed_chr := 'N';
9659    l_sn_needed_chr := 'N';
9660    l_st2_needed_chr := 'N';
9661    l_sn2_needed_chr := 'N';
9662    l_org_needed_chr := 'N';
9663    for bgrec in get_bg(p_pact_id) loop
9664 --
9665      pay_core_utils.get_leg_context_iv_name('SOURCE_ID',
9666                                             bgrec.legislation_code,
9667                                             l_inp_val_name,
9668                                             l_found
9669                                            );
9670     if (l_found = TRUE) then
9671       l_si_needed_chr := 'Y';
9672     end if;
9673 --
9674      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT',
9675                                             bgrec.legislation_code,
9676                                             l_inp_val_name,
9677                                             l_found
9678                                            );
9679     if (l_found = TRUE) then
9680       l_st_needed_chr := 'Y';
9681     end if;
9682 --
9683      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER',
9684                                             bgrec.legislation_code,
9685                                             l_inp_val_name,
9686                                             l_found
9687                                            );
9688     if (l_found = TRUE) then
9689       l_sn_needed_chr := 'Y';
9690     end if;
9691 --
9692      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT2',
9693                                             bgrec.legislation_code,
9694                                             l_inp_val_name,
9695                                             l_found
9696                                            );
9697     if (l_found = TRUE) then
9698       l_st2_needed_chr := 'Y';
9699     end if;
9700 --
9701      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER2',
9702                                             bgrec.legislation_code,
9703                                             l_inp_val_name,
9704                                             l_found
9705                                            );
9706     if (l_found = TRUE) then
9707       l_sn2_needed_chr := 'Y';
9708     end if;
9709 --
9710      pay_core_utils.get_leg_context_iv_name('ORGANIZATION_ID',
9711                                             bgrec.legislation_code,
9712                                             l_inp_val_name,
9713                                             l_found
9714                                            );
9715     if (l_found = TRUE) then
9716       l_org_needed_chr := 'Y';
9717     end if;
9718 --
9719    end loop;
9720 --
9721 -- Validate/Delete the pay_run_balances for this mode.
9722 --
9723    for cnt in 1..p_defined_balance_lst.count loop
9724 --
9725      if (p_load_type = 'FORCE') then
9726        if g_debug then
9727           hr_utility.set_location('pay_balance_pkg.create_set_group_balance', 20);
9728        end if;
9729        delete from pay_run_balances
9730         where defined_balance_id = p_defined_balance_lst(cnt).defined_balance_id
9731           and payroll_action_id = p_pact_id;
9732      elsif (p_load_type = 'TRUSTED') then
9733         null;
9734      else
9735        if g_debug then
9736           hr_utility.set_location('pay_balance_pkg.create_set_group_balance', 25);
9737        end if;
9738         declare
9739            l_dummy number;
9740         begin
9741            select 1
9742              into l_dummy
9743              from dual
9744             where exists (select 1
9745                             from pay_run_balances
9746                            where defined_balance_id = p_defined_balance_lst(cnt).defined_balance_id
9747                              and payroll_action_id = p_pact_id
9748                              and balance_value <> 0);
9749 --
9750            /* Error, there should be no rows in this mode */
9751            hr_utility.set_message(801,'HR_34723_NO_ROWS_NORMAL_MODE');
9752            hr_utility.raise_error;
9753 --
9754         exception
9755            when no_data_found then
9756                null;
9757         end;
9758      end if;
9759    end loop;
9760 --
9761 -- Generate the context list
9762 --
9763    l_cnt := 1;
9764    if g_debug then
9765       hr_utility.set_location('pay_balance_pkg.create_set_group_balance', 27);
9766    end if;
9767    for cxt in get_contexts(p_pact_id,
9768                            l_si_needed_chr,
9769                            l_st_needed_chr,
9770                            l_sn_needed_chr,
9771                            l_st2_needed_chr,
9772                            l_sn2_needed_chr,
9773                            l_org_needed_chr) loop
9774       if g_debug then
9775          hr_utility.set_location('pay_balance_pkg.create_set_group_balance', 28);
9776       end if;
9777       l_context_lst(l_cnt).tax_unit_id := cxt.tax_unit_id;
9778       l_context_lst(l_cnt).jurisdiction_code := cxt.jurisdiction_code;
9779       l_context_lst(l_cnt).source_id := cxt.source_id;
9780       l_context_lst(l_cnt).source_text := cxt.source_text;
9781       l_context_lst(l_cnt).source_number := cxt.source_number;
9782       l_context_lst(l_cnt).source_text2 := cxt.source_text2;
9783       l_context_lst(l_cnt).time_def_id := cxt.time_definition_id;
9784       l_context_lst(l_cnt).local_unit_id := cxt.local_unit_id;
9785       l_context_lst(l_cnt).source_number2 := cxt.source_number2;
9786       l_context_lst(l_cnt).organization_id := cxt.organization_id;
9787       l_context_lst(l_cnt).balance_date := cxt.balance_date;
9788 --
9789       l_cnt := l_cnt + 1;
9790    end loop;
9791 --
9792 -- Go Get the balance values
9793 --
9794    if g_debug then
9795       hr_utility.set_location('pay_balance_pkg.create_set_group_balance', 30);
9796    end if;
9797    for aarec in get_aa (p_pact_id) loop
9798       pay_balance_pkg.get_value (aarec.assignment_action_id,
9799                                  p_defined_balance_lst,
9800                                  l_context_lst,
9801                                  TRUE,
9802                                  FALSE,
9803                                  l_output_list);
9804 --
9805 --   Insert the results in the run_balance table.
9806 --
9807      if g_debug then
9808         hr_utility.set_location('pay_balance_pkg.create_set_group_balance', 35);
9809      end if;
9810      for cnt in 1..l_output_list.count loop
9811        if g_debug then
9812           hr_utility.trace('Bal Value = '||l_output_list(cnt).balance_value);
9813        end if;
9814        if (l_output_list(cnt).balance_value <> 0) then
9815 --
9816          if g_debug then
9817             hr_utility.set_location('pay_balance_pkg.create_set_group_balance', 40);
9818          end if;
9819          ins_run_balance (p_defined_balance_id => l_output_list(cnt).defined_balance_id,
9820                           p_eff_date           => aarec.effective_date,
9821                           p_bal_val            => l_output_list(cnt).balance_value,
9822                           p_payroll_act_id     => p_pact_id,
9823                           p_asg_act_id         => null,
9824                           p_asg_id             => null,
9825                           p_act_seq            => null,
9826                           p_tax_unit           => l_output_list(cnt).tax_unit_id,
9827                           p_jurisdiction       => l_output_list(cnt).jurisdiction_code,
9828                           p_source_id          => l_output_list(cnt).source_id,
9829                           p_source_text        => l_output_list(cnt).source_text,
9830                           p_source_number      => l_output_list(cnt).source_number,
9831                           p_source_text2       => l_output_list(cnt).source_text2,
9832                           p_time_def_id        => l_output_list(cnt).time_def_id,
9833                           p_balance_date       => l_output_list(cnt).balance_date,
9834                           p_local_unit_id      => l_output_list(cnt).local_unit_id,
9835                           p_source_number2     => l_output_list(cnt).source_number2,
9836                           p_organization_id    => l_output_list(cnt).organization_id
9837                          );
9838 --
9839        end if;
9840      end loop;
9841    end loop;
9842 --
9843    if g_debug then
9844       hr_utility.set_location('Leaving: pay_balance_pkg.create_set_group_balance', 50);
9845    end if;
9846 end create_set_group_balance;
9847 --------------------------------------------------------------------------
9848 -- procedure create_all_group_balances
9849 --------------------------------------------------------------------------
9850 procedure create_all_group_balances(p_pact_id    in number
9851                                    ,p_bal_list   in varchar2 default 'ALL'
9852                                    ,p_load_type  in varchar2 default 'NORMAL'
9853                                    ,p_eff_date   in date     default NULL
9854                                    ,p_delta      in varchar2 default NULL
9855                                    )
9856 is
9857 --
9858 cursor crs_pact (p_pact_id number)
9859 is
9860 select pdb.defined_balance_id
9861   from pay_defined_balances pdb,
9862        pay_balance_dimensions pbd,
9863        pay_payroll_actions    ppa,
9864        per_business_groups_perf pbg
9865  where pbd.dimension_level = 'GRP'
9866    and pdb.save_run_balance = 'Y'
9867    and pdb.balance_dimension_id = pbd.balance_dimension_id
9868    and ppa.payroll_action_id = p_pact_id
9869    and ppa.business_group_id = pbg.business_group_id
9870    and ((pdb.business_group_id = pbg.business_group_id
9871       and pdb.legislation_code is null)
9872     or
9873      (pdb.legislation_code = pbg.legislation_code
9874       and pdb.business_group_id is null)
9875     or
9876      (pdb.legislation_code is null
9877       and pdb.business_group_id is null)
9878    );
9879 --
9880 cursor crs_invalid (p_pact_id number)
9881 is
9882 select /*+ ORDERED */
9883        pdb.defined_balance_id
9884   from pay_payroll_actions    ppa,
9885        per_business_groups_perf pbg,
9886        pay_balance_validation pbv,
9887        pay_defined_balances pdb,
9888        pay_balance_dimensions pbd
9889  where pbd.dimension_level = 'GRP'
9890    and pdb.save_run_balance = 'Y'
9891    and pdb.balance_dimension_id = pbd.balance_dimension_id
9892    and ppa.payroll_action_id = p_pact_id
9893    and ppa.business_group_id = pbg.business_group_id
9894    and pbv.defined_balance_id = pdb.defined_balance_id
9895    and pbv.business_group_id = ppa.business_group_id
9896    and pbv.run_balance_status <> 'V'
9897    and ((pdb.business_group_id = pbg.business_group_id
9898       and pdb.legislation_code is null)
9899     or
9900      (pdb.legislation_code = pbg.legislation_code
9901       and pdb.business_group_id is null)
9902     or
9903      (pdb.legislation_code is null
9904       and pdb.business_group_id is null)
9905    );
9906 --
9907 save_run_bals pay_legislation_rules.rule_mode%type;
9908 bal_ret_buffer_sz number;
9909 l_param_value     pay_action_parameters.parameter_value%type;
9910 l_found       boolean;
9911 l_balance_lst t_balance_value_tab;
9912 l_cnt         number;
9913 --
9914 begin
9915 g_debug := hr_utility.debug_enabled;
9916 if g_debug then
9917    hr_utility.set_location('Entering: pay_balance_pkg.create_all_group_balances', 5);
9918 end if;
9919 --
9920    l_balance_lst.delete;
9921 --
9922    /* Get the buffer size */
9923    pay_core_utils.get_action_parameter('BAL_RETRIEVAL_BUFFER_SIZE',
9924                                        l_param_value,
9925                                        l_found);
9926    if (l_found = TRUE) then
9927      bal_ret_buffer_sz := to_number(l_param_value);
9928     if (bal_ret_buffer_sz > 1000) then
9929       bal_ret_buffer_sz := 1000;
9930     end if;
9931    else
9932      bal_ret_buffer_sz := 100;
9933    end if;
9934    hr_utility.trace('Action Parameter BAL_RETRIEVAL_BUFFER_SIZE = '||bal_ret_buffer_sz);
9935 --
9936    /* First get the legislation rule */
9937    begin
9938       select plr.rule_mode
9939         into save_run_bals
9940         from pay_legislation_rules plr,
9941              per_business_groups_perf pbg,
9942              pay_payroll_actions ppa
9943        where ppa.payroll_action_id = p_pact_id
9944          and ppa.business_group_id = pbg.business_group_id
9945          and pbg.legislation_code = plr.legislation_code
9946          and plr.rule_type = 'SAVE_RUN_BAL';
9947    exception
9948       when no_data_found then
9949         save_run_bals := 'N';
9950    end;
9951 --
9952    if (save_run_bals = 'Y') then
9953 --
9954      if g_debug then
9955         hr_utility.set_location('pay_balance_pkg.create_all_group_balances', 25);
9956      end if;
9957      l_cnt := 1;
9958 --
9959      if (p_bal_list = 'ALL'
9960          or (    p_bal_list = 'INVALID'
9961              and p_load_type = 'TRUSTED')
9962          or (    p_bal_list <> 'ALL'
9963              and p_bal_list <> 'INVALID'
9964              and p_bal_list <> 'SINGLE')) then
9965 --
9966        create_all_grp_balances_full(p_pact_id   => p_pact_id
9967                                    ,p_bal_list  => p_bal_list
9968                                    ,p_load_type => p_load_type
9969                                    ,p_eff_date  => p_eff_date
9970                                    ,p_delta     => p_delta
9971                                    );
9972 --
9973      elsif (p_bal_list = 'INVALID') then
9974 --
9975        for fullrec in crs_invalid(p_pact_id) loop
9976 --
9977          l_balance_lst(l_cnt).defined_balance_id := fullrec.defined_balance_id;
9978          if g_debug then
9979             hr_utility.trace('Added Def Bal ID '||
9980                               l_balance_lst(l_cnt).defined_balance_id);
9981          end if;
9982 --
9983          /* Have we reached the buffer limit */
9984          if (l_cnt = bal_ret_buffer_sz) then
9985 --
9986            create_set_group_balance(l_balance_lst,
9987                                     p_pact_id,
9988                                     p_load_type);
9989            l_balance_lst.delete;
9990            l_cnt := 1;
9991          else
9992            l_cnt := l_cnt + 1;
9993          end if;
9994 --
9995        end loop;
9996 --
9997        /* Do we still have values in the buffer to process */
9998        if (l_cnt <> 1) then
9999          create_set_group_balance(l_balance_lst,
10000                                   p_pact_id,
10001                                   p_load_type);
10002          l_balance_lst.delete;
10003        end if;
10004 --
10005      end if;
10006 --
10007    end if;
10008 --
10009 if g_debug then
10010    hr_utility.set_location('Leaving: pay_balance_pkg.create_all_group_balances', 30);
10011 end if;
10012 end create_all_group_balances;
10013 --
10014 --------------------------------------------------------------------------
10015 -- procedure initialise_run_balance
10016 -- This procedure initialises a run balance by creating a row in
10017 -- pay_balance_validation. This procedure will be called from the
10018 -- defined_balance trigger.
10019 -- For a user defined balance, a row will be inserted for the bg of the def bal.
10020 -- For a legislative defined balance, a row will be inserted for each bg within
10021 -- the specific legislation.
10022 -- For a core defined balance, a row will be inserted for each bg within
10023 -- those legislations that have been enabled for run balances.
10024 
10025 
10026 --------------------------------------------------------------------------
10027 procedure initialise_run_balance(p_defbal_id         in number
10028                                 ,p_baldim_id         in number
10029                                 ,p_bal_type_id       in number
10030                                 ,p_legislation_code  in varchar2
10031                                 ,p_business_group_id in number)
10032 is
10033 --
10034 --
10035 -- This cursor returns the dimension_level and confirms that it is a valid run
10036 --  balance defined balance, i.e save_run_bal = 'Y' and dim_type = 'R'
10037 --
10038 cursor get_dim_level(p_balance_dimension_id in number)
10039 is
10040 select dimension_level
10041 from   pay_balance_dimensions
10042 where  dimension_type = 'R'
10043 and    balance_dimension_id = p_balance_dimension_id;
10044 --
10045 cursor get_leg_code(p_bg_id number)
10046 is
10047 select legislation_code
10048 from   per_business_groups_perf
10049 where  business_group_id = p_bg_id;
10050 --
10051 cursor enabled_bg(p_bg_id     in number
10052                  ,p_leg_code  in varchar2
10053                  ,p_dim_level in varchar2)
10054 is
10055 select pbg.business_group_id
10056 from   per_business_groups_perf pbg
10057 ,      pay_legislation_rules plr
10058 where  pbg.legislation_code = plr.legislation_code
10059 and    plr.legislation_code = nvl(p_leg_code, plr.legislation_code)
10060 and    pbg.business_group_id = nvl(p_bg_id, pbg.business_group_id)
10061 and    plr.rule_type = decode(p_dim_level, 'ASG', 'SAVE_ASG_RUN_BAL'
10062                                          , 'GRP', 'SAVE_RUN_BAL')
10063 and    plr.rule_mode = 'Y';
10064 --
10065 cursor check_run_results(p_baltype_id in number)
10066 is
10067 select 1
10068 from   dual
10069 where exists (select /*+ ORDERED*/
10070                    null
10071               from pay_balance_feeds_f bf
10072               ,    pay_run_result_values rrv
10073               where bf.balance_type_id = p_baltype_id
10074               and   bf.input_value_id = rrv.input_value_id);
10075 --
10076 -- Bug 3364019 added the + 1 to get the correct balance_load_date
10077 --
10078 cursor bal_load_date(p_business_group_id number)
10079 is
10080 select nvl((max(ppa.effective_date)+1),fnd_date.canonical_to_date('0001/01/01')) bal_load_date
10081 from   pay_payroll_actions ppa
10082 ,      pay_action_classifications pac
10083 where ppa.action_type = pac.action_type
10084 and   pac.classification_name = 'SEQUENCED'
10085 and   ppa.business_group_id = p_business_group_id;
10086 --
10087 l_dim_level     pay_balance_dimensions.dimension_level%type;
10088 l_leg_code      per_business_groups_perf.legislation_code%type;
10089 l_bg_id         per_business_groups_perf.business_group_id%type;
10090 l_bal_load_date pay_balance_validation.balance_load_date%type;
10091 l_rr_exists     number;
10092 --
10093 BEGIN
10094 hr_utility.set_location('Entering: pay_balance_pkg.initialise_run_balance', 10);
10095 --
10096 -- we need to determine a legislation code; for core defined balances it will
10097 -- be null.
10098 --
10099 if p_legislation_code is null then
10100   if p_business_group_id is null then
10101   -- core row
10102     l_leg_code := '';
10103     hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 20);
10104   else -- bg not nulli, so user row
10105     hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 30);
10106     --
10107     open get_leg_code(p_business_group_id);
10108     fetch get_leg_code into l_leg_code;
10109     if get_leg_code%notfound then
10110       close get_leg_code;
10111       -- should raise error, but should never be raised so ok
10112       hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 40);
10113     end if;
10114   end if;
10115 else -- leg row
10116   l_leg_code := p_legislation_code;
10117   hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 50);
10118 end if;
10119 --
10120 open get_dim_level(p_baldim_id);
10121 fetch get_dim_level into l_dim_level;
10122 close get_dim_level;
10123 --
10124 -- determine whether run results exist for this balance. If not then
10125 -- use a bal load date of start of time, else use the max effective date for
10126 -- the bg.
10127 --
10128 open check_run_results(p_bal_type_id);
10129 fetch check_run_results into l_rr_exists;
10130 close check_run_results;
10131 --
10132 if l_dim_level = 'ASG' or l_dim_level = 'GRP' then
10133 hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 70);
10134 --
10135   for each_bg in enabled_bg(p_business_group_id, l_leg_code, l_dim_level)
10136   loop
10137   --
10138     if l_rr_exists = 1 then
10139       open bal_load_date(each_bg.business_group_id);
10140       fetch bal_load_date into l_bal_load_date;
10141       close bal_load_date;
10142     else
10143       l_bal_load_date := fnd_date.canonical_to_date('0001/01/01');
10144     end if;
10145     --
10146     hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 80);
10147     hr_utility.trace('def_bal: '||to_char(p_defbal_id));
10148     hr_utility.trace('bg: '||to_char(each_bg.business_group_id));
10149     hr_utility.trace('bal_load_date: '||to_char(l_bal_load_date,'DD-MON-YYYY'));
10150     --
10151     insert into pay_balance_validation
10152     (BALANCE_VALIDATION_ID
10153     ,DEFINED_BALANCE_ID
10154     ,BUSINESS_GROUP_ID
10155     ,RUN_BALANCE_STATUS
10156     ,BALANCE_LOAD_DATE)
10157     select pay_balance_validation_s.nextval
10158     ,p_defbal_id
10159     ,each_bg.business_group_id
10160     ,'V'
10161     ,l_bal_load_date
10162     from dual
10163     where not exists (select 1
10164                       from pay_balance_validation
10165                       where defined_balance_id = p_defbal_id
10166                       and business_group_id = each_bg.business_group_id);
10167     --
10168     hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 90);
10169     --
10170   end loop;
10171   hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 100);
10172   --
10173 else -- l_dim_level is null
10174 --
10175 -- this is not a valid run balance defined balance, so do nothing
10176 --
10177 hr_utility.set_location('pay_balance_pkg.initialise_run_balance', 110);
10178 end if;
10179 hr_utility.set_location('Leaving: pay_balance_pkg.initialise_run_balance', 120);
10180 end initialise_run_balance;
10181 --------------------------------------------------------------------------
10182 -- procedure set_check_latest_balances
10183 -- This procedure sets the HRASSACT CHECK_LATEST_BALANCES global
10184 -- so that latest and run level balances ARE looked for
10185 --------------------------------------------------------------------------
10186 procedure set_check_latest_balances
10187 is
10188 --
10189 begin
10190   g_debug := hr_utility.debug_enabled;
10191   if g_debug then
10192      hr_utility.set_location('Entering: pay_balance_pkg.set_check_latest_balances',5);
10193   end if;
10194 --
10195    HRASSACT.CHECK_LATEST_BALANCES := TRUE;
10196 --
10197   if g_debug then
10198      hr_utility.set_location('Exiting: pay_balance_pkg.set_check_latest_balances',5);
10199   end if;
10200 --
10201 end set_check_latest_balances;
10202 --------------------------------------------------------------------------
10203 -- procedure unset_check_latest_balances
10204 -- This procedure sets the HRASSACT CHECK_LATEST_BALANCES global
10205 -- so that latest and run level balances ARE NOT looked for
10206 --------------------------------------------------------------------------
10207 procedure unset_check_latest_balances
10208 is
10209 --
10210 begin
10211   g_debug := hr_utility.debug_enabled;
10212   if g_debug then
10213      hr_utility.set_location('Entering: pay_balance_pkg.unset_check_latest_balances',5);
10214   end if;
10215 --
10216    HRASSACT.CHECK_LATEST_BALANCES := FALSE;
10217 --
10218   if g_debug then
10219      hr_utility.set_location('Exiting: pay_balance_pkg.unset_check_latest_balances',5);
10220   end if;
10221 --
10222 end unset_check_latest_balances;
10223 --------------------------------------------------------------------------
10224 --
10225 --------------------------------------------------------------------------
10226 -- procedure create_asg_balance
10227 --------------------------------------------------------------------------
10228 procedure create_asg_balance(p_def_bal_id in number
10229                             ,p_asgact_id  in number
10230                             ,p_load_type  in varchar2 default 'NORMAL'
10231                             ,p_bal_att    in varchar2 default NULL
10232                             ,p_eff_date   in date     default NULL
10233                             ,p_delta      in varchar2 default NULL)
10234 
10235 is
10236 --
10237 begin
10238 --
10239 if g_debug then
10240   hr_utility.set_location('enter:pay_bal_pkg.create_asg_balance',10);
10241 end if;
10242      create_all_asg_balances_full(p_asgact_id  => p_asgact_id
10243                                  ,p_load_type  => p_load_type
10244                                  ,p_def_bal    => p_def_bal_id
10245                                  ,p_bal_list   => p_bal_att
10246                                  ,p_eff_date   => p_eff_date
10247                                  ,p_delta      => p_delta
10248                                  );
10249 --
10250 if g_debug then
10251   hr_utility.set_location('leave:pay_bal_pkg.create_asg_balance',20);
10252 end if;
10253 end create_asg_balance;
10254 --
10255 --------------------------------------------------------------------------
10256 -- procedure create_group_balance
10257 --------------------------------------------------------------------------
10258 procedure create_group_balance(p_def_bal_id in number
10259                               ,p_pact_id    in number
10260                               ,p_load_type  in varchar2 default 'NORMAL'
10261                               ,p_bal_att    in varchar2 default NULL
10262                               ,p_eff_date   in date     default NULL
10263                               ,p_delta      in varchar2 default NULL)
10264 is
10265 --
10266 begin
10267 --
10268 if g_debug then
10269   hr_utility.set_location('Entering:pay_bal_pkg.create_group_balance',10);
10270 end if;
10271 --
10272      create_all_grp_balances_full(p_pact_id    => p_pact_id
10273                                  ,p_load_type  => p_load_type
10274                                  ,p_def_bal    => p_def_bal_id
10275                                  ,p_bal_list   => p_bal_att
10276                                  ,p_eff_date   => p_eff_date
10277                                  ,p_delta      => p_delta
10278                                  );
10279 --
10280 if g_debug then
10281   hr_utility.set_location('Leaving:pay_bal_pkg.create_group_balance',20);
10282 end if;
10283 end create_group_balance;
10284 --
10285 procedure maintain_balances_for_action(p_asg_action in number
10286                                       )
10287 is
10288   cursor rev_rrs (revassactid number, p_si_needed varchar2, p_st_needed varchar2,
10289                   p_sn_needed varchar2, p_st2_needed varchar2,
10290                   p_sn2_needed varchar2, p_org_needed varchar2) is
10291   select prr.run_result_id,
10292          paa.tax_unit_id,
10293          prr.local_unit_id,
10294          prr.jurisdiction_code,
10295          prr.source_id original_entry_id,
10296          ppa.payroll_id,
10297          decode(p_si_needed,
10298               'Y', pay_balance_pkg.find_context('SOURCE_ID', prr.run_result_id),
10299               null)  source_id,
10300          decode(p_st_needed,
10301               'Y', pay_balance_pkg.find_context('SOURCE_TEXT', prr.run_result_id),
10302               null)  source_text,
10303          decode(p_sn_needed,
10304               'Y', pay_balance_pkg.find_context('SOURCE_NUMBER', prr.run_result_id),
10305               null)  source_number,
10306          decode(p_st2_needed,
10307               'Y', pay_balance_pkg.find_context('SOURCE_TEXT2', prr.run_result_id),
10308               null)  source_text2,
10309          decode(p_sn2_needed,
10310               'Y', pay_balance_pkg.find_context('SOURCE_NUMBER2', prr.run_result_id),
10311               null)  source_number2,
10312          decode(p_org_needed,
10313               'Y', pay_balance_pkg.find_context('ORGANIZATION_ID', prr.run_result_id),
10314               null)  organization_id,
10315          ppa.effective_date
10316     from pay_assignment_actions paa,
10317          pay_run_results        prr,
10318          pay_payroll_actions    ppa
10319    where paa.assignment_action_id = revassactid
10320      and ppa.payroll_action_id    = paa.payroll_action_id
10321      and paa.assignment_action_id = prr.assignment_action_id;
10322 --
10323       l_rule_mode pay_legislation_rules.rule_mode%type;
10324       l_si_needed pay_legislation_rules.rule_mode%type;
10325       l_st_needed pay_legislation_rules.rule_mode%type;
10326       l_sn_needed pay_legislation_rules.rule_mode%type;
10327       l_sn2_needed pay_legislation_rules.rule_mode%type;
10328       l_org_needed pay_legislation_rules.rule_mode%type;
10329       l_st2_needed pay_legislation_rules.rule_mode%type;
10330       l_inp_val_name pay_input_values_f.name%type;
10331       l_asg_id     number;
10332       l_pactid     number;
10333       l_eff_date   date;
10334       l_act_type   pay_payroll_actions.action_type%type;
10335       lat_bal_maintenance boolean;
10336       l_src_iv    varchar2(30);
10337       l_src_num   varchar2(30);
10338       l_src_num2  varchar2(30);
10339       l_org_id_iv varchar2(30);
10340       l_iv_found  boolean;
10341       l_num_found boolean;
10342       l_value     pay_action_parameters.parameter_value%type;
10343       l_tax_group hr_organization_information.org_information5%type;
10344       l_bus_grp   per_business_groups_perf.business_group_id%type;
10345       leg_code   per_business_groups_perf.legislation_code%type;
10346       l_found    boolean;
10347       cxt_id number;
10348 
10349       udca      hrassact.context_details;
10350 
10351 
10352 begin
10353    if g_debug then
10354       hr_utility.set_location('hrassact.reversal',50);
10355    end if;
10356 --
10357    select paa.assignment_id,
10358           ppa.effective_date,
10359           ppa.payroll_action_id,
10360           ppa.action_type,
10361           ppa.business_group_id
10362      into l_asg_id,
10363           l_eff_date,
10364           l_pactid,
10365           l_act_type,
10366           l_bus_grp
10367      from pay_assignment_actions paa,
10368           pay_payroll_actions    ppa
10369     where paa.assignment_action_id = p_asg_action
10370       and ppa.payroll_action_id = paa.payroll_action_id;
10371 --
10372    create_all_asg_balances(p_asgact_id => p_asg_action,
10373                            p_bal_list  =>  'ALL',
10374                            p_load_type =>  'NORMAL',
10375                            p_eff_date  =>  null,
10376                            p_delta     =>  null);
10377 --
10378 --
10379    if g_debug then
10380       hr_utility.set_location('hrassact.reversal',55);
10381    end if;
10382 --
10383 -- 2nd the group level run balances
10384 --
10385    if g_debug then
10386       hr_utility.set_location('hrassact.reversal',60);
10387    end if;
10388 --
10389    pay_balance_pkg.create_all_group_balances(l_pactid,
10390                                              'ALL',
10391                                              'NORMAL',
10392                                              NULL,
10393                                              NULL);
10394 --
10395 --
10396 --       Call to start of latest balance maintenance code
10397 --
10398    if (l_act_type = 'V') then
10399       --
10400       -- check for REV_MAINTAIN_LAT_BAL legislation rule to see if
10401       -- this functionality is supported for this reversal
10402       --
10403       begin
10404          select parameter_value
10405          into l_value
10406          from pay_action_parameters pap
10407          where pap.parameter_name = 'REV_LAT_BAL';
10408 
10409          if upper(l_value) = 'Y' then
10410             lat_bal_maintenance := TRUE;
10411          else
10412             lat_bal_maintenance := FALSE;
10413          end if;
10414       exception
10415          when others then
10416             lat_bal_maintenance := FALSE;
10417       end;
10418 
10419       --
10420       -- Bug 6737896: Added pbg.legislation_code in the query to
10421       -- populate leg_code
10422       --
10423       if lat_bal_maintenance = TRUE then
10424          begin
10425             select rule_mode, pbg.legislation_code
10426             into l_rule_mode, leg_code
10427             from pay_legislation_rules plr,
10428                  per_business_groups_perf pbg
10429             where plr.legislation_code  = pbg.legislation_code
10430             and   rule_type             = 'BAL_ADJ_LAT_BAL'
10431             and   pbg.business_group_id = l_bus_grp;
10432 
10433             if upper(l_rule_mode) <> 'Y' then
10434                lat_bal_maintenance := FALSE;
10435             end if;
10436          exception
10437             when others then
10438                lat_bal_maintenance := FALSE;
10439          end;
10440       end if;
10441   end if;
10442 
10443   if lat_bal_maintenance = FALSE then
10444      --
10445      -- delete latest balances
10446      --
10447      if g_debug then
10448         hr_utility.set_location('hrassact.reversal', 70);
10449      end if;
10450 
10451      --
10452      -- NB could enhance to
10453      -- loop for each reversal run result and run
10454      -- del_latest_balances(l_asg_id, l_eff_date, null, l_element_type_id);
10455      -- to minimise loss of latest balances
10456      --
10457      hrassact.del_latest_balances(l_asg_id, l_eff_date, null);
10458   else
10459      --
10460      -- Support maintenance of latest balances
10461      --
10462      if g_debug then
10463         hr_utility.set_location('hrassact.reversal', 80);
10464      end if;
10465 --
10466      hr_utility.trace('leg_code: '||leg_code);
10467      pay_core_utils.get_leg_context_iv_name('SOURCE_ID'
10468                                         ,leg_code
10469                                         ,l_src_iv
10470                                         ,l_iv_found);
10471      if (not l_iv_found) then
10472         l_src_iv := null;
10473      else
10474        l_si_needed := 'Y';
10475        hr_utility.trace('l_src_iv: '||l_src_iv);
10476      end if;
10477      --
10478      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER'
10479                                         ,leg_code
10480                                         ,l_src_num
10481                                         ,l_num_found);
10482      if (not l_num_found) then
10483         l_src_num := null;
10484      else
10485         l_sn_needed := 'Y';
10486         hr_utility.trace('l_src_num: '||l_src_num);
10487      end if;
10488      --
10489      pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER2'
10490                                         ,leg_code
10491                                         ,l_src_num2
10492                                         ,l_num_found);
10493      if (not l_num_found) then
10494         l_src_num2 := null;
10495      else
10496         l_sn2_needed := 'Y';
10497         hr_utility.trace('l_src_num2: '||l_src_num2);
10498      end if;
10499      pay_core_utils.get_leg_context_iv_name('ORGANIZATION_ID'
10500                                         ,leg_code
10501                                         ,l_org_id_iv
10502                                         ,l_num_found);
10503      if (not l_num_found) then
10504         l_org_id_iv := null;
10505      else
10506         l_org_needed := 'Y';
10507         hr_utility.trace('l_org_id_iv: '||l_org_id_iv);
10508      end if;
10509 
10510      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT',
10511                                             leg_code,
10512                                             l_inp_val_name,
10513                                             l_found
10514                                            );
10515      if (l_found = TRUE) then
10516         l_st_needed := 'Y';
10517      end if;
10518 
10519      pay_core_utils.get_leg_context_iv_name('SOURCE_TEXT2',
10520                                             leg_code,
10521                                             l_inp_val_name,
10522                                             l_found
10523                                            );
10524      if (l_found = TRUE) then
10525         l_st2_needed := 'Y';
10526      end if;
10527 
10528      -- get TAX GROUP context value (for US and CA)
10529      if leg_code in ('US', 'CA') then
10530         begin
10531 
10532            if leg_code = 'US' then
10533               select hoi.org_information5
10534                 into l_tax_group
10535                 from hr_organization_information hoi,
10536                      pay_assignment_actions      paa
10537                where UPPER(hoi.org_information_context) = 'FEDERAL TAX RULES'
10538                  and hoi.organization_id = paa.tax_unit_id
10539                  and paa.assignment_action_id = p_asg_action
10540                  and hoi.org_information5 is not null;
10541            else
10542                select hoi.org_information4
10543                  into l_tax_group
10544                  from hr_organization_information hoi,
10545                       pay_assignment_actions      paa
10546                 where UPPER(hoi.org_information_context) = 'CANADA EMPLOYER IDENTIFICATION'
10547                   and hoi.organization_id = paa.tax_unit_id
10548                   and paa.assignment_action_id = p_asg_action
10549                   and hoi.org_information4 is not null;
10550            end if;
10551 
10552         exception
10553            when no_data_found then
10554               l_tax_group := null;
10555         end;
10556      else
10557         l_tax_group := null;
10558      end if;
10559 
10560      for rr in rev_rrs(p_asg_action, l_si_needed, l_st_needed,
10561                        l_sn_needed, l_st2_needed, l_sn2_needed,
10562                        l_org_needed) loop
10563 
10564        -- Load the udca with the context values
10565        udca.sz := 1;
10566        hrassact.get_cache_context('PAYROLL_ID', cxt_id);
10567        udca.cxt_id(udca.sz)    := cxt_id;
10568        udca.cxt_name(udca.sz)  := 'PAYROLL_ID';
10569        udca.cxt_value(udca.sz) := rr.payroll_id;
10570        udca.sz := udca.sz + 1;
10571        hrassact.get_cache_context('ORIGINAL_ENTRY_ID', cxt_id);
10572        udca.cxt_id(udca.sz)    := cxt_id;
10573        udca.cxt_name(udca.sz)  := 'ORIGINAL_ENTRY_ID';
10574        udca.cxt_value(udca.sz) := rr.original_entry_id;
10575        if rr.tax_unit_id is not null then
10576           udca.sz := udca.sz + 1;
10577           hrassact.get_cache_context('TAX_UNIT_ID', cxt_id);
10578           udca.cxt_id(udca.sz)    := cxt_id;
10579           udca.cxt_name(udca.sz)  := 'TAX_UNIT_ID';
10580           udca.cxt_value(udca.sz) := rr.tax_unit_id;
10581        end if;
10582        if rr.jurisdiction_code is not null then
10583           udca.sz := udca.sz + 1;
10584           hrassact.get_cache_context('JURISDICTION_CODE', cxt_id);
10585           udca.cxt_id(udca.sz)    := cxt_id;
10586           udca.cxt_name(udca.sz)  := 'JURISDICTION_CODE';
10587           udca.cxt_value(udca.sz) := rr.jurisdiction_code;
10588        end if;
10589        if rr.source_id is not null then
10590           udca.sz := udca.sz + 1;
10591           hrassact.get_cache_context('SOURCE_ID', cxt_id);
10592           udca.cxt_id(udca.sz)    := cxt_id;
10593           udca.cxt_name(udca.sz)  := 'SOURCE_ID';
10594           udca.cxt_value(udca.sz) := rr.source_id;
10595        end if;
10596        if rr.source_text is not null then
10597           udca.sz := udca.sz + 1;
10598           hrassact.get_cache_context('SOURCE_TEXT', cxt_id);
10599           udca.cxt_id(udca.sz)    := cxt_id;
10600           udca.cxt_name(udca.sz)  := 'SOURCE_TEXT';
10601           udca.cxt_value(udca.sz) := rr.source_text;
10602        end if;
10603        if rr.source_text2 is not null then
10604           udca.sz := udca.sz + 1;
10605           hrassact.get_cache_context('SOURCE_TEXT2', cxt_id);
10606           udca.cxt_id(udca.sz)    := cxt_id;
10607           udca.cxt_name(udca.sz)  := 'SOURCE_TEXT2';
10608           udca.cxt_value(udca.sz) := rr.source_text2;
10609        end if;
10610        if rr.source_number is not null then
10611           udca.sz := udca.sz + 1;
10612           hrassact.get_cache_context('SOURCE_NUMBER', cxt_id);
10613           udca.cxt_id(udca.sz)    := cxt_id;
10614           udca.cxt_name(udca.sz)  := 'SOURCE_NUMBER';
10615           udca.cxt_value(udca.sz) := rr.source_number;
10616        end if;
10617        if rr.source_number2 is not null then
10618           udca.sz := udca.sz + 1;
10619           hrassact.get_cache_context('SOURCE_NUMBER2', cxt_id);
10620           udca.cxt_id(udca.sz)    := cxt_id;
10621           udca.cxt_name(udca.sz)  := 'SOURCE_NUMBER2';
10622           udca.cxt_value(udca.sz) := rr.source_number2;
10623        end if;
10624        if rr.organization_id is not null then
10625           udca.sz := udca.sz + 1;
10626           hrassact.get_cache_context('ORGANIZATION_ID', cxt_id);
10627           udca.cxt_id(udca.sz)    := cxt_id;
10628           udca.cxt_name(udca.sz)  := 'ORGANIZATION_ID';
10629           udca.cxt_value(udca.sz) := rr.organization_id;
10630        end if;
10631        if rr.local_unit_id is not null then
10632           udca.sz := udca.sz + 1;
10633           hrassact.get_cache_context('LOCAL_UNIT_ID', cxt_id);
10634           udca.cxt_id(udca.sz)    := cxt_id;
10635           udca.cxt_name(udca.sz)  := 'LOCAL_UNIT_ID';
10636           udca.cxt_value(udca.sz) := rr.local_unit_id;
10637        end if;
10638        if l_tax_group is not null then
10639           udca.sz := udca.sz + 1;
10640           hrassact.get_cache_context('TAX_GROUP', cxt_id);
10641           udca.cxt_id(udca.sz)    := cxt_id;
10642           udca.cxt_name(udca.sz)  := 'TAX_GROUP';
10643           udca.cxt_value(udca.sz) := l_tax_group;
10644        end if;
10645 
10646        -- call balance maintenance code
10647 
10648        hrassact.maintain_lat_bal(assactid => p_asg_action,
10649                         rrid     => rr.run_result_id,
10650                         eentryid => null,
10651                         effdate  => l_eff_date,
10652                         udca     => udca,
10653                         act_type => l_act_type);
10654 
10655      end loop;
10656 
10657    end if;
10658 --
10659    if g_debug then
10660       hr_utility.set_location('Leaving: hrassact.reversal', 100);
10661    end if;
10662 
10663 end maintain_balances_for_action;
10664 --
10665 begin
10666  g_payroll_action := -1;
10667  g_legislation_code := null;
10668 end pay_balance_pkg;