DBA Data[Home] [Help]

PACKAGE BODY: APPS.PAY_GB_EOY_ARCHIVE

Source


1 PACKAGE BODY pay_gb_eoy_archive AS
2 /* $Header: pygbeoya.pkb 120.22 2008/01/22 04:49:54 rlingama noship $ */
3 --
4 ------------------------------- GLOBALS -------------------------------------
5 --
6 g_package    CONSTANT VARCHAR2(20):= 'pay_gb_eoy_archive.';
7 --  Globals populated by archinit procedure or action_creation proc
8 g_payroll_action_id         pay_payroll_actions.payroll_action_id%TYPE;
9 g_start_year                DATE;
10 g_end_year                  DATE;
11 g_business_group_id         hr_organization_units.business_group_id%TYPE;
12 g_permit_number             VARCHAR2(12);
13 g_tax_district_reference    VARCHAR2(3);
14 g_asg_set_id              hr_assignment_sets.assignment_set_id%type;
15 g_context_id                number;
16 g_tax_reference_number      VARCHAR2(10); --4011263: length 10 chars
17 g_nia_able_id               pay_defined_balances.defined_balance_id%TYPE;
18 g_nia_id                    pay_defined_balances.defined_balance_id%TYPE;
19 g_nia_tot_id                pay_defined_balances.defined_balance_id%TYPE;
20 g_nia_lel_id                pay_defined_balances.defined_balance_id%TYPE;
21 g_nia_uel_id                pay_defined_balances.defined_balance_id%TYPE;
22 g_nia_auel_id               pay_defined_balances.defined_balance_id%TYPE; --EOY 07/08
23 g_nia_et_id                 pay_defined_balances.defined_balance_id%TYPE;
24 g_nib_able_id               pay_defined_balances.defined_balance_id%TYPE;
25 g_nib_id                    pay_defined_balances.defined_balance_id%TYPE;
26 g_nib_tot_id                pay_defined_balances.defined_balance_id%TYPE;
27 g_nib_lel_id                pay_defined_balances.defined_balance_id%TYPE;
28 g_nib_uel_id                pay_defined_balances.defined_balance_id%TYPE;
29 g_nib_auel_id               pay_defined_balances.defined_balance_id%TYPE; --EOY 07/08
30 g_nib_et_id                 pay_defined_balances.defined_balance_id%TYPE;
31 g_nic_able_id               pay_defined_balances.defined_balance_id%TYPE;
32 g_nic_tot_id                pay_defined_balances.defined_balance_id%TYPE;
33 g_nic_lel_id                pay_defined_balances.defined_balance_id%TYPE;
34 g_nic_uel_id                pay_defined_balances.defined_balance_id%TYPE;
35 g_nic_auel_id               pay_defined_balances.defined_balance_id%TYPE;  --EOY 07/08
36 g_nic_et_id                 pay_defined_balances.defined_balance_id%TYPE;
37 g_nic_ers_rebate_id         pay_defined_balances.defined_balance_id%TYPE;
38 g_nid_able_id               pay_defined_balances.defined_balance_id%TYPE;
39 g_nid_id                    pay_defined_balances.defined_balance_id%TYPE;
40 g_nid_tot_id                pay_defined_balances.defined_balance_id%TYPE;
41 g_nid_lel_id                pay_defined_balances.defined_balance_id%TYPE;
42 g_nid_uel_id                pay_defined_balances.defined_balance_id%TYPE;
43 g_nid_auel_id               pay_defined_balances.defined_balance_id%TYPE;  --EOY 07/08
44 g_nid_et_id                 pay_defined_balances.defined_balance_id%TYPE;
45 g_nid_ers_rebate_id         pay_defined_balances.defined_balance_id%TYPE;
46 g_nid_ees_rebate_id         pay_defined_balances.defined_balance_id%TYPE;
47 g_nid_rebate_emp_id         pay_defined_balances.defined_balance_id%TYPE;
48 g_nie_able_id               pay_defined_balances.defined_balance_id%TYPE;
49 g_nie_id                    pay_defined_balances.defined_balance_id%TYPE;
50 g_nie_tot_id                pay_defined_balances.defined_balance_id%TYPE;
51 g_nie_lel_id                pay_defined_balances.defined_balance_id%TYPE;
52 g_nie_uel_id                pay_defined_balances.defined_balance_id%TYPE;
53 g_nie_auel_id               pay_defined_balances.defined_balance_id%TYPE; --EOY 07/08
54 g_nie_et_id                 pay_defined_balances.defined_balance_id%TYPE;
55 g_nie_ers_rebate_id         pay_defined_balances.defined_balance_id%TYPE;
56 g_nif_tot_id                pay_defined_balances.defined_balance_id%TYPE;
57 g_nif_ees_rebate_id         pay_defined_balances.defined_balance_id%TYPE;
58 g_nig_tot_id                pay_defined_balances.defined_balance_id%TYPE;
59 g_nis_tot_id                pay_defined_balances.defined_balance_id%TYPE;
60 g_nij_able_id               pay_defined_balances.defined_balance_id%TYPE;
61 g_nij_id                    pay_defined_balances.defined_balance_id%TYPE;
62 g_nij_tot_id                pay_defined_balances.defined_balance_id%TYPE;
63 g_nij_lel_id                pay_defined_balances.defined_balance_id%TYPE;
64 g_nij_uel_id                pay_defined_balances.defined_balance_id%TYPE;
65 g_nij_auel_id               pay_defined_balances.defined_balance_id%TYPE; --EOY 07/08
66 g_nij_et_id                 pay_defined_balances.defined_balance_id%TYPE;
67 g_nil_able_id               pay_defined_balances.defined_balance_id%TYPE;
68 g_nil_id                    pay_defined_balances.defined_balance_id%TYPE;
69 g_nil_tot_id                pay_defined_balances.defined_balance_id%TYPE;
70 g_nil_lel_id                pay_defined_balances.defined_balance_id%TYPE;
71 g_nil_uel_id                pay_defined_balances.defined_balance_id%TYPE;
72 g_nil_auel_id               pay_defined_balances.defined_balance_id%TYPE;  --EOY 07/08
73 g_nil_et_id                 pay_defined_balances.defined_balance_id%TYPE;
74 g_ssp_id                    pay_defined_balances.defined_balance_id%TYPE;
75 g_smp_id                    pay_defined_balances.defined_balance_id%TYPE;
76 g_sap_id                    pay_defined_balances.defined_balance_id%TYPE;
77 g_spp_adopt_id              pay_defined_balances.defined_balance_id%TYPE;
78 g_spp_birth_id              pay_defined_balances.defined_balance_id%TYPE;
79 g_gross_id                  pay_defined_balances.defined_balance_id%TYPE;
80 g_notional_id               pay_defined_balances.defined_balance_id%TYPE;
81 g_paye_id                   pay_defined_balances.defined_balance_id%TYPE;
82 g_super_id                  pay_defined_balances.defined_balance_id%TYPE;
83 g_widow_id                  pay_defined_balances.defined_balance_id%TYPE;
84 g_student_loan_id           pay_defined_balances.defined_balance_id%TYPE;
85 g_taxable_id                pay_defined_balances.defined_balance_id%TYPE;
86 g_ni_arrears_id             pay_defined_balances.defined_balance_id%TYPE;
87 g_paye_details_id           pay_element_types_f.element_type_id%TYPE;
88 g_paye_element_id           pay_element_types_f.element_type_id%TYPE;
89 g_ni_id                     pay_element_types_f.element_type_id%TYPE;
90 g_category_input_id         pay_input_values_f.input_value_id%TYPE;
91 g_scon_input_id             pay_input_values_f.input_value_id%TYPE;
92 g_process_type_id           pay_input_values_f.input_value_id%TYPE;
93 --
94 g_address_line1_eid             ff_user_entities.user_entity_id%TYPE;
95 g_address_line2_eid             ff_user_entities.user_entity_id%TYPE;
96 g_address_line3_eid             ff_user_entities.user_entity_id%TYPE;
97 g_country_eid                   ff_user_entities.user_entity_id%TYPE; -- 4011263
98 g_assignment_number_eid         ff_user_entities.user_entity_id%TYPE;
99 g_county_eid                    ff_user_entities.user_entity_id%TYPE;
100 g_date_of_birth_eid             ff_user_entities.user_entity_id%TYPE;
101 g_director_indicator_eid        ff_user_entities.user_entity_id%TYPE;
102 g_effective_end_date_eid        ff_user_entities.user_entity_id%TYPE;
103 g_effective_start_date_eid      ff_user_entities.user_entity_id%TYPE;
104 g_eoy_primary_flag_eid          ff_user_entities.user_entity_id%TYPE;
105 g_expense_check_to_address_eid  ff_user_entities.user_entity_id%TYPE;
106 g_first_name_eid                ff_user_entities.user_entity_id%TYPE;
107 g_gross_pay_eid                 ff_user_entities.user_entity_id%TYPE;
108 g_notional_pay_eid              ff_user_entities.user_entity_id%TYPE;
109 g_last_asg_action_id_eid        ff_user_entities.user_entity_id%TYPE;
110 g_last_effective_date_eid       ff_user_entities.user_entity_id%TYPE;
111 g_last_multi_asg_eid            ff_user_entities.user_entity_id%TYPE;
112 g_aggregated_paye_flag_eid      ff_user_entities.user_entity_id%TYPE;
113 g_last_name_eid                 ff_user_entities.user_entity_id%TYPE;
114 g_location_id_eid               ff_user_entities.user_entity_id%TYPE;
115 g_max_period_number_eid         ff_user_entities.user_entity_id%TYPE;
116 g_middle_name_eid               ff_user_entities.user_entity_id%TYPE;
117 g_multiple_asg_flag_eid         ff_user_entities.user_entity_id%TYPE;
118 g_ni_number_eid                 ff_user_entities.user_entity_id%TYPE;
119 g_ni_able_et_eid                ff_user_entities.user_entity_id%TYPE;
120 g_ni_able_lel_eid               ff_user_entities.user_entity_id%TYPE;
121 g_ni_able_uel_eid               ff_user_entities.user_entity_id%TYPE;
122 g_ni_able_auel_eid              ff_user_entities.user_entity_id%TYPE;  --EOY 07/08
123 g_ni_earnings_eid               ff_user_entities.user_entity_id%TYPE;
124 g_ni_ees_contribution_eid       ff_user_entities.user_entity_id%TYPE;
125 g_ni_ers_rebate_eid             ff_user_entities.user_entity_id%TYPE;
126 g_ni_ees_rebate_eid             ff_user_entities.user_entity_id%TYPE;
127 g_ni_scon_ees_rebate_eid        ff_user_entities.user_entity_id%TYPE;
128 g_ni_scon_able_et_eid           ff_user_entities.user_entity_id%TYPE;
129 g_ni_scon_able_lel_eid          ff_user_entities.user_entity_id%TYPE;
130 g_ni_scon_able_uel_eid          ff_user_entities.user_entity_id%TYPE;
131 g_ni_scon_able_auel_eid         ff_user_entities.user_entity_id%TYPE; --EOY 07/08
132 g_ni_scon_earnings_eid          ff_user_entities.user_entity_id%TYPE;
133 g_ni_scon_ees_contribution_eid  ff_user_entities.user_entity_id%TYPE;
134 g_ni_scon_ers_rebate_eid        ff_user_entities.user_entity_id%TYPE;
135 g_ni_scon_tot_contribution_eid  ff_user_entities.user_entity_id%TYPE;
136 g_ni_tot_contribution_eid       ff_user_entities.user_entity_id%TYPE;
137 g_ni_refund_eid                 ff_user_entities.user_entity_id%TYPE;
138 g_ni_scon_refund_eid            ff_user_entities.user_entity_id%TYPE;
139 g_organization_id_eid           ff_user_entities.user_entity_id%TYPE;
140 g_payroll_end_year_eid          ff_user_entities.user_entity_id%TYPE;
141 g_payroll_id_eid                ff_user_entities.user_entity_id%TYPE;
142 g_payroll_period_type_eid       ff_user_entities.user_entity_id%TYPE;
143 g_payroll_start_year_eid        ff_user_entities.user_entity_id%TYPE;
144 g_pensioner_indicator_eid       ff_user_entities.user_entity_id%TYPE;
145 g_people_group_id_eid           ff_user_entities.user_entity_id%TYPE;
146 g_permit_number_eid             ff_user_entities.user_entity_id%TYPE;
147 g_person_id_eid                 ff_user_entities.user_entity_id%TYPE;
148 g_postal_code_eid               ff_user_entities.user_entity_id%TYPE;
149 g_prev_tax_paid_eid             ff_user_entities.user_entity_id%TYPE;
150 g_prev_taxable_pay_eid          ff_user_entities.user_entity_id%TYPE;
151 g_sex_eid                       ff_user_entities.user_entity_id%TYPE;
152 g_smp_eid                       ff_user_entities.user_entity_id%TYPE;
153 g_ssp_eid                       ff_user_entities.user_entity_id%TYPE;
154 g_sap_eid                       ff_user_entities.user_entity_id%TYPE;
155 g_spp_adopt_eid                 ff_user_entities.user_entity_id%TYPE;
156 g_spp_birth_eid                 ff_user_entities.user_entity_id%TYPE;
157 g_start_of_emp_eid              ff_user_entities.user_entity_id%TYPE;
158 g_superannuation_paid_eid       ff_user_entities.user_entity_id%TYPE;
159 g_superannuation_refund_eid     ff_user_entities.user_entity_id%TYPE;
160 g_tax_dist_ref_eid              ff_user_entities.user_entity_id%TYPE;
161 g_tax_code_eid                  ff_user_entities.user_entity_id%TYPE;
162 g_tax_paid_eid                  ff_user_entities.user_entity_id%TYPE;
163 g_tax_ref_eid                   ff_user_entities.user_entity_id%TYPE;
164 g_tax_ref_transfer_eid          ff_user_entities.user_entity_id%TYPE;
165 g_tax_refund_eid                ff_user_entities.user_entity_id%TYPE;
166 g_tax_run_result_id_eid         ff_user_entities.user_entity_id%TYPE;
167 g_taxable_pay_eid               ff_user_entities.user_entity_id%TYPE;
168 g_termination_date_eid          ff_user_entities.user_entity_id%TYPE;
169 g_termination_type_eid          ff_user_entities.user_entity_id%TYPE;
170 g_title_eid                     ff_user_entities.user_entity_id%TYPE;
171 g_town_or_city_eid              ff_user_entities.user_entity_id%TYPE;
172 g_w1_m1_indicator_eid           ff_user_entities.user_entity_id%TYPE;
173 g_week_53_indicator_eid         ff_user_entities.user_entity_id%TYPE;
174 g_widows_and_orphans_eid        ff_user_entities.user_entity_id%TYPE;
175 g_student_loans_eid             ff_user_entities.user_entity_id%TYPE;
176 g_assignment_message_eid        ff_user_entities.user_entity_id%TYPE;
177 g_ni_arrears_eid                ff_user_entities.user_entity_id%TYPE;
178 g_reportable_ni_eid             ff_user_entities.user_entity_id%TYPE;
179 g_agg_active_start_eid          ff_user_entities.user_entity_id%TYPE;
180 g_agg_active_end_eid            ff_user_entities.user_entity_id%TYPE;
181 --
182 -- start/end year cache globals initialised in cache_archive_value:
183 g_min_start_year                DATE;
184 g_max_end_year                  DATE;
185 g_output_header                 BOOLEAN := TRUE;
186 g_err_count                     NUMBER;
187 g_warn_count                    NUMBER;
188 g_paye_archive                  BOOLEAN := TRUE; -- Bug 6761725
189 --
190 -- Globals populated by archive_code.archive_agg_values for
191 -- Aggregated PAYE
192 TYPE g_agg_values_rec IS RECORD
193    (smp           NUMBER(15)   := 0,
194     ssp           NUMBER(15)   := 0,
195     sap           NUMBER(15)   := 0,
196     spp_adopt     NUMBER(15)   := 0,
197     spp_birth     NUMBER(15)   := 0,
198     gross_pay     NUMBER(15)   := 0,
199     notional      NUMBER(15)   := 0,
200     paye          NUMBER(15)   := 0,
201     superann      NUMBER(15)   := 0,
202     widows        NUMBER(15)   := 0,
203     taxable       NUMBER(15)   := 0,
204     student_ln    NUMBER(15)   := 0,
205     ni_arrears    NUMBER(15)   := 0,
206     paye_eff_date DATE         := hr_general.start_of_time,
207     tax_code      VARCHAR2(10) := NULL,
208     tax_basis     VARCHAR(1)   := NULL,
209     pay_previous  NUMBER(15)   := 0,
210     tax_previous  NUMBER(15)   := 0,
211     week_53       VARCHAR2(1)  := NULL);
212 --
213 --  Globals populated by archive_code.archive_ni_values procedure for
214 --     Multiple Assignment Logic
215 -- table types:
216 TYPE g_ni_values_rec IS RECORD
217   (ni_cat        VARCHAR2(1),
218    tot_contribs  NUMBER(15) := 0,
219    earnings      NUMBER(15) := 0,
220    ees_contribs  NUMBER(15) := 0,
221    ni_able_lel   NUMBER(15) := 0,
222    ni_able_et    NUMBER(15) := 0,
223    ni_able_uel   NUMBER(15) := 0,
224    ni_able_auel  NUMBER(15) := 0, --EOY 07/08
225    ers_rebate    NUMBER(15) := 0,
226    ees_rebate    NUMBER(15) := 0,
227    ni_refund     VARCHAR2(1),
228    scon          VARCHAR2(15));
229 TYPE g_ni_values_typ IS TABLE OF g_ni_values_rec
230   INDEX BY binary_integer;
231 TYPE g_asg_actions_typ IS TABLE OF
232   pay_assignment_actions.assignment_action_id%TYPE
233   INDEX BY binary_integer;
234 TYPE g_date_table_typ IS TABLE OF DATE
235   INDEX BY binary_integer;
236 TYPE g_period_table_typ IS TABLE OF VARCHAR2(30)
237   INDEX BY binary_integer;
238 TYPE g_tax_ref_table_typ IS TABLE OF VARCHAR2(10) -- 4011263
239   INDEX BY binary_integer;
240 TYPE g_max_per_table_typ IS TABLE OF NUMBER
241   INDEX BY binary_integer;
242 TYPE g_no_fisc_yr_typ   IS TABLE OF
243   per_time_period_types.number_per_fiscal_year%TYPE
244   INDEX BY binary_integer;
245 TYPE g_tax_dist_table_typ IS TABLE OF VARCHAR2(3)
246   INDEX BY binary_integer;
247 TYPE g_permit_no_table_typ IS TABLE OF VARCHAR2(12)
248   INDEX BY binary_integer;
249 TYPE g_cached_varchar_typ IS TABLE OF VARCHAR2(30)
250   INDEX BY binary_integer;
251 -- PL/SQL tables:
252 g_agg_balance_totals       g_agg_values_rec;
253 g_zero_balance_totals      g_agg_values_rec;
254 g_ni_balance_totals        g_ni_values_typ;
255 g_empty_ni_balance_totals  g_ni_values_typ;
256 g_asg_actions              g_asg_actions_typ;
257 g_empty_asg_actions        g_asg_actions_typ;
258 --PL/SQL tables for cached payroll info
259 g_pay_start_yr_tab         g_date_table_typ;
260 g_pay_end_yr_tab           g_date_table_typ;
261 g_pay_max_per_no_tab       g_max_per_table_typ;
262 g_pay_period_typ_tab       g_period_table_typ;
263 g_pay_tax_ref_tab          g_tax_ref_table_typ;
264 g_pay_tax_dist_tab         g_tax_dist_table_typ;
265 g_no_per_fiscal_yr         g_no_fisc_yr_typ;
266 --
267 -- csr_assign cache tables
268 --
269 g_payroll_end_yr_tab       g_cached_varchar_typ;
270 g_payroll_start_yr_tab     g_cached_varchar_typ;
271 g_payroll_tax_ref_tab      g_tax_ref_table_typ;
272 g_payroll_tax_dist_tab     g_tax_dist_table_typ;
273 g_payroll_permit_no_tab    g_permit_no_table_typ;
274 -- variables:
275 -- 1st two are initialised by the archinit procedure
276 g_masg_person_id           per_all_assignments_f.person_id%TYPE;
280 g_masg_active_end           per_all_assignments_f.effective_end_date%TYPE;
277 -- added g_masg_period_of_service_id to fix bug 3784871
278 g_masg_period_of_service_id per_all_assignments_f.period_of_service_id%TYPE;
279 g_masg_active_start         per_all_assignments_f.effective_start_date%TYPE;
281 g_masg_tax_ref_num         VARCHAR2(10); -- 4011263: length 10 chars
282 g_max_gross_pay            NUMBER(15) := NULL;
283 g_primary_action           pay_assignment_actions.assignment_action_id%TYPE;
284 g_min_assignment_id        per_all_assignments_f.assignment_id%TYPE;
285 g_has_non_extracted_masgs  BOOLEAN := FALSE;
286 g_num_actions              binary_integer:=0;
287 --
288 ------------------------------- FUNCTIONS -----------------------------------
289 --
290 FUNCTION get_nearest_scon(p_element_entry_id       IN NUMBER ,
291                           p_assignment_action_id   IN NUMBER,
292                           p_category               IN VARCHAR2 ,
293                           p_effective_date         IN DATE)
294                           RETURN VARCHAR2
295 -- This function searches for a SCON number to associate with the SCON balance
296 -- Balance initialization creates run results prior to the NI row that records
297 -- the SCON number. So find a row for the same category after the effective
298 -- date of the owning payroll action.
299 -- Priority is next latest SCON input with the same Category
300 -- down to next latest SCON input regardless of Category
301 IS
302   cursor get_rrv_scon (c_assignment_action_id number,
303                        c_element_entry_id     number) is
304   --
305   -- Select the SCON number from the Run Result Values table,
306   -- it is possible for the element entry value to have a NULL
307   -- SCON while the Run Result still holds a valid SCON.
308   --
309   select prrv.result_value
310   from pay_run_result_values prrv,
311        pay_run_results prr
312   where prr.source_id = c_element_entry_id
313   and   prr.element_type_id = g_ni_id
314   and   prr.assignment_action_id = c_assignment_action_id
315   and   prrv.run_result_id = prr.run_result_id
316   and   prrv.input_value_id = g_scon_input_id;
317 --
318   cursor get_scon IS
319   -- best match is if the category on the entry matches the balance category
320   -- as a workarround users may have entered scon against a different
321   -- category. So if no category matches just get the nearest scon value
322   --
323   SELECT  scon.screen_entry_value
324   FROM
325     pay_element_entry_values_f  scon,
326     pay_element_entry_values_f  cat
327   WHERE scon.element_entry_id = p_element_entry_id
328   AND   cat.element_entry_id  = p_element_entry_id
329   AND   cat.effective_start_date = scon.effective_start_date
330   AND   cat.effective_end_date   = scon.effective_end_date
331   AND   scon.input_value_id +0   = g_scon_input_id
332   AND   cat.input_value_id +0    = g_category_input_id
333   AND   scon.screen_entry_value IS NOT NULL
334   ORDER BY decode(cat.screen_entry_value,p_category,0,1),
335            ABS(p_effective_date - scon.effective_end_date);
336   --
337   l_scon    VARCHAR2(9):= NULL;
338 BEGIN
339   --dbms_output.put_line('NI ID='||g_ni_id||' EE ID='||p_element_entry_id||' SCON IV='||g_scon_input_id||' CAT IV='||g_category_input_id);
340 
341   /* 4502181- IF globals are null, query them here -- Required by the Tax Payments Listing report */
342   IF G_NI_ID IS NULL THEN
343       SELECT element_type_id
344         INTO   g_ni_id
345         FROM   pay_element_types_f
346         WHERE  element_name = 'NI'
347           AND  p_effective_date BETWEEN effective_start_date AND effective_end_date;
348         --
349         SELECT input_value_id
350         INTO   g_category_input_id
351         FROM   pay_input_values_f
352         WHERE  name = 'Category'
353           AND  element_type_id = g_ni_id
354           AND  p_effective_date BETWEEN effective_start_date AND effective_end_date;
355         --
356         SELECT input_value_id
357         INTO   g_scon_input_id
358         FROM   pay_input_values_f
359         WHERE  name = 'SCON'
360           AND  element_type_id = g_ni_id
361           AND  p_effective_date BETWEEN effective_start_date AND effective_end_date;
362   END IF;
363 
364   BEGIN
365     OPEN get_scon;
366     FETCH get_scon INTO l_scon;
367     CLOSE get_scon;
368   EXCEPTION
369     WHEN no_data_found THEN
370       l_scon := NULL;
371   END;
372 
373   --
374   -- If the scon is not on the EEV, use the
375   -- second cursor to look at the Run Result Value.
376   --
377   IF l_scon is null then
378     open get_rrv_scon(p_assignment_action_id, p_element_entry_id);
379     fetch get_rrv_scon into l_scon;
380     if get_rrv_scon%notfound then
381        l_scon := NULL;
382     end if;
383     close get_rrv_scon;
384   END IF;
385   --
386   RETURN l_scon;
387 END get_nearest_scon;
388 --
389 FUNCTION canonical_to_date(p_chardate   IN VARCHAR2)
390                            RETURN DATE
391 -- Cover on the fnd_date function, but with exception handling
392 IS
393   l_return_date   DATE;
394 BEGIN
395   l_return_date := fnd_date.canonical_to_date(p_chardate);
396   RETURN l_return_date;
397 EXCEPTION
398   WHEN OTHERS THEN
399     RETURN NULL;
400 END canonical_to_date;
401 --
402 FUNCTION canonical_to_number(p_charnum   IN VARCHAR2)
403                              RETURN NUMBER
407 BEGIN
404 -- Cover on the fnd_number function, but with exception handling
405 IS
406   l_return_num NUMBER;
408   l_return_num := fnd_number.canonical_to_number(p_charnum);
409   RETURN l_return_num;
410 EXCEPTION
411   WHEN OTHERS THEN
412     RETURN NULL;
413 END canonical_to_number;
414 --
415 FUNCTION get_arch_str(p_action_id        IN NUMBER,
416                       p_user_entity_id   IN NUMBER,
417                       p_context_value1   IN VARCHAR2 DEFAULT NULL,
418                       p_context_value2   IN VARCHAR2 DEFAULT NULL,
419                       p_context_value3   IN VARCHAR2 DEFAULT NULL)
420                       RETURN VARCHAR2
421 -- Pure Public Function which returns a value from the archive, given
422 -- the action id (ff_archive_items.context1), user entity id and up to
423 -- three additional contexts.  No validation is performed on the input
424 -- parameters.  If a matching item does not exist, null is returned.
425 -- The additional context parameters must be populated in order.
426 IS
427   l_arch_value ff_archive_items.value%type;
428 BEGIN
429   -- use implicit cursors so that too_many_rows can easily be detected
430   IF p_context_value3 IS NOT NULL THEN
431     SELECT fai.VALUE
432       INTO l_arch_value
433       FROM ff_archive_item_contexts aic1,
434            ff_archive_item_contexts aic2,
435            ff_archive_item_contexts aic3,
436            ff_archive_items         fai
437       WHERE fai.context1         = p_action_id
438       AND   fai.user_entity_id   = p_user_entity_id
439       AND   aic1.archive_item_id = fai.archive_item_id
440       AND   aic1.sequence_no     = 1
441       AND   aic1.context         = p_context_value1
442       AND   aic2.archive_item_id = fai.archive_item_id
443       AND   aic2.sequence_no     = 2
444       AND   aic2.context         = p_context_value2
445       AND   aic3.archive_item_id = fai.archive_item_id
446       AND   aic3.sequence_no     = 3
447       AND   aic3.context         = p_context_value3;
448   ELSIF p_context_value2 IS NOT NULL THEN
449     SELECT fai.VALUE
450       INTO l_arch_value
451       FROM ff_archive_items         fai,
452            ff_archive_item_contexts aic1,
453            ff_archive_item_contexts aic2
454       WHERE fai.context1         = p_action_id
455       AND   fai.user_entity_id   = p_user_entity_id
456       AND   aic1.archive_item_id = fai.archive_item_id
457       AND   aic1.sequence_no     = 1
458       AND   aic1.context         = p_context_value1
459       AND   aic2.archive_item_id = fai.archive_item_id
460       AND   aic2.sequence_no     = 2
461       AND   aic2.context         = p_context_value2;
462   ELSIF p_context_value1 IS NOT NULL THEN
463     SELECT fai.VALUE
464       INTO l_arch_value
465       FROM ff_archive_item_contexts aic1,
466            ff_archive_items         fai
467       WHERE fai.context1         = p_action_id
468       AND   fai.user_entity_id   = p_user_entity_id
469       AND   aic1.archive_item_id = fai.archive_item_id
470       AND   aic1.sequence_no     = 1
471       AND   aic1.context         = p_context_value1;
472   ELSE
473     SELECT fai.VALUE
474       INTO l_arch_value
475       FROM ff_archive_items        fai
476      WHERE fai.context1         = p_action_id
477        AND fai.user_entity_id   = p_user_entity_id;
478   END IF;
479   RETURN l_arch_value;
480 EXCEPTION
481   WHEN OTHERS THEN
482     RETURN NULL;
483 END get_arch_str;
484 --
485 FUNCTION get_arch_str(p_action_id        IN NUMBER,
486                       p_user_entity_name IN VARCHAR2,
487                       p_context_value1   IN VARCHAR2 DEFAULT NULL,
488                       p_context_value2   IN VARCHAR2 DEFAULT NULL,
489                       p_context_value3   IN VARCHAR2 DEFAULT NULL)
490                       RETURN VARCHAR2
491 -- Overloaded Pure Public Function which returns a value from the archive,
492 -- given the action id (ff_archive_items.context1), user entity name and up to
493 -- two additional contexts.  No validation is performed on the input
494 -- parameters.  If a matching item does not exist, null is returned.
495 -- The additional context parameters must be populated in order.
496 IS
497   l_user_entity_id  ff_user_entities.user_entity_id%type;
498   l_arch_value      ff_archive_items.value%type;
499 BEGIN
500   SELECT fue.user_entity_id
501     INTO l_user_entity_id
502     FROM ff_user_entities  fue
503    WHERE fue.user_entity_name = p_user_entity_name
504      AND fue.legislation_code= 'GB';
505   --
506   l_arch_value := pay_gb_eoy_archive.get_arch_str(p_action_id,
507                                                   l_user_entity_id,
508                                                   p_context_value1,
509                                                   p_context_value2,
510                                                   p_context_value3);
511   RETURN l_arch_value;
512 EXCEPTION
513   WHEN OTHERS THEN
514     RETURN NULL;
515 END get_arch_str;
516 --
517 FUNCTION get_arch_num(p_action_id        IN NUMBER,
518                       p_user_entity_name IN VARCHAR2,
519                       p_context_value1   IN VARCHAR2 DEFAULT NULL,
520                       p_context_value2   IN VARCHAR2 DEFAULT NULL,
521                       p_context_value3   IN VARCHAR2 DEFAULT NULL)
522                       RETURN NUMBER
523 -- Pure Public Function which returns a value from the archive
524 -- using get_arch_str, then formats it to a number
528 BEGIN
525 -- This could not be achieved by overloading get_arch_str as the views
526 -- wouldn't know which return data type was required.
527 IS
529   RETURN to_number(get_arch_str(p_action_id,
530                                 p_user_entity_name,
531                                 p_context_value1,
532                                 p_context_value2,
533                                 p_context_value3));
534 END get_arch_num;
535 --
536 FUNCTION get_arch_date(p_action_id        IN NUMBER,
537                        p_user_entity_name IN VARCHAR2,
538                        p_context_value1   IN VARCHAR2 DEFAULT NULL,
539                        p_context_value2   IN VARCHAR2 DEFAULT NULL,
540                        p_context_value3   IN VARCHAR2 DEFAULT NULL)
541                        RETURN DATE
542 -- Pure Public Function which returns a value from the archive
543 -- using get_arch_str, then formats it to a date
544 -- This could not be achieved by overloading get_arch_str as the views
545 -- wouldn't know which return data type was required.
546 IS
547 BEGIN
548   RETURN fnd_date.canonical_to_date(get_arch_str(p_action_id,
549                               p_user_entity_name,
550                               p_context_value1,
551                               p_context_value2,
552                               p_context_value3));
553 END get_arch_date;
554 --
555 FUNCTION get_parameter(p_parameter_string IN VARCHAR2,
556                        p_token            IN VARCHAR2,
557                        p_segment_number   IN NUMBER DEFAULT NULL)
558                        RETURN VARCHAR2
559 -- Pure Public Function which returns a specific legislative parameter,
560 -- given a string of parameters and a token.
561 -- Optional segment_number parameter indicates which segment of the parameter
562 -- to return where the parameter contains segments separated by colons
563 --   eg. SORT_OPTIONS=segment1:segment2:segment3
564 -- Now caters for spaces in parameter values (so can be used to retrieve
565 -- canonical dates) where the parameter is delimited with pipe chars
566 --   eg.  |START_DATE=1999/04/06 00:00:00|
567 IS
568   l_parameter  pay_payroll_actions.legislative_parameters%TYPE:=NULL;
569   l_start_pos  NUMBER;
570   l_delimiter  varchar2(1):=' ';
571 BEGIN
572   l_start_pos := instr(' '||p_parameter_string,l_delimiter||p_token||'=');
573   IF l_start_pos = 0 THEN
574     l_delimiter := '|';
575     l_start_pos := instr(' '||p_parameter_string,l_delimiter||p_token||'=');
576   end if;
577   IF l_start_pos <> 0 THEN
578     l_start_pos := l_start_pos + length(p_token||'=');
579     l_parameter := substr(p_parameter_string,
580                           l_start_pos,
581                           instr(p_parameter_string||' ',
582                           l_delimiter,l_start_pos)
583                           - l_start_pos);
584     IF p_segment_number IS NOT NULL THEN
585       l_parameter := ':'||l_parameter||':';
586       l_parameter := substr(l_parameter,
587                             instr(l_parameter,':',1,p_segment_number)+1,
588                             instr(l_parameter,':',1,p_segment_number+1) -1
589                             - instr(l_parameter,':',1,p_segment_number));
590     END IF;
591   END IF;
592   RETURN l_parameter;
593 END get_parameter;
594 --
595 -----------------------------------------------------------------------------
596 -- PROCEDURE:   cache_archive_value
597 -- DESCRIPTION: Stores all relevant payroll-level information from the
598 --              archive tables to cached plsql tables for performance.
599 --              Values are then retrieved using get_cached_value. Cannot
600 --              merge the two functions due to pragma restrict_references
601 --              WNPS violation.
602 -----------------------------------------------------------------------------
603 --
604 PROCEDURE cache_archive_value(p_payroll_action_id   IN NUMBER,
605                               p_end_yr_ueid         IN NUMBER,
606                               p_start_yr_ueid       IN NUMBER,
607                               p_tax_ref_ueid        IN NUMBER,
608                               p_tax_dist_ueid       IN NUMBER,
609                               p_permit_ueid         IN NUMBER) IS
610 --
611   l_archive_value VARCHAR2(30);
612   cursor get_payroll_id(c_payroll_action_id number,
613                         c_user_entity_id number) is
614   select to_number(faic.context) payroll_id
615   from ff_archive_items fai,
616        ff_archive_item_contexts faic
617   where fai.context1 = c_payroll_action_id
618   and   fai.user_entity_id = c_user_entity_id
619   and   fai.archive_item_id = faic.archive_item_id
620   and   faic.sequence_no = 1;
621 --
622 BEGIN
623    --
624    -- Default start/end year globals
625    g_min_start_year      := hr_general.end_of_time;
626    g_max_end_year        := hr_general.start_of_time;
627    --
628    FOR payroll_rec in get_payroll_id(p_payroll_action_id,p_permit_ueid) LOOP
629    --
630       g_payroll_end_yr_tab(payroll_rec.payroll_id) :=
631       pay_gb_eoy_archive.get_arch_str(p_action_id =>      p_payroll_action_id,
632                                       p_user_entity_id => p_end_yr_ueid,
633                                       p_context_value1 => payroll_rec.payroll_id);
634       g_payroll_start_yr_tab(payroll_rec.payroll_id) :=
635       pay_gb_eoy_archive.get_arch_str(p_action_id =>      p_payroll_action_id,
639       pay_gb_eoy_archive.get_arch_str(p_action_id =>      p_payroll_action_id,
636                                       p_user_entity_id => p_start_yr_ueid,
637                                       p_context_value1 => payroll_rec.payroll_id);
638       g_payroll_tax_ref_tab(payroll_rec.payroll_id) :=
640                                       p_user_entity_id => p_tax_ref_ueid,
641                                       p_context_value1 => payroll_rec.payroll_id);
642       g_payroll_tax_dist_tab(payroll_rec.payroll_id) :=
643       pay_gb_eoy_archive.get_arch_str(p_action_id =>      p_payroll_action_id,
644                                       p_user_entity_id => p_tax_dist_ueid,
645                                       p_context_value1 => payroll_rec.payroll_id);
646       g_payroll_permit_no_tab(payroll_rec.payroll_id) :=
647       pay_gb_eoy_archive.get_arch_str(p_action_id =>      p_payroll_action_id,
648                                       p_user_entity_id => p_permit_ueid,
649                                       p_context_value1 => payroll_rec.payroll_id);
650       g_min_start_year :=
651         least(g_min_start_year,
652               nvl(fnd_date.canonical_to_date
653                      (g_payroll_start_yr_tab(payroll_rec.payroll_id)),
654                      g_min_start_year));
655       g_max_end_year :=
656         greatest(g_max_end_year,
657               nvl(fnd_date.canonical_to_date
658                      (g_payroll_end_yr_tab(payroll_rec.payroll_id)),
659                       g_max_end_year));
660    END LOOP;
661    --
662 --
663 END cache_archive_value;
664 --
665 -----------------------------------------------------------------------------
666 -- FUNCTION:    get_cached_value
667 -- DESCRIPTION: This function returns a value
668 --              of an archive object given a payroll_action_id, payroll_id
669 --              and user_entity_id. WNDS WNPS set.
670 -----------------------------------------------------------------------------
671 --
672 FUNCTION get_cached_value(p_payroll_action_id    IN NUMBER,
673                           p_user_entity_name     IN VARCHAR2,
674                           p_payroll_id           IN NUMBER)
675 RETURN VARCHAR2 IS
676 --
677   l_return_value     varchar2(30);
678 --
679 BEGIN
680 --
681   BEGIN
682     --
683     if p_user_entity_name = 'X_END_YEAR' then
684        l_return_value := g_payroll_end_yr_tab(p_payroll_id);
685     elsif p_user_entity_name = 'X_START_YEAR' then
686        l_return_value := g_payroll_start_yr_tab(p_payroll_id);
687     elsif p_user_entity_name = 'X_TAX_REFERENCE_NUMBER' then
688        l_return_value := g_payroll_tax_ref_tab(p_payroll_id);
689     elsif p_user_entity_name = 'X_TAX_DISTRICT_REFERENCE' then
690        l_return_value := g_payroll_tax_dist_tab(p_payroll_id);
691     elsif p_user_entity_name = 'X_PERMIT_NUMBER' then
692        l_return_value := g_payroll_permit_no_tab(p_payroll_id);
693     end if;
694   --
695   EXCEPTION when others then
696   --
697   --RAISE; -- Initialisation in cache_archive_value must have failed.
698   --
699     l_return_value :=
700       pay_gb_eoy_archive.get_arch_str
701         (p_action_id        => p_payroll_action_id,
702          p_user_entity_name => p_user_entity_name,
703          p_context_value1   => to_char(p_payroll_id));
704   --
705   END;
706 --
707 RETURN l_return_value;
708 --
709 END get_cached_value;
710 --
711 -----------------------------------------------------------------------------
712 -- FUNCTION:    get_agg_active_start
713 -- DESCRIPTION: This function returns the earliest start date of the
714 --              active aggregated assignments on the same PAYE ref.
715 --              WNDS WNPS set.
716 -----------------------------------------------------------------------------
717 FUNCTION get_agg_active_start(p_asg_id   IN NUMBER,
718                               p_tax_ref  IN VARCHAR2,
719                               p_proll_eff_date IN DATE)
720 RETURN DATE IS
721    l_min_active       per_all_assignments_f.effective_start_date%TYPE;
722    l_person_id        per_all_people_f.person_id%TYPE;
723    l_pos_id           per_all_assignments_f.period_of_service_id%TYPE;
724    l_new_min_active   per_all_assignments_f.effective_start_date%TYPE;
725    l_term_and_xfer    VARCHAR2(1);
726    l_old_paye_ref     hr_soft_coding_keyflex.segment1%TYPE;
727    l_another_active_asg_xfer VARCHAR2(1);
728    --
729    -- cursor to get max effective_start date on the given PAYE Ref
730    -- for given assignment on or before the given date
731    CURSOR get_first_active_start IS
732    SELECT max(paaf.effective_start_date) first_st_date, max(person_id) person_id
733    FROM   per_all_assignments_f paaf,
734           per_assignment_status_types past,
735           pay_all_payrolls_f papf,
736           hr_soft_coding_keyflex flex
737    WHERE  paaf.assignment_id = p_asg_id
738    AND    paaf.assignment_status_type_id = past.assignment_status_type_id
739    AND    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
740    AND    paaf.payroll_id = papf.payroll_id
741    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
742    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
743    AND    flex.segment1 = p_tax_ref
744    AND    paaf.effective_start_date <= p_proll_eff_date;
745    --
746    -- cursor to find the first day of the assignment on the given paye ref
747    -- regardless of the status, this is to be used only when assignment has
748    -- never been active on the given PAYE Ref therefore get_first_active_start
752    FROM   per_all_assignments_f paaf,
749    -- will not be able to return first Active/Susp Status date
750    CURSOR get_first_start IS
751    SELECT max(paaf.effective_start_date) first_st_date, max(person_id) person_id, max(period_of_service_id) pos_id
753           pay_all_payrolls_f papf,
754           hr_soft_coding_keyflex flex
755    WHERE  paaf.assignment_id = p_asg_id
756    AND    paaf.payroll_id = papf.payroll_id
757    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
758    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
759    AND    flex.segment1 = p_tax_ref
760    AND    paaf.effective_start_date <= p_proll_eff_date;
761    --
762    -- Cursor to check whether the assignment was active on a different PAYE Ref
763    -- a day before it was transferred to another PAYE Ref
764    CURSOR is_term_and_xfer IS
765    SELECT 'Y' term_and_xfer, flex.segment1 old_paye_ref
766    FROM   per_all_assignments_f paaf,
767           per_assignment_status_types past,
768           pay_all_payrolls_f papf,
769           hr_soft_coding_keyflex flex
770    WHERE  paaf.assignment_id = p_asg_id
771    AND    paaf.assignment_status_type_id = past.assignment_status_type_id
772    AND    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
773    AND    paaf.payroll_id = papf.payroll_id
774    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
775    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
776    AND    flex.segment1 <> p_tax_ref
777    AND    l_min_active-1 BETWEEN paaf.effective_start_date
778                          AND paaf.effective_end_date;
779    --
780    -- Cursor to check if there was another assignment of the employee
781    -- transferred from/to same PAYE Refs on the same day as the given
782    -- assignment but remained active before and after the transfer
783    CURSOR is_another_active_asg_xfer IS
784    SELECT 'Y'
785    FROM   per_all_assignments_f paaf1,
786           per_assignment_status_types past1,
787           pay_all_payrolls_f papf1,
788           hr_soft_coding_keyflex flex1
789    WHERE  paaf1.period_of_service_id = l_pos_id
790    AND    paaf1.assignment_id <> p_asg_id
791    AND    l_min_active BETWEEN paaf1.effective_start_date
792                        AND paaf1.effective_end_date
793    AND    paaf1.assignment_status_type_id = past1.assignment_status_type_id
794    AND    past1.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
795    AND    paaf1.payroll_id = papf1.payroll_id
796    AND    p_proll_eff_date BETWEEN papf1.effective_start_date and papf1.effective_end_date
797    AND    papf1.soft_coding_keyflex_id = flex1.soft_coding_keyflex_id
798    AND    flex1.segment1 = p_tax_ref
799    AND    EXISTS ( SELECT 1
800                    FROM   per_all_assignments_f paaf2,
801                           per_assignment_status_types past2,
802                           pay_all_payrolls_f papf2,
803                           hr_soft_coding_keyflex flex2
804                   WHERE   paaf2.assignment_id = paaf1.assignment_id
805                   AND     l_min_active-1 BETWEEN paaf2.effective_start_date
806                                          AND paaf2.effective_end_date
807                   AND    paaf2.assignment_status_type_id = past2.assignment_status_type_id
808                   AND    past2.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
809                   AND    paaf2.payroll_id = papf2.payroll_id
810                   AND    p_proll_eff_date BETWEEN papf2.effective_start_date and papf2.effective_end_date
811                   AND    papf2.soft_coding_keyflex_id = flex2.soft_coding_keyflex_id
812                   AND    flex2.segment1 = l_old_paye_ref);
813    --
814    -- cursor to get first effective_start_date across all active or suspended
815    -- assignments of the person on a given tax ref as at a given date
816    CURSOR get_agg_min_start_date IS
817    SELECT min(paaf.effective_start_date) min_active
818    FROM   per_all_assignments_f paaf,
819           per_assignment_status_types past,
820           pay_all_payrolls_f papf,
821           hr_soft_coding_keyflex flex
822    WHERE  paaf.person_id = l_person_id
823    AND    (l_min_active-1) BETWEEN paaf.effective_start_date AND paaf.effective_end_date
824    AND    paaf.assignment_status_type_id = past.assignment_status_type_id
825    AND    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
826    AND    paaf.payroll_id = papf.payroll_id
827    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
828    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
829    AND    flex.segment1 = p_tax_ref;
830    --
831    l_proc   VARCHAR2(100) := 'pay_gb_eoy_archive.get_agg_active_start';
832 BEGIN
833    -- Get first active or suspended status start date for the assignment
834    OPEN  get_first_active_start;
835    FETCH get_first_active_start INTO l_min_active, l_person_id;
836    -- Bug 5909829: If assignment has never been active on this PAYE Ref
837    -- on or before the p_effective_date then check if it has been
838    -- transferred from another PAYE Ref along with at least on another
839    -- PAYE Ref
840    IF l_min_active IS NULL and l_person_id IS NULL THEN
841       -- The assignment has never been active on the given
842       -- PAYE Ref before the given date therefore
843       -- get first day on the PAYE Ref regardless of the status
844       -- on or before the given date
845       OPEN get_first_start;
846       FETCH get_first_start INTO l_min_active, l_person_id, l_pos_id;
847       IF get_first_start%FOUND THEN
848          -- Check if this assignment has been terminated and
852          OPEN is_term_and_xfer;
849          -- transferred to the current PAYE Ref on the same day
850          -- i.e., it is active on another PAYE Ref a day before
851          -- transfer
853          FETCH is_term_and_xfer INTO l_term_and_xfer, l_old_paye_ref;
854          IF is_term_and_xfer%FOUND THEN
855             -- check whether there is another active assignment
856             -- of the employee transferred along with the given
857             -- assignment on the same day
858             OPEN is_another_active_asg_xfer;
859             FETCH is_another_active_asg_xfer INTO l_another_active_asg_xfer;
860             IF is_another_active_asg_xfer%FOUND THEN
861                -- given assignment is transferred and terminated on the
862                -- same day but another assignment of the employee remained
863                -- active with same transfer (from/to PAYE Refs)
864                -- and on the same day therefore it is
865                -- continuation of same employment hence continue to find
866                -- start of this continuous active period of employment
867                l_min_active := l_min_active;
868             ELSE
869                -- given assignment is transferred and terminated on the
870                -- same day but there is NO other assignment of the employee
871                -- that remained active with same transfer (from/to PAYE Refs)
872                -- on the same day therefore it is NOT
873                -- continuation of same employment
874                l_min_active := NULL;
875             END IF;
876          END IF; -- is_term_and_xfer
877          CLOSE is_term_and_xfer;
878       END IF; -- get_first_start
879       CLOSE get_first_start;
880    END IF; -- l_min_active and l_person_id are null
881    --
882 
883    CLOSE get_first_active_start;
884    --
885    IF l_min_active IS NULL THEN
886       --modified format for bug fix 4991467
887       RETURN fnd_date.canonical_to_date('0001/01/01 00:00:00');
888    END IF;
889    -- check if any of the other assignments of this employee
890    -- that were active before this assignment
891    LOOP
892       l_new_min_active := NULL;
893       --
894       OPEN  get_agg_min_start_date;
895       FETCH get_agg_min_start_date INTO l_new_min_active;
896       CLOSE get_agg_min_start_date;
897       --
898       IF l_new_min_active IS NOT NULL THEN
899          -- new earlier start date found, continue to loop
900          -- through to look for earlier active start date amongst aggregated
901          -- assignments
902          l_min_active := l_new_min_active;
903       ELSE
904          -- there is no earlier active start date amongst aggregated asgs
905          -- hence return earliest active start date found so far
906          RETURN l_min_active;
907       END IF;
908    END LOOP;
909 END get_agg_active_start;
910 --
911 -----------------------------------------------------------------------------
912 -- FUNCTION:    get_agg_active_end
913 -- DESCRIPTION: This function returns the earliest start date of the
914 --              active aggregated assignments on the same PAYE ref.
915 --              WNDS WNPS set.
916 -----------------------------------------------------------------------------
917 FUNCTION get_agg_active_end(p_asg_id    IN NUMBER,
918                               p_tax_ref IN VARCHAR2,
919                               p_proll_eff_date IN DATE)
920 RETURN DATE IS
921    l_min_active       per_all_assignments_f.effective_start_date%TYPE;
922    l_max_active       per_all_assignments_f.effective_start_date%TYPE;
923    l_person_id        per_all_people_f.person_id%TYPE;
924    l_pos_id           per_all_assignments_f.period_of_service_id%TYPE;
925    l_new_max_active   per_all_assignments_f.effective_start_date%TYPE;
926    l_term_and_xfer    VARCHAR2(1);
927    l_old_paye_ref     hr_soft_coding_keyflex.segment1%TYPE;
928    l_another_active_asg_xfer VARCHAR2(1);
929    --
930    -- cursor to get max effective_end_date on the given PAYE Ref
931    -- for given assignment
932    CURSOR get_last_active_end IS
933    SELECT max(paaf.effective_end_date) last_end_date, max(person_id) person_id
934    FROM   per_all_assignments_f paaf,
935           per_assignment_status_types past,
936           pay_all_payrolls_f papf,
937           hr_soft_coding_keyflex flex
938    WHERE  paaf.assignment_id = p_asg_id
939    AND    paaf.assignment_status_type_id = past.assignment_status_type_id
940    AND    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
941    AND    paaf.payroll_id = papf.payroll_id
942    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
943    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
944    AND    flex.segment1 = p_tax_ref
945    AND    paaf.effective_start_date <= p_proll_eff_date;
946    --
947    -- cursor to find the first day of the assignment on the given paye ref
948    -- regardless of the status, this is to be used only when assignment has
949    -- never been active on the given PAYE Ref therefore get_first_active_start
950    -- will not be able to return first Active/Susp Status date
951    CURSOR get_first_start IS
952    SELECT max(paaf.effective_start_date) first_st_date, max(person_id) person_id, max(period_of_service_id) pos_id
953    FROM   per_all_assignments_f paaf,
954           pay_all_payrolls_f papf,
955           hr_soft_coding_keyflex flex
956    WHERE  paaf.assignment_id = p_asg_id
957    AND    paaf.payroll_id = papf.payroll_id
961    AND    paaf.effective_start_date <= p_proll_eff_date;
958    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
959    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
960    AND    flex.segment1 = p_tax_ref
962    --
963    -- Cursor to check whether the assignment was active on a different PAYE Ref
964    -- a day before it was transferred to another PAYE Ref
965    CURSOR is_term_and_xfer IS
966    SELECT 'Y' term_and_xfer, flex.segment1 old_paye_ref
967    FROM   per_all_assignments_f paaf,
968           per_assignment_status_types past,
969           pay_all_payrolls_f papf,
970           hr_soft_coding_keyflex flex
971    WHERE  paaf.assignment_id = p_asg_id
972    AND    paaf.assignment_status_type_id = past.assignment_status_type_id
973    AND    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
974    AND    paaf.payroll_id = papf.payroll_id
975    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
976    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
977    AND    flex.segment1 <> p_tax_ref
978    AND    l_min_active-1 BETWEEN paaf.effective_start_date
979                          AND paaf.effective_end_date;
980    --
981    -- Cursor to check if there was another assignment of the employee
982    -- transferred from/to same PAYE Refs on the same day as the given
983    -- assignment but remained active before and after the transfer
984    CURSOR is_another_active_asg_xfer IS
985    SELECT 'Y'
986    FROM   per_all_assignments_f paaf1,
987           per_assignment_status_types past1,
988           pay_all_payrolls_f papf1,
989           hr_soft_coding_keyflex flex1
990    WHERE  paaf1.period_of_service_id = l_pos_id
991    AND    paaf1.assignment_id <> p_asg_id
992    AND    l_min_active BETWEEN paaf1.effective_start_date
993                        AND paaf1.effective_end_date
994    AND    paaf1.assignment_status_type_id = past1.assignment_status_type_id
995    AND    past1.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
996    AND    paaf1.payroll_id = papf1.payroll_id
997    AND    p_proll_eff_date BETWEEN papf1.effective_start_date and papf1.effective_end_date
998    AND    papf1.soft_coding_keyflex_id = flex1.soft_coding_keyflex_id
999    AND    flex1.segment1 = p_tax_ref
1000    AND    EXISTS ( SELECT 1
1001                    FROM   per_all_assignments_f paaf2,
1002                           per_assignment_status_types past2,
1003                           pay_all_payrolls_f papf2,
1004                           hr_soft_coding_keyflex flex2
1005                   WHERE   paaf2.assignment_id = paaf1.assignment_id
1006                   AND     l_min_active-1 BETWEEN paaf2.effective_start_date
1007                                          AND paaf2.effective_end_date
1008                   AND    paaf2.assignment_status_type_id = past2.assignment_status_type_id
1009                   AND    past2.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
1010                   AND    paaf2.payroll_id = papf2.payroll_id
1011                   AND    p_proll_eff_date BETWEEN papf2.effective_start_date and papf2.effective_end_date
1012                   AND    papf2.soft_coding_keyflex_id = flex2.soft_coding_keyflex_id
1013                   AND    flex2.segment1 = l_old_paye_ref);
1014    --
1015    -- cursor to get latest effective_end_date across all active or suspended
1016    -- assignments of the person on a given tax ref as at a given date
1017    CURSOR get_agg_max_end_date IS
1018    SELECT max(paaf.effective_end_date) max_active
1019    FROM   per_all_assignments_f paaf,
1020           per_assignment_status_types past,
1021           pay_all_payrolls_f papf,
1022           hr_soft_coding_keyflex flex
1023    WHERE  paaf.person_id = l_person_id
1024    AND    (l_max_active+1) BETWEEN paaf.effective_start_date AND paaf.effective_end_date
1025    AND    paaf.assignment_status_type_id = past.assignment_status_type_id
1026    AND    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
1027    AND    paaf.payroll_id = papf.payroll_id
1028    AND    p_proll_eff_date BETWEEN papf.effective_start_date and papf.effective_end_date
1029    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
1030    AND    flex.segment1 = p_tax_ref;
1031    --
1032    l_proc   VARCHAR2(100) := 'pay_gb_eoy_archive.get_agg_active_end';
1033 BEGIN
1034    -- Get first active or suspended status start date for the assignment
1035    OPEN  get_last_active_end;
1036    FETCH get_last_active_end INTO l_max_active, l_person_id;
1037    -- Bug 5909829: If assignment has never been active on this PAYE Ref
1038    -- on or before the p_effective_date then check if it has been
1039    -- transferred from another PAYE Ref along with at least on another
1040    -- PAYE Ref
1041    IF l_max_active IS NULL and l_person_id IS NULL THEN
1042       -- The assignment has never been active on the given
1043       -- PAYE Ref before the given date therefore
1044       -- get first day on the PAYE Ref regardless of the status
1045       -- on or before the given date
1046       OPEN get_first_start;
1047       FETCH get_first_start INTO l_min_active, l_person_id, l_pos_id;
1048       IF get_first_start%FOUND THEN
1049          -- Check if this assignment has been terminated and
1050          -- transferred to the current PAYE Ref on the same day
1051          -- i.e., it is active on another PAYE Ref a day before
1052          -- transfer
1053          OPEN is_term_and_xfer;
1054          FETCH is_term_and_xfer INTO l_term_and_xfer, l_old_paye_ref;
1055          IF is_term_and_xfer%FOUND THEN
1056             -- check whether there is another active assignment
1060             FETCH is_another_active_asg_xfer INTO l_another_active_asg_xfer;
1057             -- of the employee transferred along with the given
1058             -- assignment on the same day
1059             OPEN is_another_active_asg_xfer;
1061             IF is_another_active_asg_xfer%FOUND THEN
1062                -- given assignment is transferred and terminated on the
1063                -- same day but another assignment of the employee remained
1064                -- active with same transfer (from/to PAYE Refs)
1065                -- and on the same day therefore it is
1066                -- continuation of same employment hence continue to find
1067                -- end of this continuous active period of employment
1068                l_max_active := l_min_active;
1069             ELSE
1070                -- given assignment is transferred and terminated on the
1071                -- same day but there is NO other assignment of the employee
1072                -- that remained active with same transfer (from/to PAYE Refs)
1073                -- on the same day therefore it is NOT
1074                -- continuation of same employment
1075                l_max_active := NULL;
1076             END IF;
1077          END IF; -- is_term_and_xfer
1078          CLOSE is_term_and_xfer;
1079       END IF; -- get_first_start
1080       CLOSE get_first_start;
1081    END IF; -- l_max_active and l_person_id are null
1082    --
1083    CLOSE get_last_active_end;
1084    --
1085    IF l_max_active IS NULL THEN
1086       --modified format for bug fix 4991467
1087       RETURN fnd_date.canonical_to_date('4712/12/31 00:00:00');
1088    END IF;
1089    --
1090    -- check if any of the other assignments of
1091    -- this employee that were active after this assignment
1092    LOOP
1093       l_new_max_active := NULL;
1094       --
1095       OPEN  get_agg_max_end_date;
1096       FETCH get_agg_max_end_date INTO l_new_max_active;
1097       CLOSE get_agg_max_end_date;
1098       --
1099       IF l_new_max_active IS NOT NULL THEN
1100          -- new latest end date found, continue to loop
1101          -- through to look for later active end date amongst aggregated
1102          -- assignments
1103          l_max_active := l_new_max_active;
1104       ELSE
1105          -- there is no later active end date amongst aggregated asgs
1106          -- hence return latest active ebd date found so far
1107          RETURN l_max_active;
1108       END IF;
1109    END LOOP;
1110 END get_agg_active_end;
1111 --
1112 ------------------------------- PROCEDURES ---------------------------------
1113 --
1114 PROCEDURE write_output_header IS
1115 BEGIN
1116    --
1117    hr_utility.set_location('pay_gb_eoy_archive.write_output_header', 10);
1118    fnd_file.put_line(fnd_file.output, rpad(' ', 41)||
1119           'End Of Year Process - Errors and Warnings Report'||
1120           rpad(' ', 30)||fnd_date.date_to_displaydate(sysdate));
1121    fnd_file.put_line(fnd_file.output, ' ');
1122    fnd_file.put_line(fnd_file.output, rpad(' ', 20)||
1123           'Request Id: '||fnd_global.conc_request_id);
1124    fnd_file.put_line(fnd_file.output, ' ');
1125    fnd_file.put_line(fnd_file.output,  rpad('Assignment', 14) || ' ' ||
1126           rpad(' ', 30) || ' ' || rpad('Error or', 10) || ' ' ||
1127           rpad(' ', 75));
1128    fnd_file.put_line(fnd_file.output,  rpad('Number', 14) || ' ' ||
1129           rpad('Full Name', 30) || ' ' || rpad('Warning', 10) || ' ' ||
1130           rpad('Message', 75));
1131    fnd_file.put_line(fnd_file.output,  rpad('-', 14, '-') || ' ' ||
1132           rpad('-', 30, '-') || ' ' || rpad('-', 10, '-') || ' ' ||
1133           rpad('-', 75, '-'));
1134    g_output_header := FALSE;
1135    hr_utility.set_location('pay_gb_eoy_archive.write_output_header', 100);
1136 END write_output_header;
1137 --
1138 FUNCTION write_output(p_assignment_number IN VARCHAR2,
1139                        p_full_name IN VARCHAR2,
1140                        p_message_type IN VARCHAR2,
1141                        p_message IN VARCHAR2) RETURN NUMBER IS
1142 --
1143    l_err_warn VARCHAR2(10);
1144    l_message  VARCHAR2(250);
1145 BEGIN
1146    --
1147    hr_utility.set_location('pay_gb_eoy_archive.write_output', 1);
1148    hr_utility.trace('p_assignment_number='||p_assignment_number);
1149    hr_utility.trace('p_full_name='||p_full_name);
1150    hr_utility.trace('p_message_type='||p_message_type);
1151    hr_utility.trace('p_message='||p_message);
1152    --
1153    -- strip ':' from the error message
1154    l_message := ltrim(p_message, ':');
1155    --
1156    IF g_output_header THEN
1157       write_output_header;
1158    END IF;
1159    hr_utility.set_location('pay_gb_eoy_archive.write_output', 30);
1160    IF p_message_type = 'E' THEN
1161       l_err_warn := 'Error';
1162       fnd_file.put_line(fnd_file.log, 'An error encountered when processing assignment '||p_assignment_number||', please check output file for more details.');
1163       g_err_count := nvl(g_err_count, 0) + 1;
1164    ELSE
1165       l_err_warn := 'Warning';
1166       g_warn_count := nvl(g_warn_count, 0) + 1;
1167    END IF;
1168    --
1169    hr_utility.set_location('pay_gb_eoy_archive.write_output', 40);
1170    fnd_file.put_line(fnd_file.output, rpad(p_assignment_number, 14)||' '||
1171           rpad(p_full_name, 30)||' '||rpad(l_err_warn,10)||' '||
1172           rpad(l_message,75));
1173    --
1174    hr_utility.set_location('pay_gb_eoy_archive.write_output', 50);
1175    IF length(l_message) > 75 THEN
1176       fnd_file.put_line(fnd_file.output, rpad(' ', 57)||
1180    hr_utility.set_location('pay_gb_eoy_archive.write_output', 100);
1177                           rpad(substr(l_message,76),75));
1178    END IF;
1179    --
1181    return 0;
1182 END write_output;
1183 --
1184 FUNCTION write_output_footer RETURN NUMBER IS
1185 BEGIN
1186    hr_utility.set_location('pay_gb_eoy_archive.write_output_header', 1);
1187    --
1188    IF g_output_header THEN
1189       write_output_header;
1190    END IF;
1191    --
1192    hr_utility.set_location('pay_gb_eoy_archive.write_output_header', 10);
1193    fnd_file.put_line(fnd_file.output, ' ');
1194    fnd_file.put_line(fnd_file.output, rpad(' ', 20)||
1195           'Total Number of Errors   = '||nvl(g_err_count,0));
1196    fnd_file.put_line(fnd_file.output, rpad(' ', 20)||
1197           'Total Number of Warnings = '||nvl(g_warn_count,0));
1198    hr_utility.set_location('pay_gb_eoy_archive.write_output_header', 100);
1199    return 0;
1200 END write_output_footer;
1201 --
1202 PROCEDURE range_cursor (pactid IN NUMBER,
1203                         sqlstr OUT NOCOPY VARCHAR2)
1204 -- public procedure which archives the payroll information, then returns a
1205 -- varchar2 defining a SQL Statement to select all the people that may be
1206 -- eligible for Year End reporting.
1207 -- The archiver uses this cursor to split the people into chunks for parallel
1208 -- processing.
1209 IS
1210   --
1211   l_proc             CONSTANT VARCHAR2(32):= g_package||'range_cursor';
1212   -- vars for constructing the sqlstr
1213   l_range_cursor              VARCHAR2(4000):= NULL;
1214   l_parameter_match           VARCHAR2(500) := NULL;
1215   -- vars for constructing an error message:
1216   l_payroll_action_message    VARCHAR(240);
1217   -- vars for holding SRS Parameters:
1218   l_start_year                DATE;
1219   l_end_year                  DATE;
1220   l_business_group_id         hr_organization_units.business_group_id%TYPE;
1221   l_permit_number             VARCHAR2(12);
1222   l_tax_district_reference    VARCHAR2(3);  -- error check will ensure numeric
1223   l_tax_reference_number      VARCHAR2(10); -- 4011263: length 10 chars
1224   l_test_indicator            varchar2(1);  -- 5909829 EOY to store test indicator value
1225   l_unique_test_id            varchar2(50); -- 5909829 EOY to store unique test id value
1226   -- vars for returns from the API:
1227   l_archive_item_id           ff_archive_items.archive_item_id%TYPE;
1228   l_ovn                       NUMBER;
1229   l_some_warning              BOOLEAN;
1230   -- vars for holding payroll data:
1231   l_payroll_start_year        DATE;
1232   l_payroll_end_year          DATE;
1233   l_payroll_period_type       VARCHAR2(30);
1234   l_payroll_max_period_number NUMBER;
1235   l_dummy                     NUMBER;
1236   -- User Entity IDs
1237   l_payroll_id_eid                 ff_user_entities.user_entity_id%TYPE;
1238   l_permit_number_eid              ff_user_entities.user_entity_id%TYPE;
1239   l_payroll_name_eid               ff_user_entities.user_entity_id%TYPE;
1240   l_tax_district_reference_eid     ff_user_entities.user_entity_id%TYPE;
1241   l_tax_reference_eid              ff_user_entities.user_entity_id%TYPE;
1242   l_tax_district_name_eid          ff_user_entities.user_entity_id%TYPE;
1243   l_employers_name_eid             ff_user_entities.user_entity_id%TYPE;
1244   l_employers_address_line_eid     ff_user_entities.user_entity_id%TYPE;
1245   l_econ_eid                       ff_user_entities.user_entity_id%TYPE;
1246 
1247 /* Start 4011263
1248   l_smp_recovered_eid              ff_user_entities.user_entity_id%TYPE;
1249   l_sap_recovered_eid              ff_user_entities.user_entity_id%TYPE;
1250   l_spp_recovered_eid              ff_user_entities.user_entity_id%TYPE;
1251   l_ssp_recovered_eid              ff_user_entities.user_entity_id%TYPE;
1252   l_smp_compensation_eid           ff_user_entities.user_entity_id%TYPE;
1253   l_sap_compensation_eid           ff_user_entities.user_entity_id%TYPE;
1254   l_spp_compensation_eid           ff_user_entities.user_entity_id%TYPE;
1255    End 4011263 */
1256 
1257   l_payroll_start_year_eid         ff_user_entities.user_entity_id%TYPE;
1258   l_payroll_end_year_eid           ff_user_entities.user_entity_id%TYPE;
1259   l_payroll_period_type_eid        ff_user_entities.user_entity_id%TYPE;
1260   l_max_period_number_eid          ff_user_entities.user_entity_id%TYPE;
1261   l_payroll_action_message_eid     ff_user_entities.user_entity_id%TYPE;
1262   -- exceptions
1263   tax_dist_ref_error    EXCEPTION; -- raised when l_tax_district_reference
1264                                    -- has incorrect format
1265   inconsis_ref_error    EXCEPTION; -- raised when a payroll has more than
1266                                    -- PAYE Ref in the tax year
1267   test_indicator_error  EXCEPTION; -- raised when Test indicaor is Yes
1268 			           -- and no Unique Test ID BUG 5909829 EOY
1269   --
1270   -- Start of BUG 5909829 EOY Changed the cursor to fetch test indicator and
1271   -- unique test id values
1272   --
1273   -- Start of BUG 5671777-5
1274   -- Changed start date of the EOY process to reflect start of the current tax year
1275   -- so need to add 12 months to the start date.
1276   --
1277   cursor csr_parameter_info(p_pact_id NUMBER) IS
1278   SELECT
1279      to_date('06/04/'||to_char(start_date,'YYYY'),'dd/mm/yyyy')
1280   -- add_months(to_date('06/04/'||to_char(start_date,'YYYY'),'dd/mm/yyyy'),12)
1281   -- End of BUG 5671777-5
1282      start_year,
1283     effective_date end_year,
1284     business_group_id,
1285     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
1286                                             'PERMIT'),1,12) permit,
1290     legislative_parameters,'TAX_REF'),4,11),'/'),1,10) tax_ref,  -- 4011263: tax ref can be 10 chars long
1287     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
1288                                             'TAX_REF'),1,3) tax_dist,
1289     substr(ltrim(substr(pay_gb_eoy_archive.get_parameter(
1291     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
1292                                             'TEST'),1,1) test_indicator,
1293     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
1294                                             'UNIQUE_TEST_ID'),1,8) unique_test_id
1295   FROM  pay_payroll_actions
1296   WHERE payroll_action_id = p_pact_id;
1297   --
1298   -- End of BUG 5909829 EOY
1299   --
1300   cursor csr_payrolls(p_bg_id NUMBER, p_end_year DATE) IS
1301   -- dont pick up null permits
1302   SELECT
1303     p.payroll_id                         payroll_id,
1304     substr(flex.segment10,1,12)          permit_number,
1305     p.payroll_name                       payroll_name,
1306     substr(flex.segment1,1,3)            tax_district_reference,
1307     substr(ltrim(substr(org_information1,4,11),'/') ,1,10)  tax_reference,
1308     flex.segment1                        emp_paye_ref,
1309     substr(org.org_information2 ,1,40)   tax_district_name,
1310     substr(ltrim(org.org_information3),1,36)    employers_name, -- 4011263: added ltrim
1311     substr(ltrim(org.org_information4),1,60)    employers_address_line, -- 4011263: added ltrim
1312     substr(nvl(flex.segment14,org.org_information7),1,9)    econ
1313 /* Start 4011263
1314     ,
1315     flex.segment11 * 100                 smp_recovered,
1316     flex.segment12 * 100                 smp_compensation,
1317     flex.segment13 * 100                 ssp_recovered,
1318     flex.segment15 * 100                 sap_recovered,
1319     flex.segment16 * 100                 sap_compensation,
1320     flex.segment17 * 100                 spp_recovered,
1321     flex.segment18 * 100                 spp_compensation
1322    End 4011263 */
1323   FROM  pay_all_payrolls_f p,
1324     hr_soft_coding_keyflex flex,
1325     hr_organization_information org
1326   WHERE p.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
1327     AND org.org_information_context = 'Tax Details References'
1328     AND org.org_information1 = flex.segment1
1329     AND NVL(org.org_information10,'UK') = 'UK'
1330     AND flex.segment10 IS NOT NULL
1331     AND p.business_group_id = p_bg_id
1332     AND org.organization_id = p_bg_id
1333     AND p_end_year BETWEEN p.effective_start_date
1334                        AND p.effective_end_date;
1335   --
1336   l_payroll_name pay_all_payrolls_f.payroll_name%TYPE;
1337   --
1338   -- cursor to find a different PAYE Ref within a tax year on a given payroll
1339   CURSOR csr_another_paye_ref(p_payroll_id NUMBER,
1340                               p_end_year DATE,
1341                               p_paye_ref VARCHAR2) IS
1342   SELECT flex.segment1
1343   FROM   pay_all_payrolls_f p,
1344          hr_soft_coding_keyflex flex
1345   WHERE  p.soft_coding_keyflex_id = flex.soft_coding_keyflex_id(+)
1346   AND    p.payroll_id = p_payroll_id
1347   AND    p.effective_start_date <= hr_gbbal.span_end(p_end_year)
1348   AND    p.effective_end_date >= hr_gbbal.span_start(p_end_year)
1349   AND    nvl(flex.segment1, 'XYZ') <> nvl(p_paye_ref, 'ABC');
1350   --
1351   l_another_paye_ref hr_soft_coding_keyflex.segment1%TYPE;
1352   --
1353   cursor csr_payroll_year (p_payroll_id NUMBER,
1354                              p_start_year DATE,
1355                              p_end_year   DATE) IS
1356   SELECT
1357     min(start_date)  start_year,
1358     max(end_date)    end_year,
1359     max(period_type) period_type,
1360     max(period_num)  max_period_number
1361   FROM  per_time_periods ptp
1362   WHERE ptp.payroll_id = p_payroll_id
1363     AND ptp.regular_payment_date BETWEEN p_start_year
1364                                      AND p_end_year;
1365   --
1366   cursor csr_user_entity(p_entity_name VARCHAR2) IS
1367   SELECT user_entity_id
1368     FROM   ff_user_entities
1369    WHERE  user_entity_name = p_entity_name
1370      AND  legislation_code = 'GB'
1371      AND  business_group_id IS NULL;
1372   --
1373   --
1374   PROCEDURE setup_entity_ids IS
1375   --
1376   BEGIN
1377 /* Start 4011263
1378     OPEN csr_user_entity('X_SSP_RECOVERED');
1379     FETCH csr_user_entity INTO l_ssp_recovered_eid;
1380     CLOSE csr_user_entity;
1381 
1382     OPEN csr_user_entity('X_SMP_COMPENSATION');
1383     FETCH csr_user_entity INTO l_smp_compensation_eid;
1384     CLOSE csr_user_entity;
1385     OPEN csr_user_entity('X_SMP_RECOVERED');
1386     FETCH csr_user_entity INTO l_smp_recovered_eid;
1387     CLOSE csr_user_entity;
1388 
1389     OPEN csr_user_entity('X_SAP_COMPENSATION');
1390     FETCH csr_user_entity INTO l_sap_compensation_eid;
1391     CLOSE csr_user_entity;
1392     OPEN csr_user_entity('X_SAP_RECOVERED');
1393     FETCH csr_user_entity INTO l_sap_recovered_eid;
1394     CLOSE csr_user_entity;
1395 
1396 
1397     OPEN csr_user_entity('X_SPP_COMPENSATION');
1398     FETCH csr_user_entity INTO l_spp_compensation_eid;
1399     CLOSE csr_user_entity;
1400     OPEN csr_user_entity('X_SPP_RECOVERED');
1401     FETCH csr_user_entity INTO l_spp_recovered_eid;
1402     CLOSE csr_user_entity;
1403    End 4011263 */
1404 
1405     OPEN csr_user_entity('X_ECON');
1406     FETCH csr_user_entity INTO l_econ_eid;
1407     CLOSE csr_user_entity;
1408     OPEN csr_user_entity('X_EMPLOYERS_ADDRESS_LINE');
1412     FETCH csr_user_entity INTO l_employers_name_eid;
1409     FETCH csr_user_entity INTO l_employers_address_line_eid;
1410     CLOSE csr_user_entity;
1411     OPEN csr_user_entity('X_EMPLOYERS_NAME');
1413     CLOSE csr_user_entity;
1414     OPEN csr_user_entity('X_TAX_DISTRICT_NAME');
1415     FETCH csr_user_entity INTO l_tax_district_name_eid;
1416     CLOSE csr_user_entity;
1417     OPEN csr_user_entity('X_TAX_REFERENCE_NUMBER');
1418     FETCH csr_user_entity INTO l_tax_reference_eid;
1419     CLOSE csr_user_entity;
1420     OPEN csr_user_entity('X_TAX_DISTRICT_REFERENCE');
1421     FETCH csr_user_entity INTO l_tax_district_reference_eid;
1422     CLOSE csr_user_entity;
1423     OPEN csr_user_entity('X_PAYROLL_NAME');
1424     FETCH csr_user_entity INTO l_payroll_name_eid;
1425     CLOSE csr_user_entity;
1426     OPEN csr_user_entity('X_PERMIT_NUMBER');
1427     FETCH csr_user_entity INTO l_permit_number_eid;
1428     CLOSE csr_user_entity;
1429     OPEN csr_user_entity('X_MAX_PERIOD_NUMBER');
1430     FETCH csr_user_entity INTO l_max_period_number_eid;
1431     CLOSE csr_user_entity;
1432     OPEN csr_user_entity('X_PERIOD_TYPE');
1433     FETCH csr_user_entity INTO l_payroll_period_type_eid;
1434     CLOSE csr_user_entity;
1435     OPEN csr_user_entity('X_END_YEAR');
1436     FETCH csr_user_entity INTO l_payroll_end_year_eid;
1437     CLOSE csr_user_entity;
1438     OPEN csr_user_entity('X_START_YEAR');
1439     FETCH csr_user_entity INTO l_payroll_start_year_eid;
1440     CLOSE csr_user_entity;
1441     OPEN csr_user_entity('X_PAYROLL_ACTION_MESSAGE');
1442     FETCH csr_user_entity INTO l_payroll_action_message_eid;
1443     CLOSE csr_user_entity;
1444   END setup_entity_ids;
1445   --
1446   PROCEDURE archive_payroll_info(p_payroll_action_id NUMBER,
1447                                  p_payroll_id NUMBER,
1448                                  p_user_entity_id NUMBER,
1449                                  p_value VARCHAR2) IS
1450   BEGIN
1451     ff_archive_api.create_archive_item
1452       (p_archive_item_id  => l_archive_item_id,
1453        p_user_entity_id   => p_user_entity_id,
1454        p_archive_value    => p_value,
1455        p_archive_type     => 'PA',
1456        p_action_id        => p_payroll_action_id,
1457        p_legislation_code => 'GB',
1458        p_object_version_number => l_ovn,
1459        p_context_name1    => 'PAYROLL_ID',
1460        p_context1         => p_payroll_id,
1461        p_some_warning     => l_some_warning);
1462   END archive_payroll_info;
1463   --
1464 BEGIN
1465   BEGIN
1466     hr_utility.set_location('Entering: '||l_proc,1);
1467     --
1468     setup_entity_ids;
1469     --
1470     -- Find payroll action parameters
1471     --
1472     --Start BUG 5909829 EOY
1473     OPEN csr_parameter_info(pactid);
1474     FETCH csr_parameter_info INTO l_start_year,
1475                                   l_end_year,
1476                                   l_business_group_id,
1477                                   l_permit_number,
1478                                   l_tax_district_reference,
1479                                   l_tax_reference_number,
1480                                   l_test_indicator,
1481                                   l_unique_test_id;
1482 
1483     CLOSE csr_parameter_info;
1484 
1485 
1486     -- Unique Test ID is mandatory if EDI Test indicator is Yes
1487 
1488     IF (l_test_indicator = 'Y' AND l_unique_test_id IS NULL) THEN
1489         fnd_file.put_line (fnd_file.LOG,'You must provide a Unique Test ID if the EDI Test Indicator is Yes.');
1490          RAISE test_indicator_error;
1491     END IF;
1492 
1493     -- End BUG 5909829 EOY
1494 
1495     BEGIN -- ensure tax district reference is numeric (if supplied)
1496       IF to_number(l_tax_district_reference) < 0 THEN
1497         RAISE value_error;
1498       END IF;
1499     EXCEPTION
1500       WHEN value_error THEN
1501         RAISE tax_dist_ref_error;
1502     END;
1503     hr_utility.set_location(l_proc,10);
1504     --
1505     l_payroll_name := NULL;
1506     -- Extract Payroll info
1507     FOR rec_payroll IN csr_payrolls(l_business_group_id,
1508                                     l_end_year)
1509     LOOP
1510       hr_utility.set_location(l_proc||' '||rec_payroll.payroll_name,20);
1511       l_payroll_name := rec_payroll.payroll_name;
1512       -- find payroll year info
1513       OPEN csr_payroll_year (rec_payroll.payroll_id,l_start_year,l_end_year);
1514       FETCH csr_payroll_year INTO   l_payroll_start_year,
1515                                     l_payroll_end_year,
1516                                     l_payroll_period_type,
1517                                     l_payroll_max_period_number;
1518       CLOSE csr_payroll_year;
1519       --
1520       l_another_paye_ref := NULL;
1521       OPEN csr_another_paye_ref(rec_payroll.payroll_id,
1522                                 l_end_year,
1523                                 rec_payroll.emp_paye_ref);
1524       FETCH csr_another_paye_ref INTO l_another_paye_ref;
1525       IF csr_another_paye_ref%FOUND THEN
1526          hr_utility.trace('After csr_another_paye_ref, l_another_paye_ref='||
1527                              l_another_paye_ref);
1528          CLOSE csr_another_paye_ref;
1529          RAISE inconsis_ref_error;
1530       ELSE
1531          CLOSE csr_another_paye_ref;
1532          hr_utility.trace('No other paye ref found on the payroll.');
1533       END IF;
1534       --
1535       -- Call API to archive Data via cover procedure
1539       archive_payroll_info(pactid, rec_payroll.payroll_id,
1536 /* Start 4011263
1537       archive_payroll_info(pactid, rec_payroll.payroll_id,
1538                            l_ssp_recovered_eid,rec_payroll.ssp_recovered);
1540                            l_smp_compensation_eid,
1541                            rec_payroll.smp_compensation);
1542       archive_payroll_info(pactid, rec_payroll.payroll_id,
1543                            l_smp_recovered_eid,
1544                            rec_payroll.smp_recovered);
1545 --
1546       archive_payroll_info(pactid, rec_payroll.payroll_id,
1547                            l_sap_compensation_eid,
1548                            rec_payroll.sap_compensation);
1549       archive_payroll_info(pactid, rec_payroll.payroll_id,
1550                            l_sap_recovered_eid,
1551                            rec_payroll.sap_recovered);
1552       archive_payroll_info(pactid, rec_payroll.payroll_id,
1553                            l_spp_compensation_eid,
1554                            rec_payroll.spp_compensation);
1555       archive_payroll_info(pactid, rec_payroll.payroll_id,
1556                            l_spp_recovered_eid,
1557                            rec_payroll.spp_recovered);
1558    End 4011263 */
1559 --
1560 
1561       archive_payroll_info(pactid, rec_payroll.payroll_id,
1562                            l_econ_eid,rec_payroll.econ);
1563       archive_payroll_info(pactid, rec_payroll.payroll_id,
1564                            l_employers_address_line_eid,
1565                            rec_payroll.employers_address_line);
1566       archive_payroll_info(pactid, rec_payroll.payroll_id,
1567                            l_employers_name_eid,
1568                            rec_payroll.employers_name);
1569       archive_payroll_info(pactid, rec_payroll.payroll_id,
1570                            l_tax_district_name_eid,
1571                            rec_payroll.tax_district_name);
1572       archive_payroll_info(pactid, rec_payroll.payroll_id,
1573                            l_tax_reference_eid,
1574                            rec_payroll.tax_reference);
1575       archive_payroll_info(pactid, rec_payroll.payroll_id,
1576                            l_tax_district_reference_eid,
1577                            rec_payroll.tax_district_reference);
1578       archive_payroll_info(pactid, rec_payroll.payroll_id,
1579                            l_payroll_name_eid,
1580                            rec_payroll.payroll_name);
1581       archive_payroll_info(pactid, rec_payroll.payroll_id,
1582                            l_permit_number_eid,
1583                            rec_payroll.permit_number);
1584       archive_payroll_info(pactid, rec_payroll.payroll_id,
1585                            l_max_period_number_eid,
1586                            l_payroll_max_period_number);
1587       archive_payroll_info(pactid, rec_payroll.payroll_id,
1588                            l_payroll_period_type_eid,
1589                            l_payroll_period_type);
1590       archive_payroll_info(pactid, rec_payroll.payroll_id,
1591                            l_payroll_end_year_eid,
1592                            fnd_date.date_to_canonical(l_payroll_end_year));
1593       archive_payroll_info(pactid, rec_payroll.payroll_id,
1594                            l_payroll_start_year_eid,
1595                            fnd_date.date_to_canonical(l_payroll_start_year));
1596     END LOOP;
1597   EXCEPTION
1598     -- Propagate error through by means of the X_PAYROLL_ACTION_MESSAGE UE.
1599     -- Start of BUG 5909829 EOY
1600     --
1601     WHEN test_indicator_error THEN
1602       l_payroll_action_message :=
1603         'You must provide a Unique Test ID if the EDI Test Indicator is Yes.';
1604       ff_archive_api.create_archive_item
1605         (p_archive_item_id  => l_archive_item_id,
1606          p_user_entity_id   => l_payroll_action_message_eid,
1607          p_archive_value    => l_payroll_action_message,
1608          p_archive_type     => 'PA',
1609          p_action_id        => pactid,
1610          p_legislation_code => 'GB',
1611          p_object_version_number => l_ovn,
1612          p_some_warning     => l_some_warning);
1613      RAISE;  -- reraise the error
1614      --
1615      -- End of BUG 5909829 EOY
1616      --
1617     WHEN tax_dist_ref_error THEN
1618       l_payroll_action_message :=
1619         'Invalid Format for Tax District Reference: Must be three numerics';
1620       ff_archive_api.create_archive_item
1621         (p_archive_item_id  => l_archive_item_id,
1622          p_user_entity_id   => l_payroll_action_message_eid,
1623          p_archive_value    => l_payroll_action_message,
1624          p_archive_type     => 'PA',
1625          p_action_id        => pactid,
1626          p_legislation_code => 'GB',
1627          p_object_version_number => l_ovn,
1628          p_some_warning     => l_some_warning);
1629       RAISE;  -- reraise the error
1630     --
1631     WHEN inconsis_ref_error THEN
1632       l_payroll_action_message :=
1633         'More than one PAYE Ref found on payroll '||l_payroll_name||
1634         ' in the tax year.';
1635       ff_archive_api.create_archive_item
1636         (p_archive_item_id  => l_archive_item_id,
1637          p_user_entity_id   => l_payroll_action_message_eid,
1638          p_archive_value    => l_payroll_action_message,
1639          p_archive_type     => 'PA',
1640          p_action_id        => pactid,
1641          p_legislation_code => 'GB',
1642          p_object_version_number => l_ovn,
1643          p_some_warning     => l_some_warning);
1644       --
1645       l_dummy := write_output(p_assignment_number => NULL,
1649 
1646                        p_full_name => NULL,
1647                        p_message_type => 'E',
1648                        p_message => l_payroll_action_message);
1650       RAISE;  -- reraise the error
1651     --
1652     WHEN OTHERS THEN
1653       -- Write to the conc logfile, and try to archive err msg.
1654       fnd_file.put_line(fnd_file.log, substr(sqlerrm(sqlcode),1,80));
1655       l_payroll_action_message := substr('Payroll Extract failed with: '||
1656                                          sqlerrm(sqlcode),1,240);
1657       ff_archive_api.create_archive_item
1658         (p_archive_item_id  => l_archive_item_id,
1659          p_user_entity_id   => l_payroll_action_message_eid,
1660          p_archive_value    => l_payroll_action_message,
1661          p_archive_type     => 'PA',
1662          p_action_id        => pactid,
1663          p_legislation_code => 'GB',
1664          p_object_version_number => l_ovn,
1665          p_some_warning     => l_some_warning);
1666       RAISE;  -- reraise the error
1667       --
1668   END; -- Payroll Extract
1669   --
1670   -- return range cursor
1671   --
1672   -- select all people in the BG, filter out non appropriate ones in
1673   -- action_creation procedure.
1674   hr_utility.set_location(l_proc,30);
1675   -- sqlstr must contain one and only one entry of :payroll_action_id
1676   -- it must be ordered by person_id
1677   --
1678   sqlstr := 'SELECT DISTINCT person_id
1679     FROM  per_all_people_f ppf,
1680           pay_payroll_actions ppa
1681     WHERE ppa.payroll_action_id = :payroll_action_id
1682     AND   ppa.business_group_id +0= ppf.business_group_id
1683     ORDER BY ppf.person_id';
1684   hr_utility.set_location('Leaving:  '||l_proc,40);
1685 EXCEPTION
1686   WHEN OTHERS THEN
1687     hr_utility.set_location(' Leaving: '||l_proc,50);
1688     fnd_file.put_line(fnd_file.log,
1689         substr('Error in rangecode '||sqlerrm(sqlcode),1,80));
1690     -- Return cursor that selects no rows
1691     sqlstr := 'select 1 from dual where to_char(:payroll_action_id) = dummy';
1692 END range_cursor;
1693 --
1694 --
1695 PROCEDURE action_creation(pactid IN NUMBER,
1696                           stperson IN NUMBER,
1697                           endperson IN NUMBER,
1698                           chunk IN NUMBER) IS
1699   --
1700   l_proc             CONSTANT VARCHAR2(35):= g_package||'action_creation';
1701   --
1702   l_actid                  pay_assignment_actions.assignment_action_id%TYPE;
1703   -- vars for returns from the API:
1704   l_archive_item_id           ff_archive_items.archive_item_id%TYPE;
1705   l_ovn                       NUMBER;
1706   l_some_warning              BOOLEAN;
1707   --
1708   l_start_year_date        DATE;
1709   --
1710   l_payroll_start_date      DATE;
1711   l_payroll_end_date        DATE;
1712   l_process_asg             BOOLEAN;
1713   --
1714   cursor csr_parameter_info(p_payroll_action_id NUMBER) IS
1715   SELECT
1716     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
1717                                             'PERMIT'),1,12) permit,
1718     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
1719                                             'TAX_REF'),1,3) tax_dist,
1720     substr(ltrim(substr(pay_gb_eoy_archive.get_parameter(
1721         legislative_parameters,'TAX_REF'),4,11),'/'),1,10) tax_ref, --4011263
1722     effective_date end_year,
1723     business_group_id,
1724     ltrim(substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
1725                                         'ASG_SET'),1,80)) asg_set
1726   FROM  pay_payroll_actions
1727   WHERE payroll_action_id = p_payroll_action_id;
1728   --
1729   cursor csr_user_entity(p_entity_name VARCHAR2) IS
1730   SELECT user_entity_id
1731     FROM   ff_user_entities
1732    WHERE  user_entity_name = p_entity_name
1733      AND  legislation_code = 'GB'
1734      AND  business_group_id IS NULL;
1735   --
1736   cursor csr_context_id(c_context_name in varchar2) is
1737   select context_id
1738   from ff_contexts
1739   where context_name = c_context_name;
1740   --
1741   cursor csr_sub_asg ( p_asg_rowid              varchar2,
1742                       p_start_date             date,
1743                       p_end_date               date,
1744                       pactid                   number,
1745                       p_start_year_eid         number,
1746                       p_payroll_end_year_eid   number
1747                      ) is
1748   SELECT 1 valid_asg
1749   FROM   per_all_assignments_f paf
1750   WHERE  paf.rowid = chartorowid(p_asg_rowid)
1751   AND    paf.effective_end_date >= p_start_date
1752   AND    paf.effective_start_date <= p_end_date
1753   AND NOT EXISTS (select 1
1754      from  per_all_assignments_f paf2
1755      where paf2.assignment_id = paf.assignment_id
1756        AND    paf2.effective_end_date > paf.effective_end_date
1757        AND    paf2.effective_end_date >=
1758                       fnd_date.canonical_to_date(pay_gb_eoy_archive.get_arch_str
1759                       (pactid, p_start_year_eid,to_char(paf2.payroll_id)))
1760        AND    paf2.effective_start_date <=
1761                  fnd_date.canonical_to_date(pay_gb_eoy_archive.get_arch_str
1762                       (pactid, p_payroll_end_year_eid, to_char(paf2.payroll_id))));
1763 --
1764   cursor csr_assignments ( p_min_start_year_date        DATE,
1765                           p_max_end_year_date          DATE,
1766                           p_end_date               DATE,
1770                           p_tax_ref                VARCHAR2,
1767                           p_bg_id                  NUMBER,
1768                           p_permit                 VARCHAR2,
1769                           p_tax_dist_ref           VARCHAR2,
1771                           p_start_year_eid         NUMBER,
1772                           p_end_year_eid   NUMBER,
1773                           p_asg_set_id     NUMBER
1774                          ) IS
1775   -- select all the assignments for a particular permit
1776   -- note we only want the last date effective row - the permit on the
1777   -- payroll for this dictates where it is reported even if the assignment
1778   -- has been on more than one payroll in the year. The exception to this
1779   -- is where tax district/reference transfers have occurred
1780   -- find the latest assignment row this payroll year
1781   -- add any assignment rows that are for tax reference changes
1782   -- pick up latest effective end date and latest payroll
1783   -- don't pick up null permits (such payroll would not have been archived)
1784   -- and if ni y is not reportable only pick up
1785   -- current year assignments
1786   -- after transfer
1787   -- Select using less stringent criteria then validate the
1788   -- rows before archiving. Performance issue with functions in where clause.
1789   -- Add Ordered Index Hints for CBO issue.
1790   -- added p_asg_set_id and usage of hr_assignment_sets (and _amendments) tables
1791   SELECT /*+ ORDERED INDEX (asg PER_ASSIGNMENTS_F_N12,
1792                             ppf PAY_PAYROLLS_F_PK,
1793                             flex HR_SOFT_CODING_KEYFLEX_PK,
1794                             org HR_ORGANIZATION_INFORMATIO_FK1,
1795                             per PER_PEOPLE_F_PK)
1796              USE_NL(asg,ppf,flex,org,per) */
1797     asg.assignment_id,
1798     asg.effective_start_date,
1799     asg.effective_end_date,
1800     asg.person_id,
1801     asg.period_of_service_id, -- added for bug 3784871
1802     pay_gb_eoy_archive.get_agg_active_start(asg.assignment_id, flex.segment1, p_end_date) agg_active_start,
1803     pay_gb_eoy_archive.get_agg_active_end(asg.assignment_id, flex.segment1, p_end_date) agg_active_end,
1804     asg.payroll_id,
1805     substr(ltrim(substr(org_information1,4,11),'/') ,1,10) tax_ref, -- 4011263
1806     decode(per.per_information9,'Y','Y',NULL) multiple_asg_flag,
1807     rowidtochar(asg.ROWID) charrowid,
1808     'N' tax_ref_xfer
1809   FROM  per_all_assignments_f       asg,
1810         pay_all_payrolls_f              ppf,
1811         hr_soft_coding_keyflex      flex,
1812         hr_organization_information org,
1813         per_all_people_f            per
1814   WHERE asg.person_id BETWEEN stperson AND endperson
1815     AND asg.business_group_id +0 = p_bg_id
1816     AND asg.effective_end_date >= p_min_start_year_date
1817     AND asg.effective_start_date <= p_max_end_year_date
1818     AND asg.payroll_id = ppf.payroll_id
1819     AND asg.period_of_service_id is not null
1820     AND p_end_date BETWEEN ppf.effective_start_date
1821                        AND ppf.effective_end_date
1822     AND ppf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
1823     AND org.organization_id +0 = p_bg_id
1824     AND org.org_information_context =
1825                  'Tax Details References'||decode(flex.segment1,'','','')
1826     AND org.org_information1 = flex.segment1
1827     AND nvl(org.org_information10,'UK') = 'UK'
1828     AND nvl(p_permit,substr(flex.segment10,1,12)) =
1829                                       substr(flex.segment10,1,12)
1830     AND nvl(p_tax_dist_ref, substr(flex.segment1,1,3)) =
1831                                       substr(flex.segment1,1,3)
1832     AND nvl(p_tax_ref, substr(ltrim(substr(org_information1,4,11),'/') ,1,10))
1833                    = substr(ltrim(substr(org_information1,4,11),'/') ,1,10)
1834     AND per.person_id = asg.person_id
1835     AND p_end_date BETWEEN per.effective_start_date
1836                        AND per.effective_end_date
1837     AND (p_asg_set_id IS NULL -- don't check for assignment set in this case
1838          OR EXISTS (SELECT 1 FROM hr_assignment_sets has1
1839                     WHERE has1.assignment_set_id = p_asg_set_id
1840                     AND has1.business_group_id = asg.business_group_id
1841                     AND nvl(has1.payroll_id, asg.payroll_id) = asg.payroll_id
1842                     AND (NOT EXISTS (SELECT 1 -- chk no amendmts
1843                                      FROM hr_assignment_set_amendments hasa1
1844                                      WHERE hasa1.assignment_set_id =
1845                                                has1.assignment_set_id)
1846                          OR EXISTS (SELECT 1 -- chk include amendmts
1847                                     FROM hr_assignment_set_amendments hasa2
1848                                     WHERE hasa2.assignment_set_id =
1849                                                has1.assignment_set_id
1850                                     AND hasa2.assignment_id = asg.assignment_id
1851                                     AND nvl(hasa2.include_or_exclude,'I') = 'I')
1852                          OR (NOT EXISTS (SELECT 1 --chk no exlude amendmts
1853                                     FROM hr_assignment_set_amendments hasa3
1854                                     WHERE hasa3.assignment_set_id =
1855                                                has1.assignment_set_id
1856                                     AND hasa3.assignment_id = asg.assignment_id
1857                                     AND nvl(hasa3.include_or_exclude,'I') = 'E')
1858                              AND NOT EXISTS (SELECT 1 --and chk no Inc amendmts
1859                                     FROM hr_assignment_set_amendments hasa4
1863                          ) -- done checking amendments
1860                                     WHERE hasa4.assignment_set_id =
1861                                                has1.assignment_set_id
1862                                     AND nvl(hasa4.include_or_exclude,'I') = 'I')                             ) -- end checking exclude amendmts
1864                     ) -- done asg set check when not null
1865            ) -- end of asg set check
1866   UNION
1867   SELECT /*+ ORDERED INDEX (PASS PER_ASSIGNMENTS_F_N12,
1868                             ASS PER_ASSIGNMENTS_F_PK,
1869                             NROLL PAY_PAYROLLS_F_PK,
1870                             FLEX HR_SOFT_CODING_KEYFLEX_PK,
1871                             PROLL PAY_PAYROLLS_F_PK,
1872                             pflex HR_SOFT_CODING_KEYFLEX_PK,
1873                             per PER_PEOPLE_F_PK)
1874              USE_NL(PASS,ASS,NROLL,FLEX,PROLL,pflex,per) */
1875     pass.assignment_id,
1876     pass.effective_start_date,
1877     pass.effective_end_date,
1878     pass.person_id,
1879     pass.period_of_service_id, -- added for bug 3784871
1880     pay_gb_eoy_archive.get_agg_active_start(pass.assignment_id, pflex.segment1, p_end_date) agg_active_start,
1881     pay_gb_eoy_archive.get_agg_active_end(pass.assignment_id, pflex.segment1, p_end_date) agg_active_end,
1882     pass.payroll_id,
1883     substr(ltrim(substr(pflex.segment1,4,11),'/') ,1,10) tax_ref, -- 4011263
1884     decode(per.per_information9,'Y','Y',NULL) multiple_asg_flag,
1885     rowidtochar(pass.rowid) charrowid,
1886     'Y' tax_ref_xfer
1887   FROM
1888            per_all_people_f  per
1889           ,per_all_assignments_f      PASS
1890           ,per_all_assignments_f  ASS
1891           ,pay_all_payrolls_f         NROLL
1892           ,hr_soft_coding_keyflex FLEX
1893           ,pay_all_payrolls_f         PROLL
1894           ,hr_soft_coding_keyflex pflex
1895   WHERE  NROLL.payroll_id = ASS.payroll_id
1896   AND    ASS.effective_start_date between
1897                   NROLL.effective_start_date and NROLL.effective_end_date
1898   AND    NROLL.soft_coding_keyflex_id = FLEX.soft_coding_keyflex_id
1899   AND    ASS.assignment_id = PASS.assignment_id
1900   AND    ASS.period_of_service_id is not null
1901   AND    PASS.effective_end_date = (ASS.effective_start_date - 1)
1902   AND    PROLL.payroll_id = PASS.payroll_id
1903   AND    PER.person_id BETWEEN stperson AND endperson
1904   AND    pass.business_group_id +0 = p_bg_id
1905   AND    pass.effective_end_date >= p_min_start_year_date
1906   AND    pass.effective_start_date <= p_max_end_year_date
1907   AND    ASS.effective_start_date between
1908                   PROLL.effective_start_date AND PROLL.effective_end_date
1909   AND    PROLL.soft_coding_keyflex_id = PFLEX.soft_coding_keyflex_id
1910   AND    ASS.payroll_id <> PASS.payroll_id
1911   AND    FLEX.segment1 <> PFLEX.segment1
1912   AND    nvl(p_permit,substr(pflex.segment10,1,12)) =
1913                                       substr(pflex.segment10,1,12)
1914   AND    nvl(p_tax_dist_ref, substr(pflex.segment1,1,3)) =
1915                                       substr(pflex.segment1,1,3)
1916   AND    nvl(p_tax_ref, substr(ltrim(substr(pflex.segment1,4,11),'/') ,1,10))
1917                    = substr(ltrim(substr(pflex.segment1,4,11),'/') ,1,10)
1918   AND    per.person_id = pass.person_id
1919   AND    p_end_date  BETWEEN per.effective_start_date
1920                          AND per.effective_end_date
1921     AND (p_asg_set_id IS NULL -- don't check for assignment set in this case
1922          OR EXISTS (SELECT 1 FROM hr_assignment_sets has1
1923                     WHERE has1.assignment_set_id = p_asg_set_id
1924                     AND has1.business_group_id = pass.business_group_id
1925                     AND nvl(has1.payroll_id, pass.payroll_id) = pass.payroll_id
1926                     AND (NOT EXISTS (SELECT 1 -- chk no amendmts
1927                                      FROM hr_assignment_set_amendments hasa1
1928                                      WHERE hasa1.assignment_set_id =
1929                                                has1.assignment_set_id)
1930                          OR EXISTS (SELECT 1 -- chk include amendmts
1931                                     FROM hr_assignment_set_amendments hasa2
1932                                     WHERE hasa2.assignment_set_id =
1933                                                has1.assignment_set_id
1934                                     AND hasa2.assignment_id = pass.assignment_id
1935                                     AND nvl(hasa2.include_or_exclude,'I') = 'I')
1936                          OR (NOT EXISTS (SELECT 1 --chk no exlude amendmts
1937                                     FROM hr_assignment_set_amendments hasa3
1938                                     WHERE hasa3.assignment_set_id =
1939                                                has1.assignment_set_id
1940                                     AND hasa3.assignment_id = pass.assignment_id
1941                                     AND nvl(hasa3.include_or_exclude,'I') = 'E')
1942                              AND NOT EXISTS (SELECT 1 --and chk no Inc amendmts
1943                                     FROM hr_assignment_set_amendments hasa4
1944                                     WHERE hasa4.assignment_set_id =
1945                                                has1.assignment_set_id
1946                                     AND nvl(hasa4.include_or_exclude,'I') = 'I')
1947                              ) -- end checking exclude amendmts
1948                          ) -- done checking amendments
1949                     ) -- done asg set check when not null
1950            ) -- end of asg set check
1951   ORDER BY 4,5,6,7,8,1,3 desc;
1952   --
1953   rec_assignment csr_assignments%ROWTYPE;
1957   IF chunk = 1 THEN
1954   rec_prev_asg   csr_assignments%ROWTYPE;
1955   --
1956 BEGIN
1958     NULL;
1959     --hr_utility.trace_on(NULL,'EOY_CHUNK1');
1960   END IF;
1961   --hr_utility.trace_on(NULL,'RMEOYAC');
1962   hr_utility.set_location('Entering: '||l_proc,1);
1963   -- Setup info and ids if new session.
1964   -- DO NOT set g_payroll_action_id here as archinit() may stop working
1965   IF g_context_id IS NULL THEN
1966     OPEN csr_parameter_info(pactid);
1967     FETCH csr_parameter_info INTO g_permit_number,
1968                                   g_tax_district_reference,
1969                                   g_tax_reference_number,
1970                                   g_end_year,
1971                                   g_business_group_id,
1972                                   g_asg_set_id;
1973     CLOSE csr_parameter_info;
1974     --
1975     OPEN csr_user_entity('X_START_YEAR');
1976     FETCH csr_user_entity INTO g_payroll_start_year_eid;
1977     CLOSE csr_user_entity;
1978     OPEN csr_user_entity('X_END_YEAR');
1979     FETCH csr_user_entity INTO g_payroll_end_year_eid;
1980     CLOSE csr_user_entity;
1981     OPEN csr_user_entity('X_PERMIT_NUMBER');
1982     FETCH csr_user_entity INTO g_permit_number_eid;
1983     CLOSE csr_user_entity;
1984     OPEN csr_user_entity('X_TAX_DISTRICT_REFERENCE');
1985     FETCH csr_user_entity INTO g_tax_dist_ref_eid;
1986     CLOSE csr_user_entity;
1987     OPEN csr_user_entity('X_TAX_REFERENCE_NUMBER');
1988     FETCH csr_user_entity INTO g_tax_ref_eid;
1989     CLOSE csr_user_entity;
1990     OPEN csr_user_entity('X_LAST_MULTI_ASG_PER_PERSON_TAX_REF');
1991     FETCH csr_user_entity INTO g_last_multi_asg_eid;
1992     CLOSE csr_user_entity;
1993     OPEN csr_user_entity('X_EFFECTIVE_END_DATE');
1994     FETCH csr_user_entity INTO g_effective_end_date_eid;
1995     CLOSE csr_user_entity;
1996     OPEN csr_user_entity('X_TAX_REF_TRANSFER');
1997     FETCH csr_user_entity INTO g_tax_ref_transfer_eid;
1998     CLOSE csr_user_entity;
1999     OPEN csr_user_entity('X_AGG_ACTIVE_START');
2000     FETCH csr_user_entity INTO g_agg_active_start_eid;
2001     CLOSE csr_user_entity;
2002     OPEN csr_user_entity('X_AGG_ACTIVE_END');
2003     FETCH csr_user_entity INTO g_agg_active_end_eid;
2004     CLOSE csr_user_entity;
2005     --
2006     OPEN  csr_context_id('PAYROLL_ID');
2007     FETCH csr_context_id INTO g_context_id;
2008     CLOSE csr_context_id;
2009     --
2010     hr_utility.set_location(l_proc,10);
2011     --
2012     cache_archive_value(p_payroll_action_id => pactid,
2013                         p_end_yr_ueid       => g_payroll_end_year_eid,
2014                         p_start_yr_ueid     => g_payroll_start_year_eid,
2015                         p_tax_ref_ueid      => g_tax_ref_eid,
2016                         p_tax_dist_ueid     => g_tax_dist_ref_eid,
2017                         p_permit_ueid       => g_permit_number_eid);
2018     --
2019   END IF; -- g_context_id IS NULL
2020   hr_utility.set_location(l_proc,13);
2021   --
2022   -- set asg backup details to unlikely values before loop.
2023   rec_prev_asg.assignment_id      := hr_api.g_number;
2024   rec_prev_asg.effective_end_date := hr_api.g_date;
2025   -- loop through assignments
2026   OPEN csr_assignments ( g_min_start_year,
2027                         g_max_end_year,
2028                         g_end_year,
2029                         g_business_group_id,
2030                         g_permit_number,
2031                         g_tax_district_reference,
2032                         g_tax_reference_number,
2033                         g_payroll_start_year_eid,
2034                         g_payroll_end_year_eid,
2035                         g_asg_set_id);
2036   LOOP
2037     FETCH csr_assignments INTO rec_assignment;
2038     hr_utility.trace(l_proc||' Fetched assignment_id='||
2039          rec_assignment.assignment_id||', g_asg_set_id='||g_asg_set_id);
2040     l_process_asg := FALSE;
2041     l_payroll_start_date :=
2042          fnd_date.canonical_to_date(pay_gb_eoy_archive.get_cached_value
2043                               (pactid,'X_START_YEAR',
2044                                to_char(rec_assignment.payroll_id)));
2045     hr_utility.trace(l_proc||' l_payroll_start_date='||fnd_date.date_to_displaydate(l_payroll_start_date));
2046     l_payroll_end_date :=
2047          fnd_date.canonical_to_date(pay_gb_eoy_archive.get_cached_value
2048                           (pactid,'X_END_YEAR',
2049                            to_char(rec_assignment.payroll_id)));
2050     hr_utility.trace(l_proc||' l_payroll_end_date='||fnd_date.date_to_displaydate(l_payroll_end_date));
2051     hr_utility.trace(l_proc||' tax_ref_xfer='||rec_assignment.tax_ref_xfer);
2052     IF nvl(rec_assignment.tax_ref_xfer,' ') = 'N' AND csr_assignments%FOUND
2053     THEN
2054       hr_utility.trace(l_proc||' opening cursor csr_sub_asg.');
2055       for asgrec in csr_sub_asg( rec_assignment.charrowid,
2056                                 l_payroll_start_date,
2057                                 l_payroll_end_date,
2058                                 pactid,
2059                                 g_payroll_start_year_eid,
2060                                 g_payroll_end_year_eid
2061                                 ) loop
2062         hr_utility.trace(l_proc||' In the loop for cursor csr_sub_asg.');
2063         l_process_asg := TRUE;
2064       end loop;
2065     ELSIF nvl(rec_assignment.tax_ref_xfer,' ') = 'Y' AND csr_assignments%FOUND
2066     THEN
2067       hr_utility.trace(l_proc||' tax ref xfer=Y and found record by csr_assignments cursor.');
2071         AND rec_assignment.effective_start_date <= l_payroll_end_date
2068       hr_utility.trace(l_proc||' rec_assignment.effective_end_date='||fnd_date.date_to_displaydate(rec_assignment.effective_end_date));
2069       hr_utility.trace(l_proc||' rec_assignment.effective_start_date='||fnd_date.date_to_displaydate(rec_assignment.effective_start_date));
2070       IF rec_assignment.effective_end_date >= l_payroll_start_date
2072       THEN
2073         l_process_asg := TRUE;
2074       END IF;
2075     ELSE
2076       -- will come here if csr_assignments%NOTFOUND
2077       l_process_asg := TRUE;
2078     end if;
2079     hr_utility.trace(l_proc||' rec_assignment.person_id='||rec_assignment.person_id);
2080     hr_utility.trace(l_proc||' rec_prev_asg.person_id='||rec_prev_asg.person_id);
2081     hr_utility.trace(l_proc||' rec_assignment.period_of_service_id='||rec_assignment.period_of_service_id);
2082     hr_utility.trace(l_proc||' rec_prev_asg.period_of_service_id='||rec_prev_asg.period_of_service_id);
2083     hr_utility.trace(l_proc||' rec_assignment.agg_active_start='||fnd_date.date_to_displaydate(rec_assignment.agg_active_start));
2084     hr_utility.trace(l_proc||' rec_prev_asg.agg_active_start='||fnd_date.date_to_displaydate(rec_prev_asg.agg_active_start));
2085     hr_utility.trace(l_proc||' rec_assignment.agg_active_end='||fnd_date.date_to_displaydate(rec_assignment.agg_active_end));
2086     hr_utility.trace(l_proc||' rec_prev_asg.agg_active_end='||fnd_date.date_to_displaydate(rec_prev_asg.agg_active_end));
2087     hr_utility.trace(l_proc||' rec_assignment.tax_ref='||rec_assignment.tax_ref);
2088     hr_utility.trace(l_proc||' rec_prev_asg.tax_ref='||rec_prev_asg.tax_ref);
2089     IF (csr_assignments%NOTFOUND
2090         OR rec_assignment.person_id <> rec_prev_asg.person_id
2091         OR rec_assignment.period_of_service_id <> rec_prev_asg.period_of_service_id -- Added to fix bug 3784871
2092         OR rec_assignment.agg_active_start <> rec_prev_asg.agg_active_start
2093         OR rec_assignment.agg_active_end <> rec_prev_asg.agg_active_end
2094         OR rec_assignment.tax_ref   <> rec_prev_asg.tax_ref)
2095       AND csr_assignments%rowcount > 0
2096       AND rec_prev_asg.multiple_asg_flag = 'Y'
2097       AND l_process_asg
2098       -- If the person or Tax ref has changed or the last row has been
2099       -- fetched, and the last action created was for a multi-asg person
2100       -- and this
2101     THEN
2102       -- archive the X_LAST_MULTI_ASG_PER_PERSON_TAX_REF DBI against
2103       -- the last action created to indicate that it was the last asg
2104       -- for that person/tax ref group.
2105       -- first row will not come here due to null trap with
2106       -- rec_prev_asg.multiple_asg_flag
2107       ff_archive_api.create_archive_item
2108         (p_archive_item_id  => l_archive_item_id,
2109          p_user_entity_id   => g_last_multi_asg_eid,
2110          p_archive_value    => 'Y',
2111          p_archive_type     => 'AAC',
2112          p_action_id        => l_actid,
2113          p_legislation_code => 'GB',
2114          p_object_version_number => l_ovn,
2115          p_some_warning     => l_some_warning);
2116     END IF;
2117     EXIT WHEN csr_assignments%NOTFOUND;
2118     hr_utility.set_location(l_proc,15);
2119     hr_utility.trace(l_proc||' rec_assignment.assignment_id='||rec_assignment.assignment_id);
2120     hr_utility.trace(l_proc||' rec_prev_asg.assignment_id='||rec_prev_asg.assignment_id);
2121     hr_utility.trace(l_proc||' rec_assignment.effective_end_date='||fnd_date.date_to_displaydate(rec_assignment.effective_end_date));
2122     hr_utility.trace(l_proc||' rec_prev_asg.effective_end_date='||fnd_date.date_to_displaydate(rec_prev_asg.effective_end_date));
2123     IF (rec_assignment.assignment_id <> rec_prev_asg.assignment_id
2124       OR rec_assignment.effective_end_date <> rec_prev_asg.effective_end_date)
2125       AND l_process_asg
2126       -- if the current row is the first row
2127       -- or is not the same as the previous one
2128       --  (ignoring tax_ref_xfer) as the 2nd part of union may bring back
2129       -- duplicates
2130     THEN
2131       hr_utility.set_location(l_proc,20);
2132       -- insert an action
2133       SELECT pay_assignment_actions_s.nextval
2134         INTO l_actid
2135         FROM dual;
2136       --
2137       hr_nonrun_asact.insact(l_actid,rec_assignment.assignment_id,
2138                              pactid,chunk,NULL);
2139       -- archive the effective end date
2140       ff_archive_api.create_archive_item
2141         (p_archive_item_id  => l_archive_item_id,
2142          p_user_entity_id   => g_effective_end_date_eid,
2143          p_archive_value    => fnd_date.date_to_canonical
2144                                 (rec_assignment.effective_end_date),
2145          p_archive_type     => 'AAC',
2146          p_action_id        => l_actid,
2147          p_legislation_code => 'GB',
2148          p_object_version_number => l_ovn,
2149          p_some_warning     => l_some_warning);
2150       -- If tax_ref_xfer is Y, archive the X_TAX_REF_TRANSFER DBI
2151       IF rec_assignment.tax_ref_xfer = 'Y' THEN
2152         ff_archive_api.create_archive_item
2153           (p_archive_item_id  => l_archive_item_id,
2154            p_user_entity_id   => g_tax_ref_transfer_eid,
2155            p_archive_value    => 'Y',
2156            p_archive_type     => 'AAC',
2157            p_action_id        => l_actid,
2158            p_legislation_code => 'GB',
2159            p_object_version_number => l_ovn,
2160            p_some_warning     => l_some_warning);
2161       END IF;
2162       -- If multiple assignments aggregated then archive first and last active
2163       -- or suspended status start and end dates amongst aggregated asgs
2167            p_user_entity_id   => g_agg_active_start_eid,
2164       IF rec_assignment.multiple_asg_flag = 'Y' THEN
2165         ff_archive_api.create_archive_item
2166           (p_archive_item_id  => l_archive_item_id,
2168            p_archive_value    => fnd_date.date_to_canonical(rec_assignment.agg_active_start),
2169            p_archive_type     => 'AAC',
2170            p_action_id        => l_actid,
2171            p_legislation_code => 'GB',
2172            p_object_version_number => l_ovn,
2173            p_some_warning     => l_some_warning);
2174            --
2175         ff_archive_api.create_archive_item
2176           (p_archive_item_id  => l_archive_item_id,
2177            p_user_entity_id   => g_agg_active_end_eid,
2178            p_archive_value    => fnd_date.date_to_canonical(rec_assignment.agg_active_end),
2179            p_archive_type     => 'AAC',
2180            p_action_id        => l_actid,
2181            p_legislation_code => 'GB',
2182            p_object_version_number => l_ovn,
2183            p_some_warning     => l_some_warning);
2184            --
2185       END IF;
2186       -- Backup the current row.
2187       rec_prev_asg := rec_assignment;
2188     END IF; -- not duplicate
2189   END LOOP;
2190   CLOSE csr_assignments;
2191 --  hr_utility.trace_off;
2192   hr_utility.set_location('Leaving:  '||l_proc,40);
2193 END action_creation;
2194 --
2195 --
2196 PROCEDURE archinit(p_payroll_action_id IN NUMBER) IS
2197   --
2198   l_proc             CONSTANT VARCHAR2(35):= g_package||'archinit';
2199   --
2200   l_payroll_start_year        DATE;
2201   l_payroll_end_year          DATE;
2202   l_payroll_period_type       VARCHAR2(30);
2203   l_payroll_max_period_number NUMBER;
2204   l_payroll_tax_ref           VARCHAR2(10);
2205   l_payroll_tax_dist          VARCHAR2(10);
2206   l_payroll_id            pay_all_payrolls_f.payroll_id%TYPE;
2207   l_number_per_fiscal_yr      NUMBER;
2208   --
2209   -- get the defined balance id for specified balance and dimension
2210   cursor get_defined_balance_id
2211     (p_balance_name VARCHAR2, p_dimension_name VARCHAR2) IS
2212   SELECT defined_balance_id
2213     FROM pay_defined_balances db,
2214          pay_balance_types    b,
2215          pay_balance_dimensions d
2216     WHERE b.balance_name = p_balance_name
2217     AND   d.dimension_name = p_dimension_name
2218     AND   db.balance_type_id = b.balance_type_id
2219     AND   db.balance_dimension_id = d.balance_dimension_id;
2220   --
2221   -- Start of BUG 5671777-5
2222   -- Changed start date of the EOY process to reflect start of the current tax year
2223   -- so need to add 12 months to the start date.
2224   --
2225   cursor csr_parameter_info(p_payroll_action_id NUMBER) IS
2226   SELECT
2227      to_date('06/04/'||to_char(start_date,'YYYY'),'dd/mm/yyyy')
2228   -- add_months(to_date('06/04/'||to_char(start_date,'YYYY'),'dd/mm/yyyy'),12)
2229   -- End of BUG 5671777-5
2230          start_year,
2231     effective_date end_year,
2232     business_group_id,
2233     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
2234                                             'PERMIT'),1,12) permit,
2235     substr(pay_gb_eoy_archive.get_parameter(legislative_parameters,
2236                                             'TAX_REF'),1,3) tax_dist,
2237     substr(ltrim(substr(pay_gb_eoy_archive.get_parameter(
2238         legislative_parameters,'TAX_REF'),4,11),'/'),1,10) tax_ref --4011263
2239   FROM  pay_payroll_actions
2240   WHERE payroll_action_id = p_payroll_action_id;
2241   --
2242   cursor csr_user_entity(p_entity_name VARCHAR2) IS
2243   SELECT user_entity_id
2244     FROM   ff_user_entities
2245    WHERE  user_entity_name = p_entity_name
2246      AND  legislation_code = 'GB'
2247      AND  business_group_id IS NULL;
2248   --
2249   cursor csr_period_type_info(p_period_type VARCHAR2) IS
2250   SELECT ptpt.number_per_fiscal_year
2251   FROM  per_time_period_types ptpt
2252   WHERE p_period_type  = ptpt.period_type;
2253   --
2254   cursor csr_payroll_info(p_pactid NUMBER) IS
2255   SELECT
2256     to_number(aic.context) payroll_id,
2257     fnd_date.canonical_to_date(fai.VALUE) start_year,
2258     fnd_date.canonical_to_date(pay_gb_eoy_archive.get_arch_str(fai.context1,
2259       g_payroll_end_year_eid,
2260       aic.context)) end_year,
2261     pay_gb_eoy_archive.get_arch_str(fai.context1,g_payroll_period_type_eid,
2262                                     aic.context) period_type,
2263     to_number(pay_gb_eoy_archive.get_arch_str(fai.context1,
2264       g_max_period_number_eid, aic.context)) max_period_number,
2265     pay_gb_eoy_archive.get_arch_str(fai.context1,g_tax_ref_eid,
2266                                     aic.context) tax_ref,
2267     pay_gb_eoy_archive.get_arch_str(fai.context1,g_tax_dist_ref_eid,
2268                                     aic.context) tax_dist
2269   FROM  ff_archive_item_contexts aic,  /* payrolls */
2270         ff_archive_items         fai,  /* X_START_YEAR */
2271         ff_user_entities         fue,
2272         pay_payroll_actions      pact
2273   WHERE pact.report_type       = 'EOY'
2274     AND pact.report_qualifier  = 'GB'
2275     AND pact.action_type       = 'X'
2276     AND pact.payroll_action_id = fai.context1
2277     AND fue.user_entity_name   = 'X_START_YEAR'
2278     AND fue.legislation_code   = 'GB'
2279     AND fue.business_group_id  IS NULL
2280     AND fue.user_entity_id     = fai.user_entity_id
2281     AND aic.archive_item_id    = fai.archive_item_id
2282     AND aic.sequence_no        = 1
2283     AND pact.payroll_action_id = p_pactid;
2287   FROM pay_assignment_actions act
2284   --
2285   CURSOR get_retry_actions IS
2286   SELECT act.assignment_action_id, act.action_status
2288   WHERE act.payroll_action_id = p_payroll_action_id
2289   AND   act.action_status = 'M';
2290   --
2291   CURSOR get_agg_non_retry_actions(p_asg_act_id NUMBER) IS
2292   SELECT act2.assignment_action_id, asg2.assignment_number, asg1.assignment_number retry_asg_number, pap.full_name, act2.action_status
2293   FROM   pay_assignment_actions act1,
2294          pay_assignment_actions act2,
2295          per_all_assignments_f asg1,
2296          per_all_assignments_f asg2,
2297          per_all_people_f pap
2298   WHERE  act1.assignment_action_id = p_asg_act_id
2299   AND    act1.assignment_id = asg1.assignment_id
2300   AND    asg1.person_id = pap.person_id
2301   AND    g_end_year between pap.effective_start_date and pap.effective_end_date
2302   AND    pap.person_id = asg2.person_id
2303   AND    asg2.assignment_id = act2.assignment_id
2304   AND    act2.payroll_action_id = act1.payroll_action_id
2305   AND    asg2.assignment_id <> asg1.assignment_id
2306   AND    (pap.per_information10 = 'Y'    -- Agg PAYE
2307           OR pap.per_information9 = 'Y') -- NI Muti Asg
2308   AND    act2.action_status <> 'M';
2309   --
2310   l_agg_non_retry_err_flag VARCHAR2(1) := 'N';
2311   --
2312   l_dummy NUMBER := 0;
2313 BEGIN
2314  -- hr_utility.trace_on(NULL,'ARCHINIT');
2315   hr_utility.set_location('Entering: '||l_proc,1);
2316   --
2317   IF g_payroll_action_id IS NULL
2318   OR g_payroll_action_id <> p_payroll_action_id THEN
2319     g_payroll_action_id := p_payroll_action_id;
2320     g_masg_person_id   := nvl(g_masg_person_id,hr_api.g_number);
2321     g_masg_period_of_service_id := nvl(g_masg_period_of_service_id, hr_api.g_number); -- Bug 3784871
2322     g_masg_active_start := nvl(g_masg_active_start, hr_api.g_sot);
2323     g_masg_active_end := nvl(g_masg_active_end, hr_api.g_eot);
2324     g_masg_tax_ref_num := nvl(g_masg_tax_ref_num,
2325                               substr(hr_api.g_varchar2,1,10)); -- 4011263: substr to 10 chars
2326     --      set up the statutory start and end year
2327     OPEN csr_parameter_info(p_payroll_action_id);
2328     FETCH csr_parameter_info INTO g_start_year,
2329                                   g_end_year,
2330                                   g_business_group_id,
2331                                   g_permit_number,
2332                                   g_tax_district_reference,
2333                                   g_tax_reference_number;
2334     CLOSE csr_parameter_info;
2335     --
2336     l_agg_non_retry_err_flag := 'N';
2337     FOR retry_actions_rec IN get_retry_actions LOOP
2338     --
2339        hr_utility.trace(l_proc||': retry action id='||
2340                 retry_actions_rec.assignment_action_id);
2341        hr_utility.trace(l_proc||': retry action status='||
2342                 retry_actions_rec.action_status);
2343        --
2344        FOR non_retry_act_rec IN get_agg_non_retry_actions(retry_actions_rec.assignment_action_id) LOOP
2345           --
2346           hr_utility.trace(l_proc||': non_retry_act_rec.assignment_action_id='||
2347                        non_retry_act_rec.assignment_action_id);
2348           hr_utility.trace(l_proc||': non_retry_act_rec.assignment_number='||
2349                        non_retry_act_rec.assignment_number);
2350           hr_utility.trace(l_proc||': non_retry_act_rec.retry_asg_number='||
2351                        non_retry_act_rec.retry_asg_number);
2352           hr_utility.trace(l_proc||': non_retry_act_rec.full_name='||
2353                        non_retry_act_rec.full_name);
2354           l_agg_non_retry_err_flag := 'Y';
2355           l_dummy := write_output(p_assignment_number => non_retry_act_rec.retry_asg_number,
2356                        p_full_name => non_retry_act_rec.full_name,
2357                        p_message_type => 'E',
2358                        p_message => 'Assignment action '||non_retry_act_rec.assignment_action_id||' for the aggrgated assignment '||non_retry_act_rec.assignment_number||' must be marked for retry.');
2359          END LOOP;
2360     END LOOP;
2361     --
2362     hr_utility.trace(l_proc||' After get_agg_non_retry_actions, l_agg_non_retry_err_flag='||l_agg_non_retry_err_flag);
2363     --
2364     IF l_agg_non_retry_err_flag = 'Y' THEN
2365        --  Raise the error and stop processing the assignments marked for retry
2366        -- because aggregated assignment(s) has(have) not been marked for retry.
2367        app_exception.raise_exception;
2368     END IF;
2369     --      find the defined balance id's for balance / dimension combos
2370     OPEN get_defined_balance_id('NI A Able','_ASG_TD_YTD');
2371     FETCH get_defined_balance_id INTO g_nia_able_id;
2372     CLOSE get_defined_balance_id;
2373     --
2374     OPEN get_defined_balance_id('NI A Employee','_ASG_TD_YTD');
2375     FETCH get_defined_balance_id INTO g_nia_id;
2376     CLOSE get_defined_balance_id;
2377     --
2378     OPEN get_defined_balance_id('NI A Total','_ASG_TD_YTD');
2379     FETCH get_defined_balance_id INTO g_nia_tot_id;
2380     CLOSE get_defined_balance_id;
2381     --
2382     OPEN get_defined_balance_id('NI A Able LEL','_ASG_TD_YTD');
2383     FETCH get_defined_balance_id INTO g_nia_lel_id;
2384     CLOSE get_defined_balance_id;
2385     --
2386     OPEN get_defined_balance_id('NI A Able UEL','_ASG_TD_YTD');
2387     FETCH get_defined_balance_id INTO g_nia_uel_id;
2388     CLOSE get_defined_balance_id;
2389     --EOY 07/08 begin
2390     OPEN get_defined_balance_id('NI A Able AUEL','_ASG_TD_YTD');
2391     FETCH get_defined_balance_id INTO g_nia_auel_id;
2395     FETCH get_defined_balance_id INTO g_nia_et_id;
2392     CLOSE get_defined_balance_id;
2393     --EOY 07/08 end
2394     OPEN get_defined_balance_id('NI A Able ET','_ASG_TD_YTD');
2396     CLOSE get_defined_balance_id;
2397     --
2398     OPEN get_defined_balance_id('NI B Able','_ASG_TD_YTD');
2399     FETCH get_defined_balance_id INTO g_nib_able_id;
2400     CLOSE get_defined_balance_id;
2401     --
2402     OPEN get_defined_balance_id('NI B Employee','_ASG_TD_YTD');
2403     FETCH get_defined_balance_id INTO g_nib_id;
2404     CLOSE get_defined_balance_id;
2405     --
2406     OPEN get_defined_balance_id('NI B Total','_ASG_TD_YTD');
2407     FETCH get_defined_balance_id INTO g_nib_tot_id;
2408     CLOSE get_defined_balance_id;
2409     --
2410     OPEN get_defined_balance_id('NI B Able LEL','_ASG_TD_YTD');
2411     FETCH get_defined_balance_id INTO g_nib_lel_id;
2412     CLOSE get_defined_balance_id;
2413     --
2414     OPEN get_defined_balance_id('NI B Able UEL','_ASG_TD_YTD');
2415     FETCH get_defined_balance_id INTO g_nib_uel_id;
2416     CLOSE get_defined_balance_id;
2417     --EOY 07/08 begin
2418     OPEN get_defined_balance_id('NI B Able AUEL','_ASG_TD_YTD');
2419     FETCH get_defined_balance_id INTO g_nib_auel_id;
2420     CLOSE get_defined_balance_id;
2421     --EOY 07/08 end
2422     OPEN get_defined_balance_id('NI B Able ET','_ASG_TD_YTD');
2423     FETCH get_defined_balance_id INTO g_nib_et_id;
2424     CLOSE get_defined_balance_id;
2425     --
2426     OPEN get_defined_balance_id('NI C Employer','_ASG_TD_YTD');
2427     FETCH get_defined_balance_id INTO g_nic_tot_id;
2428     CLOSE get_defined_balance_id;
2429     --
2430     OPEN get_defined_balance_id('NI C Able LEL','_ASG_TD_YTD');
2431     FETCH get_defined_balance_id INTO g_nic_lel_id;
2432     CLOSE get_defined_balance_id;
2433     --
2434     OPEN get_defined_balance_id('NI C Able UEL','_ASG_TD_YTD');
2435     FETCH get_defined_balance_id INTO g_nic_uel_id;
2436     CLOSE get_defined_balance_id;
2437     --EOY 07/08 Begin
2438     OPEN get_defined_balance_id('NI C Able AUEL','_ASG_TD_YTD');
2439     FETCH get_defined_balance_id INTO g_nic_auel_id;
2440     CLOSE get_defined_balance_id;
2441     --EOY 07/08 End
2442     OPEN get_defined_balance_id('NI C Able ET','_ASG_TD_YTD');
2443     FETCH get_defined_balance_id INTO g_nic_et_id;
2444     CLOSE get_defined_balance_id;
2445     --
2446     OPEN get_defined_balance_id('NI C Able','_ASG_TD_YTD');
2447     FETCH get_defined_balance_id INTO g_nic_able_id;
2448     CLOSE get_defined_balance_id;
2449     --
2450     -- Fix for Bug 1976152, added the below stmt to fetch the balance id
2451     -- for the balance NI C Employers Rebate
2452     OPEN get_defined_balance_id('NI C Ers Rebate','_ASG_TD_YTD');
2453     FETCH get_defined_balance_id INTO g_nic_ers_rebate_id;
2454     CLOSE get_defined_balance_id;
2455     --
2456     OPEN get_defined_balance_id('NI D Able','_ASG_TD_YTD');
2457     FETCH get_defined_balance_id INTO g_nid_able_id;
2458     CLOSE get_defined_balance_id;
2459     --
2460     OPEN get_defined_balance_id('NI D Employee','_ASG_TD_YTD');
2461     FETCH get_defined_balance_id INTO g_nid_id;
2462     CLOSE get_defined_balance_id;
2463     --
2464     OPEN get_defined_balance_id('NI D Total','_ASG_TD_YTD');
2465     FETCH get_defined_balance_id INTO g_nid_tot_id;
2466     CLOSE get_defined_balance_id;
2467     --
2468     OPEN get_defined_balance_id('NI D Able LEL','_ASG_TD_YTD');
2469     FETCH get_defined_balance_id INTO g_nid_lel_id;
2470     CLOSE get_defined_balance_id;
2471     --
2472     OPEN get_defined_balance_id('NI D Able UEL','_ASG_TD_YTD');
2473     FETCH get_defined_balance_id INTO g_nid_uel_id;
2474     CLOSE get_defined_balance_id;
2475     --EOY 07/08 Begin
2476     OPEN get_defined_balance_id('NI D Able AUEL','_ASG_TD_YTD');
2477     FETCH get_defined_balance_id INTO g_nid_auel_id;
2478     CLOSE get_defined_balance_id;
2479     --EOY 07/08 End
2480     OPEN get_defined_balance_id('NI D Able ET','_ASG_TD_YTD');
2481     FETCH get_defined_balance_id INTO g_nid_et_id;
2482     CLOSE get_defined_balance_id;
2483     --
2484     OPEN get_defined_balance_id('NI D Ers Rebate','_ASG_TD_YTD');
2485     FETCH get_defined_balance_id INTO g_nid_ers_rebate_id;
2486     CLOSE get_defined_balance_id;
2487     --
2488     OPEN get_defined_balance_id('NI D Ees Rebate','_ASG_TD_YTD');
2489     FETCH get_defined_balance_id INTO g_nid_ees_rebate_id;
2490     CLOSE get_defined_balance_id;
2491     --
2492     OPEN get_defined_balance_id('NI D Rebate to Employee','_ASG_TD_YTD');
2493     FETCH get_defined_balance_id INTO g_nid_rebate_emp_id;
2494     CLOSE get_defined_balance_id;
2495     --
2496     OPEN get_defined_balance_id('NI E Able','_ASG_TD_YTD');
2497     FETCH get_defined_balance_id INTO g_nie_able_id;
2498     CLOSE get_defined_balance_id;
2499     --
2500     OPEN get_defined_balance_id('NI E Employee','_ASG_TD_YTD');
2501     FETCH get_defined_balance_id INTO g_nie_id;
2502     CLOSE get_defined_balance_id;
2503     --
2504     OPEN get_defined_balance_id('NI E Total','_ASG_TD_YTD');
2505     FETCH get_defined_balance_id INTO g_nie_tot_id;
2506     CLOSE get_defined_balance_id;
2507     --
2508     OPEN get_defined_balance_id('NI E Able LEL','_ASG_TD_YTD');
2509     FETCH get_defined_balance_id INTO g_nie_lel_id;
2510     CLOSE get_defined_balance_id;
2511     --
2512     OPEN get_defined_balance_id('NI E Able UEL','_ASG_TD_YTD');
2513     FETCH get_defined_balance_id INTO g_nie_uel_id;
2514     CLOSE get_defined_balance_id;
2518     CLOSE get_defined_balance_id;
2515     --EOY 07/08 Begin
2516     OPEN get_defined_balance_id('NI E Able AUEL','_ASG_TD_YTD');
2517     FETCH get_defined_balance_id INTO g_nie_auel_id;
2519     --EOY 07/08 End
2520     OPEN get_defined_balance_id('NI E Able ET','_ASG_TD_YTD');
2521     FETCH get_defined_balance_id INTO g_nie_et_id;
2522     CLOSE get_defined_balance_id;
2523     --
2524     OPEN get_defined_balance_id('NI E Ers Rebate','_ASG_TD_YTD');
2525     FETCH get_defined_balance_id INTO g_nie_ers_rebate_id;
2526     CLOSE get_defined_balance_id;
2527     --
2528     OPEN get_defined_balance_id('NI F Total','_ASG_TD_YTD');
2529     FETCH get_defined_balance_id INTO g_nif_tot_id;
2530     CLOSE get_defined_balance_id;
2531     --
2532     OPEN get_defined_balance_id('NI F Ees Rebate','_ASG_TD_YTD');
2533     FETCH get_defined_balance_id INTO g_nif_ees_rebate_id;
2534     CLOSE get_defined_balance_id;
2535     --
2536     OPEN get_defined_balance_id('NI G Total','_ASG_TD_YTD');
2537     FETCH get_defined_balance_id INTO g_nig_tot_id;
2538     CLOSE get_defined_balance_id;
2539     --
2540     OPEN get_defined_balance_id('NI S Employer','_ASG_TD_YTD');
2541     FETCH get_defined_balance_id INTO g_nis_tot_id;
2542     CLOSE get_defined_balance_id;
2543     --
2544     OPEN get_defined_balance_id('NI J Able','_ASG_TD_YTD');
2545     FETCH get_defined_balance_id INTO g_nij_able_id;
2546     CLOSE get_defined_balance_id;
2547     --
2548     OPEN get_defined_balance_id('NI J Employee','_ASG_TD_YTD');
2549     FETCH get_defined_balance_id INTO g_nij_id;
2550     CLOSE get_defined_balance_id;
2551     --
2552     OPEN get_defined_balance_id('NI J Total','_ASG_TD_YTD');
2553     FETCH get_defined_balance_id INTO g_nij_tot_id;
2554     CLOSE get_defined_balance_id;
2555     --
2556     OPEN get_defined_balance_id('NI J Able LEL','_ASG_TD_YTD');
2557     FETCH get_defined_balance_id INTO g_nij_lel_id;
2558     CLOSE get_defined_balance_id;
2559     --
2560     OPEN get_defined_balance_id('NI J Able UEL','_ASG_TD_YTD');
2561     FETCH get_defined_balance_id INTO g_nij_uel_id;
2562     CLOSE get_defined_balance_id;
2563     --EOY 07/08 Begin
2564     OPEN get_defined_balance_id('NI J Able AUEL','_ASG_TD_YTD');
2565     FETCH get_defined_balance_id INTO g_nij_auel_id;
2566     CLOSE get_defined_balance_id;
2567     --EOY 07/08 End
2568     OPEN get_defined_balance_id('NI J Able ET','_ASG_TD_YTD');
2569     FETCH get_defined_balance_id INTO g_nij_et_id;
2570     CLOSE get_defined_balance_id;
2571     --
2572     OPEN get_defined_balance_id('NI L Able','_ASG_TD_YTD');
2573     FETCH get_defined_balance_id INTO g_nil_able_id;
2574     CLOSE get_defined_balance_id;
2575     --
2576     OPEN get_defined_balance_id('NI L Employee','_ASG_TD_YTD');
2577     FETCH get_defined_balance_id INTO g_nil_id;
2578     CLOSE get_defined_balance_id;
2579     --
2580     OPEN get_defined_balance_id('NI L Total','_ASG_TD_YTD');
2581     FETCH get_defined_balance_id INTO g_nil_tot_id;
2582     CLOSE get_defined_balance_id;
2583     --
2584     OPEN get_defined_balance_id('NI L Able LEL','_ASG_TD_YTD');
2585     FETCH get_defined_balance_id INTO g_nil_lel_id;
2586     CLOSE get_defined_balance_id;
2587     --
2588     OPEN get_defined_balance_id('NI L Able UEL','_ASG_TD_YTD');
2589     FETCH get_defined_balance_id INTO g_nil_uel_id;
2590     CLOSE get_defined_balance_id;
2591     --EOY 07/08 Begin
2592     OPEN get_defined_balance_id('NI L Able AUEL','_ASG_TD_YTD');
2593     FETCH get_defined_balance_id INTO g_nil_auel_id;
2594     CLOSE get_defined_balance_id;
2595     --EOY 07/08 End
2596     OPEN get_defined_balance_id('NI L Able ET','_ASG_TD_YTD');
2597     FETCH get_defined_balance_id INTO g_nil_et_id;
2598     CLOSE get_defined_balance_id;
2599     --
2600     OPEN get_defined_balance_id('SSP Total','_ASG_TD_YTD');
2601     FETCH get_defined_balance_id INTO g_ssp_id;
2602     CLOSE get_defined_balance_id;
2603     --
2604     OPEN get_defined_balance_id('SMP Total','_ASG_TD_YTD');
2605     FETCH get_defined_balance_id INTO g_smp_id;
2606     CLOSE get_defined_balance_id;
2607     --
2608     OPEN get_defined_balance_id('SAP Total','_ASG_TD_YTD');
2609     FETCH get_defined_balance_id INTO g_sap_id;
2610     CLOSE get_defined_balance_id;
2611     --
2612     OPEN get_defined_balance_id('SPP Adoption Total','_ASG_TD_YTD');
2613     FETCH get_defined_balance_id INTO g_spp_adopt_id;
2614     CLOSE get_defined_balance_id;
2615     --
2616     OPEN get_defined_balance_id('SPP Birth Total','_ASG_TD_YTD');
2617     FETCH get_defined_balance_id INTO g_spp_birth_id;
2618     CLOSE get_defined_balance_id;
2619     --
2620     OPEN get_defined_balance_id('Gross Pay','_ASG_TD_YTD');
2621     FETCH get_defined_balance_id INTO g_gross_id;
2622     CLOSE get_defined_balance_id;
2623     --
2624     OPEN get_defined_balance_id('Notional Pay','_ASG_TD_YTD');
2625     FETCH get_defined_balance_id INTO g_notional_id;
2626     CLOSE get_defined_balance_id;
2627     --
2628     OPEN get_defined_balance_id('PAYE','_ASG_TD_YTD');
2629     FETCH get_defined_balance_id INTO g_paye_id;
2630     CLOSE get_defined_balance_id;
2631     --
2632     OPEN get_defined_balance_id('Superannuation Total','_ASG_TD_YTD');
2633     FETCH get_defined_balance_id INTO g_super_id;
2634     CLOSE get_defined_balance_id;
2635     --
2636     OPEN get_defined_balance_id('Widows and Orphans','_ASG_TD_YTD');
2637     FETCH get_defined_balance_id INTO g_widow_id;
2638     CLOSE get_defined_balance_id;
2639     --
2643     --
2640     OPEN get_defined_balance_id('Student Loan','_ASG_TD_YTD');
2641     FETCH get_defined_balance_id INTO g_student_loan_id;
2642     CLOSE get_defined_balance_id;
2644     OPEN get_defined_balance_id('Taxable Pay','_ASG_TD_YTD');
2645     FETCH get_defined_balance_id INTO g_taxable_id;
2646     CLOSE get_defined_balance_id;
2647     --
2648     OPEN get_defined_balance_id('NI Arrears','_ASG_TD_YTD');
2649     FETCH get_defined_balance_id INTO g_ni_arrears_id;
2650     CLOSE get_defined_balance_id;
2651     --
2652     SELECT element_type_id
2653     INTO   g_paye_details_id
2654     FROM   pay_element_types_f
2655     WHERE  element_name = 'PAYE Details'
2656       AND  g_end_year BETWEEN effective_start_date AND effective_end_date;
2657     --
2658     SELECT element_type_id
2659     INTO   g_paye_element_id
2660     FROM   pay_element_types_f
2661     WHERE  element_name = 'PAYE'
2662       AND  g_end_year BETWEEN effective_start_date AND effective_end_date;
2663     --
2664     SELECT element_type_id
2665     INTO   g_ni_id
2666     FROM   pay_element_types_f
2667     WHERE  element_name = 'NI'
2668       AND  g_end_year BETWEEN effective_start_date AND effective_end_date;
2669     --
2670     SELECT input_value_id
2671     INTO   g_category_input_id
2672     FROM   pay_input_values_f
2673     WHERE  name = 'Category'
2674       AND  element_type_id = g_ni_id
2675       AND  g_end_year BETWEEN effective_start_date AND effective_end_date;
2676     --
2677     SELECT input_value_id
2678     INTO   g_process_type_id
2679     FROM   pay_input_values_f
2680     WHERE  name = 'Process Type'
2681       AND  element_type_id = g_ni_id
2682       AND  g_end_year BETWEEN effective_start_date AND effective_end_date;
2683     --
2684     SELECT input_value_id
2685     INTO   g_scon_input_id
2686     FROM   pay_input_values_f
2687     WHERE  name = 'SCON'
2688       AND  element_type_id = g_ni_id
2689       AND  g_end_year BETWEEN effective_start_date AND effective_end_date;
2690     --
2691     -- Get User Entity IDs
2692     -- Assignment and value entities
2693     OPEN csr_user_entity('X_ADDRESS_LINE1');
2694     FETCH csr_user_entity INTO g_address_line1_eid;
2695     CLOSE csr_user_entity;
2696     OPEN csr_user_entity('X_ADDRESS_LINE2');
2697     FETCH csr_user_entity INTO g_address_line2_eid;
2698     CLOSE csr_user_entity;
2699     OPEN csr_user_entity('X_ADDRESS_LINE3');
2700     FETCH csr_user_entity INTO g_address_line3_eid;
2701     CLOSE csr_user_entity;
2702     OPEN csr_user_entity('X_ASSIGNMENT_NUMBER');
2703     FETCH csr_user_entity INTO g_assignment_number_eid;
2704     CLOSE csr_user_entity;
2705     OPEN csr_user_entity('X_COUNTY');
2706     FETCH csr_user_entity INTO g_county_eid;
2707     CLOSE csr_user_entity;
2708     OPEN csr_user_entity('X_COUNTRY'); -- 4011263
2709     FETCH csr_user_entity INTO g_country_eid;
2710     CLOSE csr_user_entity;
2711     OPEN csr_user_entity('X_DATE_OF_BIRTH');
2712     FETCH csr_user_entity INTO g_date_of_birth_eid;
2713     CLOSE csr_user_entity;
2714     OPEN csr_user_entity('X_DIRECTOR_INDICATOR');
2715     FETCH csr_user_entity INTO g_director_indicator_eid;
2716     CLOSE csr_user_entity;
2717     OPEN csr_user_entity('X_EFFECTIVE_END_DATE');
2718     FETCH csr_user_entity INTO g_effective_end_date_eid;
2719     CLOSE csr_user_entity;
2720     OPEN csr_user_entity('X_EFFECTIVE_START_DATE');
2721     FETCH csr_user_entity INTO g_effective_start_date_eid;
2722     CLOSE csr_user_entity;
2723     OPEN csr_user_entity('X_EOY_PRIMARY_FLAG');
2724     FETCH csr_user_entity INTO g_eoy_primary_flag_eid;
2725     CLOSE csr_user_entity;
2726     OPEN csr_user_entity('X_EXPENSE_CHECK_SEND_TO_ADDRESS');
2727     FETCH csr_user_entity INTO g_expense_check_to_address_eid;
2728     CLOSE csr_user_entity;
2729     OPEN csr_user_entity('X_FIRST_NAME');
2730     FETCH csr_user_entity INTO g_first_name_eid;
2731     CLOSE csr_user_entity;
2732     OPEN csr_user_entity('X_GROSS_PAY');
2733     FETCH csr_user_entity INTO g_gross_pay_eid;
2734     CLOSE csr_user_entity;
2735     OPEN csr_user_entity('X_NOTIONAL_PAY');
2736     FETCH csr_user_entity INTO g_notional_pay_eid;
2737     CLOSE csr_user_entity;
2738     OPEN csr_user_entity('X_LAST_ASG_ACTION_ID');
2739     FETCH csr_user_entity INTO g_last_asg_action_id_eid;
2740     CLOSE csr_user_entity;
2741     OPEN csr_user_entity('X_LAST_EFFECTIVE_DATE');
2742     FETCH csr_user_entity INTO g_last_effective_date_eid;
2743     CLOSE csr_user_entity;
2744     OPEN csr_user_entity('X_LAST_MULTI_ASG_PER_PERSON_TAX_REF');
2745     FETCH csr_user_entity INTO g_last_multi_asg_eid;
2746     CLOSE csr_user_entity;
2747     OPEN csr_user_entity('X_AGGREGATED_PAYE_FLAG');
2748     FETCH csr_user_entity INTO g_aggregated_paye_flag_eid;
2749     CLOSE csr_user_entity;
2750     OPEN csr_user_entity('X_LAST_NAME');
2751     FETCH csr_user_entity INTO g_last_name_eid;
2752     CLOSE csr_user_entity;
2753     OPEN csr_user_entity('X_LOCATION_ID');
2754     FETCH csr_user_entity INTO g_location_id_eid;
2755     CLOSE csr_user_entity;
2756     OPEN csr_user_entity('X_MIDDLE_NAME');
2757     FETCH csr_user_entity INTO g_middle_name_eid;
2758     CLOSE csr_user_entity;
2759     OPEN csr_user_entity('X_MULTIPLE_ASG_FLAG');
2760     FETCH csr_user_entity INTO g_multiple_asg_flag_eid;
2761     CLOSE csr_user_entity;
2762     OPEN csr_user_entity('X_NATIONAL_INSURANCE_NUMBER');
2763     FETCH csr_user_entity INTO g_ni_number_eid;
2764     CLOSE csr_user_entity;
2768     OPEN csr_user_entity('X_NI_ABLE_LEL');
2765     OPEN csr_user_entity('X_NI_ABLE_ET');
2766     FETCH csr_user_entity INTO g_ni_able_et_eid;
2767     CLOSE csr_user_entity;
2769     FETCH csr_user_entity INTO g_ni_able_lel_eid;
2770     CLOSE csr_user_entity;
2771     OPEN csr_user_entity('X_NI_ABLE_UEL');
2772     FETCH csr_user_entity INTO g_ni_able_uel_eid;
2773     CLOSE csr_user_entity;
2774     --EOY 07/08 Begin
2775     OPEN csr_user_entity('X_NI_ABLE_AUEL');
2776     FETCH csr_user_entity INTO g_ni_able_auel_eid;
2777     CLOSE csr_user_entity;
2778     --EOY 07/08 End
2779     OPEN csr_user_entity('X_NI_EARNINGS');
2780     FETCH csr_user_entity INTO g_ni_earnings_eid;
2781     CLOSE csr_user_entity;
2782     OPEN csr_user_entity('X_NI_EMPLOYEES_CONTRIBUTIONS');
2783     FETCH csr_user_entity INTO g_ni_ees_contribution_eid;
2784     CLOSE csr_user_entity;
2785     OPEN csr_user_entity('X_NI_EMPLOYERS_REBATE');
2786     FETCH csr_user_entity INTO g_ni_ers_rebate_eid;
2787     CLOSE csr_user_entity;
2788     OPEN csr_user_entity('X_NI_EMPLOYEES_REBATE');
2789     FETCH csr_user_entity INTO g_ni_ees_rebate_eid;
2790     CLOSE csr_user_entity;
2791     OPEN csr_user_entity('X_NI_SCON_EMPLOYEES_REBATE');
2792     FETCH csr_user_entity INTO g_ni_scon_ees_rebate_eid;
2793     CLOSE csr_user_entity;
2794     OPEN csr_user_entity('X_NI_SCON_ABLE_ET');
2795     FETCH csr_user_entity INTO g_ni_scon_able_et_eid;
2796     CLOSE csr_user_entity;
2797     OPEN csr_user_entity('X_NI_SCON_ABLE_LEL');
2798     FETCH csr_user_entity INTO g_ni_scon_able_lel_eid;
2799     CLOSE csr_user_entity;
2800     OPEN csr_user_entity('X_NI_SCON_ABLE_UEL');
2801     FETCH csr_user_entity INTO g_ni_scon_able_uel_eid;
2802     CLOSE csr_user_entity;
2803     --EOY 07/08 Begin
2804     OPEN csr_user_entity('X_NI_SCON_ABLE_AUEL');
2805     FETCH csr_user_entity INTO g_ni_scon_able_auel_eid;
2806     CLOSE csr_user_entity;
2807     --EOY 07/08 End
2808     OPEN csr_user_entity('X_NI_SCON_EARNINGS');
2809     FETCH csr_user_entity INTO g_ni_scon_earnings_eid;
2810     CLOSE csr_user_entity;
2811     OPEN csr_user_entity('X_NI_SCON_EMPLOYEES_CONTRIBUTIONS');
2812     FETCH csr_user_entity INTO g_ni_scon_ees_contribution_eid;
2813     CLOSE csr_user_entity;
2814     OPEN csr_user_entity('X_NI_SCON_EMPLOYERS_REBATE');
2815     FETCH csr_user_entity INTO g_ni_scon_ers_rebate_eid;
2816     CLOSE csr_user_entity;
2817     OPEN csr_user_entity('X_NI_SCON_TOTAL_CONTRIBUTIONS');
2818     FETCH csr_user_entity INTO g_ni_scon_tot_contribution_eid;
2819     CLOSE csr_user_entity;
2820     OPEN csr_user_entity('X_NI_TOTAL_CONTRIBUTIONS');
2821     FETCH csr_user_entity INTO g_ni_tot_contribution_eid;
2822     CLOSE csr_user_entity;
2823     OPEN csr_user_entity('X_NI_REFUND');
2824     FETCH csr_user_entity INTO g_ni_refund_eid;
2825     CLOSE csr_user_entity;
2826     OPEN csr_user_entity('X_NI_SCON_REFUND');
2827     FETCH csr_user_entity INTO g_ni_scon_refund_eid;
2828     CLOSE csr_user_entity;
2829     OPEN csr_user_entity('X_ORGANIZATION_ID');
2830     FETCH csr_user_entity INTO g_organization_id_eid;
2831     CLOSE csr_user_entity;
2832     OPEN csr_user_entity('X_PAYROLL_ID');
2833     FETCH csr_user_entity INTO g_payroll_id_eid;
2834     CLOSE csr_user_entity;
2835     OPEN csr_user_entity('X_PENSIONER_INDICATOR');
2836     FETCH csr_user_entity INTO g_pensioner_indicator_eid;
2837     CLOSE csr_user_entity;
2838     OPEN csr_user_entity('X_PEOPLE_GROUP_ID');
2839     FETCH csr_user_entity INTO g_people_group_id_eid;
2840     CLOSE csr_user_entity;
2841     OPEN csr_user_entity('X_PERSON_ID');
2842     FETCH csr_user_entity INTO g_person_id_eid;
2843     CLOSE csr_user_entity;
2844     OPEN csr_user_entity('X_POSTAL_CODE');
2845     FETCH csr_user_entity INTO g_postal_code_eid;
2846     CLOSE csr_user_entity;
2847     OPEN csr_user_entity('X_PREVIOUS_TAX_PAID');
2848     FETCH csr_user_entity INTO g_prev_tax_paid_eid;
2849     CLOSE csr_user_entity;
2850     OPEN csr_user_entity('X_PREVIOUS_TAXABLE_PAY');
2851     FETCH csr_user_entity INTO g_prev_taxable_pay_eid;
2852     CLOSE csr_user_entity;
2853     OPEN csr_user_entity('X_SEX');
2854     FETCH csr_user_entity INTO g_sex_eid;
2855     CLOSE csr_user_entity;
2856     OPEN csr_user_entity('X_SMP');
2857     FETCH csr_user_entity INTO g_smp_eid;
2858     CLOSE csr_user_entity;
2859     OPEN csr_user_entity('X_SSP');
2860     FETCH csr_user_entity INTO g_ssp_eid;
2861     CLOSE csr_user_entity;
2862     OPEN csr_user_entity('X_SAP');
2863     FETCH csr_user_entity INTO g_sap_eid;
2864     CLOSE csr_user_entity;
2865     OPEN csr_user_entity('X_SPP_ADOPT');
2866     FETCH csr_user_entity INTO g_spp_adopt_eid;
2867     CLOSE csr_user_entity;
2868     OPEN csr_user_entity('X_SPP_BIRTH');
2869     FETCH csr_user_entity INTO g_spp_birth_eid;
2870     CLOSE csr_user_entity;
2871     OPEN csr_user_entity('X_START_OF_EMP');
2872     FETCH csr_user_entity INTO g_start_of_emp_eid;
2873     CLOSE csr_user_entity;
2874     OPEN csr_user_entity('X_SUPERANNUATION_PAID');
2875     FETCH csr_user_entity INTO g_superannuation_paid_eid;
2876     CLOSE csr_user_entity;
2877     OPEN csr_user_entity('X_SUPERANNUATION_REFUND');
2878     FETCH csr_user_entity INTO g_superannuation_refund_eid;
2879     CLOSE csr_user_entity;
2880     OPEN csr_user_entity('X_TAX_CODE');
2881     FETCH csr_user_entity INTO g_tax_code_eid;
2882     CLOSE csr_user_entity;
2883     OPEN csr_user_entity('X_TAX_PAID');
2884     FETCH csr_user_entity INTO g_tax_paid_eid;
2885     CLOSE csr_user_entity;
2889     OPEN csr_user_entity('X_TAX_REFUND');
2886     OPEN csr_user_entity('X_TAX_REF_TRANSFER');
2887     FETCH csr_user_entity INTO g_tax_ref_transfer_eid;
2888     CLOSE csr_user_entity;
2890     FETCH csr_user_entity INTO g_tax_refund_eid;
2891     CLOSE csr_user_entity;
2892     OPEN csr_user_entity('X_TAX_RUN_RESULT_ID');
2893     FETCH csr_user_entity INTO g_tax_run_result_id_eid;
2894     CLOSE csr_user_entity;
2895     OPEN csr_user_entity('X_TAXABLE_PAY');
2896     FETCH csr_user_entity INTO g_taxable_pay_eid;
2897     CLOSE csr_user_entity;
2898     OPEN csr_user_entity('X_TERMINATION_DATE');
2899     FETCH csr_user_entity INTO g_termination_date_eid;
2900     CLOSE csr_user_entity;
2901     OPEN csr_user_entity('X_TERMINATION_TYPE');
2902     FETCH csr_user_entity INTO g_termination_type_eid;
2903     CLOSE csr_user_entity;
2904     OPEN csr_user_entity('X_TITLE');
2905     FETCH csr_user_entity INTO g_title_eid;
2906     CLOSE csr_user_entity;
2907     OPEN csr_user_entity('X_TOWN_OR_CITY');
2908     FETCH csr_user_entity INTO g_town_or_city_eid;
2909     CLOSE csr_user_entity;
2910     OPEN csr_user_entity('X_W1_M1_INDICATOR');
2911     FETCH csr_user_entity INTO g_w1_m1_indicator_eid;
2912     CLOSE csr_user_entity;
2913     OPEN csr_user_entity('X_WEEK_53_INDICATOR');
2914     FETCH csr_user_entity INTO g_week_53_indicator_eid;
2915     CLOSE csr_user_entity;
2916     OPEN csr_user_entity('X_WIDOWS_AND_ORPHANS');
2917     FETCH csr_user_entity INTO g_widows_and_orphans_eid;
2918     CLOSE csr_user_entity;
2919     OPEN csr_user_entity('X_STUDENT_LOANS');
2920     FETCH csr_user_entity INTO g_student_loans_eid;
2921     CLOSE csr_user_entity;
2922     -- Payroll Entities
2923     OPEN csr_user_entity('X_TAX_DISTRICT_REFERENCE');
2924     FETCH csr_user_entity INTO g_tax_dist_ref_eid;
2925     CLOSE csr_user_entity;
2926     OPEN csr_user_entity('X_TAX_REFERENCE_NUMBER');
2927     FETCH csr_user_entity INTO g_tax_ref_eid;
2928     CLOSE csr_user_entity;
2929     OPEN csr_user_entity('X_PERMIT_NUMBER');
2930     FETCH csr_user_entity INTO g_permit_number_eid;
2931     CLOSE csr_user_entity;
2932     OPEN csr_user_entity('X_MAX_PERIOD_NUMBER');
2933     FETCH csr_user_entity INTO g_max_period_number_eid;
2934     CLOSE csr_user_entity;
2935     OPEN csr_user_entity('X_PERIOD_TYPE');
2936     FETCH csr_user_entity INTO g_payroll_period_type_eid;
2937     CLOSE csr_user_entity;
2938     OPEN csr_user_entity('X_END_YEAR');
2939     FETCH csr_user_entity INTO g_payroll_end_year_eid;
2940     CLOSE csr_user_entity;
2941     OPEN csr_user_entity('X_START_YEAR');
2942     FETCH csr_user_entity INTO g_payroll_start_year_eid;
2943     CLOSE csr_user_entity;
2944     OPEN csr_user_entity('X_ASSIGNMENT_MESSAGE');
2945     FETCH csr_user_entity INTO g_assignment_message_eid;
2946     CLOSE csr_user_entity;
2947     OPEN csr_user_entity('X_NI_ARREARS');
2948     FETCH csr_user_entity INTO g_ni_arrears_eid;
2949     CLOSE csr_user_entity;
2950     OPEN csr_user_entity('X_REPORTABLE_NI');
2951     FETCH csr_user_entity INTO g_reportable_ni_eid;
2952     CLOSE csr_user_entity;
2953     OPEN csr_user_entity('X_AGG_ACTIVE_START');
2954     FETCH csr_user_entity INTO g_agg_active_start_eid;
2955     CLOSE csr_user_entity;
2956     OPEN csr_user_entity('X_AGG_ACTIVE_END');
2957     FETCH csr_user_entity INTO g_agg_active_end_eid;
2958     CLOSE csr_user_entity;
2959     --
2960      FOR pay_info IN csr_payroll_info(g_payroll_action_id) LOOP
2961        --
2962        l_payroll_id                := pay_info.payroll_id;
2963        l_payroll_start_year        := pay_info.start_year;
2964        l_payroll_end_year          := pay_info.end_year;
2965        l_payroll_period_type       := pay_info.period_type;
2966        l_payroll_max_period_number := pay_info.max_period_number;
2967        l_payroll_tax_ref           := pay_info.tax_ref;
2968        l_payroll_tax_dist          := pay_info.tax_dist;
2969        --
2970        -- Initialise period info for this payroll ID.
2971        --
2972        OPEN csr_period_type_info(l_payroll_period_type);
2973        FETCH csr_period_type_info INTO  l_number_per_fiscal_yr;
2974        CLOSE csr_period_type_info;
2975        --
2976        -- Assign to cache tables
2977        --
2978        g_no_per_fiscal_yr(l_payroll_id)      := l_number_per_fiscal_yr;
2979        --
2980        g_pay_start_yr_tab(l_payroll_id)      := l_payroll_start_year;
2981        g_pay_end_yr_tab(l_payroll_id)        := l_payroll_end_year;
2982        g_pay_period_typ_tab(l_payroll_id)    := l_payroll_period_type;
2983        g_pay_max_per_no_tab(l_payroll_id)    := l_payroll_max_period_number;
2984        g_pay_tax_ref_tab(l_payroll_id)       := l_payroll_tax_ref;
2985        g_pay_tax_dist_tab(l_payroll_id)      := l_payroll_tax_dist;
2986        --
2987        -- Assign the max and min payroll dates for asg level csr use
2988        --
2989        g_min_start_year := least(g_min_start_year,
2990                               nvl(l_payroll_start_year,g_min_start_year));
2991        g_max_end_year := greatest(g_max_end_year,
2992                               nvl(l_payroll_end_year,g_max_end_year));
2993        --
2994      END LOOP;
2995      --
2996   END IF;
2997   hr_utility.set_location(' Leaving: '||l_proc,100);
2998 END archinit;
2999 --
3000 --
3001 PROCEDURE archive_code(p_assactid IN NUMBER, p_effective_date IN DATE) IS
3002   --
3003   l_proc             CONSTANT VARCHAR2(35):= g_package||'archive_code';
3004   -- vars for returns from the API:
3008   --
3005   l_archive_item_id           ff_archive_items.archive_item_id%TYPE;
3006   l_ovn                       NUMBER;
3007   l_some_warning              BOOLEAN;
3009   l_assignment_id       per_all_assignments_f.assignment_id%TYPE;
3010   l_effective_end_date  DATE;
3011   l_adj_eff_end_date    DATE;
3012   l_tax_ref_transfer    VARCHAR(1);
3013   l_termination_type    VARCHAR(1);
3014   --
3015   l_payroll_start_year        DATE;
3016   l_payroll_end_year          DATE;
3017   l_payroll_period_type       VARCHAR2(30);
3018   l_payroll_max_period_number NUMBER;
3019   l_payroll_tax_ref           VARCHAR2(10);
3020   l_payroll_tax_dist          VARCHAR2(3);
3021   --
3022   l_payroll_id            pay_all_payrolls_f.payroll_id%TYPE;
3023   l_assignment_number     per_all_assignments_f.assignment_number%TYPE;
3024   l_person_id             per_all_people_f.person_id%TYPE;
3025   l_organization_id       hr_organization_units.organization_id%TYPE;
3026   l_location_id           per_all_assignments_f.location_id%TYPE;
3027   l_people_group_id       per_all_assignments_f.people_group_id%TYPE;
3028   l_period_of_service_id  per_all_assignments_f.period_of_service_id%TYPE;
3029   l_agg_active_start      per_all_assignments_f.effective_start_date%TYPE;
3030   l_agg_active_end        per_all_assignments_f.effective_end_date%TYPE;
3031   l_active_start          per_all_assignments_f.effective_start_date%TYPE;
3032   l_active_end            per_all_assignments_f.effective_end_date%TYPE;
3033   --
3034   l_effective_start_date  DATE;
3035   l_final_process_date    DATE;
3036   --
3037   l_termination_date      DATE;
3038   l_actual_termination_date DATE;
3039   l_last_std_process_Date DATE;
3040   l_date_of_manual_p45_issue DATE;
3041   l_p45_issued            VARCHAR2(1);
3042   l_p45_issue_date        DATE;
3043   l_p45_action_id         NUMBER;
3044   l_p45_action_seq        NUMBER;
3045   l_p45_agg_asg_id        NUMBER;
3046   l_p45_final_pay_date    DATE;
3047   --
3048   l_last_asg_action_id    pay_assignment_actions.assignment_action_id%TYPE;
3049   l_last_effective_date   DATE;
3050   --
3051   l_paye_eff_date         DATE;
3052   l_paye_details_eff_date DATE;
3053   --
3054   --
3055   l_last_name                 per_all_people_f.last_name%TYPE;
3056   l_first_name                per_all_people_f.first_name%TYPE;
3057   l_middle_name               per_all_people_f.middle_names%TYPE;
3058   l_date_of_birth             DATE;
3059   l_title                     per_all_people_f.title%TYPE;
3060   l_expense_check_to_address  per_all_people_f.expense_check_send_to_address%TYPE;
3061   l_ni_number                 per_all_people_f.national_identifier%TYPE;
3062   l_sex                       per_all_people_f.sex%TYPE;
3063   l_pensioner_indicator       VARCHAR2(1);
3064   l_aggregated_paye_flag      VARCHAR2(1);
3065   l_multiple_asg_flag         VARCHAR2(1);
3066   --
3067   l_director_indicator        VARCHAR2(1);
3068   --
3069   l_start_of_emp              DATE;
3070   --
3071   l_address_line1             per_addresses.address_line1%type;
3072   l_address_line2             per_addresses.address_line2%type;
3073   l_address_line3             per_addresses.address_line3%type;
3074   l_town_or_city              VARCHAR2(30);
3075   l_county                    VARCHAR2(27);
3076   l_country_name              fnd_territories_tl.territory_short_name%type;
3077   l_country                   per_addresses.country%type;
3078   l_postal_code               VARCHAR2(8);
3079   --
3080   l_ni_tot                    NUMBER:= 0;
3081   l_ni_ees                    NUMBER:= 0;
3082   l_ni_able                   NUMBER:= 0;
3083   l_ni_able_lel               NUMBER:= 0;
3084   l_ni_able_uel               NUMBER:= 0;
3085   l_ni_able_auel              NUMBER:= 0; --EOY 07/08
3086   l_ni_able_et                NUMBER:= 0;
3087   l_ers_rebate                NUMBER;
3088   l_ees_rebate                NUMBER;
3089   l_rebate_emp                NUMBER;
3090   l_ni_cat                    VARCHAR2(1);
3091   l_ni_refund_flag            VARCHAR2(1);
3092   l_scon                      VARCHAR2(9);
3093   l_smp                       NUMBER(15):=0;
3094   l_ssp                       NUMBER(15):=0;
3095   l_sap                       NUMBER(15):=0;
3096   l_spp_adopt                 NUMBER(15):=0;
3097   l_spp_birth                 NUMBER(15):=0;
3098   l_gross                     NUMBER(15):=0;
3099   l_notional                  NUMBER(15):=0;
3100   l_paye                      NUMBER(15):=0;
3101   l_super                     NUMBER(15):=0;
3102   l_widow                     NUMBER(15):=0;
3103   l_taxable                   NUMBER(15):=0;
3104   l_student_loan              NUMBER(15):=0;
3105   --
3106   l_count_values              NUMBER(15):=0;
3107   l_dummy                     NUMBER(1);
3108   l_index1                    binary_integer;
3109   l_index2                    binary_integer;
3110   --
3111   l_tax_run_result_id         pay_run_results.run_result_id%TYPE;
3112   l_tax_paye_run_result_id    pay_run_results.run_result_id%TYPE;
3113   --
3114   l_tax_code                  VARCHAR2(10);
3115   l_w1_m1_indicator           VARCHAR2(1);
3116   l_previous_taxable_pay      NUMBER(15);
3117   l_previous_tax_paid         NUMBER(15);
3118   l_week_53_indicator         VARCHAR2(1);
3119   l_week_53_start             DATE;
3120   l_number_per_fiscal_yr      NUMBER;
3121   l_assignment_message        VARCHAR2(80);
3122   l_action_type               pay_payroll_actions.action_type%TYPE;
3126   -- Start of Bug 6271548
3123   l_ni_arrears                NUMBER(15):=0;
3124   l_reportable_ni_archived    BOOLEAN := FALSE;
3125 
3127   -- local variables to store NI total and able balances
3128   l_nia_tot                    NUMBER(15) :=0;
3129   l_nia_able                   NUMBER(15) :=0;
3130   l_nib_tot                    NUMBER(15) :=0;
3131   l_nib_able                   NUMBER(15) :=0;
3132   l_nic_tot                    NUMBER(15) :=0;
3133   l_nic_able                   NUMBER(15) :=0;
3134   l_nid_tot                    NUMBER(15) :=0;
3135   l_nid_able                   NUMBER(15) :=0;
3136   l_nie_tot                    NUMBER(15) :=0;
3137   l_nie_able                   NUMBER(15) :=0;
3138   l_nij_tot                    NUMBER(15) :=0;
3139   l_nij_able                   NUMBER(15) :=0;
3140   l_nil_tot                    NUMBER(15) :=0;
3141   l_nil_able                   NUMBER(15) :=0;
3142   -- End of Bug 6271548
3143 
3144   ASG_ACTION_ERROR            EXCEPTION;
3145   --
3146   cursor csr_asg_act_info(p_asgactid NUMBER) IS
3147   SELECT act.assignment_id,
3148     fnd_date.canonical_to_date(pay_gb_eoy_archive.get_arch_str
3149        (act.assignment_action_id,
3150         g_effective_end_date_eid)) end_date,
3151     nvl(pay_gb_eoy_archive.get_arch_str(act.assignment_action_id,
3152       g_tax_ref_transfer_eid),'N') tax_ref_transfer,
3153     nvl(fnd_date.canonical_to_date(pay_gb_eoy_archive.get_arch_str
3154        (act.assignment_action_id,
3155         g_agg_active_start_eid)), hr_api.g_sot) agg_active_start,
3156     nvl(fnd_date.canonical_to_date(pay_gb_eoy_archive.get_arch_str
3157        (act.assignment_action_id,
3158         g_agg_active_end_eid)), hr_api.g_eot) agg_active_end
3159   FROM  pay_assignment_actions act
3160   WHERE act.assignment_action_id = p_asgactid;
3161   --
3162    CURSOR get_asg_active_range(p_asg_id NUMBER, p_tax_ref VARCHAR2) IS
3163    SELECT min(paaf.effective_start_date) min_active, max(paaf.effective_end_date) max_active
3164    FROM   per_all_assignments_f paaf,
3165           per_assignment_status_types past,
3166           pay_all_payrolls_f papf,
3167           hr_soft_coding_keyflex flex
3168    WHERE  paaf.assignment_id = p_asg_id
3169    AND    paaf.assignment_status_type_id = past.assignment_status_type_id
3170    AND    past.per_system_status in ('ACTIVE_ASSIGN', 'SUSP_ASSIGN')
3171    AND    paaf.payroll_id = papf.payroll_id
3172    AND    paaf.effective_start_date BETWEEN papf.effective_start_date and papf.effective_end_date
3173    AND    papf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
3174    AND    flex.segment1 = p_tax_ref;
3175    --
3176   -- Bug fix for 3699865 - this cursor will retrieve the final process date of the assignment
3177   cursor csr_asg_last_process_date(p_assid NUMBER) IS
3178   SELECT distinct greatest(ppos.final_process_date,ppos.last_standard_process_date)
3179   FROM   per_all_assignments_f  paaf,
3180          per_periods_of_service ppos
3181   WHERE  paaf.assignment_id = p_assid
3182   AND    paaf.period_of_service_id = ppos.period_of_service_id;
3183   --
3184   cursor csr_basic_asg_info (p_assid NUMBER, p_eff_end_date DATE) IS
3185   SELECT  ass.payroll_id,
3186           ass.assignment_number,
3187           ass.person_id,
3188           ass.organization_id,
3189           ass.location_id,
3190           ass.people_group_id,
3191           ass.period_of_service_id
3192   FROM  per_all_assignments_f        ass
3193   WHERE ass.assignment_id      = p_assid
3194   AND   ass.effective_end_date = p_eff_end_date;
3195   --
3196   -- 1778139. The asg may have been terminated during the
3197   -- eoy run. find the latest asg before the archived end date
3198   --
3199   cursor csr_basic_inf_term (p_assid NUMBER, p_eff_end_date DATE) IS
3200   SELECT  ass.effective_end_date,
3201           ass.payroll_id,
3202           ass.assignment_number,
3203           ass.person_id,
3204           ass.organization_id,
3205           ass.location_id,
3206           ass.people_group_id,
3207           ass.period_of_service_id
3208   FROM  per_all_assignments_f        ass
3209   WHERE ass.assignment_id      = p_assid
3210   AND   ass.effective_end_date < p_eff_end_date
3211   ORDER BY ass.effective_end_date desc;
3212 
3213   --
3214   -- 1778139. The asg may have had a new update previous to its
3215   -- end date, which deletes future changes. Get the current row.
3216   --
3217   cursor csr_basic_inf_current(p_assid NUMBER, p_eff_end_date DATE) IS
3218   SELECT  ass.effective_end_date,
3219           ass.payroll_id,
3220           ass.assignment_number,
3221           ass.person_id,
3222           ass.organization_id,
3223           ass.location_id,
3224           ass.people_group_id,
3225           ass.period_of_service_id
3226   FROM  per_all_assignments_f        ass
3227   WHERE ass.assignment_id      = p_assid
3228   AND   p_eff_end_date BETWEEN
3229            ass.effective_start_date AND ass.effective_end_date;
3230   --
3231   cursor csr_asg_start(p_asg_id NUMBER, p_asg_end DATE,
3232                        p_start_year DATE, p_end_year DATE) IS
3233   SELECT max(ass.effective_start_date)
3234   FROM per_all_assignments_f  ass
3235       ,pay_all_payrolls_f         nroll
3236       ,hr_soft_coding_keyflex flex
3237       ,per_all_assignments_f      pass
3238       ,pay_all_payrolls_f         proll
3239       ,hr_soft_coding_keyflex pflex
3240   WHERE ass.assignment_id = p_asg_id
3241   AND   ass.effective_start_date < p_asg_end
3242   AND   nroll.payroll_id = ass.payroll_id
3246   AND   ass.assignment_id = pass.assignment_id
3243   AND   ass.effective_start_date BETWEEN
3244           nroll.effective_start_date AND nroll.effective_end_date
3245   AND   nroll.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
3247   AND   pass.effective_end_date = (ass.effective_start_date - 1)
3248   AND   pass.effective_end_date >=
3249           fnd_date.canonical_to_date(pay_gb_eoy_archive.get_cached_value
3250             (g_payroll_action_id, 'X_START_YEAR', to_char(pass.payroll_id)))
3251   AND   pass.effective_start_date <=
3252           fnd_date.canonical_to_date(pay_gb_eoy_archive.get_cached_value
3253             (g_payroll_action_id, 'X_END_YEAR', to_char(pass.payroll_id)))
3254   AND   proll.payroll_id = pass.payroll_id
3255   AND   pass.payroll_id <> ass.payroll_id
3256   AND   ass.effective_start_date BETWEEN
3257           proll.effective_start_date AND proll.effective_end_date
3258   AND   proll.soft_coding_keyflex_id = pflex.soft_coding_keyflex_id
3259   AND   flex.segment1 <> pflex.segment1;
3260   --
3261   cursor csr_termination(p_service_id NUMBER, p_asg_end DATE) IS
3262   SELECT actual_termination_date , last_standard_process_date, 'L' termination_type
3263   FROM   per_periods_of_service pos
3264   WHERE  pos.period_of_service_id = p_service_id
3265   AND    pos.actual_termination_date IS NOT NULL
3266   AND    pos.actual_termination_date
3267                <= least(p_asg_end,g_end_year);
3268   --
3269   CURSOR get_week_53_start(p_payroll_id NUMBER) IS
3270   SELECT start_date
3271   FROM   per_time_periods ptp
3272   WHERE  payroll_id = p_payroll_id
3273   AND    regular_payment_date BETWEEN g_start_year AND g_end_year
3274   AND    period_num = l_payroll_max_period_number;
3275   --
3276   -- Now checks the time period from the action against the
3277   -- statutory start and end date for the year.
3278   -- 2166991. Retrieve further action types, with the addition of
3279   -- reVersal types, include actions that have been reversed
3280   -- due to Master Actions not being interlocked (removed
3281   -- interlock subquery). NB you cannot reverse a payrun
3282   -- after an EOY run, (business rule).
3283   --
3284   cursor csr_last_action(p_asgid NUMBER, p_asg_start DATE,
3285                          p_asg_end DATE, p_start_year DATE,
3286                          p_end_year DATE, p_tax_ref_xfer VARCHAR2) IS
3287             SELECT /*+ USE_NL(paa, pact, ptp) */
3288                     to_number(substr(max(lpad(paa.action_sequence,15,'0')||
3289                               paa.assignment_action_id),16)),
3290             max(pact.effective_date) effective_date
3291             FROM    pay_assignment_actions paa,
3292                     pay_payroll_actions    pact,
3293                     per_time_periods ptp
3294             WHERE   paa.assignment_id = p_asgid
3295             AND     paa.payroll_action_id = pact.payroll_action_id
3296             AND     pact.time_period_id = ptp.time_period_id
3297             AND     pact.action_type IN ('Q','R','B','I','V')
3298             AND     paa.action_status = 'C'
3299             -- Added decode below for 4318185
3300             AND     pact.effective_date <= decode(p_tax_ref_xfer, 'Y', p_asg_end, pact.effective_date)
3301             AND     ptp.regular_payment_date
3302                   BETWEEN nvl(p_asg_start, p_start_year) AND p_end_year;
3303   --
3304   -- Retrieve action type info from assignment ation
3305   --
3306   cursor csr_action_details(p_assignment_action_id NUMBER) IS
3307   SELECT pact.action_type
3308   from pay_payroll_actions pact,
3309        pay_assignment_actions act
3310   where act.assignment_action_id = p_assignment_action_id
3311   and act.payroll_action_id = pact.payroll_action_id;
3312   --
3313   cursor csr_person_info(p_person_id NUMBER) IS
3314   SELECT substr(last_name, 1,35) last_name,
3315          substr(first_name, 1,35) first_name,
3316          substr(middle_names,1,35) middle_names,
3317          date_of_birth,  title,
3318          substr(expense_check_send_to_address,1,1) expense_check_send_to_address,
3319          substr(national_identifier,1,9) national_identifier,
3320          substr(sex,1,1) sex ,
3321          decode(substr(per_information4,1,1),'Y','P',' ') pensioner_indicator,
3322          decode(per_information10,'Y','Y',NULL) agg_paye_flag,
3323          decode(per_information9,'Y','Y',NULL) multiple_asg_flag -- MII
3324   FROM  per_all_people_f per
3325   WHERE per.person_id = p_person_id
3326     AND g_end_year BETWEEN per.effective_start_date
3327                        AND per.effective_end_date;
3328   --
3329   cursor csr_director(p_person_id NUMBER) IS
3330   SELECT 'D'
3331   FROM dual
3332   WHERE EXISTS (SELECT '1'
3333                 FROM per_all_people_f per
3334                 WHERE p_person_id           = per.person_id
3335                   AND per.effective_start_date    <= g_end_year
3336                   AND per.effective_end_date      >= g_start_year
3337                   AND substr(per_information2,1,1) = 'Y')
3338  AND EXISTS (SELECT '1'
3339              FROM pay_run_result_values prrv
3340              WHERE input_value_id = g_process_type_id
3341              AND result_value in ('DY', 'DN', 'DP', 'DR', 'PY')
3342              AND run_result_id = (SELECT to_number(substr(max(lpad(to_char(act.action_sequence),15,'0')|| lpad(to_char(prr.run_result_id),19,'0')),16))
3343                   FROM pay_payroll_Actions pact,
3344                   pay_assignment_actions act,
3345                   per_all_assignments_f paf,
3346                   pay_run_results prr
3347                   WHERE pact.payroll_Action_id = act.payroll_Action_id
3351                   AND paf.person_id = p_person_id
3348                   AND pact.effective_date BETWEEN g_start_year and g_end_year
3349                   AND act.action_status = 'C'
3350                   AND act.assignment_id = paf.assignment_id
3352                   AND paf.effective_start_date <= g_end_year
3353                   AND paf.effective_end_date >=  g_start_year
3354                   AND act.assignment_action_id = prr.assignment_action_id
3355                   AND prr.element_type_id = g_ni_id
3356                   AND pact.action_type IN ('Q', 'R', 'B', 'I')
3357                   AND prr.status in ('P', 'PA')));
3358   --
3359   cursor csr_addresses(p_person_id NUMBER) IS
3360   SELECT ltrim(rtrim(pad.address_line1)) address_line1,
3361          ltrim(rtrim(pad.address_line2)) address_line2,
3362          ltrim(rtrim(pad.address_line3)) address_line3,
3363          ltrim(rtrim(pad.town_or_city)) town_or_city,
3364          substr(l.meaning,1,27) county,
3365          substr(pad.postal_code,1,8),
3366          country
3367   FROM   per_addresses pad,
3368          hr_lookups l
3369   WHERE  pad.person_id = p_person_id
3370   AND    pad.primary_flag = 'Y'
3371   AND    l.lookup_type(+) = 'GB_COUNTY'
3372   AND    l.lookup_code(+) = pad.region_1
3373   AND    sysdate BETWEEN nvl(pad.date_from, sysdate)
3374                      AND nvl(pad.date_to,   sysdate);
3375   --
3376   cursor csr_country_name(p_country_code VARCHAR2) IS
3377   SELECT substr(ftt.territory_short_name, 1, 35) country -- 4011263
3378   FROM   fnd_territories_tl ftt
3379   WHERE  ftt.territory_code = p_country_code
3380   AND    ftt.language = userenv('LANG');
3381   --
3382   -- fetch the scon balances for NI F, NI G and/or NI S
3383   cursor csr_get_scon_bal(cp_l_asg_id NUMBER, cp_scon_inp_val NUMBER,
3384                           cp_cat_inp_val NUMBER, cp_element_type NUMBER) IS
3385     SELECT /*+ RULE */
3386     substr(bal.balance_name,4,1) cat_code,
3387     substr(hr_general.decode_lookup('GB_SCON',
3388       decode(substr(bal.balance_name,4,1),
3389              'F',nvl(max(decode(ev_cat.screen_entry_value,
3390                                 'F',ev_scon.screen_entry_value)),
3391                  pay_gb_eoy_archive.get_nearest_scon(
3392                  max(ev_scon.element_entry_id), cp_l_asg_id,
3393                      'F',max(pact.effective_date))),
3394              'G',nvl(max(decode(ev_cat.screen_entry_value,
3395                                 'G',ev_scon.screen_entry_value)),
3396                  pay_gb_eoy_archive.get_nearest_scon(
3397                  max(ev_scon.element_entry_id), cp_l_asg_id,
3398                      'G',max(pact.effective_date))),
3399              'S',nvl(max(decode(ev_cat.screen_entry_value,
3400                                 'S',ev_scon.screen_entry_value)),
3401                  pay_gb_eoy_archive.get_nearest_scon(
3402                  max(ev_scon.element_entry_id), cp_l_asg_id,
3403                      'S',max(pact.effective_date))),
3404              NULL)),1,9) scon,
3405     100*nvl(sum(decode(substr(bal.balance_name,6),'Able',
3406       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3407             able,
3408     100*nvl(sum(decode(substr(bal.balance_name,6),'Total',
3409       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3410             total,
3411     100*nvl(sum(decode(substr(bal.balance_name,6),'Employee',
3412       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3413             employee,
3414        --
3415        -- Bug Fix 678573 Start
3416        --
3417     100*nvl(sum(decode(substr(bal.balance_name,6),'Employer',
3418       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3419             employer,
3420        --
3421        -- Bug Fix 678573 End
3422        --
3423     100*nvl(sum(decode(substr(bal.balance_name,6),'Able ET',
3424       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3425             able_et,
3426     100*nvl(sum(decode(substr(bal.balance_name,6),'Able LEL',
3427       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3428             able_lel,
3429     100*nvl(sum(decode(substr(bal.balance_name,6),'Able UEL',
3430       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3431             able_uel,
3432     --EOY 07/08 Begin
3433     100*nvl(sum(decode(substr(bal.balance_name,6),'Able AUEL',
3434       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3435             able_auel,
3436     -- EOY 07/08 End
3437     100*nvl(sum(decode(substr(bal.balance_name,6),'Ers Rebate',
3438       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3439             ers_rebate,
3440     -- Note Ees Rebate only for F category, but zero
3441     -- retrieved in all other cases.
3442     100*nvl(sum(decode(substr(bal.balance_name,6),'Ees Rebate',
3443       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3444             ees_rebate,
3445     100*nvl(sum(decode(substr(bal.balance_name,6),'Rebate to Employee',
3446       fnd_number.canonical_to_number(target.result_value) * feed.scale,0)),0)
3447             rebate_emp
3448   FROM  pay_balance_feeds_f      feed
3449        ,pay_balance_types        bal
3450        ,pay_run_result_values    target
3451        ,pay_run_results          rr
3452        ,pay_element_entry_values_f ev_scon
3453        ,pay_element_entry_values_f ev_cat
3454        ,pay_element_entries_f    e_ni
3455        ,pay_element_links_f      el_ni
3456        ,pay_payroll_actions      pact
3460        ,per_time_periods         pptp
3457        ,pay_assignment_actions   assact
3458        ,pay_payroll_actions      bact
3459        ,per_time_periods         bptp
3461        ,pay_assignment_actions   bal_assact
3462   WHERE  bal_assact.assignment_action_id = cp_l_asg_id
3463   AND    bal_assact.payroll_action_id = bact.payroll_action_id
3464   AND    feed.balance_type_id    = bal.balance_type_id
3465   AND    bal.balance_name        LIKE 'NI%'
3466   AND    substr(bal.balance_name,4,1) IN ('F','G','S')
3467   AND    feed.input_value_id     = target.input_value_id
3468   AND    target.run_result_id    = rr.run_result_id
3469   AND    nvl(target.result_value,'0') <> '0'
3470   AND    rr.assignment_action_id = assact.assignment_action_id
3471   AND    e_ni.assignment_id      = bal_assact.assignment_id
3472   AND    ev_scon.input_value_id  +
3473              decode(ev_scon.element_entry_id,NULL,0,0) = cp_scon_inp_val
3474   AND    ev_scon.element_entry_id = e_ni.element_entry_id
3475   AND    ev_cat.input_value_id  +
3476              decode(ev_cat.element_entry_id,NULL,0,0) = cp_cat_inp_val
3477   AND    ev_cat.element_entry_id = e_ni.element_entry_id
3478   AND    el_ni.element_link_id    = e_ni.element_link_id
3479   AND    el_ni.element_type_id    = cp_element_type
3480   AND    pact.effective_date BETWEEN
3481                 e_ni.effective_start_date AND e_ni.effective_end_date
3482   AND    pact.effective_date BETWEEN
3483                 el_ni.effective_start_date AND el_ni.effective_end_date
3484   AND    pact.effective_date BETWEEN
3485                 ev_scon.effective_start_date AND ev_scon.effective_end_date
3486   AND    pact.effective_date BETWEEN
3487                 ev_cat.effective_start_date AND ev_cat.effective_end_date
3488   AND    assact.payroll_action_id = pact.payroll_action_id
3489   AND    pact.effective_date BETWEEN
3490                 feed.effective_start_date AND feed.effective_end_date
3491   AND    rr.status IN ('P','PA')
3492   AND    bptp.time_period_id = bact.time_period_id
3493   AND    pptp.time_period_id = pact.time_period_id
3494   AND    pptp.regular_payment_date >= /* fin year start */
3495                ( to_date('06-04-' || to_char( to_number(
3496                  to_char( bptp.regular_payment_date,'YYYY'))
3497           +  decode(sign( bptp.regular_payment_date - to_date('06-04-'
3498               || to_char(bptp.regular_payment_date,'YYYY'),'DD-MM-YYYY')),
3499            -1,-1,0)),'DD-MM-YYYY'))
3500   AND    pact.effective_date >=
3501        /* find the latest td payroll transfer date - compare each of the */
3502        /* assignment rows with its predecessor looking for the payroll   */
3503        /* that had a different tax district at that date */
3504         ( SELECT nvl(max(ass.effective_start_date),
3505           to_date('01-01-0001','DD-MM-YYYY'))
3506           FROM per_all_assignments_f  ass
3507               ,pay_all_payrolls_f         nroll
3508               ,hr_soft_coding_keyflex flex
3509               ,per_all_assignments_f  pass  /* previous assignment */
3510               ,pay_all_payrolls_f         proll
3511               ,hr_soft_coding_keyflex pflex
3512           WHERE ass.assignment_id = bal_assact.assignment_id
3513             AND nroll.payroll_id = ass.payroll_id
3514             AND ass.effective_start_date BETWEEN
3515                       nroll.effective_start_date AND nroll.effective_end_date
3516             AND nroll.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
3517             AND ass.assignment_id = pass.assignment_id
3518             AND pass.effective_end_date = (ass.effective_start_date - 1)
3519             AND ass.effective_start_date <= bact.effective_date
3520             AND proll.payroll_id = pass.payroll_id
3521             AND ass.effective_start_date BETWEEN
3522                       proll.effective_start_date AND proll.effective_end_date
3523             AND proll.soft_coding_keyflex_id = pflex.soft_coding_keyflex_id
3524             AND ass.payroll_id <> pass.payroll_id
3525             AND flex.segment1 <> pflex.segment1)
3526   AND    assact.action_sequence <= bal_assact.action_sequence
3527   AND    assact.assignment_id = bal_assact.assignment_id
3528   GROUP BY ev_scon.screen_entry_value, substr(bal.balance_name,4,1)
3529   ORDER BY ev_scon.screen_entry_value, substr(bal.balance_name,4,1);
3530   --
3531   cursor csr_current_cat(p_asgid NUMBER, l_asg_end DATE,
3532                          p_end_year DATE) IS
3533   SELECT  v.screen_entry_value ni_cat,
3534           substr(hr_general.decode_lookup('GB_SCON',scon.screen_entry_value)
3535                  ,1,9) ni_scon
3536   FROM    pay_element_entries_f e,
3537           pay_element_entry_values_f v,
3538           pay_element_entry_values_f  scon,
3539           pay_element_links_f link
3540   WHERE   e.assignment_id = p_asgid
3541     AND   v.input_value_id + 0 = g_category_input_id
3542     AND   v.effective_start_date = scon.effective_start_date
3543     AND   v.effective_end_date   = scon.effective_end_date
3544     AND   v.element_entry_id = scon.element_entry_id
3545     AND   scon.input_value_id + 0  = g_scon_input_id
3546     AND   link.element_type_id = g_ni_id
3547     AND   e.element_link_id = link.element_link_id
3548     AND   e.element_entry_id = v.element_entry_id
3549     AND   least(l_asg_end,p_end_year)
3550             BETWEEN link.effective_start_date AND link.effective_end_date
3551     AND   least(l_asg_end,p_end_year)
3552             BETWEEN e.effective_start_date AND e.effective_end_date
3553     AND   least(l_asg_end,p_end_year)
3554             BETWEEN v.effective_start_date AND v.effective_end_date
3555     AND   least(l_asg_end,p_end_year)
3556             BETWEEN scon.effective_start_date AND scon.effective_end_date;
3557   --
3561   WHERE   r.element_type_id = g_paye_details_id
3558   cursor csr_tax_last_run(p_last_asg_action_id NUMBER) IS
3559   SELECT  to_number(substr(max(source_type||lpad(to_char(run_result_id), 19, '0')),2)) -- gets indirect results if present else gets entry results
3560   FROM    pay_run_results r
3562     AND   r.status IN ('P', 'PA')
3563     AND   r.assignment_action_id = p_last_asg_action_id;
3564   --
3565   cursor csr_tax_last_paye_run(p_last_asg_action_id NUMBER) IS
3566   SELECT  to_number(substr(max(source_type||lpad(to_char(run_result_id), 19, '0')),2)) -- gets indirect results if present else gets entry results
3567   FROM    pay_run_results r
3568   WHERE   r.element_type_id = g_paye_element_id
3569     AND   r.status IN ('P', 'PA', 'O') -- add overridden for SR 4310794.996
3570     AND   r.assignment_action_id = p_last_asg_action_id;
3571   --
3572   cursor csr_tax_latest_run(p_assignment_id NUMBER,
3573                             p_asg_last_eff_date DATE) IS
3574   -- bug 889323 ensure the last action included PAYE Details result
3575   -- bug 1236784 ignore reversal/reversed runs
3576 SELECT /*+ ORDERED INDEX (assact2 PAY_ASSIGNMENT_ACTIONS_N51,
3577                           pact PAY_PAYROLL_ACTIONS_PK,
3578                           r2 PAY_RUN_RESULTS_N50)
3579            USE_NL(assact2, pact, r2) */
3580            to_number(substr(max(lpad(to_char(assact2.action_sequence),15,'0')
3581                 ||r2.source_type||
3582                 lpad(to_char(r2.run_result_id),19,'0')),17)) rr_id,
3583            fnd_date.canonical_to_date(substr(max(lpad(to_char(assact2.action_sequence),15,'0')||
3584                  fnd_date.date_to_canonical(pact.effective_date)),16)) eff_date
3585            FROM    pay_assignment_actions assact2,
3586                    pay_payroll_actions pact,
3587                    pay_run_results r2
3588            WHERE   assact2.assignment_id = p_assignment_id
3589            AND     r2.element_type_id+0 = g_paye_details_id
3590            AND     r2.assignment_action_id = assact2.assignment_action_id
3591            AND     r2.status IN ('P', 'PA')
3592            AND     pact.payroll_action_id = assact2.payroll_action_id
3593            AND     pact.action_type IN ( 'Q','R','B','I')
3594            AND     assact2.action_status = 'C'
3595            AND     pact.effective_date BETWEEN
3596                    g_start_year AND g_end_year
3597 /* Bug 4278570       fnd_date.canonical_to_date
3598                        (pay_gb_eoy_archive.get_arch_str(
3599                         g_payroll_action_id,
3600                         g_payroll_start_year_eid,
3601                         to_char(pact.payroll_id)))
3602                    AND fnd_date.canonical_to_date
3603                          (pay_gb_eoy_archive.get_arch_str(
3604                           g_payroll_action_id,
3605                           g_payroll_end_year_eid,
3606                           to_char(pact.payroll_id)))
3607 */
3608           AND     pact.effective_date <= p_asg_last_eff_date
3609            AND NOT EXISTS(
3610               SELECT '1'
3611               FROM  pay_action_interlocks pai,
3612                     pay_assignment_actions assact3,
3613                     pay_payroll_actions pact3
3614               WHERE   pai.locked_action_id = assact2.assignment_action_id
3615               AND     pai.locking_action_id = assact3.assignment_action_id
3616               AND     pact3.payroll_action_id = assact3.payroll_action_id
3617               AND     pact3.action_type = 'V'
3618               AND     assact3.action_status = 'C');
3619   --
3620   cursor csr_tax_latest_paye_run(p_assignment_id NUMBER,
3621                             p_asg_last_eff_date DATE) IS
3622 SELECT /*+ ORDERED INDEX (assact2 PAY_ASSIGNMENT_ACTIONS_N51,
3623                           pact PAY_PAYROLL_ACTIONS_PK,
3624                           r2 PAY_RUN_RESULTS_N50)
3625            USE_NL(assact2, pact, r2) */
3626            to_number(substr(max(lpad(to_char(assact2.action_sequence),15,'0')
3627                  ||r2.source_type||
3628                  lpad(to_char(r2.run_result_id),19,'0')),17)) rr_id,
3629            fnd_date.canonical_to_date(substr(max(lpad(to_char(assact2.action_sequence),15,'0')||
3630                  fnd_date.date_to_canonical(pact.effective_date)),16)) eff_date
3631 --           to_number(substr(max(lpad(assact2.action_sequence,15,'0')||
3632 --                             r2.run_result_id),16))
3633            FROM    pay_assignment_actions assact2,
3634                    pay_payroll_actions pact,
3635                    pay_run_results r2
3636            WHERE   assact2.assignment_id = p_assignment_id
3637            AND     r2.element_type_id+0 = g_paye_element_id
3638            AND     r2.assignment_action_id = assact2.assignment_action_id
3639            AND     r2.status IN ('P', 'PA', 'O') -- add overridden for SR 4310794.996
3640            AND     pact.payroll_action_id = assact2.payroll_action_id
3641            AND     pact.action_type IN ( 'Q','R','B','I')
3642            AND     assact2.action_status = 'C'
3643            AND     pact.effective_date BETWEEN
3644                    g_start_year AND g_end_year
3645 /* Bug 4278570       fnd_date.canonical_to_date
3646                        (pay_gb_eoy_archive.get_arch_str(
3647                         g_payroll_action_id,
3648                         g_payroll_start_year_eid,
3649                         to_char(pact.payroll_id)))
3650                    AND fnd_date.canonical_to_date
3651                          (pay_gb_eoy_archive.get_arch_str(
3652                           g_payroll_action_id,
3653                           g_payroll_end_year_eid,
3654                           to_char(pact.payroll_id)))
3655 */
3659               FROM  pay_action_interlocks pai,
3656           AND     pact.effective_date <= p_asg_last_eff_date
3657            AND NOT EXISTS(
3658               SELECT '1'
3660                     pay_assignment_actions assact3,
3661                     pay_payroll_actions pact3
3662               WHERE   pai.locked_action_id = assact2.assignment_action_id
3663               AND     pai.locking_action_id = assact3.assignment_action_id
3664               AND     pact3.payroll_action_id = assact3.payroll_action_id
3665               AND     pact3.action_type = 'V'
3666               AND     assact3.action_status = 'C');
3667   --
3668   cursor csr_tax_details_entry(p_assignment_id NUMBER,
3669                                p_asg_end DATE, p_end_year DATE,
3670                                p_update_recurring VARCHAR2) IS
3671   SELECT  max(decode(iv.name,'Tax Code',screen_entry_value,NULL)) tax_code,
3672           max(decode(iv.name,'Tax Basis',screen_entry_value,NULL)) tax_basis,
3673           100 * max(decode(iv.name,'Pay Previous',
3674                   fnd_number.canonical_to_number(screen_entry_value),NULL))
3675                                                                 pay_previous,
3676           100 * max(decode(iv.name,'Tax Previous',
3677                   fnd_number.canonical_to_number(screen_entry_value),NULL))
3678                                                                 tax_previous
3679   FROM  pay_element_entries_f e,
3680         pay_element_entry_values_f v,
3681         pay_input_values_f iv,
3682         pay_element_links_f link
3683   WHERE e.assignment_id = p_assignment_id
3684   AND   link.element_type_id = g_paye_details_id
3685   AND   e.element_link_id = link.element_link_id
3686   AND   e.element_entry_id = v.element_entry_id
3687   AND   iv.input_value_id = v.input_value_id
3688   AND   (e.updating_action_id IS NOT NULL OR p_update_recurring = 'N')
3689   AND   least(p_asg_end,p_end_year)
3690           BETWEEN link.effective_start_date AND link.effective_end_date
3691   AND   least(p_asg_end,p_end_year)
3692           BETWEEN e.effective_start_date AND e.effective_end_date
3693   AND   least(p_asg_end,p_end_year)
3694           BETWEEN iv.effective_start_date AND iv.effective_end_date
3695   AND   least(p_asg_end,p_end_year)
3696           BETWEEN v.effective_start_date AND v.effective_end_date;
3697   --
3698   cursor csr_tax_details_result(p_tax_run_result_id NUMBER) IS
3699   SELECT  max(decode(name,'Tax Code',result_value,NULL)) tax_code,
3700           max(decode(name,'Tax Basis',result_value,NULL)) tax_basis,
3701           100 * to_number(max(decode(name,'Pay Previous',
3702                   fnd_number.canonical_to_number(result_value),NULL)))
3703                                                                 pay_previous,
3704           100 * to_number(max(decode(name,'Tax Previous',
3705                   fnd_number.canonical_to_number(result_value),NULL)))
3706                                                                 tax_previous
3707   FROM pay_input_values_f v,
3708        pay_run_result_values rrv
3709   WHERE rrv.run_result_id = p_tax_run_result_id
3710     AND v.input_value_id = rrv.input_value_id
3711     AND v.element_type_id = g_paye_details_id;
3712   --
3713   cursor csr_tax_paye_result(p_tax_run_result_id NUMBER) IS
3714   SELECT  max(decode(name,'Tax Code',result_value,NULL)) tax_code,
3715           max(decode(name,'Tax Basis',result_value,NULL)) tax_basis,
3716           100 * to_number(max(decode(name,'Pay Previous',
3717                   fnd_number.canonical_to_number(result_value),NULL)))
3718                                                                 pay_previous,
3719           100 * to_number(max(decode(name,'Tax Previous',
3720                   fnd_number.canonical_to_number(result_value),NULL)))
3721                                                                 tax_previous
3722   FROM pay_input_values_f v,
3723        pay_run_result_values rrv
3724   WHERE rrv.run_result_id = p_tax_run_result_id
3725     AND v.input_value_id = rrv.input_value_id
3726     AND v.element_type_id = g_paye_element_id;
3727   --
3728   CURSOR csr_get_invalid_multiple_asg(p_person_id  NUMBER,
3729                                   p_payroll_id NUMBER,
3730                                   p_year_start DATE,
3731                                   p_year_end   DATE,
3732                                   p_tax_ref    VARCHAR2) IS
3733   -- fetch any asg for the person within the same tax reference
3734   -- but on a payroll with a different (or null) permit.
3735   SELECT 1
3736   FROM  per_all_assignments_f asg
3737   WHERE asg.person_id = p_person_id
3738   AND   asg.effective_start_date < p_year_end
3739   AND   asg.effective_end_date >= p_year_start
3740   AND   asg.payroll_id <> p_payroll_id
3741   AND   p_tax_ref = pay_gb_eoy_archive.get_cached_value(
3742                       g_payroll_action_id,'X_TAX_REFERENCE_NUMBER',
3743                       to_char(asg.payroll_id))
3744   AND   g_permit_number <> nvl(pay_gb_eoy_archive.get_cached_value(
3745                              g_payroll_action_id,'X_PERMIT_NUMBER',
3746                              to_char(asg.payroll_id)),'?');
3747   --
3748   -- Start of BUG 5671777-1
3749   --
3750   -- fetch final payment date for the give assignment action using
3751   -- X_P45_FINAL_PAYMENT_ACTION user entity
3752   --
3753   CURSOR csr_get_final_payment_date(c_asg_action_id NUMBER) IS
3754   SELECT ppa.effective_date
3755   FROM   ff_user_entities fue,
3756          ff_archive_items fai,
3757          pay_assignment_actions paa,
3758          pay_payroll_actions ppa
3759   WHERE  fue.user_entity_name = 'X_P45_FINAL_PAYMENT_ACTION'
3760   AND    fue.user_entity_id = fai.user_entity_id
3764 
3761   AND    fai.context1 = c_asg_action_id
3762   AND    fai.value = paa.assignment_action_id
3763   AND    paa.payroll_action_id = ppa.payroll_action_id;
3765   --
3766   -- End of BUG 5671777-1
3767   --
3768   PROCEDURE archive_asg_info(p_user_entity_id NUMBER,
3769                              p_value VARCHAR2,
3770                              p_actid          NUMBER DEFAULT NULL) IS
3771     l_proc             CONSTANT VARCHAR2(40):= g_package||'archive_asg_info';
3772   BEGIN
3773     IF p_value IS NOT NULL THEN
3774       hr_utility.set_location(l_proc||' '||p_user_entity_id,10);
3775       ff_archive_api.create_archive_item
3776         (p_archive_item_id  => l_archive_item_id,
3777          p_user_entity_id   => p_user_entity_id,
3778          p_archive_value    => p_value,
3779          p_action_id        => nvl(p_actid,p_assactid),
3780          p_legislation_code => 'GB',
3781          p_object_version_number => l_ovn,
3782          p_some_warning     => l_some_warning);
3783     END IF;
3784   END archive_asg_info;
3785   --
3786   PROCEDURE archive_ni_value(p_user_entity_id NUMBER,
3787                              p_value          VARCHAR2,
3788                              p_reportable     NUMBER,
3789                              p_ni_cat         VARCHAR2,
3790                              p_scon           VARCHAR2 DEFAULT NULL,
3791                              p_actid          NUMBER DEFAULT NULL) IS
3792   -- Procedure used to archive an individual NI balance value for a given
3793   -- NI Category and (where necessary) SCON.
3794   --
3795   -- The p_reportable parameter indicates that the value is reportable ie.
3796   -- to be included in the P35 tape submission.  This is necessary for
3797   -- Multiple Assignment processing to indicate whether a set of values is
3798   -- for a single (non-multi) asg, a person level total (against the
3799   -- primary assignment) or a value which contributes to a person level
3800   -- total (aka a source balance).  The values view will display this flag
3801   -- using the char code used in the previous EoY implementation.
3802   -- The expected values are as follows:
3803   --
3804   --    p_reportable  View returns  Meaning
3805   --    ------------  ------------  ----------------------------------------
3806   --               0  N             Not reportable - source balance
3807   --               1  Y             Reportable - non-multi asg value
3808   --               2  M             Reportable - multi asg person level Tot
3809   --
3810   -- This is called from the archive_ni_values procedure and part III of
3811   -- the Multiple Assignment Logic.  Calls to this
3812   -- procedure should not be made from any other places unless the Multiple
3813   -- Assignment Logic contained within the two above procedures is to be
3814   -- by-passed.
3815   -- p_actid may be given to archive the value against a different assignment
3816   -- action to that currently being processed.  This is utilised by the
3817   -- Multiple Assignment Logic Pt. III
3818   BEGIN
3819     hr_utility.trace('Entering pay_gb_eoy_archive.archive_ni_values');
3820     hr_utility.trace('p_user_entity_id='||p_user_entity_id);
3821     hr_utility.trace('p_value='||p_value);
3822     hr_utility.trace('p_reportable='||p_reportable);
3823     hr_utility.trace('p_ni_cat='||p_ni_cat);
3824     hr_utility.trace('p_scon='||p_scon);
3825     hr_utility.trace('p_actid='||p_actid);
3826     hr_utility.trace('p_assactid='||p_assactid);
3827     IF p_value IS NOT NULL THEN
3828       IF p_scon IS NOT NULL THEN
3829         ff_archive_api.create_archive_item
3830           (p_archive_item_id  => l_archive_item_id,
3831            p_user_entity_id   => p_user_entity_id,
3832            p_archive_value    => p_value,
3833            p_action_id        => nvl(p_actid,p_assactid),
3834            p_legislation_code => 'GB',
3835            p_object_version_number => l_ovn,
3836            p_context_name1    => 'TAX_UNIT_ID',
3837            p_context1         => to_char(p_reportable),
3838            p_context_name2    => 'TAX_GROUP',
3839            p_context2         => p_ni_cat,
3840            p_context_name3    => 'SOURCE_TEXT',
3841            p_context3         => p_scon,
3842            p_some_warning     => l_some_warning);
3843       ELSE
3844         ff_archive_api.create_archive_item
3845           (p_archive_item_id  => l_archive_item_id,
3846            p_user_entity_id   => p_user_entity_id,
3847            p_archive_value    => p_value,
3848            p_action_id        => nvl(p_actid,p_assactid),
3849            p_legislation_code => 'GB',
3850            p_object_version_number => l_ovn,
3851            p_context_name1    => 'TAX_UNIT_ID',
3852            p_context1         => to_char(p_reportable),
3853            p_context_name2    => 'TAX_GROUP',
3854            p_context2         => p_ni_cat,
3855            p_some_warning     => l_some_warning);
3856       END IF;
3857       --
3858       -- Set l_reportable_ni_archived variable to indicate
3859       -- a reportable non zero NI value has been archived, this will
3860       -- be used to archive X_REPORTABLE_NI flag to help decide whether
3861       -- the assignment should be reported on P14 or reconciliation
3862       -- report
3863       IF p_user_entity_id NOT IN (g_ni_scon_refund_eid, g_ni_refund_eid) THEN -- to ensure p_value is number, Bugs 4918852, 4907567
3864          IF p_reportable > 0 and p_value <> 0 THEN
3865             l_reportable_ni_archived := TRUE;
3866          END IF;
3867       END IF;
3868     END IF;
3869   END archive_ni_value;
3870   --
3871   PROCEDURE empty_masg_cache IS
3872   BEGIN
3876     g_masg_active_end         := hr_api.g_eot;
3873     g_masg_person_id          := hr_api.g_number;
3874     g_masg_period_of_service_id := hr_api.g_number; -- Bug 3784871
3875     g_masg_active_start       := hr_api.g_sot;
3877     g_masg_tax_ref_num        := substr(hr_api.g_varchar2,1,10); -- 4011263
3878     g_max_gross_pay           := NULL;
3879     g_primary_action          := NULL;
3880     g_min_assignment_id       := NULL;
3881     g_ni_balance_totals       := g_empty_ni_balance_totals;
3882     g_agg_balance_totals      := g_zero_balance_totals;
3883     g_asg_actions             := g_empty_asg_actions;
3884     g_num_actions             := 0;
3885     g_has_non_extracted_masgs := false;
3886   END empty_masg_cache;
3887   ------------------------------------------------------------------
3888   -- PROCEDURE store_agg_values
3889   -- Store a set of balance values, for the Aggregated PAYE
3890   -- Assignments in the global table. The table is emptied
3891   -- by empty_masg_cache after MA processing.
3892   -- This is in step with NI Multi Asg Logic Part II
3893   ------------------------------------------------------------------
3894   PROCEDURE store_agg_values(p_smp        NUMBER DEFAULT NULL,
3895                              p_ssp        NUMBER DEFAULT NULL,
3896                              p_sap        NUMBER DEFAULT NULL,
3897                              p_spp_adopt  NUMBER DEFAULT NULL,
3898                              p_spp_birth  NUMBER DEFAULT NULL,
3899                              p_gross      NUMBER DEFAULT NULL,
3900                              p_notional   NUMBER DEFAULT NULL,
3901                              p_paye       NUMBER DEFAULT NULL,
3902                              p_super      NUMBER DEFAULT NULL,
3903                              p_widow      NUMBER DEFAULT NULL,
3904                              p_taxable    NUMBER DEFAULT NULL,
3905                              p_student_ln NUMBER DEFAULT NULL,
3906                              p_tax_credit NUMBER DEFAULT NULL,
3907                              p_ni_arrears NUMBER DEFAULT NULL) IS
3908   --
3909   l_proc    CONSTANT VARCHAR2(40):= g_package||'archive_agg_values';
3910   --
3911   BEGIN
3912     --
3913     hr_utility.set_location(l_proc,10);
3914     BEGIN
3915        g_agg_balance_totals.smp :=
3916                   g_agg_balance_totals.smp + nvl(p_smp,0);
3917        g_agg_balance_totals.ssp :=
3918                   g_agg_balance_totals.ssp + nvl(p_ssp,0);
3919        g_agg_balance_totals.sap :=
3920                   g_agg_balance_totals.sap + nvl(p_sap,0);
3921        g_agg_balance_totals.spp_adopt :=
3922                   g_agg_balance_totals.spp_adopt + nvl(p_spp_adopt,0);
3923        g_agg_balance_totals.spp_birth :=
3924                   g_agg_balance_totals.spp_birth + nvl(p_spp_birth,0);
3925        g_agg_balance_totals.gross_pay :=
3926                   g_agg_balance_totals.gross_pay + nvl(p_gross,0);
3927        g_agg_balance_totals.notional :=
3928                   g_agg_balance_totals.notional + nvl(p_notional,0);
3929        g_agg_balance_totals.paye :=
3930                   g_agg_balance_totals.paye + nvl(p_paye,0);
3931        g_agg_balance_totals.superann :=
3932                   g_agg_balance_totals.superann + nvl(p_super,0);
3933        g_agg_balance_totals.widows :=
3934                   g_agg_balance_totals.widows + nvl(p_widow,0);
3935        g_agg_balance_totals.taxable :=
3936                   g_agg_balance_totals.taxable + nvl(p_taxable,0);
3937        g_agg_balance_totals.student_ln :=
3938                   g_agg_balance_totals.student_ln + nvl(p_student_ln,0);
3939        g_agg_balance_totals.ni_arrears :=
3940                   g_agg_balance_totals.ni_arrears + nvl(p_ni_arrears,0);
3941        hr_utility.set_location(l_proc,20);
3942     EXCEPTION WHEN NO_DATA_FOUND THEN
3943        hr_utility.set_location(l_proc,30);
3944        -- The PLSQL table is empty so first row.
3945        g_agg_balance_totals.smp := nvl(p_smp,0);
3946        g_agg_balance_totals.ssp := nvl(p_ssp,0);
3947        g_agg_balance_totals.sap := nvl(p_sap,0);
3948        g_agg_balance_totals.spp_adopt := nvl(p_spp_adopt,0);
3949        g_agg_balance_totals.spp_birth := nvl(p_spp_birth,0);
3950        g_agg_balance_totals.gross_pay := nvl(p_gross,0);
3951        g_agg_balance_totals.notional := nvl(p_notional,0);
3952        g_agg_balance_totals.paye := nvl(p_paye,0);
3953        g_agg_balance_totals.superann := nvl(p_super,0);
3954        g_agg_balance_totals.widows := nvl(p_widow,0);
3955        g_agg_balance_totals.taxable := nvl(p_taxable,0);
3956        g_agg_balance_totals.student_ln := nvl(p_student_ln,0);
3957        g_agg_balance_totals.ni_arrears := nvl(p_ni_arrears,0);
3958     END; -- store balance values in global table.
3959     --
3960     hr_utility.set_location(l_proc,40);
3961   --
3962   END store_agg_values;
3963   ------------------------------------------------------------------------
3964   PROCEDURE archive_ni_values(p_ni_cat        VARCHAR2,
3965                               p_tot_contribs  NUMBER,
3966                               p_earnings      NUMBER DEFAULT NULL,
3967                               p_ees_contribs  NUMBER DEFAULT NULL,
3968                               p_ni_able_lel   NUMBER DEFAULT NULL,
3969                               p_ni_able_et    NUMBER DEFAULT NULL,
3970                               p_ni_able_uel   NUMBER DEFAULT NULL,
3971 			      p_ni_able_auel  NUMBER DEFAULT NULL, -- EOY 07/08
3972                               p_ers_rebate    NUMBER DEFAULT NULL,
3973                               p_ees_rebate    NUMBER DEFAULT NULL,
3974                               p_scon          VARCHAR2 DEFAULT NULL) IS
3978   --
3975   -- Procedure used to archive a 'row' of balance values for a given
3976   -- NI Category and (where necessary) SCON.
3977   -- It archives the values individually by calling archive_ni_value.
3979   -- This procedure performs NI Multiple Assignment Logic (Pt. II)
3980   -- which will archive a reportable set of values if the asg is a single asg.
3981   -- If the the asg is a multi-asg, a non-reportable set of values will be
3982   -- archived and the values added to those in the cache (these are later
3983   -- archived against the primary asg in Pt. III)
3984     --
3985     l_proc             CONSTANT VARCHAR2(40):= g_package||'archive_ni_values';
3986     --
3987     l_report_values          NUMBER(1);
3988     l_refund_flag            VARCHAR2(1);
3989     l_index1                 binary_integer;
3990     --
3991   BEGIN
3992     --
3993     hr_utility.set_location('Entering: '||l_proc,1);
3994     --
3995     IF l_multiple_asg_flag = 'Y' THEN
3996       -- Do Multiple Assignment Logic Part II
3997       --
3998       -- values are to be archived as non-reportable:
3999       l_report_values := 0;
4000       --
4001       -- marker balance issue - from 06-APR-2000 not valid to only report
4002       -- category where total contributions are not zero.
4003       -- Removed previous total contribs check.
4004       -- Add the balances for this assignment to the NI balances
4005       -- (per NI category) for the person in the NI Balance Totals table
4006       --
4007       hr_utility.set_location(l_proc,10);
4008       -- Find appropriate row in NI values table:
4009         BEGIN
4010           l_index1:=0;
4011           LOOP
4012             l_index1 := l_index1 + 1;
4013             EXIT WHEN g_ni_balance_totals(l_index1).ni_cat = p_ni_cat
4014               AND nvl(g_ni_balance_totals(l_index1).scon,'NONE')
4015                                                     = nvl(p_scon,'NONE');
4016           END LOOP;
4017           -- Add balances to those in table:
4018           hr_utility.set_location(l_proc||' '||l_index1,15);
4019           g_ni_balance_totals(l_index1).tot_contribs :=
4020             g_ni_balance_totals(l_index1).tot_contribs + nvl(p_tot_contribs,0);
4021           g_ni_balance_totals(l_index1).earnings :=
4022             g_ni_balance_totals(l_index1).earnings + nvl(p_earnings,0);
4023           g_ni_balance_totals(l_index1).ees_contribs :=
4024             g_ni_balance_totals(l_index1).ees_contribs + nvl(p_ees_contribs,0);
4025           g_ni_balance_totals(l_index1).ni_able_lel :=
4026             g_ni_balance_totals(l_index1).ni_able_lel + nvl(p_ni_able_lel,0);
4027           g_ni_balance_totals(l_index1).ni_able_et :=
4028             g_ni_balance_totals(l_index1).ni_able_et + nvl(p_ni_able_et,0);
4029           g_ni_balance_totals(l_index1).ni_able_uel :=
4030             g_ni_balance_totals(l_index1).ni_able_uel + nvl(p_ni_able_uel,0);
4031           -- EOY 07/08 Begin
4032           g_ni_balance_totals(l_index1).ni_able_auel :=
4033             g_ni_balance_totals(l_index1).ni_able_auel + nvl(p_ni_able_auel,0);
4034           -- EOY 07/08 End
4035           g_ni_balance_totals(l_index1).ers_rebate :=
4036             g_ni_balance_totals(l_index1).ers_rebate + nvl(p_ers_rebate,0);
4037           g_ni_balance_totals(l_index1).ees_rebate :=
4038             g_ni_balance_totals(l_index1).ees_rebate + nvl(p_ees_rebate,0);
4039         EXCEPTION WHEN no_data_found THEN
4040           -- row not found, insert new one:
4041           hr_utility.set_location(l_proc||' '||l_index1,20);
4042           g_ni_balance_totals(l_index1).ni_cat       := p_ni_cat;
4043           g_ni_balance_totals(l_index1).scon         := p_scon;
4044           g_ni_balance_totals(l_index1).tot_contribs := nvl(p_tot_contribs,0);
4045           g_ni_balance_totals(l_index1).earnings     := nvl(p_earnings,0);
4046           g_ni_balance_totals(l_index1).ees_contribs := nvl(p_ees_contribs,0);
4047           g_ni_balance_totals(l_index1).ni_able_lel  := nvl(p_ni_able_lel,0);
4048           g_ni_balance_totals(l_index1).ni_able_et   := nvl(p_ni_able_et,0);
4049           g_ni_balance_totals(l_index1).ni_able_uel  := nvl(p_ni_able_uel,0);
4050 	  g_ni_balance_totals(l_index1).ni_able_auel := nvl(p_ni_able_auel,0); --EOY 07/08
4051           g_ni_balance_totals(l_index1).ers_rebate   := nvl(p_ers_rebate,0);
4052           g_ni_balance_totals(l_index1).ees_rebate   := nvl(p_ees_rebate,0);
4053         END; -- Find appropriate row in NI values table
4054       --
4055     ELSE
4056       -- not a multi-asg so values are to be archived as reportable.
4057       l_report_values := 1;
4058     END IF;  -- End of Multiple Assignment Logic Part II
4059     --
4060     -- Archive the values:
4061     -- Nb.  Need to archive the Total Contributions item (even if 0) as the
4062     -- values view relies on this item being in the archive in order
4063     -- for a row to be returned (it uses this item to obtain the
4064     -- contexts of reportable, NI Cat and (if necessary) SCON.
4065     --
4066     -- EOY 2004, archive a value of R in the ni_refund or
4067     -- ni_scon_refund DBI, if the Total Contributions
4068     -- are negative.
4069     --
4070     IF p_tot_contribs < 0 then
4071        l_refund_flag := 'R';
4072     ELSE
4073        l_refund_flag := '';
4074     END IF;
4075     --
4076     IF p_scon IS NOT NULL THEN
4077       hr_utility.set_location(l_proc,30);
4078       archive_ni_value(g_ni_scon_earnings_eid,p_earnings,
4079                        l_report_values,p_ni_cat,p_scon);
4080       archive_ni_value(g_ni_scon_ees_contribution_eid,p_ees_contribs,
4081                        l_report_values,p_ni_cat,p_scon);
4085                        l_report_values,p_ni_cat,p_scon);
4082       archive_ni_value(g_ni_scon_tot_contribution_eid,p_tot_contribs,
4083                        l_report_values,p_ni_cat,p_scon);
4084       archive_ni_value(g_ni_scon_able_et_eid,p_ni_able_et,
4086       archive_ni_value(g_ni_scon_able_uel_eid,p_ni_able_uel,
4087                        l_report_values,p_ni_cat,p_scon);
4088       --EOY 07/08 Begin
4089       archive_ni_value(g_ni_scon_able_auel_eid,p_ni_able_auel,
4090                        l_report_values,p_ni_cat,p_scon);
4091       --EOY 07/08 End
4092       archive_ni_value(g_ni_scon_able_lel_eid,p_ni_able_lel,
4093                        l_report_values,p_ni_cat,p_scon);
4094       archive_ni_value(g_ni_scon_ers_rebate_eid,p_ers_rebate,
4095                        l_report_values,p_ni_cat,p_scon);
4096       archive_ni_value(g_ni_scon_ees_rebate_eid,p_ees_rebate,
4097                        l_report_values,p_ni_cat,p_scon);
4098       archive_ni_value(g_ni_scon_refund_eid, l_refund_flag,
4099                        l_report_values,p_ni_cat,p_scon);
4100     ELSE
4101       hr_utility.set_location(l_proc,40);
4102       archive_ni_value(g_ni_earnings_eid,p_earnings,
4103                        l_report_values,p_ni_cat);
4104       archive_ni_value(g_ni_ees_contribution_eid,p_ees_contribs,
4105                        l_report_values,p_ni_cat);
4106       archive_ni_value(g_ni_tot_contribution_eid,p_tot_contribs,
4107                        l_report_values,p_ni_cat);
4108       archive_ni_value(g_ni_able_et_eid,p_ni_able_et,
4109                        l_report_values,p_ni_cat);
4110       archive_ni_value(g_ni_able_uel_eid,p_ni_able_uel,
4111                        l_report_values,p_ni_cat);
4112       --EOY 07/08 Begin
4113       archive_ni_value(g_ni_able_auel_eid,p_ni_able_auel,
4114                        l_report_values,p_ni_cat);
4115       --EOY 07/08 End
4116       archive_ni_value(g_ni_able_lel_eid,p_ni_able_lel,
4117                        l_report_values,p_ni_cat);
4118       archive_ni_value(g_ni_ers_rebate_eid,p_ers_rebate,
4119                        l_report_values,p_ni_cat);
4120       archive_ni_value(g_ni_ees_rebate_eid,p_ees_rebate,
4121                        l_report_values,p_ni_cat);
4122       archive_ni_value(g_ni_refund_eid, l_refund_flag,
4123                        l_report_values,p_ni_cat);
4124     END IF;
4125     hr_utility.set_location(' Leaving: '||l_proc,100);
4126   END archive_ni_values;
4127   --
4128   PROCEDURE remove_null_address_lines(p_address_line1 IN OUT NOCOPY VARCHAR2,
4129                                       p_address_line2 IN OUT NOCOPY VARCHAR2,
4130                                       p_address_line3 IN OUT NOCOPY VARCHAR2,
4131                                       p_address_line4 IN OUT NOCOPY VARCHAR2)
4132   IS
4133      --
4134      TYPE t_lines IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER;
4135      l_lines t_lines;
4136      l_dummy VARCHAR2(1000);
4137      l_proc  VARCHAR2(100) := 'pay_gb_eoy_archive.remove_null_address_lines';
4138      --
4139   BEGIN
4140      hr_utility.set_location('Entering: '||l_proc,1);
4141      hr_utility.trace('p_address_line1='||p_address_line1);
4142      hr_utility.trace('p_address_line2='||p_address_line2);
4143      hr_utility.trace('p_address_line3='||p_address_line3);
4144      hr_utility.trace('p_address_line4='||p_address_line4);
4145      --
4146      l_lines(1) := p_address_line1;
4147      l_lines(2) := p_address_line2;
4148      l_lines(3) := p_address_line3;
4149      l_lines(4) := p_address_line4;
4150      --
4151      FOR i IN 1..3 LOOP
4152         FOR j IN 1..(4-i) LOOP
4153            IF l_lines(j) IS NULL and l_lines(j+1) IS NOT NULL THEN
4154               l_lines(j) := l_lines(j+1);
4155               l_lines(j+1) := NULL;
4156            END IF;
4157         END LOOP;
4158      END LOOP;
4159      --
4160      hr_utility.set_location(l_proc,10);
4161      --
4162      p_address_line1 := l_lines(1);
4163      p_address_line2 := l_lines(2);
4164      p_address_line3 := l_lines(3);
4165      p_address_line4 := l_lines(4);
4166      --
4167      hr_utility.set_location('Leaving: '||l_proc,20);
4168      hr_utility.trace('p_address_line1='||p_address_line1);
4169      hr_utility.trace('p_address_line2='||p_address_line2);
4170      hr_utility.trace('p_address_line3='||p_address_line3);
4171      hr_utility.trace('p_address_line4='||p_address_line4);
4172   END;
4173   --
4174 BEGIN
4175   hr_utility.set_location('Entering: '||l_proc,1);
4176   hr_utility.trace('p_assactid='||p_assactid);
4177   -- Get the AAC level info.
4178   OPEN csr_asg_act_info(p_assactid);
4179   FETCH csr_asg_act_info INTO l_assignment_id,
4180                               l_effective_end_date,
4181                               l_tax_ref_transfer,
4182                               l_agg_active_start,
4183                               l_agg_active_end;
4184   CLOSE csr_asg_act_info;
4185   hr_utility.trace('After csr_asg_act_info, l_assignment_id='||l_assignment_id);
4186   hr_utility.trace('l_effective_end_date='||fnd_date.date_to_displaydate(l_effective_end_date));
4187   hr_utility.trace('l_tax_ref_transfer='||l_tax_ref_transfer);
4188   hr_utility.trace('l_agg_active_start='||fnd_date.date_to_displaydate(l_agg_active_start));
4189   hr_utility.trace('l_agg_active_end='||fnd_date.date_to_displaydate(l_agg_active_end));
4190   -- Bug fix for 3699865 - get the final process date
4191   OPEN csr_asg_last_process_date(l_assignment_id);
4192   FETCH csr_asg_last_process_date INTO l_final_process_date;
4193   CLOSE csr_asg_last_process_date;
4197      and l_final_process_date < l_effective_end_date THEN
4194   hr_utility.trace('After csr_asg_last_process_date, l_final_process_date='||fnd_date.date_to_displaydate(l_final_process_date));
4195   IF l_final_process_date is not null
4196      and nvl(l_tax_ref_transfer, 'N') = 'N'
4198     -- added extra conditions above for 5199746, so that final process date
4199     -- is used to retrieve person/asg details and balances only when
4200     -- asg has not been transferred to another PAYE Ref before termination and
4201     -- the final process date is earlier than the assignment's end date on
4202     -- the PAYE Ref at the time of action creation
4203     l_effective_end_date := l_final_process_date;
4204   END IF;
4205 
4206   -- Get basic Asg info.
4207   OPEN csr_basic_asg_info (l_assignment_id, l_effective_end_date);
4208   FETCH csr_basic_asg_info INTO l_payroll_id,
4209                             l_assignment_number,
4210                             l_person_id,
4211                             l_organization_id,
4212                             l_location_id,
4213                             l_people_group_id,
4214                             l_period_of_service_id;
4215   CLOSE csr_basic_asg_info;
4216   --
4217   hr_utility.trace('After csr_basic_asg_info, l_payroll_id='||l_payroll_id);
4218   hr_utility.trace('l_assignment_number='||l_assignment_number);
4219   hr_utility.trace('l_assignment_number='||l_assignment_number);
4220   hr_utility.trace('l_person_id='||l_person_id);
4221   hr_utility.trace('l_person_id='||l_person_id);
4222   hr_utility.trace('l_organization_id='||l_organization_id);
4223   hr_utility.trace('l_location_id='||l_location_id);
4224   hr_utility.trace('l_people_group_id='||l_people_group_id);
4225   hr_utility.trace('l_period_of_service_id='||l_period_of_service_id);
4226   --
4227     IF l_assignment_number is null then
4228     --
4229     hr_utility.trace('Assignment has been updated or terminated');
4230     -- 1.The direct match asg and end date has not been found,
4231     --   Check whether there are any asgs current at this date,
4232     --   and use the adjusted end date.
4233     --
4234     OPEN csr_basic_inf_current(l_assignment_id, l_effective_end_date);
4235     FETCH csr_basic_inf_current INTO l_adj_eff_end_date,
4236                                      l_payroll_id,
4237                                      l_assignment_number,
4238                                      l_person_id,
4239                                      l_organization_id,
4240                                      l_location_id,
4241                                      l_people_group_id,
4242                                      l_period_of_service_id;
4243     IF csr_basic_inf_current%FOUND then
4244        -- Set the eff end to the one of the current row
4245        l_effective_end_date := l_adj_eff_end_date;
4246        hr_utility.trace('After csr_basic_inf_current, l_adj_eff_end_date='||fnd_date.date_to_displaydate(l_adj_eff_end_date));
4247        hr_utility.trace('l_payroll_id='||l_payroll_id);
4248        hr_utility.trace('l_assignment_number='||l_assignment_number);
4249        hr_utility.trace('l_person_id='||l_person_id);
4250        hr_utility.trace('l_organization_id='||l_organization_id);
4251        hr_utility.trace('l_location_id='||l_location_id);
4252        hr_utility.trace('l_people_group_id='||l_people_group_id);
4253        hr_utility.trace('l_period_of_service_id='||l_period_of_service_id);
4254        hr_utility.trace('ASSIGNMENT UPDATED: '||to_char(l_assignment_id));
4255        hr_utility.trace('End date used: '||to_char(l_adj_eff_end_date));
4256        l_assignment_message :=
4257            'The Assignment has been updated during this process';
4258        archive_asg_info(g_assignment_message_eid, l_assignment_message);
4259     END IF;
4260     CLOSE csr_basic_inf_current;
4261     --
4262     IF l_assignment_number is null then  -- Still not matched.
4263        --
4264        -- 2.Check whether this has been terminated and use the
4265        --   terminated End Date:
4266        --
4267        open csr_basic_inf_term(l_assignment_id, l_effective_end_date);
4268        FETCH csr_basic_inf_term INTO l_adj_eff_end_date,
4269                                      l_payroll_id,
4270                                      l_assignment_number,
4271                                      l_person_id,
4272                                      l_organization_id,
4273                                      l_location_id,
4274                                      l_people_group_id,
4275                                      l_period_of_service_id;
4276        IF csr_basic_inf_term%FOUND then
4277           -- Set the eff end to the adjusted one from the
4278           -- terminated assignment.
4279           l_effective_end_date := l_adj_eff_end_date;
4280 /*          hr_utility.trace('After csr_basic_inf_term, l_adj_eff_end_date='||fnd_date.date_to_displaydate(l_adj_eff_end_date));
4281           hr_utility.trace('l_payroll_id='||l_payroll_id);
4282           hr_utility.trace('l_assignment_number='||l_assignment_number);
4283           hr_utility.trace('l_person_id='||l_person_id);
4284           hr_utility.trace('l_organization_id='||l_organization_id);
4285           hr_utility.trace('l_location_id='||l_location_id);
4286           hr_utility.trace('l_people_group_id='||l_people_group_id);
4287           hr_utility.trace('l_period_of_service_id='||l_period_of_service_id); */
4288           hr_utility.trace('TERMINATION: '||to_char(l_assignment_id));
4289           hr_utility.trace('End date used: '||to_char(l_adj_eff_end_date));
4290           l_assignment_message :=
4291               'The Assignment has been Terminated during this process';
4295           -- This should never be raised but placed to trap error where
4292              archive_asg_info(g_assignment_message_eid, l_assignment_message);
4293        ELSE
4294           -- Both cursors not found, so archive a default error msg.
4296           -- the asg has been somehow purged.
4297           hr_utility.trace('ASG NOT FOUND: '||to_char(l_assignment_id));
4298           l_assignment_message :=
4299              'ERROR: Assignment Cannot be found from archived info';
4300           archive_asg_info(g_assignment_message_eid, l_assignment_message);
4301        END IF;
4302        CLOSE csr_basic_inf_term;
4303     --
4304     ELSE
4305        -- Assignment number is not null
4306        IF l_payroll_id IS NULL THEN
4307           -- Payroll is null
4308           l_assignment_message :=
4309              'ERROR: Payroll not found on the assignment as at '||fnd_date.date_to_canonical(l_effective_end_date);
4310           archive_asg_info(g_assignment_message_eid, l_assignment_message);
4311        END IF;
4312     END IF;
4313   ELSE
4314      -- Assignment number is not null
4315      IF l_payroll_id IS NULL THEN
4316         -- Payroll is null
4317         l_assignment_message :=
4318            'ERROR: Payroll not found on the assignment as at '||fnd_date.date_to_canonical(l_effective_end_date);
4319         archive_asg_info(g_assignment_message_eid, l_assignment_message);
4320      END IF;
4321   --
4322   END IF;
4323   --
4324   IF l_payroll_id IS NULL THEN
4325      hr_utility.trace('l_assignment_message='||l_assignment_message);
4326      fnd_file.put_line(fnd_file.log, 'Error encountered while processing assignment action '||p_assactid);
4327      fnd_file.put_line(fnd_file.log, l_assignment_message);
4328      l_dummy := write_output(p_assignment_number => l_assignment_number,
4329                        p_full_name => l_last_name||', '||l_first_name,
4330                        p_message_type => 'E',
4331                        p_message => 'Assignment action '||p_assactid||' encountered '||l_assignment_message);
4332      RAISE ASG_ACTION_ERROR;
4333   END IF;
4334   --
4335   -- Fetch Payroll info into table structure to the local vars from
4336   -- the current plsql tab vals.
4337   --
4338   BEGIN
4339   --
4340      l_payroll_start_year        := g_pay_start_yr_tab(l_payroll_id);
4341      l_payroll_end_year          := g_pay_end_yr_tab(l_payroll_id);
4342      l_payroll_period_type       := g_pay_period_typ_tab(l_payroll_id);
4343      l_payroll_max_period_number := g_pay_max_per_no_tab(l_payroll_id);
4344      l_payroll_tax_ref           := g_pay_tax_ref_tab(l_payroll_id);
4345      l_payroll_tax_dist          := g_pay_tax_dist_tab(l_payroll_id);
4346      --
4347      l_number_per_fiscal_yr      := g_no_per_fiscal_yr(l_payroll_id);
4348      hr_utility.trace('Payroll Info Cached previously for: '||to_char(l_payroll_id));
4349   --
4350   END;
4351   --
4352   -- Get asg Start date
4353   hr_utility.trace('Calling csr_asg_start');
4354   hr_utility.trace('With: '||to_char(l_assignment_id)||','||to_char(l_effective_end_date)||','||to_char(l_payroll_start_year)||','||to_char(l_payroll_end_year));
4355   OPEN csr_asg_start(l_assignment_id, l_effective_end_date,
4356                      l_payroll_start_year, l_payroll_end_year);
4357   FETCH csr_asg_start INTO l_effective_start_date;
4358   CLOSE csr_asg_start;
4359   hr_utility.trace('After csr_asg_start, l_effective_start_date='||fnd_date.date_to_displaydate(l_effective_start_date));
4360   --
4361   hr_utility.trace('Action type: '||l_action_type);
4362   --
4363   -- Get basic person info
4364   OPEN csr_person_info(l_person_id);
4365   FETCH csr_person_info INTO  l_last_name,
4366                               l_first_name,
4367                               l_middle_name,
4368                               l_date_of_birth,
4369                               l_title,
4370                               l_expense_check_to_address,
4371                               l_ni_number,
4372                               l_sex,
4373                               l_pensioner_indicator,
4374                               l_aggregated_paye_flag,
4375                               l_multiple_asg_flag;
4376   CLOSE csr_person_info;
4377   hr_utility.trace('After csr_person_info, l_last_name='||l_last_name);
4378   hr_utility.trace('l_first_name='||l_first_name);
4379   hr_utility.trace('l_middle_name='||l_middle_name);
4380   hr_utility.trace('l_date_of_birth='||fnd_date.date_to_displaydate(l_date_of_birth));
4381   hr_utility.trace('l_title='||l_title);
4382   hr_utility.trace('l_expense_check_to_address='||l_expense_check_to_address);
4383   hr_utility.trace('l_ni_number='||l_ni_number);
4384   hr_utility.trace('l_sex='||l_sex);
4385   hr_utility.trace('l_pensioner_indicator='||l_pensioner_indicator);
4386   hr_utility.trace('l_aggregated_paye_flag='||l_aggregated_paye_flag);
4387   hr_utility.trace('l_multiple_asg_flag='||l_multiple_asg_flag);
4388   --
4389   hr_utility.trace('Before get_asg_active_range, l_payroll_tax_ref='||l_payroll_tax_ref);
4390   hr_utility.trace('l_payroll_tax_dist='||l_payroll_tax_dist);
4391   hr_utility.trace('l_assignment_id='||l_assignment_id);
4392   OPEN get_asg_active_range(l_assignment_id, l_payroll_tax_dist||'/'||l_payroll_tax_ref);
4393   FETCH get_asg_active_range INTO l_active_start, l_active_end;
4394   CLOSE get_asg_active_range;
4395   hr_utility.trace('After get_asg_active_range.');
4396   hr_utility.trace('l_active_start='||fnd_date.date_to_displaydate(l_active_start));
4397   hr_utility.trace('l_active_end='||fnd_date.date_to_displaydate(l_active_end));
4398   --
4402     hr_utility.set_location(l_proc,10);
4399   -- set termination type and date.
4400   IF l_tax_ref_transfer = 'N' THEN
4401     --
4403     -- not a tax reference transfer.
4404     OPEN csr_termination(l_period_of_service_id,
4405                          l_effective_end_date);
4406     FETCH csr_termination INTO l_actual_termination_date,
4407                                l_last_std_process_date,
4408                                l_termination_type;
4409     CLOSE csr_termination;
4410     hr_utility.trace('After csr_termination, l_actual_termination_date='||fnd_date.date_to_displaydate(l_actual_termination_date));
4411     hr_utility.trace('l_last_std_process_date='||fnd_date.date_to_displaydate(l_last_std_process_date));
4412     hr_utility.trace('l_termination_type='||l_termination_type);
4413     IF l_multiple_asg_flag = 'Y' THEN
4414        -- for multiple assignment check if aggregated active end date
4415        -- is before the actual termination date and EOY, if yes then set
4416        -- termination date to aggregated active end date
4417        IF l_agg_active_end < least(nvl(l_actual_termination_date, hr_general.end_of_time), g_end_year) THEN
4418           l_termination_date := l_agg_active_end;
4419        END IF;
4420     ELSE
4421        -- for non aggregated assignments check if this assignments
4422        -- active end date is before the actual termination date and EOY, if
4423        -- yes then set termination date to this assignment's active
4424        -- end date
4425        IF l_active_end < least(nvl(l_actual_termination_date, hr_general.end_of_time), g_end_year) THEN
4426           l_termination_date := l_active_end;
4427        END IF;
4428     END IF;
4429     --
4430     IF l_termination_date IS NOT NULL THEN
4431        -- assignment ended before the actual termination and EOY hence
4432        -- leave termination date as the last active status date
4433        NULL;
4434     ELSIF l_termination_date IS NULL AND l_actual_termination_date IS NOT NULL THEN
4435        -- assignment active end date same as actual termination hence set
4436        -- termination date to actual termination date, this is employee
4437        -- termination
4438        l_termination_date := l_actual_termination_date;
4439        -- since its a employee termination, make further checks
4440        IF nvl(l_last_std_process_date, g_end_year+1) <= g_end_year THEN
4441           -- Employee has been terminated and last_std process is before
4442           -- the end of current tax year, do nothing here, leave
4443           -- l_termination_date as actual_termination date to report on
4444           -- P14 EDI and to ensure P60 is not generated
4445           NULL;
4446           hr_utility.trace('Employee terminated and last std process before EOY.');
4447        ELSE
4448           -- Employee has been terminated but last_std_process_date is
4449           -- after current tax year hence need to check if P45 has been
4450           -- issued, if yes then leave l_termination_date as actual
4451           -- termination date else ensire l_termination_date is null
4452           -- so that P60 can be generated
4453           l_date_of_manual_p45_issue := NULL;
4454           l_date_of_manual_p45_issue := pay_p45_pkg.get_p45_eit_manual_issue_dt(l_assignment_id);
4455           hr_utility.trace('After get_p45_eit_manual_issue_dt, manual issue date='||fnd_date.date_to_displaydate(l_date_of_manual_p45_issue));
4456           --
4457           IF l_date_of_manual_p45_issue IS NOT NULL
4458              AND l_date_of_manual_p45_issue <= g_end_year THEN
4459              -- manual P45 issued in current tax year hence leave
4460              -- l_termination_date as actual termination date to report
4461              -- on p14 EDI and to ensure P60 is not generated
4462              NULL;
4463           ELSIF l_date_of_manual_p45_issue IS NOT NULL THEN
4464              -- manual p45 issued after EOY hence don't archive acual
4465              -- termination date so that p60 can be generated for current
4466              -- tax year
4467              l_termination_date := NULL;
4468           ELSE
4469              l_p45_issue_date := NULL;
4470              -- Check when was P45 issued
4471              pay_p45_pkg.get_p45_asg_action_id(l_assignment_id,
4472                                                l_p45_action_id,
4473                                                l_p45_issue_date,
4474                                                l_p45_action_seq);
4475              hr_utility.trace('After get_p45_asg_action_id, l_p45_issue_date='||fnd_date.date_to_displaydate(l_p45_issue_date));
4476              hr_utility.trace('l_p45_action_id='||l_p45_action_id);
4477 
4478              IF l_p45_issue_date IS NULL THEN
4479                 pay_p45_pkg.get_p45_agg_asg_action_id(l_assignment_id,
4480                                                     l_p45_agg_asg_id,
4481                                                     l_p45_final_pay_date,
4482                                                     l_p45_issue_date,
4483                                                     l_p45_action_id);
4484                 hr_utility.trace('After get_p45_agg_asg_action_id, l_p45_issue_date='||fnd_date.date_to_displaydate(l_p45_issue_date));
4485 
4486              END IF;
4487 
4488 	     -- Start of BUG 5671777-1
4489              --
4490              -- fetch final payment date for the aggregated p45 assignment action id
4491              --
4492              IF l_p45_issue_date IS NOT NULL THEN
4493                 open csr_get_final_payment_date(l_p45_action_id);
4494                 fetch csr_get_final_payment_date into l_p45_final_pay_date;
4495                 close csr_get_final_payment_date;
4496               END IF;
4497               -- End of BUG 5671777-1
4501 		  IF nvl(nvl(l_p45_final_pay_date,l_p45_issue_date),g_end_year+1) <= g_end_year THEN
4498               --
4499 	      -- Changed l_p45_issue_date to l_p45_final_pay_date BUG 5671777-1
4500 	      --
4502                 --IF nvl(l_p45_issue_date, g_end_year+1) <= g_end_year THEN
4503                 -- p45 has been issued before the end of tax year hence
4504                 -- leave l_termination_date as actual_termination_date to
4505                 -- report on p14 EDI and to ensure P60 is not generated
4506                 NULL;
4507                 hr_utility.trace('P45 has been issued before the EOY.');
4508              ELSE
4509                 -- p45 has not been issued before EOY hence don't archive
4510                 -- actual termination date so that p60 can be generated,
4511                 -- set l_termination_date to NULL
4512                 l_termination_date := NULL;
4513              END IF;
4514           END IF; -- l_date_of_manual_p45_issue IS (NOT) NULL
4515        END IF; -- l_last_std_process_date IS (NOT) in the current tax year
4516     END IF; -- Termination date (NOT) NULL
4517   ELSE
4518     hr_utility.set_location(l_proc,20);
4519     -- asg is tax ref transfer
4520     l_termination_type := 'R';
4521     l_termination_date := l_effective_end_date;
4522   END IF;
4523   --
4524   -- get last action this year, using stat dates.
4525   --
4526   hr_utility.trace('Calling csr_last_action');
4527   hr_utility.trace('With asg: '|| to_char(l_assignment_id));
4528   hr_utility.trace('Eff start: '|| to_char(l_effective_start_date));
4529   hr_utility.trace('Eff end: '|| to_char(l_effective_end_date));
4530   hr_utility.trace('Start Yr: '||to_char(g_start_year));
4531   hr_utility.trace('End Yr: '||to_char(g_end_year));
4532   hr_utility.trace('Tax Ref Xfer: '||l_tax_ref_transfer);
4533   OPEN csr_last_action(l_assignment_id, l_effective_start_date,
4534                        l_effective_end_date, g_start_year,
4535                        g_end_year, l_tax_ref_transfer);
4536   FETCH csr_last_action INTO  l_last_asg_action_id,
4537                               l_last_effective_date;
4538   CLOSE csr_last_action;
4539   hr_utility.trace('After csr_last_action, l_last_asg_action_id='||l_last_asg_action_id);
4540   hr_utility.trace('l_last_effective_date='||fnd_date.date_to_displaydate(l_last_effective_date));
4541   --
4542   IF l_last_asg_action_id IS NOT NULL THEN
4543      OPEN csr_action_details(l_last_asg_action_id);
4544      FETCH csr_action_details into l_action_type;
4545      CLOSE csr_action_details;
4546      hr_utility.trace('After csr_action_details, l_action_type='||l_action_type);
4547   END IF;
4548   --
4549   -- Get Non-NI balances and codes used to determine whether to further
4550   -- process the asg.
4551   IF l_last_asg_action_id IS NOT NULL THEN
4552      /* IF l_sex = 'F' THEN  rerstored as a part of 2987008
4553                             was earlier commented out for 2003,
4554                            And now commented out again for 2005, BUG 4011263 */
4555       l_smp   := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_smp_id);
4556      /* END IF; */
4557     l_notional := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_notional_id);
4558     l_ssp   := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_ssp_id);
4559     --
4560     -- 3 Defined Balance checks, can remove these after April 03.
4561     --
4562     if g_sap_id is not null then
4563       l_sap   := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_sap_id);
4564     end if;
4565     --
4566     if g_spp_adopt_id is not null then
4567       l_spp_adopt :=
4568            100 * hr_dirbal.get_balance(l_last_asg_action_id,g_spp_adopt_id);
4569     end if;
4570     --
4571     if g_spp_birth_id is not null then
4572       l_spp_birth :=
4573            100 * hr_dirbal.get_balance(l_last_asg_action_id,g_spp_birth_id);
4574     end if;
4575     --
4576     -- Gross Pay includes Notional Pay balance
4577     --
4578     l_gross := (100 * hr_dirbal.get_balance(l_last_asg_action_id,g_gross_id)) +
4579                                l_notional;
4580     l_super := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_super_id);
4581     l_taxable := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4582                                              g_taxable_id);
4583     l_student_loan := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4584                                                   g_student_loan_id);
4585     --
4586     l_ni_arrears := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4587                                              g_ni_arrears_id);
4588     --
4589     -- Start of Bug 6271548
4590     -- fetch PAYE balance for checking whether this assignment reported in
4591     -- the P35 report or not
4592     l_paye  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_paye_id);
4593 
4594     -- fetch previous_taxable_pay,previous_tax_paid balances -- Bug 6271548
4595     --
4596     IF l_action_type <> 'V' THEN
4597       -- get paye element's run result id from last run
4598       hr_utility.trace('Before csr_tax_last_paye_run, l_last_asg_action_id='||l_last_asg_action_id);
4599       OPEN csr_tax_last_paye_run(l_last_asg_action_id);
4600       FETCH csr_tax_last_paye_run INTO l_tax_paye_run_result_id;
4601       hr_utility.trace('After csr_tax_last_paye_run, l_tax_paye_run_result_id='||l_tax_paye_run_result_id);
4602       IF l_tax_paye_run_result_id IS NULL THEN
4603          -- if paye was not calculated in last run then
4604          -- get the latest run in which it was
4605          hr_utility.trace('Before csr_tax_last_paye_run, l_assignment_id='||l_assignment_id);
4609          hr_utility.trace('After csr_tax_last_paye_run, l_tax_paye_run_result_id='||l_tax_paye_run_result_id);
4606          hr_utility.trace('l_last_effective_date='||fnd_date.date_to_displaydate(l_last_effective_date));
4607          OPEN csr_tax_latest_paye_run(l_assignment_id, l_last_effective_date);
4608          FETCH csr_tax_latest_paye_run INTO l_tax_paye_run_result_id, l_paye_eff_date;
4610          hr_utility.trace('l_paye_eff_date='||fnd_date.date_to_displaydate(l_paye_eff_date));
4611          CLOSE csr_tax_latest_paye_run;
4612       ELSE
4613          -- PAYE run result found hence effective date for paye details is
4614          -- date of last assignment action
4615          l_paye_eff_date := l_last_effective_date;
4616          hr_utility.trace('l_paye_eff_date='||fnd_date.date_to_displaydate(l_paye_eff_date));
4617       END IF;
4618       CLOSE csr_tax_last_paye_run;
4619       -- most people will have had paye calculated on the last run.
4620       -- Pick these up
4621       hr_utility.trace('Before csr_tax_last_run, l_last_asg_action_id='||l_last_asg_action_id);
4622       OPEN csr_tax_last_run(l_last_asg_action_id);
4623       FETCH csr_tax_last_run INTO l_tax_run_result_id;
4624       hr_utility.trace('After csr_tax_last_run, l_tax_run_result_id='||l_tax_run_result_id);
4625       IF l_tax_run_result_id IS NULL THEN
4626         -- find the latest update
4627         hr_utility.trace('calling csr_tax_latest_run, non reversal');
4628          hr_utility.trace('Before csr_tax_latest_run, l_assignment_id='||l_assignment_id);
4629          hr_utility.trace('l_last_effective_date='||fnd_date.date_to_displaydate(l_last_effective_date));
4630         OPEN csr_tax_latest_run(l_assignment_id, l_last_effective_date);
4631         FETCH csr_tax_latest_run INTO l_tax_run_result_id, l_paye_details_eff_date;
4632          hr_utility.trace('After csr_tax_latest_run, l_tax_run_result_id='||l_tax_run_result_id);
4633          hr_utility.trace('l_paye_details_eff_date='||fnd_date.date_to_displaydate(l_paye_details_eff_date));
4634         CLOSE csr_tax_latest_run;
4635       ELSE
4636         l_paye_details_eff_date := l_last_effective_date;
4637         hr_utility.trace('l_paye_details_eff_date='||fnd_date.date_to_displaydate(l_paye_details_eff_date));
4638       END IF;
4639       CLOSE csr_tax_last_run;
4640     ELSE
4641       -- find the latest update, as reversal action
4642         hr_utility.trace('calling csr_tax_latest_run, reversal');
4643         hr_utility.trace('Before csr_tax_latest_run, l_assignment_id='||l_assignment_id);
4644        hr_utility.trace('l_last_effective_date='||fnd_date.date_to_displaydate(l_last_effective_date));
4645         OPEN csr_tax_latest_run(l_assignment_id, l_last_effective_date);
4646         FETCH csr_tax_latest_run INTO l_tax_run_result_id, l_paye_details_eff_date;
4647         CLOSE csr_tax_latest_run;
4648         hr_utility.trace('After csr_tax_latest_run, l_tax_run_result_id='||l_tax_run_result_id);
4649         hr_utility.trace('l_paye_details_eff_date='||fnd_date.date_to_displaydate(l_paye_details_eff_date));
4650         --
4651         OPEN csr_tax_latest_paye_run(l_assignment_id, l_last_effective_date);
4652         FETCH csr_tax_latest_paye_run INTO l_tax_paye_run_result_id, l_paye_eff_date;
4653         CLOSE csr_tax_latest_paye_run;
4654         hr_utility.trace('After csr_tax_latest_paye_run, l_tax_paye_run_result_id='||l_tax_paye_run_result_id);
4655         hr_utility.trace('l_paye_eff_date='||fnd_date.date_to_displaydate(l_paye_eff_date));
4656     END IF;
4657     --
4658     archive_asg_info(g_tax_run_result_id_eid, nvl(l_tax_paye_run_result_id, l_tax_run_result_id));
4659     --
4660     -- Get tax code.
4661     -- First try PAYE element run results because PAYE details
4662     -- element run result may not be the same as the one effective on
4663     -- regular payment date of a payroll with positive
4664     -- offset
4665     OPEN csr_tax_paye_result(l_tax_paye_run_result_id);
4666     FETCH csr_tax_paye_result INTO l_tax_code, l_w1_m1_indicator,
4667                                     l_previous_taxable_pay,
4668                                     l_previous_tax_paid;
4669     hr_utility.trace('After csr_tax_paye_result, l_tax_code='||l_tax_code);
4670     hr_utility.trace('l_w1_m1_indicator='||l_w1_m1_indicator);
4671     hr_utility.trace('l_previous_taxable_pay='||to_char(l_previous_taxable_pay));
4672     hr_utility.trace('l_previous_tax_paid='||to_char(l_previous_tax_paid));
4673     IF l_tax_code IS NULL THEN
4674        -- Get the details from the element entry on the added criteria that
4675        -- there exists an updating action id on the element_entry.  In other
4676        -- words, an entry achieved using an Update Recurring rule.
4677        -- Nb. both cursors used here select max(...) so a row is returned
4678        -- even if no tax details found.
4679        --
4680        OPEN csr_tax_details_entry(l_assignment_id, l_effective_end_date,
4681                                l_payroll_end_year, 'Y');
4682        FETCH csr_tax_details_entry INTO  l_tax_code, l_w1_m1_indicator,
4683                                       l_previous_taxable_pay,
4684                                       l_previous_tax_paid;
4685        hr_utility.set_location(l_proc||' '||l_tax_code||' '||
4686                                l_w1_m1_indicator,52);
4687        IF l_tax_code IS NULL THEN
4688          -- no update recurring, so retrieve the details from the run result.
4689          OPEN csr_tax_details_result(l_tax_run_result_id);
4690          FETCH csr_tax_details_result INTO l_tax_code, l_w1_m1_indicator,
4691                                        l_previous_taxable_pay,
4692                                        l_previous_tax_paid;
4693          hr_utility.set_location(l_proc||' '||l_tax_code||' '||
4694                                  l_w1_m1_indicator,54);
4698            CLOSE csr_tax_details_entry;
4695          IF l_tax_code IS NULL THEN
4696            -- If there is still no tax code, use the element entry query
4697            -- without the update recurring criteria.
4699            OPEN csr_tax_details_entry(l_assignment_id, l_effective_end_date,
4700                                       l_payroll_end_year, 'N');
4701            FETCH csr_tax_details_entry INTO  l_tax_code, l_w1_m1_indicator,
4702                                              l_previous_taxable_pay,
4703                                              l_previous_tax_paid;
4704            hr_utility.set_location(l_proc||' '||l_tax_code||' '||
4705                                    l_w1_m1_indicator,56);
4706            IF l_tax_code IS NOT NULL THEN
4707             IF l_aggregated_paye_flag = 'Y' THEN
4708                hr_utility.trace('This is an aggregated assignment.');
4709                --
4710                if l_w1_m1_indicator = 'C' then
4711                   l_w1_m1_indicator := ' ';
4712                else
4713                   hr_utility.trace('No of periods per year = '||g_no_per_fiscal_yr(l_payroll_id));
4714                   if g_no_per_fiscal_yr(l_payroll_id) in (1,2,4,6,12,24) then
4715                      l_w1_m1_indicator := 'M';
4716                   else
4717                      l_w1_m1_indicator := 'W';
4718                   end if;
4719                end if;
4720                hr_utility.trace('Aggregated tax code found so far='||g_agg_balance_totals.tax_code);
4721                hr_utility.trace('g_agg_balance_totals.paye_eff_date = '||fnd_date.date_to_displaydate(g_agg_balance_totals.paye_eff_date));
4722                -- this tax code is from the PAYE Details Entry, since it is an
4723                -- aggregated assignment therefore check if we already have a
4724                -- a tax code from a previously processed assignment of this employee
4725                IF g_agg_balance_totals.tax_code is NULL THEN
4726                   hr_utility.trace('Aggregated tax code found so far is NULL hence store '||l_tax_code||' as the aggregated tax code');
4727                   -- No tax code found on other assignments so far therefore store
4728                   -- tax code found on element entry of this assignment,
4729                   -- this value will be archived against the primary eoy action later
4730                   g_agg_balance_totals.tax_code := l_tax_code;
4731                   g_agg_balance_totals.tax_basis := l_w1_m1_indicator;
4732                   g_agg_balance_totals.pay_previous := l_previous_taxable_pay;
4733                   g_agg_balance_totals.tax_previous := l_previous_tax_paid;
4734                   g_agg_balance_totals.paye_eff_date := fnd_date.canonical_to_date('0001/01/01 00:00:00');
4735                END IF;
4736                --
4737                IF nvl(g_agg_balance_totals.week_53, ' ') = ' ' THEN
4738                   g_agg_balance_totals.week_53 := l_week_53_indicator;
4739                END IF;
4740             END IF; -- aggregated PAYE flag = Y
4741            END IF; -- tax code found on PAYE Details entry
4742          ELSE -- tax code found on PAYE Details Result
4743             IF l_aggregated_paye_flag = 'Y' THEN
4744                --
4745                if l_w1_m1_indicator = 'C' then
4746                   l_w1_m1_indicator := ' ';
4747                else
4748                   if g_no_per_fiscal_yr(l_payroll_id) in (1,2,4,6,12,24) then
4749                      l_w1_m1_indicator := 'M';
4750                   else
4751                      l_w1_m1_indicator := 'W';
4752                   end if;
4753                end if;
4754                -- this is aggregated assignment hence check whether this is the
4755                -- latest run result so far
4756                IF l_paye_details_eff_date >
4757                             g_agg_balance_totals.paye_eff_date THEN
4758                   -- this is the latest paye run results amongst aggregated
4759                   -- assignment, store values in the table to archive
4760                   -- against the primary eoy action later
4761                   g_agg_balance_totals.tax_code := l_tax_code;
4762                   g_agg_balance_totals.tax_basis := l_w1_m1_indicator;
4763                   g_agg_balance_totals.pay_previous := l_previous_taxable_pay;
4764                   g_agg_balance_totals.tax_previous := l_previous_tax_paid;
4765                   g_agg_balance_totals.paye_eff_date := l_paye_details_eff_date;
4766                END IF;
4767                --
4768                IF nvl(g_agg_balance_totals.week_53, ' ') = ' ' THEN
4769                   g_agg_balance_totals.week_53 := l_week_53_indicator;
4770                END IF;
4771             END IF; -- aggregated PAYE flag = Y
4772          END IF; -- null or nor null tax code in pay details resluts
4773          CLOSE csr_tax_details_result;
4774        END IF; -- tax code null on update recurring PAYE Details entry
4775        CLOSE csr_tax_details_entry;
4776        --
4777     ELSE -- tax code found on PAYE Reun Result
4778        IF l_aggregated_paye_flag = 'Y' THEN
4779           --
4780           if l_w1_m1_indicator = 'C' then
4781              l_w1_m1_indicator := ' ';
4782           else
4783              if g_no_per_fiscal_yr(l_payroll_id) in (1,2,4,6,12,24) then
4784                 l_w1_m1_indicator := 'M';
4785              else
4786                 l_w1_m1_indicator := 'W';
4787              end if;
4788           end if;
4789           -- this is aggregated assignment hence check whether this is the
4790           -- latest run result so far
4791           IF l_paye_eff_date > g_agg_balance_totals.paye_eff_date THEN
4795              g_agg_balance_totals.tax_code := l_tax_code;
4792              -- these are the latest paye run results amongst aggregated
4793              -- assignment, store them in the table to archive
4794              -- against the primary eoy action later
4796              g_agg_balance_totals.tax_basis := l_w1_m1_indicator;
4797              g_agg_balance_totals.pay_previous := l_previous_taxable_pay;
4798              g_agg_balance_totals.tax_previous := l_previous_tax_paid;
4799              g_agg_balance_totals.paye_eff_date := l_paye_eff_date;
4800           END IF;
4801           --
4802           IF nvl(g_agg_balance_totals.week_53, ' ') = ' ' THEN
4803              g_agg_balance_totals.week_53 := l_week_53_indicator;
4804           END IF;
4805        END IF; -- aggregated PAYE flag = 'Y'
4806     END IF; --found/not found tax code on PAYE reun result
4807     CLOSE csr_tax_paye_result;
4808 
4809     -- fetch NI x Total/Able balances for checking whether this assignment
4810     -- reported in the P35 report or not. -- Bug 6271548
4811 
4812     hr_utility.trace('fetching NI x Total/Able balances');
4813     -- NI A
4814     l_nia_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nia_tot_id);
4815     l_nia_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4816                                                g_nia_able_id);
4817 
4818     -- NI B
4819     l_nib_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id, g_nib_tot_id);
4820     l_nib_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4821                                                  g_nib_able_id);
4822 
4823     -- NI C
4824     l_nic_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nic_tot_id);
4825     l_nic_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4826                                                  g_nic_able_id);
4827 
4828     -- NI D
4829     l_nid_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nid_tot_id);
4830     l_nid_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4831                                                g_nid_able_id);
4832 
4833     -- NI E
4834     l_nie_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_tot_id);
4835     l_nie_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4836 						g_nie_able_id);
4837 
4838     -- NI J
4839     l_nij_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nij_tot_id);
4840     l_nij_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4841                                                g_nij_able_id);
4842 
4843     -- NI L
4844     l_nil_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nil_tot_id);
4845     l_nil_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
4846                                                g_nil_able_id);
4847 
4848     -- NI F,G,S
4849     hr_utility.trace('fetching NI x Total/Able balances for F,G,S category');
4850     DECLARE
4851     l_sum_ni_tot      NUMBER:= 0;
4852     l_sum_ni_able     NUMBER:= 0;
4853     l_ni_ers          NUMBER:= 0;
4854 
4855     BEGIN
4856         OPEN csr_get_scon_bal(l_last_asg_action_id,g_scon_input_id,
4857                                   g_category_input_id, g_ni_id);
4858         LOOP
4859         FETCH csr_get_scon_bal INTO l_ni_cat, l_scon, l_ni_able,
4860                                     l_ni_tot, l_ni_ees, l_ni_ers,
4861                                     l_ni_able_et,l_ni_able_lel,l_ni_able_uel,
4862 				    --EOY 07/08 Begin
4863                                     l_ni_able_auel,
4864                                     --EOY 07/08 End
4865 				    l_ers_rebate,
4866                                     l_ees_rebate, l_rebate_emp;
4867          EXIT WHEN csr_get_scon_bal%notfound;
4868 
4869          l_sum_ni_tot      := l_sum_ni_tot      + l_ni_tot;
4870          l_sum_ni_able     := l_sum_ni_able     + l_ni_able;
4871 
4872          END LOOP;
4873          CLOSE csr_get_scon_bal;
4874 
4875     l_ni_tot  := l_sum_ni_tot;
4876     l_ni_able := l_sum_ni_able;
4877     END;
4878 
4879      hr_utility.trace('l_ni_tot '||l_ni_tot);
4880      hr_utility.trace('l_ni_able '||l_ni_able );
4881   END IF;
4882 
4883   -- added additional conditions to check whether this assignment
4884   -- reported in the P35 report or not.
4885 
4886  /* IF l_last_asg_action_id IS NULL OR -- Do not process this assignment
4887      (l_termination_date < l_payroll_start_year AND
4888         l_gross = 0 AND l_taxable = 0 AND l_ssp = 0 AND l_smp = 0 AND
4889         l_student_loan = 0 AND
4890         l_super >= 0 AND l_ni_tot = 0 AND l_ni_able = 0)*/
4891 
4892     IF l_last_asg_action_id IS NULL OR -- Do not process this assignment
4893      ( l_termination_date < l_payroll_start_year AND l_taxable = 0 AND
4894        l_paye =0 AND l_previous_taxable_pay = 0 AND l_previous_tax_paid =0
4895        AND l_super = 0 AND l_ssp = 0 AND l_smp = 0 AND l_sap =0 AND
4896         l_spp_adopt = 0 AND l_spp_birth = 0 AND l_student_loan <= 0 AND l_ni_arrears = 0
4897         AND (l_nia_tot = 0 AND l_nia_able = 0 AND l_nib_tot = 0 AND l_nib_able = 0 AND
4898         l_nic_tot = 0 AND l_nic_able = 0 AND l_nid_tot = 0 AND l_nid_able = 0 AND
4899         l_nie_tot = 0 AND l_nie_able = 0 AND l_nij_tot = 0 AND l_nij_able = 0 AND
4900         l_nil_tot = 0 AND l_nil_able = 0 AND l_ni_tot = 0 AND l_ni_able = 0 ))
4901   -- End of Bug 6271548
4902   THEN
4903     hr_utility.trace('Do Not Process asg any further');
4904     -- Do not process this assignment any further and don't archive
4905     -- info extracted so far
4906     NULL;
4907   ELSE
4911     archive_asg_info(g_payroll_id_eid,l_payroll_id);
4908     hr_utility.set_location(l_proc,30);
4909     -- archive info extracted so far
4910     archive_asg_info(g_termination_type_eid,l_termination_type);
4912     archive_asg_info(g_assignment_number_eid,l_assignment_number);
4913     archive_asg_info(g_person_id_eid,l_person_id);
4914     archive_asg_info(g_organization_id_eid,l_organization_id);
4915     archive_asg_info(g_location_id_eid,l_location_id);
4916     archive_asg_info(g_people_group_id_eid,l_people_group_id);
4917     archive_asg_info(g_effective_start_date_eid,
4918                      fnd_date.date_to_canonical(l_effective_start_date));
4919     archive_asg_info(g_termination_date_eid,
4920                      fnd_date.date_to_canonical(l_termination_date));
4921     archive_asg_info(g_last_asg_action_id_eid,l_last_asg_action_id);
4922     archive_asg_info(g_last_effective_date_eid,
4923                      fnd_date.date_to_canonical(l_last_effective_date));
4924     --
4925     -- Now fetch more asg info.
4926     --
4927     IF l_multiple_asg_flag = 'Y' THEN
4928       -- Do Multiple Assignment Logic Part I
4929       --
4930       IF g_masg_person_id <> l_person_id
4931         OR g_masg_period_of_service_id <> l_period_of_service_id -- Bug 3784871
4932         OR g_masg_active_start <> l_agg_active_start
4933         OR g_masg_active_end <> l_agg_active_end
4934         OR g_masg_tax_ref_num <> l_payroll_tax_ref
4935       THEN
4936         -- 1st assignment to be processed for this person/tax ref.
4937         empty_masg_cache;
4938 	-- Bug 6761725 Assigning the false value to global for archiving the tax code details
4939 	g_paye_archive := FALSE;
4940         -- Prime the cache:
4941         g_masg_person_id   := l_person_id;
4942         g_masg_period_of_service_id   := l_period_of_service_id; -- Bug 3784871
4943         g_masg_active_start := l_agg_active_start;
4944         g_masg_active_end := l_agg_active_end;
4945         g_masg_tax_ref_num := l_payroll_tax_ref;
4946         IF g_permit_number IS NOT NULL THEN
4947           -- a permit was specified through SRS.
4948           --
4949           hr_utility.set_location(l_proc,35);
4950           -- Check that this person does not have assignments in
4951           -- different permits:
4952           hr_utility.trace('Calling csr_get_invalid_multiple_asg');
4953           OPEN csr_get_invalid_multiple_asg(l_person_id, l_payroll_id,
4954                                             l_payroll_start_year,
4955                                             l_payroll_end_year,
4956                                             l_payroll_tax_ref);
4957           FETCH csr_get_invalid_multiple_asg INTO l_dummy;
4958           IF csr_get_invalid_multiple_asg%FOUND THEN
4959             g_has_non_extracted_masgs := true;
4960           END IF;
4961           CLOSE csr_get_invalid_multiple_asg;
4962         END IF;  -- 1st asg
4963       END IF;
4964       IF g_has_non_extracted_masgs THEN
4965         -- error the assignment
4966         hr_utility.set_message(801, 'PAY_78000_MULTIPLE_PERMIT_ASG');
4967         hr_utility.raise_error;
4968       END IF;
4969     END IF;   -- End of Multiple Assignment Logic Part I
4970     --
4971     OPEN csr_director(l_person_id);
4972     FETCH csr_director INTO l_director_indicator;
4973     CLOSE csr_director;
4974     --
4975     IF l_multiple_asg_flag = 'Y' THEN
4976        IF l_agg_active_start BETWEEN g_start_year AND g_end_year THEN
4977           l_start_of_emp := l_agg_active_start;
4978        ELSE
4979           l_start_of_emp := NULL;
4980        END IF;
4981     ELSE
4982       IF l_active_start BETWEEN g_start_year AND g_end_year THEN
4983           l_start_of_emp := l_active_start;
4984        ELSE
4985           l_start_of_emp := NULL;
4986        END IF;
4987     END IF;
4988     --
4989     OPEN csr_addresses(l_person_id);
4990     FETCH csr_addresses INTO  l_address_line1,
4991                               l_address_line2,
4992                               l_address_line3,
4993                               l_town_or_city,
4994                               l_county,
4995                               l_postal_code,
4996                               l_country; -- 4011263
4997     CLOSE csr_addresses;
4998     --
4999     /* 4752018 - Push null address lines to the end */
5000     remove_null_address_lines(p_address_line1 => l_address_line1,
5001                               p_address_line2 => l_address_line2,
5002                               p_address_line3 => l_address_line3,
5003                               p_address_line4 => l_town_or_city);
5004     --
5005     OPEN csr_country_name(l_country);
5006     FETCH csr_country_name INTO l_country_name;
5007     CLOSE csr_country_name;
5008     --
5009     hr_utility.set_location(l_proc,40);
5010     --
5011     -- Archive more asg info.
5012     hr_utility.trace('Archiving more info');
5013     archive_asg_info(g_last_name_eid,l_last_name);
5014     archive_asg_info(g_first_name_eid,l_first_name);
5015     archive_asg_info(g_middle_name_eid,l_middle_name);
5016     archive_asg_info(g_date_of_birth_eid,
5017                      fnd_date.date_to_canonical(l_date_of_birth));
5018     archive_asg_info(g_title_eid,l_title);
5019     archive_asg_info(g_expense_check_to_address_eid,
5020                      l_expense_check_to_address);
5021     archive_asg_info(g_ni_number_eid,l_ni_number);
5022     archive_asg_info(g_sex_eid,l_sex);
5023     archive_asg_info(g_pensioner_indicator_eid,l_pensioner_indicator);
5027     archive_asg_info(g_start_of_emp_eid,
5024     archive_asg_info(g_multiple_asg_flag_eid,l_multiple_asg_flag);
5025     archive_asg_info(g_aggregated_paye_flag_eid, l_aggregated_paye_flag);
5026     archive_asg_info(g_director_indicator_eid,l_director_indicator);
5028                      fnd_date.date_to_canonical(l_start_of_emp));
5029     archive_asg_info(g_address_line1_eid,l_address_line1);
5030     archive_asg_info(g_address_line2_eid,l_address_line2);
5031     archive_asg_info(g_address_line3_eid,l_address_line3);
5032     archive_asg_info(g_town_or_city_eid,l_town_or_city);
5033     archive_asg_info(g_county_eid,l_county);
5034     archive_asg_info(g_country_eid,l_country_name); -- 4011263
5035     archive_asg_info(g_postal_code_eid,l_postal_code);
5036     --
5037     hr_utility.trace('End of archiving person and address info');
5038     -- Get more Non-NI balances and codes
5039     l_paye  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_paye_id);
5040     l_widow := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_widow_id);
5041     hr_utility.set_location(l_proc,50);
5042     hr_utility.trace('l_paye='||l_paye||', g_paye_id='||g_paye_id);
5043     IF nvl(l_aggregated_paye_flag,'N') <> 'Y' THEN
5044        -- Not Aggregated PAYE, so archive all values as prior to
5045        -- introduction of agg PAYE.
5046        hr_utility.trace('Not Aggregated PAYE, archive at asg level');
5047        /* IF l_sex = 'F' Then rerstored as a part of 2987008
5048                         was earlier commented out for 2003,
5049                       And now commented out again for 2005, BUG 4011263 */
5050          archive_asg_info(g_smp_eid,l_smp);
5051        /* END IF; */
5052        archive_asg_info(g_ssp_eid,l_ssp);
5053        archive_asg_info(g_sap_eid,l_sap);
5054        archive_asg_info(g_spp_adopt_eid, l_spp_adopt);
5055        archive_asg_info(g_spp_birth_eid, l_spp_birth);
5056        archive_asg_info(g_gross_pay_eid,l_gross);
5057        archive_asg_info(g_notional_pay_eid,l_notional);
5058        archive_asg_info(g_tax_paid_eid,ABS(l_paye));
5059        archive_asg_info(g_superannuation_paid_eid,ABS(l_super));
5060        archive_asg_info(g_widows_and_orphans_eid,l_widow);
5061        archive_asg_info(g_taxable_pay_eid,l_taxable);
5062        archive_asg_info(g_student_loans_eid,l_student_loan);
5063        archive_asg_info(g_ni_arrears_eid,l_ni_arrears);
5064        IF l_paye < 0 THEN
5065          archive_asg_info(g_tax_refund_eid,'R');
5066        END IF;
5067        IF l_super < 0 THEN
5068          archive_asg_info(g_superannuation_refund_eid,'R');
5069        END IF;
5070     ELSE
5071        -- Aggregated PAYE, so call store_agg_values to sum up values.
5072        store_agg_values(p_smp        => l_smp,
5073                         p_ssp        => l_ssp,
5074                         p_sap        => l_sap,
5075                         p_spp_adopt  => l_spp_adopt,
5076                         p_spp_birth  => l_spp_birth,
5077                         p_gross      => l_gross,
5078                         p_notional   => l_notional,
5079                         p_paye       => l_paye,
5080                         p_super      => l_super,
5081                         p_widow      => l_widow,
5082                         p_taxable    => l_taxable,
5083                         p_student_ln => l_student_loan,
5084                         p_ni_arrears => l_ni_arrears);
5085       --
5086     END IF;
5087     --
5088     -- Set up w1_m1 and wk_53 indicators.
5089     --
5090     if l_payroll_max_period_number in (53,54,56) then
5091        OPEN get_week_53_start(l_payroll_id);
5092        FETCH get_week_53_start INTO l_week_53_start;
5093        CLOSE get_week_53_start;
5094        --
5095        hr_utility.trace('After get_week_53_start, l_week_53_start='||
5096                        fnd_date.date_to_displaydate(l_week_53_start));
5097        IF nvl(l_week_53_start, hr_general.end_of_time) <= l_active_end THEN
5098           l_week_53_indicator := substr(to_char(l_payroll_max_period_number),2,1);
5099        END IF;
5100     else
5101        l_week_53_indicator := ' ';
5102     end if;
5103     --
5104     hr_utility.trace('Before getting paye details from run results.');
5105     hr_utility.trace('l_action_type='||l_action_type);
5106     --
5107     -- 2166991: Check the Action type, if reversal then the
5108     -- csr_tax_last_run cannot be used, call csr_tax_latest_run.
5109     -- Separate clause for V types for performance.
5110     --
5111     -- Start of Bug 6271548 Commented out below code due to previous_taxable_pay
5112     -- previous_tax_paid balances already fetched.
5113     -- Bug 6761725
5114     -- Archiving the tax code details depending on the global g_paye_archive
5115     IF g_paye_archive = FALSE THEN
5116     IF l_action_type <> 'V' THEN
5117       -- get paye element's run result id from last run
5118       hr_utility.trace('Before csr_tax_last_paye_run, l_last_asg_action_id='||l_last_asg_action_id);
5119       OPEN csr_tax_last_paye_run(l_last_asg_action_id);
5120       FETCH csr_tax_last_paye_run INTO l_tax_paye_run_result_id;
5121       hr_utility.trace('After csr_tax_last_paye_run, l_tax_paye_run_result_id='||l_tax_paye_run_result_id);
5122       IF l_tax_paye_run_result_id IS NULL THEN
5123          -- if paye was not calculated in last run then
5124          -- get the latest run in which it was
5125          hr_utility.trace('Before csr_tax_last_paye_run, l_assignment_id='||l_assignment_id);
5126          hr_utility.trace('l_last_effective_date='||fnd_date.date_to_displaydate(l_last_effective_date));
5130          hr_utility.trace('l_paye_eff_date='||fnd_date.date_to_displaydate(l_paye_eff_date));
5127          OPEN csr_tax_latest_paye_run(l_assignment_id, l_last_effective_date);
5128          FETCH csr_tax_latest_paye_run INTO l_tax_paye_run_result_id, l_paye_eff_date;
5129          hr_utility.trace('After csr_tax_last_paye_run, l_tax_paye_run_result_id='||l_tax_paye_run_result_id);
5131          CLOSE csr_tax_latest_paye_run;
5132       ELSE
5133          -- PAYE run result found hence effective date for paye details is
5134          -- date of last assignment action
5135          l_paye_eff_date := l_last_effective_date;
5136          hr_utility.trace('l_paye_eff_date='||fnd_date.date_to_displaydate(l_paye_eff_date));
5137       END IF;
5138       CLOSE csr_tax_last_paye_run;
5139       -- most people will have had paye calculated on the last run.
5140       -- Pick these up
5141       hr_utility.trace('Before csr_tax_last_run, l_last_asg_action_id='||l_last_asg_action_id);
5142       OPEN csr_tax_last_run(l_last_asg_action_id);
5143       FETCH csr_tax_last_run INTO l_tax_run_result_id;
5144       hr_utility.trace('After csr_tax_last_run, l_tax_run_result_id='||l_tax_run_result_id);
5145       IF l_tax_run_result_id IS NULL THEN
5146         -- find the latest update
5147         hr_utility.trace('calling csr_tax_latest_run, non reversal');
5148          hr_utility.trace('Before csr_tax_latest_run, l_assignment_id='||l_assignment_id);
5149          hr_utility.trace('l_last_effective_date='||fnd_date.date_to_displaydate(l_last_effective_date));
5150         OPEN csr_tax_latest_run(l_assignment_id, l_last_effective_date);
5151         FETCH csr_tax_latest_run INTO l_tax_run_result_id, l_paye_details_eff_date;
5152          hr_utility.trace('After csr_tax_latest_run, l_tax_run_result_id='||l_tax_run_result_id);
5153          hr_utility.trace('l_paye_details_eff_date='||fnd_date.date_to_displaydate(l_paye_details_eff_date));
5154         CLOSE csr_tax_latest_run;
5155       ELSE
5156         l_paye_details_eff_date := l_last_effective_date;
5157         hr_utility.trace('l_paye_details_eff_date='||fnd_date.date_to_displaydate(l_paye_details_eff_date));
5158       END IF;
5159       CLOSE csr_tax_last_run;
5160     ELSE
5161       -- find the latest update, as reversal action
5162         hr_utility.trace('calling csr_tax_latest_run, reversal');
5163         hr_utility.trace('Before csr_tax_latest_run, l_assignment_id='||l_assignment_id);
5164        hr_utility.trace('l_last_effective_date='||fnd_date.date_to_displaydate(l_last_effective_date));
5165         OPEN csr_tax_latest_run(l_assignment_id, l_last_effective_date);
5166         FETCH csr_tax_latest_run INTO l_tax_run_result_id, l_paye_details_eff_date;
5167         CLOSE csr_tax_latest_run;
5168         hr_utility.trace('After csr_tax_latest_run, l_tax_run_result_id='||l_tax_run_result_id);
5169         hr_utility.trace('l_paye_details_eff_date='||fnd_date.date_to_displaydate(l_paye_details_eff_date));
5170         --
5171         OPEN csr_tax_latest_paye_run(l_assignment_id, l_last_effective_date);
5172         FETCH csr_tax_latest_paye_run INTO l_tax_paye_run_result_id, l_paye_eff_date;
5173         CLOSE csr_tax_latest_paye_run;
5174         hr_utility.trace('After csr_tax_latest_paye_run, l_tax_paye_run_result_id='||l_tax_paye_run_result_id);
5175         hr_utility.trace('l_paye_eff_date='||fnd_date.date_to_displaydate(l_paye_eff_date));
5176     END IF;
5177     --
5178     --
5179     -- archive_asg_info(g_tax_run_result_id_eid, nvl(l_tax_paye_run_result_id, l_tax_run_result_id));
5180     --
5181     -- Get tax code.
5182     -- First try PAYE element run results because PAYE details
5183     -- element run result may not be the same as the one effective on
5184     -- regular payment date of a payroll with positive
5185     -- offset
5186     OPEN csr_tax_paye_result(l_tax_paye_run_result_id);
5187     FETCH csr_tax_paye_result INTO l_tax_code, l_w1_m1_indicator,
5188                                     l_previous_taxable_pay,
5189                                     l_previous_tax_paid;
5190     hr_utility.trace('After csr_tax_paye_result, l_tax_code='||l_tax_code);
5191     hr_utility.trace('l_w1_m1_indicator='||l_w1_m1_indicator);
5192     hr_utility.trace('l_previous_taxable_pay='||to_char(l_previous_taxable_pay));
5193     hr_utility.trace('l_previous_tax_paid='||to_char(l_previous_tax_paid));
5194     IF l_tax_code IS NULL THEN
5195        -- Get the details from the element entry on the added criteria that
5196        -- there exists an updating action id on the element_entry.  In other
5197        -- words, an entry achieved using an Update Recurring rule.
5198        -- Nb. both cursors used here select max(...) so a row is returned
5199        -- even if no tax details found.
5200        --
5201        OPEN csr_tax_details_entry(l_assignment_id, l_effective_end_date,
5202                                l_payroll_end_year, 'Y');
5203        FETCH csr_tax_details_entry INTO  l_tax_code, l_w1_m1_indicator,
5204                                       l_previous_taxable_pay,
5205                                       l_previous_tax_paid;
5206        hr_utility.set_location(l_proc||' '||l_tax_code||' '||
5207                                l_w1_m1_indicator,52);
5208        IF l_tax_code IS NULL THEN
5209          -- no update recurring, so retrieve the details from the run result.
5210          OPEN csr_tax_details_result(l_tax_run_result_id);
5211          FETCH csr_tax_details_result INTO l_tax_code, l_w1_m1_indicator,
5212                                        l_previous_taxable_pay,
5213                                        l_previous_tax_paid;
5214          hr_utility.set_location(l_proc||' '||l_tax_code||' '||
5215                                  l_w1_m1_indicator,54);
5219            CLOSE csr_tax_details_entry;
5216          IF l_tax_code IS NULL THEN
5217            -- If there is still no tax code, use the element entry query
5218            -- without the update recurring criteria.
5220            OPEN csr_tax_details_entry(l_assignment_id, l_effective_end_date,
5221                                       l_payroll_end_year, 'N');
5222            FETCH csr_tax_details_entry INTO  l_tax_code, l_w1_m1_indicator,
5223                                              l_previous_taxable_pay,
5224                                              l_previous_tax_paid;
5225            hr_utility.set_location(l_proc||' '||l_tax_code||' '||
5226                                    l_w1_m1_indicator,56);
5227            IF l_tax_code IS NOT NULL THEN
5228             IF l_aggregated_paye_flag = 'Y' THEN
5229                hr_utility.trace('This is an aggregated assignment.');
5230                --
5231                if l_w1_m1_indicator = 'C' then
5232                   l_w1_m1_indicator := ' ';
5233                else
5234                   hr_utility.trace('No of periods per year = '||g_no_per_fiscal_yr(l_payroll_id));
5235                   if g_no_per_fiscal_yr(l_payroll_id) in (1,2,4,6,12,24) then
5236                      l_w1_m1_indicator := 'M';
5237                   else
5238                      l_w1_m1_indicator := 'W';
5239                   end if;
5240                end if;
5241                hr_utility.trace('Aggregated tax code found so far='||g_agg_balance_totals.tax_code);
5242                hr_utility.trace('g_agg_balance_totals.paye_eff_date = '||fnd_date.date_to_displaydate(g_agg_balance_totals.paye_eff_date));
5243                -- this tax code is from the PAYE Details Entry, since it is an
5244                -- aggregated assignment therefore check if we already have a
5245                -- a tax code from a previously processed assignment of this employee
5246                IF g_agg_balance_totals.tax_code is NULL THEN
5247                   hr_utility.trace('Aggregated tax code found so far is NULL hence store '||l_tax_code||' as the aggregated tax code');
5248                   -- No tax code found on other assignments so far therefore store
5249                   -- tax code found on element entry of this assignment,
5250                   -- this value will be archived against the primary eoy action later
5251                   g_agg_balance_totals.tax_code := l_tax_code;
5252                   g_agg_balance_totals.tax_basis := l_w1_m1_indicator;
5253                   g_agg_balance_totals.pay_previous := l_previous_taxable_pay;
5254                   g_agg_balance_totals.tax_previous := l_previous_tax_paid;
5255                   g_agg_balance_totals.paye_eff_date := fnd_date.canonical_to_date('0001/01/01 00:00:00');
5256                END IF;
5257                --
5258                IF nvl(g_agg_balance_totals.week_53, ' ') = ' ' THEN
5259                   g_agg_balance_totals.week_53 := l_week_53_indicator;
5260                END IF;
5261             END IF; -- aggregated PAYE flag = Y
5262            END IF; -- tax code found on PAYE Details entry
5263          ELSE -- tax code found on PAYE Details Result
5264             IF l_aggregated_paye_flag = 'Y' THEN
5265                --
5266                if l_w1_m1_indicator = 'C' then
5267                   l_w1_m1_indicator := ' ';
5268                else
5269                   if g_no_per_fiscal_yr(l_payroll_id) in (1,2,4,6,12,24) then
5270                      l_w1_m1_indicator := 'M';
5271                   else
5272                      l_w1_m1_indicator := 'W';
5273                   end if;
5274                end if;
5275                -- this is aggregated assignment hence check whether this is the
5276                -- latest run result so far
5277                IF l_paye_details_eff_date >
5278                             g_agg_balance_totals.paye_eff_date THEN
5279                   -- this is the latest paye run results amongst aggregated
5280                   -- assignment, store values in the table to archive
5281                   -- against the primary eoy action later
5282                   g_agg_balance_totals.tax_code := l_tax_code;
5283                   g_agg_balance_totals.tax_basis := l_w1_m1_indicator;
5284                   g_agg_balance_totals.pay_previous := l_previous_taxable_pay;
5285                   g_agg_balance_totals.tax_previous := l_previous_tax_paid;
5286                   g_agg_balance_totals.paye_eff_date := l_paye_details_eff_date;
5287                END IF;
5288                --
5289                IF nvl(g_agg_balance_totals.week_53, ' ') = ' ' THEN
5290                   g_agg_balance_totals.week_53 := l_week_53_indicator;
5291                END IF;
5292             END IF; -- aggregated PAYE flag = Y
5293          END IF; -- null or nor null tax code in pay details resluts
5294          CLOSE csr_tax_details_result;
5295        END IF; -- tax code null on update recurring PAYE Details entry
5296        CLOSE csr_tax_details_entry;
5297        --
5298     ELSE -- tax code found on PAYE Reun Result
5299        IF l_aggregated_paye_flag = 'Y' THEN
5300           --
5301           if l_w1_m1_indicator = 'C' then
5302              l_w1_m1_indicator := ' ';
5303           else
5304              if g_no_per_fiscal_yr(l_payroll_id) in (1,2,4,6,12,24) then
5305                 l_w1_m1_indicator := 'M';
5306              else
5307                 l_w1_m1_indicator := 'W';
5308              end if;
5309           end if;
5310           -- this is aggregated assignment hence check whether this is the
5311           -- latest run result so far
5312           IF l_paye_eff_date > g_agg_balance_totals.paye_eff_date THEN
5313              -- these are the latest paye run results amongst aggregated
5314              -- assignment, store them in the table to archive
5318              g_agg_balance_totals.pay_previous := l_previous_taxable_pay;
5315              -- against the primary eoy action later
5316              g_agg_balance_totals.tax_code := l_tax_code;
5317              g_agg_balance_totals.tax_basis := l_w1_m1_indicator;
5319              g_agg_balance_totals.tax_previous := l_previous_tax_paid;
5320              g_agg_balance_totals.paye_eff_date := l_paye_eff_date;
5321           END IF;
5322           --
5323           IF nvl(g_agg_balance_totals.week_53, ' ') = ' ' THEN
5324              g_agg_balance_totals.week_53 := l_week_53_indicator;
5325           END IF;
5326        END IF; -- aggregated PAYE flag = 'Y'
5327     END IF; --found/not found tax code on PAYE reun result
5328     CLOSE csr_tax_paye_result;
5329     --
5330     g_paye_archive := TRUE;
5331     END IF;
5332     -- End of Bug 6271548
5333     hr_utility.set_location(l_proc,60);
5334     /**************************************/
5335     /* assigned blank space to l_tax_code */
5336     /**************************************/
5337     if (l_tax_code IS NULL) then
5338          l_tax_code := ' ';
5339     else
5340          l_tax_code := ltrim(l_tax_code);
5341     end if;
5342     IF nvl(l_aggregated_paye_flag, 'N') <> 'Y' THEN
5343        -- archive these values for assignment that are not aggregated for PAYE,
5344        -- values stored in the global table will be archived against the
5345        -- eoy primary action for aggregated assignments later
5346        archive_asg_info(g_tax_code_eid,l_tax_code);
5347        archive_asg_info(g_prev_taxable_pay_eid,l_previous_taxable_pay);
5348        archive_asg_info(g_prev_tax_paid_eid,l_previous_tax_paid);
5349        --
5350        if l_w1_m1_indicator = 'C' then
5351           l_w1_m1_indicator := ' ';
5352        else
5353           if g_no_per_fiscal_yr(l_payroll_id) in (1,2,4,6,12,24) then
5354              l_w1_m1_indicator := 'M';
5355           else
5356              l_w1_m1_indicator := 'W';
5357           end if;
5358        end if;
5359        --
5360        archive_asg_info(g_week_53_indicator_eid,l_week_53_indicator);
5361        archive_asg_info(g_w1_m1_indicator_eid,l_w1_m1_indicator);
5362     END IF;
5363 
5364     -- Start of Bug 6271548 commented out fetching NI x total and able balances
5365     -- due to already fetch
5366 
5367     -- Get NI balances
5368     -- NI A
5369    /* l_ni_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nia_tot_id);
5370     l_ni_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5371                                                g_nia_able_id); */
5372     IF nvl(l_nia_tot,0) <> 0 or nvl(l_nia_able,0) <> 0 THEN
5373  -- IF nvl(l_ni_tot,0) <> 0 or nvl(l_ni_able,0) <> 0 THEN
5374       hr_utility.set_location(l_proc,70);
5375 
5376       l_ni_ees  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nia_id);
5377       l_ni_able_lel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5378                                                    g_nia_lel_id);
5379       l_ni_able_uel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5380                                                    g_nia_uel_id);
5381       --EOY 07/08 Begin
5382       l_ni_able_auel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5383                                                    g_nia_auel_id);
5384       --EOY 07/08 End
5385       l_ni_able_et  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5386                                                    g_nia_et_id);
5387       /* EOY 07/08 Begin
5388       archive_ni_values('A', l_nia_tot, l_nia_able, l_ni_ees, l_ni_able_lel,
5389                              l_ni_able_et, l_ni_able_uel);  */
5390       archive_ni_values('A', l_nia_tot, l_nia_able, l_ni_ees, l_ni_able_lel,
5391                              l_ni_able_et, l_ni_able_uel, l_ni_able_auel);
5392       /* EOY 07/08 End */
5393       l_count_values := l_count_values +1;
5394     END IF;
5395     l_ni_tot      := NULL;
5396     l_ni_ees      := NULL;
5397     l_ni_able     := NULL;
5398     l_ni_able_lel := NULL;
5399     l_ni_able_uel := NULL;
5400     l_ni_able_auel := NULL; --EOY 07/08 Begin
5401     l_ni_able_et  := NULL;
5402     -- NI B
5403     /* IF l_sex = 'F' THEN -- Cat B is for Females only Bug 4011263, EOY 2005*/
5404    /* l_ni_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5405                                               g_nib_tot_id);
5406     l_ni_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5407                                                  g_nib_able_id);*/
5408       IF nvl(l_nib_tot,0) <> 0 or nvl(l_nib_able,0) <> 0 THEN
5409    -- IF nvl(l_ni_tot,0) <> 0 or nvl(l_ni_able,0) <> 0 THEN
5410         hr_utility.set_location(l_proc,80);
5411         l_ni_ees  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5412                                                  g_nib_id);
5413         l_ni_able_lel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5414                                                      g_nib_lel_id);
5415         l_ni_able_uel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5416                                                      g_nib_uel_id);
5417         --EOY 07/08 Begin
5418         l_ni_able_auel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5419                                                      g_nib_auel_id);
5420         --EOY 07/08 End
5421         l_ni_able_et  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5422                                                      g_nib_et_id);
5423         /* EOY 07/08 Begin
5424         archive_ni_values('B', l_nib_tot, l_nib_able, l_ni_ees, l_ni_able_lel,
5425                                l_ni_able_et, l_ni_able_uel); */
5426         archive_ni_values('B', l_nib_tot, l_nib_able, l_ni_ees, l_ni_able_lel,
5427                                l_ni_able_et, l_ni_able_uel, l_ni_able_auel);
5428         /* EOY 07/08 End */
5429         l_count_values := l_count_values +1;
5430       END IF;
5431     /*END IF; -- l_sex = 'F'*/
5432     l_ni_tot      := NULL;
5433     l_ni_ees      := NULL;
5434     l_ni_able     := NULL;
5435     l_ni_able_lel := NULL;
5436     l_ni_able_uel := NULL;
5437     l_ni_able_auel := NULL; -- EOY 07/08
5438     l_ni_able_et  := NULL;
5439     l_ers_rebate  := NULL;
5440     -- NI C
5441    /*l_ni_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nic_tot_id);
5442     l_ni_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5443                                                  g_nic_able_id);*/
5444     IF nvl(l_nic_tot,0) <> 0 or nvl(l_nic_able,0) <> 0 THEN
5445  -- IF nvl(l_ni_tot,0) <> 0 or nvl(l_ni_able,0) <> 0 THEN
5446       hr_utility.set_location(l_proc,90);
5447       --archive_ni_values('C', l_ni_tot);
5448 -- Bug Fix 1976152, commented the above stmt, and included the below code
5449 -- to get the balance for NI C Employers Rebate and modified the call to
5450 -- archive_ni_values procedure
5451       l_ni_able_lel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5452                                                      g_nic_lel_id);
5453       l_ni_able_uel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5454                                                     g_nic_uel_id);
5455       --EOY 07/08 Begin
5456       l_ni_able_auel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5457                                                     g_nic_auel_id);
5458       --EOY 07/08 End
5459       l_ni_able_et  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5460                                                      g_nic_et_id);
5461       l_ers_rebate := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nic_ers_rebate_id);
5462       /* EOY 07/08 Begin
5463       archive_ni_values(p_ni_cat => 'C',
5464                         p_tot_contribs => l_nic_tot,
5465                         p_earnings => l_nic_able,
5466                         p_ni_able_lel => l_ni_able_lel,
5467                         p_ni_able_et  => l_ni_able_et,
5468                         p_ni_able_uel => l_ni_able_uel,
5469                         p_ers_rebate => l_ers_rebate);  */
5470       archive_ni_values(p_ni_cat => 'C',
5471                         p_tot_contribs => l_nic_tot,
5472                         p_earnings => l_nic_able,
5473                         p_ni_able_lel => l_ni_able_lel,
5474                         p_ni_able_et  => l_ni_able_et,
5475                         p_ni_able_uel => l_ni_able_uel,
5476                         p_ni_able_auel => l_ni_able_auel,
5477                         p_ers_rebate => l_ers_rebate);
5478      --EOY 07/08 End
5479 -- End of fix Bug 1976152
5480       l_count_values := l_count_values +1;
5481     END IF;
5482     l_ni_tot      := NULL;
5483     l_ni_ees      := NULL;
5484     l_ni_able     := NULL;
5485     l_ni_able_lel := NULL;
5486     l_ni_able_uel := NULL;
5487     l_ni_able_auel := NULL;  --EOY 07/08
5488     l_ni_able_et  := NULL;
5489     l_ers_rebate  := NULL;
5490     l_rebate_emp  := NULL;
5491     -- NI D
5492    /*l_ni_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nid_tot_id);
5493     l_ni_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5494                                                g_nid_able_id);*/
5495     IF nvl(l_nid_tot,0) <> 0 or nvl(l_nid_able,0) <> 0 THEN
5496  -- IF nvl(l_ni_tot,0) <> 0 or nvl(l_ni_able,0) <> 0 THEN
5497       hr_utility.set_location(l_proc,100);
5498       l_ni_ees  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nid_id);
5499       l_ni_able_lel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5500                                                    g_nid_lel_id);
5501       l_ni_able_uel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5502                                                    g_nid_uel_id);
5503       --EOY 07/08 Begin
5504       l_ni_able_auel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5505                                                    g_nid_auel_id);
5506       --EOY 07/08 End
5507       l_ni_able_et  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5508                                                    g_nid_et_id);
5509       l_ers_rebate  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5513       l_rebate_emp  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5510                                                    g_nid_ers_rebate_id);
5511       l_ees_rebate  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5512                                                    g_nid_ees_rebate_id);
5514                                                    g_nid_rebate_emp_id);
5515       /*EOY 07/08 Begin
5516       archive_ni_values('D', l_nid_tot, l_nid_able, l_ni_ees, l_ni_able_lel,
5517                         l_ni_able_et, l_ni_able_uel,l_ers_rebate + l_ees_rebate - l_rebate_emp,
5518                         l_rebate_emp);   */
5519       archive_ni_values('D', l_nid_tot, l_nid_able, l_ni_ees, l_ni_able_lel,
5520                         l_ni_able_et, l_ni_able_uel, l_ni_able_auel,l_ers_rebate + l_ees_rebate - l_rebate_emp,
5521                         l_rebate_emp);
5522       --EOY 07/08 End
5523       l_count_values := l_count_values +1;
5524     END IF;
5525     l_ni_tot      := NULL;
5526     l_ni_ees      := NULL;
5527     l_ni_able     := NULL;
5528     l_ni_able_lel := NULL;
5529     l_ni_able_uel := NULL;
5530     l_ni_able_auel := NULL; --EOY 07/08
5531     l_ni_able_et  := NULL;
5532     l_ers_rebate  := NULL;
5533     l_ees_rebate  := NULL;
5534     -- NI E
5535     /* IF l_sex = 'F' THEN -- Cat E is for Females only Bug 4011263, EOY 2005*/
5536    /*l_ni_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_tot_id);
5537     l_ni_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_able_id);*/
5538     IF nvl(l_nie_tot,0) <> 0 or nvl(l_nie_able,0) <> 0 THEN
5539     --  IF nvl(l_ni_tot,0) <> 0 or nvl(l_ni_able,0) <> 0 THEN
5540         hr_utility.set_location(l_proc,110);
5541         l_ni_ees  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_id);
5542         l_ni_able_lel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_lel_id);
5543         l_ni_able_uel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_uel_id);
5544 	--EOY 07/08 Begin
5545         l_ni_able_auel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_auel_id);
5546         --EOY 07/08 End
5547         l_ni_able_et  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_et_id);
5548         l_ers_rebate  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nie_ers_rebate_id);
5549 	/*EOY 07/08 Begin
5550         archive_ni_values('E', l_nie_tot, l_nie_able, l_ni_ees, l_ni_able_lel,l_ni_able_et, l_ni_able_uel,l_ers_rebate);*/
5551         archive_ni_values('E', l_nie_tot, l_nie_able, l_ni_ees, l_ni_able_lel,l_ni_able_et, l_ni_able_uel, l_ni_able_auel, l_ers_rebate);
5552         --EOY 07/08 End
5553         l_count_values := l_count_values +1;
5554       END IF;
5555     /* END IF; -- l_sex = 'F' */
5556     l_ni_tot      := NULL;
5557     l_ni_ees      := NULL;
5558     l_ni_able     := NULL;
5559     l_ni_able_lel := NULL;
5560     l_ni_able_uel := NULL;
5561     l_ni_able_auel := NULL;  --EOY 07/08
5562     l_ni_able_et  := NULL;
5563     l_ers_rebate  := NULL;
5564     l_ees_rebate  := NULL;
5565     l_rebate_emp  := NULL;
5566    -- NI J
5567   /*l_ni_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nij_tot_id);
5568     l_ni_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5569                                                g_nij_able_id);*/
5570     IF nvl(l_nij_tot,0) <> 0 or nvl(l_nij_able,0) <> 0 THEN
5571  -- IF nvl(l_ni_tot,0) <> 0 or nvl(l_ni_able,0) <> 0 THEN
5572       hr_utility.set_location(l_proc,130);
5573       l_ni_ees  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nij_id);
5574       l_ni_able_lel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5575                                                    g_nij_lel_id);
5576       l_ni_able_uel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5577                                                    g_nij_uel_id);
5578       --EOY 07/08 Begin
5579       l_ni_able_auel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5580                                                    g_nij_auel_id);
5581       --EOY 07/08 End
5582       l_ni_able_et  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5583                                                    g_nij_et_id);
5584       /* EOY 07/08 Begin
5585       archive_ni_values('J', l_nij_tot, l_nij_able, l_ni_ees, l_ni_able_lel,
5586                              l_ni_able_et, l_ni_able_uel); */
5587       archive_ni_values('J', l_nij_tot, l_nij_able, l_ni_ees, l_ni_able_lel,
5588                              l_ni_able_et, l_ni_able_uel, l_ni_able_auel);
5589       --EOY 07/08 End
5590       l_count_values := l_count_values +1;
5591     END IF;
5592     l_ni_tot      := NULL;
5593     l_ni_ees      := NULL;
5594     l_ni_able     := NULL;
5595     l_ni_able_lel := NULL;
5596     l_ni_able_uel := NULL;
5597     l_ni_able_auel := NULL; -- EOY 07/08
5598     l_ni_able_et  := NULL;
5599 
5600 -- NI L
5601   /*l_ni_tot := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nil_tot_id);
5602     l_ni_able := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5603                                                g_nil_able_id);*/
5604     IF nvl(l_nil_tot,0) <> 0 or nvl(l_nil_able,0) <> 0 THEN
5605  -- IF nvl(l_ni_tot,0) <> 0 or nvl(l_ni_able,0) <> 0 THEN
5606       hr_utility.set_location(l_proc,140);
5607       l_ni_ees  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,g_nil_id);
5608       l_ni_able_lel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5609                                                    g_nil_lel_id);
5610       l_ni_able_uel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5611                                                    g_nil_uel_id);
5612       --EOY 07/08 Begin
5613       l_ni_able_auel := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5614                                                    g_nil_auel_id);
5618       /*EOY 07/08 Begin
5615       --EOY 07/08 End
5616       l_ni_able_et  := 100 * hr_dirbal.get_balance(l_last_asg_action_id,
5617                                                    g_nil_et_id);
5619       archive_ni_values('L', l_nil_tot, l_nil_able, l_ni_ees, l_ni_able_lel,
5620                              l_ni_able_et, l_ni_able_uel);  */
5621       archive_ni_values('L', l_nil_tot, l_nil_able, l_ni_ees, l_ni_able_lel,
5622                              l_ni_able_et, l_ni_able_uel, l_ni_able_auel);
5623       --EOY 07/08 End
5624       l_count_values := l_count_values +1;
5625     END IF;
5626     l_ni_tot      := NULL;
5627     l_ni_ees      := NULL;
5628     l_ni_able     := NULL;
5629     l_ni_able_lel := NULL;
5630     l_ni_able_uel := NULL;
5631     l_ni_able_auel := NULL;  --EOY 07/08
5632     l_ni_able_et  := NULL;
5633 
5634     -- End of Bug 6271548
5635 
5636     -- populate NI F, NI G and/or NI S values
5637     -- sum the NI F/G/S Total Balances
5638 /* 4221300: remove call to hr_gbbal.ni_category_exists_in_year
5639     IF HR_GBBAL.NI_CATEGORY_EXISTS_IN_YEAR(l_last_asg_action_id,'F') = 1
5640        OR HR_GBBAL.NI_CATEGORY_EXISTS_IN_YEAR(l_last_asg_action_id,'G') = 1
5641        OR HR_GBBAL.NI_CATEGORY_EXISTS_IN_YEAR(l_last_asg_action_id,'S') = 1
5642        THEN
5643 4221300 */
5644       -- F/G/S Total(s) exist(s)
5645       hr_utility.set_location(l_proc,120);
5646       -- open cursor and populate year end values
5647       DECLARE
5648         l_sum_ni_tot      NUMBER:= 0;
5649         l_sum_ni_ees      NUMBER:= 0;
5650         l_sum_ni_able     NUMBER:= 0;
5651         l_sum_ni_able_lel NUMBER:= 0;
5652         l_sum_ni_able_uel NUMBER:= 0;
5653 	l_sum_ni_able_auel NUMBER:= 0;  --EOY 07/08
5654         l_sum_ni_able_et  NUMBER:= 0;
5655         l_sum_ers_rebate  NUMBER:= 0;
5656         l_sum_ees_rebate  NUMBER:= 0;
5657         l_sum_rebate_emp  NUMBER:= 0;
5658         l_ni_ers          NUMBER:= 0;
5659         l_store_cat       VARCHAR2(1):=NULL;
5660         l_store_scon      VARCHAR2(9):=NULL;
5661       BEGIN
5662         OPEN csr_get_scon_bal(l_last_asg_action_id,g_scon_input_id,
5663                               g_category_input_id, g_ni_id);
5664         LOOP
5665           FETCH csr_get_scon_bal INTO l_ni_cat, l_scon, l_ni_able,
5666                                   l_ni_tot, l_ni_ees, l_ni_ers,
5667                                   l_ni_able_et,
5668                                   l_ni_able_lel, l_ni_able_uel,
5669 				  --EOY 07/08 Begin
5670                                   l_ni_able_auel,
5671                                   --EOY 07/08 End
5672                                   l_ers_rebate, l_ees_rebate, l_rebate_emp;
5673           IF (csr_get_scon_bal%notfound
5674               OR l_ni_cat <> l_store_cat
5675               OR l_store_scon <> l_scon)
5676           AND csr_get_scon_bal%rowcount > 0 THEN
5677             -- first row will not come here due to null trap with cat and scon
5678             -- write values from store
5679             hr_utility.trace('l_sum_ni_tot='||l_sum_ni_tot);
5680             hr_utility.trace('l_sum_ni_able='||l_sum_ni_able);
5681             -- Add following IF condition for 4221300
5682             IF (nvl(l_sum_ni_tot,0) <> 0 or nvl(l_sum_ni_able,0) <> 0) THEN
5683                archive_ni_values(l_store_cat, l_sum_ni_tot, l_sum_ni_able,
5684                                  l_sum_ni_ees, l_sum_ni_able_lel,
5685                                  l_sum_ni_able_et, l_sum_ni_able_uel,
5686 				 --EOY 07/08 Begin
5687                                  l_sum_ni_able_auel,
5688                                  --EOY 07/08 End
5689                                  l_sum_ers_rebate + l_sum_ees_rebate - l_sum_rebate_emp, l_sum_rebate_emp, l_store_scon);
5690                l_count_values := l_count_values +1;
5691             END IF; -- Bug 4221300
5692             --
5693             EXIT WHEN csr_get_scon_bal%notfound;
5694             -- reset totalling variables
5695             l_sum_ni_tot      := 0;
5696             l_sum_ni_ees      := 0;
5697             l_sum_ni_able     := 0;
5698             l_sum_ni_able_lel := 0;
5699             l_sum_ni_able_uel := 0;
5700 	    l_sum_ni_able_auel := 0; --EOY 07/08
5701             l_sum_ni_able_et  := 0;
5702             l_sum_ers_rebate  := 0;
5703             l_sum_ees_rebate  := 0;
5704             l_sum_rebate_emp  := 0;
5705           END IF;
5706           EXIT WHEN csr_get_scon_bal%notfound;
5707           -- add values to backup;
5708 /* Bug 4169542: Process NI Cat S like other cats:
5709           IF l_ni_cat = 'S' THEN
5710             l_sum_ni_tot      := l_sum_ni_tot      + l_ni_ers;
5711 -- Bug# 1794175,added the following stmt so that l_sum_ers_rebate value
5712 -- passed on to archive_ni_values procedure, earlier the value was being passed as zero
5713             l_sum_ers_rebate  := l_sum_ers_rebate  + l_ers_rebate;
5714          ELSE
5715 */
5716             l_sum_ni_tot      := l_sum_ni_tot      + l_ni_tot;
5717             l_sum_ni_ees      := l_sum_ni_ees      + l_ni_ees;
5718             l_sum_ni_able     := l_sum_ni_able     + l_ni_able;
5719             l_sum_ni_able_lel := l_sum_ni_able_lel + l_ni_able_lel;
5720             l_sum_ni_able_uel := l_sum_ni_able_uel + l_ni_able_uel;
5721 	    l_sum_ni_able_auel := l_sum_ni_able_auel + l_ni_able_auel; --EOY 07/08
5722             l_sum_ni_able_et  := l_sum_ni_able_et  + l_ni_able_et;
5723             l_sum_ers_rebate  := l_sum_ers_rebate  + l_ers_rebate;
5727           END IF;
5724             l_sum_ees_rebate  := l_sum_ees_rebate  + l_ees_rebate;
5725             l_sum_rebate_emp  := l_sum_rebate_emp + l_rebate_emp;
5726 /* Bug 4169542: Process NI Cat S like other cats: remove End IF
5728 */
5729           --store new cat and scon
5730           l_store_cat  := l_ni_cat;
5731           l_store_scon := l_scon;
5732         END LOOP;
5733         CLOSE csr_get_scon_bal;
5734       END;
5735 -- Bug 4221300    END IF; -- F/G/S Total(s) exist(s)
5736 
5737     --
5738     -- If no NI values were archived, archive the current category.
5739     IF l_count_values = 0 THEN
5740       OPEN csr_current_cat(l_assignment_id, l_effective_end_date,
5741                            l_payroll_end_year);
5742       FETCH csr_current_cat INTO l_ni_cat, l_scon;
5743       IF csr_current_cat%found THEN
5744         hr_utility.set_location(l_proc,160);
5745         archive_ni_values(p_ni_cat       => l_ni_cat,
5746                           p_tot_contribs => 0,
5747                           p_scon         => l_scon);
5748       ELSE
5749         archive_ni_values('X',0);
5750       END IF;
5751       CLOSE csr_current_cat;
5752     END IF;
5753     --
5754     IF l_multiple_asg_flag = 'Y' THEN
5755       -- Do Multiple Assignment Logic Part III
5756       -- If Aggregated PAYE flag is set, use the first assignment ID
5757       -- in the sequence, this is the rule for AggPAYE. nvl the
5758       -- assignment ID with the max number for the field.
5759       IF nvl(l_aggregated_paye_flag,'N') = 'Y' THEN
5760          hr_utility.set_location(l_proc,162);
5761          IF l_assignment_id < nvl(g_min_assignment_id,999999999) THEN
5762             hr_utility.set_location(l_proc,163);
5763             g_primary_action := p_assactid;
5764             g_min_assignment_id := l_assignment_id;
5765          END IF;
5766       ELSE
5767          -- The largest gross pay dictates the Primary Assignment
5768          -- where there is Aggregated NI only.
5769          -- Bug 2040738. >= rather than >, incase the gross pay for all multi
5770          -- assignments is zero.
5771          -- Bug 6084523: if the first assignment of the employee has negative
5772          -- gross pay then it should still initialize the g_primary_action
5773          -- therefore amended IF condition below to initialle when
5774          -- g_max_gross_pay is null, i.e., first assignment of the emp
5775            IF nvl(l_gross,0) >= g_max_gross_pay
5776                  OR g_max_gross_pay is null THEN
5777              -- store new max gross pay and asg act id
5778              hr_utility.set_location(l_proc,165);
5779              g_primary_action := p_assactid;
5780              g_max_gross_pay  := nvl(l_gross,0);
5781            END IF;
5782       END IF;
5783       -- store this action in g_asg_actions
5784       g_num_actions := g_num_actions +1;
5785       g_asg_actions(g_num_actions) := p_assactid;
5786     END IF; -- End of Multiple Assignment Logic Part III
5787   END IF; -- if no NI Y and no last action
5788   --
5789   IF l_multiple_asg_flag = 'Y' AND g_num_actions > 0
5790     -- If this is a multiple asg and at least 1 of the persons asgs was
5791     -- processed..
5792   THEN
5793     -- Do Multiple Assignment Logic Part IV
5794     -- Bug 1261138 - split MA logic III into III and IV to ensure that
5795     -- pl/sql tbl is flushed to DB even if last asg was not processed, but
5796     -- only if at least one asg was processed
5797     IF pay_gb_eoy_archive.get_arch_str(p_assactid,
5798                                        g_last_multi_asg_eid) = 'Y'
5799     THEN
5800       -- This is the last asg for the person in this tax ref
5801       -- archive the primary assignment flag against the primary action
5802       hr_utility.set_location(l_proc,170);
5803       ff_archive_api.create_archive_item
5804         (p_archive_item_id  => l_archive_item_id,
5805          p_user_entity_id   => g_eoy_primary_flag_eid,
5806          p_archive_value    => 'Y',
5807          p_action_id        => g_primary_action,
5808          p_legislation_code => 'GB',
5809          p_object_version_number => l_ovn,
5810          p_some_warning     => l_some_warning);
5811       --
5812       -- Loop through actions:
5813       BEGIN
5814         l_index1:=0;
5815         LOOP -- actions
5816           l_index1 := l_index1 + 1;
5817           hr_utility.set_location(l_proc||' '||g_asg_actions(l_index1),180);
5818           IF g_asg_actions(l_index1) = g_primary_action THEN
5819             -- This is the Primary action so Archive Balance values.
5820             -- Firstly the Aggregated PAYE values if necessary.
5821             IF nvl(l_aggregated_paye_flag,'N') = 'Y' then
5822               BEGIN
5823                  hr_utility.set_location(l_proc,181);
5824                  -- Get all values from the global table.
5825                   /* IF l_sex = 'F' THEN rerstored as a part of 2987008
5826                                      was earlier commented out for 2003,
5827                       And Commented out again for EOY 2005, Bug 4011263 */
5828                    archive_asg_info(g_smp_eid,g_agg_balance_totals.smp,
5829                                     g_primary_action);
5830                  /* END IF; */
5831                  --
5832                  archive_asg_info(g_ssp_eid,g_agg_balance_totals.ssp,
5833                                   g_primary_action);
5834                  archive_asg_info(g_sap_eid,g_agg_balance_totals.sap,
5835                                   g_primary_action);
5836                  archive_asg_info(g_spp_adopt_eid,
5837                         g_agg_balance_totals.spp_adopt,g_primary_action);
5838                  archive_asg_info(g_spp_birth_eid,
5839                         g_agg_balance_totals.spp_birth,g_primary_action);
5840                  archive_asg_info(g_gross_pay_eid,
5841                         g_agg_balance_totals.gross_pay,g_primary_action);
5842                  archive_asg_info(g_notional_pay_eid,
5843                         g_agg_balance_totals.notional,g_primary_action);
5844                  archive_asg_info(g_tax_paid_eid,
5845                         ABS(g_agg_balance_totals.paye),g_primary_action);
5846                  archive_asg_info(g_superannuation_paid_eid,
5847                         ABS(g_agg_balance_totals.superann),g_primary_action);
5848                  archive_asg_info(g_widows_and_orphans_eid,
5849                         g_agg_balance_totals.widows,g_primary_action);
5850                  archive_asg_info(g_taxable_pay_eid,
5851                         g_agg_balance_totals.taxable,g_primary_action);
5852                  archive_asg_info(g_student_loans_eid,
5853                         g_agg_balance_totals.student_ln,g_primary_action);
5854                  archive_asg_info(g_ni_arrears_eid,
5855                         g_agg_balance_totals.ni_arrears,g_primary_action);
5856                  archive_asg_info(g_tax_code_eid,
5857                         g_agg_balance_totals.tax_code, g_primary_action);
5858                  archive_asg_info(g_prev_taxable_pay_eid,
5859                         g_agg_balance_totals.pay_previous, g_primary_action);
5860                  archive_asg_info(g_prev_tax_paid_eid,
5861                         g_agg_balance_totals.tax_previous, g_primary_action);
5862                  archive_asg_info(g_w1_m1_indicator_eid,
5863                         g_agg_balance_totals.tax_basis, g_primary_action);
5864                  archive_asg_info(g_week_53_indicator_eid,
5865                         g_agg_balance_totals.week_53, g_primary_action);
5866                  --
5867                  IF g_agg_balance_totals.paye < 0 THEN
5868                     archive_asg_info(g_tax_refund_eid,'R',g_primary_action);
5869                  END IF;
5870                  --
5871                  IF g_agg_balance_totals.superann < 0 THEN
5872                     archive_asg_info(g_superannuation_refund_eid,'R',g_primary_action);
5873                  END IF;
5874                  hr_utility.set_location(l_proc,182);
5875               --
5876               EXCEPTION WHEN NO_DATA_FOUND THEN
5877                  NULL; -- All values archived
5878               END; -- Agg PAYE Block
5879             END IF; -- Agg PAYE Flag
5880             --
5881             -- Archive the NI totals from the cache against this action:
5882             BEGIN
5883               l_index2:=0;
5884               LOOP -- values
5885                 l_index2 := l_index2 + 1;
5886                  -- EOY 2004. If the Total Contribution for each category is
5887                  -- negative, archive a refund flag.
5888                 if g_ni_balance_totals(l_index2).tot_contribs < 0 then
5889                    l_ni_refund_flag := 'R';
5890                 else
5891                    l_ni_refund_flag := '';
5892                 end if;
5893                 IF g_ni_balance_totals(l_index2).scon IS NOT NULL THEN
5894                   hr_utility.set_location(l_proc||' '||
5895                                  g_ni_balance_totals(l_index2).ni_cat||' '||
5896                                  g_ni_balance_totals(l_index2).scon,190);
5897                   archive_ni_value(g_ni_scon_earnings_eid,
5898                     g_ni_balance_totals(l_index2).earnings,2,
5899                     g_ni_balance_totals(l_index2).ni_cat,
5900                     g_ni_balance_totals(l_index2).scon,
5901                     g_primary_action);
5902                   archive_ni_value(g_ni_scon_ees_contribution_eid,
5903                     g_ni_balance_totals(l_index2).ees_contribs,2,
5904                     g_ni_balance_totals(l_index2).ni_cat,
5905                     g_ni_balance_totals(l_index2).scon,
5906                     g_primary_action);
5907                   archive_ni_value(g_ni_scon_tot_contribution_eid,
5908                     g_ni_balance_totals(l_index2).tot_contribs,2,
5909                     g_ni_balance_totals(l_index2).ni_cat,
5910                     g_ni_balance_totals(l_index2).scon,
5911                     g_primary_action);
5912                   archive_ni_value(g_ni_scon_able_et_eid,
5913                     g_ni_balance_totals(l_index2).ni_able_et,2,
5914                     g_ni_balance_totals(l_index2).ni_cat,
5915                     g_ni_balance_totals(l_index2).scon,
5916                     g_primary_action);
5917                   archive_ni_value(g_ni_scon_able_uel_eid,
5918                     g_ni_balance_totals(l_index2).ni_able_uel,2,
5919                     g_ni_balance_totals(l_index2).ni_cat,
5920                     g_ni_balance_totals(l_index2).scon,
5921                     g_primary_action);
5922 		  --EOY 07/08 Begin
5923                   archive_ni_value(g_ni_scon_able_auel_eid,
5924                     g_ni_balance_totals(l_index2).ni_able_auel,2,
5925                     g_ni_balance_totals(l_index2).ni_cat,
5926                     g_ni_balance_totals(l_index2).scon,
5927                     g_primary_action);
5928                   --EOY 07/08 End
5929                   archive_ni_value(g_ni_scon_able_lel_eid,
5930                     g_ni_balance_totals(l_index2).ni_able_lel,2,
5931                     g_ni_balance_totals(l_index2).ni_cat,
5935                     g_ni_balance_totals(l_index2).ers_rebate,2,
5932                     g_ni_balance_totals(l_index2).scon,
5933                     g_primary_action);
5934                   archive_ni_value(g_ni_scon_ers_rebate_eid,
5936                     g_ni_balance_totals(l_index2).ni_cat,
5937                     g_ni_balance_totals(l_index2).scon,
5938                     g_primary_action);
5939                   archive_ni_value(g_ni_scon_ees_rebate_eid,
5940                     g_ni_balance_totals(l_index2).ees_rebate,2,
5941                     g_ni_balance_totals(l_index2).ni_cat,
5942                     g_ni_balance_totals(l_index2).scon,
5943                     g_primary_action);
5944                   archive_ni_value(g_ni_scon_refund_eid,
5945                     l_ni_refund_flag,2,
5946                     g_ni_balance_totals(l_index2).ni_cat,
5947                     g_ni_balance_totals(l_index2).scon,
5948                     g_primary_action);
5949                 ELSE
5950                   hr_utility.set_location(l_proc||' '||
5951                                  g_ni_balance_totals(l_index2).ni_cat,200);
5952                   archive_ni_value(g_ni_earnings_eid,
5953                     g_ni_balance_totals(l_index2).earnings,2,
5954                     g_ni_balance_totals(l_index2).ni_cat,
5955                     p_actid => g_primary_action);
5956                   archive_ni_value(g_ni_ees_contribution_eid,
5957                     g_ni_balance_totals(l_index2).ees_contribs,2,
5958                     g_ni_balance_totals(l_index2).ni_cat,
5959                     p_actid => g_primary_action);
5960                   archive_ni_value(g_ni_tot_contribution_eid,
5961                     g_ni_balance_totals(l_index2).tot_contribs,2,
5962                     g_ni_balance_totals(l_index2).ni_cat,
5963                     p_actid => g_primary_action);
5964                   archive_ni_value(g_ni_able_et_eid,
5965                     g_ni_balance_totals(l_index2).ni_able_et,2,
5966                     g_ni_balance_totals(l_index2).ni_cat,
5967                     p_actid => g_primary_action);
5968                   archive_ni_value(g_ni_able_uel_eid,
5969                     g_ni_balance_totals(l_index2).ni_able_uel,2,
5970                     g_ni_balance_totals(l_index2).ni_cat,
5971                     p_actid => g_primary_action);
5972 		--EOY 07/08 Begin
5973                   archive_ni_value(g_ni_able_auel_eid,
5974                     g_ni_balance_totals(l_index2).ni_able_auel,2,
5975                     g_ni_balance_totals(l_index2).ni_cat,
5976                     p_actid => g_primary_action);
5977                   --EOY 07/08 End
5978                   archive_ni_value(g_ni_able_lel_eid,
5979                     g_ni_balance_totals(l_index2).ni_able_lel,2,
5980                     g_ni_balance_totals(l_index2).ni_cat,
5981                     p_actid => g_primary_action);
5982                   archive_ni_value(g_ni_ers_rebate_eid,
5983                     g_ni_balance_totals(l_index2).ers_rebate,2,
5984                     g_ni_balance_totals(l_index2).ni_cat,
5985                     p_actid => g_primary_action);
5986                   archive_ni_value(g_ni_ees_rebate_eid,
5987                     g_ni_balance_totals(l_index2).ees_rebate,2,
5988                     g_ni_balance_totals(l_index2).ni_cat,
5989                     p_actid => g_primary_action);
5990                   archive_ni_value(g_ni_refund_eid,
5991                     l_ni_refund_flag,2,
5992                     g_ni_balance_totals(l_index2).ni_cat,
5993                     g_ni_balance_totals(l_index2).scon,
5994                     g_primary_action);
5995                 END IF; -- scon is null
5996               END LOOP; -- values
5997               EXCEPTION WHEN no_data_found THEN
5998                 -- all NI values archived
5999                 NULL;
6000             END; -- NI values block
6001           ELSE
6002             hr_utility.set_location(l_proc,210);
6003             -- not primary action, so archive reportable cat X 'row'.
6004             -- Need to archive the Total Contributions item as 0 as the
6005             -- values view relies on this item being in the archive in order
6006             -- for a row to be returned (it uses this item to obtain the
6007             -- contexts of reportable, NI Cat and (if necessary) SCON.
6008             archive_ni_value(g_ni_tot_contribution_eid,'0',1,'X',
6009                              p_actid => g_asg_actions(l_index1));
6010           END IF; -- primary action
6011         END LOOP; -- actions
6012       EXCEPTION WHEN no_data_found THEN
6013         -- all actions processed
6014         NULL;
6015       END; -- action block
6016       --
6017       IF l_reportable_ni_archived THEN
6018          hr_utility.trace('Reportable NI values archived against primary action='||g_primary_action);
6019          archive_asg_info(g_reportable_ni_eid, 'Y', g_primary_action);
6020       END IF;
6021       --
6022       empty_masg_cache;
6023     END IF; -- l_last_multi_asg
6024   ELSE -- End of Multiple Assignment Logic Part IV
6025     -- This is not an employee with aggregated multiple assignments
6026     IF l_reportable_ni_archived THEN
6027        hr_utility.trace('Reportable NI values archived against current action='||p_assactid);
6028        archive_asg_info(g_reportable_ni_eid, 'Y');
6029     END IF;
6030   END IF;
6031   --
6032   hr_utility.set_location(' Leaving: '||l_proc,220);
6033 EXCEPTION
6034    WHEN ASG_ACTION_ERROR THEN
6035       hr_utility.trace('ASG_ACTION_ERROR exception raised.');
6036       hr_utility.set_location(' Leaving: '||l_proc,230);
6037       raise;
6038 END archive_code;
6039 --
6040 PROCEDURE extract_item_report_format(p_user_entity_name    in   varchar2,
6041                                      p_archive_type        in   varchar2) IS
6042 -- This procedure inserts the necessary data into the
6043 -- PAY_REPORT_FORMAT_ITEMS_F table FOR EXTRACT ARCHIVE ITEMS ONLY.
6044 -- This distinction must be made as the procedure contains hard-
6048 -- The Datetracking is 'handled' in this case by entering
6045 -- coded data, only relevant for extract items, ie those DBI/
6046 -- User Entities starting 'X_'. Do not use this utility for
6047 -- entering other data into these tables.
6049 -- start of time and end of time for all records. Again, this
6050 -- is specific to Extract Items.
6051   --
6052   cursor csr_get_user_entity_id(c_user_entity_name VARCHAR2) IS
6053   SELECT user_entity_id
6054   FROM   ff_user_entities
6055   WHERE  user_entity_name = c_user_entity_name;
6056   --
6057   l_user_entity_id      NUMBER;
6058   invalid_archive_type  EXCEPTION;
6059   --
6060 BEGIN
6061   --
6062   hr_utility.trace('Extract Item: '||p_user_entity_name);
6063   -- Retrieve user entity ID, also validates the entity.
6064   --
6065   OPEN csr_get_user_entity_id(p_user_entity_name);
6066   FETCH csr_get_user_entity_id INTO l_user_entity_id;
6067   IF csr_get_user_entity_id%notfound THEN
6068     RAISE no_data_found;
6069   END IF;
6070   --
6071   -- Validate the Archive Type
6072   --
6073   IF p_archive_type NOT IN ('AAP','PA','AAC') THEN
6074     RAISE invalid_archive_type;
6075   END IF;
6076   --
6077   -- Parameters validated, insert the two rows into PAY_REPORT_FORMAT_ITEMS_F,
6078   -- ensuring the inserts are re-runnable.
6079   --
6080   BEGIN
6081     -- 1. Report category F
6082     --
6083     INSERT INTO pay_report_format_items_f
6084       (report_type,
6085        report_qualifier,
6086        report_category,
6087        user_entity_id,
6088        effective_start_date,
6089        effective_end_date,
6090        archive_type,
6091        updatable_flag,
6092        display_sequence)
6093     SELECT
6094       'EOY',
6095       'GB',
6096       'F',
6097       l_user_entity_id,
6098       to_date('01/01/0001','DD/MM/YYYY'),
6099       to_date('31/12/4712','DD/MM/YYYY'),
6100       p_archive_type,
6101       'N',
6102       NULL
6103     FROM sys.dual WHERE NOT EXISTS
6104         (SELECT 1 FROM pay_report_format_items_f
6105          WHERE report_type = 'EOY'
6106          AND report_qualifier = 'GB'
6107          AND user_entity_id = l_user_entity_id
6108          AND report_category = 'F');
6109     --
6110     -- 2. Report category P
6111     --
6112     INSERT INTO pay_report_format_items_f
6113       (report_type,
6114        report_qualifier,
6115        report_category,
6116        user_entity_id,
6117        effective_start_date,
6118        effective_end_date,
6119        archive_type,
6120        updatable_flag,
6121        display_sequence)
6122     SELECT
6123       'EOY',
6124       'GB',
6125       'P',
6126       l_user_entity_id,
6127       to_date('01/01/0001','DD/MM/YYYY'),
6128       to_date('31/12/4712','DD/MM/YYYY'),
6129       p_archive_type,
6130       'N',
6131       NULL
6132     FROM sys.dual WHERE NOT EXISTS
6133           (SELECT 1 FROM pay_report_format_items_f
6134            WHERE report_type = 'EOY'
6135            AND report_qualifier = 'GB'
6136            AND user_entity_id = l_user_entity_id
6137            AND report_category = 'P');
6138   END;
6139   --
6140 EXCEPTION
6141   WHEN invalid_archive_type THEN
6142     hr_utility.set_message(800, 'FF_34958_INVALID_ARCHIVE_TYPE');
6143     hr_utility.raise_error;
6144     --
6145 END extract_item_report_format;
6146 --
6147 END pay_gb_eoy_archive;