DBA Data[Home] [Help]

PACKAGE BODY: APPS.PAY_IE_P45_ARCHIVE

Source


1 PACKAGE BODY pay_ie_p45_archive AS
2 /* $Header: pyiep45.pkb 120.46.12020000.6 2013/03/14 08:00:28 rsahai ship $ */
3 /*
4 **
5 **  Copyright (C) 1999 Oracle Corporation
6 **  All Rights Reserved
7 **
8 **  IE P45 Archive Package
9 **
10 **  Change List
11 **  ===========
12 **
13 **  Date        Author   Reference Description
14 **  -----------+--------+---------+------------------------------
15 **  05 APR 2002 abhaduri  N/A        Created
16 **  10 JUN 2002 viviswan  2268282  XML Report generator
17 **                                 procedure added.
18 **  14 JUN 2002 Kavenkat           Modified the address information fields
19 **  09-JUL-2002 Kavenkat  2448728  Passed on the prepayment assignment action id
20 **                        2450336  to pay_ie_p45_archive.process_balance to archive
21 **                                 the balance 'Total Pay'.
22 **  10-JUL-2002 viviswan  2452564  Fixed EMEA BALANCE DEFINITION context getting
23 **                        2450279  archived for each chunk created by the PYUGEN
24 **                                 process.
25 **  12-JUL-2002 viviswan           Modified logic for setting Non-Cumm Flag
26 **                                 depending on Calculation Options
27 **  17-JUL-2002 viviswan  2466382  open cursor csr_check_def_exists_info closed
28 **  18-JUL-2002 viviswan  2466773  Total Pay to be claculated as IE Taxabale Pay_YTD
29 **                                 value and not Total Pay_PAYMENT.
30 **                                 Correctd Date format. Added additional joins
31 **                                 for XML Report Query.
32 **  19-JUL-2002 viviswan  2468773  Moved the EMEA BALANCE DEFINITION archive code
33 **                                 from archinit to range_cursor to avoid the same
34 **                                 context getting archived multiple times.
35 **  08-AUG-2002 viviswan  2452531  Modified the logic for archiving EMEA PAYROLL INFO
36 **                        2499841  context for XML Reporting
37 **                                 Modified to archive the RUN values in case of
38 **                                 Supplementary Run
39 **  09-AUG-2002 gbutler     11.5.8 Performance fix - added ORDERED hint
40 **                                 to csr_prepaid_assignments in action_creation
41 **                                 to resolve merge cartesian join in NOT EXISTS
42 **                                 subquery
43 **  29-OCT-2002 smrobins  2567139  Mutliplied perion_num by pay_periods per
44 **                                 period, where pay_period_per_period
45 **                                 attributed to associated payroll is
46 **                                 greater than 1. Also added mid period
47 **                                 functionality to derive mid period
48 **                                 leaver.
49 **  05-NOV-2002 smrobins           Changes period_num cursor to identify
50 **                                 assignment uniquely by effective start and
51 **                                 effective end dates
52 **  05-DEC-2002 viviswan  2643489  Performance changes and nocopy changes.
53 **  20-DEC-2002 smrobins           Changes to deriving weeks at class A
54 **                                 following changes to prsi balance
55 **                                 structure
56 **  15-MAY-2003 nsugavan  2943335  Changed PROCEDURE setup_standard_balance_table
57 **                                 to archive balance, IE Taxable Social Benefit
58 **                                 instead of balance, IE Benefit Amount
59 **                                 Also, commented the cursor cur_cal_option
60 **                                 and used IE Taxable Social Benefit balance value
61 **                                 instead to check presence of benefit amount.
62 **  12-AUG-2003 npershad 3079945   Changed the cursor csr_iea_weeks
63 **                                 to derive correct insurable weeks at class A
64 **  12-SEP-2003 npershad 3079945   Commented cusor crs_iea_weeks and added
65 **                                 call to pay_balance_pkg.get_value to derive
66 **                                 correct insurable weeks at class A in cursor
67 **                                 Cur_Act_Contexts
68 **  07-APR-2004 ssekhar  3436737   Added code to support K and M Employee and
69 **                                 Employer figures when a severance payment
70 **                                 exists
71 **  12-APR-2004 npershad 3567562   Modified the cursors csr_get_org_tax_address, csr_payroll_ifo
72 **                                 to restrict the details fetched based on the PAYE Reference.
73 **				   Added a new procedure get_paye_reference to get the PAYE reference
74 **                                 attributed to payrolls in a consolidation set.
75 **  07-JUN-2004 ssekhar  3669639   Changed the code so that l_prsi_cat is now
76 **                                 previous classes concatenated with K or M
77 **  28-JUN-2004 ssekhar  3669639   Changed the space between the classes so that
78 **                                 the display is uniform
79 **  24-AUG-2004 alikhar  3817846   Changed the cursor cur_child_pay_action to get the
80 **			 115.26	   correct maximum assignment action id
81 **  05-OCT-2004 aashokan 115.27    Fixed Case issue with cursor cur_defined_balance
82 **  06-NOV-2004 aashokan 115.28    Bug 3986018 - Added nvl in get_bal_arch_value function
83 **				   Bug 3991416 - Added period,frequency and date
84 **					         earned check to fetch single record
85 **  06-NOV-2004 npershad 115.29    Bug 3986250 - Modified the cursor Cur_Act_Contexts
86 **                                 to report correct total insurable weeks for class or subclass 'A'.
87 **  12-NOV-2004 Kthampan 115.30    Bug 4001524 - Pass g_archive_end_date to the cursor
88 **                                 cur_child_pay_action instead of the effective_date (session date)
89 **                                 to report correct total insurable weeks for class or subclass 'A'.
90 **  17-NOV-2004 alogue   115.31    Bug 4011305 - Added hints to csr_prepaid_assignments in
91 **                                 action_creation to force use of optimum plan.
92 **  20-NOV-2004 aashokan 115.32    Bug 4016508 - Tax credit and cut off to be 0 if tax basis is
93 **                                 emergency or emergency no pps.
94 **  22-NOV-2004 KThampan 115.33    Bug 4001524 - Modified cursor csr_prepaid_assignments to
95 **                                 use archive start date and archive end date
96 **  09-DEC-2004 aashokan 115.34    Bug 4050372 - Added new cursor to fetch tax basis on termination
97 **                                 date
98 **  10-jan-2005 npershad 115.35    Bug 4108423 - Modified the cursor Cur_Act_Contexts
99 **                                 to report correct total insurable weeks for class or subclass 'A'.
100 **  21-feb-2005 aashokan 115.36    Bug 4193738 - Modified cursor csr_prepaid_assignments to fetch only those
101 **				   records for which pre payment is run between a given period.
102 **  28-feb-2005 aashokan 115.37    Bug 4208273 - Modified subquery of cursor csr_prepaid_assignments
103 **  28-feb-2005 aashokan 115.38    Added act2.action_status='C' in subquery of cursor csr_prepaid_assignments
104 **  28-APR-2005 rrajaman 115.39    Removed pay_element_types_f join from cursor cur_non_cum_tax
105 **                                 for performance bug 4315023
106 **  24-MAY-2005 sgajula  115.40    Changed to refer new Information type IE_EMPLOYER_INFO to accomodate changes
107 **                                 for Employer Migration
108 **  16-JUN-2005 alikhar  115.41    Bug 4437249: Changed the cursor csr_get_arc_bal_value to get the balance
109 **                                 value for balance name with length more than 30 chars.
110 **  30-JUN-2005 sgajula  115.42    Initialized g_archive_end_date in archinit to support Retry Option.
111 **  06-JUL-2005 sgajula  115.43    Moved 'PA' archive code from range_cursor to archive_deinit,handled
112 **                                 case of zero pay but non-zero PAYE,avoid data corruption by locking,
113 **                                 and proper archiving of EMEA PAYROLL INFO
114 **  22-JUL-2005 sgajula  115.44    Called setup_standard_balance_table in archive_deinit(4508661)
115 **  26-SEP-2005 rrajaman 115.45    Added IE_EXEMPTION tax_basis condition to cursor cur_non_cum_tax(4619038).
116 **  29-SEP-2005 sgajula  115.46    Changed the deceased attribute tags(4641660)
117 **  24-OCT-2005 sgajula  115.47    Modified for 2006 Changes.
118 **  26-OCT-2005 sgajula  115.48    Changed NOTFOUND condition for cur_child_pay_action
119 **  07-Nov-2005 vikgupta 115.49    Modified the formversion and xml file version to 3 and 3.0(4721955)
120 **  08-Nov-2005 vikgupta 115.50    revet the xml file version from 3.0 to 1.0
121 **  08-Nov-2005 vikgupta 115.51    Warning message to be raised if PPSN and address are missing. Also
122 **                                 made noncumulative attribute values to true/false instead of Y/N.
123 **  09-Nov-2005 vikgupta 115.52    change the attribute firstname to firstnames.
124 **  09-Nov-2005 vikgupta 115.53    restrict the total prsi classes to be shown is 4. bug (4724788)
125 **  23-Dec-2005 sgajula  115.54    Fixed Period Number issues with Offset Payrolls (4906850)
126 **  06-Jan-2006 sgajula  115.55    Enabled the Start Date Parameter.
127 **  02-Feb-2006 rbhardwa 115.56    Changed supplementary P45 to report PRSI Class A.(5015438)
128 **  06-Feb-2006 rbhardwa 115.57    Changed supplementary P45 to report insurable weeks for PRSI Class A correctly.(5015438)
129 **  14-Feb-2006 rbhardwa 115.58    Changed supplementary P45 to not report Class A if class A insurable weeks are zero.
130 **                                 (5015438).
131 **  14-Feb-2006 sgajula  115.59    Changed get_arc_bal_value to improve the performance. Also changed to support view
132 **                                 changes to improve the performance(5005788)
133 **  15-Feb-2006 sgajula  115.60    changed cur_p45_paye_prsi_details. fetched employer_prsi(5005788)
134 **  21-Feb-2006 sgajula  115.61    removed  csr_all_payroll_info.Used csr_payroll_info to archive
135 **                                 EMEA BALANCE DEFINITION(4771780)
136 **  3-Mar-2006  sgajula  115.62    Changed to support user assignment status (5073577)
137 **  8-Mar-2006  rbhardwa 115.63    Added payroll parameter to the p45 report generator process.(5059862)
138 **  31-Mar-2006 sgajula  115.64    Changed to fetch PAYE Details from Run Results(5128377)
139 **  31-Jul-2006 vikgupta 115.65    Bug 5401393 - added abs for thistax in generate_xml.
140 **                                 Bug 5386432 - made modifications to ensure that if there are no
141 **                                 results, main P45 should be generated but if there are no run-results
142 **                                 previous P45 exists then no supplement P45 should be generated.
143 **                                 Bug 5383808 - If P45 is issued for employees which are hired in the same
144 **                                 tax year than the commencement date should be the latest hire date.
145 **                                 Similary thispay and thistax should be the period for which he was rehired.
146 **                                 and not for the entire tax year.
147 **  07-Aug-2006 vikgupta 115.66    In process_balance procedure, while calculation l_p45_last_bal_value
148 **                                 pass source_id of previous P45 archive instead of previous p45 action
149 **                                 for eg case hire in jan, run payroll, terminate, rehire in feb run payroll
150 **                                 now run P45 for jan and then for feb. In this case total pay and tax will be zero for feb.
151 **  08-Sep-2006 vikgupta 115.67    1. 5519933 - noncumulative attribute does not depend upon
152 **                                    social benefit balance.
153 **                                 2. 5510536 - Tax credit and cutoff should show either monthly or
154 **                                    weekly figures. Added cursor to display weekly figures for bi-weekly payroll
155 **                                 3. 5510536 - Insurable weeks were summing up the previous tax year
156 **                                    insurable weeks also, modified the cursor Cur_Act_Contexts in
157 **                                    process_balance to have date join.
158 **                                 4. 5510536 - PRSI figures displayed on P45 XML should be this employment
159 **                                    figures. So made changes in process_balance procedure.
160 **  22-Sep-2006 rbhardwa 115.70    Bug 5528450. Chaged tax credit and cutoff to Period Weekly Tax Credit and
161 **                                 Period Weekly Standard Rate Cutoff to archive correct values.
162 **  28-Sep-2006 sgajula  115.71    Bug 5519933. changed to report correct value against non cumulative flag
163 **                                 If employee is rehired in same tax year.
164 **  09-oct-2006 vikgupta 115.72    5597735 - Performance fix (the same was also raised in bug 5233518)
165 **                                 5591812 - to display PRSI classes only if its insurable weeks are not zero.
166 **                                 5600150 - Changed to report deceased flag correctly.
167 **  15-Jan-2007 sgajula  115.73    5758951 - If rehired on a new payroll, running P45 for old payroll does not
168 **                                           pick the employee.
169 **  04-Jun-2007 vikgupta 115.74    6144761 - Modified action creation code. As case was failing when the
170 **                                 the employee was terminated in one tax year but has FPD in next
171 **                                 tax year and has payroll and prepayments in that tax year.
172 **  07-nov-2007 rrajaman 115.75    6615117 - The p45 process is generating xml files with incorrect data.
173 **                                 For rehire having no child actions is showing -ve P45 details.
174 **  10-oct-2008 knadhan  115.76    7291676  - New P45 Changes effective from 01-jan-2009.
175 **  15-oct-2008 knadhan  115.77    7291676QA - Proper PPSN with suffices if any are displayed.
176 **  17-oct-2008 knadhan  115.78    7291676QA - Date of payment is reporting in wrong format .
177 **  8-Dec-2008  knadhan  115.79  7611974  Fixes for rehire scenario
178 ** 21-jan-2009  knadhan  115.80  7827732  Replace '-' by '' while reporting works_no
179 **  27-jan-2009 knadhan 115.81   8198702  Commented the check for termination date >2009 for main and supp p45
180 **  09-dec-2009 knadhan 115.82   9156332   intead of date earned, effective date is passed to get_supplementary_details
181 **  13-Apr-2010 knadhan 115.83   9189002  for emergency="true", noncumulative is set as true while it has to false
182 **  04-Jan-2011 abraghun 115.84  10225372 Changes to avoid impact of enabling skip terminated asg leg rule
183 **  15-Nov-2012 smeduri 115.85   13359530 2012: CHANGES FOR P45(1), P45(1) SUPPLEMENTARY AND P45(3)/P46
184 **  29-Nov-2012 rsahai  115.86   13359530-1 QA issue: Supplementary Run not picking any data.
185 **  5-dec-2012 smeduri  115.87   13359530-2 QA issue: FILE FORMAT TESTING FOR P45(1) SUPPLEMENTARY
186 **  20-dec-2011 smeduri 115.88   13513882 and P45 issues
187 **  22-dec-2011 smeduri 115.89   13013273 USCable Pay modified.
188 **  22-jun-2012 smeduri 115.92   14215867  BALANCES ISSUE WITH SUPP P45
189 **  20-jul-2012 smeduri 115.93   14197192 - IE P45 - HEADER INFORMATION FOR EMPLOYEES WHO LEFT LAST YEAR
190 **  03-oct-2012 rsahai  115.94   14627387 Class M issue, file getting rejected for this case.
191 **  07-jan-2013 rsahai  115.95   16075672 USC Cutoff-1,2 coming wrong for all except weekly, monthly.
192 **  14-mar-2013 rsahai  115.96   16407778 Replaced IE Taxable Social Benefit with IE Illness Benefit
193 --------------------------------------------------------------------------------------------------------
194 */
195 
196 TYPE balance_rec IS RECORD (
197   balance_type_id      NUMBER,
198   balance_dimension_id NUMBER,  defined_balance_id   NUMBER,
199   balance_narrative    VARCHAR2(30),
200   balance_name         VARCHAR2(60),
201   database_item_suffix VARCHAR2(30),
202   legislation_code     VARCHAR2(20));
203 
204 TYPE element_rec IS RECORD (
205   element_type_id      NUMBER,
206   input_value_id       NUMBER,
207   formula_id           NUMBER,
208   element_narrative    VARCHAR2(30));
209 
210 
211 TYPE balance_table   IS TABLE OF balance_rec   INDEX BY BINARY_INTEGER;
212 TYPE element_table   IS TABLE OF element_rec   INDEX BY BINARY_INTEGER;
213 
214 g_statutory_balance_table         balance_table;
215 g_statutory_balance_table_ppsn    balance_table;
216 
217 g_balance_archive_index           NUMBER := 0;
218 g_element_archive_index           NUMBER := 0;
219 g_max_element_index               NUMBER := 0;
220 g_max_user_balance_index          NUMBER := 0;
221 g_max_statutory_balance_index     NUMBER := 0;
222 
223 g_paye_details_element_id         NUMBER;
224 
225 g_tax_basis_id                    NUMBER;
226 g_prsi_cat_id                     NUMBER;
227 g_prsi_subcat_id                  NUMBER;
228 g_ins_weeks_id                    NUMBER;
229 
230 -- Global variables used to fetch Input value ids of Tax Credit and Cutoff 5128377
231 g_month_tax_rate		          NUMBER;
232 g_week_tax_rate                   NUMBER;
233 g_month_std_cutoff                NUMBER;
234 g_week_std_cutoff                 NUMBER;
235 g_period_week_tax_rate            NUMBER;  /* 5528450 */
236 g_period_week_std_cutoff          NUMBER;  /* 5528450 */
237 
238 
239 
240 g_package                CONSTANT VARCHAR2(30) := 'pay_ie_p45_archive.';
241 
242 g_archive_pact                    NUMBER;
243 g_archive_effective_date          DATE;
244 g_archive_start_date		    DATE;
245 g_archive_end_date		    DATE;
246 
247 g_paye_ref                        NUMBER;
248 
249 -------
250 FUNCTION test_XML(P_STRING VARCHAR2) RETURN VARCHAR2 AS
251 	l_string varchar2(1000);
252 
253 	FUNCTION replace_xml_symbols(pp_string IN VARCHAR2)
254 	RETURN VARCHAR2
255 	AS
256 
257 	ll_string   VARCHAR2(1000);
258 
259 	BEGIN
260 
261 
262 	ll_string :=  pp_string;
263 
264 	ll_string := replace(ll_string, '&', ';');
265 	ll_string := replace(ll_string, '<', '<');
266 	ll_string := replace(ll_string, '>', '>');
267 	ll_string := replace(ll_string, '''',';');
268 	ll_string := replace(ll_string, '"', ';');
269 
270 	RETURN ll_string;
271 	EXCEPTION when no_data_found then
272 	null;
273 	END replace_xml_symbols;
274 
275 begin
276 	l_string := p_string;
277 	l_string := replace_xml_symbols(l_string);
278 	l_string := pay_ie_p35_magtape.test_XML(l_string);
279 
280 RETURN l_string;
281 END ; --13359530
282 PROCEDURE get_parameters(p_payroll_action_id IN  NUMBER,
283                          p_token_name        IN  VARCHAR2,
284                          p_token_value       OUT nocopy VARCHAR2) IS
285 
286 CURSOR csr_parameter_info(p_pact_id NUMBER,
287                           p_token   CHAR) IS
288 SELECT SUBSTR(legislative_parameters,
289                INSTR(legislative_parameters,p_token)+(LENGTH(p_token)+1),
290                 INSTR(legislative_parameters,' ',
291                        INSTR(legislative_parameters,p_token))
292                  - (INSTR(legislative_parameters,p_token)+LENGTH(p_token))),
293        business_group_id
294 FROM   pay_payroll_actions
295 WHERE  payroll_action_id = p_pact_id;
296 
297 l_business_group_id               VARCHAR2(20);
298 l_token_value                     VARCHAR2(50);
299 
300 l_proc                            VARCHAR2(50) := g_package || 'get_parameters';
301 
302 BEGIN
303 
304   hr_utility.set_location('Entering ' || l_proc,10);
305 
306   hr_utility.set_location('Step ' || l_proc,20);
307   hr_utility.set_location('p_token_name = ' || p_token_name,20);
308 
309   OPEN csr_parameter_info(p_payroll_action_id,
310                           p_token_name);
311 
312   FETCH csr_parameter_info INTO l_token_value,
313                                 l_business_group_id;
314 
315   CLOSE csr_parameter_info;
316 
317   IF p_token_name = 'BG_ID'
318 
319   THEN
320 
321      p_token_value := l_business_group_id;
322 
323   ELSE
324 
325      p_token_value := l_token_value;
326 
327   END IF;
328 
329   hr_utility.set_location('l_token_value = ' || l_token_value,20);
330   hr_utility.set_location('Leaving         ' || l_proc,30);
331 
332 END get_parameters;
333 
334 --------------------------------
335 FUNCTION get_lookup_meaning(
336                      p_lookup_type    in varchar2
337                     ,p_lookup_code    in varchar2 ) RETURN varchar2 IS
338 
339   CURSOR csr_get_lookup IS
340   SELECT meaning
341   FROM   hr_lookups
342   WHERE  lookup_type=p_lookup_type
343          AND lookup_code=p_lookup_code;
344 
345 p_lookup_meaning hr_lookups.meaning%TYPE;
346 
347 BEGIN
348   p_lookup_meaning := NULL;
349   OPEN csr_get_lookup;
350   FETCH csr_get_lookup INTO p_lookup_meaning;
351   CLOSE csr_get_lookup;
352 
353 RETURN(p_lookup_meaning);
354 
355 END get_lookup_meaning;
356 
357 --------------------------------
358 PROCEDURE setup_balance_definitions(p_pactid            IN NUMBER,
359                                     p_payroll_pact      IN NUMBER,
360                                     p_effective_date    IN DATE) IS
361 
362 l_action_info_id                  NUMBER(15);
363 l_ovn                             NUMBER(15);
364 l_index                           NUMBER;
365 
366 l_proc                            VARCHAR2(50) := g_package || 'setup_balance_definitions';
367 
368 BEGIN
369 
370 
371   hr_utility.set_location('Entering        ' || l_proc,10);
372   hr_utility.set_location('Step            ' || l_proc,20);
373 
374   FOR l_index IN 1 ..g_max_statutory_balance_index
375   LOOP
376 
377     hr_utility.set_location('p_pactid = '||p_pactid,20);
378     hr_utility.set_location('p_payroll_pact = '||p_payroll_pact,20);
379     hr_utility.set_location('p_effective_date = '||p_effective_date,20);
380     hr_utility.set_location('defined_balance_id = '||g_statutory_balance_table(l_index).defined_balance_id,20);
381     hr_utility.set_location('balance_name = '||g_statutory_balance_table(l_index).balance_name,20);
382 
383       pay_action_information_api.create_action_information (
384         p_action_information_id        =>  l_action_info_id
385       , p_action_context_id            =>  p_pactid
386       , p_action_context_type          =>  'PA'
387       , p_object_version_number        =>  l_ovn
388       , p_effective_date               =>  p_effective_date
389       , p_source_id                    =>  NULL
390       , p_source_text                  =>  NULL
391       , p_action_information_category  =>  'EMEA BALANCE DEFINITION'
392       , p_action_information1          =>  p_payroll_pact
393       , p_action_information2          =>  g_statutory_balance_table(l_index).defined_balance_id
394       , p_action_information3          =>  NULL
395       , p_action_information4          =>  g_statutory_balance_table(l_index).balance_name
396       , p_action_information7          =>  'N'   /*  7291676 */
397       );
398   END LOOP;
399 /* 7291676 */
400    hr_utility.set_location(' creating emea balance definations for PPSN override balance table ',20);
401 
402    FOR l_index IN 1 ..g_max_statutory_balance_index
403   LOOP
404 
405     hr_utility.set_location('p_pactid = '||p_pactid,20);
406     hr_utility.set_location('p_payroll_pact = '||p_payroll_pact,20);
407     hr_utility.set_location('p_effective_date = '||p_effective_date,20);
408     hr_utility.set_location('defined_balance_id = '||g_statutory_balance_table(l_index).defined_balance_id,20);
409     hr_utility.set_location('balance_name = '||g_statutory_balance_table(l_index).balance_name,20);
410 
411       pay_action_information_api.create_action_information (
412         p_action_information_id        =>  l_action_info_id
413       , p_action_context_id            =>  p_pactid
414       , p_action_context_type          =>  'PA'
415       , p_object_version_number        =>  l_ovn
416       , p_effective_date               =>  p_effective_date
417       , p_source_id                    =>  NULL
418       , p_source_text                  =>  NULL
419       , p_action_information_category  =>  'EMEA BALANCE DEFINITION'
420       , p_action_information1          =>  p_payroll_pact
421       , p_action_information2          =>  g_statutory_balance_table_ppsn(l_index).defined_balance_id
422       , p_action_information3          =>  NULL
423       , p_action_information4          =>  g_statutory_balance_table_ppsn(l_index).balance_name
424       , p_action_information7          =>  'Y'   /*  7291676 */
425       );
426 
427   END LOOP;
428 
429   hr_utility.set_location('Leaving ' || l_proc,30);
430 
431 END setup_balance_definitions;
432 ------------------------
433 /* This Procedure populates PL/SQL Table with balance names and defined_balance_id which are used to
434   archive Balance Names and their Values
435   Balances With Index 1-19 are Total YTD Balances and
436   Balances with Index 20 -33 are Balances used for Supplementary P45
437 */
438 PROCEDURE setup_standard_balance_table
439 IS
440 TYPE balance_name_rec IS RECORD (
441   balance_name VARCHAR2(60));
442 TYPE balance_id_rec IS RECORD (
443   defined_balance_id NUMBER);
444 TYPE balance_name_tab IS TABLE OF balance_name_rec INDEX BY BINARY_INTEGER;
445 TYPE balance_id_tab   IS TABLE OF balance_id_rec   INDEX BY BINARY_INTEGER;
446 l_statutory_balance balance_name_tab;
447 l_statutory_bal_id  balance_id_tab;
448 
449 /* 7291676 */
450 
451 l_statutory_balance_ppsn balance_name_tab;
452 l_statutory_bal_id_ppsn  balance_id_tab;
453 
454 CURSOR csr_balance_dimension(p_balance   IN CHAR,
455                              p_dimension IN CHAR) IS
456 SELECT pdb.defined_balance_id
457 FROM   pay_balance_types pbt,
458        pay_balance_dimensions pbd,
459        pay_defined_balances pdb
460 WHERE  pdb.balance_type_id = pbt.balance_type_id
461 AND    pdb.balance_dimension_id = pbd.balance_dimension_id
462 AND    pbt.balance_name = p_balance
463 AND    pbd.database_item_suffix = p_dimension
464 AND    pdb.legislation_code = 'IE';
465 l_archive_index                   NUMBER       := 1;
466 l_archive_index_ppsn                   NUMBER       := 1; -- 7291676
467 -- Balances used for YTD
468 l_dimension                       VARCHAR2(20) := '_PER_PAYE_REF_YTD'; -- 'PER_PAYE_REF_YTD'
469 l_dimension_1                     VARCHAR2(30) := '_PER_PAYE_REF_PRSI_YTD';
470 -- Balances used for Supp P45
471 l_dimension_2                     VARCHAR2(30) := '_ASG_PAYE_REF_PRSI_RUN';            -- Bug 5015438
472 l_dimension_pay                   VARCHAR2(16) := '_PAYMENTS';
473 l_dimension_run                   VARCHAR2(16) := '_ASG_RUN';
474 l_dimension_ptd                   VARCHAR2(16) := '_ASG_PTD';
475 
476 
477 /* 7291676 */
478 l_dimension_ppsn                  VARCHAR2(50) := '_PER_PAYE_REF_PPSN_YTD';
479 l_dimension_1_ppsn                VARCHAR2(30) := '_PER_PAYE_REF_PRSI_YTD';
480 l_dimension_2_ppsn                VARCHAR2(30) := '_ASG_PAYE_REF_PRSI_RUN';
481 l_dimension_run_ppsn              VARCHAR2(16) := '_ASG_RUN';
482 l_dimension_ptd_ppsn              VARCHAR2(16) := '_ASG_PTD';
483 
484 l_found                           VARCHAR2(1)  := 'N';
485 --l_max_stat_balance                NUMBER       := 19;
486 l_max_stat_balance                NUMBER       := 23; -- 13359530
487 l_pactid                          NUMBER;
488 l_payroll_pact                    NUMBER;
489 l_proc                            VARCHAR2(100) := g_package || 'setup_standard_balance_table';
490 BEGIN
491   hr_utility.set_location('Entering ' || l_proc,10);
492   hr_utility.set_location('Step ' || l_proc,20);
493   l_statutory_balance(1).balance_name  := 'IE Taxable Pay';
494   l_statutory_balance(2).balance_name  := 'IE Net Tax';
495   l_statutory_balance(3).balance_name  := 'IE PRSI Employer';
496   l_statutory_balance(4).balance_name  := 'IE PRSI Employee';
497   l_statutory_balance(5).balance_name  := 'IE Lump Sum';
498   l_statutory_balance(6).balance_name  := 'IE PRSI Insurable Weeks';
499 /*  l_statutory_balance(15).balance_name  := 'IE Reduced Tax Credit'; --13359530
500   l_statutory_balance(16).balance_name  := 'IE Reduced Std Rate Cut Off';
501   l_statutory_balance(17).balance_name  := 'IE Taxable Social Benefit';
502   l_statutory_balance(18).balance_name := 'IE P45 Pay';
503   l_statutory_balance(19).balance_name := 'IE P45 Tax Deducted'; */
504   l_statutory_balance(14).balance_name := 'IE PRSI_ClassA Insurable Weeks';             --  Bug 5015438
505   -- Added new balances which needs to be archived when a severance payment exists
506   l_statutory_balance(7).balance_name  := 'IE PRSI K Employee Lump Sum';
507   l_statutory_balance(8).balance_name  := 'IE PRSI M Employee Lump Sum';
508   l_statutory_balance(9).balance_name  := 'IE PRSI K Employer Lump Sum';
509   l_statutory_balance(10).balance_name  := 'IE PRSI M Employer Lump Sum';
510   l_statutory_balance(11).balance_name  := 'IE PRSI K Term Insurable Weeks';
511   l_statutory_balance(12).balance_name  := 'IE PRSI M Term Insurable Weeks';
512   l_statutory_balance(13).balance_name  := 'IE Term Health Levy';
513   -- 13359530 START
514 l_statutory_balance(17).balance_name  := 'IE Reduced Tax Credit';
515   l_statutory_balance(18).balance_name  := 'IE Reduced Std Rate Cut Off';
516   l_statutory_balance(19).balance_name  := 'IE Illness Benefit';  --'IE Taxable Social Benefit';  --16407778
517   l_statutory_balance(20).balance_name := 'IE P45 Pay';
518   l_statutory_balance(21).balance_name := 'IE P45 Tax Deducted';
519 
520   l_statutory_balance(15).balance_name  := 'IE USCable Pay';
521   l_statutory_balance(16).balance_name  := 'IE USC Balance';
522   l_statutory_balance(22).balance_name  := 'IE P45 USC Pay';
523   l_statutory_balance(23).balance_name  := 'IE P45 USC Deducted';
524   hr_utility.set_location('Step = ' || l_proc,30);
525   FOR l_index IN 1 .. l_max_stat_balance
526   LOOP
527   /* 7291676 */
528   l_statutory_balance_ppsn(l_index).balance_name:=l_statutory_balance(l_index).balance_name;
529   hr_utility.set_location(' PPSN Override balance_name = ' || l_statutory_balance_ppsn(l_index).balance_name,30);
530 
531     hr_utility.set_location('l_index      = ' || l_index,30);
532     hr_utility.set_location('balance_name = ' || l_statutory_balance(l_index).balance_name,30);
533     hr_utility.set_location('l_dimension  = ' || l_dimension,30);
534   --  IF (l_index < 15) THEN -- Stores RUN balance_defined information  -- Bug 5015438
535    IF (l_index < 17 ) THEN  --  Bug 13359530
536       IF l_statutory_balance(l_index).balance_name in ('IE PRSI Insurable Weeks','IE PRSI K Term Insurable Weeks','IE PRSI M Term Insurable Weeks','IE PRSI_ClassA Insurable Weeks') THEN
537 
538          IF l_index <> 14 THEN                                                          --Bug 5015438
539       /* If the Balance is IE PRSI Insurable Weeks or IE PRSI K Term Insurable Weeks or IE PRSI M Term Insurable Weeks then attach the dimension ASG_PTD for Supp P45 Balances*/
540            OPEN csr_balance_dimension(l_statutory_balance(l_index).balance_name,l_dimension_ptd);
541            l_statutory_balance(l_max_stat_balance + l_index).balance_name :=l_statutory_balance(l_index).balance_name || 'ASG_PTD';
542 	 ELSE
543        /* If the Balance is IE PRSI_ClassA Insurable Weeks attach the dimension _ASG_PAYE_REF_PRSI_RUN to it for Supp P45 balance*/
544            OPEN csr_balance_dimension('IE PRSI Insurable Weeks',l_dimension_2);
545            l_statutory_balance(l_max_stat_balance + l_index).balance_name :=l_statutory_balance(l_index).balance_name || 'ASG_PTD';
546 	 END IF;
547 
548      ELSE
549        /* In other cases attach the dimension ASG_RUN for Supp P45 Balance */
550         OPEN csr_balance_dimension(l_statutory_balance(l_index).balance_name,l_dimension_run);
551         l_statutory_balance(l_max_stat_balance + l_index).balance_name :=l_statutory_balance(l_index).balance_name || 'ASG_RUN';
552      END IF;
553 
554       FETCH csr_balance_dimension INTO l_statutory_bal_id(l_max_stat_balance + l_index).defined_balance_id;
555        IF csr_balance_dimension%NOTFOUND THEN
556             l_statutory_bal_id(l_max_stat_balance + l_index).defined_balance_id := 0;
557        END IF;
558       CLOSE csr_balance_dimension;
559       g_statutory_balance_table(l_max_stat_balance + l_index).defined_balance_id := l_statutory_bal_id(l_max_stat_balance + l_index).defined_balance_id;
560       g_statutory_balance_table(l_max_stat_balance + l_index).balance_name := l_statutory_balance(l_max_stat_balance + l_index).balance_name;
561 
562       IF l_index <> 14 THEN                 -- Bug 5015438
563          g_statutory_balance_table(l_max_stat_balance + l_index).database_item_suffix := l_dimension_run;
564       ELSE
565          g_statutory_balance_table(l_max_stat_balance + l_index).database_item_suffix := l_dimension_2;
566       END IF;
567 
568       l_archive_index := l_archive_index + 1;
569     END IF;
570     -- Stores ASG_YTD balance_defined information
571     IF l_index <> 14 THEN                                                                           -- Bug 5015438
572        OPEN csr_balance_dimension(l_statutory_balance(l_index).balance_name,l_dimension);
573     ELSE
574       OPEN csr_balance_dimension('IE PRSI Insurable Weeks',l_dimension_1);
575     END IF;
576 
577     FETCH csr_balance_dimension INTO l_statutory_bal_id(l_index).defined_balance_id;
578       IF csr_balance_dimension%NOTFOUND THEN
579          l_statutory_bal_id(l_index).defined_balance_id := 0;
580       END IF;
581     CLOSE csr_balance_dimension;
582     g_statutory_balance_table(l_index).defined_balance_id := l_statutory_bal_id(l_index).defined_balance_id;
583     g_statutory_balance_table(l_index).balance_name := l_statutory_balance(l_index).balance_name;
584     IF l_index <> 14 THEN                                                                            -- Bug 5015438
585        g_statutory_balance_table(l_index).database_item_suffix := l_dimension;
586   --     l_archive_index := l_archive_index + 1;
587     ELSE
588        g_statutory_balance_table(l_index).database_item_suffix := l_dimension_1;
589 
590     END IF;
591        l_archive_index := l_archive_index + 1;
592   END LOOP;
593 
594 /* 7291676  to create a new balance table for assignments with PPSN override values */
595 
596   FOR l_index IN 1 .. l_max_stat_balance
597   LOOP
598     hr_utility.set_location('l_index      = ' || l_index,30);
599     hr_utility.set_location('balance_name = ' || l_statutory_balance_ppsn(l_index).balance_name,30);
600     hr_utility.set_location('l_dimension_ppsn  = ' || l_dimension_ppsn,30);
601    -- IF (l_index < 15) THEN -- Stores RUN balance_defined information                    -- Bug 5015438
602     IF (l_index < 17 ) THEN  --  Bug 13359530
603       IF l_statutory_balance_ppsn(l_index).balance_name in ('IE PRSI Insurable Weeks','IE PRSI K Term Insurable Weeks','IE PRSI M Term Insurable Weeks','IE PRSI_ClassA Insurable Weeks') THEN
604 
605          IF l_index <> 14 THEN                                                          --Bug 5015438
606       /* If the Balance is IE PRSI Insurable Weeks or IE PRSI K Term Insurable Weeks or IE PRSI M Term Insurable Weeks then attach the dimension ASG_PTD for Supp P45 Balances*/
607            OPEN csr_balance_dimension(l_statutory_balance_ppsn(l_index).balance_name,l_dimension_ptd_ppsn);
608            l_statutory_balance_ppsn(l_max_stat_balance + l_index).balance_name :=l_statutory_balance_ppsn(l_index).balance_name || 'ASG_PTD';
609 	 ELSE
610        /* If the Balance is IE PRSI_ClassA Insurable Weeks attach the dimension _ASG_PAYE_REF_PRSI_RUN to it for Supp P45 balance*/
611            OPEN csr_balance_dimension('IE PRSI Insurable Weeks',l_dimension_2_ppsn);
612            l_statutory_balance_ppsn(l_max_stat_balance + l_index).balance_name :=l_statutory_balance_ppsn(l_index).balance_name || 'ASG_PTD';
613 	 END IF;
614 
615      ELSE
616        /* In other cases attach the dimension ASG_RUN for Supp P45 Balance */
617         OPEN csr_balance_dimension(l_statutory_balance_ppsn(l_index).balance_name,l_dimension_run_ppsn);
618         l_statutory_balance_ppsn(l_max_stat_balance + l_index).balance_name :=l_statutory_balance_ppsn(l_index).balance_name || 'ASG_RUN';
619      END IF;
620 
621       FETCH csr_balance_dimension INTO l_statutory_bal_id_ppsn(l_max_stat_balance + l_index).defined_balance_id;
622        IF csr_balance_dimension%NOTFOUND THEN
623             l_statutory_bal_id_ppsn(l_max_stat_balance + l_index).defined_balance_id := 0;
624        END IF;
625       CLOSE csr_balance_dimension;
626       g_statutory_balance_table_ppsn(l_max_stat_balance + l_index).defined_balance_id := l_statutory_bal_id_ppsn(l_max_stat_balance + l_index).defined_balance_id;
627       g_statutory_balance_table_ppsn(l_max_stat_balance + l_index).balance_name := l_statutory_balance_ppsn(l_max_stat_balance + l_index).balance_name;
628 
629       IF l_index <> 14 THEN                 -- Bug 5015438
630          g_statutory_balance_table_ppsn(l_max_stat_balance + l_index).database_item_suffix := l_dimension_run_ppsn;
631       ELSE
632          g_statutory_balance_table_ppsn(l_max_stat_balance + l_index).database_item_suffix := l_dimension_2_ppsn;
633       END IF;
634 
635       l_archive_index_ppsn := l_archive_index_ppsn + 1;
636     END IF;
637     -- Stores ASG_YTD balance_defined information
638     IF l_index <> 14 THEN                                                                           -- Bug 5015438
639        OPEN csr_balance_dimension(l_statutory_balance_ppsn(l_index).balance_name,l_dimension_ppsn);
640     ELSE
641       OPEN csr_balance_dimension('IE PRSI Insurable Weeks',l_dimension_1_ppsn);
642     END IF;
643 
644     FETCH csr_balance_dimension INTO l_statutory_bal_id_ppsn(l_index).defined_balance_id;
645       IF csr_balance_dimension%NOTFOUND THEN
646          l_statutory_bal_id_ppsn(l_index).defined_balance_id := 0;
647       END IF;
648     CLOSE csr_balance_dimension;
649     g_statutory_balance_table_ppsn(l_index).defined_balance_id := l_statutory_bal_id_ppsn(l_index).defined_balance_id;
650     g_statutory_balance_table_ppsn(l_index).balance_name := l_statutory_balance_ppsn(l_index).balance_name;
651     IF l_index <> 14 THEN                                                                            -- Bug 5015438
652        g_statutory_balance_table_ppsn(l_index).database_item_suffix := l_dimension_ppsn;
653   --     l_archive_index := l_archive_index + 1;
654     ELSE
655        g_statutory_balance_table_ppsn(l_index).database_item_suffix := l_dimension_1_ppsn;
656 
657     END IF;
658        l_archive_index_ppsn := l_archive_index_ppsn + 1;
659  if(l_index<15) then
660   hr_utility.set_location('g_statutory_ppsn(l_max)'||g_statutory_balance_table_ppsn(l_max_stat_balance + l_index).defined_balance_id,40);
661   hr_utility.set_location('g_statutory_ppsn(l_max)'||g_statutory_balance_table_ppsn(l_max_stat_balance + l_index).balance_name,40);
662   end if;
663   hr_utility.set_location('g_statutory_ppsn(l_ind)'||g_statutory_balance_table_ppsn(l_index).defined_balance_id,40);
664   hr_utility.set_location('g_statutory_ppsn(l_ind)'||g_statutory_balance_table_ppsn(l_index).balance_name,40);
665   END LOOP;
666 
667   ---
668     l_archive_index_ppsn := l_archive_index_ppsn - 1;
669   l_archive_index := l_archive_index - 1;
670  -- hr_utility.set_location('retrieving PER_PAYE_REF_PRSI_YTD bal_id for Insurable weeks',40);       -- Bug 5015438
671  -- OPEN csr_balance_dimension('IE PRSI Insurable Weeks',l_dimension_1);
672  -- FETCH csr_balance_dimension  INTO l_statutory_bal_id(l_archive_index).defined_balance_id;
673  -- CLOSE csr_balance_dimension;
674  -- g_statutory_balance_table(l_archive_index).defined_balance_id := l_statutory_bal_id(l_archive_index).defined_balance_id;
675  -- g_statutory_balance_table(l_archive_index).balance_name := 'IE PRSI_ClassA Insurable Weeks';
676  -- g_statutory_balance_table(l_archive_index).database_item_suffix := l_dimension_1;
677  -- hr_utility.set_location('Step = ' || l_proc,40);
678  -- hr_utility.set_location('l_max_stat_balance       = ' || l_max_stat_balance,40);
679   g_max_statutory_balance_index := l_archive_index;
680  -- hr_utility.set_location('Step ' || l_proc,50);
681  -- hr_utility.set_location('l_archive_index = ' || l_archive_index,50);
682  -- hr_utility.set_location('Leaving ' || l_proc,60);
683 END setup_standard_balance_table;
684 ---------------------------------------
685 
686 PROCEDURE archinit (p_payroll_action_id IN NUMBER)
687 IS
688 
689  CURSOR  csr_archive_effective_date(pactid NUMBER) IS
690   SELECT effective_date
691   FROM   pay_payroll_actions
692   WHERE  payroll_action_id = pactid;
693 
694   CURSOR csr_input_value_id(p_element_name CHAR,
695                             p_value_name   CHAR) IS
696   SELECT pet.element_type_id,
697          piv.input_value_id
698   FROM   pay_input_values_f piv,
699          pay_element_types_f pet
700   WHERE  piv.element_type_id = pet.element_type_id
701   AND    pet.legislation_code = 'IE'
702   AND    pet.element_name = p_element_name
703   AND    piv.name = p_value_name;
704 
705   l_proc                            VARCHAR2(50) := g_package || 'archinit';
706   l_assignment_set_id               NUMBER;
707   l_bg_id                           NUMBER;
708   l_canonical_end_date              DATE;
709   l_canonical_start_date            DATE;
710   l_consolidation_set               NUMBER;
711   l_end_date                        VARCHAR2(30);
712   l_payroll_id                      NUMBER;
713   l_start_date                      VARCHAR2(30);
714   l_dummy                           VARCHAR2(2);
715   l_error                           varchar2(1) ;
716 BEGIN
717 
718 
719 hr_utility.set_location('Entering ' || l_proc,10);
720 
721   g_archive_pact := p_payroll_action_id;
722 
723   OPEN csr_archive_effective_date(p_payroll_action_id);
724   FETCH csr_archive_effective_date
725   INTO  g_archive_effective_date;
726   CLOSE csr_archive_effective_date;
727 
728   pay_ie_p45_archive.get_parameters (
729     p_payroll_action_id => p_payroll_action_id
730   , p_token_name        => 'EMPLOYER'
731   , p_token_value       => g_paye_ref);
732 
733   pay_ie_p45_archive.get_parameters (
734     p_payroll_action_id => p_payroll_action_id
735   , p_token_name        => 'END_DATE'
736   , p_token_value       => l_end_date);
737 
738    pay_ie_p45_archive.get_parameters (
739     p_payroll_action_id => p_payroll_action_id
740   , p_token_name        => 'START_DATE'
741   , p_token_value       => l_start_date);
742 
743   pay_ie_p45_archive.get_parameters (
744     p_payroll_action_id => p_payroll_action_id
745   , p_token_name        => 'BG_ID'
746   , p_token_value       => l_bg_id);
747 
748   hr_utility.set_location('Step ' || l_proc,20);
749   hr_utility.set_location('g_paye_ref = ' || g_paye_ref,20);
750   hr_utility.set_location('l_end_date   = ' || l_end_date,20);
751 
752   l_canonical_start_date := TO_DATE(l_start_date,'yyyy/mm/dd');
753   l_canonical_end_date   := TO_DATE(l_end_date,'yyyy/mm/dd');
754 
755   -- Initialized g_archive_end_date to support Retry Option
756   g_archive_end_date     := TO_DATE(l_end_date,'yyyy/mm/dd');
757   g_archive_start_date   := l_canonical_start_date;
758 
759   hr_utility.set_location('l_canonical_start_date = ' || l_canonical_start_date,20);
760 
761 
762   -- retrieve ids for tax elements
763    hr_utility.set_location('stage 1',22);
764 
765   OPEN csr_input_value_id('IE PAYE details','Tax Basis');
766   FETCH csr_input_value_id INTO g_paye_details_element_id,
767                                 g_tax_basis_id;
768   CLOSE csr_input_value_id;
769 
770   OPEN csr_input_value_id('IE PRSI Detail','Contribution Class');
771   FETCH csr_input_value_id INTO g_paye_details_element_id,
772                                 g_prsi_cat_id;
773   CLOSE csr_input_value_id;
774 
775     OPEN csr_input_value_id('IE PRSI Detail','Subclass');
776     FETCH csr_input_value_id INTO g_paye_details_element_id,
777                                   g_prsi_subcat_id;
778     CLOSE csr_input_value_id;
779 
780     OPEN csr_input_value_id('IE PRSI Detail','Insurable Weeks');
781     FETCH csr_input_value_id INTO g_paye_details_element_id,
782                                   g_ins_weeks_id;
783     CLOSE csr_input_value_id;
784 
785 -- Fetch the Input value ID of Monthly Tax Credit 5128377
786   OPEN csr_input_value_id('IE PAYE details','Monthly Tax Credit');
787   FETCH csr_input_value_id INTO g_paye_details_element_id,
788                                 g_month_tax_rate;
789   CLOSE csr_input_value_id;
790 
791 -- Fetch the Input value ID of Weekly Tax Credit 5128377
792   OPEN csr_input_value_id('IE PAYE details','Weekly Tax Credit');
793   FETCH csr_input_value_id INTO g_paye_details_element_id,
794                                 g_week_tax_rate;
795   CLOSE csr_input_value_id;
796 
797 -- Changed to Period Weekly Tax Credit for Bug 5528450.
798   OPEN csr_input_value_id('IE PAYE details','Period Weekly Tax Credit');
799   FETCH csr_input_value_id INTO g_paye_details_element_id,
800                                 g_period_week_tax_rate;
801   CLOSE csr_input_value_id;
802 
803 -- Fetch the Input value ID of Monthly Standard Rate Cutoff 5128377
804   OPEN csr_input_value_id('IE PAYE details','Monthly Standard Rate Cutoff');
805   FETCH csr_input_value_id INTO g_paye_details_element_id,
806                                 g_month_std_cutoff;
807   CLOSE csr_input_value_id;
808 
809 -- Fetch the Input value ID of Weekly Standard Rate Cutoff 5128377
810   OPEN csr_input_value_id('IE PAYE details','Weekly Standard Rate Cutoff');
811   FETCH csr_input_value_id INTO g_paye_details_element_id,
812                                 g_week_std_cutoff;
813   CLOSE csr_input_value_id;
814 
815 -- Changed to Period Weekly Standard Rate Cutoff for Bug 5528450.
816   OPEN csr_input_value_id('IE PAYE details','Period Weekly Standard Rate Cutoff');
817   FETCH csr_input_value_id INTO g_paye_details_element_id,
818                                 g_period_week_std_cutoff;
819   CLOSE csr_input_value_id;
820 
821        hr_utility.set_location('stage 2',23);
822     pay_ie_p45_archive.setup_standard_balance_table;
823 
824        hr_utility.set_location('stage 3',24);
825 
826   --  hr_utility.set_location('l_payroll_id           = ' || l_payroll_id,20);
827   --  hr_utility.set_location('l_consolidation_set    = ' || l_consolidation_set,20);
828   --  hr_utility.set_location('l_canonical_start_date = ' || l_canonical_start_date,20);
829   --  hr_utility.set_location('l_canonical_end_date   = ' || l_canonical_end_date,20);
830     hr_utility.set_location('Leaving ' || l_proc,20);
831   END archinit;
832   --------------------------------------------------------------------
833 
834   PROCEDURE archive_employee_details (
835     p_assactid             IN NUMBER
836   , p_assignment_id        IN NUMBER
837   , p_curr_pymt_ass_act_id IN NUMBER
838   , p_payroll_child_actid  IN NUMBER
839   , p_date_earned          IN DATE
840   , p_curr_pymt_eff_date   IN DATE
841   , p_time_period_id       IN NUMBER
842   , p_record_count         IN NUMBER
843   , p_supp_flag            IN VARCHAR2
844   , p_person_id            IN NUMBER
845   , p_termination_date     IN DATE
846   , p_last_act_seq         IN NUMBER
847   , p_last_p45_act         IN NUMBER
848   -- added effective_date for bug 5591812
849   , p_effective_date	   IN DATE
850   ,p_ppsn_override_flag IN VARCHAR2) IS
851 
852     l_action_info_id NUMBER;
853     l_ovn            NUMBER;
854     --
855     l_tax_basis      VARCHAR2(20);
856     l_tax_basis_det  VARCHAR2(20);
857     l_arch_run_count NUMBER;
858     l_prsi_cat       VARCHAR2(50) :='';
859     l_prsi_cur_cat   VARCHAR2(1);
860     l_prsi_subcat    VARCHAR2(10);
861     l_ins_weeks      VARCHAR2(10);
862     l_monthly_tax_credit      NUMBER;
863     l_weekly_tax_credit       NUMBER;
864     l_period_weekly_tax_credit  NUMBER;   /* 5528450 */
865     l_monthly_std_rate_cutoff NUMBER;
866     l_weekly_std_rate_cutoff  NUMBER;
867     l_period_weekly_std_cutoff  NUMBER;   /* 5528450 */
868     l_tax_credit              NUMBER;
869     l_std_rate_cut_off        NUMBER;
870     l_period_type             VARCHAR2(20);
871     l_date_of_birth           DATE;
872     l_first_name              per_all_people_f.first_name%TYPE;
873     l_last_name               per_all_people_f.last_name%TYPE;
874     l_bg_id                   NUMBER;
875     l_commencement_date        VARCHAR2(30);
876     -- 13359530
877 		l_usc_yrly_cutoff_1         NUMBER;
878 		l_usc_mthly_cutoff_1        NUMBER;
879 		l_usc_wkly_cutoff_1 			  NUMBER;
880 		l_usc_yrly_cutoff_2         NUMBER;
881 		l_usc_mthly_cutoff_2        NUMBER;
882 		l_usc_wkly_cutoff_2         NUMBER;
883 		l_usc_cutoff_1              NUMBER;
884 		l_usc_cutoff_2              NUMBER;
885 
886 -- Commented as PAYE Details are now fetched from Run Results (5128377)
887 /*
888     cursor cur_paye_dtl is
889        select nvl(monthly_tax_credit,0)
890             ,nvl(weekly_tax_credit,0)
891             ,nvl(monthly_std_rate_cut_off,0)
892             ,nvl(weekly_std_rate_cut_off,0)
893       from   pay_ie_paye_details_f pipd
894        where assignment_id = p_assignment_id
895          and p_date_earned between
896             effective_start_date and effective_end_date
897          and info_source in ('IE_P45','IE_ELECTRONIC','IE_CERT_TAX_CREDITS')
898          and tax_basis not in ('IE_EMERGENCY','IE_EMERGENCY_NO_PPS'); --Bug No. 4016508
899 */
900 
901    /*Bug No. 4016508*/
902    /* cursor cur_credit_cutoff_emer(p_global_name varchar2) is
903        select  fgf.global_value
904         from   ff_globals_f fgf
905        where   fgf.global_name = p_global_name
906          and   fgf.legislation_code ='IE'
907          and   p_date_earned between
908                fgf.effective_start_date and fgf.effective_end_date;*/
909 
910     cursor cur_period_type is
911        select  ptp.period_type
912         from   per_time_periods ptp
913        where   time_period_id = p_time_period_id;
914 
915     cursor cur_sep_name_dob(p_bg_id NUMBER) is
916        select  papf.date_of_birth,papf.first_name, papf.last_name
917          from  per_all_people_f papf,
918                per_all_assignments_f pasf
919          where pasf.assignment_id = p_assignment_id
920          and   p_date_earned between
921                pasf.effective_start_date and pasf.effective_end_date
922          and   pasf.business_group_id = p_bg_id
923          and   papf.person_id = pasf.person_id
924          and   p_date_earned between
925                papf.effective_start_date and papf.effective_end_date
926          and   papf.business_group_id = pasf.business_group_id;
927 
928     CURSOR cur_payroll_assg_action is
929        select  paa.assignment_action_id  pay_assg_act_id
930          from  pay_assignment_actions paa,
931                pay_payroll_actions ppa
932          where paa.assignment_id in (select assignment_id
933                                     from per_all_assignments_f
934                                     where person_id = p_person_id
935                                    )
936         and   paa.tax_unit_id = g_paye_ref
937          and   paa.payroll_action_id = ppa.payroll_action_id
938          and   ppa.action_type in ('R','Q')
939          and   paa.action_sequence > p_last_act_seq
940          and   to_char(ppa.effective_date,'YYYY') = to_char(p_date_earned, 'YYYY')
941          and   paa.action_status = 'C'
942          and   paa.source_action_id is not null
943 	   --Bug 4724788
944 	   order by paa.assignment_action_id;
945 -- Modified this cursor, parameterised dimension_name for bug 5591812
946 	cursor balance_id (bal_name varchar2, p_dimension_name varchar2) is
947 	SELECT pdb.defined_balance_id
948 	    FROM
949 		     pay_balance_dimensions pbd
950 		    ,pay_balance_types      pbt
951 		    ,pay_defined_balances pdb
952 	    WHERE
953 			pbd.dimension_name = p_dimension_name
954 		    AND pbd.business_group_id is null
955 		    AND pbd.legislation_code='IE'
956 		    AND pbt.balance_name = bal_name
957 		    AND pbt.business_group_id is null
958 		    AND pbt.legislation_code='IE'
959 		    AND pdb.balance_type_id = pbt.balance_type_id
960 		    AND pdb.balance_dimension_id= pbd.balance_dimension_id
961 		    AND pdb.business_group_id is null
962 		    AND pdb.legislation_code='IE';
963 
964     CURSOR payroll_asg_action is
965        select  max(paa.assignment_action_id)
966          from  pay_assignment_actions paa,
967                pay_payroll_actions ppa
968          where paa.assignment_id in (select assignment_id
969                                     from per_all_assignments_f
970                                     where person_id = p_person_id
971                                    )
972          and   paa.tax_unit_id = g_paye_ref
973          and   paa.payroll_action_id = ppa.payroll_action_id
974          and   ppa.action_type in ('R','Q')
975          and   to_char(ppa.effective_date,'YYYY') = to_char(p_date_earned, 'YYYY')
976          and   paa.action_status = 'C'
977          and   paa.source_action_id is not null;
978 
979 -- Fetch Commencement date  when no previous p45 produced.
980    CURSOR comm_date_first IS
981    select act_inf.action_information11
982    from   pay_action_information act_inf
983    where  act_inf.action_context_id = p_assactid
984    and    act_inf.action_information_category = 'EMPLOYEE DETAILS'
985    and    act_inf.action_context_type = 'AAP';
986 
987 -- Fetch Commencement date when p45 is produced previously.
988    CURSOR comm_date_last_p45 IS
989    select act_inf.action_information30
990    from   pay_action_information act_inf
991    where  act_inf.action_context_id = p_last_p45_act
992    and    act_inf.action_information_category = 'IE EMPLOYEE DETAILS'
993    and    act_inf.action_context_type = 'AAP';
994 
995 -- Bug 5386432
996    -- CURSOR to fetch tax credit and std cutoff from paye table for
997    -- employees having 0 earnings.
998    CURSOR csr_get_paye_details is
999    select tax_basis
1000          ,weekly_tax_credit
1001 	   ,weekly_std_rate_cut_off
1002          ,monthly_tax_credit
1003          ,monthly_std_rate_cut_off
1004     from pay_ie_paye_details_f
1005     where assignment_id = p_assignment_id
1006       and p_termination_date between effective_start_date and effective_end_date;
1007 -- bug 13359530 13513882
1008 CURSOR csr_get_usc_details is
1009    select usc_yrly_cutoff_1
1010          ,usc_mthly_cutoff_1
1011          ,usc_wkly_cutoff_1
1012          ,usc_yrly_cutoff_2
1013          ,usc_mthly_cutoff_2
1014          ,usc_wkly_cutoff_2
1015 	   ,usc_tax_basis
1016     from pay_ie_paye_details_f
1017     where assignment_id = p_assignment_id
1018       and p_termination_date between effective_start_date and effective_end_date;
1019 
1020 
1021 -- Added by vikas cursor to number_per_fiscal_year.
1022 -- Since tax credit and cutoff figures are fetched from run-results, for
1023 -- bi-weekly payroll cutoff and credit and are shown as twice of weekly figures.
1024 -- bug 5510536
1025 cursor csr_number_per_year (l_period_type     per_time_periods.period_type%type) is
1026   select number_per_fiscal_year
1027   from   per_time_period_types tpt
1028   where  period_type = l_period_type;
1029   --
1030   l_number_per_year  per_time_period_types.number_per_fiscal_year%type;
1031 
1032 --end vikas
1033 
1034 -- To display futher PRSI Classes only, if its insurable weeks are non-zero.
1035 -- bug 5591812
1036 l_prev_sequence number;
1037 l_current_sequence number;
1038 CURSOR c_context_id
1039       IS
1040          SELECT context_id
1041            FROM ff_contexts
1042           WHERE context_name = 'SOURCE_TEXT';
1043 l_context_id               ff_contexts.context_id%TYPE;
1044 l_defined_balance_id       pay_defined_balances.defined_balance_id%TYPE;
1045 v_class			varchar2(30);
1046 
1047 
1048 CURSOR Cur_Act_Contexts(l_defined_bal_id number,p_context_value varchar2) IS
1049    SELECT sum(PAY_BALANCE_PKG.GET_VALUE(l_defined_bal_id, -- changes made
1050     			             pac.ASSIGNMENT_ACTION_ID,
1051                                    g_paye_ref,
1052                                    null,
1053                                    pac.CONTEXT_ID,
1054                                    pac.CONTEXT_VALUE,
1055                                    null,
1056                                    null))
1057   FROM   pay_action_contexts pac,
1058          pay_assignment_actions pas,
1059          pay_payroll_actions ppa
1060   WHERE  substr(pac.Context_Value,1,4) = p_context_value
1061   AND    pac.assignment_id in (select papf.assignment_id
1062                                  from per_all_assignments_f papf
1063                                  where papf.person_id = p_person_id
1064                               )
1065   AND    pas.tax_unit_id = g_paye_ref
1066   AND    pas.assignment_action_id = pac.assignment_action_id
1067   AND    ppa.payroll_action_id = pas.payroll_action_id
1068   AND    ppa.effective_date between to_date('01-01-' || to_char(p_effective_date,'YYYY'),'DD-MM-YYYY') --Bug fix 4108423
1069   AND    g_archive_end_date
1070   and    pas.action_sequence > l_prev_sequence
1071   and    pas.action_sequence <= l_current_sequence;
1072 
1073   CURSOR cur_get_prev_run_seq is
1074    select paa.action_sequence
1075    from   pay_assignment_actions paa,
1076           pay_payroll_actions ppa,
1077           pay_action_interlocks pai,
1078 	    pay_assignment_actions paa1
1079    where  paa1.source_action_id = p_last_p45_act
1080      and  pai.locking_action_id = paa1.assignment_action_id
1081     and   pai.locked_action_id = paa.assignment_action_id
1082     and   paa.assignment_id in (select papf.assignment_id
1083                                  from per_all_assignments_f papf
1084                                  where papf.person_id = p_person_id
1085                               )
1086     and   paa.tax_unit_id = g_paye_ref
1087     and   paa.payroll_action_id = ppa.payroll_action_id
1088     and   ppa.action_type in ('R','Q','I','B','V');
1089 
1090    CURSOR cur_get_curr_run_seq is
1091    select action_sequence
1092    from   pay_assignment_actions ppa
1093    where  assignment_action_id = p_payroll_child_actid;
1094 
1095    l_class_weeks number;
1096 -- end for bug 5591812
1097 
1098 
1099 
1100 
1101 -- Variables to store K, M and Total Insurable Weeks
1102     k_defined_balance_id pay_defined_balances.DEFINED_BALANCE_ID%type := NULL;
1103     m_defined_balance_id pay_defined_balances.DEFINED_BALANCE_ID%type := NULL;
1104     pay_act_id number := NULL;
1105 
1106     l_proc           VARCHAR2(60) := g_package || 'archive_employee_details';
1107     l_prsi_count     NUMBER(1) := 0; --bug 4724788
1108     l_usc_tax_basis      VARCHAR2(20);
1109   --
1110    BEGIN
1111        hr_utility.set_location('Entering ' || l_proc,10);
1112     -- call generic procedure to retrieve and archive all data for
1113     -- EMPLOYEE DETAILS, ADDRESS DETAILS and EMPLOYEE NET PAY DISTRIBUTION
1114     hr_utility.set_location('Calling pay_emp_action_arch',20);
1115 
1116     pay_emp_action_arch.get_personal_information (
1117         p_payroll_action_id    => g_archive_pact            -- archive payroll_action_id
1118       , p_assactid             => p_assactid                -- archive assignment_action_id
1119       , p_assignment_id        => p_assignment_id           -- current assignment_id
1120       , p_curr_pymt_ass_act_id => p_curr_pymt_ass_act_id    -- prepayment assignment_action_id
1121       , p_curr_eff_date        => p_curr_pymt_eff_date      -- prepayment effective_date(specially reqd
1122                                                             -- for archives later than the
1123                                                             -- last process date after termination date)
1124       , p_date_earned          => p_date_earned             -- payroll date_earned
1125       , p_curr_pymt_eff_date   => p_curr_pymt_eff_date      -- prepayment effective_date
1126       , p_tax_unit_id          => g_paye_ref                -- only required for US
1127       , p_time_period_id       => p_time_period_id          -- payroll time_period_id
1128       , p_ppp_source_action_id => NULL);
1129 
1130     hr_utility.set_location('Returned from pay_emp_action_arch',30);
1131 
1132      hr_utility.set_location('p_payroll_child_actid'||p_payroll_child_actid,30);
1133      hr_utility.set_location('p_ppsn_override_flag'||p_ppsn_override_flag,30);
1134     -- get the business group id
1135     pay_ie_p45_archive.get_parameters (
1136                           p_payroll_action_id => g_archive_pact
1137                         , p_token_name        => 'BG_ID'
1138                         , p_token_value       => l_bg_id);
1139 
1140     hr_utility.set_location('p_run_assignment_action_id ='||p_payroll_child_actid,40);
1141 
1142     -- get tax basis
1143     hr_utility.set_location('g_tax_basis_id ='||g_tax_basis_id,40);
1144 -- Bug 5386432, since for terminated which does not have any element attached with FPD
1145 -- will have no child assignment actions. Call this only if child actions exists.
1146     IF p_payroll_child_actid IS NOT NULL THEN
1147 	l_tax_basis := pay_ie_archive_detail_pkg.get_tax_details (
1148                                 p_run_assignment_action_id => p_payroll_child_actid
1149                                ,p_input_value_id           => g_tax_basis_id
1150                                ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1151     END IF;
1152     hr_utility.set_location('l_tax_basis = ' || l_tax_basis,40);
1153 
1154     -- get prsi classes
1155     hr_utility.set_location('g_prsi_cat_id ='||g_prsi_cat_id,40);
1156     -- check for supplementary run
1157     /*
1158     OPEN cur_supp_run;
1159     FETCH cur_supp_run INTO l_arch_run_count;
1160     CLOSE cur_supp_run;
1161     hr_utility.set_location('l_arch_run_count ='||l_arch_run_count,40);
1162     -- if it is a supplementary run, archive only for the current run
1163     IF l_arch_run_count>1
1164     */
1165     /*
1166     IF p_supp_flag = 'Y'
1167     THEN
1168         l_prsi_cat := pay_ie_archive_detail_pkg.get_tax_details (
1169                                 p_run_assignment_action_id => p_payroll_child_actid
1170                                ,p_input_value_id           => g_prsi_cat_id
1171                                ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1172     -- otherise archive for all payroll runs
1173     ELSE
1174     */
1175     -- Added to check to dispaly further PRSI classes only if insurable
1176     -- weeks are not zero.
1177 /*      OPEN c_context_id;
1178       FETCH c_context_id INTO l_context_id;
1179       CLOSE c_context_id;
1180 
1181 	if p_supp_flag <> 'Y' then*/
1182 	-- bug 5591812
1183 		open balance_id('IE PRSI Insurable Weeks','_ASG_PAYE_REF_PRSI_RUN');
1184 		FETCH balance_id into l_defined_balance_id ;
1185 		CLOSE balance_id;
1186 
1187 	OPEN cur_get_prev_run_seq;
1188 		FETCH cur_get_prev_run_seq into l_prev_sequence;
1189 		CLOSE cur_get_prev_run_seq;
1190 
1191 		IF l_prev_sequence IS NULL THEN
1192 			l_prev_sequence := 0;
1193 		END IF;
1194 
1195 		open cur_get_curr_run_seq;
1196 		fetch cur_get_curr_run_seq into l_current_sequence;
1197 		CLOSE cur_get_curr_run_seq;
1198 
1199 		if l_current_sequence is null then
1200 			l_current_sequence := 0;
1201 		end if;
1202 		hr_utility.set_location('l_current_sequence..'||l_current_sequence,101);
1203 		hr_utility.set_location('l_prev_sequence..'||l_prev_sequence,101);
1204 
1205 
1206         FOR assg_act_rec IN cur_payroll_assg_action
1207         LOOP
1208             l_prsi_cur_cat := pay_ie_archive_detail_pkg.get_tax_details (
1209                                 p_run_assignment_action_id => assg_act_rec.pay_assg_act_id
1210                                ,p_input_value_id           => g_prsi_cat_id
1211                                ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1212             hr_utility.set_location('l_prsi_cur_cat = ' || l_prsi_cur_cat,40);
1213 
1214             IF l_prsi_cur_cat IS NOT NULL AND l_prsi_cur_cat <>'A' AND (nvl(instr(l_prsi_cat,l_prsi_cur_cat),0) = 0)
1215             THEN
1216 
1217                IF l_prsi_cat <> ' ' THEN
1218 -- Bug 3669639 Added a space after the comma so that the display is now uniform with spaces
1219                     hr_utility.set_location('In if l_prsi_cur_cat = ' || l_prsi_cur_cat,41);
1220 			      open Cur_Act_Contexts(l_defined_balance_id,'IE_'||l_prsi_cur_cat);
1221 				fetch Cur_Act_Contexts into l_class_weeks;
1222 				CLOSE Cur_Act_Contexts;
1223                         hr_utility.set_location('in if l_class_weeks ..'||l_class_weeks,101);
1224 				if nvl(l_class_weeks,0) > 0 then
1225 					l_prsi_cat := l_prsi_cat||', '|| l_prsi_cur_cat;
1226 					l_prsi_count := l_prsi_count + 1; --Bug 4724788
1227 				end if;
1228                         hr_utility.set_location('l_prsi_count = ' || l_prsi_count,420);
1229 			  --Bug 4724788
1230 			  if l_prsi_count = 2 then
1231 				exit;
1232 			  end if;
1233 		    ELSE
1234 		            v_class := 'IE_'||l_prsi_cur_cat;
1235 				hr_utility.set_location('before in else l_class_weeks ..'||l_class_weeks,101);
1236 				hr_utility.set_location('before in else l_prsi_cur_cat ..'||l_prsi_cur_cat,101);
1237 				hr_utility.set_location('v_class ..'||v_class,101);
1238 				open Cur_Act_Contexts(l_defined_balance_id,v_class);
1239 				fetch Cur_Act_Contexts into l_class_weeks;
1240 				CLOSE Cur_Act_Contexts;
1241 				hr_utility.set_location('in else l_class_weeks ..'||l_class_weeks,101);
1242 
1243 				if nvl(l_class_weeks,0) > 0 then
1244 
1245 					l_prsi_cat :=l_prsi_cur_cat;
1246 					hr_utility.set_location('In if else v_class = ' || v_class,42);
1247 				end if;
1248 			  --exit;
1249                 END IF;
1250             END IF;
1251 	    hr_utility.set_location(' kal after if and else l_prsi_cat = ' || l_prsi_cat,40);
1252 
1253         END LOOP;
1254 	 hr_utility.set_location(' kal after loop l_prsi_cat = ' || l_prsi_cat,40);
1255 	  -- end bug 5591812
1256    -- END IF;
1257     hr_utility.set_location('g_prsi_subcat_id ='||g_prsi_subcat_id,40);
1258 -- Bug 5386432, since for terminated which does not have any element attached with FPD
1259 -- will have no child assignment actions. Call this only if child actions exists.
1260     IF p_payroll_child_actid IS NOT NULL THEN
1261 	    l_prsi_subcat := pay_ie_archive_detail_pkg.get_tax_details (
1262 						  p_run_assignment_action_id => p_payroll_child_actid
1263 						 ,p_input_value_id           => g_prsi_subcat_id
1264 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1265 	    l_ins_weeks := pay_ie_archive_detail_pkg.get_tax_details (
1266 						  p_run_assignment_action_id => p_payroll_child_actid
1267 						 ,p_input_value_id           => g_ins_weeks_id
1268 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1269      END IF;
1270      hr_utility.set_location('l_prsi_subcat = ' || l_prsi_subcat,40);
1271      hr_utility.set_location('g_ins_weeks_id ='||g_ins_weeks_id,40);
1272      hr_utility.set_location('l_ins_weeks = ' || l_ins_weeks,40);
1273 
1274    -- get tax credit and std rate cut off
1275    OPEN cur_period_type;
1276    FETCH cur_period_type INTO l_period_type;
1277    CLOSE cur_period_type;
1278 
1279 -- Commented as PAYE Details are now fetched from Run Results (5128377)
1280 /*
1281    OPEN cur_paye_dtl;
1282    FETCH cur_paye_dtl INTO l_monthly_tax_credit ,
1283                            l_weekly_tax_credit,
1284                            l_monthly_std_rate_cutoff,
1285                            l_weekly_std_rate_cutoff;
1286    IF cur_paye_dtl%NOTFOUND
1287    THEN */
1288    /*Bug No. 4016508*/
1289        /*OPEN cur_credit_cutoff_emer('IE_WEEKLY_TAX_CREDIT');
1290        FETCH cur_credit_cutoff_emer INTO l_weekly_tax_credit;
1291        CLOSE cur_credit_cutoff_emer;
1292        OPEN cur_credit_cutoff_emer('IE_MONTHLY_TAX_CREDIT');
1293        FETCH cur_credit_cutoff_emer INTO l_monthly_tax_credit;
1294        CLOSE cur_credit_cutoff_emer;*/
1295 /*       l_weekly_tax_credit:=0;
1296        l_monthly_tax_credit:=0;
1297        l_monthly_std_rate_cutoff:=0;
1298        l_weekly_std_rate_cutoff:=0;
1299    END IF;
1300    CLOSE cur_paye_dtl;
1301 */
1302 
1303 -- Tax Credit and Cutoff are now fetched from Run Results 5128377
1304 -- Bug 5386432, since for terminated which does not have any element attached with FPD
1305 -- will have no child assignment actions. Call this only if child actions exists.
1306    IF p_payroll_child_actid IS NOT NULL THEN
1307 	    l_weekly_tax_credit := pay_ie_archive_detail_pkg.get_tax_details (
1308 						  p_run_assignment_action_id => p_payroll_child_actid
1309 						 ,p_input_value_id           => g_week_tax_rate
1310 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1311 
1312             /* 5528450 */
1313 	    l_period_weekly_tax_credit := pay_ie_archive_detail_pkg.get_tax_details (
1314 						  p_run_assignment_action_id => p_payroll_child_actid
1315 						 ,p_input_value_id           => g_period_week_tax_rate
1316 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1317 
1318 	    IF l_period_weekly_tax_credit IS NOT NULL THEN
1319                l_weekly_tax_credit := l_period_weekly_tax_credit;
1320 	    END IF;
1321 
1322 	    l_monthly_tax_credit := pay_ie_archive_detail_pkg.get_tax_details (
1323 						  p_run_assignment_action_id => p_payroll_child_actid
1324 						 ,p_input_value_id           => g_month_tax_rate
1325 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1326 
1327 	    l_monthly_std_rate_cutoff := pay_ie_archive_detail_pkg.get_tax_details (
1328 						  p_run_assignment_action_id => p_payroll_child_actid
1329 						 ,p_input_value_id           => g_month_std_cutoff
1330 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1331 
1332 	    l_weekly_std_rate_cutoff := pay_ie_archive_detail_pkg.get_tax_details (
1333 						  p_run_assignment_action_id => p_payroll_child_actid
1334 						 ,p_input_value_id           => g_week_std_cutoff
1335 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1336 
1337             /* 5528450 */
1338 	    l_period_weekly_std_cutoff := pay_ie_archive_detail_pkg.get_tax_details (
1339 						  p_run_assignment_action_id => p_payroll_child_actid
1340 						 ,p_input_value_id           => g_period_week_std_cutoff
1341 						 ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
1342 
1343              IF l_period_weekly_std_cutoff IS NOT NULL THEN
1344                l_weekly_std_rate_cutoff := l_period_weekly_std_cutoff;
1345 	    END IF;
1346    END IF;
1347 
1348  hr_utility.set_location('p_payroll_child_actid'|| p_payroll_child_actid,30);
1349  hr_utility.set_location('l_weekly_tax_credit'|| l_weekly_tax_credit,30);
1350  hr_utility.set_location('l_period_weekly_tax_credit'|| l_period_weekly_tax_credit,30);
1351  hr_utility.set_location('l_monthly_tax_credit'|| l_monthly_tax_credit,30);
1352  hr_utility.set_location('l_monthly_std_rate_cutoff'|| l_monthly_std_rate_cutoff,30);
1353  hr_utility.set_location('l_weekly_std_rate_cutoff'|| l_weekly_std_rate_cutoff,30);
1354  hr_utility.set_location('l_period_weekly_std_cutoff'|| l_period_weekly_std_cutoff,30);
1355    -- Bug 5386432, if no child assignment actions fetch values from
1356    -- PAYE table.
1357    IF p_payroll_child_actid IS NULL THEN
1358 	open csr_get_paye_details;
1359 	FETCH csr_get_paye_details into l_tax_basis,l_weekly_tax_credit,l_weekly_std_rate_cutoff,l_monthly_tax_credit,l_monthly_std_rate_cutoff;
1360 	CLOSE csr_get_paye_details;
1361    END IF;
1362    --- 13359530
1363    open csr_get_usc_details;
1364 	FETCH csr_get_usc_details into l_usc_yrly_cutoff_1,l_usc_mthly_cutoff_1,l_usc_wkly_cutoff_1,l_usc_yrly_cutoff_2,l_usc_mthly_cutoff_2,
1365        l_usc_wkly_cutoff_2,l_usc_tax_basis;
1366 	CLOSE csr_get_usc_details;
1367    -- Bug 5510536
1368    IF (l_period_type IN ('Bi-Week','Week','Lunar Month'))
1369    THEN
1370 	 open csr_number_per_year(l_period_type);
1371 	 FETCH csr_number_per_year into l_number_per_year;
1372 	 CLOSE csr_number_per_year;
1373        l_tax_credit :=l_weekly_tax_credit*l_number_per_year/52;
1374        l_std_rate_cut_off := l_weekly_std_rate_cutoff*l_number_per_year/52;
1375 	 --16075672
1376 	 --l_usc_cutoff_1 :=    l_usc_wkly_cutoff_1*l_number_per_year/52;  --13359530
1377 	 --l_usc_cutoff_2 :=    l_usc_wkly_cutoff_2*l_number_per_year/52;
1378 	 l_usc_cutoff_1 :=    l_usc_wkly_cutoff_1;
1379 	 l_usc_cutoff_2 :=    l_usc_wkly_cutoff_2;
1380 	 --16075672
1381    ELSIF (l_period_type IN ('Bi-Month','Calendar Month','Quarter'))
1382    THEN
1383 	 open csr_number_per_year(l_period_type);
1384 	 FETCH csr_number_per_year into l_number_per_year;
1385 	 CLOSE csr_number_per_year;
1386        l_tax_credit :=l_monthly_tax_credit* l_number_per_year/12;
1387        l_std_rate_cut_off := l_monthly_std_rate_cutoff* l_number_per_year/12;
1388 	 --16075672
1389 	 --l_usc_cutoff_1 :=    l_usc_mthly_cutoff_1*l_number_per_year/12;
1390 	 --l_usc_cutoff_2 :=    l_usc_mthly_cutoff_2*l_number_per_year/12;
1391 	 l_usc_cutoff_1 :=    l_usc_mthly_cutoff_1;
1392 	 l_usc_cutoff_2 :=    l_usc_mthly_cutoff_2;
1393 	 --16075672
1394    END IF;
1395  -- bug 13513882
1396       IF l_usc_tax_basis = 'IE_EXEMPTION' THEN
1397 		l_usc_cutoff_1 := 0 ;
1398 		l_usc_cutoff_2 := 0 ;
1399 	END IF;
1400    -- end 5510536.
1401    hr_utility.set_location('l_tax_credit = ' || l_tax_credit,40);
1402    hr_utility.set_location('l_std_rate_cut_off = ' || l_std_rate_cut_off,40);
1403     IF l_tax_basis = 'C'
1404     THEN
1405       l_tax_basis_det := 'Cumulative';
1406     ELSIF l_tax_basis = 'N'
1407     THEN
1408       l_tax_basis_det := 'Non Cumulative';
1409     ELSE
1410       l_tax_basis_det := l_tax_basis;
1411     END IF;
1412 
1413      --get the date of birth and separate name
1414      hr_utility.set_location('V_assignment_id = ' || p_assignment_id,40);
1415      hr_utility.set_location('V_date_earned = ' || p_date_earned,40);
1416 
1417      OPEN cur_sep_name_dob(l_bg_id);
1418      FETCH cur_sep_name_dob INTO l_date_of_birth,l_first_name,l_last_name;
1419      CLOSE cur_sep_name_dob;
1420 
1421 -- Fetching K, M and Total Insurable Weeks which needs to be stored
1422 -- with class name in l_prsi_cat
1423 /* 7291676 */
1424 IF p_ppsn_override_flag is not null THEN
1425 open balance_id('IE PRSI K Term Insurable Weeks','_PER_PAYE_REF_PPSN_YTD');
1426 ELSE
1427 open balance_id('IE PRSI K Term Insurable Weeks','_PER_PAYE_REF_YTD');
1428 END IF;
1429 fetch balance_id into k_defined_balance_id;
1430 close balance_id;
1431 
1432 /* 7291676 */
1433 IF p_ppsn_override_flag is not null  THEN
1434 open balance_id('IE PRSI M Term Insurable Weeks','_PER_PAYE_REF_PPSN_YTD');
1435 ELSE
1436 open balance_id('IE PRSI M Term Insurable Weeks','_PER_PAYE_REF_YTD');
1437 END IF;
1438 fetch balance_id into m_defined_balance_id;
1439 close balance_id;
1440 
1441 open payroll_asg_action;
1442 fetch payroll_asg_action into pay_act_id;
1443 close payroll_asg_action;
1444 
1445 -- Bug 3669639 : Changed the code so that l_prsi_cat is now
1446 -- previous classes concatenated with K or M
1447 -- Bug 5386432, since for terminated which does not have any element attached with FPD
1448 -- will have no child assignment actions. Call this only if child actions exists.
1449 	 hr_utility.set_location(' kal before if l_prsi_cat = ' || l_prsi_cat,40);
1450 IF pay_act_id IS NOT NULL THEN
1451 	if  pay_balance_pkg.get_value(k_defined_balance_id,pay_act_id,g_paye_ref,null,null,null,null,null) > 0 and
1452 	    l_prsi_cat is not NULL and l_prsi_count <> 2 then --Bug 4724788
1453 		  l_prsi_cat := l_prsi_cat ||', K';
1454 	elsif pay_balance_pkg.get_value(k_defined_balance_id,pay_act_id,g_paye_ref,null,null,null,null,null) > 0 and
1455 		l_prsi_cat is null then
1456 		  l_prsi_cat := 'K';
1457 	end if;
1458 
1459 	if  pay_balance_pkg.get_value(m_defined_balance_id,pay_act_id,g_paye_ref,null,null,null,null,null) > 0 and
1460 	    l_prsi_cat is not NULL and l_prsi_count <> 2 then --Bug 4724788
1461 		  l_prsi_cat := l_prsi_cat ||', M';
1462 	elsif pay_balance_pkg.get_value(m_defined_balance_id,pay_act_id,g_paye_ref,null,null,null,null,null) > 0 and
1463 		l_prsi_cat is null then
1464 		  l_prsi_cat := 'M';
1465 	end if;
1466 END IF;
1467  hr_utility.set_location(' kal after  if l_prsi_cat = ' || l_prsi_cat,40);
1468 
1469 -- bug 5383808, Fetch value of latest hire date.
1470 --if p_last_p45_act IS NULL THEN
1471 	OPEN comm_date_first;
1472 	FETCH comm_date_first INTO l_commencement_date;
1473 	CLOSE comm_date_first;
1474 /*else
1475 	OPEN comm_date_last_p45;
1476 	FETCH comm_date_last_p45 INTO l_commencement_date;
1477 	CLOSE comm_date_last_p45;
1478 end if;*/
1479 
1480 
1481 
1482 -- end bug 5383808
1483     hr_utility.set_location('Archiving IE EMPLOYEE DETAILS',50);
1484     pay_action_information_api.create_action_information (
1485       p_action_information_id        =>  l_action_info_id
1486     , p_action_context_id            =>  p_assactid
1487     , p_action_context_type          =>  'AAP'
1488     , p_object_version_number        =>  l_ovn
1489     , p_assignment_id                =>  p_assignment_id
1490     , p_effective_date               =>  g_archive_effective_date
1491     , p_source_id                    =>  NULL
1492     , p_source_text                  =>  NULL
1493     , p_action_information_category  =>  'IE EMPLOYEE DETAILS'
1494     , p_action_information1          =>  NULL
1495     , p_action_information2          =>  NULL
1496     , p_action_information3          =>  NULL
1497      , p_action_information19         => l_usc_cutoff_2  -- bug 13359530
1498     , p_action_information20         =>  l_usc_cutoff_1
1499     , p_action_information21         =>  l_tax_basis_det
1500     , p_action_information22         =>  l_prsi_cat
1501     , p_action_information23         =>  l_prsi_subcat
1502     , p_action_information24         =>  l_ins_weeks
1503     , p_action_information25         =>  to_char(l_date_of_birth,'DD-MON-YYYY')
1504     , p_action_information26         =>  l_tax_credit
1505     , p_action_information27         =>  l_std_rate_cut_off
1506     , p_action_information28         =>  l_first_name
1507     , p_action_information29         =>  l_last_name
1508     , p_action_information30         =>  l_commencement_date);
1509   END archive_employee_details;
1510 
1511   -----------------------------
1512 
1513   PROCEDURE process_balance (p_action_context_id IN NUMBER,
1514                              p_assignment_id     IN NUMBER,
1515                              p_person_id         IN NUMBER,
1516                              p_source_id         IN NUMBER,
1517                              p_effective_date    IN DATE,
1518                              p_balance           IN VARCHAR2,
1519                              p_dimension         IN VARCHAR2,
1520                              p_defined_bal_id    IN NUMBER,
1521                              p_record_count      IN NUMBER,
1522 			           p_termination_date  IN DATE,
1523 			           p_supp_flag         IN VARCHAR2,
1524 			           p_last_p45_action   IN NUMBER,
1525 			           p_last_p45_pact     IN NUMBER,           -- Bug 5005788
1526 			           p_prev_src_id       IN NUMBER) -- p45 action locked by current P45 action.
1527   IS
1528   --
1529   -- Cursor for retrieving balance type id of defined balance
1530   --
1531   CURSOR csr_bal_type IS
1532    select balance_type_id
1533    from   pay_defined_balances
1534    where  defined_balance_id = p_defined_bal_id;
1535   -- Cursor for retrieving the summed run results for
1536   -- PRSI insurable weeks where Contribution Class
1537   -- starts with IE_A
1538   -- First part of the select retrives summed up values for Payroll runs,
1539   -- Second part of the select retrives summed up values for Uploaded Balance
1540  /* CURSOR csr_iea_weeks (p_balance_type_id in number) IS
1541   select  nvl(sum(fnd_number.canonical_to_number(TARGET.result_value) * FEED.scale),0) weeks
1542   from  pay_run_result_values   TARGET
1543       , pay_balance_feeds_f     FEED
1544       , pay_run_results         RR
1545       , pay_assignment_actions  ASSACT
1546       , pay_assignment_actions  BAL_ASSACT
1547       , pay_payroll_actions     PACT
1548       , pay_payroll_actions     BACT
1549       , per_time_periods        PPTP
1550       , per_time_periods        BPTP
1551       , pay_run_results         PROCESS_RR
1552       , pay_run_result_values   PROCESS
1553       , pay_input_values_f      PROCESS_IV
1554       , pay_action_contexts     ACX_PROCESS_ID
1555       , ff_contexts             CON_PROCESS_ID
1556   where BAL_ASSACT.assignment_action_id = p_source_id
1557   and   BAL_ASSACT.payroll_action_id = BACT.payroll_action_id
1558   and   FEED.balance_type_id +0 = p_balance_type_id
1559   and   FEED.input_value_id = TARGET.input_value_id
1560   and   nvl(TARGET.result_value,'0') <> '0'
1561   and   TARGET.run_result_id = RR.run_result_id
1562   and   RR.assignment_action_id = ASSACT.assignment_action_id
1563   and   ASSACT.payroll_action_id = PACT.payroll_action_id
1564   and   PACT.effective_date between FEED.effective_start_date and FEED.effective_end_date
1565   and   RR.status in ('P','PA')
1566   and   ASSACT.action_sequence <= BAL_ASSACT.action_sequence
1567   and   ASSACT.assignment_id = BAL_ASSACT.assignment_id
1568   and   BPTP.payroll_id = BACT.payroll_id
1569   and   BACT.date_earned between BPTP.start_date and BPTP.end_date
1570   and   PPTP.payroll_id = PACT.payroll_id
1571   and   PACT.date_earned between PPTP.start_date and PPTP.end_date
1572   and   ASSACT.assignment_action_id = ACX_PROCESS_ID.assignment_action_id
1573   and   ACX_PROCESS_ID.context_id = CON_PROCESS_ID.context_id
1574   and   CON_PROCESS_ID.context_name = 'SOURCE_TEXT'
1575   and   PROCESS.result_value = ACX_PROCESS_ID.context_value
1576   and   PROCESS.run_result_id = PROCESS_RR.run_result_id
1577   and   PROCESS_RR.assignment_action_id = ASSACT.assignment_action_id
1578   and   PROCESS_RR.status in ('P','PA')
1579   and   PROCESS.input_value_id = PROCESS_IV.input_value_id
1580   and   PROCESS_IV.name = 'Contribution_Class'
1581   and   PACT.effective_date between PROCESS_IV.effective_start_date and PROCESS_IV.effective_end_date
1582   and   PACT.effective_date > to_date(to_char(PACT.effective_date, 'YYYY')||'01/01','YYYY/MM/DD')
1583   and   ACX_PROCESS_ID.context_value like 'IE_A%'
1584   and   PPTP.regular_payment_date >= trunc(BPTP.regular_payment_date,'Y')
1585   and   RR.entry_type <>'B'  -- Bug 3079945 start
1586   union all
1587   select nvl(sum(fnd_number.canonical_to_number(TARGET.result_value) * FEED.scale),0) weeks
1588   from  pay_run_result_values   TARGET
1589       , pay_run_results         RR
1590       , pay_assignment_actions  ASSACT
1591       , pay_balance_feeds_f     FEED
1592   where ASSACT.assignment_action_id in (select min(assignment_action_id) from
1593         pay_assignment_actions where assignment_id = p_assignment_id)
1594   and   FEED.balance_type_id +0 = p_balance_type_id
1595   and   FEED.input_value_id = TARGET.input_value_id
1596   and   nvl(TARGET.result_value,'0') <> '0'
1597   and   TARGET.run_result_id = RR.run_result_id
1598   and   RR.assignment_action_id = ASSACT.assignment_action_id
1599   and   RR.status in ('P','PA')
1600   and   RR.entry_type = 'B';*/
1601   --v_csr_iea_weeks csr_iea_weeks%ROWTYPE;
1602   -- Bug 3079945 End
1603   -- Cursor for retrieving the context and source id for
1604   -- payroll runs having prsi contribution class as A
1605   -- in the same tax year for which the archive is run
1606   /*CURSOR Cur_Act_Contexts IS
1607   SELECT pac.Context_ID,pac.Context_Value,pac.Assignment_action_id
1608   FROM   pay_action_contexts pac,pay_assignment_actions pas,
1609          pay_payroll_actions ppa,pay_payroll_actions appa
1610   WHERE  pac.Context_Value = 'IE_A'
1611   AND    pac.assignment_id = p_assignment_id
1612   AND    pas.assignment_action_id = pac.assignment_action_id
1613   AND    ppa.payroll_action_id = pas.payroll_action_id
1614   And    appa.payroll_action_id = g_archive_pact
1615   AND    to_char(appa.date_earned,'YYYY') = to_char(ppa.date_earned,'YYYY')
1616   AND    pac.assignment_action_id = (SELECT MAX(assignment_action_id)
1617                                       FROM pay_action_contexts
1618                                       WHERE Context_Value = 'IE_A'
1619                                       AND assignment_id = p_assignment_id);
1620   v_Cur_Act_Contexts Cur_Act_Contexts%ROWTYPE;*/
1621   --Bug 3079945 Start
1622   -- Cursor for retrieving the summed values for
1623   -- PRSI insurable weeks where Contribution Class
1624   -- starts with IE_A%
1625    l_prev_sequence number;
1626    l_current_sequence number;
1627 
1628    -- for bug 5383808, to get the action_sequence of run locked by
1629    -- the previous P45 archive.
1630    CURSOR cur_get_prev_run_seq is
1631    select paa.action_sequence
1632    from   pay_assignment_actions paa,
1633           pay_payroll_actions ppa,
1634           pay_action_interlocks pai,
1635 	    pay_assignment_actions paa1
1636    where  paa1.source_action_id = p_last_p45_action
1637      and  pai.locking_action_id = paa1.assignment_action_id
1638     and   pai.locked_action_id = paa.assignment_action_id
1639     and   paa.assignment_id in (select papf.assignment_id
1640                                  from per_all_assignments_f papf
1641                                  where papf.person_id = p_person_id
1642                               )
1643     and   paa.tax_unit_id = g_paye_ref
1644     and   paa.payroll_action_id = ppa.payroll_action_id
1645     and   ppa.action_type in ('R','Q','I','B','V');
1646 
1647    -- for bug 5383808, get the action_sequence of run locked by
1648    -- current p45 archive.
1649    CURSOR cur_get_curr_run_seq is
1650    select action_sequence
1651    from   pay_assignment_actions ppa
1652    where  assignment_action_id = p_source_id;
1653 
1654    --bug 5383808. IF previous P45 exists fetch the sum of PRSI insurable
1655    -- weeks for class between the current run action sequence locked by P45
1656    -- and run action locked by previous P45.
1657    CURSOR Cur_Act_Contexts(l_defined_bal_id number) IS
1658    SELECT sum(PAY_BALANCE_PKG.GET_VALUE(l_defined_bal_id, -- changes made
1659     			             pac.ASSIGNMENT_ACTION_ID,
1660                                    g_paye_ref,
1661                                    null,
1662                                    pac.CONTEXT_ID,
1663                                    pac.CONTEXT_VALUE,
1664                                    null,
1665                                    null))
1666   FROM   pay_action_contexts pac,
1667          pay_assignment_actions pas,
1668          pay_payroll_actions ppa
1669   WHERE  pac.Context_Value like 'IE_A%'
1670   AND    pac.assignment_id in (select papf.assignment_id
1671                                  from per_all_assignments_f papf
1672                                  where papf.person_id = p_person_id
1673                               )
1674   AND    pas.tax_unit_id = g_paye_ref
1675   AND    pas.assignment_action_id = pac.assignment_action_id
1676   AND    ppa.payroll_action_id = pas.payroll_action_id
1677  /*AND    ppa.date_earned between to_date('01-01-' || to_char(g_archive_start_date ,'YYYY'),'DD-MM-YYYY') --Bug Fix 3986250*/
1678   AND    ppa.effective_date between to_date('01-01-' || to_char(p_effective_date,'YYYY'),'DD-MM-YYYY') --Bug fix 4108423
1679   AND    g_archive_end_date
1680   and    pas.action_sequence > l_prev_sequence
1681   and    pas.action_sequence <= l_current_sequence;
1682 /*  group by pac.context_id,pac.context_value;*/
1683 
1684 
1685   -- cursor to get defined balance id
1686   -- bug 5383808
1687   cursor csr_defined_bal_id(p_balance_name varchar2) is
1688 	select defined_balance_id
1689 	from   pay_balance_types pbt,
1690 	       pay_balance_dimensions pbd,
1691 		 pay_defined_balances pdb
1692 	where  pbt.balance_name = p_balance_name
1693 	and    pbt.balance_type_id = pdb.balance_type_id
1694 	and    pbd.database_item_suffix = '_ASG_PAYE_REF_PRSI_RUN'
1695 	and    pbd.balance_dimension_id = pdb.balance_dimension_id
1696 	and    pbt.legislation_code = 'IE'
1697 	and    pbd.legislation_code = 'IE';
1698   -- bug 13513882
1699 	 cursor csr_usc_defined_bal_id(p_balance_name varchar2) is
1700 	select defined_balance_id
1701 	from   pay_balance_types pbt,
1702 	       pay_balance_dimensions pbd,
1703 		 pay_defined_balances pdb
1704 	where  pbt.balance_name = p_balance_name
1705 	and    pbt.balance_type_id = pdb.balance_type_id
1706 	and    pbd.database_item_suffix = p_dimension
1707 	and    pbd.balance_dimension_id = pdb.balance_dimension_id
1708 	and    pbt.legislation_code = 'IE'
1709 	and    pbd.legislation_code = 'IE';
1710 
1711 -- get balance from EMEA balances
1712 cursor get_prev_ins_bal is
1713    SELECT to_number(pai.action_information4)    balance_value
1714     FROM   pay_action_information pai
1715     WHERE  pai.action_context_id = p_last_p45_action
1716       AND  pai.action_information_category = 'EMEA BALANCES'
1717       AND  pai.action_information1 = p_defined_bal_id;
1718 
1719 /* 7291676 */
1720 CURSOR Cur_Act_Contexts_ppsn(l_defined_bal_id number,c_ppsn_override per_assignment_extra_info.aei_information1%type) IS
1721    SELECT sum(PAY_BALANCE_PKG.GET_VALUE(l_defined_bal_id, -- changes made
1722     			             pac.ASSIGNMENT_ACTION_ID,
1723                                    g_paye_ref,
1724                                    null,
1725                                    pac.CONTEXT_ID,
1726                                    pac.CONTEXT_VALUE,
1727                                    null,
1728                                    null))
1729   FROM   pay_action_contexts pac,
1730          pay_assignment_actions pas,
1731          pay_payroll_actions ppa
1732   WHERE  pac.Context_Value like 'IE_A%'
1733   AND    pac.assignment_id in (select paaf.assignment_id
1734                                from per_all_assignments_f paaf, per_assignment_extra_info paei
1735 		               where paaf.person_id = p_person_id
1736 			       and paaf.assignment_id=paei.assignment_id
1737 			       and paei.information_type = 'IE_ASG_OVERRIDE'
1738 			       and paei.aei_information1 = c_ppsn_override
1739                               )
1740   AND    pas.tax_unit_id = g_paye_ref
1741   AND    pas.assignment_action_id = pac.assignment_action_id
1742   AND    ppa.payroll_action_id = pas.payroll_action_id
1743  /*AND    ppa.date_earned between to_date('01-01-' || to_char(g_archive_start_date ,'YYYY'),'DD-MM-YYYY') --Bug Fix 3986250*/
1744   AND    ppa.effective_date between to_date('01-01-' || to_char(p_effective_date,'YYYY'),'DD-MM-YYYY') --Bug fix 4108423
1745   AND    g_archive_end_date
1746   and    pas.action_sequence > l_prev_sequence
1747   and    pas.action_sequence <= l_current_sequence;
1748 
1749 cursor csr_ppsn_override(p_asg_id number)
1750 is
1751 select aei_information1 PPSN_OVERRIDE
1752 from per_assignment_extra_info
1753 where assignment_id = p_asg_id
1754 and aei_information_category = 'IE_ASG_OVERRIDE';
1755 
1756 l_ppsn_override per_assignment_extra_info.aei_information1%type;
1757 
1758    v_Cur_Act_Contexts Cur_Act_Contexts%ROWTYPE;
1759   --Bug 3079945 End
1760   l_action_info_id                 NUMBER;
1761   l_balance_value                  NUMBER:=0;
1762   l_balance_value_classA           NUMBER;
1763   l_ovn                            NUMBER;
1764   l_record_count                   VARCHAR2(10);
1765   l_proc                           VARCHAR2(50) := g_package || 'process_balance';
1766   l_balance_type_id                NUMBER;
1767   l_balance_value1                 NUMBER:=0;
1768   l_balance_value2                 NUMBER :=0;
1769   --bug 5383808
1770   l_p45_last_bal_value		     NUMBER :=0;
1771   l_defined_id			     NUMBER;
1772   l_pre_ins_bal			     number;
1773   l_bik_tax_prsi_pay  NUMBER;
1774   l_court_order_child  NUMBER;
1775   l_court_order  NUMBER;
1776   l_gross_income_adj  NUMBER;
1777    l_bik_tax_prsi_pay_value  NUMBER;
1778   l_court_order_child_value  NUMBER;
1779   l_court_order_value  NUMBER;
1780   l_gross_income_adj_value  NUMBER;
1781   l_gross_last_bal_value NUMBER;
1782   l_court_order_bal_value NUMBER;
1783   l_court_order_child_bal_value NUMBER;
1784   l_bik_bal_value NUMBER;
1785 --13013273
1786 l_prsa_er_contr NUMBER;
1787 l_prsa_er_contr_id NUMBER;
1788 --13013273
1789   --6615117
1790   l_source_null_flag NUMBER:= 0;
1791 
1792   BEGIN
1793    -- hr_utility.trace_on(null,'P45');
1794     hr_utility.set_location('Entering ' || l_proc,10);
1795     hr_utility.set_location('Step ' || l_proc,20);
1796     hr_utility.set_location('p_action_context_id      = ' || p_action_context_id,20);
1797     hr_utility.set_location('p_assignment_id      = ' || p_assignment_id,20);
1798     hr_utility.set_location('p_person_id      = ' || p_person_id,20);
1799     hr_utility.set_location('p_source_id      = ' || p_source_id,20);
1800     hr_utility.set_location('p_effective_date      = ' || p_effective_date,20);
1801     hr_utility.set_location('p_balance        = ' || p_balance,20);
1802     hr_utility.set_location('p_dimension      = ' || p_dimension,20);
1803     hr_utility.set_location('p_defined_bal_id = ' || p_defined_bal_id,20);
1804     hr_utility.set_location('p_record_count = ' || p_record_count,20);
1805     hr_utility.set_location('p_termination_date = ' || p_termination_date,20);
1806     hr_utility.set_location('p_supp_flag = ' || p_supp_flag,20);
1807     hr_utility.set_location('p_last_p45_action = ' || p_last_p45_action,20);
1808     hr_utility.set_location('p_last_p45_pact = ' || p_last_p45_pact,20);
1809     hr_utility.set_location('p_prev_src_id = ' || p_prev_src_id,20);
1810 
1811      IF p_balance = 'IE PRSI_ClassA Insurable Weeks'
1812      THEN
1813      		--bug 5383808
1814 		OPEN cur_get_prev_run_seq;
1815 		FETCH cur_get_prev_run_seq into l_prev_sequence;
1816 		CLOSE cur_get_prev_run_seq;
1817 
1818 		IF l_prev_sequence IS NULL THEN
1819 			l_prev_sequence := 0;
1820 		END IF;
1821 
1822 		open cur_get_curr_run_seq;
1823 		fetch cur_get_curr_run_seq into l_current_sequence;
1824 		CLOSE cur_get_curr_run_seq;
1825 
1826 		if l_current_sequence is null then
1827 			l_current_sequence := 0;
1828 		end if;
1829 
1830 		/*OPEN csr_bal_type;
1831 		FETCH csr_bal_type into l_balance_type_id;
1832 		CLOSE csr_bal_type;*/
1833 
1834 
1835 		open csr_defined_bal_id('IE PRSI Insurable Weeks');
1836 		FETCH csr_defined_bal_id into l_defined_id;
1837 		CLOSE csr_defined_bal_id;
1838 
1839             hr_utility.set_location ('l_prev_sequence..'||l_prev_sequence,200);
1840 		hr_utility.set_location ('l_current_sequence..'||l_current_sequence,200);
1841 		hr_utility.set_location ('l_defined_id..'||l_defined_id,200);
1842 		--Commented code for bug fix 3079945
1843 		/*OPEN csr_iea_weeks(l_balance_type_id);
1844 		FETCH csr_iea_weeks into l_balance_value;
1845 		CLOSE csr_iea_weeks; */
1846 		/*OPEN Cur_Act_Contexts;
1847 		FETCH Cur_Act_Contexts INTO v_Cur_Act_Contexts;
1848 		IF Cur_Act_Contexts%FOUND
1849 		THEN
1850 			l_balance_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
1851                                                           v_Cur_Act_Contexts.ASSIGNMENT_ACTION_ID,
1852                                                           null,
1853                                                           null,
1854                                                           v_Cur_Act_Contexts.CONTEXT_ID,
1855                                                           v_Cur_Act_Contexts.CONTEXT_VALUE,
1856                                                           null,
1857                                                           null);
1858 		END IF;
1859 		CLOSE Cur_Act_Contexts;*/
1860 
1861 		--Bug 3079945 start
1862 
1863 		  /* 7291676 */
1864                  l_ppsn_override:=null;
1865 	         OPEN csr_ppsn_override(p_assignment_id);
1866 	         FETCH csr_ppsn_override INTO  l_ppsn_override;
1867 	         CLOSE csr_ppsn_override;
1868                 hr_utility.set_location('PPSN Override  value  = ' || l_ppsn_override,200);
1869 		IF l_ppsn_override IS NOT NULL THEN
1870                 OPEN  Cur_Act_Contexts_ppsn(l_defined_id,l_ppsn_override);
1871 		FETCH Cur_Act_Contexts_ppsn into l_balance_value;
1872 		CLOSE Cur_Act_Contexts_ppsn;
1873 		ELSE
1874 		OPEN  Cur_Act_Contexts(l_defined_id);
1875 		FETCH Cur_Act_Contexts into l_balance_value;
1876 		CLOSE Cur_Act_Contexts;
1877 		END IF;
1878 
1879 
1880 		--Bug 3079945 End
1881 		/*IF nvl(p_supp_flag,'N') = 'N' AND (p_last_p45_action IS NOT NULL) then
1882 			l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
1883     			                                                  p_last_p45_action,
1884 											  g_paye_ref,
1885 										        null,
1886 										        null,
1887 										        null,
1888 										        null,
1889 										        null);
1890 			l_balance_value := l_balance_value - l_p45_last_bal_value;
1891 		 END IF;*/
1892 		 l_balance_value := nvl(l_balance_value,0);
1893 		 hr_utility.set_location('IE PRSI_ClassA Insurable Weeks..'||l_balance_value,1000);
1894 		 IF p_supp_flag = 'Y' AND p_last_p45_action IS NOT NULL THEN
1895 			open get_prev_ins_bal;
1896 			FETCH get_prev_ins_bal into l_pre_ins_bal;
1897 			CLOSE get_prev_ins_bal;
1898 			l_balance_value := l_balance_value + l_pre_ins_bal;
1899 		 END IF;
1900 		 hr_utility.set_location('After IE PRSI_ClassA Insurable Weeks..'||l_pre_ins_bal,1000.1);
1901        ELSE
1902   /*        l_balance_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
1903                                                       p_source_id );
1904   */
1905 		--bug 5383808, call this only p_source_id is not null.
1906 		if p_source_id is not null then
1907 			l_balance_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
1908     			                 p_source_id,
1909                                    g_paye_ref,
1910                                    null,
1911                                    null,
1912                                    null,
1913                                    null,
1914                                    null);
1915 		--6615117
1916 		Else
1917 			l_source_null_flag := 1;
1918 		end if;
1919   --bug 13513882
1920   IF p_balance like 'IE USCable Pay' THEN
1921 	 OPEN csr_usc_defined_bal_id('IE Gross Income Adjustment');
1922 		FETCH csr_usc_defined_bal_id INTO l_gross_income_adj;
1923 		IF csr_usc_defined_bal_id%NOTFOUND THEN
1924 		 l_gross_income_adj := 0;
1925 		END IF;
1926 	 CLOSE csr_usc_defined_bal_id ;
1927          l_gross_income_adj_value := PAY_BALANCE_PKG.GET_VALUE(l_gross_income_adj,
1928     			                 p_source_id,
1929                                    g_paye_ref,
1930                                    null,
1931                                    null,
1932                                    null,
1933                                    null,
1934                                    null);
1935 	OPEN csr_usc_defined_bal_id('IE Court Order');
1936 		FETCH csr_usc_defined_bal_id INTO l_court_order;
1937 		IF csr_usc_defined_bal_id%NOTFOUND THEN
1938 		 l_court_order := 0;
1939 		END IF;
1940 	CLOSE csr_usc_defined_bal_id ;
1941           l_court_order_value := PAY_BALANCE_PKG.GET_VALUE(l_court_order,
1942     			                 p_source_id,
1943                                    g_paye_ref,
1944                                    null,
1945                                    null,
1946                                    null,
1947                                    null,
1948                                    null);
1949 	OPEN csr_usc_defined_bal_id('IE Court Order Child Portion');
1950 		FETCH csr_usc_defined_bal_id INTO l_court_order_child;
1951 		IF csr_usc_defined_bal_id%NOTFOUND THEN
1952 		l_court_order_child := 0;
1953 		END IF;
1954 	CLOSE csr_usc_defined_bal_id ;
1955           l_court_order_child_value := PAY_BALANCE_PKG.GET_VALUE(l_court_order_child,
1956     			                 p_source_id,
1957                                    g_paye_ref,
1958                                    null,
1959                                    null,
1960                                    null,
1961                                    null,
1962                                    null);
1963 	OPEN csr_usc_defined_bal_id('IE BIK Taxable and PRSIable Pay');
1964 		FETCH csr_usc_defined_bal_id INTO l_bik_tax_prsi_pay;
1965 		IF csr_usc_defined_bal_id%NOTFOUND THEN
1966 		  l_bik_tax_prsi_pay := 0;
1967 		END IF;
1968 	CLOSE csr_usc_defined_bal_id ;
1969         l_bik_tax_prsi_pay_value := PAY_BALANCE_PKG.GET_VALUE(l_bik_tax_prsi_pay,
1970     			                 p_source_id,
1971                                    g_paye_ref,
1972                                    null,
1973                                    null,
1974                                    null,
1975                                    null,
1976                                    null);
1977 --13013273
1978 	OPEN csr_usc_defined_bal_id('IE PRSA ER Contribution');
1979 		FETCH csr_usc_defined_bal_id INTO l_prsa_er_contr_id;
1980 		IF csr_usc_defined_bal_id%NOTFOUND THEN
1981 		  l_prsa_er_contr_id := 0;
1982 		END IF;
1983 	CLOSE csr_usc_defined_bal_id ;
1984         l_prsa_er_contr := PAY_BALANCE_PKG.GET_VALUE(l_prsa_er_contr_id,
1985     			                 p_source_id,
1986                                    g_paye_ref,
1987                                    null,
1988                                    null,
1989                                    null,
1990                                    null,
1991                                    null);
1992 --13013273
1993 
1994   l_balance_value := l_balance_value + l_gross_income_adj_value - l_court_order_value + l_court_order_child_value + l_bik_tax_prsi_pay_value
1995                      + l_prsa_er_contr; --13013273
1996 END IF;
1997 -- end bug 13513882
1998 
1999 		hr_utility.set_location('sg Supp Flag ='||p_supp_flag,36);
2000 		hr_utility.set_location('sg Last P45 Action ='||p_last_p45_action,37);
2001 
2002 		--bug 5383808
2003             IF (p_balance like 'IE Taxable Pay') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2004 			l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2005     			                                                  p_prev_src_id,
2006 											  g_paye_ref,
2007 										        null,
2008 										        null,
2009 										        null,
2010 										        null,
2011 										        null);
2012 
2013 			hr_utility.set_location('before IE Taxable Pay '||l_balance_value,204);
2014 			hr_utility.set_location('before IE Taxable Pay '||l_p45_last_bal_value,205);
2015 			hr_utility.set_location('p_last_p45_action '||p_last_p45_action,206);
2016 			hr_utility.set_location('p_defined_bal_id '||p_defined_bal_id,207);
2017 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2018 			--6615117
2019 			IF l_source_null_flag = 1 THEN
2020 			  l_balance_value:= l_balance_value;
2021 			ELSE
2022 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2023 			END IF;
2024 
2025 		END IF;
2026 
2027 
2028 		IF (p_balance like 'IE P45 Pay') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2029 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2030 									  p_prev_src_id,
2031 									  g_paye_ref,
2032 								        null,
2033 								        null,
2034 								        null,
2035 								        null,
2036 								        null);
2037 
2038 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2039 			--6615117
2040 			IF l_source_null_flag = 1 THEN
2041 			  l_balance_value:= l_balance_value;
2042 			ELSE
2043 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2044 			END IF;
2045 		END IF;
2046 
2047 		IF (p_balance like 'IE P45 Tax Deducted') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2048 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2049                                                         p_prev_src_id,
2050 									  g_paye_ref,
2051 								        null,
2052 								        null,
2053 								        null,
2054 								        null,
2055 								        null);
2056 
2057 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2058 			--6615117
2059 			IF l_source_null_flag = 1 THEN
2060 			  l_balance_value:= l_balance_value;
2061 			ELSE
2062 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2063 			END IF;
2064 		END IF;
2065 
2066 		IF (p_balance like 'IE Net Tax') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2067 			l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2068                                                         p_prev_src_id,
2069 									  g_paye_ref,
2070 								        null,
2071 								        null,
2072 								        null,
2073 								        null,
2074 								        null);
2075 
2076 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2077 			--6615117
2078 			IF l_source_null_flag = 1 THEN
2079 			  l_balance_value:= l_balance_value;
2080 			ELSE
2081 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2082 			END IF;
2083 		END IF;
2084 	-- 13359530 13513882
2085 IF (p_balance like 'IE USCable Pay') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2086 			l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2087                                                         p_prev_src_id,
2088 									  g_paye_ref,
2089 								        null,
2090 								        null,
2091 								        null,
2092 								        null,
2093 								        null);
2094 	-- bug  13513882
2095 			l_gross_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(l_gross_income_adj,
2096                                                         p_prev_src_id,
2097 									  g_paye_ref,
2098 								        null,
2099 								        null,
2100 								        null,
2101 								        null,
2102 								        null);
2103                  l_court_order_bal_value := PAY_BALANCE_PKG.GET_VALUE(l_court_order,
2104                                                         p_prev_src_id,
2105 									  g_paye_ref,
2106 								        null,
2107 								        null,
2108 								        null,
2109 								        null,
2110 								        null);
2111                  l_court_order_child_bal_value := PAY_BALANCE_PKG.GET_VALUE(l_court_order_child,
2112                                                         p_prev_src_id,
2113 									  g_paye_ref,
2114 								        null,
2115 								        null,
2116 								        null,
2117 								        null,
2118 								        null);
2119                  l_bik_bal_value := PAY_BALANCE_PKG.GET_VALUE(l_bik_tax_prsi_pay,
2120                                                         p_prev_src_id,
2121 									  g_paye_ref,
2122 								        null,
2123 								        null,
2124 								        null,
2125 								        null,
2126 								        null);
2127 --13013273
2128                  l_prsa_er_contr := PAY_BALANCE_PKG.GET_VALUE(l_prsa_er_contr_id,
2129                                                         p_prev_src_id,
2130 									  g_paye_ref,
2131 								        null,
2132 								        null,
2133 								        null,
2134 								        null,
2135 								        null);
2136 --13013273
2137 
2138 l_p45_last_bal_value := l_p45_last_bal_value +  l_gross_last_bal_value - l_court_order_bal_value + l_court_order_child_bal_value + l_bik_bal_value
2139                         + l_prsa_er_contr;  --13013273
2140 
2141        -- bug  13513882 end
2142 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2143 			--6615117
2144 			IF l_source_null_flag = 1 THEN
2145 			  l_balance_value:= l_balance_value;
2146 			ELSE
2147 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2148 			END IF;
2149 		END IF;
2150 IF (p_balance like 'IE USC Balance') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2151 			l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2152                                                         p_prev_src_id,
2153 									  g_paye_ref,
2154 								        null,
2155 								        null,
2156 								        null,
2157 								        null,
2158 								        null);
2159 
2160 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2161 			--6615117
2162 			IF l_source_null_flag = 1 THEN
2163 			  l_balance_value:= l_balance_value;
2164 			ELSE
2165 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2166 			END IF;
2167 		END IF;
2168 IF (p_balance like 'IE P45 USC Pay') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2169 			l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2170                                                         p_prev_src_id,
2171 									  g_paye_ref,
2172 								        null,
2173 								        null,
2174 								        null,
2175 								        null,
2176 								        null);
2177 
2178 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2179 			--6615117
2180 			IF l_source_null_flag = 1 THEN
2181 			  l_balance_value:= l_balance_value;
2182 			ELSE
2183 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2184 			END IF;
2185 		END IF;
2186 IF (p_balance like 'IE P45 USC Deducted') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2187 			l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2188                                                         p_prev_src_id,
2189 									  g_paye_ref,
2190 								        null,
2191 								        null,
2192 								        null,
2193 								        null,
2194 								        null);
2195 
2196 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2197 			--6615117
2198 			IF l_source_null_flag = 1 THEN
2199 			  l_balance_value:= l_balance_value;
2200 			ELSE
2201 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2202 			END IF;
2203 		END IF;
2204 -- 13359530
2205 		IF (p_balance like 'IE PRSI Insurable Weeks') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2206 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2207 								        p_prev_src_id,
2208 									  g_paye_ref,
2209 								        null,
2210 								        null,
2211 								        null,
2212 								        null,
2213 								        null);
2214 
2215 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2216 			--6615117
2217 			IF l_source_null_flag = 1 THEN
2218 			  l_balance_value:= l_balance_value;
2219 			ELSE
2220 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2221 			END IF;
2222 		END IF;
2223 
2224 	-- changes made for PRSI bug 5510536. To show this employment figures only.
2225 		IF (p_balance like 'IE PRSI Employer') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2226 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2227 								        p_prev_src_id,
2228 									  g_paye_ref,
2229 								        null,
2230 								        null,
2231 								        null,
2232 								        null,
2233 								        null);
2234 
2235 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2236 			--6615117
2237 			IF l_source_null_flag = 1 THEN
2238 			  l_balance_value:= l_balance_value;
2239 			ELSE
2240 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2241 			END IF;
2242 		END IF;
2243 
2244 		IF (p_balance like 'IE PRSI K Employer Lump Sum') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2245 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2246 								        p_prev_src_id,
2247 									  g_paye_ref,
2248 								        null,
2249 								        null,
2250 								        null,
2251 								        null,
2252 								        null);
2253 
2254 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2255 			--6615117
2256 			IF l_source_null_flag = 1 THEN
2257 			  l_balance_value:= l_balance_value;
2258 			ELSE
2259 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2260 			END IF;
2261 		END IF;
2262 
2263 		IF (p_balance like 'IE PRSI M Employer Lump Sum') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2264 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2265 								        p_prev_src_id,
2266 									  g_paye_ref,
2267 								        null,
2268 								        null,
2269 								        null,
2270 								        null,
2271 								        null);
2272 
2273 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2274 			--6615117
2275 			IF l_source_null_flag = 1 THEN
2276 			  l_balance_value:= l_balance_value;
2277 			ELSE
2278 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2279 			END IF;
2280 		END IF;
2281 
2282 		IF (p_balance like 'IE PRSI Employee') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2283 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2284 								        p_prev_src_id,
2285 									  g_paye_ref,
2286 								        null,
2287 								        null,
2288 								        null,
2289 								        null,
2290 								        null);
2291 
2292 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2293 			--6615117
2294 			IF l_source_null_flag = 1 THEN
2295 			  l_balance_value:= l_balance_value;
2296 			ELSE
2297 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2298 			END IF;
2299 		END IF;
2300 
2301 		IF (p_balance like 'IE PRSI K Employee Lump Sum') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2302 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2303 								        p_prev_src_id,
2304 									  g_paye_ref,
2305 								        null,
2306 								        null,
2307 								        null,
2308 								        null,
2309 								        null);
2310 
2311 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2312 			--6615117
2313 			IF l_source_null_flag = 1 THEN
2314 			  l_balance_value:= l_balance_value;
2315 			ELSE
2316 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2317 			END IF;
2318 		END IF;
2319 
2320 		IF (p_balance like 'IE PRSI M Employee Lump Sum') AND (nvl(p_supp_flag,'N') = 'N') AND (p_last_p45_action IS NOT NULL) THEN
2321 		      l_p45_last_bal_value := PAY_BALANCE_PKG.GET_VALUE(p_defined_bal_id,
2322 								        p_prev_src_id,
2323 									  g_paye_ref,
2324 								        null,
2325 								        null,
2326 								        null,
2327 								        null,
2328 								        null);
2329 
2330 			--l_balance_value := l_balance_value - l_p45_last_bal_value;
2331 			--6615117
2332 			IF l_source_null_flag = 1 THEN
2333 			  l_balance_value:= l_balance_value;
2334 			ELSE
2335 			  l_balance_value := l_balance_value - l_p45_last_bal_value;
2336 			END IF;
2337 		END IF;
2338 
2339 	END IF;
2340 
2341 	-- end changes made for PRSI
2342 
2343      --end bug 5383808
2344     hr_utility.set_location('l_balance_value = ' || l_balance_value,20);
2345 
2346     IF p_record_count = 0
2347     THEN
2348        l_record_count := NULL;
2349     ELSE
2350        l_record_count := p_record_count + 1;
2351     END IF;
2352 
2353     IF l_balance_value <> 0
2354     THEN
2355       hr_utility.set_location('Archiving EMEA BALANCES',20);
2356       pay_action_information_api.create_action_information (
2357         p_action_information_id        =>  l_action_info_id
2358       , p_action_context_id            =>  p_action_context_id
2359       , p_action_context_type          =>  'AAP'
2360       , p_object_version_number        =>  l_ovn
2361       , p_assignment_id                =>  p_assignment_id
2362       , p_effective_date               =>  p_effective_date
2363       , p_source_id                    =>  p_source_id
2364       , p_source_text                  =>  NULL
2365       , p_action_information_category  =>  'EMEA BALANCES'
2366       , p_action_information1          =>  p_defined_bal_id
2367       , p_action_information2          =>  NULL
2368       , p_action_information3          =>  NULL
2369       , p_action_information4          =>  l_balance_value
2370       , p_action_information5          =>  l_record_count);
2371     END IF;
2372 
2373     hr_utility.set_location('Leaving ' || l_proc,30);
2374   EXCEPTION
2375     WHEN NO_DATA_FOUND
2376     THEN
2377       NULL;
2378   END process_balance;
2379   ---------------------
2380     PROCEDURE process_supp_balance (p_action_context_id IN NUMBER,
2381                                     p_assignment_id     IN NUMBER,
2382                                     p_person_id         IN NUMBER,
2383 						p_source_id         IN NUMBER,
2384 						p_effective_date    IN DATE,
2385 						p_balance           IN VARCHAR2,
2386 						p_dimension         IN VARCHAR2,
2387 						p_defined_bal_id    IN NUMBER,
2388 						p_record_count      IN NUMBER,
2389   						p_termination_date  IN DATE,
2390 						p_supp_flag         IN VARCHAR2,
2391   						p_last_p45_action   IN NUMBER,
2392   						p_last_p45_pact     IN NUMBER,        -- Bug 5005788
2393   						p_ytd_balance       IN VARCHAR2,
2394   						p_ytd_def_bal_id    IN NUMBER)
2395     IS
2396     --
2397     -- Cursor for retrieving balance type id of defined balance
2398     --
2399     CURSOR csr_bal_type IS
2400      select balance_type_id
2401      from   pay_defined_balances
2402      where  defined_balance_id = p_defined_bal_id;
2403 
2404      CURSOR csr_get_curr_val(p_action_context_id NUMBER,p_def_bal_id NUMBER) IS
2405     SELECT to_number(pai.action_information4)    balance_value
2406     FROM   pay_action_information pai
2407     WHERE  pai.action_context_id = p_action_context_id
2408       AND  pai.action_information_category = 'EMEA BALANCES'
2409       AND  pai.action_information1 = p_def_bal_id;
2410 
2411 -- cursor to fetch source_id from the last p45 action
2412 CURSOR get_last_source_id is
2413 select source_id from
2414 	pay_action_information pai,
2415 	pay_assignment_actions paa
2416 where paa.assignment_action_id = p_last_p45_action
2417   and paa.assignment_action_id = pai.action_context_id
2418   and pai.action_information_category='EMEA BALANCES';
2419 
2420   -- bug 14215867 start
2421  cursor csr_usc_defined_bal_id(p_balance_name varchar2) is
2422 	select defined_balance_id
2423 	from   pay_balance_types pbt,
2424 	       pay_balance_dimensions pbd,
2425 		 pay_defined_balances pdb
2426 	where  pbt.balance_name = p_balance_name
2427 	and    pbt.balance_type_id = pdb.balance_type_id
2428 	and    pbd.database_item_suffix = '_PER_PAYE_REF_YTD'
2429 	and    pbd.balance_dimension_id = pdb.balance_dimension_id
2430 	and    pbt.legislation_code = 'IE'
2431 	and    pbd.legislation_code = 'IE';
2432 
2433 l_gross_income_adj NUMBER;
2434 l_gross_income_adj_value NUMBER;
2435 l_court_order NUMBER;
2436 l_court_order_value NUMBER;
2437 l_court_order_child NUMBER;
2438 l_court_order_child_value NUMBER;
2439 l_bik_tax_prsi_pay NUMBER;
2440 l_bik_tax_prsi_pay_value NUMBER;
2441 l_prsa_er_contr_id NUMBER;
2442 l_prsa_er_contr NUMBER;
2443  -- bug 14215867 end
2444 
2445 
2446     --Bug 3079945 End
2447     l_action_info_id                 NUMBER;
2448     l_balance_value                  NUMBER:=0;
2449     l_balance_value_classA           NUMBER;
2450     l_ovn                            NUMBER;
2451     l_record_count                   VARCHAR2(10);
2452     l_proc                           VARCHAR2(50) := g_package || 'process_supp_balance';
2453     l_balance_type_id                NUMBER;
2454     l_balance_value1                 NUMBER:=0;
2455     l_prev_source_id			 number;
2456     BEGIN
2457       hr_utility.set_location('Entering ' || l_proc,10);
2458       hr_utility.set_location('Step ' || l_proc,20);
2459       hr_utility.set_location('p_action_context_id      = ' || p_action_context_id,20);  /* 7291676 */
2460       hr_utility.set_location('p_assignment_id      = ' || p_assignment_id,20);
2461       hr_utility.set_location('p_person_id      = ' || p_person_id,20);
2462       hr_utility.set_location('p_source_id      = ' || p_source_id,20);
2463       hr_utility.set_location('p_balance        = ' || p_balance,20);
2464       hr_utility.set_location('p_dimension      = ' || p_dimension,20);
2465       hr_utility.set_location('p_defined_bal_id = ' || p_defined_bal_id,20);
2466 
2467       hr_utility.set_location('p_last_p45_action      = ' || p_last_p45_action,20);
2468       hr_utility.set_location('p_action_context_id        = ' || p_action_context_id,20);
2469       hr_utility.set_location('p_last_p45_pact      = ' || p_last_p45_pact,20);
2470       hr_utility.set_location('p_ytd_balance = ' || p_ytd_balance,20);
2471       hr_utility.set_location('p_ytd_def_bal_id = ' || p_ytd_def_bal_id,20);
2472 
2473       OPEN csr_get_curr_val(p_action_context_id,p_ytd_def_bal_id);
2474       FETCH csr_get_curr_val INTO l_balance_value;
2475       CLOSE csr_get_curr_val;
2476 	hr_utility.set_location('l_balance_value = ' || l_balance_value,20);
2477 
2478  --     l_balance_value := get_arc_bal_value(p_action_context_id,p_ytd_balance);
2479 
2480       IF p_last_p45_action IS NOT NULL THEN
2481 	-- commented by vikas
2482 	if p_balance = 'IE PRSI_ClassA Insurable WeeksASG_PTD' then
2483 		l_balance_value1 := get_arc_bal_value(p_last_p45_action,p_last_p45_pact,p_ytd_balance);     -- Bug 5005788
2484 	ELSE
2485 		OPEN get_last_source_id;
2486 		FETCH get_last_source_id into l_prev_source_id;
2487 		CLOSE get_last_source_id;
2488 		l_balance_value1 := PAY_BALANCE_PKG.GET_VALUE(p_ytd_def_bal_id,
2489 						     l_prev_source_id,
2490 						     g_paye_ref,
2491 						     null,
2492 						     null,
2493 						     null,
2494 						     null,
2495 						     null);
2496 
2497 -- bug 14215867
2498  --bug 13513882
2499   IF p_balance like 'IE USCable PayASG_RUN' THEN
2500 	 OPEN csr_usc_defined_bal_id('IE Gross Income Adjustment');
2501 		FETCH csr_usc_defined_bal_id INTO l_gross_income_adj;
2502 		IF csr_usc_defined_bal_id%NOTFOUND THEN
2503 		 l_gross_income_adj := 0;
2504 		END IF;
2505 	 CLOSE csr_usc_defined_bal_id ;
2506          l_gross_income_adj_value := PAY_BALANCE_PKG.GET_VALUE(l_gross_income_adj,
2507     			                 l_prev_source_id,
2508                                    g_paye_ref,
2509                                    null,
2510                                    null,
2511                                    null,
2512                                    null,
2513                                    null);
2514 	OPEN csr_usc_defined_bal_id('IE Court Order');
2515 		FETCH csr_usc_defined_bal_id INTO l_court_order;
2516 		IF csr_usc_defined_bal_id%NOTFOUND THEN
2517 		 l_court_order := 0;
2518 		END IF;
2519 	CLOSE csr_usc_defined_bal_id ;
2520           l_court_order_value := PAY_BALANCE_PKG.GET_VALUE(l_court_order,
2521     			                 l_prev_source_id,
2522                                    g_paye_ref,
2523                                    null,
2524                                    null,
2525                                    null,
2526                                    null,
2527                                    null);
2528 	OPEN csr_usc_defined_bal_id('IE Court Order Child Portion');
2529 		FETCH csr_usc_defined_bal_id INTO l_court_order_child;
2530 		IF csr_usc_defined_bal_id%NOTFOUND THEN
2531 		l_court_order_child := 0;
2532 		END IF;
2533 	CLOSE csr_usc_defined_bal_id ;
2534           l_court_order_child_value := PAY_BALANCE_PKG.GET_VALUE(l_court_order_child,
2535     			                 l_prev_source_id,
2536                                    g_paye_ref,
2537                                    null,
2538                                    null,
2539                                    null,
2540                                    null,
2541                                    null);
2542 	OPEN csr_usc_defined_bal_id('IE BIK Taxable and PRSIable Pay');
2543 		FETCH csr_usc_defined_bal_id INTO l_bik_tax_prsi_pay;
2544 		IF csr_usc_defined_bal_id%NOTFOUND THEN
2545 		  l_bik_tax_prsi_pay := 0;
2546 		END IF;
2547 	CLOSE csr_usc_defined_bal_id ;
2548         l_bik_tax_prsi_pay_value := PAY_BALANCE_PKG.GET_VALUE(l_bik_tax_prsi_pay,
2549     			                 l_prev_source_id,
2550                                    g_paye_ref,
2551                                    null,
2552                                    null,
2553                                    null,
2554                                    null,
2555                                    null);
2556 --13013273
2557 	OPEN csr_usc_defined_bal_id('IE PRSA ER Contribution');
2558 		FETCH csr_usc_defined_bal_id INTO l_prsa_er_contr_id;
2559 		IF csr_usc_defined_bal_id%NOTFOUND THEN
2560 		  l_prsa_er_contr_id := 0;
2561 		END IF;
2562 	CLOSE csr_usc_defined_bal_id ;
2563         l_prsa_er_contr := PAY_BALANCE_PKG.GET_VALUE(l_prsa_er_contr_id,
2564     			                 l_prev_source_id,
2565                                    g_paye_ref,
2566                                    null,
2567                                    null,
2568                                    null,
2569                                    null,
2570                                    null);
2571 --13013273
2572 
2573   l_balance_value1 := l_balance_value1 + l_gross_income_adj_value - l_court_order_value + l_court_order_child_value + l_bik_tax_prsi_pay_value
2574                      + l_prsa_er_contr; --13013273
2575 END IF;
2576 -- end bug 13513882
2577 --end bug 14215867
2578 
2579       END IF;
2580 	END IF;
2581 	hr_utility.set_location('l_balance_value1 = ' || l_balance_value1,20);
2582 
2583       l_balance_value := l_balance_value - l_balance_value1;
2584 
2585       IF p_record_count = 0
2586       THEN
2587          l_record_count := NULL;
2588       ELSE
2589          l_record_count := p_record_count + 1;
2590       END IF;
2591       IF l_balance_value <> 0
2592       THEN
2593         hr_utility.set_location('Archiving EMEA BALANCES',20);
2594         pay_action_information_api.create_action_information (
2595           p_action_information_id        =>  l_action_info_id
2596         , p_action_context_id            =>  p_action_context_id
2597         , p_action_context_type          =>  'AAP'
2598         , p_object_version_number        =>  l_ovn
2599         , p_assignment_id                =>  p_assignment_id
2600         , p_effective_date               =>  p_effective_date
2601         , p_source_id                    =>  p_source_id
2602         , p_source_text                  =>  NULL
2603         , p_action_information_category  =>  'EMEA BALANCES'
2604         , p_action_information1          =>  p_defined_bal_id
2605         , p_action_information2          =>  NULL
2606         , p_action_information3          =>  NULL
2607         , p_action_information4          =>  l_balance_value
2608         , p_action_information5          =>  l_record_count);
2609       END IF;
2610       hr_utility.set_location('Leaving ' || l_proc,30);
2611     EXCEPTION
2612       WHEN NO_DATA_FOUND
2613       THEN
2614         NULL;
2615   END process_supp_balance;
2616 
2617   --------------------------------------------------------------------------------
2618 -- To get the termination date and supplement flag
2619 -- for bug 5383808
2620 --------------------------------------------------------------------------------
2621 PROCEDURE get_termination_date (p_action_context_id       IN  NUMBER,
2622                                 p_assignment_id           IN  NUMBER,
2623                                 p_person_id               IN NUMBER,
2624 				p_date_earned		  IN DATE,
2625 			        p_termination_date        OUT NOCOPY DATE,
2626 				p_supp_pymt_date	  OUT NOCOPY DATE,
2627 			        p_supp_flag		  OUT NOCOPY VARCHAR2,
2628 			        p_deceased_flag           OUT NOCOPY VARCHAR2
2629 			       ) is
2630 
2631 CURSOR cur_service_leave IS
2632   select decode(ppos.leaving_reason, 'D','Y','N'),
2633         ppos.actual_termination_date
2634   from  per_periods_of_service ppos
2635   where ppos.person_id = p_person_id
2636   and   ppos.period_of_service_id = (select max(paf.period_of_service_id)
2637                                         from per_all_assignments_f paf,
2638                                              pay_assignment_actions paa,
2639   					               pay_action_interlocks pai
2640   	                               where   pai.locking_action_id = p_action_context_id
2641   				                 and pai.locked_action_id  = paa.assignment_action_id
2642                                          and paa.action_status IN ('C','S') --10225372
2643                                          and paa.assignment_id = paf.assignment_id
2644                                      );
2645 
2646 CURSOR cur_max_end_date IS
2647 SELECT max(paaf.effective_end_date)
2648 FROM  per_all_assignments_f paaf,
2649       pay_all_payrolls_f papf,
2650       hr_soft_coding_keyflex scl
2651 WHERE paaf.person_id = p_person_id
2652   AND paaf.payroll_id = papf.payroll_id
2653   AND papf.soft_coding_keyflex_id = scl.soft_coding_keyflex_id
2654   AND scl.segment4 = to_char(g_paye_ref)
2655   AND paaf.assignment_status_type_id in
2656 			   (SELECT ast.assignment_status_type_id
2657 			      FROM per_assignment_status_types ast
2658 			     WHERE  ast.per_system_status in ('ACTIVE_ASSIGN','SUSP_ASSIGN')
2659 			   )
2660   AND paaf.effective_end_date between g_archive_start_date and g_archive_end_date;
2661 
2662 /* changed the cursor to handle case where 2 user defined assignment status exist mapping to
2663    same per_system_status (5073577) */
2664 CURSOR cur_get_asg_end_date IS
2665 SELECT max(effective_end_date)
2666 FROM per_all_assignments_f paaf
2667 WHERE paaf.assignment_id = p_assignment_id
2668   AND paaf.assignment_status_type_id in
2669 			   (SELECT ast.assignment_status_type_id
2670 			      FROM per_assignment_status_types ast
2671 			     WHERE  ast.per_system_status in ('ACTIVE_ASSIGN','SUSP_ASSIGN')
2672 			   );
2673 
2674 
2675 
2676 cursor cur_supp_run is
2677 select act_inf.action_information3
2678  from  pay_assignment_actions paa_run,
2679        pay_action_interlocks pai,
2680        pay_assignment_actions paa,
2681        pay_payroll_actions ppa,
2682        pay_action_information act_inf
2683  where ppa.payroll_action_id = paa.payroll_action_id
2684   and  ppa.report_type = 'P45'
2685   and  ppa.report_qualifier = 'IE'
2686   and  ppa.action_type = 'X'
2687   and  paa.assignment_action_id = act_inf.action_context_id
2688   and  act_inf.action_information_category = 'IE P45 INFORMATION'
2689   and  act_inf.action_context_type = 'AAP'
2690   and  ppa.payroll_action_id <> g_archive_pact
2691   and  paa.assignment_action_id = pai.locking_action_id
2692   and  paa.source_action_id is NULL
2693   and  pai.locked_action_id = paa_run.assignment_action_id
2694   and  paa_run.assignment_id = p_assignment_id
2695   and  paa_run.action_status IN ('C','S') --10225372
2696   and  paa.action_status = 'C';
2697 
2698 
2699 l_proc             CONSTANT VARCHAR2(50):= g_package||'get_termination_date';
2700 l_deceased_flg              VARCHAR2(1);
2701 l_termination_date          DATE;
2702 l_start_date                DATE;
2703 l_end_date                  DATE;
2704 l_asg_end_date              DATE;
2705 l_last_end_date             DATE;
2706 
2707 BEGIN
2708      hr_utility.set_location('Entering ' || l_proc,20);
2709     hr_utility.set_location('Step ' || l_proc,20);
2710     hr_utility.set_location('p_action_context_id  = ' || p_action_context_id,20);
2711     hr_utility.set_location('p_assignment_id      = ' || p_assignment_id,20);
2712     hr_utility.set_location('p_person_id          = ' || p_person_id,20);
2713     hr_utility.set_location('g_paye_ref           = ' || g_paye_ref,20);
2714     hr_utility.set_location('p_termination_date           = ' || p_termination_date,20);
2715 
2716 
2717 
2718   -- get deceased flag, date of leaving
2719   OPEN cur_service_leave;
2720   FETCH cur_service_leave INTO l_deceased_flg,l_termination_date;
2721   CLOSE cur_service_leave;
2722 
2723   -- Copied to out variable (5600150)
2724   p_deceased_flag := l_deceased_flg;
2725 
2726   l_asg_end_date := l_termination_date;
2727   hr_utility.set_location('l_termination_date           = ' || l_termination_date,21);
2728 
2729   /* If employee is not terminated using end employment check for asg end date */
2730   IF l_termination_date IS NULL   THEN
2731   /* Get End Date of Employement with Employer */
2732 	  OPEN cur_max_end_date;
2733 	  FETCH cur_max_end_date INTO l_termination_date;
2734 	  CLOSE cur_max_end_date;
2735   /* Get End Date of Assignment */
2736 	  OPEN cur_get_asg_end_date;
2737 	  FETCH cur_get_asg_end_date INTO l_asg_end_date;
2738 	  CLOSE cur_get_asg_end_date;
2739   END IF;
2740  hr_utility.set_location('l_termination_date           = ' || l_termination_date,22);
2741  p_termination_date := l_termination_date;
2742  OPEN cur_supp_run;
2743   FETCH cur_supp_run INTO l_last_end_date;
2744   hr_utility.set_location('l_last_end_date = '|| l_last_end_date,20);
2745   IF l_last_end_date IS NOT NULL THEN
2746   --IF l_report_type_count >= 1 THEN
2747      p_supp_pymt_date := p_date_earned;
2748      p_supp_flag:= 'Y';
2749      p_termination_date := l_last_end_date;
2750     ELSE
2751      p_supp_flag:= 'N';
2752      p_supp_pymt_date :=null;
2753   END IF;
2754 END get_termination_date;
2755 
2756   ------------------------------------------------------------
2757   -- for bug 5383808, made the p_supp_flag,p_termination_date and
2758   -- p_supp_pymt_date as in parameters.
2759   -- added p_deceased_flag as out variable 5600150
2760   PROCEDURE archive_p45_info(p_action_context_id       IN  NUMBER,
2761                              p_assignment_id           IN  NUMBER,
2762                              p_payroll_id              IN  NUMBER,
2763                              p_date_earned             IN  DATE,
2764                              p_child_run_ass_act_id    IN  NUMBER,
2765 			     p_supp_flag               IN VARCHAR2, -- 5383808
2766 			     p_person_id               IN NUMBER,
2767 			     p_termination_date        in DATE, -- 5383808
2768 			     p_child_pay_action        IN NUMBER,
2769 			     p_supp_pymt_date	       IN DATE,
2770 			     p_deceased_flag           IN VARCHAR2
2771 				     ) -- 5383808
2772   IS
2773   l_action_info_id            NUMBER(15);
2774   l_proc             CONSTANT VARCHAR2(50):= g_package||'archive_p45_info';
2775   l_ovn                       NUMBER;
2776   l_deceased_flg              VARCHAR2(1);
2777   l_termination_date          DATE;
2778   l_period_num                NUMBER;
2779   l_calculation_option        VARCHAR2(15);
2780   l_non_cum_tax               VARCHAR2(20);
2781   l_noncum_ben_operated       VARCHAR2(5);
2782   l_emer_tax_operated         VARCHAR2(1);
2783   l_defined_balance_id        NUMBER;
2784   l_emer_num                  NUMBER;
2785   l_emer_basis_flg            VARCHAR2(1);
2786   l_supp_flg                  VARCHAR2(1);
2787   l_report_type_count         NUMBER;
2788   l_supp_pymt_date            DATE;
2789   l_number_per_fiscal_year    NUMBER;
2790   l_periods_per_period        NUMBER;
2791   l_start_date                DATE;
2792   l_end_date                  DATE;
2793   l_p45_period_num            NUMBER;
2794   -- Bug 2943335
2795   l_balance_name              varchar2(80);
2796   l_soc_ben_defined_bal_id     NUMBER;
2797   l_disability_ben_amount      NUMBER;
2798   l_soc_ben_amount             NUMBER;
2799   l_asg_end_date               DATE;
2800   l_last_end_date              DATE;
2801 
2802   -- variable used to fetch Tax Basis 5128377
2803   l_tax_basis                 VARCHAR2(20);
2804   --
2805   CURSOR cur_service_leave IS
2806   select decode(ppos.leaving_reason, 'D','Y','N'),
2807         ppos.actual_termination_date
2808   from  per_periods_of_service ppos
2809   where ppos.person_id = p_person_id
2810   and   ppos.period_of_service_id = (select max(paf.period_of_service_id)
2811                                         from per_all_assignments_f paf,
2812                                              pay_assignment_actions paa,
2813   					               pay_action_interlocks pai
2814   	                               where   pai.locking_action_id = p_action_context_id
2815   				                 and pai.locked_action_id  = paa.assignment_action_id
2816                                          and paa.action_status IN ('C','S') --10225372
2817                                          and paa.assignment_id = paf.assignment_id
2818                                      );
2819   /*
2820      SELECT  decode(ppos.leaving_reason, 'D','Y','N'),
2821              ppos.actual_termination_date
2822       FROM per_periods_of_service ppos,
2823            per_all_assignments_f paf
2824       WHERE  paf.assignment_id = p_assignment_id
2825       AND    ppos.period_of_service_id = paf.period_of_service_id;
2826    */
2827 /* changed the cursor to handle case where 2 user defined assignment status exist mapping to
2828    same per_system_status (5073577) */
2829 CURSOR cur_max_end_date IS
2830 SELECT max(paaf.effective_end_date)
2831 FROM  per_all_assignments_f paaf,
2832       pay_all_payrolls_f papf,
2833       hr_soft_coding_keyflex scl
2834 WHERE paaf.person_id = p_person_id
2835   AND paaf.payroll_id = papf.payroll_id
2836   AND papf.soft_coding_keyflex_id = scl.soft_coding_keyflex_id
2837   AND scl.segment4 = to_char(g_paye_ref)
2838   AND paaf.assignment_status_type_id in
2839 			   (SELECT ast.assignment_status_type_id
2840 			      FROM per_assignment_status_types ast
2841 			     WHERE  ast.per_system_status in ('ACTIVE_ASSIGN','SUSP_ASSIGN')
2842 			   )
2843   AND paaf.effective_end_date between g_archive_start_date and g_archive_end_date;
2844 
2845 /* changed the cursor to handle case where 2 user defined assignment status exist mapping to
2846    same per_system_status (5073577) */
2847 CURSOR cur_get_asg_end_date IS
2848 SELECT max(effective_end_date)
2849 FROM per_all_assignments_f paaf
2850 WHERE paaf.assignment_id = p_assignment_id
2851   AND paaf.assignment_status_type_id in
2852 			   (SELECT ast.assignment_status_type_id
2853 			      FROM per_assignment_status_types ast
2854 			     WHERE  ast.per_system_status in ('ACTIVE_ASSIGN','SUSP_ASSIGN')
2855 			   );
2856 
2857 -- Bug 2943335 used balance name as a parameter
2858   CURSOR cur_defined_balance(l_balance_name IN varchar2 ) IS
2859      SELECT pdb.defined_balance_id
2860       FROM   pay_balance_types pbt,
2861             pay_balance_dimensions pbd,
2862             pay_defined_balances pdb
2863       WHERE  pdb.balance_type_id = pbt.balance_type_id
2864         AND  pdb.balance_dimension_id = pbd.balance_dimension_id
2865         AND  UPPER(pbt.balance_name) = UPPER(l_balance_name)
2866         AND  pbd.database_item_suffix = '_PER_PAYE_REF_YTD';
2867 -- Bug
2868   cursor cur_period_num is
2869      select  ptp.period_num,
2870              ptpt.number_per_fiscal_year,
2871              ptpr.periods_per_period,
2872              ptp.start_date,
2873              ptp.end_date
2874       from   per_all_assignments_f paf,
2875              per_time_periods ptp,
2876              per_time_period_types ptpt,
2877              per_time_period_rules ptpr
2878      where   paf.assignment_id = p_assignment_id
2879        and   p_date_earned between paf.effective_start_date
2880                                and paf.effective_end_date
2881        and   paf.payroll_id = ptp.payroll_id
2882        and   p_date_earned between ptp.start_date and ptp.end_date
2883        and   ptp.period_type = ptpt.period_type
2884        and   ptpt.number_per_fiscal_year = ptpr.number_per_fiscal_year;
2885 
2886   --Bug:2450336
2887   -- Bug 2943335 commented unwanted cursor
2888  /* cursor cur_cal_option is
2889      select calculation_option
2890       from pay_ie_social_benefits_f psb
2891      where psb.assignment_id = p_assignment_id; */
2892   -- Bug 4315023 Removed Pay_element_types_f join for performance
2893   cursor cur_non_cum_tax is
2894      select result_value
2895       from  pay_run_result_values   prr,
2896             pay_run_results         pr,
2897             pay_input_values_f      piv,
2898             pay_assignment_actions  pas,
2899             pay_payroll_actions ppa
2900       where pas.assignment_id in (select assignment_id
2901                                     from per_all_assignments_f
2902                                     where person_id = p_person_id)
2903        and  pas.tax_unit_id  = g_paye_ref
2904        and  pas.payroll_action_id = ppa.payroll_action_id
2905        and  to_char(ppa.effective_date,'YYYY') = to_char(p_date_earned,'YYYY')
2906        and  pr.assignment_action_id   =   pas.assignment_action_id
2907        and  pr.run_result_id          =   prr.run_result_id
2908        and  prr.input_value_id        =   piv.input_value_id
2909        and  pr.element_type_id         =   piv.element_type_id
2910        and  piv.input_value_id        =   g_tax_basis_id
2911        and  piv.business_group_id     IS NULL
2912        and  piv.legislation_code      =  'IE'
2913        and  result_value not in ('IE_CUMULATIVE', 'C','IE_EXEMPTION');
2914  /*
2915  cursor cur_supp_run is
2916    select count(*)
2917    from pay_action_information pai,
2918         pay_assignment_Actions paa
2919    where paa.assignment_action_id = pai.action_context_id
2920      and pai.action_context_type = 'AAP'
2921      and pai.action_information_category = 'IE P45 INFORMATION'
2922      and paa.tax_unit_id = g_paye_ref
2923      and paa.assignment_id in (     select assignment_id
2924                                     from per_all_assignments_f
2925 				    where person_id = p_person_id
2926                                )
2927      and to_date(pai.action_information3) = l_termination_date;
2928     -- and fnd_date.canonical_to_date(pai.action_information3) = l_termination_date;
2929  */
2930 
2931 /* cursor cur_supp_run is
2932 select act_inf.action_information3
2933  from  pay_assignment_actions paa_run,
2934        pay_action_interlocks pai,
2935        pay_assignment_actions paa,
2936        pay_payroll_actions ppa,
2937        pay_action_information act_inf
2938  where ppa.payroll_action_id = paa.payroll_action_id
2939   and  ppa.report_type = 'P45'
2940   and  ppa.report_qualifier = 'IE'
2941   and  ppa.action_type = 'X'
2942   and  paa.assignment_action_id = act_inf.action_context_id
2943   and  act_inf.action_information_category = 'IE P45 INFORMATION'
2944   and  act_inf.action_context_type = 'AAP'
2945   and  ppa.payroll_action_id <> g_archive_pact
2946   and  paa.assignment_action_id = pai.locking_action_id
2947   and  paa.source_action_id is NULL
2948   and  pai.locked_action_id = paa_run.assignment_action_id
2949   and  paa_run.assignment_id = p_assignment_id
2950   and  paa_run.action_status = 'C'
2951   and  paa.action_status = 'C'; */
2952 
2953 -- Commented as Tax Basis is now fetched from Run Results (5128377)
2954 /*Bug 4050372 */
2955 /*
2956  cursor cur_tax_basis(l_termination_date date)
2957   is
2958   select 'N'
2959     from  pay_ie_paye_details_f
2960     where assignment_id=p_assignment_id
2961     and   l_termination_date between effective_start_date and effective_end_date
2962     and   tax_basis not in ('IE_EMERGENCY','IE_EMERGENCY_NO_PPS');
2963 */
2964 -- bug 13359530
2965 cursor cur_next_of_kin_details(l_termination_date date) is
2966 	SELECT DISTINCT pa.ADDRESS_LINE1 address_line1,pa.ADDRESS_LINE2 address_line2,
2967 		 pa.ADDRESS_LINE3 address_line3,paf2.LAST_NAME last_name
2968                      FROM per_contact_relationships con,
2969 				  PER_ALL_PEOPLE_F paf1,
2970 				  PER_ALL_PEOPLE_F paf2,
2971 				  fnd_lookup_values c ,
2972 				  per_addresses pa ,
2973 				  fnd_lookup_values flv
2974 		        WHERE paf1.PERSON_ID = p_person_id
2975 			and l_termination_date between paf1.EFFECTIVE_START_DATE and paf1.EFFECTIVE_END_DATE
2976 			and con.PERSON_ID=paf1.PERSON_ID  and  con.BUSINESS_GROUP_ID =paf1.BUSINESS_GROUP_ID
2977 			and l_termination_date between con.DATE_START and  nvl(con.DATE_END, l_termination_date)
2978 			AND     con.contact_type = c.lookup_code
2979 			and  c.lookup_type = 'CONTACT'
2980 			and c.lookup_code = 'IE_KIN'
2981 			and c.LANGUAGE = 'US'
2982 			AND  con.CONTACT_PERSON_ID = paf2.PERSON_ID
2983 			AND paf2.PERSON_ID = pa.PERSON_ID   AND pa.style(+) LIKE 'IE%'
2984 			AND pa.primary_flag(+) = 'Y'
2985 			AND flv.lookup_type(+) = 'IE_COUNTY'
2986 			AND flv.language(+) = 'US'
2987 			AND flv.lookup_code(+) = pa.region_1
2988 			and l_termination_date  between pa.date_from(+) and nvl(pa.date_to, l_termination_date);
2989 
2990 cur_next_of_kin_details_rec cur_next_of_kin_details%rowtype;
2991   BEGIN
2992   --
2993      hr_utility.set_location('Entering ' || l_proc,20);
2994     hr_utility.set_location('Step ' || l_proc,20);
2995     hr_utility.set_location('p_action_context_id  = ' || p_action_context_id,20);
2996     hr_utility.set_location('p_payroll_id      = ' || p_payroll_id,20);
2997     hr_utility.set_location('p_assignment_id      = ' || p_assignment_id,20);
2998     hr_utility.set_location('p_person_id          = ' || p_person_id,20);
2999     hr_utility.set_location('g_paye_ref           = ' || g_paye_ref,20);
3000     hr_utility.set_location('p_date_earned      = ' || p_date_earned,20);
3001     hr_utility.set_location('p_child_run_ass_act_id      = ' || p_child_run_ass_act_id,20);
3002 
3003 
3004    -- get deceased flag, date of leaving
3005   /* OPEN cur_service_leave;
3006      FETCH cur_service_leave INTO l_deceased_flg,l_termination_date;
3007      CLOSE cur_service_leave;
3008 
3009   l_asg_end_date := l_termination_date;
3010 
3011   -- If employee is not terminated using end employment check for asg end date
3012   IF l_termination_date IS NULL   THEN
3013   -- Get End Date of Employement with Employer
3014 	  OPEN cur_max_end_date;
3015 	  FETCH cur_max_end_date INTO l_termination_date;
3016 	  CLOSE cur_max_end_date;
3017 -- Get End Date of Assignment
3018 	  OPEN cur_get_asg_end_date;
3019 	  FETCH cur_get_asg_end_date INTO l_asg_end_date;
3020 	  CLOSE cur_get_asg_end_date;
3021   END IF;
3022  p_termination_date := l_termination_date;*/
3023   -- check whether this is a supplementary run
3024   -- and get the payment date of supplementary run
3025   /*OPEN cur_supp_run;
3026   FETCH cur_supp_run INTO l_last_end_date;
3027   hr_utility.set_location('l_last_end_date = '|| l_last_end_date,20);
3028   IF l_last_end_date IS NOT NULL THEN
3029   --IF l_report_type_count >= 1 THEN
3030      l_supp_flg :='Y';
3031      l_supp_pymt_date := p_date_earned;
3032      p_supp_flag:= 'Y';
3033      p_termination_date := l_last_end_date;
3034      l_termination_date := l_last_end_date;
3035   ELSE
3036      l_supp_flg :='N';
3037      p_supp_flag:= 'N';
3038      l_supp_pymt_date :=null;
3039   END IF;*/
3040 
3041   l_supp_flg := p_supp_flag;
3042   l_supp_pymt_date := p_supp_pymt_date;
3043   l_termination_date := p_termination_date;
3044   hr_utility.set_location('supplementary flag = '||l_supp_flg,20);
3045   hr_utility.set_location('supplementary date = '||l_supp_pymt_date,20);
3046   --
3047   --
3048   -- get pay_period_number
3049   OPEN cur_period_num;
3050   FETCH cur_period_num INTO l_period_num, l_number_per_fiscal_year, l_periods_per_period, l_start_date, l_end_date;
3051   CLOSE cur_period_num;
3052   hr_utility.set_location('period number = '||l_period_num,20);
3053   hr_utility.set_location('number per fiscal year : '||l_number_per_fiscal_year,20);
3054   hr_utility.set_location('periods per period : '||l_periods_per_period,20);
3055   hr_utility.set_location('start date : '||l_start_date,20);
3056   hr_utility.set_location('end date : '||l_end_date,20);
3057   --
3058   If l_periods_per_period = 1 then
3059      l_p45_period_num := l_period_num;
3060   Elsif l_asg_end_date between l_start_date and l_end_date then
3061      If l_number_per_fiscal_year in (13,26,52) then
3062         l_p45_period_num := (((l_period_num - 1) * l_periods_per_period) + (ceil(((l_asg_end_date) - (l_start_date))/7)));
3063      Else
3064         l_p45_period_num := (((l_period_num - 1) * l_periods_per_period) + (ceil(months_between((l_asg_end_date),(l_start_date)))));
3065      End If;
3066   Else
3067        l_p45_period_num := (l_period_num * l_periods_per_period);
3068   End If;
3069   --
3070   -- get emergency_tax_operated_flg
3071   -- Bug 2943335 passed 'IE EMERGENCY PERIOD' as a parameter to cursor which was earlier hardcoded
3072   /* OPEN cur_defined_balance('IE EMERGENCY PERIOD');
3073    FETCH cur_defined_balance INTO l_defined_balance_id;
3074    CLOSE cur_defined_balance;
3075    hr_utility.set_location('defined balance = '||l_defined_balance_id,20);
3076    l_emer_num := pay_balance_pkg.get_value(p_defined_balance_id => l_defined_balance_id
3077                                           ,p_assignment_action_id => p_child_run_ass_act_id);
3078    IF l_emer_num >0 THEN
3079      l_emer_basis_flg :='Y';
3080    ELSE
3081      l_emer_basis_flg :='N';
3082    END IF;*/
3083 /*Bug 4050372*/
3084    l_emer_basis_flg := 'Y';
3085 
3086 -- Emergency Flag is now fetched from Run Results 5128377
3087 -- for bug 5386432, call only if p_child_run_ass_act_id is not null
3088 IF p_child_run_ass_act_id IS NOT NULL THEN
3089     l_tax_basis := pay_ie_archive_detail_pkg.get_tax_details (
3090                                 p_run_assignment_action_id => p_child_run_ass_act_id
3091                                ,p_input_value_id           => g_tax_basis_id
3092                                ,p_date_earned              => to_char(p_date_earned, 'yyyy/mm/dd'));
3093 END IF;
3094 
3095 /*
3096    OPEN cur_tax_basis(l_asg_end_date);
3097    FETCH cur_tax_basis INTO l_emer_basis_flg;
3098    CLOSE cur_tax_basis;
3099 */
3100 IF l_tax_basis NOT IN('IE_EMERGENCY','IE_EMERGENCY_NO_PPS') THEN
3101 	l_emer_basis_flg := 'N';
3102 END IF;
3103 
3104    hr_utility.set_location('emergency basis = '||l_emer_basis_flg,20);
3105    --
3106   -- get non_cumulative_operated for benefits
3107 -- Bug 2943335 Commented out code below
3108   /* OPEN cur_cal_option;
3109    FETCH cur_cal_option INTO l_calculation_option;
3110    hr_utility.set_location('l_calculation_option'||l_calculation_option,20);
3111    --Bug:2450336
3112     IF l_calculation_option IN  ('IE_OPTION3','IE_OPTION4') THEN
3113        l_noncum_ben_operated := 'Y';
3114     ELSIF l_calculation_option  IN  ('IE_OPTION1','IE_OPTION2') THEN */
3115 -- Bug 2943335
3116   --
3117   -- get Social benefits amount paid
3118   -- From 1-jan-2006 noncumulative value does not depend upon social benefit
3119 -- amount. Bug 5519933. Removed the check.
3120 
3121    /*OPEN cur_defined_balance('IE Taxable Social Benefit');
3122    FETCH cur_defined_balance INTO l_soc_ben_defined_bal_id;
3123    CLOSE cur_defined_balance;
3124    hr_utility.set_location('defined balance id = '||l_soc_ben_defined_bal_id,25);
3125 
3126 -- bug 5386432, call only if p_child_run_ass_act_id is not null
3127    IF p_child_run_ass_act_id IS NOT NULL THEN
3128 	l_soc_ben_amount := pay_balance_pkg.get_value(l_soc_ben_defined_bal_id,
3129                                                  p_child_run_ass_act_id
3130                                                 ,g_paye_ref
3131                                                 ,null
3132                                                 ,null
3133                                                 ,null
3134                                                 ,null
3135                                                 ,null
3136                                                 );
3137    end if;
3138    hr_utility.set_location('benefit amount = '||l_soc_ben_amount,25);
3139 -- Check this to identify if employee was ever paid social benefit amount
3140 -- Replacing fix for Bug:2450336
3141 IF ( l_soc_ben_amount <> 0) THEN */
3142 -- end of changes for Bug 2943335
3143 -- commented for Bug 5519933.
3144 -- If Employee is terminated and rehired in same tax year tax details of previous termination are
3145 -- reported in this termination against non cumulative flag
3146  /*
3147       OPEN cur_non_cum_tax;
3148       FETCH cur_non_cum_tax INTO l_non_cum_tax;
3149       hr_utility.set_location('l_non_cum_tax'||l_non_cum_tax,20);
3150 
3151       IF cur_non_cum_tax%FOUND THEN
3152         l_noncum_ben_operated := 'true';
3153       ELSE
3154         l_noncum_ben_operated := 'false';
3155       END IF;
3156       CLOSE cur_non_cum_tax;
3157 */
3158 l_noncum_ben_operated := 'false';
3159 IF l_tax_basis NOT IN('IE_CUMULATIVE', 'C','IE_EXEMPTION','IE_EMERGENCY','IE_EMERGENCY_NO_PPS') THEN /* 9189002  */
3160 	l_noncum_ben_operated := 'true';
3161 END IF;
3162 
3163 /*ELSE
3164       l_noncum_ben_operated := 'false';
3165 END IF;*/
3166 
3167   -- CLOSE cur_cal_option;
3168   hr_utility.set_location('non cum basis operated = '||l_noncum_ben_operated,20);
3169   open cur_next_of_kin_details(l_termination_date);
3170   fetch cur_next_of_kin_details into cur_next_of_kin_details_rec;
3171   close cur_next_of_kin_details;
3172    --
3173    -- archive the details
3174     pay_action_information_api.create_action_information (
3175          p_action_information_id        =>  l_action_info_id
3176        , p_action_context_id            =>  p_action_context_id
3177        , p_action_context_type          =>  'AAP'
3178        , p_object_version_number        =>  l_ovn
3179        , p_effective_date               =>  g_archive_effective_date
3180        , p_source_id                    =>  p_child_pay_action
3181        , p_source_text                  =>  NULL
3182        , p_action_information_category  =>  'IE P45 INFORMATION'
3183        , p_action_information1          =>  p_deceased_flag
3184        , p_action_information2          =>  l_supp_flg
3185        , p_action_information3          =>  l_termination_date
3186        , p_action_information4          =>  l_emer_basis_flg
3187        , p_action_information5          =>  l_p45_period_num
3188        , p_action_information6          =>  l_noncum_ben_operated
3189        , p_action_information7          =>  l_supp_pymt_date
3190        , p_action_information8          =>  p_person_id
3191        , p_action_information9          =>  p_date_earned --7291676
3192 	 , p_action_information10          =>  cur_next_of_kin_details_rec.address_line1
3193  	 , p_action_information11          =>  cur_next_of_kin_details_rec.address_line2
3194 	 , p_action_information12          =>  cur_next_of_kin_details_rec.address_line3
3195         , p_action_information13          =>  null
3196  	  , p_action_information14          =>  cur_next_of_kin_details_rec.last_name
3197       );
3198   --
3199   hr_utility.set_location('Leaving '||l_proc,20);
3200   END archive_p45_info;
3201  ---------------------------------------------------------------------------------
3202   --Moved the archiving Payroll Action Level Info archivng part from range_cursor 4468864
3203   PROCEDURE archive_deinit(p_payroll_action_id IN NUMBER) IS
3204   l_proc    CONSTANT VARCHAR2(50):= g_package||'archive_deinit';
3205     -- vars for constructing the sqlstr
3206     l_range_cursor              VARCHAR2(4000) := NULL;
3207     l_parameter_match           VARCHAR2(500)  := NULL;
3208     l_ovn                       NUMBER(15);
3209     l_request_id                NUMBER;
3210     l_action_info_id            NUMBER(15);
3211     l_business_group_id         NUMBER;
3212     g_tax_dis_ref               varchar2(10);
3213   CURSOR csr_check_archived(p_pact_id NUMBER) IS
3214   SELECT 1
3215   FROM   DUAL
3216   WHERE EXISTS (SELECT NULL
3217   		FROM pay_action_information pai
3218   		WHERE pai.action_context_id = p_pact_id
3219   		AND   pai.action_context_type = 'PA'
3220   		AND   rownum = 1
3221   	       );
3222     CURSOR csr_input_value_id(p_element_name CHAR,
3223                               p_value_name   CHAR) IS
3224     SELECT pet.element_type_id,
3225            piv.input_value_id
3226     FROM   pay_input_values_f piv,
3227            pay_element_types_f pet
3228     WHERE  piv.element_type_id = pet.element_type_id
3229     AND    pet.legislation_code = 'IE'
3230     AND    pet.element_name = p_element_name
3231     AND    piv.name = p_value_name;
3232 -- Archive all the prepayments information locked by the P45 4468864
3233     CURSOR csr_payroll_info(p_pact_id NUMBER,
3234                        --     p_payroll_id       NUMBER,
3235                        --     p_consolidation_id NUMBER,
3236                             p_start_date       DATE,
3237                             p_end_date         DATE,
3238 	--		    g_tax_dis_ref      VARCHAR2,
3239 	                    g_paye_ref         NUMBER) IS
3240     SELECT pact.payroll_action_id payroll_action_id,
3241            pact.effective_date effective_date,
3242            pact.date_earned date_earned,
3243            pact.payroll_id payroll_id,
3244            org.org_information1 tax_details_ref_no,
3245            org.org_information2 employer_paye_ref_no,
3246            hrl.address_line_1 employer_tax_addr1,
3247            hrl.address_line_2 employer_tax_addr2,
3248            hrl.address_line_3 employer_tax_addr3,
3249            hrl.telephone_number_1 employer_tax_ref_phone
3250            --
3251     FROM   pay_all_payrolls_f ppf,
3252            pay_payroll_actions pact,
3253            hr_organization_information org,
3254 	   hr_soft_coding_keyflex flex,
3255 	   hr_organization_units hou,
3256 	   hr_locations_all hrl
3257     WHERE  org.org_information_context = 'IE_EMPLOYER_INFO' -- for migration changes 4369280
3258     AND    ppf.business_group_id = hou.business_group_id
3259     AND    org.organization_id   = hou.organization_id
3260     AND    hou.location_id       = hrl.location_id(+)
3261         /*
3262            org.org_information_context = 'IE_ORG_INFORMATION'
3263     AND    ppf.business_group_id = org.organization_id
3264          */
3265     AND    pact.payroll_id = ppf.payroll_id
3266     AND    pact.effective_date BETWEEN
3267                  ppf.effective_start_date AND ppf.effective_end_date
3268 --    AND    pact.payroll_id = NVL(p_payroll_id,pact.payroll_id)
3269 --    AND    ppf.consolidation_set_id = p_consolidation_id
3270     AND    pact.effective_date BETWEEN
3271                  p_start_date AND nvl(p_end_date,to_date('31-12-4712','dd-mm-rrrr'))
3272     AND    (pact.action_type = 'P' OR
3273             pact.action_type = 'U')
3274     AND    pact.action_status = 'C'
3275     --Added for bug fix 3567562, to filter payroll information based on PAYE reference
3276     AND    ppf.soft_coding_keyflex_id = flex.soft_coding_keyflex_id
3277     AND    org.organization_id  = flex.segment4
3278     /*
3279     AND    org.org_information1 = flex.segment1
3280     AND    org.org_information2 = flex.segment3
3281     */
3282 --    AND    org.org_information_id  = g_tax_dis_ref
3283  --   AND    org.org_information2 = g_paye_ref
3284       AND    org.organization_id = g_paye_ref
3285     AND    exists  		   (SELECT NULL
3286   				    FROM   pay_assignment_actions paa,
3287   				    	   pay_action_interlocks pai,
3288   				    	   pay_assignment_actions paa_arc
3289   				    WHERE  pai.locked_action_id = paa.assignment_action_id
3290   				    AND    pai.locking_action_id = paa_arc.assignment_action_id
3291   				    AND    paa_arc.payroll_action_id = p_pact_id
3292   				    AND    paa.payroll_action_id  = pact.payroll_action_id
3293   				   );
3294   l_check_payroll_info VARCHAR2(1):='N';
3295   -- Cursor csr_get_org_tax_address
3296   CURSOR csr_get_org_tax_address(-- c_consolidation_set PAY_CONSOLIDATION_SETS.CONSOLIDATION_SET_ID%type
3297                        --          ,g_tax_dis_ref varchar2,
3298                                  g_paye_ref    number
3299                                   ) IS
3300   SELECT
3301            hrl.address_line_1        employer_tax_addr1,
3302            hrl.address_line_2        employer_tax_addr2,
3303            hrl.address_line_3        employer_tax_addr3,
3304            org_info.org_information4 employer_tax_contact,
3305            hrl.telephone_number_1    employer_tax_ref_phone,
3306            org_all.name              employer_tax_rep_name,
3307           org_all.business_group_id     business_group_id
3308            --
3309     FROM   hr_all_organization_units   org_all
3310           ,hr_organization_information org_info
3311       --    ,pay_consolidation_sets pcs
3312           ,hr_locations_all hrl
3313     WHERE  /*pcs.consolidation_set_id  = c_consolidation_set
3314     AND    org_all.organization_id   = pcs.business_group_id
3315     AND    org_info.organization_id  = org_all.organization_id
3316     AND    org_info.org_information_context  = 'IE_ORG_INFORMATION'
3317     AND    org_all.business_group_id   = pcs.business_group_id
3318     AND*/    org_info.organization_id  = org_all.organization_id
3319     AND    org_info.org_information_context  = 'IE_EMPLOYER_INFO' --for migration changes 4369280
3320     AND    org_all.location_id = hrl.location_id (+)
3321     --Added new condition for bug fix 3567562 to filter record based on PAYE reference and Tax District Reference
3322 --    AND    org_info.org_information1 = g_tax_dis_ref
3323  --   AND    org_info.org_information2 = g_paye_ref ;
3324       AND    org_info.organization_id = g_paye_ref ;
3325   ---- Cursor csr_check_archive
3326   CURSOR  csr_check_archive( cp_payroll_action_id number
3327                             ,cp_payroll_id        number
3328                             ,cp_effective_date    date) IS
3329     SELECT  DISTINCT paf.organization_id
3330     FROM    per_all_assignments_f paf
3331     WHERE   paf.payroll_id = cp_payroll_id
3332     AND     cp_effective_date between paf.effective_start_date
3333                               AND     paf.effective_end_date
3334     AND     NOT EXISTS (
3335             SELECT  NULL
3336             FROM    pay_action_information pai
3337             WHERE   pai.action_context_id           = cp_payroll_action_id
3338             AND     pai.action_context_type         = 'PA'
3339             AND     pai.action_information_category = 'ADDRESS DETAILS'
3340             AND     pai.action_information1         = paf.organization_id
3341             AND     pai.action_information14        = 'Employer Address');
3342 -- Archive against only those prepayments which are locked by P45 4468864
3343 -- Commented to improve the performance 4771780
3344 /*
3345     CURSOR csr_all_payroll_info(p_pact_id       NUMBER) IS
3346       SELECT pact.payroll_action_id payroll_action_id,
3347              pact.effective_date effective_date
3348       FROM   pay_assignment_actions paa,
3349              pay_action_interlocks pai,
3350              pay_assignment_actions paa_arc,
3351              pay_payroll_actions pact
3352       WHERE  pai.locked_action_id = paa.assignment_action_id
3353       AND    pai.locking_action_id = paa_arc.assignment_action_id
3354       AND    paa_arc.payroll_action_id = p_pact_id
3355       AND    paa.payroll_action_id  = pact.payroll_action_id
3356       AND    (pact.action_type = 'P' OR
3357               pact.action_type = 'U')
3358     AND    pact.action_status = 'C';
3359  */
3360   l_dummy                           NUMBER;
3361   l_assignment_set_id               NUMBER;
3362   l_bg_id                           NUMBER;
3363   l_canonical_end_date              DATE;
3364   l_canonical_start_date            DATE;
3365   l_consolidation_set               NUMBER;
3366   l_end_date                        VARCHAR2(30);
3367   l_legislation_code                VARCHAR2(30) := 'IE';
3368   l_payroll_id                      NUMBER;
3369   l_start_date                      VARCHAR2(30);
3370   l_tax_period_no                   VARCHAR2(30);
3371   l_curr_payroll_id                 NUMBER;
3372   l_error                           varchar2(1) ;
3373   l_archived                        NUMBER;
3374 BEGIN
3375 
3376   hr_utility.set_location('Entering ' || l_proc,10);
3377   l_archived := 0;
3378 -- Check whether assignment action is retried 4468864
3379   OPEN csr_check_archived(p_payroll_action_id);
3380   FETCH csr_check_archived INTO l_archived;
3381   CLOSE csr_check_archived;
3382 IF l_archived = 0 THEN
3383     pay_ie_p45_archive.get_parameters (
3384       p_payroll_action_id => p_payroll_action_id
3385     , p_token_name        => 'EMPLOYER'
3386     , p_token_value       => g_paye_ref);
3387     /*
3388     pay_ie_p45_archive.get_parameters (
3389       p_payroll_action_id => p_payroll_action_id
3390     , p_token_name        => 'CONSOLIDATION'
3391     , p_token_value       => l_consolidation_set);
3392     pay_ie_p45_archive.get_parameters (
3393       p_payroll_action_id => p_payroll_action_id
3394     , p_token_name        => 'ASSIGNMENT_SET'
3395     , p_token_value       => l_assignment_set_id);
3396     pay_ie_p45_archive.get_parameters (
3397       p_payroll_action_id => p_payroll_action_id
3398     , p_token_name        => 'START_DATE'
3399     , p_token_value       => l_start_date);
3400     */
3401     pay_ie_p45_archive.get_parameters (
3402       p_payroll_action_id => p_payroll_action_id
3403     , p_token_name        => 'END_DATE'
3404     , p_token_value       => l_end_date);
3405 
3406  pay_ie_p45_archive.get_parameters (
3407       p_payroll_action_id => p_payroll_action_id
3408     , p_token_name        => 'START_DATE'
3409     , p_token_value       => l_start_date);
3410 
3411     pay_ie_p45_archive.get_parameters (
3412       p_payroll_action_id => p_payroll_action_id
3413     , p_token_name        => 'BG_ID'
3414     , p_token_value       => l_bg_id);
3415     hr_utility.set_location('Step ' || l_proc,20);
3416     l_canonical_start_date := TO_DATE(l_start_date,'yyyy/mm/dd');
3417     l_canonical_end_date   := TO_DATE(l_end_date,'yyyy/mm/dd');
3418 --get_paye_reference (l_consolidation_set,g_paye_ref,l_bg_id,l_canonical_start_date,l_canonical_end_date,l_error);
3419 /*if l_error ='Y' then
3420    NULL;
3421 else
3422 */
3423     FOR tax_info_rec IN csr_get_org_tax_address (g_paye_ref) LOOP
3424     --
3425     pay_action_information_api.create_action_information (
3426       p_action_information_id        => l_action_info_id
3427     , p_action_context_id            => p_payroll_action_id
3428     , p_action_context_type          => 'PA'
3429     , p_object_version_number        => l_ovn
3430     , p_action_information_category  => 'ADDRESS DETAILS'
3431     , p_action_information1          => tax_info_rec.business_group_id
3432     , p_action_information5          => tax_info_rec.employer_tax_addr1
3433     , p_action_information6          => tax_info_rec.employer_tax_addr2
3434     , p_action_information7          => tax_info_rec.employer_tax_addr3
3435     , p_action_information14         => 'IE Employer Tax Address'
3436     , p_action_information26         => tax_info_rec.employer_tax_contact
3437     , p_action_information27         => tax_info_rec.employer_tax_ref_phone
3438     , p_action_information28         => tax_info_rec.employer_tax_rep_name);
3439     --
3440     END LOOP;
3441     l_curr_payroll_id:= -1;
3442     FOR rec_payroll_info in csr_payroll_info(p_payroll_action_id,
3443                                     --         l_payroll_id,
3444                                     --         l_consolidation_set,
3445                                              l_canonical_start_date,
3446                                              l_canonical_end_date,
3447 					--     g_tax_dis_ref,
3448 					     g_paye_ref)
3449     LOOP
3450       -- Cursor csr_check_archive called
3451       OPEN csr_check_archive( p_payroll_action_id
3452                          ,rec_payroll_info.payroll_id
3453                          ,l_canonical_end_date);
3454       FETCH csr_check_archive INTO l_dummy;
3455       IF csr_check_archive%FOUND THEN
3456            pay_emp_action_arch.arch_pay_action_level_data (
3457                           p_payroll_action_id => p_payroll_action_id
3458                         , p_payroll_id        => rec_payroll_info.payroll_id
3459                         , p_effective_date    => l_canonical_end_date);
3460       END IF;
3461       CLOSE csr_check_archive;
3462            hr_utility.set_location('rec_payroll_info.payroll_action_id   = ' || rec_payroll_info.payroll_action_id,30);
3463            hr_utility.set_location('rec_payroll_info.tax_details_ref     = ' || rec_payroll_info.tax_details_ref_no,30);
3464            hr_utility.set_location('rec_payroll_info.employers_paye_ref_no    = ' || rec_payroll_info.employer_paye_ref_no,30);
3465          hr_utility.set_location('Archiving EMEA PAYROLL INFO',30);
3466          pay_action_information_api.create_action_information (
3467            p_action_information_id        =>  l_action_info_id
3468          , p_action_context_id            =>  p_payroll_action_id
3469          , p_action_context_type          =>  'PA'
3470          , p_object_version_number        =>  l_ovn
3471          , p_effective_date               =>  rec_payroll_info.effective_date
3472          , p_source_id                    =>  NULL
3473          , p_source_text                  =>  NULL
3474          , p_action_information_category  =>  'EMEA PAYROLL INFO'
3475          , p_action_information1          =>  rec_payroll_info.payroll_action_id
3476          , p_action_information2          =>  rec_payroll_info.payroll_id
3477          , p_action_information3          =>  l_consolidation_set
3478          , p_action_information4          =>  rec_payroll_info.tax_details_ref_no
3479          , p_action_information5          =>  rec_payroll_info.employer_tax_ref_phone
3480          , p_action_information6          =>  rec_payroll_info.employer_paye_ref_no
3481          , p_action_information8          =>  rec_payroll_info.employer_tax_addr1
3482          , p_action_information9          =>  rec_payroll_info.employer_tax_addr2
3483          , p_action_information10         =>  rec_payroll_info.employer_tax_addr3);
3484 /* Coomented to improve the performance 4771780 since the cursor csr_all_payroll_info has high cost.
3485   as the same cursor csr_payroll_info can be used to get the required details */
3486 /*
3487      END LOOP;
3488       -- setup statutory balances pl/sql table
3489       pay_ie_p45_archive.setup_standard_balance_table;
3490       FOR rec_payroll_info in csr_all_payroll_info(p_payroll_action_id)
3491       LOOP
3492  */
3493       hr_utility.trace('Entered payroll info');
3494       pay_balance_pkg.set_context('PAYROLL_ACTION_ID'
3495                                  , rec_payroll_info.payroll_action_id);
3496       pay_ie_p45_archive.setup_balance_definitions(p_payroll_action_id,
3497                                                    rec_payroll_info.payroll_action_id,
3498                                                    rec_payroll_info.effective_date);
3499       END LOOP;
3500 --end if;
3501 END IF;
3502   Exception
3503   when others then
3504    hr_utility.set_location('Leaving via exception section ' || l_proc,40);
3505   END archive_deinit;
3506 
3507  ---------------------------------------------------------------------------------
3508 
3509   PROCEDURE range_cursor (pactid IN NUMBER,
3510                           sqlstr OUT nocopy VARCHAR2)
3511   -- public procedure which archives the payroll information, then returns a
3512   -- varchar2 defining a SQL statement to select all the people that may be
3513   -- eligible for payslip reports.
3514   -- The archiver uses this cursor to split the people into chunks for parallel
3515   -- processing.
3516   IS
3517   --
3518   l_proc    CONSTANT VARCHAR2(50):= g_package||'range_cursor';
3519     -- vars for constructing the sqlstr
3520     l_range_cursor              VARCHAR2(4000) := NULL;
3521     l_parameter_match           VARCHAR2(500)  := NULL;
3522     l_ovn                       NUMBER(15);
3523     l_request_id                NUMBER;
3524     l_action_info_id            NUMBER(15);
3525     l_business_group_id         NUMBER;
3526     g_tax_dis_ref               varchar2(10);
3527   --
3528   l_check_payroll_info VARCHAR2(1):='N';
3529   ---- Cursor csr_check_archive
3530   l_dummy                           NUMBER;
3531   l_assignment_set_id               NUMBER;
3532   l_bg_id                           NUMBER;
3533   l_canonical_end_date              DATE;
3534   l_canonical_start_date            DATE;
3535   l_consolidation_set               NUMBER;
3536   l_end_date                        VARCHAR2(30);
3537   l_legislation_code                VARCHAR2(30) := 'IE';
3538   l_payroll_id                      NUMBER;
3539   l_start_date                      VARCHAR2(30);
3540   l_tax_period_no                   VARCHAR2(30);
3541   l_curr_payroll_id                 NUMBER;
3542   l_error                           varchar2(1) ;
3543   l_employer                        NUMBER;
3544 
3545   BEGIN
3546   -- hr_utility.trace_on(null,'P45');
3547     hr_utility.set_location('Entering ' || l_proc,10);
3548    /*
3549     pay_ie_p45_archive.get_parameters (
3550       p_payroll_action_id => pactid
3551     , p_token_name        => 'PAYROLL'
3552     , p_token_value       => l_payroll_id);
3553     pay_ie_p45_archive.get_parameters (
3554       p_payroll_action_id => pactid
3555     , p_token_name        => 'CONSOLIDATION'
3556     , p_token_value       => l_consolidation_set);
3557     pay_ie_p45_archive.get_parameters (
3558       p_payroll_action_id => pactid
3559     , p_token_name        => 'ASSIGNMENT_SET'
3560     , p_token_value       => l_assignment_set_id);
3561     pay_ie_p45_archive.get_parameters (
3562       p_payroll_action_id => pactid
3563     , p_token_name        => 'START_DATE'
3564     , p_token_value       => l_start_date);
3565     */
3566     pay_ie_p45_archive.get_parameters (
3567       p_payroll_action_id => pactid
3568     , p_token_name        => 'END_DATE'
3569     , p_token_value       => l_end_date);
3570 
3571     pay_ie_p45_archive.get_parameters (
3572       p_payroll_action_id => pactid
3573     , p_token_name        => 'BG_ID'
3574     , p_token_value       => l_bg_id);
3575 
3576     pay_ie_p45_archive.get_parameters (
3577       p_payroll_action_id => pactid
3578     , p_token_name        => 'EMPLOYER'
3579     , p_token_value       => l_employer);
3580 
3581     pay_ie_p45_archive.get_parameters (
3582       p_payroll_action_id => pactid
3583     , p_token_name        => 'START_DATE'
3584     , p_token_value       => l_start_date);
3585 
3586     hr_utility.set_location('Step ' || l_proc,20);
3587     --hr_utility.set_location('l_payroll_id = ' || l_payroll_id,20);
3588     --hr_utility.set_location('l_start_date = ' || l_start_date,20);
3589     --hr_utility.set_location('l_end_date   = ' || l_end_date,20);
3590     --hr_utility.set_location('l_payroll_id = ' || l_payroll_id,20);
3591     --hr_utility.set_location('l_start_date = ' || l_start_date,20);
3592  --   l_canonical_start_date := TO_DATE(l_start_date,'yyyy/mm/dd');
3593  --   l_canonical_end_date   := TO_DATE(l_end_date,'yyyy/mm/dd');
3594      --archive EMEA PAYROLL INFO for each prepayment run identified
3595     --hr_utility.set_location('l_payroll_id           = ' || l_payroll_id,20);
3596     --hr_utility.set_location('l_consolidation_set    = ' || l_consolidation_set,20);
3597     -- hr_utility.set_location('l_canonical_start_date = ' || l_canonical_start_date,20);
3598     -- hr_utility.set_location('l_canonical_end_date   = ' || l_canonical_end_date,20);
3599 --Call made to procedure get_paye_referene to get the PAYE reference attributed at payroll level,added for bug fix 3567562.
3600 --get_paye_reference (l_consolidation_set,g_paye_ref,l_bg_id,l_canonical_start_date,l_canonical_end_date,l_error);
3601 --Added for bug fix 3567562
3602 /*
3603 if l_error ='Y' then
3604 	sqlstr := 'SELECT 1 FROM dual WHERE to_char(:payroll_action_id) = dummy';
3605 else
3606 */
3607     sqlstr := 'SELECT DISTINCT person_id
3608                FROM   per_people_f ppf,
3609                       pay_payroll_actions ppa
3610                WHERE  ppa.payroll_action_id = :payroll_action_id
3611                AND    ppa.business_group_id +0= ppf.business_group_id
3612                ORDER BY ppf.person_id';
3613     hr_utility.set_location('Leaving ' || l_proc,40);
3614 --end if;
3615   Exception
3616   when others then
3617    hr_utility.set_location('Leaving via exception section ' || l_proc,40);
3618    sqlstr:='select 1 from dual where to_char(:payroll_action_id) = dummy';
3619   END range_cursor;
3620 
3621   -------------------------------------------------
3622   PROCEDURE action_creation (pactid in number,
3623                              stperson in number,
3624                              endperson in number,
3625                              chunk in number) is
3626   --
3627   CURSOR csr_prepaid_assignments(p_pact_id          NUMBER,
3628                                  stperson           NUMBER,
3629                                  endperson          NUMBER,
3630                                  p_paye_ref         NUMBER,
3631 				 l_payroll_id       NUMBER                     -- 5059862 p45 payroll parameter change
3632                                  ) IS
3633   SELECT as1.person_id person_id,
3634 	 act.assignment_id assignment_id,
3635          act.assignment_action_id run_action_id,
3636          act1.assignment_action_id prepaid_action_id,
3637 	 as1.assignment_number works_number,
3638 	 as1.period_of_service_id period_of_service_id
3639   FROM   --per_periods_of_service ppos,
3640          per_all_assignments_f as1,
3641          pay_assignment_actions act,
3642          pay_payroll_actions appa,
3643          pay_action_interlocks pai,
3644          pay_assignment_actions act1,
3645          pay_payroll_actions appa2
3646   WHERE  /*appa.consolidation_set_id = p_consolidation_id*/
3647          act.tax_unit_id = p_paye_ref
3648   AND    appa.effective_date BETWEEN g_archive_start_date AND g_archive_end_date
3649   AND    as1.person_id BETWEEN stperson AND endperson
3650   /* Assignment End Date should be between archive start date and end date */
3651   AND    as1.effective_end_date between g_archive_start_date AND g_archive_end_date
3652   AND  (as1.effective_end_date = (select max(effective_end_date)
3653                                     from  per_all_assignments_f paf1
3654                                    where paf1.assignment_id = as1.assignment_id
3655 /* changed the cursor to handle case where 2 user defined assignment status exist mapping to
3656    same per_system_status (5073577) */
3657                                      and   paf1.assignment_status_type_id in
3658                                            (SELECT ast.assignment_status_type_id
3659                                               FROM per_assignment_status_types ast
3660   					     WHERE  ast.per_system_status in ('ACTIVE_ASSIGN','SUSP_ASSIGN')
3661   					   )
3662 			         )
3663         AND    as1.effective_end_date <> to_date('31-12-4712','DD-MM-YYYY')
3664        )
3665   AND (as1.payroll_id in (select b.payroll_id                                      -- 5059862
3666                             from per_assignments_f a,per_assignments_f b
3667 			   where a.payroll_id = l_payroll_id
3668 			     and a.person_id = b.person_id
3669 			     and a.period_of_Service_id = b.period_of_Service_id
3670 			     and a.period_of_Service_id = as1.period_of_Service_id  -- 5758951
3671 			     and a.person_id  = as1.person_id
3672                              and a.effective_start_date <= g_archive_end_date
3673                        --      and a.effective_end_date >= trunc(g_archive_end_date,'Y') -- bug 6144761
3674 			     -- 5758951
3675 			     and a.effective_end_date = (select max(effective_end_date)
3676                                                            from  per_all_assignments_f paf1
3677                                                           where paf1.assignment_id = a.assignment_id
3678                                                             and   paf1.assignment_status_type_id in
3679                                            (SELECT ast.assignment_status_type_id
3680                                               FROM per_assignment_status_types ast
3681   					     WHERE  ast.per_system_status in ('ACTIVE_ASSIGN','SUSP_ASSIGN')
3682   					   )
3683 					                 )
3684 			 )
3685        OR l_payroll_id is null)
3686 
3687   --
3688   AND    appa.action_type IN ('R','Q')                             -- Payroll Run or Quickpay Run
3689   AND    act.payroll_action_id = appa.payroll_action_id
3690   AND    act.source_action_id IS NULL
3691   AND    as1.assignment_id = act.assignment_id
3692   AND    act.action_status IN ('C','S') --10225372
3693   AND    act.assignment_action_id = pai.locked_action_id
3694   AND    act1.assignment_action_id = pai.locking_action_id
3695   AND    act1.action_status IN ('C','S')  --10225372
3696   AND    act1.payroll_action_id = appa2.payroll_action_id
3697   AND    appa2.action_type IN ('P','U') -- Prepayments or Quickpay Prepayments
3698   AND    appa2.payroll_action_id = (SELECT /*+ USE_NL(ACT2 APPA4)*/
3699                                         max(appa4.payroll_action_id)
3700                                   FROM  /*pay_pre_payments ppp, --Bug 4193738 --Bug 4468864*/
3701 					pay_assignment_actions act2,
3702                                         pay_payroll_actions appa4
3703                                   WHERE /*ppp.assignment_action_id=act2.assignment_action_id
3704 				  AND*/ act2.assignment_id = act.assignment_id
3705  				  AND   act2.action_status IN ('C','S')  --10225372
3706                                   AND   appa4.payroll_action_id = act2.payroll_action_id
3707                                   AND   appa4.action_type in ('P','U')
3708                                   AND appa4.effective_date BETWEEN g_archive_start_date AND g_archive_end_date)
3709   -- bug 5597735, change the not exists clause.
3710   -- refer bug 5233518 for more details.
3711   AND    NOT EXISTS (SELECT /*+ ORDERED use_nl(appa3)*/ null
3712                       from   pay_assignment_actions act3,
3713                              pay_payroll_actions appa3,
3714                              pay_action_interlocks pai, --bug 4208273
3715                              pay_assignment_actions act2, --bug 4208273
3716                              pay_payroll_actions appa4 --bug 4208273
3717                       where  pai.locked_action_id= act3.assignment_action_id
3718                       and pai.locking_action_id=act2.assignment_action_id
3719         and    act3.action_sequence  >= act1.action_sequence  --bug 4193738
3720         and    act3.assignment_id in (select distinct paaf.assignment_id
3721                                       from  per_all_assignments_f paaf
3722                                       where paaf.person_id = as1.person_id
3723                                      )
3724         and    act3.tax_unit_id = act1.tax_unit_id
3725         and    act3.action_status IN ('C','S')  --10225372
3726         and    act2.action_status = 'C'
3727         and    act3.payroll_action_id=appa4.payroll_action_id
3728         and    appa4.action_type in ('P','U')
3729         and    act2.payroll_action_id = appa3.payroll_action_id
3730                       and    appa3.action_type = 'X'
3731                       and    appa3.report_type = 'P45')
3732    /* check person does not hold employment with the employer between start of year and archive end date */
3733    AND       NOT EXISTS (
3734 				SELECT MIN(paf.effective_start_date),MAX(paf.effective_end_date)
3735 				FROM per_all_assignments_f paf,
3736 				     pay_all_payrolls_f papf,
3737 				     hr_soft_coding_keyflex scl
3738 				WHERE paf.person_id = as1.person_id
3739 				AND paf.payroll_id = papf.payroll_id
3740 /* changed the cursor to handle case where 2 user defined assignment status exist mapping to
3741    same per_system_status (5073577) */
3742 				AND paf.assignment_status_type_id in
3743 		                                           (SELECT ast.assignment_status_type_id
3744                                                               FROM per_assignment_status_types ast
3745   					                     WHERE  ast.per_system_status in ('ACTIVE_ASSIGN','SUSP_ASSIGN')
3746   					                   )
3747 				AND  g_archive_end_date  between papf.effective_start_date and papf.effective_end_date
3748 				AND papf.soft_coding_keyflex_id = scl.soft_coding_keyflex_id
3749 				AND scl.segment4 = to_char(p_paye_ref)
3750 				group by paf.assignment_id
3751 				having min(paf.effective_start_date) <= g_archive_end_date
3752 				and    max(paf.effective_end_date) > g_archive_end_date
3753 			  )
3754   ORDER BY as1.person_id,as1.assignment_number,act.assignment_id
3755   FOR UPDATE OF as1.assignment_id;
3756 
3757   /* 7291676 */
3758 
3759 cursor csr_ppsn_override(p_asg_id number)
3760 is
3761 select aei_information1 PPSN_OVERRIDE
3762 from per_assignment_extra_info
3763 where assignment_id = p_asg_id
3764 and aei_information_category = 'IE_ASG_OVERRIDE';
3765 
3766 l_ppsn_override per_assignment_extra_info.aei_information1%type;
3767 
3768 cursor csr_ppsn_min_asg(p_ppsn_override varchar2, p_person_id number)
3769 is
3770 select MIN(paei.assignment_id) ovrride_asg
3771 from per_assignment_extra_info paei
3772 where paei.information_type = 'IE_ASG_OVERRIDE'
3773 and paei.aei_information1 = p_ppsn_override
3774 and exists
3775 (select 1 from per_all_assignments_f paaf
3776   where paaf.assignment_id = paei.assignment_id
3777   and paaf.person_id  = p_person_id)
3778 GROUP BY paei.aei_information1;
3779 
3780 l_ppsn_override_asg per_assignment_extra_info.assignment_id%type;
3781 l_temp_person_id		per_people_f.person_id%TYPE :=0;
3782 
3783 
3784   l_actid                           NUMBER;
3785   l_canonical_end_date              DATE;
3786   l_canonical_start_date            DATE;
3787   l_consolidation_set               VARCHAR2(30);
3788   l_end_date                        VARCHAR2(20);
3789   l_payroll_id                      NUMBER;
3790   l_prepay_action_id                NUMBER;
3791   l_start_date                      VARCHAR2(20);
3792   l_person_id                       NUMBER;
3793   l_error                           varchar2(1) ;
3794   l_period_of_service_id            NUMBER;
3795   l_bg_id                           NUMBER;
3796  --
3797   l_proc VARCHAR2(50) := g_package||'action_creation';
3798   BEGIN
3799 
3800     --hr_utility.trace_on(null,'P45');
3801     hr_utility.set_location('Entering ' || l_proc,10);
3802     pay_ie_p45_archive.get_parameters (
3803       p_payroll_action_id => pactid
3804     , p_token_name        => 'EMPLOYER'
3805     , p_token_value       => g_paye_ref);
3806 
3807     pay_ie_p45_archive.get_parameters (
3808       p_payroll_action_id => pactid
3809     , p_token_name        => 'END_DATE'
3810     , p_token_value       => l_end_date);
3811 
3812     pay_ie_p45_archive.get_parameters (
3813     p_payroll_action_id => pactid
3814   , p_token_name        => 'BG_ID'
3815   , p_token_value       => l_bg_id);
3816 
3817       pay_ie_p45_archive.get_parameters (
3818       p_payroll_action_id => pactid
3819     , p_token_name        => 'START_DATE'
3820     , p_token_value       => l_start_date);
3821 
3822     pay_ie_p45_archive.get_parameters (                         -- 5059862
3823       p_payroll_action_id => pactid
3824     , p_token_name        => 'PAYROLL'
3825     , p_token_value       => l_payroll_id);
3826 
3827     hr_utility.set_location('Step ' || l_proc,20);
3828     hr_utility.set_location('g_paye_ref = ' || g_paye_ref,20);
3829     hr_utility.set_location('l_end_date   = ' || l_end_date,20);
3830 
3831     l_canonical_start_date := TO_DATE(l_start_date,'yyyy/mm/dd');
3832     l_canonical_end_date   := TO_DATE(l_end_date,'yyyy/mm/dd');
3833     g_archive_start_date   := l_canonical_start_date;
3834     g_archive_end_date     := TO_DATE(l_end_date,'yyyy/mm/dd');
3835 --    l_payroll_id           := TO_NUM(l_payroll_id);
3836 
3837     l_prepay_action_id := 0;
3838     l_person_id := 0;
3839     l_period_of_service_id := 0;
3840 
3841     hr_utility.set_location('l_start_date = ' || l_canonical_start_date,20);
3842 
3843 
3844     --get_paye_reference (l_consolidation_set,g_paye_ref,l_bg_id,l_canonical_start_date,l_canonical_end_date,l_error);
3845     hr_utility.set_location('Before csr_prepaid_assignments',21);
3846 
3847     l_ppsn_override := NULL;
3848     l_ppsn_override_asg := NULL;
3849 
3850     FOR csr_rec IN csr_prepaid_assignments(pactid,
3851                                            stperson,
3852                                            endperson,
3853                                            g_paye_ref,
3854 					   l_payroll_id)
3855     LOOP
3856 
3857    /* 7291676 */
3858     hr_utility.set_location('Person id..'||to_char(csr_rec.person_id),21-1);
3859     hr_utility.set_location('Temp Person id..'||to_char(l_person_id),21-2);
3860 	--
3861 	     /* 7291676QA */
3862 	     l_ppsn_override := NULL;
3863              l_ppsn_override_asg := NULL;
3864 	     hr_utility.set_location('before fetch l_ppsn_override'||to_char(l_ppsn_override),21-3);
3865 	     hr_utility.set_location(' before fetch l_ppsn_override_asg'||to_char(l_ppsn_override_asg),21-3);
3866 
3867             OPEN csr_ppsn_override(csr_rec.assignment_id);
3868             FETCH csr_ppsn_override INTO l_ppsn_override;
3869             CLOSE csr_ppsn_override;
3870 
3871 	hr_utility.set_location('l_ppsn_override'||to_char(l_ppsn_override),21-3);
3872 
3873            IF l_ppsn_override IS NOT NULL THEN
3874 		OPEN csr_ppsn_min_asg(l_ppsn_override,csr_rec.person_id);
3875 	        FETCH csr_ppsn_min_asg INTO l_ppsn_override_asg;
3876 		CLOSE csr_ppsn_min_asg;
3877 		hr_utility.set_location('l_ppsn_override_asg'||to_char(l_ppsn_override_asg),21-4);
3878 	   END IF;
3879 
3880 
3881 
3882 	hr_utility.set_location('csr_rec.assignment_id'||csr_rec.assignment_id,21-4);
3883 
3884        IF (l_person_id <> csr_rec.person_id and l_ppsn_override IS NULL )
3885        OR
3886        (l_ppsn_override_asg=csr_rec.assignment_id and l_ppsn_override IS NOT NULL)
3887        THEN
3888 
3889       hr_utility.set_location('Different Person '|| csr_rec.person_id ,22);
3890 
3891       SELECT pay_assignment_actions_s.NEXTVAL
3892       INTO   l_actid
3893       FROM   dual;
3894 
3895       -- CREATE THE ARCHIVE ASSIGNMENT ACTION FOR THE MASTER ASSIGNMENT ACTION
3896       hr_utility.set_location('ASSIGNMENT ID : ' || csr_rec.assignment_id,23);
3897       hr_utility.trace('ASSIGNMENT ID : ' || csr_rec.assignment_id);
3898 
3899       hr_nonrun_asact.insact(l_actid,csr_rec.assignment_id,pactid,chunk,g_paye_ref);
3900       -- CREATE THE ARCHIVE TO PAYROLL MASTER ASSIGNMENT ACTION INTERLOCK AND
3901       -- THE ARCHIVE TO PREPAYMENT ASSIGNMENT ACTION INTERLOCK
3902       -- hr_utility.set_location('creating lock1 ' || l_actid || ' to ' || csr_rec.run_action_id,20);
3903       -- hr_utility.set_location('creating lock2 ' || l_actid || ' to ' || csr_rec.prepaid_action_id,20);
3904      END IF; --
3905       hr_utility.set_location('l_prepay_action_id : ' || l_prepay_action_id,100);
3906 	hr_utility.set_location('csr_rec.prepaid_action_id : ' || csr_rec.prepaid_action_id,101);
3907 	hr_utility.set_location('l_actid : ' || l_actid,102);
3908 
3909       IF l_prepay_action_id <> csr_rec.prepaid_action_id THEN
3910       hr_utility.set_location('locked id : ' || csr_rec.prepaid_action_id,23);
3911        hr_nonrun_asact.insint(l_actid,csr_rec.prepaid_action_id);
3912       END IF;
3913 
3914       hr_nonrun_asact.insint(l_actid,csr_rec.run_action_id);
3915 
3916       l_prepay_action_id := csr_rec.prepaid_action_id;
3917       l_person_id := csr_rec.person_id;
3918       l_period_of_service_id := csr_rec.period_of_service_id;
3919 
3920     END LOOP;
3921 
3922     hr_utility.set_location('Leaving ' || l_proc,20);
3923   END action_creation;
3924 
3925   ----------------------------
3926   PROCEDURE archive_code (p_assactid       in number,
3927                           p_effective_date in date) IS
3928   CURSOR csr_assignment_actions(p_locking_action_id NUMBER) IS
3929   SELECT pre.locked_action_id      pre_assignment_action_id,
3930          pay.locked_action_id      master_assignment_action_id,
3931          assact.assignment_id      assignment_id,
3932          assact.payroll_action_id  pay_payroll_action_id,
3933          paa.effective_date        effective_date,
3934          ppaa.effective_date       pre_effective_date,
3935          paa.date_earned           date_earned,
3936          ptp.time_period_id        time_period_id
3937   FROM   pay_action_interlocks pre,
3938          pay_action_interlocks pay,
3939          pay_payroll_actions paa,
3940          pay_payroll_actions ppaa,
3941          pay_assignment_actions assact,
3942          pay_assignment_actions passact,
3943          per_time_periods ptp  -- Added to retrieve correct time_period_id 4906850
3944   WHERE  pre.locked_action_id = pay.locking_action_id
3945   AND    pre.locking_action_id = p_locking_action_id
3946   AND    pre.locked_action_id = passact.assignment_action_id
3947   AND    passact.payroll_action_id = ppaa.payroll_action_id
3948   AND    ppaa.action_type IN ('P','U')
3949   AND    pay.locked_action_id = assact.assignment_action_id
3950   AND    assact.payroll_action_id = paa.payroll_action_id
3951   AND    assact.source_action_id IS NULL
3952   AND    ptp.payroll_id = paa.payroll_id
3953   AND    paa.date_earned between ptp.start_date and ptp.end_date
3954   --
3955   ORDER BY pay.locked_action_id DESC;
3956 
3957   -- cursor to retrieve payroll run assignment_action_ids
3958   -- Bug Fix 3817846
3959   -- Changed the cursor cur_child_pay_action
3960   /*CURSOR cur_child_pay_action(p_assignment_id NUMBER,
3961                               p_date_earned   DATE)is
3962   SELECT max(paa.assignment_action_id)
3963   FROM pay_assignment_actions paa,
3964        pay_payroll_actions ppa
3965   where paa.assignment_id = p_assignment_id
3966   AND paa.payroll_action_id = ppa.payroll_action_id
3967   AND ppa.date_earned =p_date_earned
3968   AND ppa.action_type in ('R','Q')
3969   AND paa.action_status = 'C'
3970   AND paa.source_action_id is not null;*/
3971 /*New Cursor to fetch latest child action */
3972 CURSOR cur_child_pay_action (p_person_id IN NUMBER,
3973                              p_effective_date IN DATE,
3974                              p_lat_act_seq IN NUMBER) is
3975 SELECT /*+ USE_NL(paa, ppa) */
3976       fnd_number.canonical_to_number(substr(max(lpad(paa.action_sequence,15,'0')||
3977       paa.assignment_action_id),16))
3978 FROM pay_assignment_actions paa,
3979      pay_payroll_actions ppa
3980 WHERE paa.payroll_action_id = ppa.payroll_action_id
3981   AND paa.assignment_id in (select assignment_id
3982                               from per_all_assignments_f
3983 		             where person_id = p_person_id
3984 			   )
3985   AND paa.tax_unit_id = g_paye_ref
3986   AND  (paa.source_action_id is not null or ppa.action_type in ('I','V','B'))
3987     AND  ppa.effective_date between trunc(p_effective_date,'Y') and p_effective_date
3988   AND  paa.action_sequence > p_lat_act_seq
3989   AND  ppa.action_type in ('R', 'Q', 'I', 'V', 'B')
3990   AND  paa.action_status = 'C';
3991 
3992   -- cursor to find assignment action locked by latest P45 child action
3993   CURSOR cur_get_latest_p45(p_pact_id NUMBER,
3994                             p_person_id NUMBER
3995 			   ) IS
3996  SELECT max(lpad(paa_src.action_sequence,15,'0')|| paa_src.assignment_action_id)
3997     FROM pay_payroll_actions ppa_p45,
3998          pay_assignment_actions p45_src,
3999 	 pay_action_information pai_p45,
4000 	 pay_assignment_actions paa_src
4001     WHERE ppa_p45.action_type = 'X'
4002       AND ppa_p45.report_type = 'P45'
4003       AND ppa_p45.report_qualifier = 'IE'
4004       AND ppa_p45.payroll_action_id <> p_pact_id
4005       AND ppa_p45.payroll_action_id = p45_src.payroll_action_id
4006       AND p45_src.assignment_action_id = pai_p45.action_context_id
4007       AND pai_p45.action_context_type = 'AAP'
4008       AND pai_p45.action_information_category = 'IE P45 INFORMATION'
4009       AND pai_p45.source_id = paa_src.assignment_action_id
4010       AND p45_src.action_status = 'C'
4011       AND paa_src.tax_unit_id = g_paye_ref
4012       AND p45_src.tax_unit_id = g_paye_ref
4013       AND pai_p45.action_information8 = to_char(p_person_id);
4014 
4015  -- Cursor to fetch action context id of P45 for previous period of service.
4016  /* 7291676 */
4017   CURSOR cur_get_last_p45(p_person_id NUMBER,p_termination_date DATE,p_pact NUMBER, c_assignment_id NUMBER) IS
4018   SELECT fnd_number.canonical_to_number(substr(max(lpad(paa.action_sequence,15,'0')||
4019       paa.assignment_action_id),16))
4020   FROM pay_payroll_actions ppa,
4021        pay_assignment_actions paa,
4022        pay_action_information pai
4023   WHERE paa.assignment_action_id = pai.action_context_id
4024    AND  pai.action_information_category = 'IE P45 INFORMATION'
4025    AND  pai.action_context_type = 'AAP'
4026    AND  paa.tax_unit_id = g_paye_ref
4027    AND  pai.action_information3 between trunc(p_termination_date,'Y') and p_termination_date
4028    AND  ppa.payroll_action_id = paa.payroll_action_id
4029    AND  ppa.report_type = 'P45'
4030    AND  ppa.report_category = 'ARCHIVE'
4031    AND  ppa.report_qualifier = 'IE'
4032    AND  ppa.effective_date between trunc(g_archive_end_date,'Y') and g_archive_end_date
4033    AND  paa.payroll_action_id <> p_pact
4034    AND  paa.action_status = 'C'
4035    AND  pai.action_information8 = to_char(p_person_id)
4036   -- AND  paa.assignment_id=c_assignment_id
4037   ; /* knadhan QA */
4038 
4039   -- cursor to fetch Payroll action of Last P45 to pass to get_arc_bal_value 5005788
4040   CURSOR cur_get_p45_pact(p_p45_aact pay_assignment_actions.assignment_action_id%TYPE) IS
4041  SELECT paa.payroll_action_id
4042    FROM pay_assignment_actions paa
4043  WHERE  paa.assignment_action_id = p_p45_aact;
4044 
4045   -- cursor to retrieve payroll id
4046   CURSOR cur_assgn_payroll(p_assignment_id NUMBER,
4047                            p_date_earned DATE) IS
4048   SELECT payroll_id,person_id,period_of_service_id
4049   FROM per_all_assignments_f
4050   WHERE assignment_id = p_assignment_id
4051   AND p_date_earned
4052       BETWEEN effective_start_date AND effective_end_date;
4053 
4054 /* 7291676 */
4055 
4056 cursor csr_ppsn_override(p_asg_id number)
4057 is
4058 select aei_information1 PPSN_OVERRIDE
4059 from per_assignment_extra_info
4060 where assignment_id = p_asg_id
4061 and aei_information_category = 'IE_ASG_OVERRIDE';
4062 
4063 l_ppsn_override per_assignment_extra_info.aei_information1%type;
4064 
4065 CURSOR cur_child_pay_action_ppsn (p_person_id IN NUMBER,
4066                              p_effective_date IN DATE,
4067                              p_lat_act_seq IN NUMBER,
4068 			     c_ppsn_override per_assignment_extra_info.aei_information1%type) is
4069 SELECT /*+ USE_NL(paa, ppa) */
4070       fnd_number.canonical_to_number(substr(max(lpad(paa.action_sequence,15,'0')||
4071       paa.assignment_action_id),16))
4072 FROM pay_assignment_actions paa,
4073      pay_payroll_actions ppa
4074 WHERE paa.payroll_action_id = ppa.payroll_action_id
4075   AND paa.assignment_id in (select paaf.assignment_id
4076                               from per_all_assignments_f paaf, per_assignment_extra_info paei
4077 		             where paaf.person_id = p_person_id
4078 			       and paaf.assignment_id=paei.assignment_id
4079 			       and paei.information_type = 'IE_ASG_OVERRIDE'
4080 			       and paei.aei_information1 = c_ppsn_override     --'314678745T'
4081 			   )
4082   AND paa.tax_unit_id = g_paye_ref
4083   AND  (paa.source_action_id is not null or ppa.action_type in ('I','V','B'))
4084     AND  ppa.effective_date between trunc(p_effective_date,'Y') and p_effective_date
4085   AND  paa.action_sequence > p_lat_act_seq
4086   AND  ppa.action_type in ('R', 'Q', 'I', 'V', 'B')
4087   AND  paa.action_status = 'C';
4088 
4089   l_child_pay_action_ppsn           NUMBER;
4090 
4091   l_actid                           NUMBER;
4092   l_action_context_id               NUMBER;
4093   l_action_info_id                  NUMBER(15);
4094   l_assignment_action_id            NUMBER;
4095   l_business_group_id               NUMBER;
4096   l_chunk_number                    NUMBER;
4097   l_assignment_id                   NUMBER;
4098   l_date_earned                     DATE;
4099   l_ovn                             NUMBER;
4100   l_person_id                       NUMBER;
4101   l_pos_id                          NUMBER;
4102   l_record_count                    NUMBER;
4103   l_salary                          VARCHAR2(10);
4104   l_sequence                        NUMBER;
4105   l_child_pay_action                NUMBER;
4106   l_payroll_id                      NUMBER;
4107   l_supp_flag                       VARCHAR2(1):='N';
4108   l_deceased_flag                   VARCHAR2(1):='N';
4109   l_proc                            VARCHAR2(50) := g_package || 'archive_code';
4110   l_lat_act_seq                     NUMBER;
4111   l_termination_date                DATE;
4112   l_last_p45_action                 NUMBER;
4113  -- l_max_stat_balance                NUMBER       := 19;
4114   l_concat_sequence                 VARCHAR2(40);
4115   l_prev_src_id                     NUMBER;
4116   l_last_p45_pact                   NUMBER;
4117   -- l_max_stat_balance                NUMBER       := 19; 13359530
4118  l_max_stat_balance                NUMBER       := 23;
4119   -- 5386432
4120   l_supp_pymt_date                  DATE;
4121 
4122 
4123 
4124   BEGIN
4125 
4126     l_lat_act_seq := NULL;
4127     hr_utility.set_location('Entering'|| l_proc,10);
4128     hr_utility.set_location('Step '|| l_proc,20);
4129     hr_utility.set_location('p_assactid = ' || p_assactid,20);
4130 
4131     -- retrieve the chunk number for the current assignment action
4132     SELECT paa.chunk_number,paa.assignment_id
4133     INTO   l_chunk_number,l_assignment_id
4134     FROM   pay_assignment_actions paa
4135     WHERE  paa.assignment_action_id = p_assactid;
4136 
4137     l_action_context_id := p_assactid;
4138     l_record_count := 0;
4139 
4140     FOR csr_rec IN csr_assignment_actions(p_assactid)
4141     LOOP
4142       hr_utility.set_location('csr_rec.master_assignment_action_id = ' || csr_rec.master_assignment_action_id,20);
4143       hr_utility.set_location('csr_rec.pre_assignment_action_id    = ' || csr_rec.pre_assignment_action_id,20);
4144       hr_utility.set_location('csr_rec.assignment_id    = ' || csr_rec.assignment_id,20);
4145       hr_utility.set_location('csr_rec.date_earned    = ' ||to_char( csr_rec.date_earned,'dd-mon-yyyy'),20);
4146       hr_utility.set_location('csr_rec.pre_effective_date    = ' ||to_char( csr_rec.pre_effective_date,'dd-mon-yyyy'),20);
4147       hr_utility.set_location('csr_rec.time_period_id    = ' || csr_rec.time_period_id,20);
4148 
4149            OPEN cur_assgn_payroll(csr_rec.assignment_id,csr_rec.date_earned);
4150            FETCH cur_assgn_payroll INTO l_payroll_id,l_person_id,l_pos_id;
4151            CLOSE cur_assgn_payroll;
4152  /* 7291676 */
4153            l_ppsn_override:=null; -- 7291676
4154 	   open csr_ppsn_override(csr_rec.assignment_id);
4155 	   fetch csr_ppsn_override into  l_ppsn_override;
4156 	   close csr_ppsn_override;
4157            hr_utility.set_location('PPSN Override  value  = ' || l_ppsn_override,20);
4158 
4159 
4160       --Fetch the action sequence of latest payroll run child action locked by latest P45
4161       --For the assignment 4468864
4162       OPEN cur_get_latest_p45(g_archive_pact,l_person_id);
4163       FETCH cur_get_latest_p45 INTO l_concat_sequence;
4164 
4165 	      IF cur_get_latest_p45%NOTFOUND THEN
4166 	      hr_utility.set_location('Action Sequence notfound   = ' || l_lat_act_seq,21);
4167 		l_lat_act_seq := 0;
4168 		l_prev_src_id := 0;
4169 	      END IF;
4170 
4171             l_lat_act_seq := nvl(substr(l_concat_sequence,1,15),0);
4172             l_prev_src_id := nvl(substr(l_concat_sequence,16),0);
4173 
4174 	      hr_utility.set_location('Action Sequence  = ' || l_lat_act_seq,21);
4175       CLOSE cur_get_latest_p45;
4176 
4177       hr_utility.set_location('Action Sequence    = ' || l_lat_act_seq,21);
4178 
4179       -- Bug Fix 3817846
4180       -- OPEN cur_child_pay_action(csr_rec.assignment_id, csr_rec.date_earned);
4181       -- Bug Fix 4001524
4182       -- OPEN cur_child_pay_action(csr_rec.assignment_id, csr_rec.effective_date);
4183       /* 7291676 */
4184 
4185       l_child_pay_action_ppsn := NULL;
4186       OPEN cur_child_pay_action_ppsn(l_person_id,g_archive_end_date,l_lat_act_seq,l_ppsn_override);
4187       FETCH cur_child_pay_action_ppsn INTO l_child_pay_action_ppsn;
4188       hr_utility.set_location('Child Action PPSN ='||l_child_pay_action_ppsn,20);
4189       CLOSE cur_child_pay_action_ppsn;
4190 
4191       l_child_pay_action := NULL;
4192       OPEN cur_child_pay_action(l_person_id,g_archive_end_date,l_lat_act_seq);
4193       FETCH cur_child_pay_action INTO l_child_pay_action;
4194 
4195       if (l_child_pay_action_ppsn is null) THEN
4196       l_child_pay_action_ppsn:=l_child_pay_action;
4197       end if;
4198       hr_utility.set_location('Child Action PPSN after assigning ='||l_child_pay_action_ppsn,20);
4199 
4200     --  hr_utility.set_location('Child Action PPSN  ='|| l_child_pay_action_ppsn,24);
4201        hr_utility.set_location('Child Action ='||l_child_pay_action,24);
4202 
4203 	 -------------- Moved here for bug 5386432  ----
4204 	   get_termination_date(p_action_context_id     => p_assactid,
4205                             p_assignment_id           => csr_rec.assignment_id,
4206                             p_person_id               => l_person_id,
4207 				    p_date_earned             =>  csr_rec.effective_date, -- csr_rec.date_earned,  9156332
4208 			          p_termination_date        => l_termination_date,
4209 				    p_supp_pymt_date		=> l_supp_pymt_date,
4210 				    p_supp_flag			=> l_supp_flag,
4211 				    p_deceased_flag             => l_deceased_flag
4212 			          );
4213 	   OPEN cur_get_last_p45(l_person_id,l_termination_date,g_archive_pact,csr_rec.assignment_id);
4214 	   FETCH cur_get_last_p45 into l_last_p45_action;
4215 	   CLOSE cur_get_last_p45;
4216 
4217 	   -- Fetch the Payroll action of Last P45 5005788
4218 	   OPEN cur_get_p45_pact(l_last_p45_action);
4219 	   FETCH cur_get_p45_pact INTO l_last_p45_pact;
4220 	   CLOSE cur_get_p45_pact;
4221 	   hr_utility.set_location(' l_termination_date = '||l_termination_date,30);
4222 	   hr_utility.set_location(' l_supp_pymt_date = '||l_supp_pymt_date,30);
4223 	   hr_utility.set_location(' l_supp_flag = '||l_supp_flag,30);
4224 
4225      ------------------
4226     IF ((l_child_pay_action IS NULL) and l_supp_flag = 'Y' ) THEN
4227      NULL;
4228     ELSE
4229       IF (l_record_count = 0 AND csr_rec.assignment_id = l_assignment_id)
4230       THEN
4231       -- Create child P45 action to lock the child payroll process child action
4232       -- To avoid data corruption 4468864
4233       SELECT pay_assignment_actions_s.NEXTVAL
4234       INTO   l_actid
4235       FROM dual;
4236 
4237       hr_nonrun_asact.insact(
4238         lockingactid => l_actid
4239       , assignid     => l_assignment_id
4240       , pactid       => g_archive_pact
4241       , chunk        => l_chunk_number
4242       , greid        => g_paye_ref
4243       , prepayid     => NULL
4244       , status       => 'C'
4245       , source_act   => p_assactid);
4246 
4247           hr_utility.set_location('creating lock4 ' || l_actid || ' to ' || l_child_pay_action,30);
4248           -- bug 5386432, checks l_child_pay_action is not null, since for zero
4249 	    -- earnigns there will not child actions, so cant lock any
4250 	    IF l_child_pay_action IS NOT NULL THEN
4251 		hr_nonrun_asact.insint(
4252 			lockingactid => l_actid
4253 		    , lockedactid  => l_child_pay_action);
4254 	    END IF;
4255 
4256            pay_ie_p45_archive.archive_p45_info(
4257                     p_action_context_id    => p_assactid,
4258                     p_assignment_id        => csr_rec.assignment_id, -- assignment_id
4259                     p_payroll_id           => l_payroll_id,
4260                     p_date_earned          => csr_rec.date_earned,
4261                     p_child_run_ass_act_id => l_child_pay_action,
4262                     p_supp_flag            => l_supp_flag,
4263 		        p_person_id            => l_person_id,
4264 		        p_termination_date     => l_termination_date,
4265 		        p_child_pay_action     => l_child_pay_action_ppsn,   -- child payroll assignment action id
4266 			p_supp_pymt_date	 => l_supp_pymt_date,
4267 			p_deceased_flag        => l_deceased_flag);
4268 
4269 	   -- Moved this above as we will now have to archive for Main P45 even if
4270 	   -- it has no run-results. bug 5386432
4271 	   /*open cur_get_last_p45(l_person_id,l_termination_date,g_archive_pact);
4272 	   fetch cur_get_last_p45 into l_last_p45_action;
4273 	   close cur_get_last_p45;
4274 
4275 	   -- Fetch the Payroll action of Last P45 5005788
4276 	   OPEN cur_get_p45_pact(l_last_p45_action);
4277 	   FETCH cur_get_p45_pact INTO l_last_p45_pact;
4278 	   CLOSE cur_get_p45_pact; */
4279 
4280 
4281 
4282 	   hr_utility.set_location('sg Person Id ='||l_person_id,32);
4283 	   hr_utility.set_location('sg Termination Date ='||l_termination_date,33);
4284            hr_utility.set_location('sg Payroll action ='||g_archive_pact,34);
4285             hr_utility.set_location('sg P45 action ='||l_last_p45_action,35);
4286 
4287 	   IF l_last_p45_action IS NOT NULL THEN
4288 		hr_nonrun_asact.insint(
4289             lockingactid => l_actid
4290           , lockedactid  => l_last_p45_action);
4291 	   END IF;
4292 
4293            pay_ie_p45_archive.archive_employee_details(
4294                     p_assactid             => l_action_context_id -- P45 master action
4295                   , p_assignment_id        => l_assignment_id
4296                   , p_curr_pymt_ass_act_id => csr_rec.pre_assignment_action_id  -- prepayment assignment_action_id
4297                   , p_date_earned          => csr_rec.date_earned               -- payroll date_earned
4298                   , p_payroll_child_actid  => l_child_pay_action_ppsn                -- payroll assignment action id ( 7291676)
4299                   , p_curr_pymt_eff_date   => csr_rec.pre_effective_date        -- prepayment effective_date
4300                   , p_time_period_id       => csr_rec.time_period_id            -- payroll time_period_id
4301                   , p_record_count         => l_record_count
4302                   , p_supp_flag             => l_supp_flag
4303                   , p_person_id            => l_person_id
4304                   , p_termination_date     => l_termination_date
4305                   , p_last_act_seq         => l_lat_act_seq
4306                   , p_last_p45_act         => l_last_p45_action
4307 			,p_effective_date		 => csr_rec.effective_date
4308 			,p_ppsn_override_flag => l_ppsn_override  /* 7291676 */
4309 			);
4310 
4311            -- Statutory Balances are archived for all Separate Payment assignment actions
4312            -- and the last (i.e. highest action_sequence) Process Separately assignment action
4313            -- (EMEA BALANCES)
4314            hr_utility.set_location('Archive User Balances - Complete',60);
4315            -- archive statutory balances
4316            hr_utility.set_location('Archive Statutory Balances - Starting',70);
4317            hr_utility.set_location('g_max_statutory_balance_index = '|| g_max_statutory_balance_index,70);
4318 
4319 
4320            hr_utility.set_location('PPSN Override  value  = ' || l_ppsn_override,70);
4321 
4322            FOR l_index IN 1 .. g_max_statutory_balance_index
4323            LOOP
4324 
4325 	   if (l_ppsn_override is null) then
4326 
4327                hr_utility.set_location('l_index = ' || l_index,70);
4328                hr_utility.set_location('balance_name ='||g_statutory_balance_table(l_index).balance_name,70);
4329                hr_utility.set_location('database_item_suffix ='||g_statutory_balance_table(l_index).database_item_suffix,70);
4330              /*
4331               --Bug:2448728.Passing the prepayment assignment action id to p_source_id as the _PAYMENTS
4332               --balances are fed only during pre Payments.
4333                If g_statutory_balance_table(l_index).balance_name = 'Total Pay' Then
4334                pay_ie_p45_archive.process_balance (
4335                        p_action_context_id => l_action_context_id
4336                      , p_assignment_id     => csr_rec.assignment_id
4337                      , p_source_id         => csr_rec.pre_assignment_action_id
4338                      , p_effective_date    => csr_rec.effective_date
4339                      , p_balance           => g_statutory_balance_table(l_index).balance_name
4340                      , p_dimension         => g_statutory_balance_table(l_index).database_item_suffix
4341                      , p_defined_bal_id    => g_statutory_balance_table(l_index).defined_balance_id
4342                      , p_record_count      => l_record_count);
4343                 Else
4344               */
4345           --      IF ( l_supp_flag = 'Y' OR  l_index < 19 OR l_index > 31 ) THEN
4346 
4347                --   IF (l_index < 20) THEN                                                        -- Bug 5015438 13359530
4348 		   IF (l_index < 24) THEN
4349 				pay_ie_p45_archive.process_balance (
4350                                      p_action_context_id => l_action_context_id
4351                                    , p_assignment_id     => csr_rec.assignment_id
4352                                    , p_person_id         => l_person_id
4353                                    , p_source_id         => l_child_pay_action
4354                                    , p_effective_date    => csr_rec.effective_date
4355                                    , p_balance           => g_statutory_balance_table(l_index).balance_name
4356                                    , p_dimension         => g_statutory_balance_table(l_index).database_item_suffix
4357                                    , p_defined_bal_id    => g_statutory_balance_table(l_index).defined_balance_id
4358                                    , p_record_count      => l_record_count
4359 				           , p_termination_date  => l_termination_date
4360 				           , p_supp_flag         => l_supp_flag
4361 				           , p_last_p45_action   => l_last_p45_action
4362 				           , p_last_p45_pact     => l_last_p45_pact         -- Bug 5005788
4363 				           , p_prev_src_id       => l_prev_src_id);
4364 		  ELSE
4365 		    IF (l_supp_flag = 'Y') THEN
4366                                pay_ie_p45_archive.process_supp_balance (
4367                                      p_action_context_id => l_action_context_id
4368                                    , p_assignment_id     => csr_rec.assignment_id
4369                                    , p_person_id         => l_person_id
4370                                    , p_source_id         => l_child_pay_action
4371                                    , p_effective_date    => csr_rec.effective_date
4372                                    , p_balance           => g_statutory_balance_table(l_index).balance_name
4373                                    , p_dimension         => g_statutory_balance_table(l_index).database_item_suffix
4374                                    , p_defined_bal_id    => g_statutory_balance_table(l_index).defined_balance_id
4375                                    , p_record_count      => l_record_count
4376 				           , p_termination_date  => l_termination_date
4377 				           , p_supp_flag         => l_supp_flag
4378 				           , p_last_p45_action   => l_last_p45_action
4379 				           , p_last_p45_pact     => l_last_p45_pact          -- Bug 5005788
4380 				           , p_ytd_balance       => g_statutory_balance_table(l_index - l_max_stat_balance).balance_name
4381 				           , p_ytd_def_bal_id    => g_statutory_balance_table(l_index - l_max_stat_balance).defined_balance_id);
4382 		    END IF;
4383 
4384 	          END IF;
4385 
4386 		  ELSE   /* if ppsn override is present */
4387 		   hr_utility.set_location('entered the else block in the archve code  ' ,70);
4388                      hr_utility.set_location('l_index = ' || l_index,70);
4389                hr_utility.set_location('balance_name ='||g_statutory_balance_table_ppsn(l_index).balance_name,70);
4390                hr_utility.set_location('database_item_suffix ='||g_statutory_balance_table_ppsn(l_index).database_item_suffix,70);
4391 
4392                  --  IF (l_index < 20) THEN                                                        -- Bug 5015438  13359530
4393 		     IF (l_index < 24) THEN
4394 				pay_ie_p45_archive.process_balance (
4395                                      p_action_context_id => l_action_context_id
4396                                    , p_assignment_id     => csr_rec.assignment_id
4397                                    , p_person_id         => l_person_id
4398                                    , p_source_id         => l_child_pay_action_ppsn
4399                                    , p_effective_date    => csr_rec.effective_date
4400                                    , p_balance           => g_statutory_balance_table_ppsn(l_index).balance_name
4401                                    , p_dimension         => g_statutory_balance_table_ppsn(l_index).database_item_suffix
4402                                    , p_defined_bal_id    => g_statutory_balance_table_ppsn(l_index).defined_balance_id
4403                                    , p_record_count      => l_record_count
4404 				           , p_termination_date  => l_termination_date
4405 				           , p_supp_flag         => l_supp_flag
4406 				           , p_last_p45_action   => l_last_p45_action
4407 				           , p_last_p45_pact     => l_last_p45_pact         -- Bug 5005788
4408 				           , p_prev_src_id       => l_prev_src_id);
4409 
4410 		  ELSE
4411 		    IF (l_supp_flag = 'Y') THEN
4412                     hr_utility.set_location('entered the if  block and supp flag is y  ' ,70);
4413                                pay_ie_p45_archive.process_supp_balance (
4414                                      p_action_context_id => l_action_context_id
4415                                    , p_assignment_id     => csr_rec.assignment_id
4416                                    , p_person_id         => l_person_id
4417                                    , p_source_id         => l_child_pay_action_ppsn
4418                                    , p_effective_date    => csr_rec.effective_date
4419                                    , p_balance           => g_statutory_balance_table_ppsn(l_index).balance_name
4420                                    , p_dimension         => g_statutory_balance_table_ppsn(l_index).database_item_suffix
4421                                    , p_defined_bal_id    => g_statutory_balance_table_ppsn(l_index).defined_balance_id
4422                                    , p_record_count      => l_record_count
4423 				           , p_termination_date  => l_termination_date
4424 				           , p_supp_flag         => l_supp_flag
4425 				           , p_last_p45_action   => l_last_p45_action
4426 				           , p_last_p45_pact     => l_last_p45_pact          -- Bug 5005788
4427 				           , p_ytd_balance       => g_statutory_balance_table_ppsn(l_index - l_max_stat_balance).balance_name
4428 				           , p_ytd_def_bal_id    => g_statutory_balance_table_ppsn(l_index - l_max_stat_balance).defined_balance_id);
4429 		    END IF;
4430 
4431 	          END IF;
4432 
4433 		  END IF; --  ppsn override if condition
4434               --  End If;
4435            END LOOP;
4436 	   l_ppsn_override:=null; -- 7291676
4437            hr_utility.set_location('Archive Statutory Balances - Complete',70);
4438 	   	      l_record_count := l_record_count + 1;
4439         END IF;
4440        END IF;
4441       CLOSE cur_child_pay_action;
4442       l_date_earned := csr_rec.date_earned;
4443     END LOOP;
4444     hr_utility.set_location('Leaving '|| l_proc,80);
4445   END archive_code;
4446 
4447   --------------------------------------------------------------------------------
4448   -- Bug 2643489: Function to return balnce values archived by P45 Archive Process
4449   --------------------------------------------------------------------------------
4450 
4451 -- Added the parameter p_payroll_action_id to improve the performance,
4452   FUNCTION get_arc_bal_value(
4453                        p_assignment_action_id  in number
4454                       ,p_payroll_action_id     in number
4455                       ,p_balance_name          in varchar2 ) return number
4456   AS
4457 
4458  /* Split the cursor to 2 cursors to improve the performace.new parameter is added to reduce the number of
4459     tables involved to 2 from 5 (5005788) */
4460     CURSOR csr_get_def_bal(p_payroll_action_id pay_payroll_actions.payroll_action_id%TYPE
4461     			        ,p_balance_name      pay_balance_types.balance_name%TYPE
4462 				,c_ppsn_flag varchar2) IS
4463     SELECT pai1.action_information2
4464       FROM pay_action_information pai1
4465      WHERE pai1.action_context_type         = 'PA'
4466        AND pai1.action_information_category = 'EMEA BALANCE DEFINITION'
4467        AND substr(pai1.action_information4, 1,50) = p_balance_name
4468        AND pai1.action_context_id = p_payroll_action_id
4469       -- AND pai1.action_information7 = c_ppsn_flag
4470       -- and ((nvl(pai1.action_information7,'N')='N') or (pai1.action_information7='Y'))
4471  and ((nvl(pai1.action_information7,'N')='N' and c_ppsn_flag='N') or (nvl(pai1.action_information7,'N')='Y' and c_ppsn_flag='Y') ) ;
4472 
4473     CURSOR csr_get_arc_bal_value(p_assignment_action_id  pay_assignment_actions.assignment_action_id%TYPE
4474                                 ,p_def_bal_id          pay_action_information.action_information1%TYPE) IS
4475       SELECT to_number(pai2.action_information4)    balance_value
4476         FROM pay_action_information pai2
4477       WHERE pai2.action_context_id = p_assignment_action_id
4478         AND pai2.action_information_category = 'EMEA BALANCES'
4479         AND pai2.action_context_type         = 'AAP'
4480         AND pai2.action_information1         =  p_def_bal_id;
4481 
4482 /* 7291676 */
4483    CURSOR csr_check_override_present (p_assignment_action_id  pay_assignment_actions.assignment_action_id%TYPE) IS
4484    SELECT paei.aei_information1
4485    FROM   per_assignment_extra_info paei,
4486           pay_assignment_actions paa
4487    WHERE paa.assignment_action_id=p_assignment_action_id
4488      and paei.assignment_id=paa.assignment_id
4489      and paei.aei_information_category = 'IE_ASG_OVERRIDE';
4490 
4491   l_ppsn_override  per_assignment_extra_info.aei_information1%type;
4492   l_ppsn_override_flag varchar2(2);
4493 
4494   l_bal_value number:=null;
4495   l_def_bal_id pay_action_information.action_information1%TYPE := NULL;
4496 
4497   BEGIN
4498 
4499 --hr_utility.trace_on(null,'P45XML');
4500     OPEN csr_check_override_present(p_assignment_action_id);
4501     FETCH csr_check_override_present INTO l_ppsn_override;
4502 
4503     IF csr_check_override_present%found and l_ppsn_override is not null THEN
4504     l_ppsn_override_flag:='Y';
4505     ELSE
4506     l_ppsn_override_flag:='N';
4507     END IF;
4508     CLOSE csr_check_override_present;
4509 
4510     hr_utility.set_location('l_ppsn_override_flag '||l_ppsn_override_flag,400);
4511 
4512     IF p_assignment_action_id IS NOT NULL AND p_balance_name IS NOT NULL THEN
4513       OPEN csr_get_def_bal(p_payroll_action_id,p_balance_name,l_ppsn_override_flag);
4514       FETCH csr_get_def_bal INTO l_def_bal_id;
4515       CLOSE csr_get_def_bal;
4516 	hr_utility.set_location('p_payroll_action_id '||p_payroll_action_id,400);
4517 	hr_utility.set_location('p_balance_name '||p_balance_name,400);
4518 	hr_utility.set_location('l_def_bal_id '||l_def_bal_id,400);
4519 
4520       IF l_def_bal_id IS NOT NULL THEN
4521         OPEN csr_get_arc_bal_value(p_assignment_action_id,l_def_bal_id);
4522         FETCH csr_get_arc_bal_value into l_bal_value;
4523         CLOSE csr_get_arc_bal_value;
4524       END IF;
4525 
4526     END IF;
4527 /*Bug 3986018*/
4528 hr_utility.set_location('l_def_bal_id '||l_def_bal_id,400);
4529 
4530     return(nvl(l_bal_value,0));
4531 
4532   END get_arc_bal_value;
4533 
4534   ---------------------------------------------------------------------
4535   -- Procedure generate_xml - Generates P45 XML Output File
4536   -- viviswan 29-may-2002 created
4537   ---------------------------------------------------------------------
4538   PROCEDURE generate_xml(
4539                          errbuf                   out nocopy varchar2
4540                         ,retcode                  out nocopy varchar2
4541                         ,p_p45_archive_process    in  number
4542                         ,p_assignment_id          in  number) IS
4543   -- Commented for Bug 2643489 Performance
4544     /*SELECT  paa.assignment_id assignment_id
4545          ,paa.assignment_action_id
4546          ,pai_iep45.action_information2                                   supp_flag
4547          ,emp_details.pps_no                                              ppsn
4548          ,emp_details.last_name                                           surname
4549          ,emp_details.first_name                                          firstname
4550          ,emp_details.works_no                                            works
4551          ,emp_details.deceased                                            deceased
4552          ,to_char(emp_details.date_of_birth,'dd/mm/rrrr')                 dob
4553          ,emp_address.address1                                            address1
4554          ,emp_address.address2                                            address2
4555          ,emp_address.address3                                            address3
4556          ,to_char(emp_details.date_of_commencement,'dd/mm/rrrr')          start1
4557          ,to_char(emp_details.date_of_leaving,'dd/mm/rrrr')               end1
4558          ,decode(ptp.period_type,'Lunar Month','W',decode(instr(ptp.period_type,'Week'),0,'M','W')) freq
4559          ,to_number(substr(pai_iep45.action_information5, 1,30))                  period
4560          ,(round(to_number(substr(pai_ieed.action_information26, 1,30)),2)*100)     taxcredit
4561          ,(round(to_number(substr(pai_ieed.action_information27, 1,30)),2)*100)     cutoff
4562          ,pai_iep45.action_information4                                   emergency_tax
4563          ,substr(pai_ieed.action_information22, 1,30)                     prsi_class
4564          ,(round(to_number(emp_paye.total_tax),2)*100)                    totaltax
4565          ,(round(to_number(emp_paye.total_pay),2)*100)                    totalpay
4566          ,(round(to_number(emp_paye.this_tax),2)*100)                     thistax
4567          ,(round(to_number(emp_paye.this_pay),2)*100)                     thispay
4568          ,(round(to_number(emp_paye.lump_sum),2)*100)                     lumpsum
4569          ,(round(to_number(emp_prsi.total_prsi),2)*100)                   totalprsi
4570          ,(round(to_number(emp_prsi.total_employee_prsi),2)*100)          employeeprsi
4571          ,emp_prsi.insurable_weeks                                        totalweeks
4572          ,emp_prsi.class_a_insurable_weeks                                totalaweeks
4573          ,(round(to_number(emp_soc.disability_benefit),2)*100)            benefit
4574          ,(round(to_number(emp_soc.red_tax_credit),2)*100)                taxcreditreduction
4575          ,(round(to_number(emp_soc.red_std_cut_off),2)*100)               cutoffreduction
4576          ,emp_soc.non_cummulative_basis                                   noncumulative
4577          ,pai_epif.action_information6                                    employer_number
4578          ,(round(to_number(emp_supp.total_tax),2)*100)                    supp_totaltax
4579          ,(round(to_number(emp_supp.total_pay),2)*100)                    supp_totalpay
4580          ,(round(to_number(emp_supp.lump_sum),2)*100)                     supp_lumpsum
4581          ,(round(to_number(emp_supp.total_prsi),2)*100)                   supp_totalprsi
4582          ,(round(to_number(emp_supp.total_employee_prsi),2)*100)          supp_employeeprsi
4583          ,emp_supp.insurable_weeks                                        supp_totalweeks
4584   FROM    pay_action_information                  pai_ed
4585          ,pay_action_information                  pai_iep45
4586          ,pay_action_information                  pai_ieed
4587          ,pay_action_information                  pai_epif
4588          ,pay_assignment_actions                  paa
4589          ,pay_action_interlocks                   pai_arc
4590          ,pay_assignment_actions                  paa_payroll
4591          ,per_time_periods                        ptp
4592          ,pay_ie_p45_address_details              emp_address
4593          ,pay_ie_p45_employee_details             emp_details
4594          ,pay_ie_p45_soc_ben_details              emp_soc
4595          ,pay_ie_p45_prsi_details                 emp_prsi
4596          ,pay_ie_p45_paye_details                 emp_paye
4597          ,pay_ie_p45_supp_details                 emp_supp
4598   WHERE   paa.payroll_action_id                   = c_p45_arch_id
4599   AND     paa.assignment_action_id                = pai_arc.locking_action_id
4600   AND     paa_payroll.assignment_action_id        = pai_arc.locked_action_id
4601   AND     paa.assignment_action_id                = pai_iep45.action_context_id
4602   AND     pai_iep45.action_context_type           ='AAP'
4603   AND     pai_iep45.action_information_category   = 'IE P45 INFORMATION'
4604   AND     paa.assignment_action_id                = pai_ed.action_context_id
4605   AND     pai_ed.action_context_type              ='AAP'
4606   AND     pai_ed.action_information_category      = 'EMPLOYEE DETAILS'
4607   AND     ptp.time_period_id                      = pai_ed.action_information16
4608   AND     paa.assignment_action_id                = pai_ieed.action_context_ID
4609   AND     pai_ieed.action_context_type            ='AAP'
4610   AND     pai_ieed.action_information_category    = 'IE EMPLOYEE DETAILS'
4611   AND     paa.payroll_action_id                   = pai_epif.action_context_ID (+)
4612   AND     pai_epif.action_context_type    (+)     ='PA'
4613   AND     pai_epif.action_information_category (+)= 'EMEA PAYROLL INFO'
4614   AND     pai_epif.action_information1            =  paa_payroll.payroll_action_id
4615   AND     emp_address.assignment_action_id        = paa.assignment_action_id
4616   AND     emp_details.assignment_action_id        = paa.assignment_action_id
4617   AND     emp_soc.assignment_action_id            = paa.assignment_action_id
4618   AND     emp_prsi.assignment_action_id           = paa.assignment_action_id
4619   AND     emp_paye.assignment_action_id           = paa.assignment_action_id
4620   AND     emp_supp.assignment_id (+)              = paa.assignment_id
4621   AND     paa.assignment_id                       = NVL(p_assignment_id,paa.assignment_id)
4622   ORDER BY pai_iep45.action_information2;
4623   */
4624   -- The above cursor has been modified as specified below.
4625   CURSOR  cur_p45_details(
4626           c_p45_arch_id pay_payroll_actions.payroll_action_id%TYPE) IS
4627   SELECT  paa.assignment_id assignment_id
4628          ,paa.assignment_action_id
4629          ,pai_iep45.action_information2                                   supp_flag
4630          ,decode(ptp.period_type,'Lunar Month','W',decode(instr(ptp.period_type,'Week'),0,'M','W')) freq --13359530-1
4631          ,to_number(substr(pai_iep45.action_information5, 1,30))                  period
4632          ,pai_iep45.action_information4                                   emergency_tax,
4633           to_date(substr(pai_iep45.action_information7, 1,30),'DD/MM/RRRR') date_paid --Bug 3991416
4634 	  ,pai_iep45.action_information8  person_id  -- 7291676
4635 	  ,pai_iep45.action_information9  main_p45_date_paid  -- 7291676
4636   FROM    pay_action_information                  pai_ed
4637          ,pay_action_information                  pai_iep45
4638          ,pay_assignment_actions                  paa
4639          ,per_time_periods                        ptp
4640   WHERE   paa.payroll_action_id                   = c_p45_arch_id
4641   AND     paa.assignment_action_id                = pai_iep45.action_context_id
4642   AND     pai_iep45.action_context_type           ='AAP'
4643   AND     pai_iep45.action_information_category   = 'IE P45 INFORMATION'
4644   AND     ptp.time_period_id                      = pai_ed.action_information16
4645   AND     paa.assignment_action_id                = pai_ed.action_context_ID
4646   AND     pai_ed.action_context_type              ='AAP'
4647   AND     pai_ed.action_information_category      = 'EMPLOYEE DETAILS'
4648   AND     paa.assignment_id                       = NVL(p_assignment_id,paa.assignment_id)
4649   ORDER BY pai_iep45.action_information2;
4650 --
4651 CURSOR  cur_p45_employer_no (p_assignment_action_id pay_assignment_actions.assignment_action_id%TYPE )IS
4652 SELECT  pai_epif.action_information6                                    employer_number
4653 FROM    pay_assignment_actions                  paa
4654        ,pay_action_interlocks                   pai_arc
4655        ,pay_assignment_actions                  paa_payroll
4656        ,pay_action_information                  pai_epif
4657 WHERE   paa.assignment_action_id                = p_assignment_action_id
4658 AND     paa.assignment_action_id                = pai_arc.locking_action_id
4659 AND     paa_payroll.assignment_action_id        = pai_arc.locked_action_id
4660 AND     paa.payroll_action_id                   = pai_epif.action_context_ID
4661 AND     pai_epif.action_context_type            ='PA'
4662 AND     pai_epif.action_information_category   = 'EMEA PAYROLL INFO'
4663 AND     pai_epif.action_information1            =  paa_payroll.payroll_action_id;
4664 --
4665 CURSOR  cur_p45_ie_emp_details (p_assignment_action_id pay_assignment_actions.assignment_action_id%TYPE )IS
4666 SELECT  (round(to_number(substr(nvl(pai_ieed.action_information26,'0'), 1,30)),2)*100)     taxcredit
4667        ,(round(to_number(substr(nvl(pai_ieed.action_information27,'0'), 1,30)),2)*100)     cutoff
4668        ,substr(pai_ieed.action_information22, 1,30)                               prsi_class
4669 	,(round(to_number(substr(nvl(pai_ieed.action_information20,'0'), 1,30)),2)*100)     usccutoff1 -- BUG 13359530
4670       ,(round(to_number(substr(nvl(pai_ieed.action_information19,'0'), 1,30)),2)*100)     usccutoff2
4671 FROM    pay_action_information                  pai_ieed
4672 WHERE   pai_ieed.action_context_ID              = p_assignment_action_id
4673 AND     pai_ieed.action_context_type            = 'AAP'
4674 AND     pai_ieed.action_information_category    = 'IE EMPLOYEE DETAILS';
4675 cur_p45_ie_emp_details_rec cur_p45_ie_emp_details%ROWTYPE;
4676 --
4677 --6615117
4678 CURSOR  cur_p45_emp_soc_details (p_assignment_action_id pay_assignment_actions.assignment_action_id%TYPE )IS
4679 SELECT       (round(to_number(nvl(emp_soc.disability_benefit,'0')),2)*100)            benefit
4680             ,(round(to_number(nvl(emp_soc.red_tax_credit,'0')),2)*100)                taxcreditreduction
4681             ,(round(to_number(nvl(emp_soc.red_std_cut_off,'0')),2)*100)               cutoffreduction
4682             ,emp_soc.non_cummulative_basis                                   noncumulative
4683 FROM        pay_ie_p45_soc_ben_details              emp_soc
4684 WHERE       emp_soc.assignment_action_id        = p_assignment_action_id;
4685 cur_p45_emp_soc_details_rec cur_p45_emp_soc_details%ROWTYPE;
4686 --
4687 --6615117
4688 CURSOR  cur_p45_emp_details (p_assignment_action_id pay_assignment_actions.assignment_action_id%TYPE )IS
4689 SELECT      emp_details.pps_no                                              ppsn
4690            ,emp_details.last_name                                           surname
4691            ,emp_details.first_name                                          firstname
4692            ,emp_details.works_no                                            works
4693            ,emp_details.deceased                                            deceased
4694            ,to_char(emp_details.date_of_birth,'dd/mm/rrrr')                 dob
4695            ,to_char(emp_details.date_of_commencement,'dd/mm/rrrr')          start1
4696            ,to_char(emp_details.date_of_leaving,'dd/mm/rrrr')               end1
4697 	     ,emp_details.address_line1                                           address_line1
4698 		,emp_details.address_line2                                           address_line2
4699 		,emp_details.address_line3                                           address_line3
4700 		,emp_details.address_line4                                           address_line4
4701 		,emp_details.kin_name                                          kin_name
4702 FROM        pay_ie_p45_employee_details              emp_details
4703 WHERE       emp_details.assignment_action_id        = p_assignment_action_id;
4704 cur_p45_emp_details_rec cur_p45_emp_details%ROWTYPE;
4705 --
4706 CURSOR  cur_p45_emp_address (p_assignment_action_id pay_assignment_actions.assignment_action_id%TYPE )IS
4707 SELECT      emp_address.address1                                            address1
4708            ,emp_address.address2                                            address2
4709            ,emp_address.address3                                            address3
4710 FROM        pay_ie_p45_address_details              emp_address
4711 WHERE       emp_address.assignment_action_id        = p_assignment_action_id;
4712 cur_p45_emp_address_rec cur_p45_emp_address%ROWTYPE;
4713 --
4714 CURSOR  cur_p45_paye_prsi_details (p_assignment_action_id pay_assignment_actions.assignment_action_id%TYPE )IS
4715 SELECT   (round(to_number(emp_paye.total_tax),2)*100)                     totaltax
4716          ,(round(to_number(emp_paye.total_pay),2)*100)                    totalpay
4717          ,(round(to_number(emp_paye.this_tax),2)*100)                     thistax
4718          ,(round(to_number(emp_paye.this_pay),2)*100)                     thispay
4719          ,(round(to_number(emp_paye.lump_sum),2)*100)                     lumpsum
4720          ,(round(to_number(emp_prsi.total_employer_prsi),2)*100)          employerprsi          -- Bug  5005788
4721          ,(round(to_number(emp_prsi.total_employee_prsi),2)*100)          employeeprsi
4722          ,emp_prsi.insurable_weeks                                        totalweeks
4723          ,emp_prsi.class_a_insurable_weeks                                totalaweeks
4724 		, (round(to_number(emp_paye.this_usc_pay),2)*100)                thisuscpay
4725          , (round(to_number(emp_paye.this_usc),2)*100)                    thisusc
4726          , (round(to_number(emp_paye.total_usc_pay),2)*100)               totaluscpay
4727          , (round(to_number(emp_paye.total_usc),2)*100)                   totalusc
4728 FROM     pay_ie_p45_prsi_details                 emp_prsi
4729         ,pay_ie_p45_paye_details                 emp_paye
4730 WHERE   emp_prsi.assignment_action_id            = p_assignment_action_id
4731 AND     emp_paye.assignment_action_id                   = p_assignment_action_id;
4732 cur_p45_paye_prsi_rec  cur_p45_paye_prsi_details%ROWTYPE;
4733 --
4734 --Bug 3991416 Added period,frequency and date earned check to fetch single record
4735 CURSOR  cur_p45_supp_details (p_assignment_id per_all_assignments_f.assignment_id%TYPE,
4736                               p_date_earned date,
4737                               p_period NUMBER,
4738                               p_freq VARCHAR2) IS
4739 SELECT  (round(to_number(emp_supp.total_tax),2)*100)                    supp_totaltax
4740        ,(round(to_number(emp_supp.total_pay),2)*100)                    supp_totalpay
4741        ,(round(to_number(emp_supp.lump_sum),2)*100)                     supp_lumpsum
4742        ,(round(to_number(emp_supp.total_employer_prsi),2)*100)           supp_totalprsi           -- Bug  5005788
4743        ,(round(to_number(emp_supp.total_employee_prsi),2)*100)          supp_employeeprsi
4744        ,emp_supp.insurable_weeks                                        supp_totalweeks
4745        ,emp_supp.supp_insurable_classA_weeks                            supp_classA_weeks          -- Bug 5015438
4746 	,(round(to_number(emp_supp.total_usc),2)*100)                    supp_totalusc   -- bug 13359530
4747       ,(round(to_number(emp_supp.total_usc_pay),2)*100)                 supp_totaluscpay
4748 FROM    pay_ie_p45_supp_details                 emp_supp
4749 WHERE   emp_supp.assignment_id                  = p_assignment_id
4750 AND     emp_supp.date_paid =p_date_earned
4751 AND     emp_supp.pay_period =p_period
4752 AND     emp_supp.period_frequency = decode(p_freq,'M','Monthly','Weekly');
4753 --
4754 CURSOR  cur_p30_start_date(
4755         c_p30_data_lock_process pay_payroll_actions.payroll_action_id%TYPE) IS
4756 SELECT  to_char(MIN(ppa_arc.start_date),'DD/MM/RRRR') start_date
4757 FROM    pay_assignment_actions paa_p30,
4758         pay_action_interlocks  pai_p30,
4759         pay_assignment_actions paa_arc,
4760         pay_payroll_actions    ppa_arc
4761 WHERE   paa_p30.payroll_Action_id    = c_p30_data_lock_process
4762 AND     paa_p30.assignment_action_id = pai_p30.locking_action_id
4763 AND     paa_arc.assignment_action_id = pai_p30.locked_action_id
4764 AND     ppa_arc.payroll_action_id    = paa_arc.payroll_action_id;
4765 --
4766 CURSOR  cur_employer_address(
4767         c_payroll_action_id pay_payroll_actions.payroll_action_id%TYPE) IS
4768 SELECT  substr(pai.action_information5,1,30)  employer_tax_addr1
4769        ,substr(pai.action_information6,1,30)  employer_tax_addr2
4770        ,substr(pai.action_information7,1,30)  employer_tax_addr3
4771        ,substr(pai.action_information26,1,30) employer_tax_contact
4772        ,substr(pai.action_information27,1,12) employer_tax_ref_phone
4773        ,substr(pai.action_information28,1,30) employer_tax_rep_name
4774 FROM    pay_action_information pai
4775 WHERE   pai.action_context_id            =  c_payroll_action_id
4776 AND     pai.action_context_type          = 'PA'
4777 AND     pai.action_information_category  = 'ADDRESS DETAILS'
4778 AND     pai.action_information14         = 'IE Employer Tax Address';
4779 
4780 
4781 /* 7291676 */
4782 /* to check whether the termination date of the assignment is after 2009 */
4783 CURSOR cur_service_leave_year(c_person_id per_all_people_f.person_id%type,c_action_context_id pay_assignment_actions.assignment_action_id%type) IS
4784   select 'Y'
4785   from  per_periods_of_service ppos
4786   where ppos.person_id = c_person_id
4787   and   ppos.period_of_service_id = (select max(paf.period_of_service_id)
4788                                         from per_all_assignments_f paf,
4789                                              pay_assignment_actions paa,
4790   					               pay_action_interlocks pai
4791   	                               where   pai.locking_action_id = c_action_context_id
4792   				                 and pai.locked_action_id  = paa.assignment_action_id
4793                                          and paa.action_status IN ('C','S')  --10225372
4794                                          and paa.assignment_id = paf.assignment_id
4795                                      )
4796   and to_char(trunc(ppos.actual_termination_date,'Y'),'YYYY')>='2009';
4797 l_term_yr_2009     boolean;
4798 l_flag      varchar2(1);
4799 
4800 cursor csr_ppsn_override(p_asg_id number)
4801 is
4802 select aei_information1 PPSN_OVERRIDE
4803 from per_assignment_extra_info
4804 where assignment_id = p_asg_id
4805 and aei_information_category = 'IE_ASG_OVERRIDE';
4806 
4807 l_ppsn_override per_assignment_extra_info.aei_information1%type;
4808 
4809 cursor csr_supp_paydetails(p_asg_id number)
4810 is
4811 select aei_information1 year1
4812       ,(round(to_number(nvl(aei_information2,'0')),2)*100) pay1
4813       ,aei_information3 year2
4814       ,(round(to_number(nvl(aei_information4,'0')),2)*100) pay2
4815       ,aei_information5 year3
4816       ,(round(to_number(nvl(aei_information6,'0')),2)*100) pay3
4817       ,aei_information7 year4
4818       ,(round(to_number(nvl(aei_information8,'0')),2)*100) pay4
4819       ,aei_information9 year5
4820       ,(round(to_number(nvl(aei_information10,'0')),2)*100) pay5
4821       ,(round(to_number(nvl(aei_information11,'0')),2)*100) allotherpay
4822 from per_assignment_extra_info
4823 where assignment_id = p_asg_id
4824 and aei_information_category = 'IE_SUPP_P45_PAY';
4825 
4826 csr_supp_paydetails_rec csr_supp_paydetails%rowtype;
4827 l_eit_sum number;
4828 
4829 error_message boolean;
4830 l_str_Common VARCHAR2(2000);
4831 l_start_date                      VARCHAR2(30);
4832 l_eit_supp_flag boolean;
4833 
4834 
4835 --
4836 --
4837 l_root_start_tag        varchar2(200);
4838 l_root_start_tag_new    varchar2(200);
4839 l_root_end_tag          varchar2(50);
4840 --
4841 l_employer_start_tag    varchar2(20);
4842 l_employer_end_tag      varchar2(20);
4843 --
4844 l_p45_start_tag         varchar2(20);
4845 l_p45_end_tag           varchar2(20);
4846 --
4847 --
4848 l_employer_paye_number  varchar2(80);
4849 l_employer_number       varchar2(10);
4850 l_employer_name         varchar2(30);
4851 l_employer_add1         varchar2(30);
4852 l_employer_add2         varchar2(30);
4853 l_employer_add3         varchar2(30);
4854 l_employer_contact      varchar2(20);
4855 l_employer_phone        varchar2(12);
4856 --
4857 l_supp_totaltax         number;
4858 l_supp_totalpay         number;
4859 l_supp_lumpsum          number;
4860 l_supp_totalprsi        number;
4861 l_supp_employeeprsi     number;
4862 l_supp_totalweeks       varchar2(10);
4863 l_supp_classA_weeks     varchar2(10);                          -- Bug 5015438
4864 --
4865 l_employment_unit       varchar2(3);
4866 once_per_run            varchar2(1);
4867 vfrom                   number;
4868 vfound                  number;
4869 vto                     number;
4870 v_prsi_class            varchar2(10);
4871 ppsn_flag			number(1) :=1;
4872 warn_status			number(1) := 0;
4873 l_conc_status		BOOLEAN;
4874 l_total_prsi            NUMBER;
4875 -- 13359530
4876 l_product		VARCHAR2(25):= 'ORACLE';
4877 l_root_start_tag_new1    varchar2(200);
4878 l_supp_totalusc         number;
4879 l_supp_totaluscpay         number;
4880 l_sur_name varchar2(1000); -- 13359530
4881 l_first_name varchar2(1000);
4882 l_addressline1 varchar2(100);
4883 l_addressline2 varchar2(100);
4884 l_addressline3 varchar2(100);
4885 BEGIN
4886   once_per_run          := 'N' ;
4887   l_employment_unit     := '000';
4888   l_root_start_tag      :='<P45File currency="E" formversion="3" language="E" printer="0">';
4889   l_root_start_tag_new  :='<P45File currency="E" formversion="4" language="E" printer="0">'; -- 7291676
4890   l_root_start_tag_new1  :='<P45File xmlns="http://www.ros.ie/schemas/P45/v5/" currency="E" formversion="5" language="E" printer="0" product="ORACLE">'; -- 13359530
4891   l_root_end_tag        :='</P45File>';
4892   FOR p45_rec IN cur_p45_details(p_p45_archive_process) LOOP
4893     OPEN cur_p45_paye_prsi_details(p45_rec.assignment_action_id);
4894       FETCH cur_p45_paye_prsi_details into cur_p45_paye_prsi_rec;
4895     CLOSE cur_p45_paye_prsi_details;
4896     --
4897     OPEN cur_p45_emp_address(p45_rec.assignment_action_id);
4898       FETCH cur_p45_emp_address into cur_p45_emp_address_rec;
4899     CLOSE cur_p45_emp_address;
4900     --
4901     OPEN cur_p45_emp_soc_details(p45_rec.assignment_action_id);
4902       FETCH cur_p45_emp_soc_details into cur_p45_emp_soc_details_rec;
4903     CLOSE cur_p45_emp_soc_details;
4904     --
4905     OPEN cur_p45_emp_details(p45_rec.assignment_action_id);
4906       FETCH cur_p45_emp_details into cur_p45_emp_details_rec;
4907     CLOSE cur_p45_emp_details;
4908 
4909       l_sur_name		:= test_XML(substr(cur_p45_emp_details_rec.surname,1,20));
4910 	l_first_name	:= test_XML(substr(cur_p45_emp_details_rec.firstname,1,20));
4911 	l_addressline1 := test_XML(substr(cur_p45_emp_address_rec.address1,1,35));
4912 	l_addressline2 := test_XML(substr(cur_p45_emp_address_rec.address2,1,35));
4913 	l_addressline3 := test_XML(substr(cur_p45_emp_address_rec.address3,1,35));
4914     --
4915     OPEN cur_p45_ie_emp_details(p45_rec.assignment_action_id);
4916       FETCH cur_p45_ie_emp_details into cur_p45_ie_emp_details_rec;
4917     CLOSE cur_p45_ie_emp_details;
4918     --
4919     OPEN cur_p45_employer_no(p45_rec.assignment_action_id);
4920       FETCH cur_p45_employer_no into l_employer_number;
4921     CLOSE cur_p45_employer_no;
4922 
4923     l_total_prsi := 0;          -- Bug  5005788
4924 
4925 
4926   IF  p45_rec.supp_flag = 'Y' THEN
4927         -- Get Supp Details
4928 	/*Bug 3991416*/
4929          OPEN cur_p45_supp_details(p45_rec.assignment_id,p45_rec.date_paid,p45_rec.period,p45_rec.freq);
4930          FETCH cur_p45_supp_details INTO  l_supp_totaltax
4931                                           ,l_supp_totalpay
4932                                           ,l_supp_lumpsum
4933                                           ,l_supp_totalprsi
4934                                           ,l_supp_employeeprsi
4935                                           ,l_supp_totalweeks
4936 							,l_supp_classA_weeks
4937 							,l_supp_totalusc
4938 							,l_supp_totaluscpay;                          -- Bug 5015438
4939         l_supp_totalprsi := NVL(l_supp_totalprsi,0) + NVL(l_supp_employeeprsi,0);         -- Bug  5005788
4940         CLOSE cur_p45_supp_details;
4941   END IF;
4942   -- Report the assignment if the Pay and Tax values exist.
4943   IF  (((p45_rec.supp_flag = 'N') /*AND (NVL(cur_p45_paye_prsi_rec.totalpay,0) <> 0 OR
4944                                       NVL(cur_p45_paye_prsi_rec.totaltax,0) <> 0 OR
4945                                       NVL(cur_p45_paye_prsi_rec.thispay,0) <> 0 OR
4946                                       NVL(cur_p45_paye_prsi_rec.thistax,0) <> 0 OR
4947                                       NVL(cur_p45_paye_prsi_rec.lumpsum,0) <> 0 OR
4948                                       NVL(cur_p45_paye_prsi_rec.employerprsi,0) <> 0 OR     -- Bug  5005788
4949                                       NVL(cur_p45_paye_prsi_rec.employeeprsi,0) <> 0 OR
4950                                       NVL(cur_p45_paye_prsi_rec.totalweeks,0) <> 0
4951                                       )*/
4952        ) OR
4953        ((p45_rec.supp_flag = 'Y') AND (NVL(l_supp_totalpay,0) <> 0 OR
4954                                       NVL(l_supp_totaltax,0) <> 0 OR
4955                                       NVL(l_supp_totalprsi,0) <> 0 OR
4956                                       NVL(l_supp_employeeprsi,0) <> 0 OR
4957                                       NVL(l_supp_lumpsum,0) <> 0 OR
4958                                       NVL(l_supp_totalweeks,0) <> 0 OR
4959 						 NVL(l_supp_classA_weeks,0) <> 0 OR                           -- Bug 5015438
4960 						 NVL(l_supp_totalusc,0) <> 0   OR  -- bug 13359530
4961                                       NVL(l_supp_totaluscpay,0) <> 0
4962                                       )
4963        )
4964       ) THEN
4965 		IF once_per_run = 'N' THEN
4966 			-- Start of xml doc
4967 			/* 7291676 */
4968 			 --bug 14197192
4969 			/*    pay_ie_p45_archive.get_parameters (
4970 				     p_payroll_action_id => p_p45_archive_process
4971 				    , p_token_name        => 'START_DATE'
4972 				    , p_token_value       => l_start_date); */
4973 				pay_ie_p45_archive.get_parameters (
4974 				     p_payroll_action_id => p_p45_archive_process
4975 				    , p_token_name        => 'END_DATE'
4976 				    , p_token_value       => l_start_date);
4977 
4978 
4979 			-- P45File root ELEMENT
4980 		IF (to_number(to_char(to_date(l_start_date,'yyyy/mm/dd'),'yyyy')) >= 2012) THEN   -- 13359530
4981 		      FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
4982 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,l_root_start_tag_new1);
4983 		ELSE
4984 		    FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'<?xml version="1.0" encoding="UTF-8"?>');
4985 			IF (to_number(to_char(to_date(l_start_date,'yyyy/mm/dd'),'yyyy')) >= 2009) THEN   -- 7291676
4986 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,l_root_start_tag_new);
4987 			ELSE
4988 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,l_root_start_tag);
4989 			END IF;
4990 		END IF;
4991 
4992 			-- Get Employer Address
4993 			OPEN cur_employer_address(p_p45_archive_process);
4994 			FETCH cur_employer_address INTO l_employer_add1
4995 								    ,l_employer_add2
4996 								    ,l_employer_add3
4997 								    ,l_employer_contact
4998 								    ,l_employer_phone
4999 								    ,l_employer_name;
5000 			CLOSE cur_employer_address;
5001 			-- Employer ELEMENT
5002 			FND_FILE.PUT(FND_FILE.OUTPUT,'  <Employer ');
5003 			FND_FILE.PUT(FND_FILE.OUTPUT,'number="' || l_employer_number ||'" ');
5004 			FND_FILE.PUT(FND_FILE.OUTPUT,'name="'   || l_employer_name        ||'" ');
5005 			IF l_employer_add1 IS NOT NULL THEN
5006 				FND_FILE.PUT(FND_FILE.OUTPUT,'address1="' || l_employer_add1    ||'" ');
5007 			END IF;
5008 			IF l_employer_add2 IS NOT NULL THEN
5009 				FND_FILE.PUT(FND_FILE.OUTPUT,'address2="' || l_employer_add2    ||'" ');
5010 			END IF;
5011 			IF l_employer_add3 IS NOT NULL THEN
5012 				FND_FILE.PUT(FND_FILE.OUTPUT,'address3="' || l_employer_add3    ||'" ');
5013 			END IF;
5014 			FND_FILE.PUT(FND_FILE.OUTPUT,'contact="'  || l_employer_contact ||'" ');
5015 			IF l_employer_phone IS NOT NULL THEN
5016 			FND_FILE.PUT(FND_FILE.OUTPUT,'phone="'    || l_employer_phone   ||'" ');
5017 		      END IF;
5018 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5019 			-- End of Employer
5020 			once_per_run := 'Y';
5021 		END IF;
5022 	IF (to_number(to_char(nvl(p45_rec.date_paid,p45_rec.main_p45_date_paid),'yyyy')) >= 2009) THEN   -- 7291676
5023 		IF p45_rec.supp_flag = 'N' THEN
5024 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  <P45>');
5025 		ELSE
5026 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  <P45Supp>');
5027 		END IF;
5028 		-- Employee
5029 		FND_FILE.PUT(FND_FILE.OUTPUT,'    <Employee ');
5030 		IF cur_p45_emp_details_rec.ppsn  IS NOT NULL THEN  -- Optional
5031                 /* 7291676 */
5032 		        l_ppsn_override:=null;
5033 			open csr_ppsn_override(p45_rec.assignment_id);
5034 			fetch csr_ppsn_override into l_ppsn_override;
5035 			close csr_ppsn_override;
5036 			FND_FILE.PUT(FND_FILE.OUTPUT,'ppsn="'     ||nvl(l_ppsn_override, cur_p45_emp_details_rec.ppsn )     ||'" ');
5037 			ppsn_flag := 1;
5038 		 ELSE
5039 			ppsn_flag := 0;
5040 		END IF;
5041 
5042 		-- required
5043 		FND_FILE.PUT(FND_FILE.OUTPUT,'surname="'    || l_sur_name   ||'" ');
5044 		FND_FILE.PUT(FND_FILE.OUTPUT,'firstnames="'  || l_first_name ||'" ');
5045 		IF cur_p45_emp_details_rec.works IS NOT NULL THEN  -- Optional
5046 			FND_FILE.PUT(FND_FILE.OUTPUT,'works="'    || replace(cur_p45_emp_details_rec.works,'-','')     ||'" '); /* 7827732 */
5047 		END IF;
5048 
5049 		IF cur_p45_emp_details_rec.dob IS NOT NULL THEN  -- Optional
5050 			FND_FILE.PUT(FND_FILE.OUTPUT,'dob="'      || cur_p45_emp_details_rec.dob       ||'" ');
5051 
5052 			ELSIF  cur_p45_emp_details_rec.dob  IS NULL and ppsn_flag = 0 THEN
5053 					warn_status := 1;
5054 			Fnd_file.put_line(FND_FILE.LOG,'Employee '|| cur_p45_emp_details_rec.works||' : PPSN and date of birth missing for employee' );
5055 		END IF;
5056 
5057 		IF l_addressline1  IS NOT NULL THEN  -- Optional
5058 			FND_FILE.PUT(FND_FILE.OUTPUT,'address1="' || l_addressline1  ||'" ');
5059 		ELSIF  l_addressline1  IS NULL and ppsn_flag = 0 THEN
5060 		-- Enter the employee details in the log
5061 			warn_status := 1;
5062 			Fnd_file.put_line(FND_FILE.LOG,'Employee '|| cur_p45_emp_details_rec.works||' : PPSN and Address Line 1 missing for employee' );
5063 		END IF;
5064 
5065 		IF l_addressline2 IS NOT NULL THEN  -- Optional
5066 			FND_FILE.PUT(FND_FILE.OUTPUT,'address2="' || l_addressline2  ||'" ');
5067 		ELSIF  l_addressline2  IS NULL and ppsn_flag = 0 THEN
5068 			-- Enter the employee details in the log
5069 			warn_status := 1;
5070 			Fnd_file.put_line(FND_FILE.LOG,'Employee '|| cur_p45_emp_details_rec.works||' : PPSN and Address Line 2 missing for employee');
5071 		END IF;
5072 
5073 		IF l_addressline3  IS NOT NULL THEN  -- Optional
5074 			FND_FILE.PUT(FND_FILE.OUTPUT,'address3="' || l_addressline3  ||'" ');
5075 		END IF;
5076 		FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5077 		-- Employment
5078 		IF p45_rec.supp_flag = 'N' THEN
5079 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <Employment ');
5080 			IF cur_p45_emp_details_rec.start1  IS NOT NULL THEN  -- Optional
5081 				FND_FILE.PUT(FND_FILE.OUTPUT,'start="' || cur_p45_emp_details_rec.start1    ||'" ');
5082 			END IF;
5083 		-- required
5084 		         /* 7291676 */
5085 			 /* 8198702 */
5086 		    --   IF (to_number(to_char(to_date(nvl(cur_p45_emp_details_rec.end1,'01/01/2009'),'dd/mm/yyyy'),'yyyy')) >= 2009) THEN   -- 7291676
5087 			FND_FILE.PUT(FND_FILE.OUTPUT,'end="'   || cur_p45_emp_details_rec.end1      ||'" ');
5088 		   --	END IF;
5089 			IF (to_number(to_char(nvl(p45_rec.date_paid,p45_rec.main_p45_date_paid),'yyyy')) <= 2011) THEN
5090 			FND_FILE.PUT(FND_FILE.OUTPUT,'unit="'  || l_employment_unit ||'" ');
5091 			END IF ; -- 13359530
5092 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5093                 ELSE
5094 		    IF (to_number(to_char(nvl(p45_rec.date_paid,p45_rec.main_p45_date_paid),'yyyy')) >= 2012) THEN
5095 			    FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <Deceased ');
5096 			  IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5097 			    FND_FILE.PUT(FND_FILE.OUTPUT,'isdeceased="true" ' );
5098 			ELSE
5099 			    FND_FILE.PUT(FND_FILE.OUTPUT,'isdeceased="false" ' );
5100 			END IF;
5101 			  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5102 		   END IF;
5103                        FND_FILE.PUT(FND_FILE.OUTPUT,'    <EmploymentSupp ');
5104 		       /* 7291676 */
5105 		       /* 8198702 */
5106 		     --  IF (to_number(to_char(to_date(nvl(cur_p45_emp_details_rec.end1,'01/01/2009'),'dd/mm/yyyy'),'yyyy')) >= 2009) THEN   -- 7291676
5107 		       FND_FILE.PUT(FND_FILE.OUTPUT,'end="'   || cur_p45_emp_details_rec.end1      ||'" ');
5108 		     -- END IF;
5109 			IF (to_number(to_char(nvl(p45_rec.date_paid,p45_rec.main_p45_date_paid),'yyyy')) <= 2011) THEN
5110 			FND_FILE.PUT(FND_FILE.OUTPUT,'unit="'  || l_employment_unit ||'" ');
5111 			END IF; -- 13359530
5112 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5113 		END IF;
5114 		-- Pay
5115 		IF p45_rec.supp_flag = 'N' THEN /* 7291676 Pay tag only for main p45, for supp its paydetails */
5116 
5117 		-- required
5118 		        FND_FILE.PUT(FND_FILE.OUTPUT,'    <Pay ');
5119 			FND_FILE.PUT(FND_FILE.OUTPUT,'freq="'      || lower(p45_rec.freq)      ||'" ');
5120 			FND_FILE.PUT(FND_FILE.OUTPUT,'period="'    || p45_rec.period    ||'" ');
5121 			FND_FILE.PUT(FND_FILE.OUTPUT,'taxcredit="' || cur_p45_ie_emp_details_rec.taxcredit ||'" ');
5122 			FND_FILE.PUT(FND_FILE.OUTPUT,'cutoff="'   || cur_p45_ie_emp_details_rec.cutoff    ||'" ');
5123 
5124 			IF p45_rec.emergency_tax = 'Y' THEN
5125 				FND_FILE.PUT(FND_FILE.OUTPUT,'emergency="' ||  'true'     ||'" ');
5126 		        ELSE
5127 				FND_FILE.PUT(FND_FILE.OUTPUT,'emergency="' ||  'false'     ||'" ');
5128 		        END IF;
5129 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />'); -- 7291676
5130 	/*	ELSE
5131 		        OPEN csr_supp_paydetails(p45_rec.assignment_id);
5132 			FETCH csr_supp_paydetails INTO csr_supp_paydetails_rec;
5133 
5134 			IF csr_supp_paydetails%FOUND THEN */
5135 			    /*   l_eit_sum:= to_number(nvl(csr_supp_paydetails_rec.pay1,0))
5136 			                   + to_number(nvl(csr_supp_paydetails_rec.pay2,0))
5137 					   + to_number(nvl(csr_supp_paydetails_rec.pay3,0))
5138 					   + to_number(nvl(csr_supp_paydetails_rec.pay4,0))
5139 					   + to_number(nvl(csr_supp_paydetails_rec.pay5,0))
5140 					   + to_number(nvl(csr_supp_paydetails_rec.allotherpay,0));  */
5141 
5142 
5143                             --   IF l_eit_sum= l_supp_totalpay THEN
5144 				/* l_eit_supp_flag:= false;
5145 				FND_FILE.PUT(FND_FILE.OUTPUT,'    <PaymentDetails ');
5146 					IF csr_supp_paydetails_rec.year1 IS NOT NULL THEN
5147 						FND_FILE.PUT(FND_FILE.OUTPUT,' year1="'      || csr_supp_paydetails_rec.year1        ||'" ');
5148 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay1="'      || csr_supp_paydetails_rec.pay1        ||'" ');
5149 
5150 					END IF;
5151 					IF csr_supp_paydetails_rec.year2 IS NOT NULL THEN
5152 					   IF csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year2 THEN
5153                                                 l_eit_supp_flag:= true;
5154                                            END IF;
5155 					   IF NOT l_eit_supp_flag THEN
5156 						FND_FILE.PUT(FND_FILE.OUTPUT,' year2="'      || csr_supp_paydetails_rec.year2        ||'" ');
5157 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay2="'      || csr_supp_paydetails_rec.pay2        ||'" ');
5158                                            END IF;
5159 					END IF;
5160 					IF csr_supp_paydetails_rec.year3 IS NOT NULL THEN
5161 					   IF( (csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year3 )
5162 					     OR ( csr_supp_paydetails_rec.year2=csr_supp_paydetails_rec.year3 )
5163 					     )THEN
5164                                                 l_eit_supp_flag:= true;
5165                                            END IF;
5166 					   IF NOT l_eit_supp_flag THEN
5167 						FND_FILE.PUT(FND_FILE.OUTPUT,' year3="'      || csr_supp_paydetails_rec.year3        ||'" ');
5168 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay3="'      || csr_supp_paydetails_rec.pay3        ||'" ');
5169 					   END IF;
5170 					END IF;
5171 					IF csr_supp_paydetails_rec.year4 IS NOT NULL THEN
5172 					   IF ((csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year4)
5173 					     OR (csr_supp_paydetails_rec.year2=csr_supp_paydetails_rec.year4)
5174 					     OR (csr_supp_paydetails_rec.year3=csr_supp_paydetails_rec.year4 )
5175 					     )THEN
5176                                                 l_eit_supp_flag:= true;
5177                                            END IF;
5178 					   IF NOT l_eit_supp_flag THEN
5179 						FND_FILE.PUT(FND_FILE.OUTPUT,' year4="'      || csr_supp_paydetails_rec.year4        ||'" ');
5180 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay4="'      || csr_supp_paydetails_rec.pay4        ||'" ');
5181                                            END IF;
5182 					END IF;
5183 					IF csr_supp_paydetails_rec.year5 IS NOT NULL THEN
5184 					   IF ((csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year5 )
5185 					     OR (csr_supp_paydetails_rec.year2=csr_supp_paydetails_rec.year5)
5186 					     OR (csr_supp_paydetails_rec.year3=csr_supp_paydetails_rec.year5)
5187 					     OR (csr_supp_paydetails_rec.year4=csr_supp_paydetails_rec.year5)
5188 					     )THEN
5189                                                 l_eit_supp_flag:= true;
5190                                            END IF;
5191 					   IF NOT l_eit_supp_flag THEN
5192 						FND_FILE.PUT(FND_FILE.OUTPUT,' year5="'      || csr_supp_paydetails_rec.year5        ||'" ');
5193 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay5="'      || csr_supp_paydetails_rec.pay5        ||'" ');
5194                                            END IF;
5195 					END IF;
5196 					IF csr_supp_paydetails_rec.allotherpay <>0 THEN
5197 						FND_FILE.PUT(FND_FILE.OUTPUT,' allotherpay="'      || csr_supp_paydetails_rec.allotherpay        ||'" ');
5198 					END IF;
5199 					 FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5200 					 IF l_eit_supp_flag THEN
5201 					 l_str_Common:=' Ensure that the Year1, Year2, Year3, Year4, and Year5 values are not the same for the assignment  '|| p45_rec.assignment_id;
5202 					Fnd_file.put_line(FND_FILE.LOG,l_str_common);
5203 					error_message := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',
5204 			                                 'IE P45 XML report completed with validation warning(s).');
5205 					END IF; */
5206                         /*      ELSE
5207 					l_str_Common:=' Ensure that the sum of Pay1, Pay2, Pay3, Pay4, Pay5 and All Other Pay is equal to the total pay in supplementary p45 for the asssingment'|| p45_rec.assignment_id;
5208 					Fnd_file.put_line(FND_FILE.LOG,l_str_common);
5209 					error_message := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',
5210 			                                 'IE P45 XML report completed with validation warning(s).');
5211 
5212                               END IF;  */
5213 
5214 		/*	ELSE
5215 			        FND_FILE.PUT(FND_FILE.OUTPUT,'    <PaymentDetails ');
5216 				FND_FILE.PUT(FND_FILE.OUTPUT,' year1="'      || to_char(p45_rec.date_paid,'yyyy')        ||'" ');
5217 				FND_FILE.PUT(FND_FILE.OUTPUT,' pay1="'      || l_supp_totalpay        ||'" ');
5218                                 FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5219 			END IF;
5220 			CLOSE csr_supp_paydetails; */
5221 		END IF;
5222 
5223 
5224 
5225 
5226 		IF p45_rec.supp_flag = 'N' THEN
5227 		-- Tax Details for Normal P45 Run
5228 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <TaxDetails ');
5229 			IF cur_p45_paye_prsi_rec.totalpay  IS NOT NULL THEN  -- Optional
5230 				FND_FILE.PUT(FND_FILE.OUTPUT,'totalpay="' || cur_p45_paye_prsi_rec.totalpay  ||'" ');
5231 			END IF;
5232 			IF cur_p45_paye_prsi_rec.totaltax  IS NOT NULL THEN  -- Optional
5233 				FND_FILE.PUT(FND_FILE.OUTPUT,'totaltax="' || cur_p45_paye_prsi_rec.totaltax  ||'" ');
5234 			END IF;
5235 			IF cur_p45_paye_prsi_rec.thispay  IS NOT NULL THEN  -- Optional
5236 				FND_FILE.PUT(FND_FILE.OUTPUT,'thispay="'  || cur_p45_paye_prsi_rec.thispay   ||'" ');
5237 			END IF;
5238 			IF cur_p45_paye_prsi_rec.thistax  IS NOT NULL THEN  -- Optional
5239 			      -- for bug 5401393, negative tax should not be displayed with - sign.
5240 				FND_FILE.PUT(FND_FILE.OUTPUT,'thistax="'  || abs(cur_p45_paye_prsi_rec.thistax)   ||'" ');
5241 				IF cur_p45_paye_prsi_rec.thistax < 0 THEN
5242 					FND_FILE.PUT(FND_FILE.OUTPUT,'thistaxrefunded="true" ');
5243 				ELSE
5244 					FND_FILE.PUT(FND_FILE.OUTPUT,'thistaxrefunded="false" ');
5245 				END IF;
5246 			END IF;
5247 
5248 			IF cur_p45_paye_prsi_rec.lumpsum  IS NOT NULL THEN  -- Optional
5249 				FND_FILE.PUT(FND_FILE.OUTPUT,'lumpsum="'  || cur_p45_paye_prsi_rec.lumpsum   ||'" ');
5250 			END IF;
5251 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5252 			-- PRSI
5253 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <PRSI ');
5254 			-- Bug  5005788
5255 			l_total_prsi := NVL(cur_p45_paye_prsi_rec.employerprsi,0) + NVL(cur_p45_paye_prsi_rec.employeeprsi,0);
5256 			FND_FILE.PUT(FND_FILE.OUTPUT,'total="'    || NVL(l_total_prsi,0)  ||'" ');
5257 			IF cur_p45_paye_prsi_rec.employeeprsi  IS NOT NULL THEN  -- Optional
5258 				FND_FILE.PUT(FND_FILE.OUTPUT,'employee="' || NVL(cur_p45_paye_prsi_rec.employeeprsi,0) ||'" ');
5259 			END IF;
5260 			FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="'    || NVL(cur_p45_paye_prsi_rec.totalweeks,0)   ||'" ');
5261 
5262 		ELSE
5263 		-- Tax Details for  P45 Supp Run
5264 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <TaxDetailsSupp '); -- 7291676
5265 			IF l_supp_totalpay  IS NOT NULL THEN  -- Optional
5266 			--	FND_FILE.PUT(FND_FILE.OUTPUT,'totalpay="' || l_supp_totalpay  ||'" '); /* 7291676 */
5267 			        FND_FILE.PUT(FND_FILE.OUTPUT,'paysupp="' || l_supp_totalpay  ||'" ');  /* 7291676 */
5268 			END IF;
5269 			IF l_supp_totaltax  IS NOT NULL THEN  -- Optional
5270 			--	FND_FILE.PUT(FND_FILE.OUTPUT,'totaltax="' || l_supp_totaltax  ||'" '); /* 7291676 */
5271 			        FND_FILE.PUT(FND_FILE.OUTPUT,'taxdsupp="' || l_supp_totaltax  ||'" '); /* 7291676 */
5272 			END IF;
5273                         /* 7291676 */
5274 			IF p45_rec.date_paid IS NOT NULL THEN
5275 
5276                                 FND_FILE.PUT(FND_FILE.OUTPUT,'dateofpayment="' || to_char(p45_rec.date_paid,'dd/mm/rrrr')  ||'" ');
5277 			END IF;
5278 
5279                         IF l_supp_totalprsi IS NOT NULL THEN
5280 
5281                                 FND_FILE.PUT(FND_FILE.OUTPUT,'totalprsi="' || l_supp_totalprsi  ||'" ');
5282 			END IF;
5283 			IF l_supp_employeeprsi IS NOT NULL THEN
5284 
5285                                 FND_FILE.PUT(FND_FILE.OUTPUT,'employeeshare="' || l_supp_employeeprsi  ||'" ');
5286 			END IF;
5287 			-- BUG 13359530
5288 		IF (to_number(to_char(nvl(p45_rec.date_paid,p45_rec.main_p45_date_paid),'yyyy')) >= 2012) THEN
5289 			IF l_supp_totaluscpay IS NOT NULL THEN
5290 
5291                                 FND_FILE.PUT(FND_FILE.OUTPUT,'uscpaysupp="' || l_supp_totaluscpay  ||'" ');
5292 			END IF;
5293 			IF l_supp_totalusc IS NOT NULL THEN
5294 
5295                                 FND_FILE.PUT(FND_FILE.OUTPUT,'usctaxdsupp="' || l_supp_totalusc  ||'" ');
5296 			END IF;
5297 END IF;
5298 -- BUG 13359530 END
5299 
5300 			/* 7291676 */
5301 			/*
5302 			IF l_supp_lumpsum  IS NOT NULL THEN  -- Optional
5303 				FND_FILE.PUT(FND_FILE.OUTPUT,'lumpsum="'  || l_supp_lumpsum   ||'" ');
5304 			END IF;
5305 			*/
5306 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5307 			-- PRSI details for P45 Supp Run
5308 			/* 7291676 commenting the PRSI section for supp p45 */
5309 			/*
5310 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <PRSI ');
5311 			FND_FILE.PUT(FND_FILE.OUTPUT,'total="'    || NVL(l_supp_totalprsi,0)    ||'" ');
5312 			IF l_supp_employeeprsi  IS NOT NULL THEN  -- Optional
5313 				FND_FILE.PUT(FND_FILE.OUTPUT,'employee="' || NVL(l_supp_employeeprsi,0) ||'" ');
5314 			END IF;
5315 			FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="'    || NVL(l_supp_totalweeks,0)  ||'" '); */
5316 		  OPEN csr_supp_paydetails(p45_rec.assignment_id);
5317 			FETCH csr_supp_paydetails INTO csr_supp_paydetails_rec;
5318 
5319 			IF csr_supp_paydetails%FOUND THEN
5320 			    /*   l_eit_sum:= to_number(nvl(csr_supp_paydetails_rec.pay1,0))
5321 			                   + to_number(nvl(csr_supp_paydetails_rec.pay2,0))
5322 					   + to_number(nvl(csr_supp_paydetails_rec.pay3,0))
5323 					   + to_number(nvl(csr_supp_paydetails_rec.pay4,0))
5324 					   + to_number(nvl(csr_supp_paydetails_rec.pay5,0))
5325 					   + to_number(nvl(csr_supp_paydetails_rec.allotherpay,0));  */
5326                                l_eit_supp_flag:= false;
5327 
5328                             --   IF l_eit_sum= l_supp_totalpay THEN
5329 					FND_FILE.PUT(FND_FILE.OUTPUT,'    <PaymentDetails ');
5330 					IF csr_supp_paydetails_rec.year1 IS NOT NULL THEN
5331 						FND_FILE.PUT(FND_FILE.OUTPUT,' year1="'      || csr_supp_paydetails_rec.year1        ||'" ');
5332 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay1="'      || csr_supp_paydetails_rec.pay1        ||'" ');
5333 
5334 					END IF;
5335 					IF csr_supp_paydetails_rec.year2 IS NOT NULL THEN
5336 					   IF csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year2 THEN
5337                                                 l_eit_supp_flag:= true;
5338                                            END IF;
5339 					   IF NOT l_eit_supp_flag THEN
5340 						FND_FILE.PUT(FND_FILE.OUTPUT,' year2="'      || csr_supp_paydetails_rec.year2        ||'" ');
5341 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay2="'      || csr_supp_paydetails_rec.pay2        ||'" ');
5342                                            END IF;
5343 					END IF;
5344 					IF csr_supp_paydetails_rec.year3 IS NOT NULL THEN
5345 					   IF( (csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year3 )
5346 					     OR ( csr_supp_paydetails_rec.year2=csr_supp_paydetails_rec.year3 )
5347 					     )THEN
5348                                                 l_eit_supp_flag:= true;
5349                                            END IF;
5350 					   IF NOT l_eit_supp_flag THEN
5351 						FND_FILE.PUT(FND_FILE.OUTPUT,' year3="'      || csr_supp_paydetails_rec.year3        ||'" ');
5352 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay3="'      || csr_supp_paydetails_rec.pay3        ||'" ');
5353 					   END IF;
5354 					END IF;
5355 					IF csr_supp_paydetails_rec.year4 IS NOT NULL THEN
5356 					   IF ((csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year4)
5357 					     OR (csr_supp_paydetails_rec.year2=csr_supp_paydetails_rec.year4)
5358 					     OR (csr_supp_paydetails_rec.year3=csr_supp_paydetails_rec.year4 )
5359 					     )THEN
5360                                                 l_eit_supp_flag:= true;
5361                                            END IF;
5362 					   IF NOT l_eit_supp_flag THEN
5363 						FND_FILE.PUT(FND_FILE.OUTPUT,' year4="'      || csr_supp_paydetails_rec.year4        ||'" ');
5364 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay4="'      || csr_supp_paydetails_rec.pay4        ||'" ');
5365                                            END IF;
5366 					END IF;
5367 					IF csr_supp_paydetails_rec.year5 IS NOT NULL THEN
5368 					   IF ((csr_supp_paydetails_rec.year1=csr_supp_paydetails_rec.year5 )
5369 					     OR (csr_supp_paydetails_rec.year2=csr_supp_paydetails_rec.year5)
5370 					     OR (csr_supp_paydetails_rec.year3=csr_supp_paydetails_rec.year5)
5371 					     OR (csr_supp_paydetails_rec.year4=csr_supp_paydetails_rec.year5)
5372 					     )THEN
5373                                                 l_eit_supp_flag:= true;
5374                                            END IF;
5375 					   IF NOT l_eit_supp_flag THEN
5376 						FND_FILE.PUT(FND_FILE.OUTPUT,' year5="'      || csr_supp_paydetails_rec.year5        ||'" ');
5377 						FND_FILE.PUT(FND_FILE.OUTPUT,' pay5="'      || csr_supp_paydetails_rec.pay5        ||'" ');
5378                                            END IF;
5379 					END IF;
5380 					IF csr_supp_paydetails_rec.allotherpay <>0 THEN
5381 						FND_FILE.PUT(FND_FILE.OUTPUT,' allotherpay="'      || csr_supp_paydetails_rec.allotherpay        ||'" ');
5382 					END IF;
5383 					 FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5384 					 IF l_eit_supp_flag THEN
5385 					 l_str_Common:=' Ensure that the Year1, Year2, Year3, Year4, and Year5 values are not the same for the assignment  '|| p45_rec.assignment_id;
5386 					Fnd_file.put_line(FND_FILE.LOG,l_str_common);
5387 					error_message := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',
5388 			                                 'IE P45 XML report completed with validation warning(s).');
5389 					END IF;
5390                         /*      ELSE
5391 					l_str_Common:=' Ensure that the sum of Pay1, Pay2, Pay3, Pay4, Pay5 and All Other Pay is equal to the total pay in supplementary p45 for the asssingment'|| p45_rec.assignment_id;
5392 					Fnd_file.put_line(FND_FILE.LOG,l_str_common);
5393 					error_message := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',
5394 			                                 'IE P45 XML report completed with validation warning(s).');
5395 
5396                               END IF;  */
5397 
5398 			ELSE
5399 			        FND_FILE.PUT(FND_FILE.OUTPUT,'    <PaymentDetails ');
5400 				FND_FILE.PUT(FND_FILE.OUTPUT,' year1="'      || to_char(p45_rec.date_paid,'yyyy')        ||'" ');
5401 				FND_FILE.PUT(FND_FILE.OUTPUT,' pay1="'      || l_supp_totalpay        ||'" ');
5402                                 FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5403 			END IF;
5404 			CLOSE csr_supp_paydetails;
5405 
5406 		END IF;
5407 		-- PRSIClass for main P45
5408       	IF cur_p45_ie_emp_details_rec.prsi_class  IS NOT NULL OR
5409 	         ( NVL(cur_p45_paye_prsi_rec.totalaweeks,0) <> 0 and p45_rec.supp_flag <> 'Y' ) OR
5410 	         ( NVL(l_supp_classA_weeks,0) <> 0 and p45_rec.supp_flag <> 'N' ) THEN
5411 		       /* 7291676 */
5412 		       IF p45_rec.supp_flag <> 'Y' THEN
5413 			 FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'>');
5414 		       END IF;
5415 			IF (NVL(cur_p45_paye_prsi_rec.totalaweeks,0) <> 0) and p45_rec.supp_flag <> 'Y'  THEN 	-- Bug 5015438
5416 				FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5417 				FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || 'A'  ||'" ');
5418 				FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="' || cur_p45_paye_prsi_rec.totalaweeks ||'" ');
5419 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5420 			END IF;
5421 			 /* 7291676 */
5422 			/*
5423 			IF (NVL(l_supp_classA_weeks,0) <> 0 ) and p45_rec.supp_flag <> 'N'  THEN
5424 				FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5425 				FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || 'A'  ||'" ');
5426 				FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="' || l_supp_classA_weeks ||'" ');
5427 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5428 			END IF; */
5429 
5430 			--14627387
5431 			vfound:= instr(cur_p45_ie_emp_details_rec.prsi_class,',',1,1);
5432 			v_prsi_class:= substr(cur_p45_ie_emp_details_rec.prsi_class,1);
5433 
5434 			IF vfound = 0 AND v_prsi_class = 'M' THEN
5435 				FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5436 				FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || 'A'  ||'" ');
5437 				FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="0" ');
5438 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5439 			END IF;
5440 			--14627387
5441 
5442 			vfrom:=1;
5443 			vto:=length(cur_p45_ie_emp_details_rec.prsi_class);
5444 			LOOP
5445 				vfound:= instr(cur_p45_ie_emp_details_rec.prsi_class,',',vfrom,1);
5446 				IF (vfound > 0 ) THEN
5447 					vto:=vfound-vfrom;
5448 					v_prsi_class:= substr(cur_p45_ie_emp_details_rec.prsi_class,vfrom,vto);
5449 					FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5450 					FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || v_prsi_class  ||'" ');
5451 					FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5452 					vfrom:=vfound+1;
5453 				ELSE
5454 					v_prsi_class:= substr(cur_p45_ie_emp_details_rec.prsi_class,vfrom);
5455 					IF v_prsi_class IS NOT NULL THEN
5456 						FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5457 						FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || v_prsi_class  ||'" ');
5458 						FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5459 					END IF;
5460 					EXIT;
5461 				END IF;
5462 			END LOOP;
5463 			/* 7291676 */
5464 			IF p45_rec.supp_flag <> 'Y' THEN
5465                 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    </PRSI>');
5466 			END IF;
5467 		ELSE
5468 		        /* 7291676 */
5469 		        IF p45_rec.supp_flag <> 'Y' THEN
5470 			        FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5471 			END IF;
5472 		END IF;
5473 		IF p45_rec.supp_flag = 'N' THEN
5474 			-- Disability
5475 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <Disability ');
5476 			FND_FILE.PUT(FND_FILE.OUTPUT,'benefit="'            || nvl(cur_p45_emp_soc_details_rec.benefit,0)            ||'" ');
5477 			IF cur_p45_emp_soc_details_rec.taxcreditreduction  <> 0 THEN  -- Optional
5478 				FND_FILE.PUT(FND_FILE.OUTPUT,'taxcreditreduction="' || cur_p45_emp_soc_details_rec.taxcreditreduction ||'" ');
5479 			END IF;
5480 			IF cur_p45_emp_soc_details_rec.cutoffreduction  <> 0  THEN  -- Optional
5481 				FND_FILE.PUT(FND_FILE.OUTPUT,'cutoffreduction="'    || cur_p45_emp_soc_details_rec.cutoffreduction    ||'" ');
5482 			END IF;
5483 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5484 			-- Deceased
5485 
5486 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <Basis ');
5487 			IF cur_p45_emp_soc_details_rec.noncumulative  IS NOT NULL THEN  -- Optional
5488 				FND_FILE.PUT(FND_FILE.OUTPUT,'noncumulative="'      || cur_p45_emp_soc_details_rec.noncumulative      ||'" ');
5489 			END IF;
5490 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5491 
5492 -- 13359530
5493 	IF (to_number(to_char(nvl(p45_rec.date_paid,p45_rec.main_p45_date_paid),'yyyy')) >= 2012) THEN
5494 	          FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <Deceased ');
5495 		IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5496 			FND_FILE.PUT(FND_FILE.OUTPUT,'isdeceased="true" ' );
5497 		ELSE
5498 			FND_FILE.PUT(FND_FILE.OUTPUT,'isdeceased="false" ' );
5499 		END IF;
5500           FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5501 	    FND_FILE.PUT(FND_FILE.OUTPUT,'    <USC ');
5502 		IF cur_p45_paye_prsi_rec.totaluscpay  IS NOT NULL THEN
5503 			FND_FILE.PUT(FND_FILE.OUTPUT,'totalpaytodate="'      || cur_p45_paye_prsi_rec.totaluscpay      ||'" ');
5504 		END IF;
5505 		IF cur_p45_paye_prsi_rec.totalusc  IS NOT NULL THEN
5506 			FND_FILE.PUT(FND_FILE.OUTPUT,'totalusctodate="'      || cur_p45_paye_prsi_rec.totalusc      ||'" ');
5507 		end if;
5508 		IF cur_p45_paye_prsi_rec.thisuscpay  IS NOT NULL THEN
5509 			FND_FILE.PUT(FND_FILE.OUTPUT,'totalpaythisemp="'      || cur_p45_paye_prsi_rec.thisuscpay      ||'" ');
5510 		END IF;
5511 		IF cur_p45_paye_prsi_rec.thisusc  IS NOT NULL THEN
5512 			FND_FILE.PUT(FND_FILE.OUTPUT,'totaluscthisemp="'      || abs(cur_p45_paye_prsi_rec.thisusc)      ||'" ');
5513 		END IF;
5514 		IF cur_p45_paye_prsi_rec.thisusc < 0 THEN
5515 			FND_FILE.PUT(FND_FILE.OUTPUT,'uscrefunded="true" ');
5516 		ELSE
5517 		      FND_FILE.PUT(FND_FILE.OUTPUT,'uscrefunded="false" ');
5518 				END IF;
5519                 FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'>');
5520 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <USCCutoff ');
5521 			FND_FILE.PUT(FND_FILE.OUTPUT,'band="'      || 'Band1'      ||'" ');
5522 			FND_FILE.PUT(FND_FILE.OUTPUT,'amount="'      || cur_p45_ie_emp_details_rec.usccutoff1      ||'" ');
5523                   FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5524 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <USCCutoff ');
5525 			FND_FILE.PUT(FND_FILE.OUTPUT,'band="'      || 'Band2'      ||'" ');
5526 			FND_FILE.PUT(FND_FILE.OUTPUT,'amount="'      || cur_p45_ie_emp_details_rec.usccutoff2      ||'" ');
5527 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5528 
5529 
5530      	FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    </USC>');
5531 
5532        FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <NextOfKin  ');
5533 	IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5534            FND_FILE.PUT(FND_FILE.OUTPUT,'name="'      || cur_p45_emp_details_rec.kin_name      ||'" ');
5535 		IF cur_p45_emp_details_rec.address_line1  IS NOT NULL THEN
5536            FND_FILE.PUT(FND_FILE.OUTPUT,'address1="'      || cur_p45_emp_details_rec.address_line1      ||'" ');
5537 		END IF;
5538 		IF cur_p45_emp_details_rec.address_line2  IS NOT NULL THEN
5539            FND_FILE.PUT(FND_FILE.OUTPUT,'address2="'      || cur_p45_emp_details_rec.address_line2      ||'" ');
5540 		END IF;
5541 		IF cur_p45_emp_details_rec.address_line3  IS NOT NULL THEN
5542            FND_FILE.PUT(FND_FILE.OUTPUT,'address3="'      || cur_p45_emp_details_rec.address_line3      ||'" ');
5543 		END IF;
5544 		IF cur_p45_emp_details_rec.address_line4  IS NOT NULL THEN
5545            FND_FILE.PUT(FND_FILE.OUTPUT,'address4="'      || cur_p45_emp_details_rec.address_line4      ||'" ');
5546 		END IF;
5547 	END IF;
5548         FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5549 	  ELSE
5550 			IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5551 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <Deceased/> ');
5552 			END IF;
5553 	  END IF;
5554 
5555 --13359530
5556 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  </P45> ');
5557 		ELSE
5558 			--  FND_FILE.PUT(FND_FILE.OUTPUT,' <Disability ');
5559 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'benefit="'            || p45_rec.benefit            ||'" ');
5560 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'taxcreditreduction="' || p45_rec.taxcreditreduction ||'" ');
5561 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'cutoffreduction="'    || p45_rec.cutoffreduction    ||'" ');
5562 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'noncumulative="'      || p45_rec.noncumulative      ||'" ');
5563 			--  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5564 			-- 13359530
5565 	IF (to_number(to_char(nvl(p45_rec.date_paid,p45_rec.main_p45_date_paid),'yyyy')) >= 2012) THEN
5566 	         /* FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <Deceased ');
5567 		IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5568 			FND_FILE.PUT(FND_FILE.OUTPUT,'isdeceased="true" ' );
5569 		ELSE
5570 			FND_FILE.PUT(FND_FILE.OUTPUT,'isdeceased="false" ' );
5571 		END IF;
5572 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />'); */
5573 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <NextOfKin  ');
5574 		IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5575 			FND_FILE.PUT(FND_FILE.OUTPUT,'name="'      || cur_p45_emp_details_rec.kin_name      ||'" ');
5576 			IF cur_p45_emp_details_rec.address_line1  IS NOT NULL THEN
5577 				FND_FILE.PUT(FND_FILE.OUTPUT,'address1="'      || cur_p45_emp_details_rec.address_line1      ||'" ');
5578 			END IF;
5579 			IF cur_p45_emp_details_rec.address_line2  IS NOT NULL THEN
5580 				FND_FILE.PUT(FND_FILE.OUTPUT,'address2="'      || cur_p45_emp_details_rec.address_line2      ||'" ');
5581 			END IF;
5582 			IF cur_p45_emp_details_rec.address_line3  IS NOT NULL THEN
5583 				FND_FILE.PUT(FND_FILE.OUTPUT,'address3="'      || cur_p45_emp_details_rec.address_line3      ||'" ');
5584 			END IF;
5585 			IF cur_p45_emp_details_rec.address_line4  IS NOT NULL THEN
5586 				FND_FILE.PUT(FND_FILE.OUTPUT,'address4="'      || cur_p45_emp_details_rec.address_line4      ||'" ');
5587 			END IF;
5588 		 END IF;
5589         FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5590 ELSE
5591 			IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5592 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <Deceased/> '); /* 7291676 */
5593 			END IF;
5594 END IF;
5595 		--13359530
5596 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  </P45Supp> ');
5597 		END IF;
5598     ELSE
5599 
5600     IF p45_rec.supp_flag = 'N' THEN
5601 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  <P45>');
5602 		ELSE
5603 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  <P45Supp>');
5604 		END IF;
5605 		-- Employee
5606 		FND_FILE.PUT(FND_FILE.OUTPUT,'    <Employee ');
5607 		IF cur_p45_emp_details_rec.ppsn  IS NOT NULL THEN  -- Optional
5608 		/* 7291676QA */
5609 		        l_ppsn_override:=null;
5610 			open csr_ppsn_override(p45_rec.assignment_id);
5611 			fetch csr_ppsn_override into l_ppsn_override;
5612 			close csr_ppsn_override;
5613 			FND_FILE.PUT(FND_FILE.OUTPUT,'ppsn="'     || nvl(l_ppsn_override, cur_p45_emp_details_rec.ppsn )      ||'" ');
5614 			ppsn_flag := 1;
5615 		ELSE
5616 			ppsn_flag := 0;
5617 		END IF;
5618 
5619 		-- required
5620 		FND_FILE.PUT(FND_FILE.OUTPUT,'surname="'    || l_sur_name   ||'" ');
5621 		FND_FILE.PUT(FND_FILE.OUTPUT,'firstnames="'  || l_first_name ||'" ');
5622 		IF cur_p45_emp_details_rec.works IS NOT NULL THEN  -- Optional
5623 			FND_FILE.PUT(FND_FILE.OUTPUT,'works="'    || replace(cur_p45_emp_details_rec.works,'-','')     ||'" ');  /* 7827732 */
5624 		END IF;
5625 
5626 		IF cur_p45_emp_details_rec.dob IS NOT NULL THEN  -- Optional
5627 			FND_FILE.PUT(FND_FILE.OUTPUT,'dob="'      || cur_p45_emp_details_rec.dob       ||'" ');
5628 		END IF;
5629 
5630 		IF l_addressline1  IS NOT NULL THEN  -- Optional
5631 			FND_FILE.PUT(FND_FILE.OUTPUT,'address1="' || l_addressline1  ||'" ');
5632 		ELSIF  l_addressline1  IS NULL and ppsn_flag = 0 THEN
5633 		-- Enter the employee details in the log
5634 			warn_status := 1;
5635 			Fnd_file.put_line(FND_FILE.LOG,'Employee '|| cur_p45_emp_details_rec.works||' : PPSN and Address Line 1 missing for employee' );
5636 		END IF;
5637 
5638 		IF l_addressline2 IS NOT NULL THEN  -- Optional
5639 			FND_FILE.PUT(FND_FILE.OUTPUT,'address2="' || l_addressline2  ||'" ');
5640 		ELSIF  l_addressline2  IS NULL and ppsn_flag = 0 THEN
5641 			-- Enter the employee details in the log
5642 			warn_status := 1;
5643 			Fnd_file.put_line(FND_FILE.LOG,'Employee '|| cur_p45_emp_details_rec.works||' : PPSN and Address Line 2 missing for employee');
5644 		END IF;
5645 
5646 		IF l_addressline3  IS NOT NULL THEN  -- Optional
5647 			FND_FILE.PUT(FND_FILE.OUTPUT,'address3="' || l_addressline3  ||'" ');
5648 		END IF;
5649 		FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5650 		-- Employment
5651 		FND_FILE.PUT(FND_FILE.OUTPUT,'    <Employment ');
5652 		IF cur_p45_emp_details_rec.start1  IS NOT NULL THEN  -- Optional
5653 			FND_FILE.PUT(FND_FILE.OUTPUT,'start="' || cur_p45_emp_details_rec.start1    ||'" ');
5654 		END IF;
5655 		-- required
5656 		FND_FILE.PUT(FND_FILE.OUTPUT,'end="'   || cur_p45_emp_details_rec.end1      ||'" ');
5657 		FND_FILE.PUT(FND_FILE.OUTPUT,'unit="'  || l_employment_unit ||'" ');
5658 		FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5659 		-- Pay
5660 		FND_FILE.PUT(FND_FILE.OUTPUT,'    <Pay ');
5661 		-- required
5662 		FND_FILE.PUT(FND_FILE.OUTPUT,'freq="'      || p45_rec.freq      ||'" ');
5663 		FND_FILE.PUT(FND_FILE.OUTPUT,'period="'    || p45_rec.period    ||'" ');
5664 		FND_FILE.PUT(FND_FILE.OUTPUT,'taxcredit="' || cur_p45_ie_emp_details_rec.taxcredit ||'" ');
5665 		FND_FILE.PUT(FND_FILE.OUTPUT,'cutoff="'   || cur_p45_ie_emp_details_rec.cutoff    ||'" ');
5666 
5667 		IF p45_rec.emergency_tax = 'Y' THEN
5668 			FND_FILE.PUT(FND_FILE.OUTPUT,'emergency="' ||  'true'     ||'" ');
5669 		ELSE
5670 			FND_FILE.PUT(FND_FILE.OUTPUT,'emergency="' ||  'false'     ||'" ');
5671 		END IF;
5672 	      FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5673 
5674 		IF p45_rec.supp_flag = 'N' THEN
5675 		-- Tax Details for Normal P45 Run
5676 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <TaxDetails ');
5677 			IF cur_p45_paye_prsi_rec.totalpay  IS NOT NULL THEN  -- Optional
5678 				FND_FILE.PUT(FND_FILE.OUTPUT,'totalpay="' || cur_p45_paye_prsi_rec.totalpay  ||'" ');
5679 			END IF;
5680 			IF cur_p45_paye_prsi_rec.totaltax  IS NOT NULL THEN  -- Optional
5681 				FND_FILE.PUT(FND_FILE.OUTPUT,'totaltax="' || cur_p45_paye_prsi_rec.totaltax  ||'" ');
5682 			END IF;
5683 			IF cur_p45_paye_prsi_rec.thispay  IS NOT NULL THEN  -- Optional
5684 				FND_FILE.PUT(FND_FILE.OUTPUT,'thispay="'  || cur_p45_paye_prsi_rec.thispay   ||'" ');
5685 			END IF;
5686 			IF cur_p45_paye_prsi_rec.thistax  IS NOT NULL THEN  -- Optional
5687 			      -- for bug 5401393, negative tax should not be displayed with - sign.
5688 				FND_FILE.PUT(FND_FILE.OUTPUT,'thistax="'  || abs(cur_p45_paye_prsi_rec.thistax)   ||'" ');
5689 				IF cur_p45_paye_prsi_rec.thistax < 0 THEN
5690 					FND_FILE.PUT(FND_FILE.OUTPUT,'thistaxrefunded="true" ');
5691 				ELSE
5692 					FND_FILE.PUT(FND_FILE.OUTPUT,'thistaxrefunded="false" ');
5693 				END IF;
5694 			END IF;
5695 
5696 			IF cur_p45_paye_prsi_rec.lumpsum  IS NOT NULL THEN  -- Optional
5697 				FND_FILE.PUT(FND_FILE.OUTPUT,'lumpsum="'  || cur_p45_paye_prsi_rec.lumpsum   ||'" ');
5698 			END IF;
5699 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5700 			-- PRSI
5701 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <PRSI ');
5702 			-- Bug  5005788
5703 			l_total_prsi := NVL(cur_p45_paye_prsi_rec.employerprsi,0) + NVL(cur_p45_paye_prsi_rec.employeeprsi,0);
5704 			FND_FILE.PUT(FND_FILE.OUTPUT,'total="'    || NVL(l_total_prsi,0)  ||'" ');
5705 			IF cur_p45_paye_prsi_rec.employeeprsi  IS NOT NULL THEN  -- Optional
5706 				FND_FILE.PUT(FND_FILE.OUTPUT,'employee="' || NVL(cur_p45_paye_prsi_rec.employeeprsi,0) ||'" ');
5707 			END IF;
5708 			FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="'    || NVL(cur_p45_paye_prsi_rec.totalweeks,0)   ||'" ');
5709 		ELSE
5710 		-- Tax Details for  P45 Supp Run
5711 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <TaxDetails ');
5712 			IF l_supp_totalpay  IS NOT NULL THEN  -- Optional
5713 				FND_FILE.PUT(FND_FILE.OUTPUT,'totalpay="' || l_supp_totalpay  ||'" ');
5714 			END IF;
5715 			IF l_supp_totaltax  IS NOT NULL THEN  -- Optional
5716 				FND_FILE.PUT(FND_FILE.OUTPUT,'totaltax="' || l_supp_totaltax  ||'" ');
5717 			END IF;
5718 			IF l_supp_lumpsum  IS NOT NULL THEN  -- Optional
5719 				FND_FILE.PUT(FND_FILE.OUTPUT,'lumpsum="'  || l_supp_lumpsum   ||'" ');
5720 			END IF;
5721 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5722 			-- PRSI details for P45 Supp Run
5723 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <PRSI ');
5724 			FND_FILE.PUT(FND_FILE.OUTPUT,'total="'    || NVL(l_supp_totalprsi,0)    ||'" ');
5725 			IF l_supp_employeeprsi  IS NOT NULL THEN  -- Optional
5726 				FND_FILE.PUT(FND_FILE.OUTPUT,'employee="' || NVL(l_supp_employeeprsi,0) ||'" ');
5727 			END IF;
5728 			FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="'    || NVL(l_supp_totalweeks,0)  ||'" ');
5729 		END IF;
5730 		-- PRSIClass for main P45
5731       	IF cur_p45_ie_emp_details_rec.prsi_class  IS NOT NULL OR
5732 	         ( NVL(cur_p45_paye_prsi_rec.totalaweeks,0) <> 0 and p45_rec.supp_flag <> 'Y' ) OR
5733 	         ( NVL(l_supp_classA_weeks,0) <> 0 and p45_rec.supp_flag <> 'N' ) THEN
5734 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'>');
5735 			IF (NVL(cur_p45_paye_prsi_rec.totalaweeks,0) <> 0) and p45_rec.supp_flag <> 'Y'  THEN 	-- Bug 5015438
5736 				FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5737 				FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || 'A'  ||'" ');
5738 				FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="' || cur_p45_paye_prsi_rec.totalaweeks ||'" ');
5739 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5740 			END IF;
5741 			IF (NVL(l_supp_classA_weeks,0) <> 0 ) and p45_rec.supp_flag <> 'N'  THEN
5742 				FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5743 				FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || 'A'  ||'" ');
5744 				FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="' || l_supp_classA_weeks ||'" ');
5745 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5746 			END IF;
5747 
5748 			--14627387
5749 			vfound:= instr(cur_p45_ie_emp_details_rec.prsi_class,',',1,1);
5750 			v_prsi_class:= substr(cur_p45_ie_emp_details_rec.prsi_class,1);
5751 
5752 			IF vfound = 0 AND v_prsi_class = 'M' THEN
5753 				FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5754 				FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || 'A'  ||'" ');
5755 				FND_FILE.PUT(FND_FILE.OUTPUT,'weeks="0" ');
5756 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5757 			END IF;
5758 			--14627387
5759 
5760 			vfrom:=1;
5761 			vto:=length(cur_p45_ie_emp_details_rec.prsi_class);
5762 			LOOP
5763 				vfound:= instr(cur_p45_ie_emp_details_rec.prsi_class,',',vfrom,1);
5764 				IF (vfound > 0 ) THEN
5765 					vto:=vfound-vfrom;
5766 					v_prsi_class:= substr(cur_p45_ie_emp_details_rec.prsi_class,vfrom,vto);
5767 					FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5768 					FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || v_prsi_class  ||'" ');
5769 					FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5770 					vfrom:=vfound+1;
5771 				ELSE
5772 					v_prsi_class:= substr(cur_p45_ie_emp_details_rec.prsi_class,vfrom);
5773 					IF v_prsi_class IS NOT NULL THEN
5774 						FND_FILE.PUT(FND_FILE.OUTPUT,'      <PRSIClass ');
5775 						FND_FILE.PUT(FND_FILE.OUTPUT,'class="' || v_prsi_class  ||'" ');
5776 						FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5777 					END IF;
5778 					EXIT;
5779 				END IF;
5780 			END LOOP;
5781 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    </PRSI>');
5782 		ELSE
5783 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'/>');
5784 		END IF;
5785 		IF p45_rec.supp_flag = 'N' THEN
5786 			-- Disability
5787 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <Disability ');
5788 			FND_FILE.PUT(FND_FILE.OUTPUT,'benefit="'            || nvl(cur_p45_emp_soc_details_rec.benefit,0)            ||'" ');
5789 			IF cur_p45_emp_soc_details_rec.taxcreditreduction  IS NOT NULL THEN  -- Optional
5790 				FND_FILE.PUT(FND_FILE.OUTPUT,'taxcreditreduction="' || cur_p45_emp_soc_details_rec.taxcreditreduction ||'" ');
5791 			END IF;
5792 			IF cur_p45_emp_soc_details_rec.cutoffreduction  IS NOT NULL THEN  -- Optional
5793 				FND_FILE.PUT(FND_FILE.OUTPUT,'cutoffreduction="'    || cur_p45_emp_soc_details_rec.cutoffreduction    ||'" ');
5794 			END IF;
5795 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5796 			-- Deceased
5797 
5798 			FND_FILE.PUT(FND_FILE.OUTPUT,'    <Basis ');
5799 			IF cur_p45_emp_soc_details_rec.noncumulative  IS NOT NULL THEN  -- Optional
5800 				FND_FILE.PUT(FND_FILE.OUTPUT,'noncumulative="'      || cur_p45_emp_soc_details_rec.noncumulative      ||'" ');
5801 			END IF;
5802 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5803 
5804 			IF cur_p45_emp_details_rec.deceased = 'Y' THEN
5805 				FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'    <Deceased/> ');
5806 			END IF;
5807 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  </P45> ');
5808 		ELSE
5809 			--  FND_FILE.PUT(FND_FILE.OUTPUT,' <Disability ');
5810 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'benefit="'            || p45_rec.benefit            ||'" ');
5811 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'taxcreditreduction="' || p45_rec.taxcreditreduction ||'" ');
5812 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'cutoffreduction="'    || p45_rec.cutoffreduction    ||'" ');
5813 			--  FND_FILE.PUT(FND_FILE.OUTPUT,'noncumulative="'      || p45_rec.noncumulative      ||'" ');
5814 			--  FND_FILE.PUT_LINE(FND_FILE.OUTPUT,' />');
5815 			FND_FILE.PUT_LINE(FND_FILE.OUTPUT,'  </P45Supp> ');
5816 		END IF;
5817   END IF; -- 7291676
5818 	END IF;
5819 END LOOP;
5820 IF once_per_run = 'Y' THEN
5821 	-- End of ROOT P45File ELEMENT
5822 	FND_FILE.PUT_LINE(FND_FILE.OUTPUT,l_root_end_tag);
5823 END IF;
5824 
5825 
5826 IF warn_status =1 then
5827 	l_conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS
5828 		 (
5829 		  status => 'WARNING',
5830 		  message => 'PPSN and Address missing. Please check the log file for more details.'
5831 		 );
5832 
5833 END IF;
5834 
5835 END generate_xml;
5836 
5837 END pay_ie_p45_archive;
5838