DBA Data[Home] [Help]

PACKAGE BODY: APPS.PAY_BALANCE_PKG

Source


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