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