DBA Data[Home] [Help]

PACKAGE BODY: APPS.PAY_AU_PAYMENT_SUMMARY

Source


1 package body pay_au_payment_summary as
2 /* $Header: pyaupsp.pkb 120.93.12020000.5 2012/12/07 03:07:55 skshin ship $*/
3 /*
4 *** ------------------------------------------------------------------------+
5 *** Program:     pay_au_payment_summary (Package Body)
6 ***
7 *** Change History
8 ***
9 *** Date       Changed By  Version  Description of Change
10 *** ---------  ----------  -------  ----------------------------------------+
11 *** 01 MAR 01  rbsinha     1.0      Initial version
12 *** 11 MAY 01  rbsinha     1.19     Bug #1768813 - to retrieve the value  of
13                                     fringe_benefits calc_all_balances (effective_date,assignment_id,defined_balance_id)
14                                     is being used instead of calc_all_balances(assignment_action_id,defined_balance_id)
15 *** 11 MAY 01  rbsinha     1.19     Bug #1764010 - process_assignment cursor modified
16                                     Locked action id for payment summary report changed to
17                                     Locking action id
18 *** 11 MAY 01  rbsinha     1.21     Bug #1763290 -Modified employee_details
19                                     cursor to archive current and terminated
20                                     employees.Removed sort items from
21                                     archive_etp_details.
22                                     Bug #1763245 -Modified employer_details
23                                     and supplier_details
24 *** 22 MAY     rbsinha     1.22     Bug No : 17164017 and Bug No : 1789886
25                                     Added archive item : X_UNION_NAMES and
26                                     X_PAYMENT_SUMMARY_SIGNATORY
27 *** 17 JUL     rbsinha     1.25     Bug No : 1746093 - Added the fields
28                                     X_ETP_EMPLOYEE_PAYMENT_DATE and
29                                     X_ETP_ON_TERMINATION_PAID . These fields
30                                     are populated in the procedure
31 *** 15 Sep     apunekar    1.26     Made changes for Bug1956018.
32 *** 18 Sep     rbsinha     1.27     Made changes for Bug1951539 and Bug1903647.
33 *** 03 Oct     apunekar    1.28     Made changes  for Bug2021219.
34 *** 11 Oct     apunekar    1.28     Made changes  for Bug1955993
35 *** 29 Oct 01  shoskatt    1.39     The archived value of x_post_jun_83_untaxed_asg_ytd
36 ***                                 should not contain lumpsum D Amount(Bug #2075782)
37 *** 31 Oct 01  shoskatt    1.40     If the Termination Date of the employee is in the
38 ***                                 next financial year, then the employee type should
39 ***                                 be current(Bug# 1973978)
40 *** 17 Apr 02 srrajago     1.41     Performance Issue (Bug No : 2263587)
41 *** 17 Apr 02 srrajago     1.42     Included checkfile command
42 *** 22 Apr 02 srrajago     1.43     In Cursor employee_detail, hr_locations_all has been replaced by hr_locations (Bug No : 2263587).
43 *** 06 May 02 vgsriniv     1.44     Cursor get_allowance_balances modified Bug 2359428, 2359423
44 *** 03 Jun 02 Ragovind     1.45     Cursor get_allowance_balances modified for Bug#2398315 to fix the bug 2359428 and to make compatible with both 8i and 9i database versions.
45 *** 15 Jul 02 shoskatt     1.46     For retrieving the ETP Payment values, get the maximum assignment
46 ***                                 action id for which ETP values have been processed  (Bug #2459527)
47 *** 16 Jul 02 shoskatt     1.47     Removed some redundant cursors,Introduced date check for
48 ***                                 c_get_max_ass_act_id cursor,Also fixed Bug #2448446
49 *** 24 Jul 02 shoskatt     1.48     Improved performance by tuning cursors process_assignments,
50 ***                                 c_archive_fbt_info and c_archive_info(Bug #2454595)
51 *** 08 Aug 02 shoskatt     1.49     Improved performance by tuning cursors process_assignments
52 ***                                 and etp_code (Bug #2501105)
53 *** 09 Aug 02 Ragovind     1.50     Changed the cursor archive_employee cursor to fetch the data for Terminated-Rehired
54 ***                                 Employees (Bug# 2448441).
55 *** 16 Aug 02 vgsriniv     1.52     Modified employee_details cursor. Removed to_date for the date parameter
56 ***                                 in the select statement for employee type (Bug #2512431)
57 *** 26 Sep 02 kaverma      1.53     Modified cursor employer_details and etp_code. Also
58 ***                                 introduced a cursor balance_exists (Bug No 2581436 )
59 *** 24 Oct 02 srrajago     1.54     Modified the cursor process_assignments (Bug No : 2574186)
60 *** 28 Oct 02 srrajago     1.55     Included action_type 'I' for 'Balance Initialization' in cursor
61 ***                                 process_assignments (Bug No : 2574186)
62 *** 01 Nov 02 shoskatt     1.56     Changed the etp_code to check for action type and added the function
63 ***                                 to check for the existence of Lumpsum C value. balance_exists cursor
64 ***                                 has been changed. (Bug #2646912)
65 *** 18 Nov 02 Ragovind     1.57     Modified the cursor Get_Allowance_Balances for Performance Improvement for Bug#2665475.
66 *** 01 Dec 02 Apunekar     1.58     Modified cursor employee_details for Bug#2689175
67 *** 01 Dec 02 Apunekar     1.59     Added nocopy
68 *** 16 Jan 03 Apunekar     1.61     Modified process_assignments,employee_details cursor and
69 ***                                 archive_etp_payment_details procedure (Bug 2574186)
70 *** 30 Jan 03 Kaverma      1.62     Modified 'process_assignments' cursor 'not exists' clause (Bug 2777142)
71 *** 30 Jan 03 Apunekar     1.63     Modified cursors employee_details and  etp_details for bug 2774577
72 *** 18 Feb 03 Ragvoind     1.64     Add Join for Period of service ID to the Employee_details Cursor. Bug#2786146
73 *** 25 Feb 03 Ragovind     1.65     Modified the process_assignments cursor to avoid MERGE-JOIN-CARTESIAN Bug#2786835.
74 *** 04 Mar 03 Kaverma      1.66     Modified procedure 'archive_prepost_details' to use Lump Sum C Payment balance
75 ***                                 to get pre and post 83 values (Bug No : 2826802)
76 *** 24 Mar 03 Kaverma      1.67     Modified cursor etp_code (Bug No : 2856638)
77 *** 02 May 03 Nanuradh     1.68     Modified the proceduce assignment_action_code by adding temporay check.
78 ***                                 Excluded processing all the employees, where lump sum E payment exists
79 ***                                 after 01-JUL-2003 (Bug: 2822446)
80 *** 02 May 03 Nanuradh     1.69     Modified the cursor c_employee_number to improve the performance.(Bug: 2822446)
81 *** 05 May 03 Nanuradh     1.70     Modified the cursor c_employee_number.
82 *** 05 May 03 Nanuradh     1.71     Corrected the message to be printed in the out file.
83 *** 05 May 03 Apunekar     1.72     Bug2855658 - Fixed for Retro Allowances
84 *** 05 May 03 Apunekar     1.73     Bug2855658 - Fixed for Performance
85 *** 15 May 03 Ragovind     1.74     Bug2819479 - ETP Pre/Post Enhancement.
86 *** 21 May 03 Apunekar     1.75     Bug2968127 - Cleared PL/SQL allowances table after processing.
87 *** 27 May 03 Apunekar     1.76     Bug2977533 - Ordered addresses by date_from.
88 *** 27-Jun-03 Hnainani     11591.5  Bug#3019374  Removed FBT check from Main Query
89 *** 28-Jun-03 SRussell     11591.7  Bug#3019374  Replaced process_Assignments
90                                     cursor after discussion with core.
91 *** 28-Jun-03 SRussell     1.83     Copied branched version 11591.7 into main code.
92 *** 28-Jun-03 SRussell     11592.2  Branched code. Put hints in
93                                     process_assignments cursor.
94 *** 04-Jul-03 Apunekar     11592.3  Added check to get latest person record in financial year in process_assignments
95 *** 04-Jul-03 Apunekar     115.84   Bug3019374 Changed process_assignments,etp_code,Get_retro_Entry_ids cursors , included branched version changes in mainline code.
96 *** 16-Jul-03 Apunekar     115.85   Includes fix for 3048724
97 *** 17-Jul-03 Apunekar     115.86   Bug3019374 Modified Cursor employee_details for performance fix.
98 *** 23 Jul 03 Nanuradh     115.87   Bug#2984390 - Added an extra parameter to the function call etp_prepost_ratios - ETP Pre/post Enhancement
99 *** 29 Jul 03 Nanuradh     115.88   Bug#2881272 -  Modified the proceduce assignment_action_code by removing the temporary check.
100 ***                                 Included processing of all the employees, where Lump sum E payment exists after 01-JUL-2003.
101 *** 28-Jul-03 Apunekar     115.90   Bug3073082 - Cursor employee_details removed date formatting in decode.
102 *** 29-Jul-03 Nanuradh     115.91   Bug#2881272 - Deleted the commented code in process_assignments function.
103 *** 28-Jul-03 Apunekar     115.92   Bug#3075153 - Modified employee_details
104 *** 04-Aug-03 Apunekar     115.93   Bug#3077528 - Backed out previous changes made
105 *** 06-Aug-03 Apunekar     115.94   Bug#3043049 - Used secured views in range_code and process_assignments for security
106 *** 06-Aug-03 Apunekar     115.95   Bug#3043049 - Used secured views in process_assignments_only for security
107 *** 13-AUG-03 Nanuradh     115.96   Bug#3095919 - If single lump sum E payment is less than $400 then the payment is included in gross earnings
108 ***                                 otherwise it is included in Lump sum E payment.
109 *** 21-AUG-03 punmehta     115.97   Bug#3095919 - Modified the Cursor c_get_pay_earned_date to fetch effective_date instead of date_earned
110 *** 22-AUG-03 punmehta     115.98   Bug#3095919 - Modified the Cursor name c_get_pay_earned_date
111 ***                                 to c_get_pay_effective_date and variable name of date_earned to effective_date
112 *** 22-Nov-03 punmehta    115.99    Bug#3263659 - Modified employee_details and archive code to check for the termination date
113 ***                     and added check for 'g_debug' before tracing for performance
114 *** 11-Dec-03 punmehta    115.100   Bug#3263659 - Archive_code , before calling archive_etp_code put a check for termination date
115 *** 06-Feb-04 punmehta    115.101   Bug#3245909 - Modified c_get_pay_effective_date cursor to fetch Dates for only master assignment action.
116 *** 06-Feb-04 punmehta    115.102   Bug#3245909 - Removed unwanted code.
117 *** 10-Feb-04 punmehta    115.103   Bug#3098353 - Archived a new flag which is false if all the balances are zero.
118 *** 11-Feb-04 punmehta    115.104   Bug#3098353 - Modified IF caluse for setting employee_end date.
119 *** 11-Feb-04 punmehta    115.105   Bug#3098353 - Renamed variables
120 *** 12-Feb-04 punmehta    115.106   Bug#3132178 - New procedure for calling magtape process.
121 *** 13-Feb-04 punmehta    115.107   Bug#3132178 - Coding Standards, changed SELECT to cursor
122 *** 18-Feb-04 jkarouza    115.08    Bug#3172963 - Use of BBR to retrieve balances wherever possible.
123 *** 02-Apr-04 punmehta    115.109   Bug#3549553 - Modified Union cursor and FBT cursor for balances.
124 *** 24-Apr-04 puchil      115.110   Bug#3586388 - Changed the cursor etp_code and removed gscc warnings.
125 *** 07-May-04 avenkatk    115.111   Bug#3580487 - Modified Union Cursor,removed call to hr_aubal.calc_all_balances.
126 *** 28-MAY-04 punmehta    115.112   Bug#3642409 - Added new index INDEX(a per_assignments_f_N12) to cursor process_assignments.
127 *** 28-MAY-04 punmehta    115.112   Bug#3642409 - Removed Rownum from cursor process_assignments.
128 *** 03-JUN-04 abhkumar    115.113   Bug#3661230 - Modified the process assignment cursor to take all the employees for archiving purpose.
129 *** 21-JUN-04 srrajago    115.118   Bug#3701869 - Modified the cursor csr_get_bbr_aseq to fetch max action_sequence instead of
130 ***                                               assignment_action_id. Introduced cursor csr_get_bbr_assignment_action to pass the
131 ***                                               correct assignment_action_id based on action_sequence to pay_balance_pkg.Also handled
132 ***                                               the parameter Last Year Termination value setting if it is not enabled.
133 *** 21-JUN-04 srrajago    115.119   Bug#3701869 - Modified cursors 'csr_get_bbr_aseq' and 'csr_get_bbr_asg_actions'. Handled the parameter
134 ***                                               Last Year Termination value setting if it is not enabled in another place which was
135 ***                                               missed in the previous fix.
136 *** 25-JUN-04 srrajago    115.120   Bug#3603495 - Performance Fix - Modified cursors 'c_get_pay_effective_date' and 'etp_BA_or_BI'.
137 ***                                               Introduced per_assignments_f table and its joins.
138 *** 03-JUL-04 srrajago    115.122   Bug#3743010 - Reverted back the fix in the previous version fixed for Bug: 3728357. This is same as
139 ***                                               the version 115.20
140 *** 05-JUL-04 srrajago    115.123   Bug#3603495 - Performance Fix - Modified cursor 'c_get_pay_effective_date'.
141 *** 05-JUL-04 punmehta    115.124   Bug#3744930 - Modified for Re-hire
142 *** 05-JUL-04 punmehta    115.125   Bug#3755305 - Modified to get the action_id based on max action_squence
143 *** 09 Aug 04 abhkumar    115.126   Bug2610141  - Legal Employer Enhancement
144 *** 12 Aug 04 abhkumar    115.127   Bug2610141  - Modified code so that legal employer end date is not archived twice.
145 *** 06 Oct 04 avenkatk    115.128   Bug#3815301 - Modified cursor process_assignments and process_assignments_only for Payroll Updation.
146 *** 12 Oct 04 avenkatk    115.129   Bug#3815301 - Modified cursor process_assignments and process_assignments_only for better performance.
147 *** 09 Dec 04 ksingla     115.130   Bug#3937976 - Archived a new flag which is true if an employee ,current or terminated in the current year,
148                                                   has zero balances in the current year.
149 *** 14 Dec 04 ksingla     115.131   Bug#3937976 - Removed redundant code,defaulted the parameter l_curr_term_0_bal_flag to 'NO'.
150 *** 15 Dec 04 hnainani    115.132   Bug#4015082 - Changes to archive Workplace Giving Deductions
151 *** 23 Dec 04 abhkumar    115.133   Bug#4063321 - Fixed issues related to Terminated employees, allowance details and LE dates
152 *** 24 Dec 04 abhkumar    115.134   Bug#4063321 - Removed GSCC errors
153 *** 30 Dec 04 ksingla     115.135   Bug#4000955 - Modified subquery of process_assignments and process_assignments_only not to archive employees for
154                                                   any legal employer if Manual PS is issued for 'ALL' legal employers or without any legal employer.
155 *** 30 Dec 04 avenkatk    115.136   Bug#3899641 - Functional Dependancy Comment Added.
156 *** 07 Feb 05 ksingla     115.137   Bug#4161460   Modified the cursor get_allowance_balances
157 *** 12 Feb 05 abhargav    115.138   bug#4174037   Modified the cursor get_allowance_balances to avoid the unnecessary get_value() call.
158 *** 17 Feb 05 abhkumar    115.139   Bug#4161460   Rolled back changes made in 115.137
159 *** 16 Mar 05 ksingla     115.140   Bug#4177679   Modified the subquery of employee_details to archive correct employee le_start_date
160                                                   when legal employer is changed .
161 *** 17 Mar 05 ksingla     115.141   Bug#4177679   Modified the subquery of employee_details to archive correct employee le_start_date
162                                                   when person details are modified .
163 *** 31 Mar 05 ksingla     115.142   Bug#4177679   Modified the subquery of employee_details to archive correct employee le_end_date
164                                                   when "Leave Loading" segment is modified.
165 *** 11 Apr 05 ksingla     115.143   Bug#4278361   Modified the cursor etp_paid. Added a new table pay_payrolls_f for performance issues.
166 *** 12 Apr 05 avenkatk    115.144   Bug#4299506   Modified Cursor employee_details - Sub Query modified to archive employee details for employees terminated in previous year.
167 *** 13 Apr 05 ksingla     115.145   Bug#4278379   Modified the cursor get_retro_entry_ids for performance.
168 *** 14 Apr 05 ksingla     115.146   Bug#4278379   Rolled back the changes done to cursor get_retro_entry_ids.
169 *** 18 Apr 05 ksingla     115.147   Bug#4278299   Modified the cursor get_allowance_balances for performance.
170 *** 19 Apr 05 ksingla     115.148   Bug#4281290   Modified the cursor etp_code for performance.
171 *** 20 Apr 05 ksingla     115.149   Bug#4177679   Modified for etp employee start date.
172 *** 25 Apr 05 ksingla     115.150   Bug#4278299   Rolled back the changes done in version 115.147.
173 *** 05 May 05 abhkumar    115.151   Bug#4377367   Added join in the cursor process_assignments to archive the end-dated employees.
174 *** 24 May 05 abhargav    115.152   Bug#4363057   Changes due to Retro Tax enhancement.
175 *** 24 May 05 abhargav    115.152   Bug#4387183   Modified file to to archive employee details for FBT employees.
176                                                   Included the fix for Bug# 4375020
177                                                   and  included action_type 'V' in cursor csr_get_dates
178 *** 20 Jul 05 abhkumar    115.153   Bug#4418107   Modified call to pay_au_paye_ff.get_retro_period in adjust_retro_allowances
179 *** 08-AUG-05 hnainani    115.154   Bug#3660322   Added Quotes around Extra Information Query (-999) to not erro out for Character values
180 *** 02-OCT-05 abhkumar    115.156   Bug#4653934   Modified assignment action code to pick those employees who do have payroll attached
181                                                   at start of the financial year but not at the end of financial year.
182 *** 15-Nov-05 avenkatk    115.157   Bug#4738470   Change cursor for Maximum assignment_action_id.
183 *** 02-DEC-05 abhkumar    115.158   Bug#4701566   Modified the cursor get_allowance_balances to get allowance value for end-dated
184                                                   employees and also improve the performance of the query.
185 *** 06-DEC-05 abhkumar    115.159   Bug#4863149   Modified the code to raise error message when there is no defined balance id for the allowance balance.
186 *** 06-Dec-05 ksingla     115.160   Bug#4866415   Removed round for l_pre01jul1983_value and l_post30jun1983_value
187 *** 06-DEC095 avenkatk    115.161   Bug#4866934   Initialized balance values to 0 for FBT Employee
188 *** 04-Jan-06 ksingla     115.162   Bug#4925650   Modified cursor c_get_effective_date to resolve performance issues.
189 *** 04-Jan-06 ksingla     115.163   Bug#4926521   Modified cursor process_assignments to resolve performance issues.
190 *** 03-Mar-06 abhkumar    115.164   Bug#5075662   Modified for etp employee start date.
191 *** 16-Mar-06 ksingla     115.165   Bug#5099419   Modified to round off correctly.
192 *** 21-Mar-06 ksingla     115.166   Bug#5099419   Removed changes done for bug 4926521
193 *** 20-Jun-06 ksingla     115.167   Bug#5333143   Add_months included to fetch FBT_RATE and MEDICARE_LEVY
194 *** 29-Jun-06 avenkatk    115.168   Bug#5364017   Added check for "Generic" Address Style in Employee Address.
195 *** 03-Jul-06 avenkatk    115.169   Bug#5367061   ETP Start Date with be ETP Service Date entered else Hire Date. Backed out Fix 4177679.
196 *** 11-Aug-06 hnainani    115.170   Bug#5395393   Modified the v_lst_year_start variable to be assigned to financial year start
197 ***                                               instead of FBT year Start. This was done to keep it consistent with the
198 ***                                               Archive_code procedure and End Of Year Reconciliation Reports.
199 *** 01-Sep-06 sclarke     115.172   Bug#4925547   Altered archive of allowances and unions to support 2006/2007 layout.
200 *** 05-Oct-06 priupadh    115.174                 The file is now dual maintained ,R12 version will be in Sync.
201 *** 06-Oct-06 hnainani    115.179   Bug# 5377624  Modified cursor c_get_pay_Effective_date to link Time Period Id with Date_Earned
202 ***                                               to get the assignment action id
203 ***
204 *** 10-Oct-06 abhargav    115.180   Bug#4925547   Added bug references for changes done under Bug#4925547.
205 *** 17-Nov-06 abhargav    115.181   Bug#5591993   Modified cursor CSR_UNIONS_2006 to avoid MJC.
206 *** 20-Nov-06 sclarke     115.182   Bug#5666937   Enabled the 'order by' of the where clause of get_allowance_balances
207 ***                                               cursor.
208 *** 28-Nov-06 sclarke     115.183   Bug#5679568   Procedure archive_2006_unions -
209 ***                                               Handled case where both old and new unions exists but number of new unions is < 4
210 *** 19-Dec-06 ksingla     115.184   Bug#5708255   Added code to get value of global FBT_THRESHOLD
211 *** 27-Dec-06 ksingla      115.185  Bug#5708255   Added to_number to all occurrences of  g_fbt_threshold
212 *** 8-Jan-06 ksingla       115.186  Bug#5743196   Added nvl to cursor Get_Retro_allowances
213 *** 23-Mar-07 ksingla      115.187  Bug#5371102   Modified for performance fixes
214 *** 26-Apr-07 sbaburao     115.188  Bug#5846278   Modified the function adjust_retro_allowance for Enhanced Retropay
215 *** 27-Apr-07 sbaburao     115.189  Bug#5846278   Modified the check for cursor get_legislation_rule to default the value of l_adv_retro_flag to 'N'
216 *** 10-May-07 priupadh     115.190  Bug#5956223   Modified function archive_etp_details added archive items X_TRANSITIONAL_ETP and X_PART_OF_PREVIOUS_ETP
217 *** 24-May-07 priupadh     115.191  Bug#6069614   Removed the if conditions which checks the death benefit type other then 'Dependent'
218 *** 01-Jun-07 tbakashi     115.192  Bug#6086060   Added trunc function for union values and allowance values.
219 *** 09-Jun-07 tbakashi     115.193  Bug#6086060   Added trunc in archive_limited_values and removed in archive_allowance_details
220 *** 10-Jun-07 priupadh     115.194  Bug#6112527   Added the changes removed in Bug#6069614 , with condition that for death benefit type Dependent
221 ***                                               only archive if Fin Year is 2007/2008 or greater .
222 *** 14-Jun-07 tbakashi     115.195 Bug#6086060    Removing the trunc's added as they create inconsistency of value in data file.
223 *** 23-Aug-07 avenkatk     115.196  Bug#5371102   Modified Cursor csr_get_dates for performance.
224 *** 03-Sep-07 priupadh     115.197 Bug#6192381    For multiple ETP enh ,modified initialization_code,archive_code,archive_prepost_details
225 ***                                               archive_etp_details and added procedure adjust_old_etp_values
226 *** 06-Sep-07 priupadh     115.198 Bug#6192381    Modified the Balance Names for Invalidity Component,removed commented code from archive_prepost_details
227 *** 07-Sep-07 priupadh     115.199 Bug#6192381    Removed multiple comments tab in one line to avoid chksql error
228 *** 07-Jan-08 avenkatk     115.200 Bug#6470581    Added Changes for supporting Amended payment summaries
229 *** 23-Jan-08 avenkatk     115.201 Bug#6740581    Resolved GSCC Errors
230 *** 30-May-08 priupadh     115.202 Bug#7135544    Added NVL (N) to variables lv_transitional_flag and lv_part_prev_etp_flag
231 ***                                               if Null values are returned from cursor.
232 *** 18-Jun-08 avenkatk     115.203 Bug#7138494    Added Changes for RANGE_PERSON_ID
233 *** 26-Jun-08 priupadh     115.204 Bug#7171534    Added t_allowance_balance.count > 0 in adjust_retro_allowances
234 *** 07-Jul-08 avenkatk     115.205 Bug#7234263    Modified archive_employee_details for End assignments
235 *** 16-Jul-08 avenkatk     115.206 Bug#7242551    Modified archive_employee_details for fetching correct TFN
236 *** 25-Nov-08 skshin       115.207 Bug#7571001    Added changes in archive_allowance_detils and adjust_retro_allowances for Balance Attribute reporting
237 *** 25-Nov-08 skshin       115.208 Bug#7571001    Modified Cursor Get_retro_Entry_ids and Cursor Get_Retro_allowances to avoid duplcate joins
238 *** 16-Apr-09 skshin       115.209 Bug#8423565    Modified archive_union_name to display Miscellaneous when 'Balance Initialization 4' is used.
239 *** 28-Apr-09 pmatamsr     115.210 Bug#8441044    Cursor c_get_pay_effective_date is modified to consider Lump Sum E payments for payment summary gross calculation
240 ***                                               for action types 'B' and 'I'.
241 *** 13-May-09 pmatamsr     115.211 Bug#8315198    Modified initialization_code,archive_etp_details,archive_employee_details,archive_balance_details and
242 ***                                               archive_code procedures as part of FY2009 Payment Summary changes.
243 *** 18-May-09 pmatamsr     115.212 Bug#8315198    Modified archive_etp_details,archive_employee_details and archive_code procedures such that the new DB items
244 ***                                               added for FY2009 Payment Summary enhancement are archived only if FY is 2009 or greater.
245 *** 23-Jun-09 pmatamsr     115.213 Bug#8587013    Modified initialization_code,archive_balance_details and archive_code procedures to remove references to
246 ***                                               Other Income balance.
247 *** 03-Aug-09 dduvvuri     115.214 Bug#5008855    Modified cursors etp_details and csr_union_fees to add a join on per_periods_of _service
248 *** 07-Sep-09 pmatamsr     115.215 Bug#8769345    Modified initialization_code and archive_prepost_details such that the new balances addeed are used
249 **                                                during the pre 83 and post 83 components archival process.
250 *** 19-Nov-09 skshin       115.218 Bug#8711855    Moved single Lump Sum E adjustment logic to new function get_lumpsumE_value.
251 ***                                               Modifed archive_balance_details procedure to call get_lumpsumE_value function and include additional Lump Sum E balances to existing Lump Sum E calculation.
252 *** 15-Dec-09 pmatamsr     115.219 Bug#9190980    Modified Lump Sum E adjustment logic for Retro GT12 Pre Tax Deductions in get_lumpsumE_value function.
253 *** 13-Jan-09 pmatamsr     115.220 Bug#9226023    Added logic to support the calculation of taxable and tax free portions of ETP for the terminated employees processed
254 ***                                               before applying the patch 8769345.
255 *** 04-May-10 skshin       115.223 Bug#9675451    Restored the missing code to assign v_earnings_ytd and v_lump_sum_E_ytd to v_bal_value in archive_balance_details.
256 *** 05-Jul-10 avenkatk     115.226 Bug#9872891    For employee terminated in previous year with ETP pay this year - modified the ETP Pay Date and LE End Date archived
257 *** 06-Jul-10 skshin       115.227 Bug#9147430    Added Foreign Workers
258 *** 23-Aug-10 skshin       115.228 Bug#10048508   Repositioned to get g_business_group_id corectly and added to check FW allowance/union plsql table values exist.
259 *** 31-Aug-10 dduvvuri     115.229 Bug#10064551   Modified select column for middle_name in cursor employee_details in procedure archive_employee_details
260                                                   and in cursor etp_details in procedure archive_etp_details.
261 *** 04-Sep-10 skshin       115.230 Bug#10084818   Initialized l_union_count, l_total_value, and l_alw_total for FW2 payment summary
262 *** 09-Sep-10 skshin       115.232 Bug#10100047   Added p_fw_alw_limit_tab and removed cdep_ytd value from g_net_balance.
263 *** 18-Oct-10 skshin       115.233 Bug#10143762   Adjusted Exempt Foreign Income from Gross_Earnings for both INB and FW type.
264 *** 22-Nov-10 skshin       115.234 Bug#10216064    LT12_Curr retro and LT12_Curr retro Tax are to be reported on each type of payment summary based on
265 ***                                    9950136     assignment type of original period.  The other retros and associated retro Taxes are to be reported on INB payment summary.
266 *** 30-Nov-10 avenkatk     115.235 Bug#10348688  Corrected parameters in call to get_foreign_payment_amount
267 *** 01-Dec-10 avenkatk     115.236 Bug#10331262   Added new procedure -get_foreign_leave_payments. Made changes for FW Termination payment reporting
268 *** 13-May-11 skshin       115.237 Bug#12400935   Modified range_process_assignments cursor and added additional joins for better performance
269 ***  01-June-11 scireddy  115.238 Bug# 12584258  Changes done in archive_etp_payment_details procedure to provide the actual_termination_date for ETP_Payment_date,
270 ***                if there are no Prepayments after balance adjustments.
271 *** 02-Jun-11  scireddy   115.239 Bug#12584258  Modified the Else clause in 'archive_etp_payment_details' procedure and added a join condition in 'act_term_date' cursor.
272 *** 06-Jun-11  scireddy   115.241 Bug# 12625929  Changes are done to the Else clause in 'archive_etp_payment_details' procedure.
273 *** 07-Jul-11  dduvvuri   115.243 Bug# 12725161 This version is a rollback of 115.242 version with the bug 12698821 fixed in a different way to avoid eoy related issues
274 *** 27-Jul-11  rrajaman   115.244 Bug#12744254  Added context check in cursors referring to assignment EIT for HR_PS_ISSUE_DATE_AU
275 *** 26-Sep-11  dduvvuri   115.245 Bug#12400821  Performance improvements in EXISTS clause in all 3 assignment_action_code cursors done for westpac customer.
276 *** 28-Oct-11  dduvvuri   115.246 Bug#9846470   Modified the archiving logic for X_EMPLOYEE_LE_END_DATE archive item for FBT only payments for active and
277 ***                                             terminated employees
278 *** 06-Dec-11  prasrang   115.247 Bug#13043341  Added cursors range_process_assignment_set and process_assignment_set in assignment_action_code procedure to
279 ***                                             handle Assignment Set parameter.
280 ***                               Bug#13043357  Changed the parameter to cursor tfn_number in procedure archive_employee_details.
281 *** 16-Dec-11  prasrang   115.248 Bug#13043357  Parameter l_date_earned used with Cursor tfn_number in procedure archive_employee_details if no value
282 ***                                             returned using p_year_end parameter.
283 *** 02-Feb-12 skshin      115.249 Bug#13362286  Add Retro Earnings Additional GT12 balance for Lump Sum E and foreign worker
284 *** 10-May-12 skshin      115.250 Bug#14060570  Adjusted the negative retro allowance greater than 12 months
285 *** 07-Dec-12 skshin      115.252 Bug#14703826 Modifed to archive the new ETP balances
286 *** -------------------------------------------------------------------------------------------------------+
287 */
288 g_debug             boolean;
289 g_business_group_id number;
290 g_package           constant varchar2(30) := 'pay_au_payment_summary';
291 g_legislation_code  constant varchar2(2)  := 'AU';
292 g_dimension_id      pay_balance_dimensions.balance_dimension_id%type;
293 g_balance_type_id   pay_balance_types.balance_type_id%type; /*Bug#5591993*/
294 g_fbt_threshold ff_globals_f.global_value%TYPE ; /* Bug 5708255 */
295 g_attribute_id        pay_balance_attributes.attribute_id%type;   -- bug 7571001
296 g_termination_type  varchar2(4); /*Bug#8315198 - Variable to store the termination type*/
297 --
298 /*
299 ** 4177679 this new flag would be Y when the etp employee date has to be archived
300 ** else N if le start date has to be archived for X_ETP_EMPLOYEE_START_DATE
301 */
302 g_le_etp_flag varchar2(10) ;
303 --
304 -------------------------------------------------------------------------------------------------------+
305 -- Define global variable to store defined_balance_id's and the corresponding balance values for BBR.
306 -------------------------------------------------------------------------------------------------------+
307 p_balance_value_tab     pay_balance_pkg.t_balance_value_tab;    -- 3172963
308 p_context_table         pay_balance_pkg.t_context_tab;          -- 2610141
309 p_result_table          pay_balance_pkg.t_detailed_bal_out_tab; -- 2610141
310 p_lump_sum_E_ptd_tab    pay_balance_pkg.t_balance_value_tab;    -- 8711855
311 
312 ---------------------------------------------------------------------------------------------------------------------------+
313 --Bug 6192381
314 --For ETP Payment Balances define global variable to store defined_balance_id's and the corresponding balance values for BBR.
315 ---------------------------------------------------------------------------------------------------------------------------+
316 
317 p_etp_balance_value_tab     pay_balance_pkg.t_balance_value_tab;
318 p_etp_context_table         pay_balance_pkg.t_context_tab;
319 p_etp_result_table          pay_balance_pkg.t_detailed_bal_out_tab;
320 
321 /* Bug 6470581 - PL/SQL table to hold Archive Items when submitted for Amended payment summary */
322 p_all_dbi_tab pay_au_payment_summary_amend.archive_db_tab ;
323 
324 /* start bug 9147430 */
325 -- Foreign Worker status check global variables
326 g_fw_check varchar2(1);
327 g_fw_fbt_check varchar2(2);
328 g_fw_fbt_term_check varchar2(2);
329 
330 -- Defined_balance_IDs for Foregin Worker balances
331 p_fw_fbt_bal_type_tab     pay_au_foreign_workers.tab_bal_type;
332 p_fw_balance_type_tab     pay_au_foreign_workers.tab_bal_type;
333 p_fw_alw_balance_type_tab     pay_au_foreign_workers.tab_bal_type;
334 p_fw_union_balance_type_tab     pay_au_foreign_workers.tab_bal_type;
335 
336 -- Global variables for comparison
337 g_fw_fbt_balance number ;
338 g_net_balance number ;
339 g_fw_union_value number;
340 p_fw_union_tab_sorted tab_allownace_balance;
341 p_fw_alw_tab_sorted tab_allownace_balance;
342 
343 type fw_gross_type_table is table of varchar2(1) index by binary_integer;
344 t_fw_gross_type fw_gross_type_table;
345 f_fw_date_tab_g         pay_au_foreign_workers.tab_fw_dates;
346 j_fw_date_tab_g         pay_au_foreign_workers.tab_fw_dates;
347 
348 /* end bug 9147430 */
349 
350 /* bug 14703826 - balance plsql tables for new ETP balances */
351 p_etp_excl_bal_value_tab     pay_balance_pkg.t_balance_value_tab;
352 p_etp_non_excl_bal_value_tab pay_balance_pkg.t_balance_value_tab;
353 p_etp_excl_result_table      pay_balance_pkg.t_detailed_bal_out_tab;
354 p_etp_non_excl_result_table  pay_balance_pkg.t_detailed_bal_out_tab;
355 
356   --------------------------------------------------------------------+
357   -- This procedure returns a sql string to select a range
358   -- of assignments eligible for archival.
359   --------------------------------------------------------------------+
360 
361   procedure range_code
362     (p_payroll_action_id   in pay_payroll_actions.payroll_action_id%type,
363      p_sql                out nocopy varchar2) is
364   begin
365       g_debug := hr_utility.debug_enabled;
366       IF g_debug THEN
367          hr_utility.set_location('Start of range_code',1);
368      END if;
369 /* Bug#3043049*/
370     p_sql := ' select distinct p.person_id'                                       ||
371              ' from   per_people_f p,'                                        ||
372                     ' pay_payroll_actions pa'                                     ||
373              ' where  pa.payroll_action_id = :payroll_action_id'                  ||
374              ' and    p.business_group_id = pa.business_group_id'                 ||
375              ' order by p.person_id';
376       IF g_debug THEN
377         hr_utility.set_location('End of range_code',2);
378       END if;
379   end range_code;
380 
381 
382   --------------------------------------------------------------------+
383   -- This procedure is used to set global contexts
384   -- however in current case it is a dummy procedure. In case this
385   -- procedure is not present then archiver assumes that no archival is required.
386   --------------------------------------------------------------------+
387 
388 
389 procedure initialization_code (p_payroll_action_id  in pay_payroll_actions.payroll_action_id%type) is
390   --
391   --------------------------------------------------------------------------------------+
392   -- Cursor to setup table to hold balance_id's to be retrieved using BBR. (Bug3172963)
393   --------------------------------------------------------------------------------------+
394   --
395 
396 /*Bug 6192381 Added New Balances for Multiple ETP Enhancement*/
397 /*Bug 8315198 Added two new Balances for FY 2009 Payment Summary enhancement */
398 /*Bug 8587013 Removed 'Other Income' balance and moved 'Exempt Foreign Employment Income' balance to that index position*/
399   cursor c_get_defined_balance_id is
400   select decode(pbt.balance_name, 'CDEP',1
401                                 , 'Leave Payments Marginal',2
402                                 , 'Lump Sum A Deductions',3
403                                 , 'Lump Sum A Payments',4
404                                 , 'Lump Sum B Deductions',5
405                                 , 'Lump Sum B Payments',6
406                                 , 'Lump Sum C Deductions',7
407                                 , 'Lump Sum C Payments',8
408                                 , 'Lump Sum D Payments',9
409                                 , 'Total_Tax_Deductions',10
410                                 , 'Termination Deductions',11
411                                 , 'Exempt Foreign Employment Income',12 /* 8315198, 8587013 */
412                                 , 'Union Fees',13
413                                 , 'Invalidity Payments',14
414                                 , 'Lump Sum E Payments',15
415                                 , 'Earnings_Total', 16
416                                 , 'Workplace Giving Deductions' , 17
417                                 , 'ETP Deductions Transitional Not Part of Prev Term',18  /* Begin 6192381 */
418                                 , 'ETP Deductions Transitional Part of Prev Term',19
419                                 , 'ETP Deductions Life Benefit Not Part of Prev Term',20
420                                 , 'ETP Deductions Life Benefit Part of Prev Term',21
421                                 , 'Invalidity Payments Life Benefit Not Part of Prev Term',22
422                                 , 'Invalidity Payments Life Benefit Part of Prev Term',23
423                                 , 'Invalidity Payments Transitional Not Part of Prev Term',24
424                                 , 'Invalidity Payments Transitional Part of Prev Term',25 /*4015082 ,6192381 */
425                                 , 'Reportable Employer Superannuation Contributions',26  /*Bug 8315198 ,8587013 */
426                                 , 'Retro Earnings Leave Loading GT 12 Mths Amount', 27  -- start 8711855
427                                 , 'Retro Earnings Spread GT 12 Mths Amount', 28
428                                 , 'Retro Pre Tax GT 12 Mths Amount', 29
429                                 , 'Foreign Leave Payments',30                   /* Start Bug 10331262 */
430                                 , 'Foreign Leave Payments Marginal',31
431                                 , 'Foreign Lump Sum A Payments',32
432                                 , 'Foreign Leave Component Deduction',33
433                                 , 'Foreign Lump Sum A Deduction',34
434                                 , 'Retro Earnings Additional GT 12 Mths Amount',35  -- bug 13362286
435                                 , 'ETP Deductions Excluded',36 /* start bug 14703826 */
436                                 , 'ETP Deductions Excluded Part of Prev',37
437                                 , 'ETP Deductions Non Excluded',38
438                                 , 'ETP Deductions Non Excluded Part of Prev',39 /* end bug 14703826 */
439                                 ) sort_index, -- end 8711855
440          pdb.defined_balance_id defined_balance_id
441   from   pay_balance_types pbt
442   ,      pay_balance_dimensions pbd
443   ,      pay_defined_balances pdb
444   where  pbt.balance_name in     ( 'CDEP'
445                                  , 'Leave Payments Marginal'
446                                  , 'Lump Sum A Deductions'
447                                  , 'Lump Sum A Payments'
448                                  , 'Lump Sum B Deductions'
449                                  , 'Lump Sum B Payments'
450                                  , 'Lump Sum C Deductions'
451                                  , 'Lump Sum C Payments'
452                                  , 'Lump Sum D Payments'
453                                  , 'Total_Tax_Deductions'
454                                  , 'Termination Deductions'
455                                  , 'Exempt Foreign Employment Income' /* 8315198 ,8587013 */
456                                  , 'Union Fees'
457                                  , 'Invalidity Payments'
458                                  , 'Lump Sum E Payments'
459                                  , 'Earnings_Total'
460                                  , 'Workplace Giving Deductions'
461                                 , 'ETP Deductions Transitional Not Part of Prev Term' /* Begin 6192381 */
462                                 , 'ETP Deductions Transitional Part of Prev Term'
463                                 , 'ETP Deductions Life Benefit Not Part of Prev Term'
464                                 , 'ETP Deductions Life Benefit Part of Prev Term'
465                                 , 'Invalidity Payments Life Benefit Not Part of Prev Term'
466                                 , 'Invalidity Payments Life Benefit Part of Prev Term'
467                                 , 'Invalidity Payments Transitional Not Part of Prev Term'
468                                 , 'Invalidity Payments Transitional Part of Prev Term'  /* 4015082 , End 6192381 */
469                                 , 'Reportable Employer Superannuation Contributions'  /*Bug 8315198*/
470                                 , 'Retro Earnings Leave Loading GT 12 Mths Amount' -- start 8711855
471                                 , 'Retro Earnings Spread GT 12 Mths Amount'
472                                 , 'Retro Pre Tax GT 12 Mths Amount'
473                                 , 'Foreign Leave Payments'
474                                 , 'Foreign Leave Payments Marginal'
475                                 , 'Foreign Lump Sum A Payments'
476                                 , 'Foreign Leave Component Deduction'
477                                 , 'Foreign Lump Sum A Deduction'
478                                 , 'Retro Earnings Additional GT 12 Mths Amount' -- bug 13362286
479                                                                 , 'ETP Deductions Excluded'
480                                                                 , 'ETP Deductions Excluded Part of Prev'
481                                                                 , 'ETP Deductions Non Excluded'
482                                                                 , 'ETP Deductions Non Excluded Part of Prev'
483                                 )  -- end 8711855
484   and   pbd.database_item_suffix = '_ASG_LE_YTD' --2610141
485   and   pbt.balance_type_id      = pdb.balance_type_id
486   and   pbd.balance_dimension_id = pdb.balance_dimension_id
487   and   pbt.legislation_code     = 'AU'
488   order by sort_index;
489 
490 /*Bug 6192381 Added cursor c_get_etp_defined_balance_id for ETP Pay Balances*/
491 /* Bug 8769345 - Added new ETP Taxable and Tax Free balances to the cursor */
492   cursor c_get_etp_defined_balance_id is
493   select decode(pbt.balance_name, 'ETP Payments Transitional Not Part of Prev Term',1
494                                 , 'ETP Payments Transitional Part of Prev Term',2
495                                 , 'ETP Payments Life Benefit Not Part of Prev Term',3
496                                 , 'ETP Payments Life Benefit Part of Prev Term',4
497                                 , 'ETP Tax Free Payments Transitional Not Part of Prev Term',5
498                                 , 'ETP Taxable Payments Transitional Not Part of Prev Term',6
499                                 , 'ETP Tax Free Payments Transitional Part of Prev Term',7
500                                 , 'ETP Taxable Payments Transitional Part of Prev Term',8
501                                 , 'ETP Tax Free Payments Life Benefit Not Part of Prev Term',9
502                                 , 'ETP Taxable Payments Life Benefit Not Part of Prev Term',10
503                                 , 'ETP Tax Free Payments Life Benefit Part of Prev Term',11
504                                 , 'ETP Taxable Payments Life Benefit Part of Prev Term',12
505                                 , 'Lump Sum C Payments',13) sort_index,
506          pdb.defined_balance_id defined_balance_id
507   from   pay_balance_types pbt
508   ,      pay_balance_dimensions pbd
509   ,      pay_defined_balances pdb
510   where  pbt.balance_name in     ('ETP Payments Transitional Not Part of Prev Term'
511                                  ,'ETP Payments Transitional Part of Prev Term'
512                                  ,'ETP Payments Life Benefit Not Part of Prev Term'
513                                  ,'ETP Payments Life Benefit Part of Prev Term'
514                                  ,'Lump Sum C Payments'
515                                  ,'ETP Tax Free Payments Transitional Not Part of Prev Term'
516                                  ,'ETP Taxable Payments Transitional Not Part of Prev Term'
517                                  ,'ETP Tax Free Payments Transitional Part of Prev Term'
518                                  ,'ETP Taxable Payments Transitional Part of Prev Term'
519                                  ,'ETP Tax Free Payments Life Benefit Not Part of Prev Term'
520                                  ,'ETP Taxable Payments Life Benefit Not Part of Prev Term'
521                                  ,'ETP Tax Free Payments Life Benefit Part of Prev Term'
522                                  ,'ETP Taxable Payments Life Benefit Part of Prev Term')
523   and   pbd.database_item_suffix = '_ASG_LE_YTD'
524   and   pbt.balance_type_id      = pdb.balance_type_id
525   and   pbd.balance_dimension_id = pdb.balance_dimension_id
526   and   pbt.legislation_code     = 'AU'
527   order by sort_index;
528 
529   /* start bug 14703826 - Defined Balance IDs for new ETP balances */
530   cursor c_etp_excl_def_bal_id is
531   select decode(pbt.balance_name, 'ETP Tax Free Payments Excluded',1
532                             , 'ETP Taxable Payments Excluded',2
533                             , 'Invalidity Payments',3
534                             , 'ETP Deductions Excluded',4
535                             , 'ETP Deductions Excluded Part of Prev',5) sort_index,
536          pdb.defined_balance_id defined_balance_id
537   from   pay_balance_types pbt
538   ,      pay_balance_dimensions pbd
539   ,      pay_defined_balances pdb
540   where  pbt.balance_name in     (
541                               'ETP Tax Free Payments Excluded'
542                             , 'ETP Taxable Payments Excluded'
543                             , 'Invalidity Payments'
544                             , 'ETP Deductions Excluded'
545                             , 'ETP Deductions Excluded Part of Prev')
546   and   pbd.database_item_suffix = '_ASG_LE_RUN'
547   and   pbt.balance_type_id      = pdb.balance_type_id
548   and   pbd.balance_dimension_id = pdb.balance_dimension_id
549   and   pbt.legislation_code     = 'AU'
550   order by sort_index;
551 
552   cursor c_etp_non_excl_def_bal_id is
553   select decode(pbt.balance_name, 'ETP Tax Free Payments Non Excluded',1
554                             , 'ETP Taxable Payments Non Excluded',2
555                             , 'ETP Deductions Non Excluded',3
556                             , 'ETP Deductions Non Excluded Part of Prev', 4) sort_index,
557          pdb.defined_balance_id defined_balance_id
558   from   pay_balance_types pbt
559   ,      pay_balance_dimensions pbd
560   ,      pay_defined_balances pdb
561   where  pbt.balance_name in     (
562                               'ETP Tax Free Payments Non Excluded'
563                             , 'ETP Taxable Payments Non Excluded'
564                             , 'ETP Deductions Non Excluded'
565                             , 'ETP Deductions Non Excluded Part of Prev')
566   and   pbd.database_item_suffix = '_ASG_LE_RUN'
567   and   pbt.balance_type_id      = pdb.balance_type_id
568   and   pbd.balance_dimension_id = pdb.balance_dimension_id
569   and   pbt.legislation_code     = 'AU'
570   order by sort_index;
571 
572 /* end bug 14703826 */
573 
574   --
575   /*
576   ** 4863149 - Introduced a new cursor c_le_ytd_dimension_id
577   **           Moved here to avoid having to be executed for each assignment
578   */
579   cursor c_le_ytd_dimension_id is
580   select balance_dimension_id
581   from   pay_balance_dimensions pbd
582   where  pbd.dimension_name = '_ASG_LE_YTD'
583   and    pbd.legislation_code = g_legislation_code;
584   /*Bug#5591993*/
585   -- Cursor to fetch the balance type id for Seeded balance Union Fees  which will be
586   -- used in  Curosr CSR_UNIONS_2006
587   CURSOR c_union_balance IS
588   SELECT balance_type_id from pay_balance_types
589   WHERE balance_name='Union Fees'
590   and   legislation_code='AU';
591 
592 
593  /* Bug 6470581 - Added the following to initialize the global g_fbt_threshold */
594    CURSOR  c_get_fbt_global(c_year_end DATE)
595    IS
596    SELECT  global_value
597    FROM   ff_globals_f
598    WHERE  global_name = 'FBT_THRESHOLD'
599    AND    legislation_code = 'AU'
600    AND    c_year_end BETWEEN effective_start_date
601                          AND effective_end_date;
602 
603   CURSOR get_params(c_payroll_action_id  per_all_assignments_f.assignment_id%type)
604   IS
605   SELECT  to_date('30-06-'||substr(pay_core_utils.get_parameter('FINANCIAL_YEAR',legislative_parameters),6,4),'DD-MM-YYYY') Financial_year_end
606          ,pay_core_utils.get_parameter('REGISTERED_EMPLOYER',ppa.legislative_parameters) registered_employer /*bug9147430*/
607          ,ppa.business_group_id
608   FROM  pay_payroll_actions ppa
609   WHERE ppa.payroll_action_id = c_payroll_Action_id;
610 
611   /* bug 7571001 - added get_balance_attribute cursor to initialize g_attribute_id*/
612   CURSOR get_balance_attribute (c_attribute_name PAY_BAL_ATTRIBUTE_DEFINITIONS.attribute_name%type) IS
613       select attribute_id
614       from PAY_BAL_ATTRIBUTE_DEFINITIONS
615       where attribute_name = c_attribute_name
616       ;
617 
618    /*bug8711855 - Modified to fetch defined_balance_ids of Lump Sum E balances*/
619    CURSOR  c_single_lumpsum_E_payment  IS
620    SELECT decode(pbt.balance_name,
621                               'Lump Sum E Payments', 1
622                              ,'Retro Earnings Leave Loading GT 12 Mths Amount', 2
623                              ,'Retro Earnings Spread GT 12 Mths Amount', 3
624                              ,'Retro Pre Tax GT 12 Mths Amount', 4
625                              ,'Retro Earnings Additional GT 12 Mths Amount', 5) sort_index -- bug 13362286
626                , pdb.defined_balance_id defined_balance_id
627    FROM  pay_balance_types      pbt,
628          pay_defined_balances   pdb,
629          pay_balance_dimensions pbd
630    WHERE pbt.legislation_code = 'AU'
631    AND  pbt.balance_name in ( 'Lump Sum E Payments'
632                              ,'Retro Earnings Leave Loading GT 12 Mths Amount'
633                              ,'Retro Earnings Spread GT 12 Mths Amount'
634                              ,'Retro Pre Tax GT 12 Mths Amount'
635                              ,'Retro Earnings Additional GT 12 Mths Amount') -- bug 13362286
636    AND  pbt.balance_type_id = pdb.balance_type_id
637    AND  pbd.balance_dimension_id = pdb.balance_dimension_id
638    AND  pbd.dimension_name = '_ASG_LE_PTD'
639    order by sort_index;
640 
641   l_fin_year_date DATE;
642 
643 /* End Bug 6470581 */
644 
645 /* Start of bug9147430 */
646 
647   cursor c_get_fw_fbt_bal_type_id is
648   select balance_type_id
649   from pay_balance_types
650   where balance_name = 'Fringe Benefits'
651   and legislation_code = 'AU';
652 
653   cursor c_get_fw_bal_type_id is
654   select decode(pbt.balance_name,
655                                   'Earnings_Total',1
656                                 , 'Leave Payments Marginal',2
657                                 , 'Workplace Giving Deductions',3
658                                 , 'Lump Sum E Payments',4
659                                 , 'Retro Earnings Leave Loading GT 12 Mths Amount',5
660                                 , 'Retro Earnings Spread GT 12 Mths Amount',6
661                                 , 'Retro Pre Tax GT 12 Mths Amount',7
662                                 , 'Total_Tax_Deductions',8
663                                 , 'Termination Deductions',9
664                                 , 'Lump Sum C Deductions',10
665                                 , 'Foreign Tax Deductions',11
666                                 , 'Lump Sum A Payments',12
667                                 , 'Lump Sum D Payments',13
668                                 , 'Reportable Employer Superannuation Contributions',14
669                                 , 'Union Fees',15
670                                 , 'CDEP',16
671                                 , 'Exempt Foreign Employment Income',17
672                                 , 'Retro LT 12 Mths Prev Yr Amount', 18
673                                 , 'Retro Earnings Leave Loading LT 12 Mths Prev Yr Amount', 19
674                                 , 'Retro Earnings Spread LT 12 Mths Prev Yr Amount', 20
675                                 , 'Retro Pre Tax LT 12 Mths Prev Yr Amount', 21
676                                 , 'Retro LT 12 Mths Curr Yr Amount', 22
677                                 , 'Retro Earnings Leave Loading LT 12 Mths Curr Yr Amount', 23
678                                 , 'Retro Earnings Spread LT 12 Mths Curr Amount', 24
679                                 , 'Retro Tax GT12 Amount', 25
680                                 , 'Retro Tax LT12 Prev Amount', 26
681                                 , 'Retro Tax LT12 Curr Amount', 27
682                                 , 'Foreign Leave Payments',28
683                                 , 'Retro Earnings Additional GT 12 Mths Amount', 29 -- bug 13362286
684                                 , 'Retro Earnings Additional LT12 Prev Mths Amount', 30
685                                 , 'Retro Earnings Additional LT12 Curr Mths Amount', 31
686                        ) sort_index,
687          pbt.balance_type_id balance_type_id
688     from   pay_balance_types pbt
689     where  pbt.balance_name in     (
690                                   'Earnings_Total'
691                                 , 'Leave Payments Marginal'
692                                 , 'Workplace Giving Deductions'
693                                 , 'Lump Sum E Payments'
694                                 , 'Retro Earnings Leave Loading GT 12 Mths Amount'
695                                 , 'Retro Earnings Spread GT 12 Mths Amount'
696                                 , 'Retro Pre Tax GT 12 Mths Amount'
697                                 , 'Total_Tax_Deductions'
698                                 , 'Termination Deductions'
699                                 , 'Lump Sum C Deductions'
700                                 , 'Foreign Tax Deductions'
701                                 , 'Lump Sum A Payments'
702                                 , 'Lump Sum D Payments'
703                                 , 'Reportable Employer Superannuation Contributions'
704                                 , 'Union Fees'
705                                 , 'CDEP'
706                                 , 'Exempt Foreign Employment Income' -- bug 10143762
707                                 /* start bug 9950136 */
708                                 , 'Retro LT 12 Mths Prev Yr Amount'
709                                 , 'Retro Earnings Leave Loading LT 12 Mths Prev Yr Amount'
710                                 , 'Retro Earnings Spread LT 12 Mths Prev Yr Amount'
711                                 , 'Retro Pre Tax LT 12 Mths Prev Yr Amount'
712                                 , 'Retro LT 12 Mths Curr Yr Amount'
713                                 , 'Retro Earnings Leave Loading LT 12 Mths Curr Yr Amount'
714                                 , 'Retro Earnings Spread LT 12 Mths Curr Amount'
715                                 , 'Retro Tax GT12 Amount'
716                                 , 'Retro Tax LT12 Prev Amount'
717                                 , 'Retro Tax LT12 Curr Amount'
718                                 /* end bug 9950136 */
719                                 , 'Foreign Leave Payments'
720                                 , 'Retro Earnings Additional GT 12 Mths Amount'
721                                 , 'Retro Earnings Additional LT12 Prev Mths Amount'
722                                 , 'Retro Earnings Additional LT12 Curr Mths Amount'
723                                  )
724      and    pbt.legislation_code     = 'AU'
725      order by sort_index;
726 
727 cursor c_get_fw_alw_bal_type_id is
728 SELECT  pbt.balance_type_id,
729              pbt.balance_name
730 FROM  pay_balance_attributes pba
731      ,pay_defined_balances   pdb
732      ,pay_balance_types      pbt
733      ,pay_balance_dimensions pbd
734 WHERE pba.attribute_id         = g_attribute_id
735 AND   pdb.defined_balance_id   = pba.defined_balance_id
736 AND   pbt.balance_type_id      = pdb.balance_type_id
737 AND   pdb.business_group_id = g_business_group_id
738 AND   pbd.balance_dimension_id = pdb.balance_dimension_id
739 AND   pbd.dimension_name = '_ASG_LE_YTD';
740 
741 cursor c_get_fw_union_bal_type_id is
742   select distinct pbt.balance_type_id
743   from   pay_element_types_f pet
744           , pay_balance_types pbt
745           , pay_input_values_f piv
746           , pay_balance_feeds_f pbf
747   where  pet.element_information_category = 'AU_VOLUNTARY DEDUCTIONS'
748   and    pet.element_information1        = pbt.balance_type_id
749   and    pet.element_type_id = piv.element_type_id
750   and    piv.input_value_id = pbf.input_value_id
751   and    pbf.balance_type_id             = g_balance_type_id
752   and    pbt.business_group_id = g_business_group_id;
753 
754 cnt number := 1;
755 idx number := 1;
756 l_registered_employer hr_organization_units.organization_id%type;
757 l_fw_asg_f_status_id per_assignment_status_types.assignment_status_type_id%TYPE;
758 l_fw_asg_j_status_id per_assignment_status_types.assignment_status_type_id%TYPE;
759 l_fw_exist varchar2(1);
760 raise_no_status_type exception;
761 raise_invalid_LE_fw_setup exception;
762 
763 /* End of bug9147430 */
764 
765   l_procedure constant varchar2(80) := g_package || '.initialization_code';
766   --
767 begin
768   g_debug := hr_utility.debug_enabled;
769   if g_debug then
770     hr_utility.set_location('Start of initialization_code',1);
771   end if;
772   --
773   open c_union_balance;
774   fetch c_union_balance into g_balance_type_id;
775   close c_union_balance;
776   --
777   --
778   open c_le_ytd_dimension_id;
779   fetch c_le_ytd_dimension_id into g_dimension_id;
780   close c_le_ytd_dimension_id;
781   --
782   p_balance_value_tab.delete;
783   for csr_rec in c_get_defined_balance_id
784   loop
785      p_balance_value_tab(csr_rec.sort_index).defined_balance_id := csr_rec.defined_balance_id;
786   end loop;
787 
788   if g_debug then
789      hr_utility.set_location('Defined Balance Ids for balances are:' , 15);
790      hr_utility.trace('--------------------------------------------');
791      hr_utility.trace('CDEP                    ===>' || p_balance_value_tab(1).defined_balance_id);
792      hr_utility.trace('Leave Payments Marginal ===>' || p_balance_value_tab(2).defined_balance_id);
793      hr_utility.trace('Earnings_Total          ===>' || p_balance_value_tab(16).defined_balance_id);
794      hr_utility.trace('Workplace Giving        ===>' || p_balance_value_tab(17).defined_balance_id); /*4015082 */
795      hr_utility.trace('Lump Sum A Deductions   ===>' || p_balance_value_tab(3).defined_balance_id);
796      hr_utility.trace('Lump Sum A Payments     ===>' || p_balance_value_tab(4).defined_balance_id);
797      hr_utility.trace('Lump Sum B Deductions   ===>' || p_balance_value_tab(5).defined_balance_id);
798      hr_utility.trace('Lump Sum B Payments     ===>' || p_balance_value_tab(6).defined_balance_id);
799      hr_utility.trace('Lump Sum C Deductions   ===>' || p_balance_value_tab(7).defined_balance_id);
800      hr_utility.trace('Lump Sum C Payments     ===>' || p_balance_value_tab(8).defined_balance_id);
801      hr_utility.trace('Lump Sum D Payments     ===>' || p_balance_value_tab(9).defined_balance_id);
802      hr_utility.trace('Total_Tax_Deduction     ===>' || p_balance_value_tab(10).defined_balance_id);
803      hr_utility.trace('Termination Deductions  ===>' || p_balance_value_tab(11).defined_balance_id);
804      hr_utility.trace('Union Fees              ===>' || p_balance_value_tab(13).defined_balance_id);
805      hr_utility.trace('Invalidity Payments     ===>' || p_balance_value_tab(14).defined_balance_id);
806      hr_utility.trace('Lump Sum E Payments     ===>' || p_balance_value_tab(15).defined_balance_id);
807      hr_utility.trace('ETP Deductions Transitional Not Part of Prev Term      ===>' || p_balance_value_tab(18).defined_balance_id);/*Begin  Bug 6192381 */
808      hr_utility.trace('ETP Deductions Transitional Part of Prev Term          ===>' || p_balance_value_tab(19).defined_balance_id);
809      hr_utility.trace('ETP Deductions Life Benefit Not Part of Prev Term      ===>' || p_balance_value_tab(20).defined_balance_id);
810      hr_utility.trace('ETP Deductions Life Benefit Part of Prev Term          ===>' || p_balance_value_tab(21).defined_balance_id);
811      hr_utility.trace('Invalidity Payments Life Benefit Not Part of Prev Term  ===>' || p_balance_value_tab(22).defined_balance_id);
812      hr_utility.trace('Invalidity Payments Life Benefit Part of Prev Term      ===>' || p_balance_value_tab(23).defined_balance_id);
813      hr_utility.trace('Invalidity Payments Transitional Not Part of Prev Term  ===>' || p_balance_value_tab(24).defined_balance_id);
814      hr_utility.trace('Invalidity Payments Transitional Part of Prev Term      ===>' || p_balance_value_tab(25).defined_balance_id);/*End  Bug 6192381 */
815      hr_utility.trace('Reportable Employer Superannuation Contributions      ===>' || p_balance_value_tab(26).defined_balance_id);/*Begin 8315198*/
816      hr_utility.trace('Exempt Foreign Employment Income      ===>' || p_balance_value_tab(12).defined_balance_id);/*End 8315198*/
817      hr_utility.trace('Retro Earnings Leave Loading GT 12 Mths Amount     ===>' || p_balance_value_tab(27).defined_balance_id); -- start 8711855
818      hr_utility.trace('Retro Earnings Spread GT 12 Mths Amount    ===>' || p_balance_value_tab(28).defined_balance_id);
819      hr_utility.trace('Retro Pre Tax GT 12 Mths Amount     ===>' || p_balance_value_tab(29).defined_balance_id); -- end 8711855
820      hr_utility.trace('Retro Earnings Additional GT 12 Mths Amount     ===>' || p_balance_value_tab(35).defined_balance_id); -- end 8711855
821      hr_utility.trace('ETP Deductions Excluded     ===>' || p_balance_value_tab(36).defined_balance_id);  /* start bug 14703826 */
822      hr_utility.trace('ETP Deductions Excluded Part of Prev     ===>' || p_balance_value_tab(37).defined_balance_id);
823      hr_utility.trace('ETP Deductions Non Excluded     ===>' || p_balance_value_tab(38).defined_balance_id);
824      hr_utility.trace('ETP Deductions Non Excluded Part of Prev     ===>' || p_balance_value_tab(39).defined_balance_id); /* end bug 14703826 */
825   end if;
826 
827 /*Bug 8711855 For fetching Defined Balance Id's for Lump Sum E Balances_PTD */
828   p_lump_sum_E_ptd_tab.delete;
829   for csr_rec in c_single_lumpsum_E_payment loop
830      p_lump_sum_E_ptd_tab(csr_rec.sort_index).defined_balance_id := csr_rec.defined_balance_id;
831   end loop;
832 
833   if g_debug then
834      hr_utility.set_location('Defined Balance Ids for Lump Sume E PTD are:' , 15);
835      hr_utility.trace('--------------------------------------------');
836      hr_utility.trace('Lump Sum E Payments   ===>' || p_lump_sum_E_ptd_tab(1).defined_balance_id);
837      hr_utility.trace('Retro Earnings Leave Loading GT 12 Mths Amount   ===>' || p_lump_sum_E_ptd_tab(2).defined_balance_id);
838      hr_utility.trace('Retro Earnings Spread GT 12 Mths Amount   ===>' || p_lump_sum_E_ptd_tab(3).defined_balance_id);
839      hr_utility.trace('Retro Pre Tax GT 12 Mths Amount  ===>' || p_lump_sum_E_ptd_tab(4).defined_balance_id);
840      hr_utility.trace('Retro Earnings Additional GT 12 Mths Amount  ===>' || p_lump_sum_E_ptd_tab(5).defined_balance_id);
841   end if;
842 
843 /*Bug 6192381 For fetching Defined Balance Id's for new ETP payment Balances */
844   p_etp_balance_value_tab.delete;
845   for csr_etp_rec in c_get_etp_defined_balance_id
846   loop
847      p_etp_balance_value_tab(csr_etp_rec.sort_index).defined_balance_id := csr_etp_rec.defined_balance_id;
848   end loop;
849 
850 
851   if g_debug then
852      hr_utility.set_location('Defined Balance Ids for ETP Payment balances are:' , 15);
853      hr_utility.trace('--------------------------------------------');
854      hr_utility.trace('ETP Payments Transitional Not Part of Prev Term   ===>' || p_etp_balance_value_tab(1).defined_balance_id);
855      hr_utility.trace('ETP Payments Transitional Part of Prev Term       ===>' || p_etp_balance_value_tab(2).defined_balance_id);
856      hr_utility.trace('ETP Payments Life Benefit Not Part of Prev Term   ===>' || p_etp_balance_value_tab(3).defined_balance_id);
857      hr_utility.trace('ETP Payments Life Benefit Part of Prev Term       ===>' || p_etp_balance_value_tab(4).defined_balance_id);
858      hr_utility.trace('Lump Sum C Payments                               ===>' || p_etp_balance_value_tab(13).defined_balance_id);
859     /* Start 8769345 */
860      hr_utility.trace('ETP Tax Free Payments Transitional Not Part of Prev Term  ===>' || p_etp_balance_value_tab(5).defined_balance_id);
861      hr_utility.trace('ETP Taxable Payments Transitional Not Part of Prev Term   ===>' || p_etp_balance_value_tab(6).defined_balance_id);
862      hr_utility.trace('ETP Tax Free Payments Transitional Part of Prev Term      ===>' || p_etp_balance_value_tab(7).defined_balance_id);
863      hr_utility.trace('ETP Taxable Payments Transitional Part of Prev Term       ===>' || p_etp_balance_value_tab(8).defined_balance_id);
864      hr_utility.trace('ETP Tax Free Payments Life Benefit Not Part of Prev Term  ===>' || p_etp_balance_value_tab(9).defined_balance_id);
865      hr_utility.trace('ETP Taxable Payments Life Benefit Not Part of Prev Term   ===>' || p_etp_balance_value_tab(10).defined_balance_id);
866      hr_utility.trace('ETP Tax Free Payments Life Benefit Part of Prev Term      ===>' || p_etp_balance_value_tab(11).defined_balance_id);
867      hr_utility.trace('ETP Taxable Payments Life Benefit Part of Prev Term       ===>' || p_etp_balance_value_tab(12).defined_balance_id);
868     /* End 8769345 */
869   end if;
870 
871   /* start bug 14703826 - plsql table to store defined_balance_ids for new ETP balances */
872   p_etp_excl_bal_value_tab.delete;
873   for csr_etp_excl_rec in c_etp_excl_def_bal_id
874   loop
875      p_etp_excl_bal_value_tab(csr_etp_excl_rec.sort_index).defined_balance_id := csr_etp_excl_rec.defined_balance_id;
876   end loop;
877 
878 
879   if g_debug then
880      hr_utility.set_location('Defined Balance Ids for ETP Excluded Payments RUN balances are:' , 16);
881      hr_utility.trace('--------------------------------------------');
882          hr_utility.trace('ETP Tax Free Payments Excluded          ===>' || p_etp_excl_bal_value_tab(1).defined_balance_id);
883          hr_utility.trace('ETP Taxable Payments Excluded           ===>' || p_etp_excl_bal_value_tab(2).defined_balance_id);
884          hr_utility.trace('Invalidity Payments                     ===>' || p_etp_excl_bal_value_tab(3).defined_balance_id);
885          hr_utility.trace('ETP Deductions Excluded                 ===>' || p_etp_excl_bal_value_tab(4).defined_balance_id);
886          hr_utility.trace('ETP Deductions Excluded Part of Prev    ===>' || p_etp_excl_bal_value_tab(5).defined_balance_id);
887   end if;
888 
889   p_etp_non_excl_bal_value_tab.delete;
890   for csr_etp_non_excl_rec in c_etp_non_excl_def_bal_id
891   loop
892      p_etp_non_excl_bal_value_tab(csr_etp_non_excl_rec.sort_index).defined_balance_id := csr_etp_non_excl_rec.defined_balance_id;
893   end loop;
894 
895 
896   if g_debug then
897      hr_utility.set_location('Defined Balance Ids for ETP Non Excluded Payments RUN balances are:' , 17);
898      hr_utility.trace('--------------------------------------------');
899          hr_utility.trace('ETP Tax Free Payments Excluded          ===>' || p_etp_non_excl_bal_value_tab(1).defined_balance_id);
900          hr_utility.trace('ETP Taxable Payments Excluded           ===>' || p_etp_non_excl_bal_value_tab(2).defined_balance_id);
901          hr_utility.trace('ETP Deductions Non Excluded             ===>' || p_etp_non_excl_bal_value_tab(3).defined_balance_id);
902          hr_utility.trace('ETP Deductions Non Excluded Part of Prev ===>' || p_etp_non_excl_bal_value_tab(4).defined_balance_id);
903   end if;
904 
905   /* end bug 14703826 */
906 
907 
908   /* Bug 6470581 - Initialize g_payment_summary_type */
909         g_payment_summary_type  := 'O';
910 
911   /* Bug 6470581 - Added code to initialize g_fbt_threshold */
912 
913         OPEN get_params(p_payroll_action_id);
914         FETCH get_params INTO l_fin_year_date,l_registered_employer,g_business_group_id; -- bug10048508
915         CLOSE get_params;
916 
917         OPEN c_get_fbt_global (add_months(l_fin_year_date,-3));  /* Add_months included for bug 5333143 */
918         FETCH c_get_fbt_global into g_fbt_threshold;
919         CLOSE c_get_fbt_global;
920 
921   /* bug 7571001 - initialize g_attribute_id */
922   open get_balance_attribute('AU_EOY_ALLOWANCE');
923   fetch get_balance_attribute into g_attribute_id;
924   close get_balance_attribute;
925 
926   if g_debug then
927      hr_utility.set_location('g_fbt_threshold           '||g_fbt_threshold,19);
928      hr_utility.set_location('End of initialization_code',20);
929   end if;
930    /* End Changes Bug 6470581 */
931 
932 /* Start of bug9147430 */
933 
934   p_fw_fbt_bal_type_tab.delete;
935   open c_get_fw_fbt_bal_type_id;
936   fetch c_get_fw_fbt_bal_type_id into p_fw_fbt_bal_type_tab(1).balance_type_id;
937   close c_get_fw_fbt_bal_type_id;
938 
939   p_fw_balance_type_tab.delete;
940   for csr_rec in c_get_fw_bal_type_id loop
941      p_fw_balance_type_tab(csr_rec.sort_index).balance_type_id := csr_rec.balance_type_id;
942   end loop;
943 
944   if g_debug then
945      hr_utility.set_location('Balance Type IDs for FW Balances are:' , 30);
946      hr_utility.trace('--------------------------------------------');
947      hr_utility.trace('Fringe Benefits               ===>' || p_fw_fbt_bal_type_tab(1).balance_type_id);
948      hr_utility.trace('Earnings_Total                    ===>' || p_fw_balance_type_tab(1).balance_type_id);
949      hr_utility.trace('Leave Payments Marginal               ===>' || p_fw_balance_type_tab(2).balance_type_id);
950      hr_utility.trace('Workplace Giving Deductions               ===>' || p_fw_balance_type_tab(3).balance_type_id);
951      hr_utility.trace('Lump Sum E Payments               ===>' || p_fw_balance_type_tab(4).balance_type_id);
952      hr_utility.trace('Retro Earnings Leave Loading GT 12 Mths Amount               ===>' || p_fw_balance_type_tab(5).balance_type_id);
953      hr_utility.trace('Retro Earnings Spread GT 12 Mths Amount               ===>' || p_fw_balance_type_tab(6).balance_type_id);
954      hr_utility.trace('Retro Pre Tax GT 12 Mths Amount               ===>' || p_fw_balance_type_tab(7).balance_type_id);
955      hr_utility.trace('Total_Tax_Deductions               ===>' || p_fw_balance_type_tab(8).balance_type_id);
956      hr_utility.trace('Termination Deductions               ===>' || p_fw_balance_type_tab(9).balance_type_id);
957      hr_utility.trace('Lump Sum C Deductions               ===>' || p_fw_balance_type_tab(10).balance_type_id);
958      hr_utility.trace('Foreign Tax Deductions               ===>' || p_fw_balance_type_tab(11).balance_type_id);
959      hr_utility.trace('Lump Sum A Payments               ===>' || p_fw_balance_type_tab(12).balance_type_id);
960      hr_utility.trace('Lump Sum D Payments               ===>' || p_fw_balance_type_tab(13).balance_type_id);
961      hr_utility.trace('Reportable Employer Superannuation Contributions               ===>' || p_fw_balance_type_tab(14).balance_type_id);
962      hr_utility.trace('Union Fees               ===>' || p_fw_balance_type_tab(15).balance_type_id);
963      hr_utility.trace('CDEP               ===>' || p_fw_balance_type_tab(16).balance_type_id);
964      hr_utility.trace('Exempt Foreign Employment Income               ===>' || p_fw_balance_type_tab(17).balance_type_id);
965      hr_utility.trace('Retro LT 12 Mths Prev Yr Amount              ===>' || p_fw_balance_type_tab(18).balance_type_id);
966      hr_utility.trace('Retro Earnings Leave Loading LT 12 Mths Prev Yr Amount               ===>' || p_fw_balance_type_tab(19).balance_type_id);
967      hr_utility.trace('Retro Earnings Spread LT 12 Mths Prev Yr Amount               ===>' || p_fw_balance_type_tab(20).balance_type_id);
968      hr_utility.trace('Retro Pre Tax LT 12 Mths Prev Yr Amount               ===>' || p_fw_balance_type_tab(21).balance_type_id);
969      hr_utility.trace('Retro LT 12 Mths Curr Yr Amount               ===>' || p_fw_balance_type_tab(22).balance_type_id);
970      hr_utility.trace('Retro Earnings Leave Loading LT 12 Mths Curr Yr Amount               ===>' || p_fw_balance_type_tab(23).balance_type_id);
971      hr_utility.trace('Retro Earnings Spread LT 12 Mths Curr Amount               ===>' || p_fw_balance_type_tab(24).balance_type_id);
972      hr_utility.trace('Retro Tax GT12 Amount               ===>' || p_fw_balance_type_tab(25).balance_type_id);
973      hr_utility.trace('Retro Tax LT12 Prev Amount               ===>' || p_fw_balance_type_tab(26).balance_type_id);
974      hr_utility.trace('Retro Tax LT12 Curr Amount               ===>' || p_fw_balance_type_tab(27).balance_type_id);
975      hr_utility.trace('Retro Earnings Additional GT 12 Mths Amount               ===>' || p_fw_balance_type_tab(29).balance_type_id);
976      hr_utility.trace('Retro Earnings Additional LT12 Prev Mths Amount               ===>' || p_fw_balance_type_tab(30).balance_type_id);
977      hr_utility.trace('Retro Earnings Additional LT12 Curr Mths Amount               ===>' || p_fw_balance_type_tab(31).balance_type_id);
978   end if;
979 
980   p_fw_alw_balance_type_tab.delete;
981   for csr_rec in c_get_fw_alw_bal_type_id loop
982      p_fw_alw_balance_type_tab(cnt).balance_type_id := csr_rec.balance_type_id;
983      cnt := cnt + 1;
984   end loop;
985 
986 p_fw_union_balance_type_tab.delete;
987   for csr_rec in c_get_fw_union_bal_type_id loop
988      p_fw_union_balance_type_tab(idx).balance_type_id := csr_rec.balance_type_id;
989      idx := idx + 1;
990   end loop;
991 
992 /* End of bug9147430 */
993 
994 
995 exception
996   when others then
997   if g_debug then
998      hr_utility.set_location('Error in initialization_code',100);
999   end if;
1000   raise;
1001 
1002 end initialization_code;
1003 
1004 
1005 /*
1006     Bug 7138494 - Added Function range_person_on
1007 --------------------------------------------------------------------
1008     Name  : range_person_on
1009     Type  : Function
1010     Access: Private
1011     Description: Checks if RANGE_PERSON_ID is enabled for
1012                  Archive process.
1013   --------------------------------------------------------------------
1014 */
1015 
1016 FUNCTION range_person_on
1017 RETURN BOOLEAN
1018 IS
1019 
1020  CURSOR csr_action_parameter is
1021   select parameter_value
1022   from pay_action_parameters
1023   where parameter_name = 'RANGE_PERSON_ID';
1024 
1025  CURSOR csr_range_format_param is
1026   select par.parameter_value
1027   from   pay_report_format_parameters par,
1028          pay_report_format_mappings_f map
1029   where  map.report_format_mapping_id = par.report_format_mapping_id
1030   and    map.report_type = 'AU_PAYMENT_SUMMARY'
1031   and    map.report_format = 'AU_PAYMENT_SUMMARY'
1032   and    map.report_qualifier = 'AU'
1033   and    par.parameter_name = 'RANGE_PERSON_ID'; -- Bug fix 5567246
1034 
1035   l_return boolean;
1036   l_action_param_val varchar2(30);
1037   l_report_param_val varchar2(30);
1038 
1039 BEGIN
1040 
1041     g_debug := hr_utility.debug_enabled;
1042 
1043     IF g_debug
1044     THEN
1045         hr_utility.set_location('range_person_on',10);
1046     END IF;
1047 
1048   BEGIN
1049 
1050     open csr_action_parameter;
1051     fetch csr_action_parameter into l_action_param_val;
1052     close csr_action_parameter;
1053 
1054     IF g_debug
1055     THEN
1056         hr_utility.set_location('range_person_on',20);
1057     END IF;
1058 
1059     open csr_range_format_param;
1060     fetch csr_range_format_param into l_report_param_val;
1061     close csr_range_format_param;
1062     IF g_debug
1063     THEN
1064         hr_utility.set_location('range_person_on',30);
1065     END IF;
1066   EXCEPTION WHEN NO_DATA_FOUND THEN
1067      l_return := FALSE;
1068   END;
1069   --
1070     IF g_debug
1071     THEN
1072         hr_utility.set_location('range_person_on',40);
1073     END IF;
1074 
1075   IF l_action_param_val = 'Y' AND l_report_param_val = 'Y' THEN
1076      l_return := TRUE;
1077 
1078     IF g_debug
1079     THEN
1080              hr_utility.trace('Range Person = True');
1081     END IF;
1082   ELSE
1083      l_return := FALSE;
1084   END IF;
1085 --
1086  RETURN l_return;
1087 --
1088 END range_person_on;
1089 
1090   --------------------------------------------------------------------+
1091   -- This procedure further restricts the assignment_id's
1092   -- returned by range_code
1093   --------------------------------------------------------------------+
1094 
1095 -- this procedure filters the assignments selected by range_code procedure
1096 -- it then calls hr_nonrun.insact to create an assignment  id
1097 -- the cursor to select assignment action selects  three types of employees
1098 -- all current assignments eligible  for archival ,
1099 -- terminated employees eligible for archival
1100 -- and all those assignment ids who have received a Fringe benifit
1101 -- component of more than $1000 for that FBT year
1102 -- and have been terminated during the FBT year
1103 
1104 procedure assignment_action_code
1105     (p_payroll_action_id  in pay_payroll_actions.payroll_action_id%type,
1106      p_start_person_id    in per_all_people_f.person_id%type,
1107      p_end_person_id      in per_all_people_f.person_id%type,
1108      p_chunk              in number) is
1109 
1110 
1111      v_next_action_id  pay_assignment_actions.assignment_action_id%type;
1112 
1113      v_lst_year_start       date ;
1114      v_fbt_year_start       date ;
1115      v_lst_fbt_year_start   date ; --Bug#3661230
1116      v_fbt_year_end         date ;
1117      v_fin_year_start       date ;
1118      v_fin_year_end         date ;
1119      v_assignment_id        varchar2(50);
1120      v_registered_employer  varchar2(50);
1121      v_financial_year       varchar2(50);
1122      v_payroll_id           varchar2(50);
1123      v_employee_type        varchar2(1);
1124      v_asg_id               number;
1125      v_reg_emp              number;
1126      l_lst_yr_term          varchar(10); --Bug#3661230
1127      v_assignment_set_id    VARCHAR2(50); --Bug#13043341
1128      v_count                NUMBER; --Bug#13043341
1129 
1130 -- Bug 13043341
1131 
1132      cursor cur_inc_exc_flag
1133      is
1134      select distinct hasa.include_or_exclude inc_or_exc
1135      from
1136     hr_assignment_set_amendments hasa,
1137     hr_assignment_sets has
1138      where hasa.assignment_set_id = has.assignment_set_id
1139      and    has.business_group_id  = g_business_group_id
1140      and    has.assignment_set_id  = v_assignment_set_id;
1141 
1142     l_cur_inc_exc_flag_rec cur_inc_exc_flag%rowtype;
1143 
1144      ----------------------------------------------+
1145      -- cursor to get the archive parameters
1146      ----------------------------------------------+
1147   cursor get_params(c_payroll_action_id  per_all_assignments_f.assignment_id%type)
1148   is
1149   select to_date('01-07-'||substr(pay_core_utils.get_parameter('FINANCIAL_YEAR',legislative_parameters),1,4),'DD-MM-YYYY') Financial_year_start
1150         ,to_date('30-06-'||substr(pay_core_utils.get_parameter('FINANCIAL_YEAR',legislative_parameters),6,4),'DD-MM-YYYY') Financial_year_end
1151         ,to_date('01-04-'||substr(pay_core_utils.get_parameter('FINANCIAL_YEAR',legislative_parameters),1,4),'DD-MM-YYYY') FBT_year_start
1152         ,to_date('30-06-'||substr(pay_core_utils.get_parameter('FINANCIAL_YEAR',legislative_parameters),1,4),'DD-MM-YYYY') FBT_year_end
1153         ,decode(pay_core_utils.get_parameter('EMPLOYEE_TYPE',legislative_parameters),'C','Y','T','N','B','%')   Employee_type
1154         ,pay_core_utils.get_parameter('REGISTERED_EMPLOYER',legislative_parameters)                             Registered_Employer
1155         ,pay_core_utils.get_parameter('FINANCIAL_YEAR',legislative_parameters)                                  Financial_year
1156         ,decode(pay_core_utils.get_parameter('ASSIGNMENT_ID',legislative_parameters),null,'%',
1157                 pay_core_utils.get_parameter('ASSIGNMENT_ID',legislative_parameters))               Assignment_id
1158         ,decode(pay_core_utils.get_parameter('PAYROLL',legislative_parameters),null,'%',pay_core_utils.get_parameter('PAYROLL',legislative_parameters)) payroll_id
1159                ,pay_core_utils.get_parameter('LST_YR_TERM',legislative_parameters)              lst_yr_term    /*3661230*/
1160         ,pay_core_utils.get_parameter('BUSINESS_GROUP_ID',legislative_parameters)               Business_group_id
1161     ,decode(pay_core_utils.get_parameter('ASSIGNMENT_SET_ID',legislative_parameters),null,'%',
1162                 pay_core_utils.get_parameter('ASSIGNMENT_SET_ID',legislative_parameters))         Assignment_set_id /*Bug 13043341*/
1163   from  pay_payroll_actions
1164   where payroll_action_id = c_payroll_Action_id;
1165 
1166      ----------------------------------------------+
1167      -- cursor to restrict assignment ids
1168      ----------------------------------------------+
1169 /* 4926521 Modified adn put group by in 2 sub queries for performacne */
1170 /* 5099419 Removed fix for bug 4926521 */
1171   Cursor process_assignments(c_payroll_action_id  in pay_payroll_actions.payroll_action_id%type,
1172                            c_start_person_id    in per_all_people_f.person_id%type,
1173                            c_end_person_id      in per_all_people_f.person_id%type)
1174       is
1175  select /*+ INDEX(p per_people_f_pk)
1176             INDEX(a per_assignments_f_fk1)
1177             INDEX(a per_assignments_f_N12)
1178             INDEX(pa pay_payroll_actions_pk)
1179             INDEX(pps per_periods_of_service_n3)
1180         */ distinct a.assignment_id
1181     from  per_people_f      p /*Bug3043049*/
1182          ,per_assignments_f a /*Bug3043049*/
1183          ,pay_payroll_actions   pa
1184          ,per_periods_of_service  pps
1185    where  pa.payroll_action_id       = c_payroll_action_id
1186     and   p.person_id                between c_start_person_id and c_end_person_id
1187     and   p.person_id                = a.person_id
1188     and   decode(pps.actual_termination_date,null,'Y',decode(sign(pps.actual_termination_date - (v_fin_year_end)),1,'Y','N')) LIKE v_employee_type --Bug#3744930
1189     and pps.period_of_service_id = a.period_of_service_id
1190     and   a.business_group_id        = pa.business_group_id
1191     and   to_char(a.assignment_id)      like v_assignment_id
1192     and   pps.person_id              = p.person_id
1193     and   nvl(pps.actual_termination_date, v_lst_year_start) >= v_lst_year_start  -- Bug3661230, Bug3048724 ,Bug 3263659
1194     and v_fin_year_end between p.effective_start_date and p.effective_end_date
1195     --  and   least(nvl(pps.actual_termination_date,v_fin_year_end),v_fin_year_end) between a.effective_start_date and a.effective_end_date -- Bug 3815301
1196     and   a.effective_end_date = (select max(effective_end_date) /* 4377367 */
1197                               From  per_assignments_f iipaf
1198                   WHERE iipaf.assignment_id  = a.assignment_id
1199                     and iipaf.effective_end_date >= v_fbt_year_start
1200                     and iipaf.effective_start_date <= v_fin_year_end
1201                 AND iipaf.payroll_id IS NOT NULL) /*Bug# 4653934*/
1202     and   a.payroll_id like v_payroll_id  -- Bug 3815301
1203     AND   EXISTS
1204          (select /*+ ORDERED */''
1205            from
1206                   per_assignments_f             paaf
1207                  ,pay_assignment_actions        rpac
1208                  ,pay_payroll_actions           rppa
1209            where rppa.effective_date between  v_fin_year_start  and v_fin_year_end
1210            and  rppa.action_type            in ('R','Q','B','I')
1211            and  rpac.tax_unit_id            = v_reg_emp
1212            and  rppa.payroll_action_id      = rpac.payroll_action_id
1213            and  rpac.action_status          ='C'
1214            and  rpac.assignment_id          = a.assignment_id
1215            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1216            and  paaf.assignment_id          = a.assignment_id
1217            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1218            UNION
1219            select /*+ ORDERED */''
1220            from
1221                   per_assignments_f             paaf
1222                  ,pay_assignment_actions        rpac
1223                  ,pay_payroll_actions           rppa
1224            where pps.actual_termination_date between v_lst_fbt_year_start  and v_fbt_year_end /*Bug3263659 */ --Bug#3661230
1225            and rppa.effective_date between  v_fbt_year_start  and v_fbt_year_end
1226            and pay_balance_pkg.get_value(g_fbt_defined_balance_id,rpac.assignment_action_id
1227                             + decode(rppa.payroll_id, 0, 0, 0),v_reg_emp,null,null,null,null) > to_number(g_fbt_threshold) /* Bug 5708255 */ --2610141
1228            and  rppa.action_type            in ('R','Q','B','I')
1229            and  rpac.tax_unit_id            = v_reg_emp
1230            and  rppa.payroll_action_id      = rpac.payroll_action_id
1231            and  rpac.action_status          ='C'
1232            and  rpac.assignment_id          = a.assignment_id
1233            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1234            and  paaf.assignment_id          = a.assignment_id
1235            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1236            )
1237     and not exists
1238          (select  distinct paat.assignment_id
1239                  from  pay_action_interlocks  pail,
1240                        pay_assignment_actions paat,
1241                        pay_payroll_actions paas
1242                  where paat.assignment_id   = a.assignment_id
1243                    and paas.action_type     ='X'
1244                    and paas.action_status   ='C'
1245                    and paas.report_type     ='AU_PAYMENT_SUMMARY_REPORT'
1246                    and pail.locking_action_id  = paat.assignment_action_id
1247                    and paat.payroll_action_id = paas.payroll_action_id
1248                    and pay_core_utils.get_parameter('FINANCIAL_YEAR',paas.legislative_parameters) = v_financial_year
1249            and pay_core_utils.get_parameter('REGISTERED_EMPLOYER',paas.legislative_parameters) = v_reg_emp
1250            )
1251          and not exists ( select  aei_information1
1252                  from  per_assignment_extra_info,
1253                        hr_lookups
1254                 where  assignment_id        = a.assignment_id
1255                   and aei_information_category = 'HR_PS_ISSUE_DATE_AU'  --Bug 12744254
1256                   and  aei_information1     is not null
1257                   and  aei_information1     = lookup_code
1258           and  nvl(aei_information2,v_reg_emp)     = decode(aei_information2,'-999',aei_information2,v_reg_emp) -- 2610141 and 4000955
1259                   and lookup_type ='AU_PS_FINANCIAL_YEAR'
1260                   and meaning = v_financial_year
1261          );
1262 
1263 /*Bug 4000955  Modified subquery of process_assignments not to archive employees for
1264                any legal employer if Manual PS is issued for 'ALL' legal employers or without any legal employer
1265            If the Manual PS is issued for 'ALL' the legal employers the aei_information2 would be -999 done
1266            in the view HR_AU_LEG_EMP_AEI_V */
1267 
1268 /* Cursor added for bug3019374 -- Processed when a single assignment is entered*/
1269 
1270 
1271   Cursor process_assignments_only(c_payroll_action_id  in pay_payroll_actions.payroll_action_id%type,
1272                            c_start_person_id    in per_all_people_f.person_id%type,
1273                            c_end_person_id      in per_all_people_f.person_id%type)
1274       is
1275  select  distinct a.assignment_id
1276    from  per_people_f      p /*Bug3043049*/
1277          ,per_assignments_f a /*Bug3043049*/
1278          ,pay_payroll_actions   pa
1279          ,per_periods_of_service  pps
1280    where  pa.payroll_action_id       = c_payroll_action_id
1281     and   p.person_id                between c_start_person_id and c_end_person_id
1282     and   p.person_id                = a.person_id
1283     and decode(pps.actual_termination_date,null,'Y',decode(sign(pps.actual_termination_date - (v_fin_year_end)),1,'Y','N')) LIKE v_employee_type ----Bug#3744930
1284     and pps.period_of_service_id = a.period_of_service_id
1285     and   a.business_group_id        = pa.business_group_id
1286     and   a.assignment_id      = v_assignment_id
1287     and   pps.person_id              = p.person_id
1288     and   nvl(pps.actual_termination_date, v_lst_year_start) >= v_lst_year_start   -- Bug3661230 , Bug3048724, Bug 3263659
1289     and   v_fin_year_end between p.effective_start_date and p.effective_end_date
1290 --    and   least(nvl(pps.actual_termination_date,v_fin_year_end),v_fin_year_end) between a.effective_start_date and a.effective_end_date -- Bug 3815301
1291     and   a.effective_end_date = (select max(effective_end_date) /* 4377367 */
1292                                        From  per_assignments_f iipaf
1293                                        WHERE iipaf.assignment_id  = a.assignment_id
1294                                        and iipaf.effective_end_date >= v_fbt_year_start
1295                                        and iipaf.effective_start_date <= v_fin_year_end
1296                                   AND iipaf.payroll_id IS NOT NULL) /*Bug# 4653934*/
1297     and   a.payroll_id like v_payroll_id  -- Bug 3815301
1298     AND   EXISTS
1299          (select /*+ ORDERED */''
1300            from
1301                   per_assignments_f             paaf
1302                  ,pay_assignment_actions        rpac
1303                  ,pay_payroll_actions           rppa
1304            where rppa.effective_date between  v_fin_year_start  and v_fin_year_end
1305            and  rppa.action_type            in ('R','Q','B','I')
1306            and  rpac.tax_unit_id            = v_reg_emp
1307            and  rppa.payroll_action_id      = rpac.payroll_action_id
1308            and  rpac.action_status          ='C'
1309            and  rpac.assignment_id          = v_assignment_id
1310            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1311            and  paaf.assignment_id          = v_assignment_id
1312            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1313            UNION
1314            select /*+ ORDERED */''
1315            from
1316                   per_assignments_f             paaf
1317                  ,pay_assignment_actions        rpac
1318                  ,pay_payroll_actions           rppa
1319            where pps.actual_termination_date between v_lst_fbt_year_start  and v_fbt_year_end /*Bug3263659 */ --Bug#3661230
1320            and rppa.effective_date between  v_fbt_year_start  and v_fbt_year_end
1321            and pay_balance_pkg.get_value(g_fbt_defined_balance_id,rpac.assignment_action_id
1322                             + decode(rppa.payroll_id, 0, 0, 0),v_reg_emp,null,null,null,null) > to_number(g_fbt_threshold) /* Bug 5708255 */ --2610141
1323            and  rppa.action_type            in ('R','Q','B','I')
1324            and  rpac.tax_unit_id            = v_reg_emp
1325            and  rppa.payroll_action_id      = rpac.payroll_action_id
1326            and  rpac.action_status          ='C'
1327            and  rpac.assignment_id          = v_assignment_id
1328            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1329            and  paaf.assignment_id          = v_assignment_id
1330            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1331            )
1332     and not exists
1333          (select  distinct paat.assignment_id
1334                  from  pay_action_interlocks  pail,
1335                        pay_assignment_actions paat,
1336                        pay_payroll_actions paas
1337                  where paat.assignment_id   = a.assignment_id
1338                    and paas.action_type     ='X'
1339                    and paas.action_status   ='C'
1340                    and paas.report_type     ='AU_PAYMENT_SUMMARY_REPORT'
1341                    and pail.locking_action_id  = paat.assignment_action_id
1342                    and paat.payroll_action_id = paas.payroll_action_id
1343                    and pay_core_utils.get_parameter('FINANCIAL_YEAR',paas.legislative_parameters) = v_financial_year
1344            and pay_core_utils.get_parameter('REGISTERED_EMPLOYER',paas.legislative_parameters) = v_reg_emp) --2610141
1345          and not exists ( select  aei_information1
1346                  from  per_assignment_extra_info,
1347                        hr_lookups
1348                 where  assignment_id        = a.assignment_id
1349                   and aei_information_category = 'HR_PS_ISSUE_DATE_AU'  --Bug 12744254
1350                   and  aei_information1     is not null
1351                   and  aei_information1     = lookup_code
1352           and  nvl(aei_information2,v_reg_emp)     = decode(aei_information2,'-999',aei_information2,v_reg_emp) -- 2610141 and 4000955
1353                   and lookup_type ='AU_PS_FINANCIAL_YEAR'
1354                   and meaning = v_financial_year);
1355 
1356 
1357 /*Bug 4000955  Modified subquery of process_assignments_only not to archive employees for
1358                any legal employer if Manual PS is issued for 'ALL' legal employers or without any legal employer
1359            If the Manual PS is issued for 'ALL' the legal employers the aei_information2 would be -999 done
1360            in the view HR_AU_LEG_EMP_AEI_V
1361 */
1362 
1363 
1364 /*
1365    Bug 7138494 - Added Cursor for Range Person
1366                - Uses person_id in pay_population_ranges
1367   --------------------------------------------------------------------+
1368   -- Cursor      : range_process_assignments
1369   -- Description : Fetches assignments For Recconciling Payment Summary
1370   --               Returns DISTINCT assignment_id
1371   --               Used when RANGE_PERSON_ID feature is enabled
1372   --------------------------------------------------------------------+
1373 */
1374 
1375 CURSOR range_process_assignments(c_payroll_action_id  IN pay_payroll_actions.payroll_action_id%TYPE,
1376                                  c_chunk IN NUMBER)
1377 IS
1378 SELECT a.assignment_id
1379     FROM  per_people_f      p /*Bug3043049*/
1380          ,per_assignments_f a /*Bug3043049*/
1381          ,pay_payroll_actions   pa
1382          ,per_periods_of_service  pps
1383          ,pay_population_ranges   ppr
1384    WHERE  pa.payroll_action_id       = c_payroll_action_id
1385     AND   pa.payroll_action_id       = ppr.payroll_action_id
1386     AND ppr.payroll_action_id = c_payroll_action_id
1387     AND   ppr.chunk_number           = c_chunk
1388     AND   p.person_id                = ppr.person_id
1389     AND   p.person_id                = a.person_id
1390     AND a.person_id = ppr.person_id
1391     AND   decode(pps.actual_termination_date,null,'Y',decode(sign(pps.actual_termination_date - (v_fin_year_end)),1,'Y','N')) LIKE v_employee_type --Bug#3744930
1392     AND   pps.period_of_service_id = a.period_of_service_id
1393     AND   a.business_group_id        = pa.business_group_id
1394     AND   to_char(a.assignment_id)      LIKE v_assignment_id
1395     AND   pps.person_id              = p.person_id
1396     AND   nvl(pps.actual_termination_date, v_lst_year_start) >= v_lst_year_start  -- Bug3661230, Bug3048724 ,Bug 3263659
1397     AND   v_fin_year_end BETWEEN p.effective_start_date AND p.effective_end_date
1398     AND   a.effective_end_date = (SELECT MAX(effective_end_date) /* 4377367 */
1399                                   FROM  per_assignments_f iipaf
1400                                   WHERE iipaf.assignment_id  = a.assignment_id
1401                                   AND iipaf.person_id = a.person_id
1402                                   AND iipaf.effective_end_date >= v_fbt_year_start
1403                                   AND iipaf.effective_start_date <= v_fin_year_end
1404                                   AND iipaf.payroll_id IS NOT NULL) /*Bug# 4653934*/
1405     AND   a.payroll_id LIKE v_payroll_id  -- Bug 3815301
1406     AND   EXISTS
1407          (select /*+ ORDERED */''
1408            from
1409                   per_assignments_f             paaf
1410                  ,pay_assignment_actions        rpac
1411                  ,pay_payroll_actions           rppa
1412            where rppa.effective_date between  v_fin_year_start  and v_fin_year_end
1413            and  rppa.action_type            in ('R','Q','B','I')
1414            and  rpac.tax_unit_id            = v_reg_emp
1415            and  rppa.payroll_action_id      = rpac.payroll_action_id
1416            and  rpac.action_status          ='C'
1417            and  rpac.assignment_id          = a.assignment_id
1418            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1419            and  paaf.assignment_id          = a.assignment_id
1420            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1421            UNION
1422            select /*+ ORDERED */''
1423            from
1424                   per_assignments_f             paaf
1425                  ,pay_assignment_actions        rpac
1426                  ,pay_payroll_actions           rppa
1427            where pps.actual_termination_date between v_lst_fbt_year_start  and v_fbt_year_end /*Bug3263659 */ --Bug#3661230
1428            and rppa.effective_date between  v_fbt_year_start  and v_fbt_year_end
1429            and pay_balance_pkg.get_value(g_fbt_defined_balance_id,rpac.assignment_action_id
1430                             + decode(rppa.payroll_id, 0, 0, 0),v_reg_emp,null,null,null,null) > to_number(g_fbt_threshold) /* Bug 5708255 */ --2610141
1431            and  rppa.action_type            in ('R','Q','B','I')
1432            and  rpac.tax_unit_id            = v_reg_emp
1433            and  rppa.payroll_action_id      = rpac.payroll_action_id
1434            and  rpac.action_status          ='C'
1435            and  rpac.assignment_id          = a.assignment_id
1436            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1437            and  paaf.assignment_id          = a.assignment_id
1438            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1439            )
1440     AND NOT EXISTS
1441          (SELECT  paat.assignment_id
1442             FROM  pay_action_interlocks  pail,
1443                  pay_assignment_actions paat,
1444                  pay_payroll_actions paas
1445             WHERE paat.assignment_id   = a.assignment_id
1446             AND paas.action_type     ='X'
1447             AND paas.action_status   ='C'
1448             AND paas.report_type     ='AU_PAYMENT_SUMMARY_REPORT'
1449             AND pail.locking_action_id  = paat.assignment_action_id
1450             AND paat.payroll_action_id = paas.payroll_action_id
1451             AND pay_core_utils.get_parameter('FINANCIAL_YEAR',paas.legislative_parameters) = v_financial_year
1452             AND pay_core_utils.get_parameter('REGISTERED_EMPLOYER',paas.legislative_parameters) = v_reg_emp
1453            )
1454     AND NOT EXISTS
1455          ( SELECT  aei_information1
1456             FROM  per_assignment_extra_info,
1457                   hr_lookups
1458             WHERE  assignment_id        = a.assignment_id
1459             AND aei_information_category = 'HR_PS_ISSUE_DATE_AU'  --Bug 12744254
1460             AND  aei_information1       IS NOT NULL
1461             AND  aei_information1       = lookup_code
1462             AND  nvl(aei_information2,v_reg_emp)     = decode(aei_information2,'-999',aei_information2,v_reg_emp) -- 2610141 and 4000955
1463             AND lookup_type             ='AU_PS_FINANCIAL_YEAR'
1464             AND meaning                 = v_financial_year
1465          );
1466 
1467   cursor   next_action_id is
1468   select   pay_assignment_actions_s.nextval
1469   from   dual;
1470 
1471 /* Bug 5708255 */
1472   -------------------------------------------
1473   -- Added cursor to get value of global FBT_THRESHOLD
1474   --------------------------------------------
1475 CURSOR  c_get_fbt_global(c_year_end DATE)
1476        IS
1477    SELECT  global_value
1478    FROM   ff_globals_f
1479     WHERE  global_name = 'FBT_THRESHOLD'
1480     AND    legislation_code = 'AU'
1481     AND    c_year_end BETWEEN effective_start_date
1482                           AND effective_end_date ;
1483 
1484 --amit
1485 
1486 Cursor c_fbt_balance is
1487   select    pdb.defined_balance_id
1488   from      pay_balance_types            pbt,
1489         pay_defined_balances         pdb,
1490         pay_balance_dimensions       pbd
1491   where  pbt.balance_name               ='Fringe Benefits'
1492   and  pbt.balance_type_id            = pdb.balance_type_id
1493   and  pdb.balance_dimension_id       = pbd.balance_dimension_id /* Bug 2501105 */
1494   and  pbd.legislation_code           ='AU'
1495   and  pbd.dimension_name             ='_ASG_LE_FBT_YTD' --2610141
1496   and  pbd.legislation_code = pbt.legislation_code
1497   and  pbd.legislation_code = pdb.legislation_code;
1498 
1499 /*
1500    Bug 13043341- Added Cursor for Range Person
1501                - Uses person_id in pay_population_ranges
1502   -------------------------------------------------------------------------+
1503   -- Cursor      : range_process_assignment_set
1504   -- Description : Fetches assignments For Archival, process assignment set
1505   --           parameter.
1506   --               Returns DISTINCT assignment_id
1507   --               Used when RANGE_PERSON_ID feature is enabled
1508   --------------------------------------------------------------------------+
1509 */
1510 
1511 CURSOR range_process_assignment_set(c_payroll_action_id  IN pay_payroll_actions.payroll_action_id%TYPE,
1512                                  c_chunk IN NUMBER)
1513 IS
1514 SELECT a.assignment_id
1515     FROM  per_people_f      p
1516          ,per_assignments_f a
1517          ,pay_payroll_actions   pa
1518          ,per_periods_of_service  pps
1519          ,pay_population_ranges   ppr
1520      ,hr_assignment_set_amendments hasa
1521    WHERE  pa.payroll_action_id       = c_payroll_action_id
1522     AND   pa.payroll_action_id       = ppr.payroll_action_id
1523     AND   ppr.payroll_action_id      = c_payroll_action_id
1524     AND   ppr.chunk_number           = c_chunk
1525     AND   p.person_id                = ppr.person_id
1526     AND   p.person_id                = a.person_id
1527     AND   a.person_id                = ppr.person_id
1528     AND   decode(pps.actual_termination_date,null,'Y',decode(sign(pps.actual_termination_date - (v_fin_year_end)),1,'Y','N')) LIKE v_employee_type --Bug#3744930
1529     AND   pps.period_of_service_id   = a.period_of_service_id
1530     AND   a.business_group_id        = pa.business_group_id
1531     AND   hasa.ASSIGNMENT_SET_ID     = to_number(v_assignment_set_id)
1532     AND   a.assignment_id            = hasa.ASSIGNMENT_ID
1533     AND   upper(hasa.INCLUDE_OR_EXCLUDE)='I'
1534     AND   pps.person_id              = p.person_id
1535     AND   nvl(pps.actual_termination_date, v_lst_year_start) >= v_lst_year_start
1536     AND   v_fin_year_end BETWEEN p.effective_start_date AND p.effective_end_date
1537     AND   a.effective_end_date = (SELECT MAX(effective_end_date) /* 4377367 */
1538                                   FROM  per_assignments_f iipaf
1539                                   WHERE iipaf.assignment_id  = a.assignment_id
1540                                   AND iipaf.person_id = a.person_id
1541                                   AND iipaf.effective_end_date >= v_fbt_year_start
1542                                   AND iipaf.effective_start_date <= v_fin_year_end
1543                                   AND iipaf.payroll_id IS NOT NULL)
1544     AND   a.payroll_id LIKE v_payroll_id
1545     AND   EXISTS
1546          (select /*+ ORDERED */''
1547            from
1548                   per_assignments_f             paaf
1549                  ,pay_assignment_actions        rpac
1550                  ,pay_payroll_actions           rppa
1551            where rppa.effective_date between  v_fin_year_start  and v_fin_year_end
1552            and  rppa.action_type            in ('R','Q','B','I')
1553            and  rpac.tax_unit_id            = v_reg_emp
1554            and  rppa.payroll_action_id      = rpac.payroll_action_id
1555            and  rpac.action_status          ='C'
1556            and  rpac.assignment_id          = a.assignment_id
1557            and  rppa.payroll_id             = paaf.payroll_id
1558            and  paaf.assignment_id          = a.assignment_id
1559            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date
1560            UNION
1561            select /*+ ORDERED */''
1562            from
1563                   per_assignments_f             paaf
1564                  ,pay_assignment_actions        rpac
1565                  ,pay_payroll_actions           rppa
1566            where pps.actual_termination_date between v_lst_fbt_year_start  and v_fbt_year_end
1567            and rppa.effective_date between  v_fbt_year_start  and v_fbt_year_end
1568            and pay_balance_pkg.get_value(g_fbt_defined_balance_id,rpac.assignment_action_id
1569                             + decode(rppa.payroll_id, 0, 0, 0),v_reg_emp,null,null,null,null) > to_number(g_fbt_threshold)
1570            and  rppa.action_type            in ('R','Q','B','I')
1571            and  rpac.tax_unit_id            = v_reg_emp
1572            and  rppa.payroll_action_id      = rpac.payroll_action_id
1573            and  rpac.action_status          ='C'
1574            and  rpac.assignment_id          = a.assignment_id
1575            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1576            and  paaf.assignment_id          = a.assignment_id
1577            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1578            )
1579     AND NOT EXISTS
1580          (SELECT  paat.assignment_id
1581             FROM  pay_action_interlocks  pail,
1582                  pay_assignment_actions paat,
1583                  pay_payroll_actions paas
1584             WHERE paat.assignment_id   = a.assignment_id
1585             AND paas.action_type     ='X'
1586             AND paas.action_status   ='C'
1587             AND paas.report_type     ='AU_PAYMENT_SUMMARY_REPORT'
1588             AND pail.locking_action_id  = paat.assignment_action_id
1589             AND paat.payroll_action_id = paas.payroll_action_id
1590             AND pay_core_utils.get_parameter('FINANCIAL_YEAR',paas.legislative_parameters) = v_financial_year
1591             AND pay_core_utils.get_parameter('REGISTERED_EMPLOYER',paas.legislative_parameters) = v_reg_emp
1592            )
1593     AND NOT EXISTS
1594          ( SELECT  aei_information1
1595             FROM  per_assignment_extra_info,
1596                   hr_lookups
1597             WHERE  assignment_id        = a.assignment_id
1598             AND aei_information_category = 'HR_PS_ISSUE_DATE_AU'
1599             AND  aei_information1       IS NOT NULL
1600             AND  aei_information1       = lookup_code
1601             AND  nvl(aei_information2,v_reg_emp)     = decode(aei_information2,'-999',aei_information2,v_reg_emp)
1602             AND lookup_type             ='AU_PS_FINANCIAL_YEAR'
1603             AND meaning                 = v_financial_year
1604          );
1605 
1606 /* Bug 13043341 Process assignment set when RANGE_PERSON_ID feature is diabled */
1607   Cursor process_assignment_set(c_payroll_action_id  in pay_payroll_actions.payroll_action_id%type,
1608                            c_start_person_id    in per_all_people_f.person_id%type,
1609                            c_end_person_id      in per_all_people_f.person_id%type)
1610       is
1611  select /*+ INDEX(p per_people_f_pk)
1612             INDEX(a per_assignments_f_fk1)
1613             INDEX(a per_assignments_f_N12)
1614             INDEX(pa pay_payroll_actions_pk)
1615             INDEX(pps per_periods_of_service_n3)
1616         */ distinct a.assignment_id
1617     from  per_people_f      p /*Bug3043049*/
1618          ,per_assignments_f a /*Bug3043049*/
1619          ,pay_payroll_actions   pa
1620          ,per_periods_of_service  pps
1621          ,hr_assignment_set_amendments hasa
1622    where  pa.payroll_action_id       = c_payroll_action_id
1623     and   p.person_id                between c_start_person_id and c_end_person_id
1624     and   p.person_id                = a.person_id
1625     and   decode(pps.actual_termination_date,null,'Y',decode(sign(pps.actual_termination_date - (v_fin_year_end)),1,'Y','N')) LIKE v_employee_type --Bug#3744930
1626     and pps.period_of_service_id = a.period_of_service_id
1627     and   a.business_group_id        = pa.business_group_id
1628     AND   hasa.ASSIGNMENT_SET_ID     = to_number(v_assignment_set_id)
1629     AND   a.assignment_id            = hasa.ASSIGNMENT_ID
1630     AND   upper(hasa.INCLUDE_OR_EXCLUDE)='I'
1631     and   pps.person_id              = p.person_id
1632     and   nvl(pps.actual_termination_date, v_lst_year_start) >= v_lst_year_start  -- Bug3661230, Bug3048724 ,Bug 3263659
1633     and v_fin_year_end between p.effective_start_date and p.effective_end_date
1634     --  and   least(nvl(pps.actual_termination_date,v_fin_year_end),v_fin_year_end) between a.effective_start_date and a.effective_end_date -- Bug 3815301
1635     and   a.effective_end_date = (select max(effective_end_date) /* 4377367 */
1636                               From  per_assignments_f iipaf
1637                   WHERE iipaf.assignment_id  = a.assignment_id
1638                     and iipaf.effective_end_date >= v_fbt_year_start
1639                     and iipaf.effective_start_date <= v_fin_year_end
1640                 AND iipaf.payroll_id IS NOT NULL) /*Bug# 4653934*/
1641     and   a.payroll_id like v_payroll_id  -- Bug 3815301
1642     AND   EXISTS
1643          (select /*+ ORDERED */''
1644            from
1645                   per_assignments_f             paaf
1646                  ,pay_assignment_actions        rpac
1647                  ,pay_payroll_actions           rppa
1648            where rppa.effective_date between  v_fin_year_start  and v_fin_year_end
1649            and  rppa.action_type            in ('R','Q','B','I')
1650            and  rpac.tax_unit_id            = v_reg_emp
1651            and  rppa.payroll_action_id      = rpac.payroll_action_id
1652            and  rpac.action_status          ='C'
1653            and  rpac.assignment_id          = a.assignment_id
1654            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1655            and  paaf.assignment_id          = a.assignment_id
1656            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1657            UNION
1658            select /*+ ORDERED */''
1659            from
1660                   per_assignments_f             paaf
1661                  ,pay_assignment_actions        rpac
1662                  ,pay_payroll_actions           rppa
1663            where pps.actual_termination_date between v_lst_fbt_year_start  and v_fbt_year_end /*Bug3263659 */ --Bug#3661230
1664            and rppa.effective_date between  v_fbt_year_start  and v_fbt_year_end
1665            and pay_balance_pkg.get_value(g_fbt_defined_balance_id,rpac.assignment_action_id
1666                             + decode(rppa.payroll_id, 0, 0, 0),v_reg_emp,null,null,null,null) > to_number(g_fbt_threshold) /* Bug 5708255 */ --2610141
1667            and  rppa.action_type            in ('R','Q','B','I')
1668            and  rpac.tax_unit_id            = v_reg_emp
1669            and  rppa.payroll_action_id      = rpac.payroll_action_id
1670            and  rpac.action_status          ='C'
1671            and  rpac.assignment_id          = a.assignment_id
1672            and  rppa.payroll_id             = paaf.payroll_id  /*Bug 3815301 */
1673            and  paaf.assignment_id          = a.assignment_id
1674            and  rppa.effective_date between paaf.effective_start_date and paaf.effective_end_date /*Bug 3815301 */
1675            )
1676     and not exists
1677          (select  distinct paat.assignment_id
1678                  from  pay_action_interlocks  pail,
1679                        pay_assignment_actions paat,
1680                        pay_payroll_actions paas
1681                  where paat.assignment_id   = a.assignment_id
1682                    and paas.action_type     ='X'
1683                    and paas.action_status   ='C'
1684                    and paas.report_type     ='AU_PAYMENT_SUMMARY_REPORT'
1685                    and pail.locking_action_id  = paat.assignment_action_id
1686                    and paat.payroll_action_id = paas.payroll_action_id
1687                    and pay_core_utils.get_parameter('FINANCIAL_YEAR',paas.legislative_parameters) = v_financial_year
1688            and pay_core_utils.get_parameter('REGISTERED_EMPLOYER',paas.legislative_parameters) = v_reg_emp
1689            )
1690          and not exists ( select  aei_information1
1691                  from  per_assignment_extra_info,
1692                        hr_lookups
1693                 where  assignment_id        = a.assignment_id
1694                   and aei_information_category = 'HR_PS_ISSUE_DATE_AU'  --Bug 12744254
1695                   and  aei_information1     is not null
1696                   and  aei_information1     = lookup_code
1697           and  nvl(aei_information2,v_reg_emp)     = decode(aei_information2,'-999',aei_information2,v_reg_emp) -- 2610141 and 4000955
1698                   and lookup_type ='AU_PS_FINANCIAL_YEAR'
1699                   and meaning = v_financial_year
1700          );
1701 
1702 begin
1703   g_debug := hr_utility.debug_enabled;
1704 
1705 
1706   IF g_debug THEN
1707       hr_utility.set_location('Start of assignment_action_code',1);
1708   END IF;
1709   -------------------------------------------------------------
1710   -- get the paramters for archival process
1711   -------------------------------------------------------------
1712       open   get_params(p_payroll_action_id);
1713    fetch  get_params
1714     into  v_fin_year_start
1715          ,v_fin_year_end
1716          ,v_fbt_year_start
1717          ,v_fbt_year_end
1718          ,v_employee_type
1719          ,v_registered_employer
1720          ,v_financial_year
1721          ,v_assignment_id
1722          ,v_payroll_id
1723          ,l_lst_yr_term           /*Bug3661230*/
1724          ,g_business_group_id
1725      ,v_assignment_set_id;    /*Bug 13043341*/
1726    close get_params;
1727 
1728    /* The following check is introduced for Bug: 3701869. Parameter Last Year Termination has been introduced through
1729       Bug: 3263659. If the customer does not have this new functionality enabled, the default should work. Hence the
1730       check for the parameter if null has been introduced to make sure the correct fbt dates are set. */
1731 
1732    IF (l_lst_yr_term IS NULL) THEN
1733      l_lst_yr_term := 'Y';
1734    END IF;
1735 
1736    /**** Bug#3661230  **********/
1737    IF l_lst_yr_term = 'Y' THEN
1738      -- v_lst_year_start := ADD_MONTHS(v_fbt_year_start,-12); -- 3263659
1739      v_lst_year_start := ADD_MONTHS(v_fin_year_start,-12); -- 5395393
1740      v_lst_fbt_year_start := v_fbt_year_start;
1741    ELSE
1742      v_lst_year_start := TO_DATE('01-01-1900','DD-MM-YYYY');
1743      v_lst_fbt_year_start := TO_DATE('01-01-1900','DD-MM-YYYY');
1744    END IF;
1745   /* end of Bug#3661230 **/
1746 
1747 ---amit
1748     If g_fbt_defined_balance_id is null OR g_fbt_defined_balance_id =0 Then
1749        Open  c_fbt_balance;
1750        Fetch c_fbt_balance into  g_fbt_defined_balance_id;
1751        Close c_fbt_balance;
1752    End if;
1753 ----amit
1754 
1755         /* Bug 5708255 */
1756         open c_get_fbt_global (add_months(v_fin_year_end,-3));  /* Add_months included for bug 5333143 */
1757         fetch c_get_fbt_global into g_fbt_threshold;
1758         close c_get_fbt_global;
1759 
1760         hr_utility.set_location('Anitha g_fbt_threshold Value in ass_action_code        '||g_fbt_threshold,1000);
1761 
1762   v_reg_emp := to_number(v_registered_employer); /*added-sun*/
1763 
1764 if (v_assignment_id <> '%' and v_payroll_id <> '%') then /*Added for bug 3019374*/
1765 
1766   for process_rec in process_assignments_only(p_payroll_action_id,
1767                                               p_start_person_id,
1768                                               p_end_person_id)
1769   Loop     /* Bug: 2881272 - Removed the temporary check which was included during the fix 2822446 */
1770      open next_action_id;
1771      fetch next_action_id into v_next_action_id;
1772      close next_action_id;
1773      IF g_debug THEN
1774         hr_utility.set_location('Calling hr_nonrun_asact.insact for assignment id :'||process_rec.assignment_id,2);
1775      END if;
1776 
1777      hr_nonrun_asact.insact(v_next_action_id,
1778                             process_rec.assignment_id,
1779                             p_payroll_action_id,
1780                             p_chunk,
1781                             null);
1782      IF g_debug THEN
1783         hr_utility.set_location('After calling hr_nonrun_asact.insact',3);
1784      END if;
1785   end loop;
1786 
1787 /* Bug 13043341 Elsif block added to Process Assignment Set*/
1788 
1789 elsif v_assignment_set_id <> '%'
1790 then
1791 
1792 OPEN cur_inc_exc_flag;
1793 FETCH cur_inc_exc_flag INTO l_cur_inc_exc_flag_rec;
1794 CLOSE cur_inc_exc_flag;
1795 
1796 IF l_cur_inc_exc_flag_rec.inc_or_exc = 'I' THEN
1797 /* Process Assignment Set with assignments included*/
1798 
1799 IF range_person_on
1800 THEN
1801 
1802     FOR csr_rec IN range_process_assignment_set(p_payroll_action_id
1803                                                  ,p_chunk)
1804     LOOP
1805 
1806     OPEN  next_action_id;
1807         FETCH next_action_id INTO v_next_action_id;
1808         CLOSE next_action_id;
1809         IF g_debug THEN
1810            hr_utility.set_location('Calling hr_nonrun_asact.insact for assignment id :'||csr_rec.assignment_id,2);
1811         END if;
1812 
1813             hr_nonrun_asact.insact(v_next_action_id,
1814                                    csr_rec.assignment_id,
1815                                    p_payroll_action_id,
1816                                    p_chunk,
1817                                    null);
1818         IF g_debug THEN
1819                hr_utility.set_location('After calling hr_nonrun_asact.insact',3);
1820             END IF;
1821     END LOOP;
1822 
1823 ELSE /* Retain Old Logic - No Range Person */
1824 
1825   for process_rec in process_assignment_set(p_payroll_action_id,
1826                                             p_start_person_id,
1827                                             p_end_person_id)
1828    Loop
1829          open next_action_id;
1830          fetch next_action_id into v_next_action_id;
1831          close next_action_id;
1832         IF g_debug THEN
1833            hr_utility.set_location('Calling hr_nonrun_asact.insact for assignment id :'||process_rec.assignment_id,2);
1834         END if;
1835             hr_nonrun_asact.insact(v_next_action_id,
1836                            process_rec.assignment_id,
1837                            p_payroll_action_id,
1838                            p_chunk,
1839                            null);
1840             IF g_debug THEN
1841            hr_utility.set_location('After calling hr_nonrun_asact.insact',3);
1842            END if;
1843 end loop;
1844 
1845 END IF; /* End Range Person check */
1846 
1847 
1848 
1849 
1850 ELSIF ( l_cur_inc_exc_flag_rec.inc_or_exc = 'E' ) OR ( l_cur_inc_exc_flag_rec.inc_or_exc IS NULL ) THEN
1851 /* Process Assignment Set with assignments excluded */
1852 
1853 IF l_cur_inc_exc_flag_rec.inc_or_exc IS NULL THEN
1854 /* To handle the assignment set with no Assignments included or excluded */
1855 v_count := 2;
1856 END IF;
1857 
1858    select PAYROLL_ID into v_payroll_id from HR_ASSIGNMENT_SETS where ASSIGNMENT_SET_ID=v_assignment_set_id;
1859 
1860 IF range_person_on
1861 THEN
1862 
1863 
1864     FOR csr_rec IN range_process_assignments(p_payroll_action_id
1865                                                  ,p_chunk)
1866     LOOP
1867 
1868       IF l_cur_inc_exc_flag_rec.inc_or_exc = 'E' THEN
1869     SELECT count(*) INTO v_count FROM hr_assignment_set_amendments
1870     WHERE ASSIGNMENT_SET_ID=v_assignment_set_id AND ASSIGNMENT_ID=csr_rec.assignment_id;
1871       END IF;
1872 
1873       IF (v_count = 0 AND l_cur_inc_exc_flag_rec.inc_or_exc = 'E') OR (v_count = 2)
1874       THEN
1875     OPEN  next_action_id;
1876         FETCH next_action_id INTO v_next_action_id;
1877         CLOSE next_action_id;
1878         IF g_debug THEN
1879            hr_utility.set_location('Calling hr_nonrun_asact.insact for assignment id :'||csr_rec.assignment_id,2);
1880         END if;
1881 
1882             hr_nonrun_asact.insact(v_next_action_id,
1883                                    csr_rec.assignment_id,
1884                                    p_payroll_action_id,
1885                                    p_chunk,
1886                                    null);
1887         IF g_debug THEN
1888                hr_utility.set_location('After calling hr_nonrun_asact.insact',3);
1889             END IF;
1890       END IF;
1891     END LOOP;
1892 
1893 ELSE /* Retain Old Logic - No Range Person */
1894 
1895   for process_rec in process_assignments(p_payroll_action_id,
1896                                             p_start_person_id,
1897                                             p_end_person_id)
1898    Loop
1899 
1900       IF l_cur_inc_exc_flag_rec.inc_or_exc = 'E' THEN
1901     SELECT count(*) INTO v_count FROM hr_assignment_set_amendments
1902     WHERE ASSIGNMENT_SET_ID=v_assignment_set_id AND ASSIGNMENT_ID=process_rec.assignment_id;
1903       END IF;
1904 
1905       IF (v_count = 0 AND l_cur_inc_exc_flag_rec.inc_or_exc = 'E') OR (v_count = 2)
1906       THEN
1907     open next_action_id;
1908             fetch next_action_id into v_next_action_id;
1909             close next_action_id;
1910         IF g_debug THEN
1911            hr_utility.set_location('Calling hr_nonrun_asact.insact for assignment id :'||process_rec.assignment_id,2);
1912         END if;
1913             hr_nonrun_asact.insact(v_next_action_id,
1914                            process_rec.assignment_id,
1915                            p_payroll_action_id,
1916                            p_chunk,
1917                            null);
1918             IF g_debug THEN
1919            hr_utility.set_location('After calling hr_nonrun_asact.insact',3);
1920         END if;
1921       END IF;
1922   end loop;
1923 
1924 END IF; /* End Range Person check */
1925 END IF; /* End of the flag l_cur_inc_exc_flag_rec check */
1926 /* Bug 13043341 end of Elsif block added to Process Assignment Set*/
1927 
1928 else
1929 /* Multiple Assignments */
1930 
1931    /* Bug 7138494 - Added Changes for Range Person
1932        - Call Cursor using pay_population_ranges if Range Person Enabled
1933          Else call Old Cursor
1934    */
1935 
1936 IF range_person_on
1937 THEN
1938 
1939     FOR csr_rec IN range_process_assignments(p_payroll_action_id
1940                                             ,p_chunk)
1941     LOOP
1942         OPEN  next_action_id;
1943         FETCH next_action_id INTO v_next_action_id;
1944         CLOSE next_action_id;
1945         IF g_debug THEN
1946            hr_utility.set_location('Calling hr_nonrun_asact.insact for assignment id :'||csr_rec.assignment_id,2);
1947         END if;
1948 
1949             hr_nonrun_asact.insact(v_next_action_id,
1950                                    csr_rec.assignment_id,
1951                                    p_payroll_action_id,
1952                                    p_chunk,
1953                                    null);
1954             IF g_debug THEN
1955                hr_utility.set_location('After calling hr_nonrun_asact.insact',3);
1956             END IF;
1957     END LOOP;
1958 
1959 ELSE /* Retain Old Logic - No Range Person */
1960 
1961   for process_rec in process_assignments (p_payroll_action_id,
1962                                           p_start_person_id,
1963                                           p_end_person_id)
1964    Loop
1965         open next_action_id;
1966             fetch next_action_id into v_next_action_id;
1967             close next_action_id;
1968         IF g_debug THEN
1969            hr_utility.set_location('Calling hr_nonrun_asact.insact for assignment id :'||process_rec.assignment_id,2);
1970         END if;
1971             hr_nonrun_asact.insact(v_next_action_id,
1972                            process_rec.assignment_id,
1973                            p_payroll_action_id,
1974                            p_chunk,
1975                            null);
1976             IF g_debug THEN
1977            hr_utility.set_location('After calling hr_nonrun_asact.insact',3);
1978         END if;
1979   end loop;
1980 
1981 END IF; /* End Range Person check */
1982 
1983 end if;
1984 
1985   IF g_debug THEN
1986       hr_utility.set_location('End of assignment_action_code',4);
1987   END if;
1988 
1989 
1990 exception
1991   when others then
1992     IF g_debug THEN
1993         hr_utility.set_location('error raised in assignment_action_code procedure ',5);
1994     END if;
1995     raise;
1996 end assignment_action_code;
1997 
1998 
1999 ---------------------------------
2000   --Functions
2001   function get_max_effective_person_date (p_person_id per_all_people_f .person_id%type)
2002   return date
2003   is
2004 l_effective_date date ;
2005 cursor c_effective_date
2006       is
2007     select  max(effective_start_date)
2008     from per_all_people_f  p
2009     where person_id =p_person_id ;
2010 begin
2011    open c_effective_date;
2012    fetch c_effective_date into l_effective_date ;
2013    close c_effective_date ;
2014    return l_effective_date ;
2015 end;
2016 
2017   function get_max_effective_asg_date(p_asg_id per_all_assignments_f .assignment_id%type)
2018   return date
2019   is
2020 l_effective_date date ;
2021 cursor c_effective_date
2022       is
2023 select  max(effective_start_date)
2024       from per_all_assignments_f  p
2025    where assignment_id =p_asg_id ;
2026 begin
2027    open c_effective_date;
2028    fetch c_effective_date into l_effective_date ;
2029    close c_effective_date ;
2030    return l_effective_date ;
2031 end;
2032 
2033 
2034 
2035    ------------------------------------------------------------------------+
2036     -- Creates the Extract Archive Database Item.
2037     -- Called from
2038     -- 1. Archive_supplier_details
2039     -- 2. Archive_employer_details
2040     -- 3. Archive_employee_details
2041     -- 4. Archive_Balance_Details
2042     ------------------------------------------------------------------------+
2043 
2044 
2045 procedure create_extract_archive_details
2046     (p_assignment_action_id  in     pay_assignment_actions.assignment_action_id%type,
2047      p_user_entity_name      in     ff_user_entities.user_entity_name%type,
2048      p_value                 in out nocopy ff_archive_items.value%type) is
2049 
2050 
2051 
2052   cursor  get_user_entity_id(c_user_entity_name in varchar2)
2053       is
2054   select  fue.user_entity_id,
2055           dbi.data_type
2056     from  ff_user_entities  fue,
2057           ff_database_items dbi
2058    where  user_entity_name   =c_user_entity_name
2059    and    fue.user_entity_id =dbi.user_entity_id;
2060 
2061    v_full_name           per_all_people_f.full_name%type;
2062    v_user_entity_id      ff_user_entities.user_entity_id%type;
2063    v_archive_item_id         ff_archive_items.archive_item_id%type;
2064    v_data_type               ff_database_items.data_type%type;
2065    v_object_version_number   ff_archive_items.object_version_number%type;
2066    v_some_warning            boolean;
2067 
2068    i_index      NUMBER;         /* Bug 6470581 */
2069 
2070 begin
2071      g_debug := hr_utility.debug_enabled;
2072 
2073   IF g_debug THEN
2074       hr_utility.set_location('Start of create_extract_archive_details',15);
2075       hr_utility.set_location('Assignment action id is :' || p_assignment_action_id,16);
2076       hr_utility.set_location('Database item name is   :' || p_user_entity_name,16);
2077       hr_utility.set_location('Value is                :' || p_value,17);
2078   END if;
2079 
2080     /* Bug 6470581 - Added Changes for Amended Payment Summary Type
2081        i. If g_payment_summary_type = 'O' (Original) - Archive values in ff_archive_items
2082       ii. If g_payment_summary_type = 'A' (Amended)  - Add values to PL/SQL Table p_all_dbi_tab
2083     */
2084 
2085 IF g_payment_summary_type = 'O'
2086 THEN
2087 
2088   open get_user_entity_id (p_user_entity_name);
2089      hr_utility.trace('the value of the user entity is '||p_user_entity_name);
2090   fetch get_user_entity_id into v_user_Entity_id,
2091                                 v_data_type;
2092 
2093    -----------------------------------------------------------------------------------------------+
2094   -- if the archive item datatype is date then convert
2095   -- it into canonical format before archiving
2096   -- this is required because before inserting into ff_archive_items
2097   -- ff_archive_api validates the value
2098    -----------------------------------------------------------------------------------------------+
2099 
2100   if (v_data_type = 'D') then
2101     p_value:= to_char(to_date(p_value,'DDMMYYYY'),fnd_date.canonical_mask);
2102   end if;
2103 
2104 
2105   if get_user_entity_id%found then
2106 
2107     close get_user_entity_id;
2108     ff_archive_api.create_archive_item
2109          (p_validate              => false                    -- boolean  in default
2110          ,p_archive_item_id       => v_archive_item_id        -- number   out
2111          ,p_user_entity_id        => v_user_entity_id         -- number   in
2112          ,p_archive_value         => p_value                  -- varchar2 in
2113          ,p_archive_type          => 'AAP'                    -- varchar2 in default
2114          ,p_action_id             => p_assignment_action_id   -- number   in
2115          ,p_legislation_code      => 'AU'                     -- varchar2 in
2116          ,p_object_version_number => v_object_version_number  -- number   out
2117          ,p_context_name1         => 'ASSIGNMENT_ACTION_ID'   -- varchar2 in default
2118          ,p_context1              => p_assignment_action_id   -- varchar2 in default
2119          ,p_some_warning          => v_some_warning);         -- boolean  out
2120   else
2121 
2122     close get_user_entity_id;
2123      IF g_debug THEN
2124      hr_utility.set_location('User entity not found :'||p_user_entity_name,20);
2125      END if;
2126   end if;
2127 
2128 ELSE
2129 
2130           OPEN get_user_entity_id (p_user_entity_name);
2131           FETCH get_user_entity_id INTO v_user_Entity_id,
2132                                         v_data_type;
2133           IF (v_data_type = 'D') THEN
2134             p_value:= to_char(to_date(p_value,'DDMMYYYY'),fnd_date.canonical_mask);
2135           END IF;
2136 
2137           IF get_user_entity_id%FOUND
2138           THEN
2139                 CLOSE get_user_entity_id;
2140                 IF g_debug
2141                 THEN
2142                         hr_utility.set_location('Amended Payment Summary - Update the archive PL/SQL table',2000);
2143                         hr_utility.set_location('p_user_entity_name     '||p_user_entity_name,2000);
2144                 END IF;
2145                 i_index := NVL(p_all_dbi_tab.LAST,-1) + 1;
2146                 p_all_dbi_tab(i_index).db_item_name  := p_user_entity_name;
2147                 p_all_dbi_tab(i_index).db_item_value := p_value;
2148 
2149                 IF g_debug
2150                 THEN
2151                         hr_utility.set_location('Updated Index         '||i_index,2010);
2152                 END IF;
2153         ELSE
2154                 CLOSE get_user_entity_id;
2155                 IF g_debug THEN
2156                         hr_utility.set_location('User entity not found :'||p_user_entity_name,2020);
2157                 END if;
2158         END IF;
2159 END IF;
2160 
2161   IF g_debug THEN
2162      hr_utility.set_location('End of create_extract_archive_detail',18);
2163   END if;
2164 
2165 exception
2166   when others then
2167   if get_user_entity_id%isopen then
2168    close get_user_entity_id;
2169    IF g_debug THEN
2170      hr_utility.set_location('closing..',117);
2171    END if;
2172   end if;
2173   IF g_debug THEN
2174      hr_utility.set_location('Error in create_extract_archive_details',20);
2175   END if;
2176   raise;
2177 end create_extract_archive_details;
2178 
2179 ----------------------------------------------------------------------+
2180 -- procedure to archive balance details
2181 -- Passed Balance name Bug #2454595
2182 ----------------------------------------------------------------------+
2183 
2184 procedure archive_balance_details
2185      (p_assignment_action_id    in   pay_assignment_actions.ASSIGNMENT_ACTION_ID%TYPE
2186      ,p_max_assignment_action_id    in   pay_assignment_actions.ASSIGNMENT_ACTION_ID%TYPE --2610141
2187      ,p_registered_employer     in   NUMBER
2188      ,p_database_item_name      in   ff_database_items.user_name%TYPE
2189      ,p_balance_name            in   pay_balance_types.balance_name%TYPE
2190      ,p_legislation_code        in   pay_defined_balances.legislation_code%TYPE
2191      ,p_year_start              in   DATE
2192      ,p_year_end                in   DATE
2193      ,p_assignment_id           in   pay_assignment_actions.ASSIGNMENT_ID%type
2194      ,p_payroll_action_id       in   pay_payroll_actions.payroll_action_id%TYPE
2195      ,p_bal_value       OUT  NOCOPY varchar2) IS  -- Bug 3098353
2196 
2197       v_bal_value                    varchar2(20);
2198       v_earnings_ytd                 varchar2(20);
2199       v_lump_sum_E_ytd               varchar2(20);
2200       v_adj_lump_sum_E_ptd           number;
2201       v_adj_lump_sum_pre_tax         number; -- Bug 9190980
2202       v_effective_date               DATE;
2203 
2204    -------------------------------------------------------------------+
2205    -- Cursor to calculate balances for a given database item
2206    -- To calculate balances we require defined balance id and
2207    -- that can be retrieved only from pay_defined_balances
2208    -- first cursor is to retrive Fringe Benefits Balance
2209    -- second cursor is to retrive other Balance values
2210    -------------------------------------------------------------------+
2211    -- cursors modified for bug #1768813
2212    -------------------------------------------------------------------+
2213 
2214    -------------------------------------------------------------------+
2215    -- cursors  c_archive_fbt_info and c_archive_info
2216    -- modified to use balance name instead of archive item name
2217    -- Bug #2454595
2218    -------------------------------------------------------------------+
2219    cursor  c_archive_fbt_info(c_balance_name         pay_balance_types.balance_name%type,
2220                               c_year_end             DATE,
2221                               c_assignment_id        pay_assignment_actions.assignment_id%type)
2222    is
2223    select pay_balance_pkg.get_value(pdb.defined_balance_id,
2224                                     p_max_assignment_action_id,
2225                     p_registered_employer,
2226                     null,null,null,null) --2610141
2227     from   pay_balance_types      pbt,
2228            pay_defined_balances   pdb,
2229            pay_balance_dimensions pbd
2230     where  pbt.balance_name = c_balance_name
2231       and  pbt.legislation_code = 'AU'
2232       and  pbt.balance_type_id = pdb.balance_type_id
2233       and  pbd.balance_dimension_id = pdb.balance_dimension_id
2234       and  pbd.dimension_name = '_ASG_LE_FBT_YTD';
2235 
2236 
2237 
2238    ------------------------------------------------------------
2239    -- Cursor to calculate tax deduction balance  ( Bug 1903647)
2240    ------------------------------------------------------------
2241 
2242 
2243    CURSOR  c_get_global(c_name     VARCHAR2
2244                        ,c_year_end DATE)
2245        IS
2246    SELECT  global_value
2247           ,data_type
2248     FROM   ff_globals_f
2249     WHERE  global_name = c_name
2250     AND    legislation_code = 'AU'
2251     AND    c_year_end BETWEEN effective_start_date
2252                           AND effective_end_date ;
2253     /*bug8711855 - Moved c_single_lumpsum_E_payment and c_get_pay_effective_date cursors to
2254                    get_lumpsumE_value function to handle single Lump Sum E adjustment */
2255 
2256     r_global c_get_global%ROWTYPE;
2257 
2258      sum_various         Number;
2259      counter             Number;
2260      l_reporting_amt     Number;
2261      l_medicare_levy     NUMBER ;
2262      l_fbt_rate          NUMBER ;
2263     l_assignment_action_id number; --2610141
2264 
2265      e_bad_global        exception ;
2266 
2267     /*bug9147430*/
2268     l_fw_fbt_output_tab    pay_balance_pkg.t_detailed_bal_out_tab;
2269     l_fw_reporting_amt     Number := 0;
2270     l_fw_f_type varchar2(1);
2271     l_fw_j_type varchar2(1);
2272     f_fw_date_tab         pay_au_foreign_workers.tab_fw_dates;
2273     j_fw_date_tab         pay_au_foreign_workers.tab_fw_dates;
2274 
2275 begin
2276    sum_various := 0;
2277    counter := 1;
2278 
2279    IF g_debug THEN
2280       hr_utility.set_location('Start of create_extract_archive_balance',1);
2281       hr_utility.set_location('Start of p_assg_action_id '|| p_assignment_action_id,2);
2282       hr_utility.set_location('Start of p_database_item_name '|| p_database_item_name,3);
2283    END if;
2284 
2285    ------------------------------------------------------------+
2286    -- get the balance value for the database item
2287    ------------------------------------------------------------+
2288 
2289    if p_database_item_name = 'X_FRINGE_BENEFITS_ASG_YTD' then
2290       open c_archive_fbt_info (p_balance_name, -- Bug #2454595
2291                                p_year_end,
2292                                p_assignment_id);
2293 
2294       fetch  c_archive_fbt_info
2295        into  v_bal_value;          -- Bug #2454595
2296 
2297 /* Bug 5708255  Changed from 1000 to g_fbt_threshold */
2298 
2299    if c_archive_fbt_info%found AND v_bal_value > to_number(g_fbt_threshold) THEN  --Bug: 3549553- To fetch only ASG_YTD level balances
2300          close c_archive_fbt_info;
2301          IF g_debug THEN
2302         hr_utility.set_location('Calling create_extract_archive_details for item :'|| p_database_item_name,4);
2303      END if;
2304 
2305          ----------------------------------------------------
2306          --  get global values for MEDICARE_LEVY and FBT_RATE
2307          ---------------------------------------------------
2308 
2309          open c_get_global ( 'FBT_RATE',add_months(p_year_end,-3));  /* Add_months included for bug 5333143 */
2310          fetch c_get_global
2311           into r_global;
2312 
2313          l_fbt_rate := r_global.global_value ;
2314 
2315          if c_get_global%notfound then
2316            raise e_bad_global;
2317          end if;
2318 
2319          close c_get_global ;
2320 
2321          open c_get_global ( 'MEDICARE_LEVY',add_months(p_year_end,-3));  /* Add_months included for bug 5333143 */
2322 
2323          fetch c_get_global
2324           into r_global;
2325 
2326          l_medicare_levy := r_global.global_value ;
2327 
2328          if c_get_global%notfound then
2329             raise e_bad_global;
2330          end if;
2331 
2332          close c_get_global ;
2333 
2334          l_reporting_amt := v_bal_value/(1-(l_fbt_rate+l_medicare_levy));
2335          l_reporting_amt := round(l_reporting_amt,2);
2336 
2337 
2338          create_extract_archive_details(p_assignment_action_id
2339                                        ,p_database_item_name
2340                                        ,l_reporting_amt);
2341 
2342         /* start bug9147430 - Foregin Worker Fringe Benefit archiving */
2343         if g_debug then
2344             hr_utility.set_location('Starting FW FBT check .. '||
2345                                       'g_fw_fbt_term_check '||g_fw_fbt_term_check||' , g_fw_fbt_check '||g_fw_fbt_check, 5);
2346         end if;
2347 
2348         if g_fw_fbt_term_check <> 'NN' or g_fw_fbt_check <> 'NN' then
2349 
2350           l_fw_fbt_output_tab.delete;
2351           t_fw_gross_type.delete;
2352           f_fw_date_tab.delete;
2353           j_fw_date_tab.delete;
2354 
2355           if g_fw_fbt_term_check <> 'NN' then -- Case for terminated employee with FB before the financial year start
2356 
2357             if g_fw_fbt_term_check = 'YY' or g_fw_fbt_term_check = 'YN' then
2358               pay_au_foreign_workers.get_foreign_worker_dates(p_assignment_id, p_registered_employer, add_months(p_year_start,-3), (p_year_start - 1),
2359                                       pay_au_foreign_workers.g_fw_f_type, f_fw_date_tab);
2360             end if;
2361             if g_fw_fbt_term_check = 'YY' or g_fw_fbt_term_check = 'NY' then
2362               pay_au_foreign_workers.get_foreign_worker_dates(p_assignment_id, p_registered_employer, add_months(p_year_start,-3), (p_year_start - 1),
2363                                       pay_au_foreign_workers.g_fw_j_type, j_fw_date_tab);
2364             end if;
2365 
2366             if f_fw_date_tab.count > 0 then
2367               t_fw_gross_type(1) := 'F';
2368               if j_fw_date_tab.count >0 then
2369                 t_fw_gross_type(2) := 'J';
2370               end if;
2371             elsif j_fw_date_tab.count > 0 then
2372               t_fw_gross_type(1) := 'J';
2373             end if;
2374 
2375             for i_idx in t_fw_gross_type.first .. t_fw_gross_type.last loop
2376 
2377 
2378               IF t_fw_gross_type(i_idx) = 'F' THEN
2379 
2380                 pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
2381                                                                    p_registered_employer,
2382                                                                    f_fw_date_tab,
2383                                                                    add_months(p_year_start,-3),
2384                                                                    (p_year_start - 1),
2385                                                                    pay_au_foreign_workers.g_fw_f_type,
2386                                                                    p_fw_fbt_bal_type_tab,
2387                                                                    l_fw_fbt_output_tab) ;
2388 
2389               ELSIF t_fw_gross_type(i_idx) = 'J' THEN
2390                 pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
2391                                                                    p_registered_employer,
2392                                                                    j_fw_date_tab,
2393                                                                    add_months(p_year_start,-3),
2394                                                                    (p_year_start - 1),
2395                                                                    pay_au_foreign_workers.g_fw_j_type,
2396                                                                    p_fw_fbt_bal_type_tab,
2397                                                                    l_fw_fbt_output_tab) ;
2398               END IF;
2399 
2400               if l_fw_fbt_output_tab(1).balance_value >0 then
2401                   l_fw_reporting_amt := l_fw_fbt_output_tab(1).balance_value/(1-(l_fbt_rate+l_medicare_levy));
2402                   l_fw_reporting_amt := round(l_fw_reporting_amt,2);
2403                   g_fw_fbt_balance := g_fw_fbt_balance + l_fw_fbt_output_tab(1).balance_value;
2404 
2405                   create_extract_archive_details(p_assignment_action_id
2406                                             ,'X_FW'||i_idx||'_FRINGE_BENEFITS'
2407                                             ,l_fw_reporting_amt);
2408               end if;
2409 
2410             end loop;
2411 
2412           elsif g_fw_fbt_check <> 'NN' then  -- Case for employees in normal FB year
2413 
2414             if g_fw_fbt_check = 'YY' or g_fw_fbt_check = 'YN' then
2415               pay_au_foreign_workers.get_foreign_worker_dates(p_assignment_id, p_registered_employer, add_months(p_year_start,-3), add_months(p_year_end,-3),
2416                                       pay_au_foreign_workers.g_fw_f_type, f_fw_date_tab);
2417             end if;
2418             if  g_fw_fbt_check = 'YY' or g_fw_fbt_check = 'NY' then
2419               pay_au_foreign_workers.get_foreign_worker_dates(p_assignment_id, p_registered_employer, add_months(p_year_start,-3), add_months(p_year_end,-3),
2420                                       pay_au_foreign_workers.g_fw_j_type, j_fw_date_tab);
2421             end if;
2422 
2423             if f_fw_date_tab.count > 0 then
2424               t_fw_gross_type(1) := 'F';
2425               if j_fw_date_tab.count >0 then
2426                 t_fw_gross_type(2) := 'J';
2427               end if;
2428             elsif j_fw_date_tab.count > 0 then
2429               t_fw_gross_type(1) := 'J';
2430             end if;
2431 
2432             for i_idx in t_fw_gross_type.first .. t_fw_gross_type.last loop
2433 
2434 
2435               IF t_fw_gross_type(i_idx) = 'F' THEN
2436 
2437                 pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
2438                                                                    p_registered_employer,
2439                                                                    f_fw_date_tab,
2440                                                                    add_months(p_year_start,-3),
2441                                                                    add_months(p_year_end,-3),
2442                                                                    pay_au_foreign_workers.g_fw_f_type,
2443                                                                    p_fw_fbt_bal_type_tab,
2444                                                                    l_fw_fbt_output_tab) ;
2445 
2446               ELSIF t_fw_gross_type(i_idx) = 'J' THEN
2447                 pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
2448                                                                    p_registered_employer,
2449                                                                    j_fw_date_tab,
2450                                                                    add_months(p_year_start,-3),
2451                                                                    add_months(p_year_end,-3),
2452                                                                    pay_au_foreign_workers.g_fw_j_type,
2453                                                                    p_fw_fbt_bal_type_tab,
2454                                                                    l_fw_fbt_output_tab) ;
2455               END IF;
2456 
2457               if l_fw_fbt_output_tab(1).balance_value >0 then
2458                   l_fw_reporting_amt := l_fw_fbt_output_tab(1).balance_value/(1-(l_fbt_rate+l_medicare_levy));
2459                   l_fw_reporting_amt := round(l_fw_reporting_amt,2);
2460                   g_fw_fbt_balance := g_fw_fbt_balance + l_fw_fbt_output_tab(1).balance_value;
2461 
2462                   create_extract_archive_details(p_assignment_action_id
2463                                             ,'X_FW'||i_idx||'_FRINGE_BENEFITS'
2464                                             ,l_fw_reporting_amt);
2465               end if;
2466 
2467             end loop;
2468 
2469         end if;
2470 
2471          if g_debug then
2472             hr_utility.set_location('Leaving FW FBT g_fw_fbt_balance : '||g_fw_fbt_balance,5);
2473         end if;
2474 
2475      end if;
2476 
2477      g_net_balance := v_bal_value;
2478 
2479       /* end bug9147430 */
2480 
2481       else
2482          IF g_debug THEN
2483         hr_utility.set_location('FBT Below Reportable limit for this balance:'||p_database_item_name,5);
2484      END if;
2485          v_bal_value := 0;
2486          close c_archive_fbt_info;
2487       end if;
2488 
2489    elsif p_database_item_name = 'X_EARNINGS_TOTAL_ASG_YTD' then
2490 
2491       ---------------------------------------
2492       -- archive earnings_total balance
2493       ---------------------------------------
2494       v_lump_sum_E_ytd :=0;
2495       v_earnings_ytd := 0;
2496 
2497 /* Remove as balances now retrieved using BBR - Bug 3172963
2498       open c_archive_total_earnings_info ( p_year_end,
2499                                            p_assignment_id);
2500 
2501       fetch  c_archive_total_earnings_info into v_earnings_ytd;
2502 
2503       if c_archive_total_earnings_info%found then
2504          close c_archive_total_earnings_info;
2505 */
2506       /* Bug 3172963 */
2507       /* v_earnings_total := 'Leave Payments Marginal' + 'Earnings_Total' + 'Workplace Giving 4015082 - Exempt Foreign Employment Income 10143762 */
2508       v_earnings_ytd := p_result_table(2).balance_value + p_result_table(16).balance_value + p_result_table(17).balance_value - p_result_table(12).balance_value;
2509 
2510 
2511       if (v_earnings_ytd >= 0) then
2512 
2513     /* Remove as balance now retrieved using BBR - Bug 3172963
2514        open c_archive_info('Lump Sum E Payments',p_year_end,p_assignment_id);
2515        fetch c_archive_info into v_lump_sum_E_ytd;
2516        close c_archive_info;
2517     */
2518 
2519          /*bug8711855 - after fetching and calculating lump_sum_e_ytd,
2520                         get_lumpsumE_value is called to adjust with less than 400 single lump sum E_ptd*/
2521 
2522          v_lump_sum_E_ytd := p_result_table(15).balance_value + p_result_table(27).balance_value
2523                              + p_result_table(28).balance_value - p_result_table(29).balance_value +  p_result_table(35).balance_value;
2524 
2525          /* Bug 9190980 -  Added argument v_adj_lump_sum_pre_tax in the function call for fetching Retro GT12 Pre Tax deduction after Lump Sum Adjestment */
2526          if v_lump_sum_E_ytd <> 0 then
2527 
2528                v_lump_sum_E_ytd := pay_au_payment_summary.get_lumpsumE_value(p_registered_employer, p_assignment_id, p_year_start,
2529                                                            p_year_end, p_lump_sum_E_ptd_tab, v_lump_sum_E_ytd, v_adj_lump_sum_E_ptd,v_adj_lump_sum_pre_tax);
2530          end if;
2531 
2532          /* The following logic makes sure that more than 400 Lump Sum E values are relfected in Lump Sum E archive */
2533          v_earnings_ytd := v_earnings_ytd - v_lump_sum_E_ytd;
2534          v_bal_value := v_earnings_ytd + v_lump_sum_E_ytd; --Bug 3098353, 9675451
2535          g_net_balance := g_net_balance + v_bal_value;  /*bug9147430*/
2536 
2537          IF g_debug THEN
2538             hr_utility.set_location('Calling create_extract_archive_details for item :'|| p_database_item_name,4);
2539          END if;
2540 
2541          create_extract_archive_details(p_assignment_action_id
2542                                        ,p_database_item_name
2543                                        ,v_earnings_ytd);
2544          create_extract_archive_details(p_assignment_action_id
2545                                        ,'X_LUMP_SUM_E_PAYMENTS_ASG_YTD'
2546                                        ,v_lump_sum_E_ytd);
2547       else
2548          IF g_debug THEN
2549             hr_utility.set_location('Balance value not found for this balance:'||p_database_item_name,5);
2550          END if;
2551           /* close c_archive_total_earnings_info; */ --Bug 3172963
2552       end if;
2553 
2554    elsif p_database_item_name = 'X_TOTAL_TAX_DEDUCTIONS_ASG_YTD' then
2555 
2556       ---------------------------------------
2557       -- archive Total Tax Deductions balance
2558       ---------------------------------------
2559 
2560 /* Remove as balances now retrieved using BBR - Bug 3172963 */
2561 /*   open c_archive_total_tax_info( p_year_end,
2562                                    p_assignment_id);
2563 
2564     fetch  c_archive_total_tax_info into v_bal_value;
2565 
2566     if c_archive_total_tax_info%found then
2567        close c_archive_total_tax_info;
2568 */
2569 
2570       -- v_bal_val := 'Total_Tax_Deductions' + 'Termination Deductions' - 'Lump Sum C Deductions'
2571       v_bal_value := p_result_table(10).balance_value +  p_result_table(11).balance_value
2572                                   -  p_result_table(7).balance_value;
2573 
2574      g_net_balance := g_net_balance + v_bal_value; /*bug9147430*/
2575 
2576       IF g_debug THEN
2577          hr_utility.set_location('Calling create_extract_archive_details for item :'|| p_database_item_name,4);
2578       END if;
2579       create_extract_archive_details(p_assignment_action_id
2580                                     ,p_database_item_name
2581                                     ,v_bal_value);
2582 
2583    else /* archive other balances */
2584 
2585 /* Remove as balances now retrieved using BBR - Bug 3172963 */
2586 /*   open c_archive_info (p_balance_name, -- Bug#2454595
2587                        p_year_end,
2588                        p_assignment_id);
2589 
2590     fetch  c_archive_info into  v_bal_value;  -- Bug #2454595
2591 
2592     if c_archive_info%found then
2593        close c_archive_info;
2594 */
2595 
2596 /*Bug 6192381 Added New Balances for Multiple ETP Enhancement*/
2597 /*Bug 8587013 - Other Income is removed and 'Exempt Foreign Employment Income' balance is moved from
2598                 index position 27 to 12 */
2599       if (p_balance_name = 'CDEP') then
2600          v_bal_value := p_result_table(1).balance_value;
2601       elsif (p_balance_name = 'Lump Sum A Deductions') then
2602          v_bal_value := p_result_table(3).balance_value;
2603       elsif (p_balance_name = 'Lump Sum A Payments') then
2604          v_bal_value := p_result_table(4).balance_value;
2605          g_net_balance := g_net_balance + v_bal_value;  --bug9147430
2606       elsif (p_balance_name = 'Lump Sum B Deductions') then
2607          v_bal_value := p_result_table(5).balance_value;
2608       elsif (p_balance_name = 'Lump Sum B Payments') then
2609          v_bal_value := p_result_table(6).balance_value;
2610          g_net_balance := g_net_balance + v_bal_value;  --bug9147430
2611       elsif (p_balance_name = 'Lump Sum D Payments') then
2612          v_bal_value := p_result_table(9).balance_value;
2613          g_net_balance := g_net_balance + v_bal_value;  --bug9147430
2614       elsif (p_balance_name = 'Exempt Foreign Employment Income') then  /*Bug 8587013*/
2615          v_bal_value := p_result_table(12).balance_value;
2616          g_net_balance := g_net_balance + v_bal_value;  --bug9147430
2617       elsif (p_balance_name = 'Union Fees') then
2618          v_bal_value := p_result_table(13).balance_value;
2619       elsif (p_balance_name = 'Invalidity Payments') then
2620          v_bal_value := p_result_table(14).balance_value;
2621       elsif (p_balance_name = 'Lump Sum C Payments') then
2622          v_bal_value := p_result_table(8).balance_value;
2623       elsif (p_balance_name = 'Lump Sum C Deductions') then
2624          v_bal_value := p_result_table(7).balance_value;
2625       elsif (p_balance_name = 'Workplace Giving Deductions') then /* 4015082 */
2626          v_bal_value := p_result_table(17).balance_value;
2627       elsif (p_balance_name = 'ETP Deductions Transitional Not Part of Prev Term') then /* Begin 6192381 */
2628          v_bal_value := p_result_table(18).balance_value;
2629       elsif (p_balance_name = 'ETP Deductions Transitional Part of Prev Term') then
2630          v_bal_value := p_result_table(19).balance_value;
2631       elsif (p_balance_name = 'ETP Deductions Life Benefit Not Part of Prev Term') then
2632          v_bal_value := p_result_table(20).balance_value;
2633       elsif (p_balance_name = 'ETP Deductions Life Benefit Part of Prev Term') then
2634          v_bal_value := p_result_table(21).balance_value;
2635       elsif (p_balance_name = 'Invalidity Payments Life Benefit Not Part of Prev Term') then
2636          v_bal_value := p_result_table(22).balance_value;
2637       elsif (p_balance_name = 'Invalidity Payments Life Benefit Part of Prev Term') then
2638          v_bal_value := p_result_table(23).balance_value;
2639       elsif (p_balance_name = 'Invalidity Payments Transitional Not Part of Prev Term') then
2640          v_bal_value := p_result_table(24).balance_value;
2641       elsif (p_balance_name = 'Invalidity Payments Transitional Part of Prev Term') then
2642          v_bal_value := p_result_table(25).balance_value; /* End 6192381 */
2643       elsif (p_balance_name = 'Reportable Employer Superannuation Contributions') then /*Begin 8315198*/
2644          v_bal_value := p_result_table(26).balance_value;
2645          g_net_balance := g_net_balance + v_bal_value;  --bug9147430
2646       elsif (p_balance_name = 'ETP Deductions Excluded') then /* start bug 14703826 */
2647          v_bal_value := p_result_table(36).balance_value;
2648       elsif (p_balance_name = 'ETP Deductions Excluded Part of Prev') then
2649          v_bal_value := p_result_table(37).balance_value;
2650       elsif (p_balance_name = 'ETP Deductions Non Excluded') then
2651          v_bal_value := p_result_table(38).balance_value;
2652       elsif (p_balance_name = 'ETP Deductions Non Excluded Part of Prev') then
2653          v_bal_value := p_result_table(39).balance_value;  /* end bug 14703826 */
2654       end if; /*End 8315198*/
2655 
2656       IF g_debug THEN
2657          hr_utility.set_location('Calling create_extract_archive_details for item :'|| p_database_item_name,4);
2658       END if;
2659       create_extract_archive_details(p_assignment_action_id
2660                                     ,p_database_item_name
2661                                     ,v_bal_value);
2662 
2663 /*   else
2664        IF g_debug THEN
2665        hr_utility.set_location('Balance value not found for this balance:'||p_database_item_name,5);
2666        END if;
2667        close c_archive_info;
2668     end if;
2669 */
2670 
2671    end if;
2672    p_bal_value := v_bal_value; --Bug 3098353
2673 
2674 exception
2675   when e_bad_global then
2676     IF g_debug THEN
2677     hr_utility.set_location('archive_balance_details : Global value not found  ',15);
2678     END if;
2679     close c_get_global;
2680   when zero_divide then
2681     IF g_debug THEN
2682         hr_utility.set_location('archive_balance_details : Division By Zero   ',15);
2683     END if;
2684   when others then
2685     IF g_debug THEN
2686         hr_utility.set_location('Error in archive_balance_details procedure',15);
2687     END if;
2688     raise;
2689 
2690 end  archive_balance_details;
2691 
2692 -----------------------------------------------------------------------------
2693      /*Function Introduced for Bug2855658*/
2694 
2695   function adjust_retro_allowances(t_allowance_balance IN OUT NOCOPY tab_allownace_balance
2696      ,p_year_start              in   DATE
2697      ,p_year_end                in   DATE
2698      ,p_assignment_id           in   pay_assignment_actions.ASSIGNMENT_ID%type
2699      ,p_registered_employer     in   NUMBER --2610141
2700      )
2701   return number
2702   is
2703 
2704 /* bug 7571001 - Modified Get_retro_Entry_ids and Get_Retro_allowances cursors
2705                                to accommodate Balance Attribute reporting
2706 */
2707   CURSOR Get_retro_Entry_ids(c_year_start DATE,
2708                              c_year_end   DATE,
2709                              c_assignment_id  pay_assignment_actions.assignment_id%type)
2710   IS
2711   SELECT  /*+ ORDERED */ pee.element_entry_id element_entry_id,
2712           ppa.date_earned date_earned,
2713           pee.assignment_id assignment_id,
2714           pac.tax_unit_id,  /* Added for bug #5846278 */
2715           pdb.balance_type_id
2716 FROM    per_all_assignments_f  paa
2717        ,per_periods_of_service pps
2718        ,pay_assignment_actions pac
2719        ,pay_payroll_actions    ppa
2720        ,pay_element_entries_f pee
2721        ,pay_run_results        prr
2722        ,pay_element_types_f    pet
2723        /* below added for bug 7571001 */
2724        ,PAY_BAL_ATTRIBUTE_DEFINITIONS pbad
2725        , PAY_BALANCE_ATTRIBUTES pba
2726        ,pay_defined_balances pdb
2727        ,pay_balance_dimensions pbd
2728        ,PAY_BALANCE_FEEDS_F pbf
2729        ,pay_input_values_f piv
2730      WHERE paa.assignment_id        = c_assignment_id
2731       /* start added for bug 7571001 */
2732       AND pbad.attribute_name = 'AU_EOY_ALLOWANCE'
2733       AND pbad.legislation_code = 'AU'
2734       AND pac.assignment_id = c_assignment_id
2735      AND pbad.attribute_id = pba.attribute_id
2736      AND pba.defined_balance_id = pdb.defined_balance_id
2737      and   pbd.balance_dimension_id = pdb.balance_dimension_id
2738      and   pbd.dimension_name = '_ASG_LE_YTD'
2739      and   pbd.legislation_code = 'AU'
2740      AND pdb.balance_type_id = pbf.balance_type_id
2741      AND pbf.input_value_id = piv.input_value_id
2742      AND piv.element_type_id = pet.element_type_id
2743      /* end added for bug 7571001 */
2744      AND   pps.PERIOD_OF_SERVICE_ID = paa.PERIOD_OF_SERVICE_ID
2745      AND   NVL(pps.actual_termination_date,c_year_end)
2746            BETWEEN paa.effective_start_date AND paa.effective_end_date
2747      AND   pac.payroll_action_id = ppa.payroll_Action_id
2748      AND   pac.assignment_id = paa.assignment_id
2749      AND   pac.tax_unit_id   = p_registered_employer --2610141
2750      AND   ppa.effective_date BETWEEN c_year_start AND c_year_end /*bug 4063321*/
2751      AND   pac.assignment_Action_id = prr.assignment_Action_id
2752      AND   prr.element_type_id=pet.element_type_id
2753      AND   pee.element_entry_id=prr.source_id
2754      AND   pee.creator_type in ('EE','RR')
2755      AND   pee.assignment_id = paa.assignment_id /*Added for bug3019374*/
2756      AND   ppa.action_status='C'
2757      AND   pac.action_status='C'
2758      AND   ppa.date_earned between pee.effective_start_date and pee.effective_end_date
2759      AND   ppa.date_earned BETWEEN pet.effective_start_date AND  pet.effective_end_date
2760      AND   ppa.date_earned between pbf.effective_start_date and pbf.effective_end_date
2761      AND   ppa.date_earned between piv.effective_start_date and piv.effective_end_date
2762      ;
2763 
2764 
2765        Cursor Get_Retro_allowances(c_element_entry_id  pay_element_entries_f.element_entry_id%type,
2766                                                                 c_balance_type_id pay_defined_balances.defined_balance_id%type)
2767        IS
2768         select  NVL(pbt.reporting_name,pbt.balance_name)  balance_name  /* Bug 5743196 Added nvl */
2769                      ,prv.result_value balance_value
2770         from
2771         pay_element_entries_f pee,
2772         pay_run_results prr,
2773         pay_run_result_values prv,
2774         pay_element_types_f    pet,
2775         pay_balance_types      pbt
2776          /* below added for bug 7571001 */
2777        ,PAY_BALANCE_FEEDS_F pbf
2778        ,pay_input_values_f piv
2779         where
2780         pee.element_entry_id=c_element_entry_id
2781         and prv.run_result_id=prr.run_result_id
2782         AND pee.element_entry_id=prr.source_id
2783         AND prr.element_type_id=pet.element_type_id
2784          /* start added for bug 7571001 */
2785         AND pbt.balance_type_id = c_balance_type_id
2786         AND pbt.balance_type_id = pbf.balance_type_id
2787         AND pbf.input_value_id = piv.input_value_id
2788         AND piv.element_type_id = pet.element_type_id
2789          /* end added for bug 7571001 */
2790         AND pee.effective_start_date between pet.effective_start_date and pet.effective_end_date
2791         AND pee.effective_start_date between pbf.effective_start_date and pbf.effective_end_date
2792         AND pee.effective_start_date between piv.effective_start_date and piv.effective_end_date
2793         ;
2794 
2795 
2796 
2797 /* Added to check the legislation rule for bug #5846278 */
2798         CURSOR get_legislation_rule
2799         IS
2800         SELECT plr.rule_mode
2801         FROM   pay_legislation_rules plr
2802         WHERE  plr.legislation_code = 'AU'
2803         AND    plr.rule_type ='ADVANCED_RETRO';
2804 
2805        rec_retro_Allowances Get_retro_Allowances%ROWTYPE;
2806        TYPE
2807       r_ret_allowances IS RECORD(balance_name  pay_balance_types.balance_name%TYPE,
2808                              balance_value Number);
2809        TYPE
2810        tab_ret_allowances IS TABLE OF r_ret_allowances INDEX BY BINARY_INTEGER;
2811        t_ret_allowances tab_ret_allowances;
2812 
2813        rec_ret_entry_ids Get_retro_Entry_ids%ROWTYPE;
2814 
2815 
2816      ret_counter Number;
2817      retro_start date;
2818      retro_end date;
2819      x number;
2820      /* Added for #bug no 5846278 */
2821      orig_eff_date date;
2822      retro_eff_date date;
2823      time_span varchar2(10);
2824      retro_type varchar2(50);
2825      l_adv_retro_flag pay_legislation_rules.rule_mode%TYPE;
2826 
2827      Begin
2828        g_debug := hr_utility.debug_enabled;
2829        ret_counter := 1;
2830     /* Bug# 5846278 */
2831     /* Checked for legislation rule.*/
2832 
2833      OPEN get_legislation_rule;
2834      FETCH get_legislation_rule INTO l_adv_retro_flag;
2835      IF  get_legislation_rule%NOTFOUND THEN
2836         l_adv_retro_flag := 'N';
2837      END IF;
2838      CLOSE get_legislation_rule;
2839 
2840      /* Retropay by element - logic for Retropay By Element is used */
2841 
2842     IF l_adv_retro_flag <> 'Y'
2843     THEN
2844 
2845        OPEN Get_retro_Entry_ids(p_year_start,p_year_end,p_assignment_id);
2846        LOOP
2847        FETCH Get_retro_Entry_ids INTO rec_ret_entry_ids;
2848        IF Get_retro_Entry_ids%NOTFOUND Then
2849           IF g_debug THEN
2850           hr_utility.set_location('Get_retro_Entry_Id: not found',1);
2851       END if;
2852       Exit;
2853        End If;
2854       IF g_debug THEN
2855         hr_utility.set_location('Calling Get Retro Periods',2);
2856       END if;
2857 
2858        x:=pay_au_paye_ff.get_retro_period(rec_ret_entry_ids.element_entry_id,
2859                                           rec_ret_entry_ids.date_earned,
2860                                           p_registered_employer, /*Bug 4418107*/
2861                                           retro_start,
2862                                           retro_end);
2863 
2864       IF g_debug THEN
2865       hr_utility.set_location('Back from call to Get Retro Periods',3);
2866       END if;
2867 
2868        IF months_between(rec_ret_entry_ids.date_earned,retro_end) > 12 then
2869           IF g_debug THEN
2870                   hr_utility.set_location('Getting Retro Allowance  Greater than 12 months',4);
2871           END if;
2872 
2873           OPEN  Get_retro_Allowances(rec_ret_entry_ids.element_entry_id, rec_ret_entry_ids.balance_type_id);
2874           FETCH Get_retro_Allowances INTO rec_retro_Allowances;
2875           CLOSE Get_retro_Allowances;
2876 
2877 
2878            If NVL(rec_retro_Allowances.balance_value,0) <> 0 Then  /*bug14060570 to check +/- amounts */
2879 
2880               t_ret_allowances(ret_counter).balance_name   := rec_retro_Allowances.balance_name;
2881               t_ret_allowances(ret_counter).balance_value  := rec_retro_Allowances.balance_value;
2882               ret_counter := ret_counter+1;
2883            End If;
2884 
2885       END IF;
2886     END LOOP;
2887 
2888     CLOSE Get_retro_Entry_ids;
2889 
2890 /*Bug 7171534 Added t_allowance_balance.count > 0 in if clause */
2891    if t_ret_allowances.count > 0 and t_allowance_balance.count > 0 then
2892     For i in 1..t_ret_allowances.last
2893     LOOP
2894         For j in 1..t_allowance_balance.last
2895         LOOP
2896           if t_ret_allowances(i).balance_name = t_allowance_balance(j).balance_name then
2897           t_allowance_balance(j).balance_value := t_allowance_balance(j).balance_value - t_ret_allowances(i).balance_value;
2898           exit;
2899            end if;
2900         END LOOP;
2901     END LOOP;
2902    end if;
2903 
2904   t_ret_allowances.delete;
2905 
2906 /*bug #5846278 Enh Retro .
2907    If Retrospective Payment Greater than 12 months then it is deducted from total allowance*/
2908 
2909  ELSE
2910  OPEN Get_retro_Entry_ids(p_year_start,p_year_end,p_assignment_id);
2911      LOOP
2912      FETCH Get_retro_Entry_ids INTO rec_ret_entry_ids;
2913       IF Get_retro_Entry_ids%NOTFOUND Then
2914           IF g_debug THEN
2915           hr_utility.set_location('Get_retro_Entry_Id: not found',1);
2916           END if;
2917        Exit;
2918       End If;
2919       IF g_debug THEN
2920         hr_utility.set_location('Calling Get Retro Time Span',2);
2921       END if;
2922 
2923        x:= pay_au_paye_ff.get_retro_time_span(rec_ret_entry_ids.element_entry_id,
2924                                           rec_ret_entry_ids.date_earned,
2925                                           rec_ret_entry_ids.tax_unit_id,
2926                                           retro_start,
2927                                           retro_end,
2928                                           orig_eff_date,
2929                                           retro_eff_date,
2930                                           time_span,
2931                                           retro_type);
2932       IF g_debug THEN
2933       hr_utility.set_location('Back from call to Get Retro Time Span',3);
2934       END if;
2935       IF time_span ='GT12' then
2936           IF g_debug THEN
2937                   hr_utility.set_location('Getting Retro Allowance  Greater than 12 months',4);
2938           END if;
2939           OPEN  Get_retro_Allowances(rec_ret_entry_ids.element_entry_id, rec_ret_entry_ids.balance_type_id);
2940           FETCH Get_retro_Allowances INTO rec_retro_Allowances;
2941           CLOSE Get_retro_Allowances;
2942 
2943            If NVL(rec_retro_Allowances.balance_value,0) <> 0 Then /*bug14060570 to check +/- amou
2944 nts */
2945               t_ret_allowances(ret_counter).balance_name   := rec_retro_Allowances.balance_name;
2946               t_ret_allowances(ret_counter).balance_value  := rec_retro_Allowances.balance_value;
2947               ret_counter := ret_counter+1;
2948 
2949            End If;
2950       END IF;
2951     END LOOP;
2952 
2953     CLOSE Get_retro_Entry_ids;
2954 
2955 /*Bug 7171534 Added t_allowance_balance.count > 0 in if clause */
2956    if t_ret_allowances.count > 0 and t_allowance_balance.count > 0 then
2957     For i in 1..t_ret_allowances.last
2958     LOOP
2959         For j in 1..t_allowance_balance.last
2960         LOOP
2961 
2962           if t_ret_allowances(i).balance_name = t_allowance_balance(j).balance_name then
2963           t_allowance_balance(j).balance_value := t_allowance_balance(j).balance_value - t_ret_allowances(i).balance_value;
2964 
2965           exit;
2966           end if;
2967         END LOOP;
2968     END LOOP;
2969    end if;
2970 
2971   t_ret_allowances.delete;
2972 
2973 END IF;  /*bug #5846278 */
2974    return 1;
2975 
2976    End adjust_retro_allowances;
2977 
2978 
2979 /* Bug#4925547 */
2980 
2981 procedure archive_limited_values
2982 (p_assignment_action_id number
2983 ,p_table                in out nocopy tab_allownace_balance
2984 ,p_limit                in binary_integer
2985 ,p_name_prefix          in varchar2
2986 ,p_name_suffix          in varchar2
2987 ,p_value_prefix         in varchar2
2988 ,p_value_suffix         in varchar2
2989 ,p_total_name           in varchar2
2990 ) is
2991   l_total_value number := 0;
2992   l_procedure   constant varchar2(80) := g_package||'.archive_limited_values';
2993 begin
2994   --
2995   -- Do the number of available balance exceed the limit?
2996   --
2997   if p_table.count > p_limit then
2998     --
2999     -- Sum the number of balances above the limit into a single value
3000     -- Store the summed value and the name into the index corresponding to the limit.
3001     --
3002     for i in p_limit..p_table.count
3003     loop
3004       if g_debug then
3005         hr_utility.set_location(l_procedure,5);
3006       end if;
3007       if p_table.exists(i) then
3008         l_total_value := l_total_value + p_table(i).balance_value;
3009       end if;
3010     end loop;
3011     p_table(p_limit).balance_name  := p_total_name;
3012     p_table(p_limit).balance_value := l_total_value;
3013   end if;
3014   --
3015   -- Archive the values up to the limit
3016   --
3017   for i in 1..(p_limit)
3018   loop
3019     if p_table.exists(i) then
3020       if g_debug then
3021         hr_utility.set_location(l_procedure||' : archiving',20);
3022       end if;
3023 
3024       create_extract_archive_details
3025       (p_assignment_action_id
3026       ,p_name_prefix || i || p_name_suffix
3027       , p_table(i).balance_name
3028       );
3029       create_extract_archive_details
3030       (p_assignment_action_id
3031       ,p_value_prefix || i || p_value_suffix
3032       ,p_table(i).balance_value
3033       );
3034     end if;
3035   end loop;
3036 end archive_limited_values;
3037 
3038 PROCEDURE get_foreign_leave_payments
3039                 (p_assignment_id    IN  pay_assignment_actions.assignment_id%TYPE
3040                 ,p_tax_unit_id      IN  pay_assignment_actions.tax_unit_id%TYPE
3041                 ,p_start_date       IN  DATE
3042                 ,p_end_date         IN  DATE
3043                 ,p_leave_earnings   OUT NOCOPY NUMBER
3044                 ,p_leave_tax        OUT NOCOPY NUMBER
3045                 )
3046 IS
3047 
3048 CURSOR get_max_action_id
3049         (c_assignment_id        pay_assignment_actions.assignment_id%TYPE
3050         ,c_tax_unit_id          pay_assignment_actions.tax_unit_id%TYPE
3051         ,c_start_date           DATE
3052         ,c_end_date             DATE
3053         )
3054 IS
3055  SELECT TO_NUMBER(SUBSTR(MAX(LPAD(paa.action_sequence,15,'0')||paa.assignment_action_id),16)) assignment_action_id
3056          ,ptp.start_date
3057          ,ptp.end_date
3058          ,MAX(paa.action_sequence)   action_sequence
3059   FROM   pay_assignment_actions         paa,
3060          pay_payroll_actions            ppa,
3061          per_assignments_f              paf,
3062          per_time_periods               ptp
3063   WHERE  paa.assignment_id          = paf.assignment_id
3064   AND    paf.assignment_id          = c_assignment_id
3065   AND    ppa.payroll_action_id      = paa.payroll_action_id
3066   AND    ppa.date_earned            BETWEEN ptp.start_date AND ptp.end_date
3067   AND    ppa.payroll_id             =  paf.payroll_id
3068   AND    paf.payroll_id             =  ptp.payroll_id
3069   AND    ppa.action_type            in ('R', 'Q', 'I', 'V', 'B')
3070   AND    ppa.effective_date         BETWEEN paf.effective_start_date AND paf.effective_end_date
3071   AND    paa.action_status      = 'C'
3072   AND    paa.tax_unit_id        = c_tax_unit_id
3073   AND    ptp.start_date         <= c_end_date
3074   AND    ptp.end_date           >= c_start_date
3075   GROUP BY paa.assignment_id, ptp.time_period_id, ptp.start_date,ptp.end_date
3076   ORDER BY ptp.start_date;
3077 
3078 
3079     CURSOR get_asg_dates
3080            (c_assignment_id   per_assignments_f.assignment_id%TYPE
3081            ,c_tax_unit_id     VARCHAR2
3082            ,c_start_date      DATE
3083            ,c_end_date        DATE
3084            ,c_f_status_id     VARCHAR2
3085            ,c_j_status_id     VARCHAR2)
3086     IS
3087     SELECT paf.assignment_id
3088           ,paf.effective_start_date
3089           ,paf.effective_end_date
3090           ,paf.payroll_id
3091           ,hsc.segment1 tax_unit_id
3092     FROM   per_assignments_f paf
3093           ,hr_soft_coding_keyflex hsc
3094     WHERE  paf.assignment_id                = c_assignment_id
3095     AND    paf.soft_coding_keyflex_id       = hsc.soft_coding_keyflex_id
3096     AND    hsc.segment1                     LIKE c_tax_unit_id
3097     AND    to_char(paf.assignment_status_type_id) NOT LIKE c_f_status_id
3098     AND    to_char(paf.assignment_status_type_id) NOT LIKE c_j_status_id
3099     AND    paf.effective_start_date <= c_end_date
3100     AND    paf.effective_end_date   >= c_start_date
3101     ORDER BY paf.effective_start_date ASC;
3102 
3103 
3104     CURSOR csr_get_defined_balances
3105     IS
3106 
3107     SELECT pdb.defined_balance_id
3108            ,decode(pbt.balance_name, 'Foreign Leave Payments',1
3109                                    , 'Earnings_Total',2
3110                                    , 'Standard_Tax_Deductions',3
3111                                    , 'Foreign Worker Days',4
3112                     ) sort_index
3113     FROM    pay_defined_balances    pdb
3114            ,pay_balance_dimensions  pbd
3115            ,pay_balance_types       pbt
3116     WHERE pdb.balance_dimension_id  = pbd.balance_dimension_id
3117     AND   pbd.dimension_name        = '_ASG_LE_PTD'
3118     AND   pbd.legislation_code      = 'AU'
3119     AND   pdb.balance_type_id       = pbt.balance_type_id
3120     AND   pbt.legislation_code      = 'AU'
3121     AND   pbt.balance_name          IN (
3122                                         'Foreign Leave Payments'
3123                                        ,'Earnings_Total'
3124                                        ,'Standard_Tax_Deductions'
3125                                        ,'Foreign Worker Days')
3126     ORDER BY sort_index;
3127 
3128     l_days          NUMBER;
3129     l_period_days   NUMBER;
3130     l_start_date    DATE;
3131     l_end_date      DATE;
3132     l_std_factor    NUMBER;
3133 
3134     l_total_earnings    NUMBER;
3135     l_total_tax         NUMBER;
3136 
3137     l_earnings          NUMBER;
3138     l_tax               NUMBER;
3139 
3140     l_proc_name         VARCHAR2(100);
3141 
3142     l_leave_balance_value_tab   pay_balance_pkg.t_balance_value_tab;
3143     l_leave_context_table       pay_balance_pkg.t_context_tab;
3144     l_result_table              pay_balance_pkg.t_detailed_bal_out_tab;
3145 
3146 BEGIN
3147 
3148     g_debug     := hr_utility.debug_enabled;
3149 
3150     IF g_debug
3151     THEN
3152         l_proc_name     := g_package||'.get_foreign_leave_payments';
3153         hr_utility.set_location('Entering Procedure     '||l_proc_name,1000);
3154     END IF;
3155 
3156     l_total_earnings    := 0;
3157     l_total_tax         := 0;
3158 
3159     l_leave_context_table(1).tax_unit_id   := p_tax_unit_id ;
3160 
3161     FOR csr_rec IN csr_get_defined_balances
3162     LOOP
3163         l_leave_balance_value_tab(csr_rec.sort_index).defined_balance_id    := csr_rec.defined_balance_id;
3164     END LOOP;
3165 
3166     FOR csr_rec IN get_max_action_id(p_assignment_id
3167                                     ,p_tax_unit_id
3168                                     ,p_start_date
3169                                     ,p_end_date)
3170     LOOP
3171         l_result_table.DELETE;
3172 
3173         IF csr_rec.assignment_action_id IS NOT NULL
3174         THEN
3175                 pay_balance_pkg.get_value
3176                         (p_assignment_action_id     => csr_rec.assignment_action_id
3177                         ,p_defined_balance_lst      => l_leave_balance_value_tab
3178                         ,p_context_lst              => l_leave_context_table
3179                         ,p_output_table             => l_result_table
3180                         );
3181         END IF;
3182 
3183         IF g_debug
3184         THEN
3185             hr_utility.set_location('Start Date                '||csr_rec.start_date,1000);
3186             hr_utility.set_location('End Date                  '||csr_rec.end_date,1000);
3187             hr_utility.set_location('Foreign Leave Payments    '||l_result_table(1).balance_value,1000);
3188             hr_utility.set_location('Earnings_Total            '||l_result_table(2).balance_value,1000);
3189             hr_utility.set_location('Standard_Tax_Deductions   '||l_result_table(3).balance_value,1000);
3190             hr_utility.set_location('Foreign Worker Days       '||l_result_table(4).balance_value,1000);
3191         END IF;
3192 
3193         l_period_days   := (csr_rec.end_date - csr_rec.start_date) + 1;
3194         l_days          := 0;
3195         l_std_factor     := 1;
3196 
3197         IF l_result_table(1).balance_value  <> 0
3198         THEN
3199             /* Foreign Leave Payments Exist. Now verify if these are for
3200                 Standard Periods
3201             */
3202             IF pay_au_foreign_workers.cached   = FALSE
3203             THEN
3204                 pay_au_foreign_workers.populate_asg_status_id;
3205             END IF;
3206 
3207             IF (l_result_table(4).balance_value  = 0)
3208             THEN
3209 
3210                 FOR csr_days_rec IN get_asg_dates
3211                                    (p_assignment_id
3212                                    ,p_tax_unit_id
3213                                    ,csr_rec.start_date
3214                                    ,csr_rec.end_date
3215                                    ,pay_au_foreign_workers.g_fw_asg_f_status_id
3216                                    ,pay_au_foreign_workers.g_fw_asg_j_status_id)
3217                 LOOP
3218                     l_start_date    := GREATEST(csr_days_rec.effective_start_date,csr_rec.start_date);
3219                     l_end_date      := LEAST(csr_days_rec.effective_end_date,csr_rec.end_date);
3220                     l_days  := l_days + (l_end_date - l_start_date) + 1;
3221                 END LOOP;
3222 
3223                 l_std_factor     := l_days/l_period_days;
3224 
3225             ELSE
3226                 l_std_factor     := 1 - LEAST(l_result_table(4).balance_value/l_period_days,1);
3227             END IF;
3228         END IF;
3229 
3230         l_earnings  :=  TRUNC(l_result_table(1).balance_value * l_std_factor);
3231         l_tax       :=  TRUNC(l_result_table(3).balance_value * (l_result_table(1).balance_value/l_result_table(2).balance_value) * l_std_factor);
3232 
3233         l_total_earnings    :=  l_total_earnings + l_earnings;
3234         l_total_tax         :=  l_total_tax + l_tax;
3235 
3236         IF g_debug
3237         THEN
3238             hr_utility.set_location('l_std_factor                '||l_std_factor,1000);
3239             hr_utility.set_location('l_earnings                  '||l_earnings,1000);
3240             hr_utility.set_location('l_tax                       '||l_tax,1000);
3241         END IF;
3242 
3243     END LOOP;
3244 
3245         IF g_debug
3246         THEN
3247             hr_utility.set_location('Leaving Procedure     '||l_proc_name,1000);
3248             hr_utility.set_location('Final l_total_earnings               '||l_total_earnings,1000);
3249             hr_utility.set_location('Final l_total_tax                    '||l_total_tax,1000);
3250         END IF;
3251 
3252     p_leave_earnings   :=   l_total_earnings;
3253     p_leave_tax        :=   l_total_tax;
3254 
3255 
3256 
3257 END get_foreign_leave_payments;
3258 
3259 ----------------------------------------------------------------------------
3260 /* Archive FW Details - bug9147430
3261     In addition to Individual eoy archiving, the procedure archives FW eoy values for each Foreign Worker status type
3262 
3263   Basic rule to produce FW payment summary is
3264            Total EOY balances including FW - FW EOY balances > 0
3265 
3266            IF Total EOY balances - FW EOY balances > 0 THEN
3267                 IF FW EOY balance <> 0 THEN
3268                       X_FW_PAYG_TYPE = 'PF'
3269                 ELSE
3270                       X_FW_PAYG_TYPE = 'P'
3271                 END IF
3272            ELSIF Total EOY balances - FW EOY balances = 0 THEN
3273                       X_FW_PAYG_TYPE = 'F'
3274            END IF
3275 
3276   Allowance and Unions should be sorted after adjusting FW eoy values to display on payment summary.
3277 
3278 */
3279 ----------------------------------------------------------------------------
3280 procedure archive_fw_details
3281      (p_assignment_id           in   pay_assignment_actions.ASSIGNMENT_ID%type
3282      ,p_registered_employer     in   NUMBER
3283      ,p_assignment_action_id       in  pay_assignment_actions.assignment_action_id%type
3284      ,p_year_start              in   DATE
3285      ,p_year_end                in   DATE
3286      ,p_fw_check in varchar2
3287      ,p_actual_termination_date in    per_periods_of_service.actual_termination_date%TYPE
3288      ,p_fw_alw_bal_exist in varchar2
3289      ) is
3290 
3291 cursor get_balance_name (c_defined_balance_id pay_defined_balances.defined_balance_id%type) is
3292 select NVL(pbt.reporting_name,pbt.balance_name) balance_name
3293 from pay_defined_balances pdb,
3294         pay_balance_types pbt
3295 where pdb.defined_balance_id = c_defined_balance_id
3296 and pdb.balance_type_id = pbt.balance_type_id
3297 and pdb.business_group_id = g_business_group_id;
3298 
3299   cursor csr_union_fees
3300   (c_assignment_id per_all_assignments.assignment_id%type
3301   ,c_fw_period_end      date
3302   ) is
3303   select distinct pet.reporting_name
3304   ,      pet.element_information_category
3305   ,      pet.element_information1
3306   from   pay_balance_types      pbt
3307   ,      pay_balance_feeds_f    pbf
3308   ,      pay_input_values_f     piv
3309   ,      pay_element_types_f    pet
3310   ,      pay_element_entries_f  pee
3311   ,      pay_element_links_f    pel
3312   ,      per_all_people_f       pap
3313   ,      per_periods_of_service pps
3314   ,      per_all_assignments_f  paa
3315   where  pet.element_type_id      = piv.element_type_id
3316   and    pbf.input_value_id       = piv.input_value_id
3317   and    pbf.balance_type_id      = pbt.balance_type_id
3318   and    pet.element_type_id      = pel.element_type_id
3319   and    pel.element_link_id      = pee.element_link_id
3320   and    pee.assignment_id        = c_assignment_id
3321   and    pee.assignment_id        = paa.assignment_id
3322   and    paa.person_id            = pap.person_id
3323   and    paa.person_id            = pps.person_id
3324   and    paa.period_of_service_id = pps.period_of_service_id
3325   and    pbt.balance_name         = 'Union Fees'
3326   and    pbt.legislation_code     = g_legislation_code
3327   and    pet.effective_start_date = (select max(et.effective_start_date )
3328                                      from pay_element_types_f et
3329                                      where et.element_type_id= pet.element_type_id
3330                                      and c_fw_period_end between pet.effective_Start_date and pet.effective_end_date
3331                                    );
3332 
3333      l_procedure           constant varchar2(80) := 'archive_fw_details';
3334      l_fw_earnings_ytd number;
3335      l_fw_lump_sumE_ytd number;
3336      l_fw_tax_withheld number;
3337      l_fw_sub_balance number :=0;
3338      l_fw_net_balance number := 0;
3339      l_fw_tot_sub_balance number :=0;
3340      l_fw_tot_net_balance number := 0;
3341      l_payg_fw_type varchar2(10) := 'P';
3342      l_fw_date_tab         pay_au_foreign_workers.tab_fw_dates;
3343      l_fw_output_tab    pay_balance_pkg.t_detailed_bal_out_tab;
3344      l_fw_alw_output_tab    pay_balance_pkg.t_detailed_bal_out_tab;
3345      l_fw_union_output_tab    pay_balance_pkg.t_detailed_bal_out_tab;
3346      l_fw_start_date date;
3347      l_fw_end_date date;
3348      l_fw_date varchar2(20);
3349      x number;
3350      u_idx number;
3351      a_idx number;
3352      i_index number;
3353      p_fw_alw_tab tab_allownace_balance;
3354      p_fw_alw_limit_tab tab_allownace_balance;
3355      p_fw_allowance_tab tab_allownace_balance;
3356      p_fw_union_tab tab_allownace_balance;
3357 
3358 
3359 --create or replace type rec_au_eoy_bal_type is object (balance_name varchar2(80), balance_value number);
3360 --create or replace type tab_au_eoy_bal_type is table of rec_au_eoy_bal_type ;
3361      t_alw_bal_sort tab_au_eoy_bal_type := tab_au_eoy_bal_type();
3362      cnt number;
3363      idx number;
3364      t_union_bal_sort tab_au_eoy_bal_type := tab_au_eoy_bal_type();
3365      counter number;
3366      ix number;
3367      l_reporting_name   pay_element_types_f.reporting_name%type;
3368      l_2006_union_setup_found          boolean := false;
3369      l_union_count number;
3370      l_total_value number;
3371      l_4_value     number;
3372      l_4_name      pay_balance_types.balance_name%type;
3373 
3374     l_fw_f_type varchar2(1);
3375     l_fw_j_type varchar2(1);
3376 
3377     l_alw_text varchar2(240);
3378     l_alw_total number;
3379     type p_fw_alw_concat_type is table of varchar2(240) index by binary_integer;
3380     p_fw_alw_tab_concat p_fw_alw_concat_type;
3381     p_fw_alw_tab_concat_new p_fw_alw_concat_type;
3382 
3383     /* start bug 9950136 */
3384     n number;
3385     l_retro_fw_gross number;
3386     l_retro_fw_tax number;
3387     /* end bug 9950136 */
3388 
3389     /* Start bug 10331262 */
3390     l_lve_earnings      NUMBER;
3391     l_lve_tax           NUMBER;
3392     l_term_earnings     NUMBER;
3393     l_term_tax          NUMBER;
3394     l_lumpsuma          NUMBER;
3395     /* End bug 10331262  */
3396 
3397 begin
3398 
3399      g_debug := hr_utility.debug_enabled;
3400 
3401      if g_debug then
3402           hr_utility.set_location('Entering ' || l_procedure, 1);
3403           hr_utility.set_location('p_assignment_id ' || p_assignment_id, 1);
3404           hr_utility.set_location('p_registered_employer ' || p_registered_employer, 1);
3405           hr_utility.set_location('p_fw_alw_bal_exist ' || p_fw_alw_bal_exist, 1);
3406      end if;
3407 
3408      /* Initialise Values */
3409             l_lve_earnings     := 0;
3410             l_lve_tax          := 0;
3411             l_term_earnings    := 0;
3412             l_term_tax         := 0;
3413             l_lumpsuma         := 0;
3414 
3415 
3416 IF p_fw_check = 'Y' THEN
3417 
3418   if t_fw_gross_type.count = 0 then    -- this condition is not executed when it is called from archive_allowance_details
3419 
3420         p_fw_union_tab_sorted.delete;
3421         p_fw_alw_tab_sorted.delete;
3422         f_fw_date_tab_g.delete;
3423         j_fw_date_tab_g.delete;
3424 
3425      l_fw_f_type := pay_au_foreign_workers.check_foreign_worker(p_assignment_id
3426                                                                ,p_registered_employer
3427                                                                ,p_year_start
3428                                                                ,p_year_end
3429                                                                ,pay_au_foreign_workers.g_fw_f_type);
3430 
3431      l_fw_j_type := pay_au_foreign_workers.check_foreign_worker(p_assignment_id
3432                                                                ,p_registered_employer
3433                                                                ,p_year_start
3434                                                                ,p_year_end
3435                                                                ,pay_au_foreign_workers.g_fw_j_type);
3436 
3437      if l_fw_f_type = 'Y' then
3438         pay_au_foreign_workers.get_foreign_worker_dates(p_assignment_id
3439                                                        ,p_registered_employer
3440                                                        ,p_year_start
3441                                                        ,p_year_end
3442                                                        ,pay_au_foreign_workers.g_fw_f_type
3443                                                        ,f_fw_date_tab_g);
3444      end if;
3445      if l_fw_j_type = 'Y' then
3446         pay_au_foreign_workers.get_foreign_worker_dates(p_assignment_id
3447                                                        ,p_registered_employer
3448                                                        ,p_year_start
3449                                                        ,p_year_end
3450                                                        ,pay_au_foreign_workers.g_fw_j_type
3451                                                        ,j_fw_date_tab_g);
3452      end if;
3453 
3454       if f_fw_date_tab_g.count > 0 then
3455         t_fw_gross_type(1) := 'F';
3456         if j_fw_date_tab_g.count >0 then
3457           t_fw_gross_type(2) := 'J';
3458         end if;
3459        elsif j_fw_date_tab_g.count > 0 then
3460           t_fw_gross_type(1) := 'J';
3461       end if;
3462 
3463   end if;
3464 
3465 if g_debug then
3466     hr_utility.set_location('t_fw_gross_type.count '||t_fw_gross_type.count,2);
3467 end if;
3468 
3469 FOR i_idx in t_fw_gross_type.first .. t_fw_gross_type.last
3470 LOOP
3471 
3472 IF p_fw_alw_bal_exist = 'N' THEN
3473 
3474     create_extract_archive_details(p_assignment_action_id
3475                                             ,'X_FW'||i_idx||'_GROSS_PAYMENT_TYPE'
3476                                             ,t_fw_gross_type(i_idx));
3477 
3478     /* FW Start Date and End Date check*/
3479     if g_debug then hr_utility.set_location('FW Gross Type : ' || t_fw_gross_type(i_idx),10); end if;
3480 
3481         IF t_fw_gross_type(i_idx) = 'F' THEN
3482 
3483            i_index := f_fw_date_tab_g.FIRST;
3484            l_fw_start_date := f_fw_date_tab_g(i_index).start_date;
3485 
3486            i_index := f_fw_date_tab_g.LAST;
3487            l_fw_end_date := f_fw_date_tab_g(i_index).end_date;
3488 
3489         ELSIF t_fw_gross_type(i_idx) = 'J' THEN
3490 
3491            i_index := j_fw_date_tab_g.FIRST;
3492            l_fw_start_date := j_fw_date_tab_g(i_index).start_date;
3493 
3494            i_index := j_fw_date_tab_g.LAST;
3495            l_fw_end_date := j_fw_date_tab_g(i_index).end_date;
3496 
3497         END IF;
3498 
3499 
3500         if p_actual_termination_date is not null and
3501            p_actual_termination_date between l_fw_start_date and l_fw_end_date then
3502                     l_fw_end_date :=p_actual_termination_date;
3503        end if;
3504 
3505          if g_debug then
3506                hr_utility.trace('l_fw_start_date : '||to_char(l_fw_start_date, 'DD-MON-YYYY'));
3507                hr_utility.trace('l_fw_end_date : '||to_char(l_fw_end_date, 'DD-MON-YYYY'));
3508          end if;
3509 
3510          l_fw_date := to_char(l_fw_start_date,'DDMMYYYY');
3511          create_extract_archive_details(p_assignment_action_id
3512                                     ,'X_FW'||i_idx||'_START_DATE'
3513                                     ,l_fw_date);
3514 
3515          l_fw_date := to_char(l_fw_end_date,'DDMMYYYY');
3516          create_extract_archive_details(p_assignment_action_id
3517                                    ,'X_FW'||i_idx||'_END_DATE'
3518                                    ,l_fw_date);
3519 
3520     /* FW EOY blance archive value */
3521      l_fw_output_tab.delete;
3522      IF t_fw_gross_type(i_idx) = 'F' THEN
3523        pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
3524                                                                        p_registered_employer,
3525                                                                        f_fw_date_tab_g,
3526                                                                        p_year_start,
3527                                                                        p_year_end,
3528                                                                        pay_au_foreign_workers.g_fw_f_type,
3529                                                                        p_fw_balance_type_tab,
3530                                                                        l_fw_output_tab) ;
3531      ELSIF t_fw_gross_type(i_idx) = 'J' THEN
3532        pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
3533                                                                        p_registered_employer,
3534                                                                        j_fw_date_tab_g,
3535                                                                        p_year_start,
3536                                                                        p_year_end,          /* Bug 10348688 */
3537                                                                        pay_au_foreign_workers.g_fw_j_type,
3538                                                                        p_fw_balance_type_tab,
3539                                                                        l_fw_output_tab) ;
3540      END IF;
3541 
3542     if g_debug then
3543       hr_utility.set_location('FW Balance Values are  ------------------------------------------------',12);
3544       hr_utility.trace('Earnings_Total                    ===>' || l_fw_output_tab(1).balance_value);
3545       hr_utility.trace('Leave Payments Marginal                    ===>' || l_fw_output_tab(2).balance_value);
3546       hr_utility.trace('Workplace Giving Deductions                    ===>' || l_fw_output_tab(3).balance_value);
3547       hr_utility.trace('Lump Sum E Payments                   ===>' || l_fw_output_tab(4).balance_value);
3548       hr_utility.trace('Retro Earnings Leave Loading GT 12 Mths Amount                    ===>' || l_fw_output_tab(5).balance_value);
3549       hr_utility.trace('Retro Earnings Spread GT 12 Mths Amount                    ===>' || l_fw_output_tab(6).balance_value);
3550       hr_utility.trace('Retro Pre Tax GT 12 Mths Amount                    ===>' || l_fw_output_tab(7).balance_value);
3551       hr_utility.trace('Total_Tax_Deductions                    ===>' || l_fw_output_tab(8).balance_value);
3552       hr_utility.trace('Termination Deductions                    ===>' || l_fw_output_tab(9).balance_value);
3553       hr_utility.trace('Lump Sum C Deductions                    ===>' || l_fw_output_tab(10).balance_value);
3554       hr_utility.trace('Foreign Tax Deductions                    ===>' || l_fw_output_tab(11).balance_value);
3555       hr_utility.trace('Lump Sum A Payments                    ===>' || l_fw_output_tab(12).balance_value);
3556       hr_utility.trace('Lump Sum D Payments                    ===>' || l_fw_output_tab(13).balance_value);
3557       hr_utility.trace('Reportable Employer Superannuation Contributions                    ===>' || l_fw_output_tab(14).balance_value);
3558       hr_utility.trace('Union Fees               ===>' || l_fw_output_tab(15).balance_value);
3559       hr_utility.trace('CDEP               ===>' || l_fw_output_tab(16).balance_value);
3560       hr_utility.trace('Exempt Foreign Employment Income               ===>' || l_fw_output_tab(17).balance_value);
3561       hr_utility.trace('Retro LT 12 Mths Prev Yr Amount               ===>' || l_fw_output_tab(18).balance_value);
3562       hr_utility.trace('Retro Earnings Leave Loading LT 12 Mths Prev Yr Amount               ===>' || l_fw_output_tab(19).balance_value);
3563       hr_utility.trace('Retro Earnings Spread LT 12 Mths Prev Yr Amount               ===>' || l_fw_output_tab(20).balance_value);
3564       hr_utility.trace('Retro Pre Tax LT 12 Mths Prev Yr Amount               ===>' || l_fw_output_tab(21).balance_value);
3565       hr_utility.trace('Retro LT 12 Mths Curr Yr Amount               ===>' || l_fw_output_tab(22).balance_value);
3566       hr_utility.trace('Retro Earnings Leave Loading LT 12 Mths Curr Yr Amount               ===>' || l_fw_output_tab(23).balance_value);
3567       hr_utility.trace('Retro Earnings Spread LT 12 Mths Curr Amount               ===>' || l_fw_output_tab(24).balance_value);
3568       hr_utility.trace('Retro Tax GT12 Amount               ===>' || l_fw_output_tab(25).balance_value);
3569       hr_utility.trace('Retro Tax LT12 Prev Amount               ===>' || l_fw_output_tab(26).balance_value);
3570       hr_utility.trace('Retro Tax LT12 Curr Amount               ===>' || l_fw_output_tab(27).balance_value);
3571       hr_utility.trace('Retro Earnings Additional GT 12 Mths Amount               ===>' || l_fw_output_tab(29).balance_value);  -- bug 13362286
3572       hr_utility.trace('Retro Earnings Additional LT12 Prev Mths Amount               ===>' || l_fw_output_tab(30).balance_value);
3573       hr_utility.trace('Retro Earnings Additional LT12 Curr Mths Amount               ===>' || l_fw_output_tab(31).balance_value);
3574     end if;
3575 
3576           /* Bug 10331262 - Reporting of Termination Payments and Foreign Leave Payments
3577 
3578             Leave Payments:
3579             l_lve_earnings        =  Foreign Leave Payment (for non FW-periods)
3580             l_lve_tax             =  Tax on Foreign Leave Payment (for non FW-periods)
3581             l_term_earnings       = Foreign Leave Payments Marginal
3582             l_lump_sum_a          = Foreign Lump Sum A Payments
3583             l_term_tax            = Foreign Leave Components Deduction + Foreign Lump Sum A Deduction
3584           */
3585                 IF (i_idx = 1)
3586                 THEN
3587                        l_term_earnings  := p_result_table(31).balance_value;
3588                        l_lumpsuma       := p_result_table(32).balance_value;
3589                        l_term_tax       := p_result_table(33).balance_value + p_result_table(34).balance_value;
3590 
3591                         IF (p_result_table(30).balance_value - l_fw_output_tab(28).balance_value) <> 0
3592                         THEN
3593 
3594                             get_foreign_leave_payments(p_assignment_id
3595                                                       ,p_registered_employer
3596                                                       ,p_year_start
3597                                                       ,p_year_end
3598                                                       ,l_lve_earnings
3599                                                       ,l_lve_tax);
3600                         ELSE
3601                             l_lve_earnings     := 0;
3602                             l_lve_tax          := 0;
3603                         END IF;
3604 
3605                 ELSE
3606                         l_lve_earnings     := 0;
3607                         l_lve_tax          := 0;
3608                         l_term_earnings    := 0;
3609                         l_term_tax         := 0;
3610                         l_lumpsuma         := 0;
3611 
3612                 END IF;
3613 
3614 
3615 
3616            /* Bug 10331262 - Modified termination payments added to earnings
3617               l_fw_earnings_ytd  := 'Earnings_Total' + 'Foreign Termination Payments' + 'Foreign Leave Payments'
3618                                     + 'Workplace Giving' - 'Exempt Foreign Employment Income'   --10143762
3619               l_fw_lump_sumE_ytd := 'Lump Sum E Payments' + 'Retro Earnings Leave Loading GT 12 Mths Amount'
3620                                     + 'Retro Earnings Spread GT 12 Mths Amount' - 'Retro Pre Tax GT 12 Mths Amount'*/
3621            -- No retro allowance adjustment is unnecessary
3622            l_fw_earnings_ytd := l_fw_output_tab(1).balance_value + l_term_earnings + l_lve_earnings
3623                               + l_fw_output_tab(3).balance_value - l_fw_output_tab(17).balance_value;
3624 
3625            l_fw_lump_sumE_ytd := l_fw_output_tab(4).balance_value + l_fw_output_tab(5).balance_value
3626                                              + l_fw_output_tab(6).balance_value - l_fw_output_tab(7).balance_value
3627                                              + l_fw_output_tab(29).balance_value;  -- bug 13362286
3628 
3629 
3630           /* start bug 9950136 */
3631           l_retro_fw_gross := 0;
3632           l_retro_fw_tax := 0;
3633           IF t_fw_gross_type(i_idx) = 'F' THEN
3634             n := pay_au_payment_summary.adjust_retro_fw(p_assignment_id
3635                                                        ,p_registered_employer
3636                                                        ,p_year_start
3637                                                        ,p_year_end
3638                                                        ,pay_au_foreign_workers.g_fw_f_type
3639                                                        ,l_retro_fw_gross
3640                                                        ,l_retro_fw_tax);
3641           ELSIF t_fw_gross_type(i_idx) = 'J' THEN
3642             n := pay_au_payment_summary.adjust_retro_fw(p_assignment_id, p_registered_employer, p_year_start, p_year_end, pay_au_foreign_workers.g_fw_j_type,
3643                                                l_retro_fw_gross, l_retro_fw_tax);
3644           END IF;
3645 
3646 
3647 
3648 
3649            /* 9950136 - Reset the Earnings values  adjusted for Retro
3650               13362286 - Retro Earnings Additional
3651               Earnings = Earnings - Retro GT12 (lump sum E)
3652                                   - (Retro LT 12 Mths Prev Yr Amount + Retro Earnings Leave Loading LT 12 Mths Prev Yr Amount
3653                                        + Retro Earnings Spread LT 12 Mths Prev Yr Amount - Retro Pre Tax LT 12 Mths Prev Yr Amount + Retro Earnings Additional LT12 Prev Mths Amount)
3654                                   - (Retro LT 12 Mths Curr Yr Amount + Retro Earnings Leave Loading LT 12 Mths Curr Yr Amount
3655                                        + Retro Earnings Spread LT 12 Mths Curr Amount + Retro Earnings Additional LT12 Curr Mths Amount - l_retro_fw_gross
3656 
3657               Bug 10331262 - Replaced Termination Deductions, Lump Sum C with Foreign Termination Deductions
3658              Tax Withheld = Total_Tax_Deductions + Termination Deductions - Lump Sum C Deductions
3659                            - Retro Tax GT12 Amount
3660                            - 'Retro Tax LT12 Prev Amount'
3661                            - (Retro Tax LT12 Curr Amount - l_retro_fw_tax)
3662             */
3663 
3664            l_fw_earnings_ytd := l_fw_earnings_ytd - l_fw_lump_sumE_ytd  -- GT12
3665                                                   -(l_fw_output_tab(18).balance_value + l_fw_output_tab(19).balance_value --LT12_Prev
3666                                                   + l_fw_output_tab(20).balance_value - l_fw_output_tab(21).balance_value + l_fw_output_tab(30).balance_value)
3667                                                   - (l_fw_output_tab(22).balance_value + l_fw_output_tab(23).balance_value -- LT12_Curr
3668                                                   +l_fw_output_tab(24).balance_value + l_fw_output_tab(31).balance_value - l_retro_fw_gross) ;
3669 
3670            l_fw_lump_sumE_ytd := 0;
3671 
3672            l_fw_tax_withheld  := l_fw_output_tab(8).balance_value + l_term_tax + l_lve_tax
3673                                 - l_fw_output_tab(25).balance_value
3674                                 - l_fw_output_tab(26).balance_value
3675                                 - (l_fw_output_tab(27).balance_value - l_retro_fw_tax);
3676           /* end bug 9950136 */
3677 
3678            create_extract_archive_details(p_assignment_action_id
3679                                          ,'X_FW'||i_idx||'_WORKPLACE_GIVING_DEDUCTIONS'
3680                                          ,l_fw_output_tab(3).balance_value);
3681 
3682            create_extract_archive_details(p_assignment_action_id
3683                                        ,'X_FW'||i_idx||'_GROSS_PAYMENTS'
3684                                        ,l_fw_earnings_ytd);
3685 
3686            create_extract_archive_details(p_assignment_action_id
3687                                        ,'X_FW'||i_idx||'_LUMP_SUM_E_PAYMENTS'
3688                                        ,l_fw_lump_sumE_ytd);
3689 
3690 
3691            create_extract_archive_details(p_assignment_action_id
3692                                         ,'X_FW'||i_idx||'_TOTAL_TAX_WITHHELD'
3693                                          ,l_fw_tax_withheld);
3694 
3695            create_extract_archive_details(p_assignment_action_id
3696                                          ,'X_FW'||i_idx||'_FOREIGN_TAX_PAID'
3697                                          , l_fw_output_tab(11).balance_value);
3698 
3699            /* Bug 10331262  - Replaced Lump Sum A based on Foreign Plan payments */
3700            create_extract_archive_details(p_assignment_action_id
3701                                          ,'X_FW'||i_idx||'_LUMP_SUM_A_PAYMENTS'
3702                                          ,l_lumpsuma);
3703 
3704            create_extract_archive_details(p_assignment_action_id
3705                                          ,'X_FW'||i_idx||'_LUMP_SUM_D_PAYMENTS'
3706                                          ,l_fw_output_tab(13).balance_value);
3707 
3708            create_extract_archive_details(p_assignment_action_id
3709                                          ,'X_FW'||i_idx||'_RPT_EMPLOYER_SUPERANN_CONTR'
3710                                          ,l_fw_output_tab(14).balance_value);
3711 
3712             create_extract_archive_details(p_assignment_action_id
3713                                          ,'X_FW'||i_idx||'_UNION_FEES'
3714                                          ,l_fw_output_tab(15).balance_value);
3715 
3716             create_extract_archive_details(p_assignment_action_id
3717                                          ,'X_FW'||i_idx||'_CDEP'
3718                                          ,l_fw_output_tab(16).balance_value);
3719 
3720           l_fw_net_balance      := l_fw_earnings_ytd + l_fw_lump_sumE_ytd + l_fw_tax_withheld
3721                                     + l_fw_output_tab(12).balance_value + l_fw_output_tab(13).balance_value + l_fw_output_tab(14).balance_value ;
3722           l_fw_sub_balance      := l_fw_output_tab(13).balance_value + l_fw_output_tab(14).balance_value ;
3723           l_fw_tot_net_balance  := l_fw_tot_net_balance + l_fw_net_balance;
3724           l_fw_tot_sub_balance  := l_fw_tot_sub_balance + l_fw_sub_balance;
3725 
3726   /* When FW Unions balance value > 0 */
3727   IF l_fw_output_tab(15).balance_value > 0 THEN
3728      if g_debug then
3729           hr_utility.set_location('FW Union starting ' || l_procedure, 20);
3730      end if;
3731 
3732      g_fw_union_value := g_fw_union_value + l_fw_output_tab(15).balance_value;
3733      l_union_count := 0;  -- bug#10084818  initializing
3734      l_total_value := 0;  -- bug#10084818  initializing
3735 
3736      l_fw_union_output_tab.delete;
3737      IF t_fw_gross_type(i_idx) = 'F' THEN
3738       pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
3739                                                                 p_registered_employer,
3740                                                                 f_fw_date_tab_g,
3741                                                                 p_year_start,
3742                                                                 p_year_end,
3743                                                                 pay_au_foreign_workers.g_fw_f_type,
3744                                                                 p_fw_union_balance_type_tab,
3745                                                                 l_fw_union_output_tab) ;
3746       ELSIF t_fw_gross_type(i_idx) = 'J' THEN
3747       pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
3748                                                                 p_registered_employer,
3749                                                                 j_fw_date_tab_g,
3750                                                                 p_year_start,
3751                                                                 p_year_end,
3752                                                                 pay_au_foreign_workers.g_fw_j_type,
3753                                                                 p_fw_union_balance_type_tab,
3754                                                                 l_fw_union_output_tab);
3755       END IF;
3756 
3757      if l_fw_union_output_tab.count > 0 then
3758 
3759         p_fw_union_tab.delete;
3760         t_union_bal_sort.delete;
3761 
3762         counter := 1;
3763         for i in l_fw_union_output_tab.first .. l_fw_union_output_tab.last loop
3764               if l_fw_union_output_tab(i).balance_value > 0 then
3765                     for union_rec in get_balance_name(l_fw_union_output_tab(i).defined_balance_id) loop
3766                                p_fw_union_tab(counter).balance_name := union_rec.balance_name;
3767                                p_fw_union_tab(counter).balance_value := l_fw_union_output_tab(i).balance_value;
3768                                counter := counter + 1;
3769                       end loop;
3770                  end if;
3771         end loop;
3772 
3773        if p_fw_union_tab.count >0 then
3774 /*
3775            if g_debug then
3776                 hr_utility.trace('FW Union before Sorting');
3777                 for i in p_fw_union_tab.first .. p_fw_union_tab.last loop
3778                      hr_utility.trace(p_fw_union_tab(i).balance_name||' $'||p_fw_union_tab(i).balance_value);
3779                 end loop;
3780             end if;
3781 */
3782             IF t_fw_gross_type(i_idx) = 'F' THEN
3783                 for i in f_fw_date_tab_g.first .. f_fw_date_tab_g.last loop
3784                      for csr_rec in csr_union_fees(p_assignment_id, f_fw_date_tab_g(i).end_date) loop
3785                             if l_2006_union_setup_found then
3786                                     null;
3787                              else
3788                                   if csr_rec.element_information_category = 'AU_VOLUNTARY DEDUCTIONS'
3789                                   and csr_rec.element_information1 is not null then
3790                                            l_2006_union_setup_found := true;
3791                                    end if;
3792                               end if;
3793 
3794                               l_reporting_name := csr_rec.reporting_name;
3795                               l_union_count := l_union_count + 1;
3796                       end loop;
3797                   end loop;
3798            ELSIF t_fw_gross_type(i_idx) = 'J' THEN
3799                 for i in j_fw_date_tab_g.first .. j_fw_date_tab_g.last loop
3800                      for csr_rec in csr_union_fees(p_assignment_id, j_fw_date_tab_g(i).end_date) loop
3801                             if l_2006_union_setup_found then
3802                                  null;
3803                              else
3804                                 if csr_rec.element_information_category = 'AU_VOLUNTARY DEDUCTIONS'
3805                                 and csr_rec.element_information1 is not null then
3806                                             l_2006_union_setup_found := true;
3807                                  end if;
3808                              end if;
3809 
3810                              l_reporting_name := csr_rec.reporting_name;
3811                              l_union_count := l_union_count + 1;
3812                          end loop;
3813                      end loop;
3814            END IF;
3815 
3816            if l_union_count > 1 or l_reporting_name = 'Balance Initialization 4' then
3817                      l_reporting_name := 'MISCELLANEOUS';
3818            end if;
3819 
3820           if l_reporting_name is not null then
3821 
3822                                   create_extract_archive_details
3823                                   (p_assignment_action_id
3824                                   ,'X_FW'||i_idx||'_UNION_NAMES'
3825                                   ,l_reporting_name
3826                                   );
3827 
3828           end if;
3829 
3830           if l_2006_union_setup_found then
3831 
3832               ix := p_fw_union_tab.first;
3833               loop
3834               exit when ix is null;
3835                   t_union_bal_sort.extend;
3836                   t_union_bal_sort(t_union_bal_sort.last) := rec_au_eoy_bal_type(p_fw_union_tab(ix).balance_name
3837                                                                                                           , p_fw_union_tab(ix).balance_value);
3838                   ix := p_fw_union_tab.next(ix);
3839               end loop;
3840 
3841               SELECT CAST (MULTISET (SELECT rec_au_eoy_bal_type(balance_name, balance_value)
3842                                                       FROM   TABLE(t_union_bal_sort)
3843                                                       ORDER BY balance_value desc)
3844                                    AS tab_au_eoy_bal_type)
3845                   INTO t_union_bal_sort
3846                  FROM dual;
3847 
3848                if g_debug then
3849                      hr_utility.trace('FW Union after Sorting');
3850                      for i in t_union_bal_sort.first .. t_union_bal_sort.last loop
3851                            hr_utility.trace(t_union_bal_sort(i).balance_name||' $'||t_union_bal_sort(i).balance_value);
3852                       end loop;
3853                 end if;
3854 
3855                 for i in 1..3 loop
3856                   if t_union_bal_sort.exists(i) then -- bug10048508
3857                     create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_' || i || '_NAME',t_union_bal_sort(i).balance_name);
3858                     create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_' || i || '_VALUE',t_union_bal_sort(i).balance_value);
3859                     l_total_value := l_total_value + t_union_bal_sort(i).balance_value;
3860                   end if;
3861                 end loop;
3862 
3863                 if t_union_bal_sort.count > 4 or l_total_value <> l_fw_output_tab(15).balance_value then
3864                    l_4_name  := 'Miscellaneous';
3865                    l_4_value := l_fw_output_tab(15).balance_value - l_total_value;
3866                   create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_4_NAME',l_4_name);
3867                   create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_4_VALUE',l_4_value);
3868                 else
3869                    if t_union_bal_sort.exists(4) then
3870                      if (l_total_value + t_union_bal_sort(4).balance_value) =  l_fw_output_tab(15).balance_value then
3871                           l_4_name := t_union_bal_sort(4).balance_name;
3872                           l_4_value := t_union_bal_sort(4).balance_value;
3873                      else
3874                           l_4_name  := 'Miscellaneous';
3875                           l_4_value := l_fw_output_tab(15).balance_value - l_total_value;
3876                      end if;
3877                      create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_4_NAME',l_4_name);
3878                      create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_4_VALUE',l_4_value);
3879                   end if;
3880              end if;
3881 
3882              if p_fw_union_tab_sorted.count >0 then  -- to adjust and sort normal Union values in archive_2006_unions
3883                 u_idx := p_fw_union_tab_sorted.last + 1;
3884                 for i in t_union_bal_sort.first .. t_union_bal_sort.last loop
3885                     p_fw_union_tab_sorted(u_idx).balance_name := t_union_bal_sort(i).balance_name;
3886                     p_fw_union_tab_sorted(u_idx).balance_value := t_union_bal_sort(i).balance_value;
3887                     u_idx := u_idx + 1;
3888                  end loop;
3889               else
3890                  for i in t_union_bal_sort.first .. t_union_bal_sort.last loop
3891                    p_fw_union_tab_sorted(i).balance_name := t_union_bal_sort(i).balance_name;
3892                    p_fw_union_tab_sorted(i).balance_value := t_union_bal_sort(i).balance_value;
3893                  end loop;
3894                end if;
3895 
3896        else
3897 
3898          if l_reporting_name = 'MISCELLANEOUS' then
3899             l_reporting_name := 'Miscellaneous';
3900          end if;
3901 
3902           create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_1_NAME',l_reporting_name);
3903           create_extract_archive_details(p_assignment_action_id,'X_FW'||i_idx||'_UNION_1_VALUE',l_fw_output_tab(15).balance_value);
3904 
3905         end if;
3906       end if;
3907    end if;
3908 
3909      if g_debug then
3910           hr_utility.set_location('FW Union ending ' || l_procedure, 22);
3911      end if;
3912 
3913   END IF;  --  End FW Union
3914 
3915 ELSE  -- p_fw_alw_bal_exist <> 'N' when Allowance balances exist
3916 
3917      if g_debug then
3918           hr_utility.set_location('FW Allowance starting ' || l_procedure, 30);
3919      end if;
3920 
3921       l_alw_total := 0;  -- bug#10084818  initializing
3922       l_fw_alw_output_tab.delete;
3923 
3924       IF t_fw_gross_type(i_idx) = 'F' THEN
3925         pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
3926                                                                 p_registered_employer,
3927                                                                 f_fw_date_tab_g,
3928                                                                 p_year_start,
3929                                                                 p_year_end,
3930                                                                 pay_au_foreign_workers.g_fw_f_type,
3931                                                                 p_fw_alw_balance_type_tab,
3932                                                                 l_fw_alw_output_tab) ;
3933       ELSIF t_fw_gross_type(i_idx) = 'J' THEN
3934         pay_au_foreign_workers.get_foreign_payment_amounts(p_assignment_id,
3935                                                                 p_registered_employer,
3936                                                                 j_fw_date_tab_g,
3937                                                                 p_year_start,
3938                                                                 p_year_end,
3939                                                                 pay_au_foreign_workers.g_fw_j_type,
3940                                                                 p_fw_alw_balance_type_tab,
3941                                                                 l_fw_alw_output_tab) ;
3942       END IF;
3943 
3944       if l_fw_alw_output_tab.count > 0 then
3945 
3946            p_fw_allowance_tab.delete;
3947            t_alw_bal_sort.delete;
3948            p_fw_alw_tab.delete;
3949            p_fw_alw_limit_tab.delete;
3950 
3951            cnt := 1;
3952            for i in l_fw_alw_output_tab.first .. l_fw_alw_output_tab.last loop
3953                 if l_fw_alw_output_tab(i).balance_value > 0 then
3954                    for alw_rec in get_balance_name(l_fw_alw_output_tab(i).defined_balance_id) loop
3955                         p_fw_allowance_tab(cnt).balance_name := alw_rec.balance_name;
3956                         p_fw_allowance_tab(cnt).balance_value := l_fw_alw_output_tab(i).balance_value;
3957                         cnt := cnt + 1;
3958                      end loop;
3959                   end if;
3960             end loop;
3961 
3962             if p_fw_allowance_tab.count >0 then
3963 /*
3964                 if g_debug then
3965                   hr_utility.trace('FW Allowance before Adjustment');
3966                   for i in p_fw_allowance_tab.first .. p_fw_allowance_tab.last loop
3967                       hr_utility.trace(p_fw_allowance_tab(i).balance_name||' $'||p_fw_allowance_tab(i).balance_value);
3968                     end loop;
3969                  end if;
3970 */
3971                  IF t_fw_gross_type(i_idx) = 'F' THEN
3972                       for i in f_fw_date_tab_g.FIRST .. f_fw_date_tab_g.LAST loop
3973                                            x := adjust_retro_allowances
3974                                                  (p_fw_allowance_tab
3975                                                  ,f_fw_date_tab_g(i).start_date
3976                                                  ,f_fw_date_tab_g(i).end_date
3977                                                  ,p_assignment_id
3978                                                  ,p_registered_employer
3979                                                  );
3980                        end loop;
3981                   ELSIF t_fw_gross_type(i_idx) = 'J' THEN
3982                        for i in j_fw_date_tab_g.FIRST ..j_fw_date_tab_g.LAST loop
3983                                            x := adjust_retro_allowances
3984                                                  (p_fw_allowance_tab
3985                                                  ,j_fw_date_tab_g(i).start_date
3986                                                  ,j_fw_date_tab_g(i).end_date
3987                                                  ,p_assignment_id
3988                                                  ,p_registered_employer
3989                                                  );
3990                         end loop;
3991                     END IF;
3992 /*
3993                     if g_debug then
3994                        hr_utility.trace('FW Allowance after Adjustment');
3995                        for i in p_fw_allowance_tab.first .. p_fw_allowance_tab.last loop
3996                               hr_utility.trace(p_fw_allowance_tab(i).balance_name||' $'||p_fw_allowance_tab(i).balance_value);
3997                        end loop;
3998                     end if;
3999 */
4000                     idx := p_fw_allowance_tab.first;
4001                     loop
4002                     exit when idx is null;
4003                        t_alw_bal_sort.extend;
4004                        t_alw_bal_sort(t_alw_bal_sort.last) := rec_au_eoy_bal_type(p_fw_allowance_tab(idx).balance_name
4005                                                                                                          , p_fw_allowance_tab(idx).balance_value);
4006                         idx := p_fw_allowance_tab.next(idx);
4007                     end loop;
4008 
4009                     SELECT CAST (MULTISET (SELECT rec_au_eoy_bal_type(balance_name, balance_value)
4010                                                             FROM   TABLE(t_alw_bal_sort)
4011                                                            ORDER BY balance_value desc)
4012                                           AS tab_au_eoy_bal_type)
4013                      INTO t_alw_bal_sort
4014                      FROM dual;
4015 
4016 
4017                       for i in t_alw_bal_sort.first .. t_alw_bal_sort.last loop
4018                          p_fw_alw_tab(i).balance_name := t_alw_bal_sort(i).balance_name;
4019                          p_fw_alw_tab(i).balance_value := t_alw_bal_sort(i).balance_value;
4020                        end loop;
4021 
4022                        if g_debug then
4023                           hr_utility.trace('FW Allowance after Sorting');
4024                           for i in p_fw_alw_tab.first .. p_fw_alw_tab.last loop
4025                                hr_utility.trace(p_fw_alw_tab(i).balance_name||' $'||p_fw_alw_tab(i).balance_value);
4026                            end loop;
4027                         end if;
4028 
4029 
4030                         idx := p_fw_alw_tab.first;
4031 
4032                         if p_fw_alw_tab.count > 30 then
4033                             for i in 30 .. p_fw_alw_tab.last loop
4034                                 l_alw_total := l_alw_total + p_fw_alw_tab(i).balance_value;
4035                              end loop;
4036                               p_fw_alw_tab_concat(30) := 'Miscellaneous $'||l_alw_total ;
4037 
4038                             for i in idx .. 29 loop
4039                                     p_fw_alw_tab_concat(i) := p_fw_alw_tab(i).balance_name||' $'||trunc(p_fw_alw_tab(i).balance_value)||' ';
4040                             end loop;
4041                         else
4042                             for i in idx .. 30 loop
4043                                if p_fw_alw_tab.exists(i) then
4044                                     p_fw_alw_tab_concat(i) := p_fw_alw_tab(i).balance_name||' $'||trunc(p_fw_alw_tab(i).balance_value)||' ';
4045                                 else
4046                                     p_fw_alw_tab_concat(i) := '';
4047                                 end if;
4048                             end loop;
4049                         end if;
4050 
4051                         p_fw_alw_tab_concat_new(1) := p_fw_alw_tab_concat(1)||p_fw_alw_tab_concat(2)||p_fw_alw_tab_concat(3)||
4052                                                                         p_fw_alw_tab_concat(4)||p_fw_alw_tab_concat(5)||p_fw_alw_tab_concat(6);
4053                         p_fw_alw_tab_concat_new(2) := p_fw_alw_tab_concat(7)||p_fw_alw_tab_concat(8)||p_fw_alw_tab_concat(9)||
4054                                                                         p_fw_alw_tab_concat(10)||p_fw_alw_tab_concat(11)||p_fw_alw_tab_concat(12);
4055                         p_fw_alw_tab_concat_new(3) := p_fw_alw_tab_concat(13)||p_fw_alw_tab_concat(14)||p_fw_alw_tab_concat(15)||
4056                                                                         p_fw_alw_tab_concat(16)||p_fw_alw_tab_concat(17)||p_fw_alw_tab_concat(18);
4057                         p_fw_alw_tab_concat_new(4) := p_fw_alw_tab_concat(19)||p_fw_alw_tab_concat(20)||p_fw_alw_tab_concat(21)||
4058                                                                         p_fw_alw_tab_concat(22)||p_fw_alw_tab_concat(23)||p_fw_alw_tab_concat(24);
4059                         p_fw_alw_tab_concat_new(5) := p_fw_alw_tab_concat(25)||p_fw_alw_tab_concat(26)||p_fw_alw_tab_concat(27)||
4060                                                                         p_fw_alw_tab_concat(28)||p_fw_alw_tab_concat(29)||p_fw_alw_tab_concat(30);
4061 
4062 
4063                         for i in 1 .. 5 loop
4064                             create_extract_archive_details(p_assignment_action_id
4065                                                        ,'X_FW'||i_idx||'_ALLOWANCE_CONCAT'||i
4066                                                        ,p_fw_alw_tab_concat_new(i));
4067                         end loop;
4068 /*
4069                         archive_limited_values
4070                                             (p_assignment_action_id => p_assignment_action_id
4071                                             ,p_table                => p_fw_alw_tab
4072                                             ,p_limit                => 30
4073                                             ,p_name_prefix          => 'X_FW'||i_idx||'_ALLOWANCE_'
4074                                             ,p_name_suffix          => '_NAME'
4075                                             ,p_value_prefix         => 'X_FW'||i_idx||'_ALLOWANCE_'
4076                                             ,p_value_suffix         => '_VALUE'
4077                                             ,p_total_name           => 'Miscellaneous'
4078                                             );
4079 */
4080                           p_fw_alw_limit_tab := p_fw_alw_tab;
4081                           archive_limited_values
4082                                           (p_assignment_action_id   => p_assignment_action_id
4083                                           ,p_table                  => p_fw_alw_limit_tab
4084                                           ,p_limit                  => 4
4085                                           ,p_name_prefix            => 'X_FW'||i_idx||'_ALLOWANCE_NAME_'
4086                                           ,p_name_suffix            => ''
4087                                           ,p_value_prefix           => 'X_FW'||i_idx||'_ALLOWANCE_'
4088                                           ,p_value_suffix           => '_ASG_YTD'
4089                                           ,p_total_name             => 'Various'
4090                                           );
4091 
4092                            if p_fw_alw_tab_sorted.count >0 then  -- to adjust and sort normal Allowance values in archive_allowance_details
4093                                  a_idx := p_fw_alw_tab_sorted.last + 1;
4094                                 for i in p_fw_alw_tab.first .. p_fw_alw_tab.last loop
4095                                       p_fw_alw_tab_sorted(a_idx).balance_name := p_fw_alw_tab(i).balance_name;
4096                                       p_fw_alw_tab_sorted(a_idx).balance_value := p_fw_alw_tab(i).balance_value;
4097                                       a_idx := a_idx + 1;
4098                                   end loop;
4099                              else
4100                                   for i in p_fw_alw_tab.first .. p_fw_alw_tab.last loop
4101                                         p_fw_alw_tab_sorted(i).balance_name := p_fw_alw_tab(i).balance_name;
4102                                         p_fw_alw_tab_sorted(i).balance_value := p_fw_alw_tab(i).balance_value;
4103                                    end loop;
4104                             end if;
4105               end if;
4106          end if;
4107      if g_debug then
4108           hr_utility.set_location('FW Allowance ending ' || l_procedure, 32);
4109      end if;
4110 
4111   END IF;
4112 
4113 END LOOP;
4114 
4115 ELSE  -- p_fw_check <> 'Y' : Cases for FBT exists before p_year_start
4116 
4117           if g_fw_fbt_balance > 0 then
4118                      l_fw_date := to_char(p_year_start,'DDMMYYYY');
4119                      create_extract_archive_details(p_assignment_action_id
4120                                                    ,'X_FW1_START_DATE'
4121                                                    ,l_fw_date);
4122 
4123                       l_fw_date := to_char(p_year_start,'DDMMYYYY');
4124                      create_extract_archive_details(p_assignment_action_id
4125                                                    ,'X_FW1_END_DATE'
4126                                                    ,l_fw_date);
4127           end if;
4128 
4129 END IF;
4130 
4131 
4132 
4133 IF p_fw_alw_bal_exist = 'N' THEN
4134 
4135   l_fw_tot_net_balance := l_fw_tot_net_balance + g_fw_fbt_balance ;
4136   l_fw_tot_sub_balance := l_fw_tot_sub_balance + g_fw_fbt_balance ;
4137 
4138 if g_debug then
4139      hr_utility.set_location(' g_net_balance '||g_net_balance,41);
4140      hr_utility.set_location(' l_fw_tot_net_balance '||l_fw_tot_net_balance,41);
4141      hr_utility.set_location(' l_fw_tot_sub_balance '||l_fw_tot_sub_balance,41);
4142      hr_utility.set_location(' g_fw_fbt_balance '||g_fw_fbt_balance,41);
4143      hr_utility.set_location(' g_fw_union_value '||g_fw_union_value,41);
4144 end if;
4145 
4146     if l_fw_tot_net_balance <> 0 then
4147           if (g_net_balance - l_fw_tot_net_balance) <> 0 then
4148                 if l_fw_tot_net_balance - l_fw_tot_sub_balance = 0 then  -- PAYG Payment Summary only
4149                      l_payg_fw_type := 'P';
4150                 else
4151                      l_payg_fw_type := 'PF';
4152                 end if;
4153           else
4154                      l_payg_fw_type := 'F';  -- FW Net Balance only (g_net_balance - l_fw_net_balance = 0)
4155           end if;
4156     elsif g_fw_fbt_balance <> 0 then  -- g_fw_fbt_term_check or g_fw_fbt_check = 'Y' case
4157                if g_net_balance - g_fw_fbt_balance <> 0 then
4158                      l_payg_fw_type := 'P';
4159                else
4160                      l_payg_fw_type := 'F';
4161                end if;
4162     end if;
4163 
4164            create_extract_archive_details(p_assignment_action_id
4165                                        ,'X_FW_PAYG_TYPE'
4166                                        ,l_payg_fw_type);
4167 
4168 END IF;
4169 
4170      if g_debug then
4171           hr_utility.set_location('Leaving ' || l_procedure, 99);
4172      end if;
4173 
4174 exception
4175   when others then
4176     IF g_debug THEN
4177     hr_utility.set_location('archive_fw_details : Excpetions raised  ',88);
4178     END if;
4179   raise;
4180 
4181 end archive_fw_details;
4182 
4183 --
4184 ----------------------------------------------------------------------------
4185 --* Archive Allowance details
4186 -----------------------------
4187 
4188 procedure archive_allowance_details
4189 (p_assignment_action_id     in   pay_assignment_actions.assignment_action_id%type
4190 ,p_max_assignment_action_id in   pay_assignment_actions.assignment_action_id%type
4191 ,p_registered_employer      in   number
4192 ,p_year_start               in   date
4193 ,p_year_end                 in   date
4194 ,p_assignment_id            in   pay_assignment_actions.assignment_id%type
4195 ,p_alw_bal_exist            out  nocopy varchar2                                   -- 3098353
4196 ) IS
4197   l_procedure               constant varchar2(80) := g_package||'.archive_allowance_details';
4198 
4199 /* bug 7571001 - Modified get_allowance_balances cursor to return as per balance attribute
4200 */
4201 
4202 CURSOR get_allowance_balances IS
4203 SELECT  pdb.defined_balance_id
4204        ,NVL(pbt.reporting_name,pbt.balance_name) balance_name
4205        ,pay_balance_pkg.get_value(pdb.defined_balance_id
4206                                   ,p_max_assignment_action_id
4207                                   ,p_registered_employer
4208                                   ,NULL,NULL,NULL,NULL,NULL,NULL,NULL) balance_value
4209 FROM  pay_balance_attributes pba
4210      ,pay_defined_balances   pdb
4211      ,pay_balance_types      pbt
4212      ,pay_balance_dimensions pbd
4213 WHERE pba.attribute_id         = g_attribute_id
4214 AND   pdb.defined_balance_id   = pba.defined_balance_id
4215 AND   pbt.balance_type_id      = pdb.balance_type_id
4216 AND   pdb.balance_type_id = pbt.balance_type_id
4217 AND pdb.business_group_id = g_business_group_id
4218 and   pbd.balance_dimension_id = pdb.balance_dimension_id
4219 and   pbd.dimension_name = '_ASG_LE_YTD'
4220 ORDER BY 3 DESC
4221 ;
4222 
4223   counter           number := 1;
4224   i                 number;
4225   t_allowance_2006  tab_allownace_balance;
4226   idx number; /*bug9147430*/
4227   t_alw_bal_sort tab_au_eoy_bal_type := tab_au_eoy_bal_type(); /*bug9147430*/
4228   --
4229 begin
4230   if g_debug then
4231     hr_utility.set_location(l_procedure, 1);
4232     hr_utility.trace('p_assignment_action_id.....= ' || p_assignment_action_id);
4233     hr_utility.trace('p_max_assignment_action_id.= ' || p_max_assignment_action_id);
4234     hr_utility.trace('p_registered_employer......= ' || p_registered_employer);
4235     hr_utility.trace('p_year_start...............= ' || p_year_start);
4236     hr_utility.trace('p_year_end.................= ' || p_year_end);
4237     hr_utility.trace('p_assignment_id............= ' || p_assignment_id);
4238   end if;
4239   --
4240   -----------------------------------------------------------------------------------------
4241   --  Archive the Allowances
4242   -----------------------------------------------------------------------------------------
4243   --
4244   -- Archive up to 4 allowances , store the value and name of the balance for the employees
4245   -- If the employee has more than 4 allowances than store name and value of first
4246   -- 3 allowances based on the value of balance in descending order.
4247   -- Then calculate the sum of the remaining balances and store name as 'Various'
4248   -- /* Bug#4925547 */
4249   -- To support 2006/2007 Payment Summary we need to archive the new 30 allowance names and
4250   -- values as well as continuing to archive the original 4 items mentioned above
4251   --
4252   -----------------------------------------------------------------------------------------
4253   --
4254 /* start bug 7571001*/
4255   for rec_allowance_balances in get_allowance_balances loop
4256 
4257       if nvl(rec_allowance_balances.balance_value,0) >0 then
4258        t_allowance_balance(counter).balance_name  := rec_allowance_balances.balance_name;
4259        t_allowance_balance(counter).balance_value := rec_allowance_balances.balance_value;
4260 
4261         if g_debug then
4262           hr_utility.set_location(l_procedure, 3);
4263           hr_utility.trace('t_allowance_ balance name ('||counter||') = '|| t_allowance_balance(counter).balance_name);
4264           hr_utility.trace('t_allowance_balance value ('||counter||') = '||t_allowance_balance(counter).balance_value);
4265         end if;
4266 
4267         counter := counter +1;
4268 
4269       end if;
4270   end loop;
4271 /* end bug 7571001*/
4272   --
4273   -- 2855658
4274   --
4275   i := adjust_retro_allowances
4276        (t_allowance_balance
4277        ,p_year_start
4278        ,p_year_end
4279        ,p_assignment_id
4280        ,p_registered_employer --2610141
4281        );
4282 
4283 /* bug9147430 - archive_fw_details procedure is called to adjust normal Allowances by reducing FW allowances */
4284 if (to_number(to_char(p_year_start,'YYYY')) >= 2010) and t_allowance_balance.count > 0 and g_fw_check = 'Y' then
4285 
4286     if g_debug then hr_utility.set_location('Starting Allowances adjustment by FW Allowances',100); end if;
4287 
4288      archive_fw_details
4289        (p_assignment_id
4290        ,p_registered_employer
4291        ,p_assignment_action_id
4292        ,p_year_start
4293        ,p_year_end
4294        ,g_fw_check
4295        ,null
4296        ,'Y');
4297 
4298     if p_fw_alw_tab_sorted.count >0 then
4299          for i in t_allowance_balance.first .. t_allowance_balance.last loop
4300                for j in p_fw_alw_tab_sorted.first .. p_fw_alw_tab_sorted.last loop
4301                     if t_allowance_balance(i).balance_name = p_fw_alw_tab_sorted(j).balance_name then
4302                          t_allowance_balance(i).balance_value := t_allowance_balance(i).balance_value - p_fw_alw_tab_sorted(j).balance_value;
4303                     end if;
4304                end loop;
4305          end loop;
4306 
4307          -- Reordering
4308 
4309         if g_debug then
4310            hr_utility.trace('After FW Allowance Adjustment');
4311            for i in t_allowance_balance.first .. t_allowance_balance.last loop
4312                hr_utility.trace(t_allowance_balance(i).balance_name||' $'||t_allowance_balance(i).balance_value);
4313             end loop;
4314         end if;
4315 
4316         idx := t_allowance_balance.first;
4317         loop
4318        exit when idx is null;
4319          t_alw_bal_sort.extend;
4320          t_alw_bal_sort(t_alw_bal_sort.last) := rec_au_eoy_bal_type(t_allowance_balance(idx).balance_name
4321                                                                                             , t_allowance_balance(idx).balance_value);
4322          idx := t_allowance_balance.next(idx);
4323        end loop;
4324 
4325        SELECT CAST (MULTISET (SELECT rec_au_eoy_bal_type(balance_name, balance_value)
4326                                                FROM   TABLE(t_alw_bal_sort)
4327                                                WHERE balance_value > 0
4328                                                ORDER BY balance_value desc)
4329                              AS tab_au_eoy_bal_type)
4330         INTO t_alw_bal_sort
4331         FROM dual;
4332 
4333          hr_utility.trace('After FW Allowance Sorting');
4334          if t_alw_bal_sort.count >0 then  -- bug10048508
4335            t_allowance_balance.delete;
4336            for i in t_alw_bal_sort.first .. t_alw_bal_sort.last loop
4337               t_allowance_balance(i).balance_name := t_alw_bal_sort(i).balance_name;
4338               t_allowance_balance(i).balance_value := t_alw_bal_sort(i).balance_value;
4339 
4340               if g_debug then
4341                 hr_utility.trace(t_alw_bal_sort(i).balance_name||' $'||t_alw_bal_sort(i).balance_value);
4342               end if;
4343            end loop;
4344           end if;
4345     end if;
4346 
4347     if g_debug then hr_utility.set_location('Ending Allowances adjustment by FW Allowances',199); end if;
4348 
4349  end if;
4350 
4351   --
4352   -- Copy the table as we need to manipulate and archive twice
4353   -- to support the pre 2006 and post archives.
4354   --
4355   t_allowance_2006 := t_allowance_balance;
4356   --
4357   -- /* Bug#4925547 */
4358   -- Always archive the previous method for allowances
4359   -- as these are used in other processes as well as form
4360   -- the total value for allowances in the payment summary
4361   --
4362   --
4363   -- If more than 4 allowances exists, calculate sum of all
4364   -- and assign the sum to 4th balance with name 'Various'
4365   --
4366   archive_limited_values
4367   (p_assignment_action_id   => p_assignment_action_id
4368   ,p_table                  => t_allowance_balance
4369   ,p_limit                  => 4
4370   ,p_name_prefix            => 'X_ALLOWANCE_NAME_'
4371   ,p_name_suffix            => ''
4372   ,p_value_prefix           => 'X_ALLOWANCE_'
4373   ,p_value_suffix           => '_ASG_YTD'
4374   ,p_total_name             => 'Various'
4375   );
4376   --/* Bug#4925547 */
4377   -- Now if the financial year is 2006/2007 or above
4378   -- then we perform the additional archive.
4379   --
4380   if to_number(to_char(p_year_start,'YYYY')) >= 2006 then
4381     if g_debug then
4382       hr_utility.set_location(l_procedure, 30);
4383     end if;
4384     --
4385     archive_limited_values
4386     (p_assignment_action_id => p_assignment_action_id
4387     ,p_table                => t_allowance_2006
4388     ,p_limit                => 30
4389     ,p_name_prefix          => 'X_ALLOWANCE_'
4390     ,p_name_suffix          => '_NAME'
4391     ,p_value_prefix         => 'X_ALLOWANCE_'
4392     ,p_value_suffix         => '_VALUE'
4393     ,p_total_name           => 'Miscellaneous'
4394     );
4395   end if;
4396   --
4397   -- 3098353:- Checks if the Allowance exist in the current year.
4398   --
4399   if t_allowance_balance.count > 0 then
4400     p_alw_bal_exist := 'TRUE';
4401   else
4402     p_alw_bal_exist := 'FALSE';
4403   end if;
4404   ---------------------------------------------------------
4405   t_allowance_balance.delete; /* 2968127- cleared PL/SQL table */
4406   if g_debug then
4407     hr_utility.set_location(l_procedure,999);
4408   end if;
4409 exception
4410   when others then
4411     if g_debug then
4412       hr_utility.set_location(l_procedure,000);
4413     end if;
4414     raise;
4415 end  archive_allowance_details;
4416 --
4417 -----------------------------------------------------------
4418 -- in case for a terminated employee payroll has been run
4419 -- but prepayments have not been made then this is an exception
4420 -- condition in the validation report. This procedure
4421 -- archives X_ETP_ON_TERMNATION_PAID and
4422 -- X_ETP_EMPLOYEE_PAYMENT_DATE
4423 -----------------------------------------------------------
4424 
4425 procedure archive_etp_payment_details
4426     ( p_assignment_action_id pay_assignment_actions.assignment_action_id%type,
4427       p_registered_employer  NUMBER, --2610141
4428       p_assignment_id        per_all_Assignments_f.assignment_id%type ,
4429       p_year_start           date,
4430       p_year_end             date ) as
4431 
4432 
4433 cursor  etp_paid(c_assignment_id per_all_Assignments_f.assignment_id%type,
4434                  c_year_start    date ,
4435                  c_year_end      date )  is
4436 
4437 select  prv.result_value
4438        ,ppa.payroll_action_id
4439        ,pac.assignment_action_id
4440        ,ppa.effective_date
4441 from    pay_element_types_f    pet
4442        ,pay_input_values_f     piv
4443        ,per_all_assignments_f  paa
4444        ,pay_run_results        prr
4445        ,pay_run_result_values  prv
4446        ,pay_assignment_actions pac
4447        ,pay_payroll_actions    ppa
4448        ,pay_payrolls_f   papf         /* bug Number 4278361 */
4449 where   pet.element_type_id      = piv.element_type_id
4450   and   pet.element_name         = 'ETP on Termination'
4451   and   piv.name                 = 'Pay ETP Components'
4452   and   paa.assignment_id        = c_assignment_id
4453   and   prv.input_value_id       = piv.input_value_id
4454   and   prr.element_type_id      = pet.element_type_id
4455   and   prr.run_result_id        = prv.run_result_id
4456   and   prr.assignment_action_id = pac.assignment_action_id
4457   and   pac.assignment_id        = paa.assignment_id
4458   and   pac.payroll_action_id    = ppa.payroll_action_id
4459   and   paa.effective_start_date between pet.effective_start_date
4460                                      and pet.effective_end_date
4461   and   paa.effective_start_date between piv.effective_start_date
4462                                      and piv.effective_end_date
4463    and papf.payroll_id=paa.payroll_id
4464    and ppa.payroll_id=papf.payroll_id
4465    and    ppa.action_type             in ('R','Q','I','B','V')    /* bug Number 4278361 */
4466   and ppa.effective_date between papf.effective_start_date and papf.effective_end_date
4467   and   ppa.effective_date       between paa.effective_start_date
4468                                      and paa.effective_end_date
4469   and   ppa.effective_date between c_year_start
4470                                and c_year_end
4471   and   pac.tax_unit_id = p_registered_employer; --2610141
4472 
4473 cursor  etp_prepayment
4474           (c_assignment_action_id pay_assignment_actions.assignment_action_id%type,
4475            c_payroll_action_id    pay_payroll_actions.payroll_action_id%type,
4476            c_year_start           date,
4477            c_year_end             date )
4478     is  select  to_char(pppa.effective_date,'DDMMYYYY')
4479          from   pay_action_interlocks    pai
4480                ,pay_assignment_actions   pac
4481                ,pay_payroll_actions      ppa
4482                ,pay_assignment_actions   ppac
4483                ,pay_payroll_actions      pppa
4484         where   pac.payroll_action_id    = ppa.payroll_action_id
4485           and   pac.assignment_action_id = c_assignment_action_id
4486           and   pac.assignment_action_id = pai.locked_action_id
4487           and   ppa.payroll_action_id    = c_payroll_action_id
4488           and   ppac.assignment_action_id =pai.locking_Action_id
4489           and   pppa.payroll_Action_id   = ppac.payroll_Action_id
4490           and   ppa.effective_date      between c_year_start
4491                                             and c_year_end;
4492   r_etp_paid etp_paid%rowtype;
4493 
4494 -- cursor to get payroll and assignment actions when termination payments are done
4495 -- through balance adjustment/balance initialization(Bug 2574186)
4496 
4497 /* Bug No: 3603495 - Performance Fix - Modified the following cursor by introducing per_assignments_f table and its joins */
4498 
4499 cursor etp_BA_or_BI
4500          (c_assignment_id per_all_assignments_f.assignment_id%type,
4501           c_year_start    date ,
4502           c_year_end      date )
4503    is  select  max(ppa.payroll_action_id) payroll_action_id
4504               ,max(pac.assignment_action_id) assignment_action_id
4505         from   per_assignments_f      paf
4506               ,pay_assignment_actions pac
4507               ,pay_payroll_actions    ppa
4508        where   pac.assignment_id     = c_assignment_id
4509          and   pac.tax_unit_id = p_registered_employer --2610141
4510          and   paf.assignment_id     = pac.assignment_id
4511          and   ppa.action_type       in ('B','I')
4512          and   pac.payroll_action_id = ppa.payroll_action_id
4513          and   pac.action_status     = 'C'
4514          and   ppa.action_status     = 'C'
4515          and   ppa.payroll_id = paf.payroll_id /* Added for bug 5371102 for performance*/
4516          and   ppa.date_earned between paf.effective_start_date and paf.effective_end_date /* Added for bug 5371102 for performance*/
4517          and  (pay_balance_pkg.get_value(pkg_lump_sum_c_def_bal_id, pac.assignment_action_id,p_registered_employer,null,null,null,null)) > 0 --2610141
4518          and   ppa.effective_date between c_year_start
4519                                       and c_year_end ;
4520 
4521   r_etp_ba_or_bi etp_BA_or_BI%rowtype;
4522 
4523   -- Bug No : 2574186 initialize the l_etp_paid as 'N', So that the dbi
4524   -- X_ETP_ON_TERMINATION_PAID is archived as 'N' incase the Lump C Payemnt
4525   -- exists and prepayment is not not run
4526   l_etp_paid        varchar2(1);
4527   l_etp_paid_date   varchar2(20);
4528 
4529 -- Bug# 12584258 for handling NULL value in ETP_Payment_Date -- START
4530 cursor  act_term_date
4531           (c_assignment_id per_all_assignments_f.assignment_id%type,
4532           c_year_start    date ,
4533           c_year_end      date )
4534     is  select  to_char(pps.actual_termination_date,'DDMMYYYY')
4535          from   per_all_assignments_f paa,
4536                 per_periods_of_service pps
4537         where   paa.person_id = pps.person_id
4538           and   paa.period_of_service_id = pps.period_of_service_id
4539           and   paa.assignment_id = c_assignment_id
4540           and   pps.actual_termination_date between paa.effective_start_date
4541                                                 and paa.effective_end_date
4542           and   pps.actual_termination_date between c_year_start
4543                                                 and c_year_end ;
4544 -- Bug# 12584258 -- END
4545 
4546 begin
4547 
4548   l_etp_paid := 'N';
4549 
4550   open etp_paid(p_assignment_id,p_year_start,p_year_end);
4551   fetch etp_paid into r_etp_paid ;
4552 
4553   if (etp_paid%found and r_etp_paid.result_value = 'Y') then
4554       l_etp_paid := 'Y';
4555 
4556     open etp_prepayment(r_etp_paid.assignment_action_id,r_etp_paid.payroll_action_id,p_year_start,p_year_end);
4557     fetch etp_prepayment into l_etp_paid_date;
4558 
4559     if (etp_prepayment%notfound) then
4560       l_etp_paid := 'N';
4561     end if;
4562 
4563     close etp_prepayment;
4564 
4565   else
4566     -- Bug 2574186 - Check if the prepayment is run in case of BA/BI
4567     open etp_BA_or_BI(p_assignment_id,p_year_start,p_year_end);
4568     fetch etp_BA_or_BI into r_etp_ba_or_bi ;
4569     if etp_BA_or_BI%found then
4570        l_etp_paid := 'Y';
4571        open etp_prepayment(r_etp_ba_or_bi.assignment_action_id,r_etp_ba_or_bi.payroll_action_id,p_year_start,p_year_end);
4572        fetch etp_prepayment into l_etp_paid_date;
4573        if (etp_prepayment%notfound) then
4574          l_etp_paid := 'N';
4575        end if;
4576        close etp_prepayment;
4577     end if;
4578     close etp_BA_or_BI;
4579 -- Bug# 12584258 for handling NULL value in ETP_Payment_Date -- START
4580    if l_etp_paid_date is NULL then
4581     open act_term_date(p_assignment_id,p_year_start,p_year_end);
4582     fetch act_term_date into l_etp_paid_date;
4583      if (act_term_date%notfound) then
4584       l_etp_paid_date := to_char(p_year_end,'DDMMYYYY');
4585      end if;
4586      l_etp_paid := 'Y';  /* Added for Bug# 12625929 */
4587     close act_term_date;  -- Bug# 12584258  END--
4588    end if;
4589   end if;
4590   close etp_paid;
4591 
4592 
4593   ---------------------------------------
4594   -- create the archive items
4595   ---------------------------------------
4596 
4597   create_extract_archive_details(p_assignment_action_id,
4598                                 'X_ETP_ON_TERMINATION_PAID',
4599                                  l_etp_paid );
4600   create_extract_archive_details(p_assignment_action_id,
4601                                 'X_ETP_EMPLOYEE_PAYMENT_DATE',
4602                                  l_etp_paid_date);
4603 exception
4604   when others then
4605     IF g_debug THEN
4606     hr_utility.set_location('Error in archive_etp_payment_details ',99);
4607     END if;
4608     Raise;
4609 end archive_etp_payment_details ;
4610 --
4611 -- /* Bug#4925547 */
4612 -- As of 2006/2007 financial year we provide an alternative solution to archiving
4613 -- union fees.
4614 --
4615 -- As of 2006/2007 there is a new method of setting up union fees
4616 --------------------------------------------------------------------
4617 -- This method involves entering an associated balance into a flex
4618 -- segment of the Element Developer DF.
4619 -- The archive must continue to use the old method (mentioned above)
4620 -- and in addition to this, if the financial year is 2006/2007 or
4621 -- above then also archive for the new method.
4622 --------------------------------------------------------------------
4623 --
4624 -- bug9147430 :
4625 -- when the logic is changed, archive_fw_details should be modified together.
4626 procedure archive_2006_unions
4627 (p_assignment_id              in  per_all_assignments.assignment_id%type
4628 ,p_assignment_action_id       in  pay_assignment_actions.assignment_action_id%type
4629 ,p_max_assignment_action_id   in  pay_assignment_actions.assignment_action_id%type
4630 ,p_registered_employer        in  number
4631 ,p_year_start                 in  date
4632 ,p_year_end                   in  date
4633 --,p_alw_bal_exist              out nocopy varchar2
4634 ) as
4635 
4636   --
4637   -- This cursor returns the balance value for union elements.  Element that are identified as a 'Union' fee element
4638   -- will have segment 1 of the Element Developer flex linking the element to a specific balance
4639   -- in conjunction with feeding the legislative balance called 'Union Fees'
4640   --  Bug#5591993 Removed join for table pay_balance_type to avoid MJC
4641   --              and moved the code to cursor c_union_balance(initliazation code).
4642   cursor csr_unions_2006 (c_balance_type pay_balance_types.balance_type_id%type) is
4643   select distinct nvl(pbt.reporting_name, pbt.balance_name) balance_name
4644   ,      pay_balance_pkg.get_value(pdb.defined_balance_id, p_max_assignment_action_id, p_registered_employer,null,null,null,null)   balance_value
4645   ,      pdb.defined_balance_id                            def_id
4646   ,      pbt.balance_type_id                               bal_type_id
4647   from   pay_element_types_f         pet
4648   ,      per_all_assignments_f       paa
4649   ,      pay_balance_types           pbt
4650   ,      pay_defined_balances        pdb
4651   ,      pay_payroll_actions         ppa
4652   ,      pay_assignment_actions      pac
4653   ,      pay_run_results             prr
4654   ,      pay_balance_feeds_f         pbf
4655   ,      pay_input_values_f          piv
4656   where  pac.assignment_id               = p_assignment_id
4657   and    pac.tax_unit_id                 = p_registered_employer
4658   and    paa.assignment_id               = pac.assignment_id
4659   and    paa.assignment_id               = p_assignment_id
4660   and    pac.payroll_action_id           = ppa.payroll_Action_id
4661   and    ppa.effective_date              between p_year_start and p_year_end
4662   and    ppa.payroll_id                  = paa.payroll_id
4663   and    ppa.action_type                 in ('Q','R','B','I','V')
4664   and    pac.assignment_action_id        = prr.assignment_action_id
4665   and    prr.element_type_id             = pet.element_type_id
4666   and    pet.element_information_category = 'AU_VOLUNTARY DEDUCTIONS'
4667   and    pet.element_information1        = pbt.balance_type_id
4668   and    pbt.balance_type_id             = pdb.balance_type_id(+)
4669   and    pdb.balance_dimension_id(+)     = g_dimension_id
4670   and    ppa.effective_date              between paa.effective_start_date and paa.effective_end_date
4671   and    ppa.date_earned                 between pet.effective_start_date and pet.effective_end_date
4672   and    pet.element_type_id             = piv.element_type_id
4673   and    prr.element_type_id             = piv.element_type_id
4674   and    ppa.date_earned                 between piv.effective_start_date and piv.effective_end_date
4675   and    piv.input_value_id              = pbf.input_value_id
4676   and    ppa.date_earned                 between pbf.effective_start_date and pbf.effective_end_date
4677   and    pbf.balance_type_id             = c_balance_type
4678   order by 2 desc ;
4679   --
4680   l_procedure   constant varchar2(80) := g_package || '.archive_2006_unions';
4681   l_counter     number := 1;
4682   l_4_value     number;
4683   l_4_name      pay_balance_types.balance_name%type;
4684   l_total_value number := 0;
4685   l_balance_type pay_balance_types.balance_type_id%type;
4686   idx number; /*bug9147430*/
4687   t_union_bal_sort tab_au_eoy_bal_type := tab_au_eoy_bal_type();  /*bug9147430*/
4688 
4689 begin
4690   g_debug := hr_utility.debug_enabled;
4691   --
4692   -- If the financial year permits then perform the union fees archive for 2006/2007.
4693   --
4694   if to_number(to_char(p_year_start,'YYYY')) >= 2006 then
4695     --
4696     for rec_union in csr_unions_2006(g_balance_type_id) loop
4697 
4698       --
4699       -- 4863149 - Raise error when there is no defined balance id for allowance balance
4700       --
4701       if g_debug then
4702         hr_utility.set_location(l_procedure, 3);
4703       end if;
4704       --
4705       if rec_union.def_id is null then
4706         raise_application_error(-20101, 'Balance ID ' || rec_union.bal_type_id || ' not associated with dimension _ASG_LE_YTD');
4707       end if ;
4708       --
4709       -- Store all the values in a table
4710       --
4711       if nvl(rec_union.balance_value,0) > 0 then
4712         if g_debug then
4713           hr_utility.set_location(l_procedure, 3);
4714           hr_utility.set_location(l_procedure || ' : balance_name... = '||rec_union.balance_name,3);
4715           hr_utility.set_location(l_procedure || ' : balance_value.. = '||rec_union.balance_value,3);
4716         end if;
4717         t_union_table(l_counter).balance_name  := rec_union.balance_name;
4718         t_union_table(l_counter).balance_value := rec_union.balance_value;
4719         l_counter := l_counter + 1;
4720       end if;
4721     end loop;
4722 
4723    /* bug9147430 - Adjust normal Unions values by FW Unions values */
4724     if (to_number(to_char(p_year_start,'YYYY')) >= 2010)  and p_fw_union_tab_sorted.count > 0 then
4725 
4726         if g_debug then hr_utility.set_location('Starting Unions adjustment by FW Unions',100); end if;
4727 
4728          for i in t_union_table.first .. t_union_table.last loop
4729                for j in p_fw_union_tab_sorted.first .. p_fw_union_tab_sorted.last loop
4730                     if t_union_table(i).balance_name = p_fw_union_tab_sorted(j).balance_name then
4731                          t_union_table(i).balance_value := t_union_table(i).balance_value - p_fw_union_tab_sorted(j).balance_value;
4732                     end if;
4733                end loop;
4734          end loop;
4735 
4736         if g_debug then
4737            hr_utility.trace('After FW Union Adjustment');
4738            for i in t_union_table.first .. t_union_table.last loop
4739               hr_utility.trace(t_union_table(i).balance_name||' $'||t_union_table(i).balance_value);
4740             end loop;
4741         end if;
4742 
4743         idx := t_union_table.first;
4744         loop
4745         exit when idx is null;
4746           t_union_bal_sort.extend;
4747           t_union_bal_sort(t_union_bal_sort.last) := rec_au_eoy_bal_type(t_union_table(idx).balance_name
4748                                                                                                  , t_union_table(idx).balance_value);
4749           idx := t_union_table.next(idx);
4750        end loop;
4751 
4752        SELECT CAST (MULTISET (SELECT rec_au_eoy_bal_type(balance_name, balance_value)
4753                                                FROM   TABLE(t_union_bal_sort)
4754                                                WHERE balance_value > 0
4755                                                ORDER BY balance_value desc)
4756                              AS tab_au_eoy_bal_type)
4757        INTO t_union_bal_sort
4758        FROM dual;
4759 
4760        hr_utility.trace('After FW Union Sorting');
4761        if t_union_bal_sort.count >0 then -- bug10048508
4762          t_union_table.delete;
4763          for i in t_union_bal_sort.first .. t_union_bal_sort.last loop
4764              t_union_table(i).balance_name := t_union_bal_sort(i).balance_name;
4765              t_union_table(i).balance_value := t_union_bal_sort(i).balance_value;
4766 
4767               if g_debug then
4768                 hr_utility.trace(t_union_bal_sort(i).balance_name||' $'||t_union_bal_sort(i).balance_value);
4769               end if;
4770          end loop;
4771        end if;
4772 
4773           p_result_table(13).balance_value := p_result_table(13).balance_value - g_fw_union_value;
4774 
4775         if g_debug then hr_utility.set_location('Ending Unions adjustment by FW Unions',199); end if;
4776 
4777     end if;
4778 
4779     --
4780     -- No archive the names, values
4781     --
4782     /*
4783     archive_limited_values
4784     (p_assignment_action_id => p_assignment_action_id
4785     ,p_table                => t_union_table
4786     ,p_limit                => 4
4787     ,p_name_prefix          => 'X_UNION_'
4788     ,p_name_suffix          => '_NAME'
4789     ,p_value_prefix         => 'X_UNION_'
4790     ,p_value_suffix         => '_VALUE'
4791     ,p_total_name           => 'Miscellaneous'
4792     );
4793     */
4794     -- Due to the change in display of union fees for the 2006 payment summary layout,
4795     -- there are now 2 methods of setting up union fee elements.
4796     -- =====================================================
4797     -- The first and 'original' method involves users feeding their own elements to the
4798     -- 'Union Fees' legislative balance.
4799     -- This is mandatory setup so that all existing functionality is retained.
4800     --
4801     -- The 'new' method involves users 'linking' their union elements to an additional balance
4802     -- they have created.  This enables the identification of values for the individual unions.
4803     -- The 'linking' involves creating a feed to the new balance and selecting that balance
4804     -- as the 'Union fees primary balance' in the Element DF.
4805     -- This setup is not mandatory
4806     --
4807     -- Although it is recommended that users setup ALL their union elements using only 1 of the above methods,
4808     -- it cannot be enforced within the application. Therefore, to handle this case we use the 'Union Fees'
4809     -- balance as the definitive balance and check this when archiving union elements which adhere to the
4810     -- 'new' method of setup
4811     --
4812     --
4813     -- Archive the first 3 elements which adhere to the 'new' setup
4814     --
4815     for i in 1..3
4816     loop
4817       --
4818       -- Only archive if the values exist
4819       --
4820       if t_union_table.exists(i) then
4821         if g_debug then
4822           hr_utility.set_location(l_procedure,20);
4823         end if;
4824         create_extract_archive_details(p_assignment_action_id,'X_UNION_' || i || '_NAME',t_union_table(i).balance_name);
4825         create_extract_archive_details(p_assignment_action_id,'X_UNION_' || i || '_VALUE',t_union_table(i).balance_value);
4826         l_total_value := l_total_value + t_union_table(i).balance_value;
4827       end if;
4828     end loop;
4829     --
4830     -- We still need to archive the 4th position... if it exists
4831     --
4832     if g_debug then
4833       hr_utility.trace('There are '||t_union_table.count||' union elements');
4834       hr_utility.trace('Already archived value is '||l_total_value);
4835     end if;
4836     --
4837     -- The definitive figure for Union Fees is stored in the legislative union balance 'Union Fees'.
4838     -- It is possible that there may be a combination of old and new union elements being used.
4839     --
4840     -- A 'misc' figure must be archived if:
4841     -- 1. There exists more than 4 new union elements.
4842     -- 2. There is a combination of new and old union elements.
4843     --    In this case the sum of new unions will not equal the legislative union balance.
4844     --
4845     --
4846     -- 5679568
4847     --
4848     if t_union_table.count > 4
4849     or l_total_value <> p_result_table(13).balance_value then
4850       --
4851       -- There are more than 4 union elements with the 'new' setup method
4852       -- however, it is possible that the remaining union elements are not all using the 'new' method
4853       -- so we must use the definitive balance to obtain the correct value.  To do this
4854       -- we subtract the values just archived from the definitive balance.  The remainder must
4855       -- equal the correct value.
4856       --
4857       l_4_name  := 'Miscellaneous';
4858       l_4_value := p_result_table(13).balance_value - l_total_value;
4859       create_extract_archive_details(p_assignment_action_id,'X_UNION_4_NAME',l_4_name);
4860       create_extract_archive_details(p_assignment_action_id,'X_UNION_4_VALUE',l_4_value);
4861     else
4862       --
4863       -- If a 4th union exists then lets archive it
4864       --
4865       if t_union_table.exists(4) then
4866         --
4867         -- Check if the values added up equal the value stored in the definitive balance.
4868         --
4869         if g_debug then
4870           hr_utility.set_location(l_procedure,35);
4871         end if;
4872         --
4873         if (l_total_value + t_union_table(4).balance_value) = p_result_table(13).balance_value then
4874           --
4875           -- All elements have been setup using the new method and therefore the values are correct.
4876           -- We can store the 4th value in its corresponding position
4877           --
4878           if g_debug then
4879             hr_utility.set_location(l_procedure,40);
4880           end if;
4881           --
4882           l_4_name := t_union_table(4).balance_name;
4883           l_4_value := t_union_table(4).balance_value;
4884         else
4885           --
4886           -- There is a amalgamation of methods used to setup union elements.
4887           -- This means that we need to check the definitive balance to obtain correct values.
4888           -- This additional value must be stored in the 4th position and displayed as 'Miscellaneous'
4889           --
4890           l_4_name  := 'Miscellaneous';
4891           l_4_value := p_result_table(13).balance_value - l_total_value;
4892         end if;
4893         create_extract_archive_details(p_assignment_action_id,'X_UNION_4_NAME',l_4_name);
4894         create_extract_archive_details(p_assignment_action_id,'X_UNION_4_VALUE',l_4_value);
4895       end if;
4896     end if;
4897     --
4898   end if;
4899   --
4900   /*
4901   if t_union_table.count > 0 then
4902     p_alw_bal_exist := 'TRUE';
4903   else
4904     p_alw_bal_exist := 'FALSE';
4905   end if;
4906   */
4907   ---------------------------------------------------------
4908   t_union_table.delete;
4909   --
4910 end archive_2006_unions;
4911 --
4912 ----------------------------------------------------------------------+
4913 -- procedure to archive 'X_UNION_NAME' - added for
4914 -- bug no : 1764017
4915 ----------------------------------------------------------------------+
4916 --
4917 -- bug9147430
4918 -- When the logic is changed, archive_fw_details should be modified together.
4919 procedure archive_union_name
4920   (p_assignment_id              in per_all_assignments.assignment_id%type
4921   ,p_assignment_action_id       in pay_assignment_actions.assignment_action_id%type
4922   ,p_max_assignment_action_id   in pay_assignment_actions.assignment_action_id%type
4923   ,p_registered_employer        number
4924   ,p_year_start                 in date
4925   ,p_year_end                   in date
4926   ) as
4927   --
4928   cursor csr_union_fees
4929   (c_assignment_id per_all_assignments.assignment_id%type
4930   ,c_year_end      date
4931   ) is
4932   select distinct pet.reporting_name
4933   ,      pet.element_information_category
4934   ,      pet.element_information1
4935   from   pay_balance_types      pbt
4936   ,      pay_balance_feeds_f    pbf
4937   ,      pay_input_values_f     piv
4938   ,      pay_element_types_f    pet
4939   ,      pay_element_entries_f  pee
4940   ,      pay_element_links_f    pel
4941   ,      per_all_people_f       pap
4942   ,      per_periods_of_service pps
4943   ,      per_all_assignments_f  paa
4944   where  pet.element_type_id      = piv.element_type_id
4945   and    pbf.input_value_id       = piv.input_value_id
4946   and    pbf.balance_type_id      = pbt.balance_type_id
4947   and    pet.element_type_id      = pel.element_type_id
4948   and    pel.element_link_id      = pee.element_link_id
4949   and    pee.assignment_id        = c_assignment_id
4950   and    pee.assignment_id        = paa.assignment_id
4951   and    paa.person_id            = pap.person_id
4952   and    paa.person_id            = pps.person_id
4953   and    paa.period_of_service_id = pps.period_of_service_id
4954   and    pbt.balance_name         = 'Union Fees'
4955   and    pbt.legislation_code     = g_legislation_code
4956   and    pet.effective_start_date = (select max(et.effective_start_date )
4957                                      from pay_element_types_f et
4958                                      where et.element_type_id= pet.element_type_id
4959                                      and nvl(pps.actual_termination_date,c_year_end)
4960                                             between pet.effective_Start_date and pet.effective_end_date
4961                                    );
4962  --
4963  -- 2610141 - Removed the cursor for getting union fees
4964  --
4965  /*CURSOR  union_def_bal_id
4966     IS
4967  SELECT pdb.defined_balance_id
4968   FROM  pay_balance_types      pbt
4969        ,pay_defined_balances   pdb
4970        ,pay_balance_dimensions pbd
4971  WHERE  pbt.balance_name         = 'Union Fees'
4972    AND pbt.legislation_code     = 'AU'
4973    AND pbd.legislation_code     = 'AU'
4974    AND pbt.balance_type_id      = pdb.balance_type_id
4975    AND pbd.balance_dimension_id = pdb.balance_dimension_id
4976    AND pbd.dimension_name       = '_ASG_LE_YTD'; --2610141*/
4977 
4978   l_reporting_name   pay_element_types_f.reporting_name%type;
4979   l_procedure        constant varchar2(80) := g_package || '.archive_union_name';
4980   l_element_information_category    pay_element_types_f.element_information_category%type;
4981   l_element_information1            pay_element_types_f.element_information1%type;
4982   l_count                           number := 0;
4983   l_2006_union_setup_found          boolean;
4984   --
4985 begin
4986   g_debug := hr_utility.debug_enabled;
4987   if g_debug then
4988     hr_utility.set_location(l_procedure,1);
4989   end if;
4990   --
4991   -------------------------------------------
4992   -- if two or more elements feed the balance
4993   -- 'Union Fees' then reporting_name = 'MISCELLANEOUS'
4994   -- else archive the reporting name for the element.
4995   -- Notice that only the name is considered here, the value
4996   -- is archived elsewhere.
4997   --
4998   --
4999   -- 4738470 - Included check for balance value
5000   -- Only a single value needs to be checked, which is already stored in
5001   -- position 13 of the table.
5002   --
5003   if p_result_table(13).balance_value > 0 then
5004     --
5005     for rec_union_fees in csr_union_fees(p_assignment_id, p_year_end) loop
5006       -- /* Bug#4925547 */
5007       -- Check if any of the elements have had the new union setup entered
5008       --
5009       if rec_union_fees.element_information_category = 'AU_VOLUNTARY DEDUCTIONS'
5010               and rec_union_fees.element_information1 is not null then
5011         l_2006_union_setup_found := true;
5012       end if;
5013       --
5014       l_reporting_name := rec_union_fees.reporting_name;
5015       l_count := l_count + 1;
5016     end loop;
5017     --
5018     -- If more than 1 union fees element exists
5019     -- then the displayed name should be as follows
5020     --
5021     if l_count > 1 or l_reporting_name = 'Balance Initialization 4' then /*bug 8423565*/
5022       l_reporting_name :='MISCELLANEOUS';
5023     end if;
5024     --
5025     if g_debug then
5026       hr_utility.trace('reporting name : '||l_reporting_name);
5027     end if;
5028     --
5029   end if;
5030   --
5031   if l_reporting_name is not null then
5032     if g_debug then
5033       hr_utility.set_location(l_procedure, 20);
5034     end if;
5035     --
5036     create_extract_archive_details
5037     (p_assignment_action_id
5038     ,'X_UNION_NAMES'
5039     ,l_reporting_name
5040     );
5041   end if;
5042   --  /* Bug#4925547 */
5043   -- If the new setup has been performed then archive for this case
5044   --
5045   if l_2006_union_setup_found then
5046     if g_debug then
5047       hr_utility.set_location(l_procedure, 30);
5048     end if;
5049     archive_2006_unions
5050     (p_assignment_id              => p_assignment_id
5051     ,p_assignment_action_id       => p_assignment_action_id
5052     ,p_max_assignment_action_id   => p_max_assignment_action_id
5053     ,p_registered_employer        => p_registered_employer
5054     ,p_year_start                 => p_year_start
5055     ,p_year_end                   => p_year_end
5056 --    ,p_alw_bal_exist              => p_alw_bal_exist
5057     );
5058   else
5059     -- /* Bug#4925547 */
5060     -- Otherwise we archive the old setup method for unions
5061     -- into the new archive items, assuming the financial year
5062     -- is 2006/2007 and above
5063     --
5064     if g_debug then
5065       hr_utility.set_location(l_procedure, 40);
5066     end if;
5067     --
5068     if to_number(to_char(p_year_start,'YYYY')) >= 2006 then
5069 
5070       if (to_number(to_char(p_year_start,'YYYY')) >= 2010)  and g_fw_union_value > 0 then --bug9147430
5071                p_result_table(13).balance_value := p_result_table(13).balance_value - g_fw_union_value;
5072       end if;
5073 
5074       if g_debug then
5075         hr_utility.set_location(l_procedure, 50);
5076         hr_utility.trace('l_reporting_name => '||l_reporting_name);
5077         hr_utility.trace('l_value_13       => '||p_result_table(13).balance_value);
5078       end if;
5079       -- /* Bug#4925547 */
5080       -- Format the misc name correctly for 2006 layout
5081       --
5082       if l_reporting_name = 'MISCELLANEOUS' then
5083         l_reporting_name := 'Miscellaneous';
5084       end if;
5085       create_extract_archive_details(p_assignment_action_id,'X_UNION_1_NAME',l_reporting_name);
5086       create_extract_archive_details(p_assignment_action_id,'X_UNION_1_VALUE',p_result_table(13).balance_value);
5087     end if;
5088   end if;
5089   --
5090   if g_debug then
5091       hr_utility.set_location(l_procedure,999);
5092   end if;
5093 exception
5094   when others then
5095     if g_debug then
5096       hr_utility.set_location(l_procedure, 000);
5097     end if;
5098     raise;
5099 end archive_union_name;
5100 
5101 ----------------------------------------------------------------------+
5102 -- procedure to archive terminated employees details
5103 ----------------------------------------------------------------------+
5104 Procedure archive_etp_details
5105       (p_business_group_id    in hr_organization_units.business_group_id%type,
5106        p_registered_employer  in hr_organization_units.organization_id%type,
5107        p_payroll_action_id    in pay_payroll_actions.payroll_action_id%type,
5108        p_assignment_Action_id in pay_assignment_actions.assignment_action_id%type,
5109        p_assignment_id        in pay_assignment_actions.assignment_id%type,
5110        p_year_start           in pay_payroll_actions.effective_date%type,
5111        p_year_end         in pay_payroll_Actions.effective_date%type,
5112        p_lst_year_start       in pay_payroll_Actions.effective_date%type,/*Bug3661230 Added one extra parameter*/
5113        p_transitional_flag    out nocopy varchar2, /*Bug 6192381 Added New Parameters p_transitional_flag and p_part_prev_etp_flag */
5114        p_part_prev_etp_flag   out nocopy varchar2) is
5115 
5116 
5117   l_etp_last_name         per_all_people_f.last_name%type;
5118   l_etp_first_name        per_all_people_f.first_name%type;
5119   l_etp_middle_name       per_all_people_f.middle_names%type;
5120   l_etp_address_1             hr_locations.address_line_1%type;
5121   l_etp_address_2             hr_locations.address_line_2%type;
5122   l_etp_address_3             hr_locations.address_line_3%type;
5123   l_etp_suburb                hr_locations.town_or_city%type;
5124   l_etp_state                 hr_locations.region_1%type;
5125   l_etp_postcode          hr_locations.postal_code%type;
5126   l_etp_country               fnd_territories_tl.territory_short_name%type;
5127   l_etp_employee_number           per_all_people_f.employee_number%type;
5128   l_etp_date_of_birth             per_all_people_f.date_of_birth%type;
5129   l_etp_employee_start_date       per_periods_of_service.date_start%type;
5130   l_etp_death_benefit             per_periods_of_service.leaving_reason%type;
5131   l_asgmnt_loc                    hr_locations.location_code%type;
5132   l_emp_no                        per_all_people_f.employee_number%type;
5133   l_payroll                       pay_all_payrolls_f.payroll_name%type;
5134   l_emp_type                      per_all_people_f.current_employee_flag%type;
5135   l_address_date_from date;
5136 
5137   -----------------------------------------------------------------------------------------------+
5138   -- cursor to fetch terminated employees details
5139   -----------------------------------------------------------------------------------------------+
5140   /*Bug 8315198 - Modified cursor to fetch the Tax file number entered in termination form for employees recieving Death Benefit ETP */
5141   cursor  etp_details( c_business_group_id    in hr_organization_units.business_group_id%type,
5142                        c_registered_employer  in hr_organization_units.organization_id%type,
5143                        c_payroll_action_id    in pay_payroll_actions.payroll_action_id%type,
5144                        c_assignment_id        in pay_assignment_actions.assignment_id%type,
5145                        c_year_start           in pay_payroll_actions.effective_date%type,
5146                        c_year_end         in pay_payroll_Actions.effective_date%type)
5147       is
5148   select  pev.screen_entry_value                tax_file_number
5149          ,pap.last_name                         employee_last_name
5150          ,pap.first_name                        employee_first_name
5151          ,substr(pap.middle_names, 1, decode(instr(pap.middle_names,' '), 0, 60, instr(pap.middle_names,' ',1)-1)) employee_middle_name --Bug 10064551
5152          ,pad.address_line1                     employee_address_1
5153          ,pad.address_line2                     employee_address_2
5154          ,pad.address_line3                     employee_address_3
5155          ,pad.town_or_city                      employee_suburb
5156          ,pad.region_1                          employee_state
5157          ,pad.postal_code                       employee_postcode
5158          ,fta.territory_short_name              employee_country
5159          ,pad.style                             address_style           -- Bug 5364017
5160          ,pad.country                           address_country         -- Bug 5364017
5161          ,pap.employee_number                   employee_number
5162          ,to_char(pap.date_of_birth,'DDMMYYYY') employee_date_of_birth
5163          ,to_char(pps.date_start,'DDMMYYYY')    employee_start_date
5164          ,pps.pds_information2                  death_benefit_type
5165          ,nvl(to_char(pps.actual_termination_date,'DDMMYYYY'),'31124712') employee_termination_date
5166          ,decode(pps.pds_information1, 'AU_D','Y', 'N') death_benefit/*bug#1955993*/
5167          ,pad.date_from date_from/*Bug 2977533 */
5168          ,decode(pps.pds_information1, 'AU_D',nvl(pps.pds_information11,'000 000 000'),pev.screen_entry_value) etp_death_benefit_tfn  /*Bug#8315198*/
5169    from   hr_organization_information  hoi,
5170           hr_organization_units        hou,
5171           hr_soft_coding_keyflex       hsc,
5172           pay_element_types_f          pet,
5173           pay_input_values_f           piv,
5174           pay_element_links_f          pel,
5175           pay_element_entries_f        pee,
5176           pay_element_entry_values_f   pev,
5177           per_all_assignments_f        paa,
5178           per_all_people_f             pap,
5179           per_addresses                pad,
5180           fnd_territories_tl           fta,
5181           per_periods_of_service       pps,
5182           pay_payroll_actions          ppa,
5183           pay_assignment_actions       pac,
5184           hr_locations_all             hlc   /* Bug No : 2263587 */
5185    where  hou.business_group_id       = c_business_group_id
5186      and  hou.organization_id         = c_registered_employer
5187      and  ppa.action_type             = 'X'
5188      and  hou.organization_id         = hoi.organization_id
5189      and  hoi.org_information_context = 'AU_LEGAL_EMPLOYER'
5190      and  hou.business_group_id       = pap.business_group_id
5191      and  hsc.soft_coding_keyflex_id  = paa.soft_coding_keyflex_id
5192      and  pet.element_name            = 'Tax Information'
5193      and  piv.name                    = 'Tax File Number'
5194      and  pet.element_type_id         = piv.element_type_id
5195      and  pet.element_type_id         = pel.element_type_id
5196      and  pel.element_link_id         = pee.element_link_id
5197      and  pee.element_entry_id        = pev.element_entry_id
5198      and  piv.input_value_id          = pev.input_value_id
5199      and  paa.assignment_id           = pee.assignment_id
5200      and  pap.person_id               = paa.person_id
5201      and  pap.person_id               = pad.person_id(+)
5202      and  pad.primary_flag(+)         = 'Y' /*Added for bug 2774577*/
5203      and  fta.territory_code(+)       = pad.country
5204      and  fta.language(+)             = userenv('LANG')
5205      and  paa.location_id             = hlc.location_id(+)
5206      and  pap.person_id               = pps.person_id
5207      and  paa.period_of_service_id    = pps.period_of_service_id
5208      and  ppa.payroll_action_id       = pac.payroll_action_id
5209      and  paa.assignment_id           = pac.assignment_id
5210      and  pac.assignment_id           = c_assignment_id
5211      and  pac.payroll_action_id       = c_payroll_action_id
5212      and  hsc.segment1                = c_registered_employer /*Bug 2610141, Bug 4063321*/
5213      and  pps.actual_termination_date  between  paa.effective_start_date
5214                                           and  paa.effective_end_date
5215      and  pps.actual_termination_date  between  pap.effective_start_date
5216                                           and  pap.effective_end_date
5217      and  pps.actual_termination_date  between  pel.effective_start_date
5218                                           and  pel.effective_end_date
5219      and  pps.actual_termination_date  between  pee.effective_start_date
5220                                           and  pee.effective_end_date
5221      and  pps.actual_termination_date  between  pet.effective_start_date
5222                                           and  pet.effective_end_date
5223      and  pps.actual_termination_date  between  pev.effective_start_date
5224                                           and  pev.effective_end_date
5225      and  pps.actual_termination_date  between  piv.effective_start_date
5226                                           and  piv.effective_end_date
5227      and  pps.actual_termination_date between p_lst_year_start /*Bug3661230*/
5228                                           and  c_year_end
5229     order by pad.date_from desc;/*Bug 2977533 */
5230 
5231   --------------------------------------------------------------------------------------------------------------------------+
5232   -- Bug 5956223 cursor to fetch input values Transitional ETP and Part of Previously Paid ETP of Element ETP on Termination
5233   --------------------------------------------------------------------------------------------------------------------------+
5234 cursor  etp_trans_paid_flags(c_assignment_id per_all_Assignments_f.assignment_id%type,
5235                                c_year_start    date ,
5236                                c_year_end      date ) IS
5237 select  prv.result_value INPUT_VALUE,piv.name INPUT_NAME
5238 from    pay_element_types_f    pet
5239        ,pay_input_values_f     piv
5240        ,per_all_assignments_f  paa
5241        ,pay_run_results        prr
5242        ,pay_run_result_values  prv
5243        ,pay_assignment_actions pac
5244        ,pay_payroll_actions    ppa
5245 where   pet.element_type_id      = piv.element_type_id
5246   and   pet.element_name         = 'ETP on Termination'
5247   and   piv.name                 in ('Transitional ETP','Part of Previously Paid ETP')
5248   and   paa.assignment_id        = c_assignment_id
5249   and   prv.input_value_id       = piv.input_value_id
5250   and   prr.element_type_id      = pet.element_type_id
5251   and   prr.run_result_id        = prv.run_result_id
5252   and   prr.assignment_action_id = pac.assignment_action_id
5253   and   pac.assignment_id        = paa.assignment_id
5254   and   pac.payroll_action_id    = ppa.payroll_action_id
5255   and   paa.effective_start_date between pet.effective_start_date and pet.effective_end_date
5256   and   paa.effective_start_date between piv.effective_start_date and piv.effective_end_date
5257    and    ppa.action_type             in ('R','Q','I','B','V')
5258   and   ppa.effective_date       between paa.effective_start_date and paa.effective_end_date
5259   and   ppa.effective_date between c_year_start  and c_year_end
5260  and   pac.tax_unit_id = p_registered_employer
5261  order by prr.run_result_id;
5262 
5263    l_etp_details  etp_details%rowtype;
5264    l_country      fnd_territories_tl.territory_short_name%type;
5265 
5266    lv_transitional_flag  VARCHAR2(1);
5267    lv_part_prev_etp_flag VARCHAR2(1);
5268 Begin
5269   IF g_debug THEN
5270       hr_utility.set_location('Start of archive etp details procedure.. ', 1);
5271       hr_utility.set_location('Assignment action id : '||p_assignment_action_id, 2);
5272   END if;
5273 
5274    lv_transitional_flag   :='N';
5275    lv_part_prev_etp_flag  :='N';
5276 
5277   open etp_details(p_business_group_id
5278                   ,p_registered_employer
5279                   ,p_payroll_action_id
5280                   ,p_assignment_id
5281                   ,p_year_start
5282                   ,p_year_end);
5283 
5284   fetch etp_details into l_etp_details;
5285 
5286   if etp_details%found then
5287     close etp_details;
5288     IF g_debug THEN
5289         hr_utility.set_location('etp tfn ' || l_etp_details.tax_file_number, 1);
5290     END if;
5291 
5292     create_extract_archive_details(p_assignment_action_id,
5293                                   'X_ETP_TAX_FILE_NUMBER',
5294                                    l_etp_details.tax_file_number);
5295 
5296     create_extract_archive_details(p_assignment_action_id,
5297                                    'X_ETP_EMPLOYEE_SURNAME',
5298                                     l_etp_details.employee_last_name);
5299 
5300 
5301     create_extract_archive_details(p_assignment_action_id,
5302                                   'X_ETP_EMPLOYEE_FIRST_NAME',
5303                                    l_etp_details.employee_first_name);
5304 
5305     create_extract_archive_details(p_assignment_action_id,
5306                                   'X_ETP_EMPLOYEE_MIDDLE_NAME',
5307                                    l_etp_details.employee_middle_name);
5308 
5309     create_extract_archive_details(p_assignment_action_id,
5310                                   'X_ETP_EMPLOYEE_ADDRESS_1',
5311                                    l_etp_details.employee_address_1);
5312 
5313     create_extract_archive_details(p_assignment_action_id,
5314                                   'X_ETP_EMPLOYEE_ADDRESS_2',
5315                                    l_etp_details.employee_address_2);
5316 
5317    create_extract_archive_details(p_assignment_action_id,
5318                                   'X_ETP_EMPLOYEE_ADDRESS_3',
5319                                    l_etp_details.employee_address_3);
5320 
5321     create_extract_archive_details(p_assignment_action_id,
5322                                   'X_ETP_EMPLOYEE_SUBURB',
5323                                    l_etp_details.employee_suburb);
5324 
5325     create_extract_archive_details(p_assignment_action_id,
5326                                   'X_ETP_EMPLOYEE_STATE',
5327                                    l_etp_details.employee_state);
5328 
5329     create_extract_archive_details(p_assignment_action_id,
5330                                   'X_ETP_EMPLOYEE_POSTCODE',
5331                                    l_etp_details.employee_postcode);
5332 
5333     /* Bug 5364017 - Check for Generic Address
5334        IF Address Style is Generic - then get the country name as entered in Address form
5335     */
5336       IF    (l_etp_details.address_style = 'GENERIC')
5337       THEN
5338             l_country := l_etp_details.address_country;
5339       ELSE
5340             l_country := l_etp_details.employee_country;
5341       END IF;
5342 
5343     create_extract_archive_details(p_assignment_action_id,
5344                                   'X_ETP_EMPLOYEE_COUNTRY',
5345                                    l_country);
5346 
5347     create_extract_archive_details(p_assignment_action_id,
5348                                   'X_ETP_EMPLOYEE_DATE_OF_BIRTH',
5349                                    l_etp_details.employee_date_of_birth);
5350 
5351     create_extract_archive_details(p_assignment_action_id,
5352                                   'X_ETP_DEATH_BENEFIT',
5353                                    l_etp_details.death_benefit);
5354 
5355     create_extract_archive_details(p_assignment_action_id,
5356                                   'X_ETP_DEATH_BENEFIT_TYPE',
5357                                    l_etp_details.death_benefit_type);
5358 
5359    /*Begin 8315198 - The value of tax file number for death benefit ETP entered in termination form
5360                      is archived from FY 2009 onwards*/
5361 
5362   if (to_number(to_char(p_year_start,'YYYY')) >= 2009) then
5363     create_extract_archive_details(p_assignment_action_id,
5364                                   'X_ETP_DEATH_BENEFIT_TFN',
5365                                    l_etp_details.etp_death_benefit_tfn);
5366   end if;
5367    /*End 8315198*/
5368 
5369   else
5370     IF g_debug THEN
5371     hr_utility.set_location('Details for terminated employee not found ', 3);
5372    END if;
5373     close etp_details;
5374   end if;
5375 
5376 /*Bug 5956223 Begin*/
5377 for csr_trans in etp_trans_paid_flags(p_assignment_id ,p_year_start,p_year_end )
5378 loop
5379     if csr_trans.INPUT_NAME = 'Transitional ETP' then
5380        lv_transitional_flag := csr_trans.INPUT_VALUE;
5381     end if;
5382 
5383     if csr_trans.INPUT_NAME = 'Part of Previously Paid ETP' then
5384        lv_part_prev_etp_flag := csr_trans.INPUT_VALUE;
5385     end if;
5386 
5387 end loop;
5388 
5389 /*Bug 6192381  assigning values to out variables */
5390 /*Bug 7135544  Added NVL (N) to lv_transitional_flag and lv_part_prev_etp_flag */
5391          p_transitional_flag  := nvl(lv_transitional_flag,'N');
5392          p_part_prev_etp_flag := nvl(lv_part_prev_etp_flag,'N');
5393 
5394   create_extract_archive_details(p_assignment_action_id,
5395                                 'X_TRANSITIONAL_ETP',
5396                                 p_transitional_flag);
5397 
5398   create_extract_archive_details(p_assignment_action_id,
5399                                 'X_PART_OF_PREVIOUS_ETP',
5400                                 p_part_prev_etp_flag);
5401 
5402 /*Bug 5956223 End*/
5403 
5404 if (to_number(to_char(p_year_start,'YYYY')) <2012) then /* bug 14703826 - skip to call archive_etp_payment_details */
5405   ----------------------------------------------------
5406   -- Added for Bug 1746093
5407   -- this subprogram archives X_ETP_ON_TERMINATION_PAID
5408   -- and  X_ETP_EMPLOYEE_PAYMENT_DATE
5409   ----------------------------------------------------
5410    archive_etp_payment_details( p_assignment_action_id ,
5411                         p_registered_employer, --2610141
5412                         p_assignment_id ,
5413                         p_year_start,
5414                         p_year_end );
5415 end if;
5416 
5417   IF g_debug THEN
5418       hr_utility.set_location('exiting archive etp details procedure ', 3);
5419   END if;
5420 exception
5421   when others then
5422     if etp_details%isopen  then
5423       close etp_details;
5424     end if;
5425     IF g_debug THEN
5426         hr_utility.set_location('Error in archive etp details.  ', 300);
5427     END if;
5428     raise;
5429 End archive_etp_details;
5430 
5431   ----------------------------------------------------------------------+
5432    -- this procedure creates archive items
5433     -- X_PRE_JUL_83_COMPONENT_ASG_YTD
5434     -- X_POST_JUN_83_TAXED_ASG_YTD
5435     -- X_POST_JUN_83_UNTAXED_ASG_YTD
5436     -- X_DAYS_PRE_JUL_83
5437     -- X_DAYS_POST_JUL_83
5438    -- for terminated employees
5439   ---------------------------------------------------------------------+
5440 
5441 
5442 procedure archive_prepost_details
5443      (p_assignment_action_id    in   pay_assignment_actions.ASSIGNMENT_ACTION_ID%TYPE
5444      ,p_max_assignment_action_id in  pay_assignment_actions.ASSIGNMENT_ACTION_ID%TYPE --2610141
5445      ,p_registered_employer     in   pay_assignment_actions.TAX_UNIT_ID%TYPE --2610141
5446      ,p_legislation_code        in   pay_defined_balances.legislation_code%TYPE
5447      ,p_assignment_id           in   pay_assignment_actions.ASSIGNMENT_ID%type
5448      ,p_payroll_action_id       in   pay_payroll_actions.payroll_action_id%TYPE
5449      ,p_actual_termination_date in   per_periods_of_service.actual_termination_date%TYPE
5450      ,p_date_start              in   per_periods_of_service.date_start%TYPE
5451      ,p_year_start              in   pay_payroll_actions.effective_date%type
5452      ,p_year_end                in   pay_payroll_Actions.effective_date%type
5453      ,p_transitional_flag       in   varchar2 /*Bug 6192381 Added New Parameters p_transitional_flag and p_part_prev_etp_flag */
5454      ,p_part_prev_etp_flag      in   varchar2) is
5455 
5456 
5457       v_some_warning                 BOOLEAN;
5458       v_bal_value                    VARCHAR2(20);
5459       v_user_entity_id               NUMBER;
5460       v_archive_item_id              ff_archive_items.archive_item_id%TYPE;
5461       v_object_version_number        NUMBER;
5462       e_prepost_error                EXCEPTION;
5463       l_result                       NUMBER;
5464 
5465       l_etp_payment                  NUMBER;
5466       l_pre01jul1983_days            NUMBER;
5467       l_post30jun1983_days           NUMBER;
5468       l_pre01jul1983_ratio           NUMBER;
5469       l_post30jun1983_ratio          NUMBER;
5470       l_pre01jul1983_value           NUMBER;
5471       l_post30jun1983_value          NUMBER;
5472       l_etp_service_date             date;    /*  Bug# 2984390 */
5473       v_etp_service_date             VARCHAR2(20);
5474       l_le_etp_service_date          date; /* Bug 4177679*/
5475       l_balance_name                 pay_balance_types.balance_name%TYPE;
5476       l_indx                         NUMBER;
5477       l_lump_sum_c_pre_83_amt        NUMBER; /* Start 8769345 */
5478       l_lump_sum_c_post_83_amt       NUMBER; /* End 8769345 */
5479      TYPE prepost_rec IS RECORD (item_name  varchar2(50),
5480                                  item_value number );
5481      TYPE prepost_type IS TABLE OF prepost_rec INDEX BY BINARY_INTEGER;
5482      tab_prepost_dtls prepost_type;
5483 
5484       l_etp_pay_tran_no_ppetp       NUMBER;
5485       l_etp_pay_tran_ppetp          NUMBER;
5486       l_etp_pay_no_tran_no_ppetp    NUMBER;
5487       l_etp_pay_no_tran_ppetp       NUMBER;
5488       l_etp_pay_total               NUMBER;
5489 
5490       l_old_etp_pay                 NUMBER;
5491       l_old_pre01jul1983_value      NUMBER;
5492       l_old_post30jun1983_value     NUMBER;
5493 
5494       l_item_name_pre               VARCHAR2(50);
5495       l_item_name_post               VARCHAR2(50);
5496       l_etp_new_bal_total           NUMBER; /* Bug 9226023 - Variable to store the sum of Taxable and Tax free portions of ETP balances */
5497       l_etp_pay_value               NUMBER; /* Bug 9226023 - Temporary variable to store the ETP Taxble or Tax Free value */
5498 
5499      /* Bug 2826802 : Cursor to get the Lump Sum C Payment balance value */
5500      /* Bug 6192381 : Removed cursor get_etp_payment_value as using BBR now*/
5501 
5502   type etp_bal_type is table of varchar2(100) index by binary_integer;
5503   tab_etp_bal_name etp_bal_type ;
5504 
5505   begin
5506      IF g_debug THEN
5507         hr_utility.set_location('Start of archive_prepost_details',15);
5508         hr_utility.set_location('Start of p_assg_action_id '|| p_assignment_action_id,16);
5509      END if;
5510 
5511      --------------------------------------------------------------------------------+
5512      -- if the employee type is not current then archive the following termination details
5513      -- X_PRE_JUL_83_COMPONENT_ASG_YTD
5514      -- X_POST_JUN_83_UNTAXED_ASG_YTD
5515      -- X_POST_JUN_83_TAXED_ASG_YTD
5516      -- X_DAYS_PRE_JUL_83
5517      -- X_DAYS_POST_JUL_83
5518 
5519      --   Bug No : 2826802
5520      --   The value of the Lump Sum C Payment balance will be fetched using
5521      --   hr_aubal.calc_all_balances. The value will be used as ETP Payment.
5522      --
5523      --   This value is multiplied by etp ratio obtained from
5524      --   pay_au_terminations.etp_prepost_ratios to get
5525      --   the value for X_PRE_JUL_83_COMPONENT and  X_POST_JUN_83_TAXED.
5526      --
5527      --   Value for the other two archive items X_DAYS_PRE_JUL_83
5528      --   and  X_DAYS_POST_JUL_83 are returned as an out parameter.
5529      --   The archive item name and archive item value are
5530      --   populated in a pl/sql table ( which is of record type)
5531      --
5532      --   Now we have all the parameters required to call create_extract_archive_details
5533      --   so we call it for the above archive items.
5534      --------------------------------------------------------------------------------+
5535 
5536      /* Bug 2826802 : Get the Value of etp payment as value of the Lump Sum C Payment
5537         Balance and use it to get the pre and post 83 values */
5538 
5539          l_pre01jul1983_value  := 0;
5540          l_post30jun1983_value := 0;
5541 
5542          l_pre01jul1983_days  := 0;
5543          l_post30jun1983_days := 0;
5544 
5545       l_etp_pay_tran_no_ppetp       := 0;
5546       l_etp_pay_tran_ppetp          := 0;
5547       l_etp_pay_no_tran_no_ppetp    := 0;
5548       l_etp_pay_no_tran_ppetp       := 0;
5549       l_etp_pay_total               := 0;
5550 
5551       l_old_etp_pay                 := 0;
5552       l_old_pre01jul1983_value      := 0;
5553       l_old_post30jun1983_value     := 0;
5554 
5555      --------------------------------------------------------------------------------+
5556      -- this procedure gets the ratios to calculate prejul83 balance and postjun83 balance
5557      --------------------------------------------------------------------------------+
5558          IF g_debug THEN
5559             hr_utility.set_location('calling pay_au_terminations.etp_prepost_ratios ',17);
5560          END if;
5561 
5562           l_result := pay_au_terminations.etp_prepost_ratios(
5563                      p_assignment_id              -- number                  in
5564                     ,p_date_start                 -- date                    in
5565                     ,p_actual_termination_date    -- date                    in
5566                     ,'N'                          -- Bug#2819479 Flag to check whether this function called by Termination Form.
5567                     ,l_pre01jul1983_days          -- number                  out
5568                     ,l_post30jun1983_days         -- number                  out
5569                     ,l_pre01jul1983_ratio         -- number                  out
5570                     ,l_post30jun1983_ratio        -- number                  out
5571                     ,l_etp_service_date           -- date                    out
5572                     ,l_le_etp_service_date);      -- date                    out  /* Bug 4177679 */
5573 
5574 /*Bug 6192381 Introduced call to pay_balance_pkg.get_value to use BBR for ETP Payment Balances
5575   Removed cursor get_etp_payment_value and its call*/
5576 
5577     p_etp_result_table.delete;
5578 
5579     p_etp_context_table(1).tax_unit_id := p_registered_employer;
5580     --
5581     pay_balance_pkg.get_value
5582     (p_assignment_action_id     => p_max_assignment_action_id
5583     ,p_defined_balance_lst  => p_etp_balance_value_tab
5584     ,p_context_lst      => p_etp_context_table
5585     ,p_output_table         => p_etp_result_table
5586     );
5587 
5588     if g_debug then
5589       hr_utility.trace('------------------------------------------------');
5590       hr_utility.trace('ETP Payments Transitional Not Part of Prev Term   ===>' || p_etp_result_table(1).balance_value);
5591       hr_utility.trace('ETP Payments Transitional Part of Prev Term       ===>' || p_etp_result_table(2).balance_value);
5592       hr_utility.trace('ETP Payments Life Benefit Not Part of Prev Term   ===>' || p_etp_result_table(3).balance_value);
5593       hr_utility.trace('ETP Payments Life Benefit Part of Prev Term       ===>' || p_etp_result_table(4).balance_value);
5594       hr_utility.trace('Lump Sum C Payments                               ===>' || p_etp_result_table(13).balance_value);
5595      /* Start 8769345 - Added trace for new ETP balances */
5596       hr_utility.trace('ETP Tax Free Payments Transitional Not Part of Prev Term ===>' || p_etp_result_table(5).balance_value);
5597       hr_utility.trace('ETP Taxable Payments Transitional Not Part of Prev Term  ===>' || p_etp_result_table(6).balance_value);
5598       hr_utility.trace('ETP Tax Free Payments Transitional Part of Prev Term     ===>' || p_etp_result_table(7).balance_value);
5599       hr_utility.trace('ETP Taxable Payments Transitional Part of Prev Term      ===>' || p_etp_result_table(8).balance_value);
5600       hr_utility.trace('ETP Tax Free Payments Life Benefit Not Part of Prev Term ===>' || p_etp_result_table(9).balance_value);
5601       hr_utility.trace('ETP Taxable Payments Life Benefit Not Part of Prev Term  ===>' || p_etp_result_table(10).balance_value);
5602       hr_utility.trace('ETP Tax Free Payments Life Benefit Part of Prev Term     ===>' || p_etp_result_table(11).balance_value);
5603       hr_utility.trace('ETP Taxable Payments Life Benefit Part of Prev Term      ===>' || p_etp_result_table(12).balance_value);
5604      /* End 8769345 */
5605     end if;
5606 
5607     tab_etp_bal_name(1) := 'ETP Payments Transitional Not Part of Prev Term';
5608     tab_etp_bal_name(2) := 'ETP Payments Transitional Part of Prev Term';
5609     tab_etp_bal_name(3) := 'ETP Payments Life Benefit Not Part of Prev Term';
5610     tab_etp_bal_name(4) := 'ETP Payments Life Benefit Part of Prev Term';
5611     tab_etp_bal_name(13) := 'Lump Sum C Payments';
5612  /* Start 8769345 */
5613     tab_etp_bal_name(5) := 'ETP Tax Free Payments Transitional Not Part of Prev Term';
5614     tab_etp_bal_name(6) := 'ETP Taxable Payments Transitional Not Part of Prev Term';
5615     tab_etp_bal_name(7) := 'ETP Tax Free Payments Transitional Part of Prev Term';
5616     tab_etp_bal_name(8) := 'ETP Taxable Payments Transitional Part of Prev Term';
5617     tab_etp_bal_name(9) := 'ETP Tax Free Payments Life Benefit Not Part of Prev Term';
5618     tab_etp_bal_name(10) := 'ETP Taxable Payments Life Benefit Not Part of Prev Term';
5619     tab_etp_bal_name(11) := 'ETP Tax Free Payments Life Benefit Part of Prev Term';
5620     tab_etp_bal_name(12) := 'ETP Taxable Payments Life Benefit Part of Prev Term';
5621  /* End  8769345 */
5622 
5623      l_indx := 1;
5624 
5625      /* Bug 8769345 - Two new local variables are defined which will hold the pre 83 and post 83 components of
5626                   Lump Sum C Payments */
5627 
5628      l_lump_sum_c_pre_83_amt := 0;
5629      l_lump_sum_c_post_83_amt := 0;
5630 
5631 /* Start 9226023 - The sum of Taxable and Tax free portions of ETP balances introduced as part of bug 8769345
5632                    are stored in the variable ,which will be used in calculating the ETP Taxable and Tax free amounts of
5633            terminated employees processed before applying the patch 8769345*/
5634 
5635      l_etp_new_bal_total := p_etp_result_table(5).balance_value + p_etp_result_table(6).balance_value +
5636                     p_etp_result_table(7).balance_value + p_etp_result_table(8).balance_value +
5637                 p_etp_result_table(9).balance_value + p_etp_result_table(10).balance_value +
5638                 p_etp_result_table(11).balance_value + p_etp_result_table(12).balance_value;
5639 
5640 /* End 9226023 */
5641 
5642      for etp_pay_rec in 1..tab_etp_bal_name.count
5643      loop
5644 
5645          if g_debug then
5646            hr_utility.trace('Balance Name '||tab_etp_bal_name(etp_pay_rec)|| ' Balance Value '||p_etp_result_table(etp_pay_rec).balance_value);
5647          end if;
5648 
5649 
5650           if l_result = 0 then
5651              raise e_prepost_error;
5652           else
5653                  l_etp_pay_value :=0;
5654         --------------------------------------------------------------------------------+
5655         --populate a pl/sql table with the details of the archive items
5656         --------------------------------------------------------------------------------+
5657 
5658   /* Bug 6192381  appending Pre Post values for new balances */
5659 
5660          if tab_etp_bal_name(etp_pay_rec) = 'ETP Payments Transitional Not Part of Prev Term' then
5661 
5662                l_etp_pay_tran_no_ppetp :=  p_etp_result_table(etp_pay_rec).balance_value;
5663 
5664              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Payments Transitional Part of Prev Term' then
5665 
5666                l_etp_pay_tran_ppetp :=  p_etp_result_table(etp_pay_rec).balance_value;
5667 
5668              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Payments Life Benefit Not Part of Prev Term' then
5669 
5670                l_etp_pay_no_tran_no_ppetp :=  p_etp_result_table(etp_pay_rec).balance_value;
5671 
5672          elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Payments Life Benefit Part of Prev Term' then
5673 
5674                l_etp_pay_no_tran_ppetp :=  p_etp_result_table(etp_pay_rec).balance_value;
5675 
5676              /* Start 8769345 - Code is modified such that pre 83 and post 83 values for different combinations of Transitional and
5677                             Part of Previously Paid ETP are calculated by directly accessing the new ETP tax free and taxable balances. */
5678          /* Start 9226023 - Logic modified to support the calculation of Taxable and Tax Free portions of ETP for terminated employees
5679                             processed before applying the patch 8769345 */
5680 
5681              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Tax Free Payments Transitional Not Part of Prev Term' then
5682 
5683            if (l_etp_new_bal_total > 0) then
5684              if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5685             l_etp_pay_value :=p_etp_result_table(etp_pay_rec).balance_value;
5686 
5687          elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5688                     l_etp_pay_value := (p_etp_result_table(1).balance_value - (p_etp_result_table(5).balance_value + p_etp_result_table(6).balance_value))*l_pre01jul1983_ratio +
5689                                               p_etp_result_table(etp_pay_rec).balance_value;
5690                  end if;
5691            else
5692           l_etp_pay_value := p_etp_result_table(1).balance_value*l_pre01jul1983_ratio;
5693            end if;
5694 
5695           tab_prepost_dtls(l_indx).item_name :='X_PRE_JUL_83_COMP_TRANS_NOT_PPTERM_ASG_YTD';
5696           tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2) ;
5697           l_lump_sum_c_pre_83_amt := l_lump_sum_c_pre_83_amt + tab_prepost_dtls(l_indx).item_value;
5698 
5699           l_indx := l_indx + 1;
5700              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Taxable Payments Transitional Not Part of Prev Term' then
5701 
5702         if (l_etp_new_bal_total > 0) then
5703          if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5704                     l_etp_pay_value :=p_etp_result_table(etp_pay_rec).balance_value;
5705 
5706          elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5707             l_etp_pay_value := (p_etp_result_table(1).balance_value - (p_etp_result_table(5).balance_value + p_etp_result_table(6).balance_value))*l_post30jun1983_ratio +
5708                                               p_etp_result_table(etp_pay_rec).balance_value;
5709                  end if;
5710            else
5711            l_etp_pay_value := p_etp_result_table(1).balance_value*l_post30jun1983_ratio;
5712            end if;
5713 
5714               tab_prepost_dtls(l_indx).item_name :='X_POST_JUN_83_TAXED_TRANS_NOT_PPTERM_ASG_YTD';
5715               tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2);
5716           l_lump_sum_c_post_83_amt := l_lump_sum_c_post_83_amt + tab_prepost_dtls(l_indx).item_value;
5717 
5718               l_indx := l_indx + 1;
5719 
5720              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Tax Free Payments Transitional Part of Prev Term' then
5721 
5722                if (l_etp_new_bal_total > 0) then
5723          if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5724                     l_etp_pay_value :=p_etp_result_table(etp_pay_rec).balance_value;
5725 
5726                  elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5727                     l_etp_pay_value := (p_etp_result_table(2).balance_value - (p_etp_result_table(7).balance_value + p_etp_result_table(8).balance_value))*l_pre01jul1983_ratio +
5728                                              p_etp_result_table(etp_pay_rec).balance_value;
5729                  end if;
5730            else
5731            l_etp_pay_value := p_etp_result_table(2).balance_value*l_pre01jul1983_ratio;
5732            end if;
5733 
5734           tab_prepost_dtls(l_indx).item_name :='X_PRE_JUL_83_COMP_TRANS_PPTERM_ASG_YTD';
5735               tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2);
5736               l_lump_sum_c_pre_83_amt := l_lump_sum_c_pre_83_amt + tab_prepost_dtls(l_indx).item_value;
5737 
5738           l_indx := l_indx + 1;
5739 
5740              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Taxable Payments Transitional Part of Prev Term' then
5741 
5742                if (l_etp_new_bal_total > 0) then
5743          if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5744                     l_etp_pay_value :=p_etp_result_table(etp_pay_rec).balance_value;
5745 
5746                  elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5747                     l_etp_pay_value := (p_etp_result_table(2).balance_value - (p_etp_result_table(7).balance_value + p_etp_result_table(8).balance_value))*l_post30jun1983_ratio +
5748                                              p_etp_result_table(etp_pay_rec).balance_value;
5749                  end if;
5750            else
5751            l_etp_pay_value := p_etp_result_table(2).balance_value*l_post30jun1983_ratio;
5752            end if;
5753 
5754           tab_prepost_dtls(l_indx).item_name :='X_POST_JUN_83_TAXED_TRANS_PPTERM_ASG_YTD';
5755               tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2);
5756           l_lump_sum_c_post_83_amt := l_lump_sum_c_post_83_amt + tab_prepost_dtls(l_indx).item_value;
5757 
5758           l_indx := l_indx + 1;
5759 
5760              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Tax Free Payments Life Benefit Not Part of Prev Term' then
5761 
5762                if (l_etp_new_bal_total > 0) then
5763          if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5764                     l_etp_pay_value :=p_etp_result_table(etp_pay_rec).balance_value;
5765 
5766                  elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5767                     l_etp_pay_value := (p_etp_result_table(3).balance_value - (p_etp_result_table(9).balance_value + p_etp_result_table(10).balance_value))*l_pre01jul1983_ratio +
5768                                              p_etp_result_table(etp_pay_rec).balance_value;
5769                  end if;
5770            else
5771            l_etp_pay_value := p_etp_result_table(3).balance_value*l_pre01jul1983_ratio;
5772            end if;
5773 
5774               tab_prepost_dtls(l_indx).item_name :='X_PRE_JUL_83_COMP_NOT_TRANS_NOT_PPTERM_ASG_YTD';
5775               tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2);
5776           l_lump_sum_c_pre_83_amt := l_lump_sum_c_pre_83_amt + tab_prepost_dtls(l_indx).item_value;
5777 
5778           l_indx := l_indx + 1;
5779 
5780              elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Taxable Payments Life Benefit Not Part of Prev Term' then
5781 
5782                if (l_etp_new_bal_total > 0) then
5783          if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5784                     l_etp_pay_value :=p_etp_result_table(etp_pay_rec).balance_value;
5785 
5786                  elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5787                     l_etp_pay_value := (p_etp_result_table(3).balance_value - (p_etp_result_table(9).balance_value + p_etp_result_table(10).balance_value))*l_post30jun1983_ratio +
5788                                              p_etp_result_table(etp_pay_rec).balance_value;
5789                  end if;
5790            else
5791            l_etp_pay_value := p_etp_result_table(3).balance_value*l_post30jun1983_ratio;
5792            end if;
5793 
5794           tab_prepost_dtls(l_indx).item_name :='X_POST_JUN_83_TAXED_NOT_TRANS_NOT_PPTERM_ASG_YTD';
5795               tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2);
5796           l_lump_sum_c_post_83_amt := l_lump_sum_c_post_83_amt + tab_prepost_dtls(l_indx).item_value;
5797 
5798           l_indx := l_indx + 1;
5799 
5800          elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Tax Free Payments Life Benefit Part of Prev Term' then
5801 
5802                if (l_etp_new_bal_total > 0) then
5803          if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5804                     l_etp_pay_value :=p_etp_result_table(etp_pay_rec).balance_value;
5805 
5806                  elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5807                     l_etp_pay_value := (p_etp_result_table(4).balance_value - (p_etp_result_table(11).balance_value + p_etp_result_table(12).balance_value))*l_pre01jul1983_ratio +
5808                                              p_etp_result_table(etp_pay_rec).balance_value;
5809                  end if;
5810            else
5811            l_etp_pay_value := p_etp_result_table(4).balance_value*l_pre01jul1983_ratio;
5812            end if;
5813 
5814               tab_prepost_dtls(l_indx).item_name :='X_PRE_JUL_83_COMP_NOT_TRANS_PPTERM_ASG_YTD';
5815               tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2) ;
5816               l_lump_sum_c_pre_83_amt := l_lump_sum_c_pre_83_amt + tab_prepost_dtls(l_indx).item_value;
5817 
5818           l_indx := l_indx + 1;
5819 
5820          elsif tab_etp_bal_name(etp_pay_rec) = 'ETP Taxable Payments Life Benefit Part of Prev Term' then
5821 
5822                if (l_etp_new_bal_total > 0) then
5823          if (p_etp_result_table(13).balance_value - l_etp_new_bal_total = 0) then
5824                     l_etp_pay_value := p_etp_result_table(etp_pay_rec).balance_value;
5825 
5826                  elsif (p_etp_result_table(13).balance_value - l_etp_new_bal_total > 0) then
5827                     l_etp_pay_value := (p_etp_result_table(4).balance_value - (p_etp_result_table(11).balance_value + p_etp_result_table(12).balance_value))*l_post30jun1983_ratio +
5828                                               p_etp_result_table(etp_pay_rec).balance_value;
5829                  end if;
5830            else
5831            l_etp_pay_value := p_etp_result_table(4).balance_value*l_post30jun1983_ratio;
5832            end if;
5833 
5834               tab_prepost_dtls(l_indx).item_name :='X_POST_JUN_83_TAXED_NOT_TRANS_PPTERM_ASG_YTD';
5835               tab_prepost_dtls(l_indx).item_value := round(l_etp_pay_value,2);
5836           l_lump_sum_c_post_83_amt := l_lump_sum_c_post_83_amt + tab_prepost_dtls(l_indx).item_value;
5837 
5838           l_indx := l_indx + 1;
5839 
5840          /* End 8769345 */
5841          /* End 9226023 */
5842             elsif tab_etp_bal_name(etp_pay_rec) = 'Lump Sum C Payments' then
5843 
5844                l_etp_pay_total :=  p_etp_result_table(etp_pay_rec).balance_value;
5845 
5846                tab_prepost_dtls(l_indx).item_name :='X_PRE_JUL_83_COMPONENT_ASG_YTD';
5847                tab_prepost_dtls(l_indx).item_value :=l_lump_sum_c_pre_83_amt;
5848 
5849                l_indx := l_indx + 1;
5850 
5851                tab_prepost_dtls(l_indx).item_name :='X_POST_JUN_83_TAXED_ASG_YTD';
5852                tab_prepost_dtls(l_indx).item_value :=l_lump_sum_c_post_83_amt;
5853 
5854                l_indx := l_indx + 1;
5855 
5856                tab_prepost_dtls(l_indx).item_name :='X_DAYS_PRE_JUL_83';
5857                tab_prepost_dtls(l_indx).item_value :=l_pre01jul1983_days;
5858 
5859                l_indx := l_indx + 1;
5860 
5861                tab_prepost_dtls(l_indx).item_name :='X_DAYS_POST_JUL_83';
5862                tab_prepost_dtls(l_indx).item_value :=l_post30jun1983_days;
5863 
5864                l_indx := l_indx + 1;
5865 
5866             end if;
5867 
5868         /*  Bug# 2984390 - If ETP service date is entered then X_ETP_EMPLOYEE_START_DATE consists of ETP service
5869                            date otherwise Hiredate */
5870         /* Bug 4177679 - If ETP service date is entered then X_ETP_EMPLOYEE_START_DATE consists of ETP service
5871                            date otherwise legal employer start date */
5872        /*  Bug# 5367061 - If ETP service date is entered then X_ETP_EMPLOYEE_START_DATE consists of ETP service
5873                            date otherwise Hiredate, Fix made in 4177679 backed out. */
5874 
5875             g_le_etp_flag := 'Y' ;
5876 
5877              v_etp_service_date := to_char(l_etp_service_date,'DDMMYYYY');
5878       --------------------------------------------------------------------------------+
5879         -- fetch the user_entity_id for all the archive items and call procedure
5880         -- ff_archive_api.create_archive_item to arhive this value.
5881         --------------------------------------------------------------------------------+
5882        end if;
5883      end loop;
5884 
5885         tab_prepost_dtls(l_indx).item_name :='X_POST_JUN_83_UNTAXED_ASG_YTD';
5886         tab_prepost_dtls(l_indx).item_value :=0;                                 /* Bug #2075782 */
5887 
5888      create_extract_archive_details(p_assignment_action_id,
5889                                        'X_ETP_EMPLOYEE_START_DATE',
5890                                         v_etp_service_date);
5891 
5892 /* Begin Bug 6192381  For Termination Payments before Multiple ETP Payment Enhancement the new balances for each combination
5893 does not exist , therefore to find out the values difference of Old balance which has total values and sum of new balances
5894 is taken to archive the old values
5895 
5896 This value is then divided in Pre Post ratios and added to corresponding values based on input values Transitional ETP
5897 and Part of Previously Paid ETP .
5898 */
5899 
5900      l_old_etp_pay  := l_etp_pay_total - ( l_etp_pay_tran_no_ppetp + l_etp_pay_tran_ppetp    +
5901                                            l_etp_pay_no_tran_no_ppetp + l_etp_pay_no_tran_ppetp );
5902 
5903     If ( l_old_etp_pay > 0 ) then
5904 
5905           l_old_pre01jul1983_value  := round(l_old_etp_pay*l_pre01jul1983_ratio,2);
5906           l_old_post30jun1983_value := round(l_old_etp_pay*l_post30jun1983_ratio,2);
5907 
5908          If p_transitional_flag ='Y' and p_part_prev_etp_flag = 'N' then
5909            l_item_name_pre  := 'X_PRE_JUL_83_COMP_TRANS_NOT_PPTERM_ASG_YTD';
5910            l_item_name_post := 'X_POST_JUN_83_TAXED_TRANS_NOT_PPTERM_ASG_YTD';
5911          End if;
5912 
5913          If p_transitional_flag ='Y' and p_part_prev_etp_flag = 'Y' then
5914            l_item_name_pre  := 'X_PRE_JUL_83_COMP_TRANS_PPTERM_ASG_YTD';
5915            l_item_name_post := 'X_POST_JUN_83_TAXED_TRANS_PPTERM_ASG_YTD';
5916          End if;
5917 
5918          If p_transitional_flag ='N' and p_part_prev_etp_flag = 'N' then
5919            l_item_name_pre  := 'X_PRE_JUL_83_COMP_NOT_TRANS_NOT_PPTERM_ASG_YTD';
5920            l_item_name_post := 'X_POST_JUN_83_TAXED_NOT_TRANS_NOT_PPTERM_ASG_YTD';
5921          End if;
5922 
5923          If p_transitional_flag ='N' and p_part_prev_etp_flag = 'Y' then
5924            l_item_name_pre  := 'X_PRE_JUL_83_COMP_NOT_TRANS_PPTERM_ASG_YTD';
5925            l_item_name_post := 'X_POST_JUN_83_TAXED_NOT_TRANS_PPTERM_ASG_YTD';
5926          End if;
5927 
5928          for j in 1..tab_prepost_dtls.COUNT
5929          loop
5930 
5931              if tab_prepost_dtls(j).item_name = l_item_name_pre then
5932                 tab_prepost_dtls(j).item_value := tab_prepost_dtls(j).item_value + l_old_pre01jul1983_value ;
5933              end if;
5934 
5935              if tab_prepost_dtls(j).item_name = l_item_name_post then
5936                 tab_prepost_dtls(j).item_value := tab_prepost_dtls(j).item_value + l_old_post30jun1983_value ;
5937              end if;
5938          end loop;
5939 
5940     End if; /* End Bug 6192381 */
5941 
5942         for cnt in 1..tab_prepost_dtls.COUNT loop
5943            create_extract_archive_details(p_assignment_action_id,
5944                                           tab_prepost_dtls(cnt).item_name,
5945                                           tab_prepost_dtls(cnt).item_value);
5946         end loop;
5947 
5948 
5949      IF g_debug THEN
5950         hr_utility.set_location('End of archive_prepost_details',18);
5951      END if;
5952 
5953 exception
5954   when e_prepost_error then
5955      IF g_debug THEN
5956         hr_utility.set_location('error from pay_au_terminations.etp_prepost_ratios',20);
5957      END if;
5958   when others then
5959      IF g_debug THEN
5960         hr_utility.set_location('error in archive_prepost_details',21);
5961      END if;
5962   raise;
5963 end  archive_prepost_details;
5964 
5965 /* bug 14703826 - new procedure to archive ETP balances for
5966           1) Excluded
5967                 2) Excluded Part of Prev
5968                 3) Non Excluded
5969                 4) None Excluded Part of Prev
5970 
5971                 The sequence to archive is
5972                 1) find run results of ETP Taxable elements
5973                 2) Add run balances for each run results
5974                 3) Find Balance Adjustments by Lump Sum C Payments
5975                 4) Add adjusted ETP balances
5976                 5) Adjust etp balances according to etp payment date
5977                 6) Archive the adjusted balance values
5978 */
5979 procedure archive_prepost_details_2012
5980      (p_assignment_action_id    in   pay_assignment_actions.ASSIGNMENT_ACTION_ID%TYPE
5981      ,p_max_assignment_action_id in  pay_assignment_actions.ASSIGNMENT_ACTION_ID%TYPE --2610141
5982      ,p_registered_employer     in   pay_assignment_actions.TAX_UNIT_ID%TYPE --2610141
5983      ,p_legislation_code        in   pay_defined_balances.legislation_code%TYPE
5984      ,p_assignment_id           in   pay_assignment_actions.ASSIGNMENT_ID%type
5985      ,p_payroll_action_id       in   pay_payroll_actions.payroll_action_id%TYPE
5986      ,p_actual_termination_date in   per_periods_of_service.actual_termination_date%TYPE
5987      ,p_date_start              in   per_periods_of_service.date_start%TYPE
5988      ,p_year_start              in   pay_payroll_actions.effective_date%type
5989      ,p_year_end                in   pay_payroll_Actions.effective_date%type) is
5990 
5991   cursor c_get_etp_deduction is
5992     SELECT  pet.element_name
5993              ,to_char(ppa.effective_date,'DDMMYYYY') run_effective_date
5994              ,pac.assignment_action_id
5995              ,ppa.payroll_action_id
5996   FROM    pay_run_results prr
5997                  ,per_all_assignments_f paaf
5998          ,pay_assignment_actions pac
5999          ,pay_payroll_actions ppa
6000                  ,pay_payrolls_f papf
6001          ,pay_element_types_f pet
6002   WHERE   pac.assignment_id = p_assignment_id
6003   AND     pac.assignment_action_id = prr.assignment_action_id
6004     AND     pac.tax_unit_id = p_registered_employer
6005     AND     pac.assignment_id = paaf.assignment_id
6006     AND     paaf.assignment_id = p_assignment_id
6007   AND     pac.payroll_action_id = ppa.payroll_action_id
6008     AND     ppa.action_type             in ('R','Q','I','B','V')
6009     AND     ppa.payroll_id = papf.payroll_id
6010     AND     paaf.payroll_id = papf.payroll_id
6011   AND     ppa.effective_date between p_year_start and p_year_end
6012   AND     prr.status in ('P','PA')
6013   AND     prr.element_type_id = pet.element_type_id
6014   AND     pet.element_name IN ('ETP Taxable Payments Excluded','ETP Taxable Payments Non Excluded')
6015   AND     pet.legislation_code = 'AU'
6016   AND     ppa.effective_date between pet.effective_start_date and pet.effective_end_date
6017     AND     ppa.effective_date between paaf.effective_start_date and paaf.effective_end_date
6018   order by pet.element_name, pac.assignment_action_id;
6019 
6020   cursor  etp_prepayment
6021           (c_assignment_action_id pay_assignment_actions.assignment_action_id%type,
6022            c_payroll_action_id    pay_payroll_actions.payroll_action_id%type) is
6023         select  to_char(pppa.effective_date,'DDMMYYYY')
6024          from   pay_action_interlocks    pai
6025                ,pay_assignment_actions   pac
6026                ,pay_payroll_actions      ppa
6027                ,pay_assignment_actions   ppac
6028                ,pay_payroll_actions      pppa
6029         where   pac.payroll_action_id    = ppa.payroll_action_id
6030           and   pac.assignment_action_id = c_assignment_action_id
6031           and   pac.assignment_action_id = pai.locked_action_id
6032           and   ppa.payroll_action_id    = c_payroll_action_id
6033           and   ppac.assignment_action_id =pai.locking_Action_id
6034           and   pppa.payroll_Action_id   = ppac.payroll_Action_id
6035           and   ppa.effective_date      between p_year_start
6036                                             and p_year_end;
6037 
6038 cursor c_BA_BI (c_lsC_payments_def_bal_id number) is
6039       select   ppa.payroll_action_id
6040               ,pac.assignment_action_id
6041                             ,to_char(ppa.effective_date,'DDMMYYYY') run_effective_date
6042         from   per_assignments_f      paf
6043               ,pay_assignment_actions pac
6044               ,pay_payroll_actions    ppa
6045        where   pac.assignment_id     = p_assignment_id
6046          and   pac.tax_unit_id = p_registered_employer
6047          and   paf.assignment_id     = pac.assignment_id
6048          and   paf.assignment_id    = p_assignment_id
6049          and   ppa.action_type       in ('B','I')
6050          and   pac.payroll_action_id = ppa.payroll_action_id
6051          and   pac.action_status     = 'C'
6052          and   ppa.action_status     = 'C'
6053          and   ppa.payroll_id = paf.payroll_id
6054          and   ppa.date_earned between paf.effective_start_date and paf.effective_end_date
6055          and  (pay_balance_pkg.get_value(c_lsC_payments_def_bal_id, pac.assignment_action_id,p_registered_employer,null,null,null,null)) <> 0
6056          and   ppa.effective_date between p_year_start and p_year_end
6057                  order by pac.assignment_action_id;
6058 
6059      TYPE prepost_rec IS RECORD (payment_date varchar2(20),
6060                                                                  etp_paid varchar2(1),
6061                                  deduction_value number,
6062                                                                  taxable_value number,
6063                                                                  taxfree_value number,
6064                                                                  invalid_value number
6065                                                                  );
6066      TYPE prepost_type IS TABLE OF prepost_rec INDEX BY BINARY_INTEGER;
6067      tab_etp_excl_dtls prepost_type;
6068          tab_etp_excl_pp_dtls prepost_type;
6069          tab_etp_non_excl_dtls prepost_type;
6070          tab_etp_non_excl_pp_dtls prepost_type;
6071      tab_etp_excl_arch prepost_type;
6072          tab_etp_excl_pp_arch prepost_type;
6073          tab_etp_non_excl_arch prepost_type;
6074          tab_etp_non_excl_pp_arch prepost_type;
6075 
6076          type etp_bal_type is table of varchar2(100) index by binary_integer;
6077      tab_etp_bal_name etp_bal_type ;
6078 
6079          l_indx1                         NUMBER;
6080      l_indx2                         NUMBER;
6081      l_indx3                         NUMBER;
6082      l_indx4                         NUMBER;
6083          etp_payment_date        VARCHAR2(20);
6084          idx                              NUMBER;
6085          l_diff_payment_date_flag        BOOLEAN;
6086          too_many_etp_payments           EXCEPTION;
6087 
6088          l_lsC_post_83_amt_excl          NUMBER := 0;
6089          l_lsC_pre_83_amt_excl           NUMBER := 0;
6090          l_lsC_post_83_amt_excl_pp       NUMBER := 0;
6091          l_lsC_pre_83_amt_excl_pp        NUMBER := 0;
6092          l_lsC_post_83_amt_non_excl      NUMBER := 0;
6093          l_lsC_pre_83_amt_non_excl       NUMBER := 0;
6094          l_lsC_post_83_amt_non_excl_pp   NUMBER := 0;
6095          l_lsC_pre_83_amt_non_excl_pp    NUMBER := 0;
6096          l_inv_pay_amt_excl              NUMBER;
6097          l_inv_pay_amt_pp_excl           NUMBER;
6098      l_lump_sum_c_pre_83_amt         NUMBER;
6099      l_lump_sum_c_post_83_amt        NUMBER;
6100 
6101       l_result                       NUMBER;
6102             l_pre01jul1983_days            NUMBER;
6103       l_post30jun1983_days           NUMBER;
6104       l_pre01jul1983_ratio           NUMBER;
6105       l_post30jun1983_ratio          NUMBER;
6106       l_pre01jul1983_value           NUMBER;
6107       l_post30jun1983_value          NUMBER;
6108       l_etp_service_date             date;
6109       v_etp_service_date             VARCHAR2(20);
6110       l_le_etp_service_date          date;
6111             l_etp_paid                     varchar2(1) := 'Y';
6112             l_ex_assignment_action_id      NUMBER;
6113             l_ne_assignment_action_id      NUMBER;
6114 
6115 
6116 begin
6117      IF g_debug THEN
6118         hr_utility.set_location('Start of archive_prepost_details_2012',1);
6119         hr_utility.set_location('Start of p_assg_action_id '|| p_assignment_action_id,1);
6120      END if;
6121 
6122   l_indx1 := 1;
6123     l_indx2 := 1;
6124     l_indx3 := 1;
6125     l_indx4 := 1;
6126     l_ex_assignment_action_id := 0;
6127     l_ne_assignment_action_id := 0;
6128     tab_etp_excl_dtls.delete;
6129     tab_etp_excl_pp_dtls.delete;
6130     tab_etp_non_excl_dtls.delete;
6131     tab_etp_non_excl_pp_dtls.delete;
6132 
6133     p_etp_context_table(1).tax_unit_id := p_registered_employer;
6134 
6135     /* ETP on TERMINATION RUN RESULTS */
6136   FOR rec_deduction in c_get_etp_deduction LOOP
6137 
6138       if g_debug then
6139         hr_utility.set_location('archive_prepost_details_2012 run results',2);
6140                 hr_utility.set_location('rec_deduction.element_name '||rec_deduction.element_name,2);
6141                 hr_utility.set_location('rec_deduction.assignment_action_id '||rec_deduction.assignment_action_id,2);
6142                 hr_utility.set_location('rec_deduction.payroll_action_id '||rec_deduction.payroll_action_id,2);
6143         end if;
6144 
6145     IF rec_deduction.element_name = 'ETP Taxable Payments Excluded' THEN
6146           IF rec_deduction.assignment_action_id <> l_ex_assignment_action_id THEN  /* for multiple ETP on Termination entries in the same run */
6147 
6148                  p_etp_excl_result_table.delete;
6149 
6150          pay_balance_pkg.get_value
6151         (p_assignment_action_id     => rec_deduction.assignment_action_id
6152         ,p_defined_balance_lst  => p_etp_excl_bal_value_tab
6153         ,p_context_lst      => p_etp_context_table
6154         ,p_output_table         => p_etp_excl_result_table
6155         );
6156 
6157           IF p_etp_excl_result_table.count > 0 THEN
6158                 IF p_etp_excl_result_table(4).balance_value <> 0 THEN /* ETP Deductions Excluded */
6159 
6160                         open etp_prepayment (rec_deduction.assignment_action_id, rec_deduction.payroll_action_id);
6161                         fetch etp_prepayment into etp_payment_date;
6162                         if etp_prepayment%FOUND then
6163                            tab_etp_excl_dtls(l_indx1).payment_date := etp_payment_date;
6164                              tab_etp_excl_dtls(l_indx1).etp_paid := 'Y';
6165                         else
6166                              tab_etp_excl_dtls(l_indx1).payment_date := rec_deduction.run_effective_date;
6167                              tab_etp_excl_dtls(l_indx1).etp_paid := 'N';
6168                         end if;
6169                         close etp_prepayment;
6170 
6171                            tab_etp_excl_dtls(l_indx1).taxfree_value := p_etp_excl_result_table(1).balance_value;
6172                              tab_etp_excl_dtls(l_indx1).taxable_value := p_etp_excl_result_table(2).balance_value;
6173                              tab_etp_excl_dtls(l_indx1).invalid_value := p_etp_excl_result_table(3).balance_value;
6174                              tab_etp_excl_dtls(l_indx1).deduction_value := p_etp_excl_result_table(4).balance_value;
6175 
6176                          l_indx1 := l_indx1 + 1;
6177 
6178 
6179         ELSIF p_etp_excl_result_table(5).balance_value <> 0 THEN /* ETP Deductions Excluded Part of Prev */
6180 
6181                         open etp_prepayment (rec_deduction.assignment_action_id, rec_deduction.payroll_action_id);
6182                         fetch etp_prepayment into etp_payment_date;
6183                         if etp_prepayment%FOUND then
6184                            tab_etp_excl_pp_dtls(l_indx2).payment_date := etp_payment_date;
6185                              tab_etp_excl_pp_dtls(l_indx2).etp_paid := 'Y';
6186                         else
6187                            tab_etp_excl_pp_dtls(l_indx2).payment_date := rec_deduction.run_effective_date;
6188                              tab_etp_excl_pp_dtls(l_indx2).etp_paid := 'N';
6189                         end if;
6190                         close etp_prepayment;
6191 
6192                              tab_etp_excl_pp_dtls(l_indx2).taxfree_value := p_etp_excl_result_table(1).balance_value;
6193                              tab_etp_excl_pp_dtls(l_indx2).taxable_value := p_etp_excl_result_table(2).balance_value;
6194                              tab_etp_excl_pp_dtls(l_indx2).invalid_value := p_etp_excl_result_table(3).balance_value;
6195                              tab_etp_excl_pp_dtls(l_indx2).deduction_value := p_etp_excl_result_table(5).balance_value;
6196 
6197                          l_indx2 := l_indx2 + 1;
6198 
6199              ELSIF  p_etp_excl_result_table(2).balance_value <> 0 THEN /* Death Benefit Type = T or D for taxfree only */
6200 
6201                         open etp_prepayment (rec_deduction.assignment_action_id, rec_deduction.payroll_action_id);
6202                         fetch etp_prepayment into etp_payment_date;
6203                         if etp_prepayment%FOUND then
6204                            tab_etp_excl_dtls(l_indx1).payment_date := etp_payment_date;
6205                              tab_etp_excl_dtls(l_indx1).etp_paid := 'Y';
6206                         else
6207                              tab_etp_excl_dtls(l_indx1).payment_date := rec_deduction.run_effective_date;
6208                              tab_etp_excl_dtls(l_indx1).etp_paid := 'N';
6209                         end if;
6210                         close etp_prepayment;
6211 
6212                            tab_etp_excl_dtls(l_indx1).taxfree_value := p_etp_excl_result_table(1).balance_value;
6213                              tab_etp_excl_dtls(l_indx1).taxable_value := p_etp_excl_result_table(2).balance_value;
6214                              tab_etp_excl_dtls(l_indx1).invalid_value := p_etp_excl_result_table(3).balance_value;
6215                              tab_etp_excl_dtls(l_indx1).deduction_value := p_etp_excl_result_table(4).balance_value;
6216 
6217                          l_indx1 := l_indx1 + 1;
6218 
6219              END IF;
6220          END IF;
6221 
6222          l_ex_assignment_action_id := rec_deduction.assignment_action_id;
6223 
6224       END IF;
6225         END IF;
6226 
6227         IF rec_deduction.element_name = 'ETP Taxable Payments Non Excluded' THEN
6228           IF rec_deduction.assignment_action_id <> l_ne_assignment_action_id THEN /* for multiple ETP on Termination entries in the same run */
6229 
6230                  p_etp_non_excl_result_table.delete;
6231 
6232          pay_balance_pkg.get_value
6233         (p_assignment_action_id     => rec_deduction.assignment_action_id
6234         ,p_defined_balance_lst  => p_etp_non_excl_bal_value_tab
6235         ,p_context_lst      => p_etp_context_table
6236         ,p_output_table         => p_etp_non_excl_result_table
6237         );
6238 
6239               IF p_etp_non_excl_result_table.count > 0 THEN
6240                   IF p_etp_non_excl_result_table(3).balance_value <> 0 THEN /* ETP Deductions Non Excluded */
6241 
6242                         open etp_prepayment (rec_deduction.assignment_action_id, rec_deduction.payroll_action_id);
6243                         fetch etp_prepayment into etp_payment_date;
6244                         if etp_prepayment%FOUND then
6245                            tab_etp_non_excl_dtls(l_indx3).payment_date := etp_payment_date;
6246                              tab_etp_non_excl_dtls(l_indx3).etp_paid := 'Y';
6247                         else
6248                              tab_etp_non_excl_dtls(l_indx3).payment_date := rec_deduction.run_effective_date;
6249                              tab_etp_non_excl_dtls(l_indx3).etp_paid := 'N';
6250                         end if;
6251                         close etp_prepayment;
6252 
6253                            tab_etp_non_excl_dtls(l_indx3).taxfree_value := p_etp_non_excl_result_table(1).balance_value;
6254                              tab_etp_non_excl_dtls(l_indx3).taxable_value := p_etp_non_excl_result_table(2).balance_value;
6255                              tab_etp_non_excl_dtls(l_indx3).deduction_value := p_etp_non_excl_result_table(3).balance_value;
6256 
6257                          l_indx3 := l_indx3 + 1;
6258 
6259                 ELSIF p_etp_non_excl_result_table(4).balance_value <> 0 THEN /* ETP Deductions Non Excluded Part of Prev*/
6260 
6261                         open etp_prepayment (rec_deduction.assignment_action_id, rec_deduction.payroll_action_id);
6262                         fetch etp_prepayment into etp_payment_date;
6263                         if etp_prepayment%FOUND then
6264                            tab_etp_non_excl_pp_dtls(l_indx4).payment_date := etp_payment_date;
6265                              tab_etp_non_excl_pp_dtls(l_indx4).etp_paid := 'Y';
6266                         else
6267                              tab_etp_non_excl_pp_dtls(l_indx4).payment_date := rec_deduction.run_effective_date;
6268                              tab_etp_non_excl_pp_dtls(l_indx4).etp_paid := 'N';
6269                         end if;
6270                         close etp_prepayment;
6271 
6272                            tab_etp_non_excl_pp_dtls(l_indx4).taxfree_value := p_etp_non_excl_result_table(1).balance_value;
6273                              tab_etp_non_excl_pp_dtls(l_indx4).taxable_value := p_etp_non_excl_result_table(2).balance_value;
6274                              tab_etp_non_excl_pp_dtls(l_indx4).deduction_value := p_etp_non_excl_result_table(4).balance_value;
6275 
6276                          l_indx4 := l_indx4 + 1;
6277 
6278                 END IF;
6279                 END IF;
6280 
6281               l_ne_assignment_action_id := rec_deduction.assignment_action_id;
6282 
6283           END IF;
6284         END IF;
6285 
6286     END LOOP;
6287 
6288    /* BALANCE ADJUSTMENT/INITIALISATION */
6289     l_indx1 := nvl(tab_etp_excl_dtls.last,0) + 1;
6290         l_indx2 := nvl(tab_etp_excl_pp_dtls.last,0) + 1;
6291       FOR r_BA_BI in c_BA_BI(p_etp_excl_bal_value_tab(2).defined_balance_id) LOOP
6292 
6293       if g_debug then
6294         hr_utility.set_location('archive_prepost_details_2012 BA/BI',3);
6295                 hr_utility.set_location('r_BA_BI.run_effective_date '||r_BA_BI.run_effective_date,3);
6296                 hr_utility.set_location('r_BA_BI.assignment_action_id '||r_BA_BI.assignment_action_id,3);
6297                 hr_utility.set_location('r_BA_BI.payroll_action_id '||r_BA_BI.payroll_action_id,3);
6298         end if;
6299 
6300                  p_etp_excl_result_table.delete;
6301 
6302          pay_balance_pkg.get_value
6303         (p_assignment_action_id     => r_BA_BI.assignment_action_id
6304         ,p_defined_balance_lst  => p_etp_excl_bal_value_tab
6305         ,p_context_lst      => p_etp_context_table
6306         ,p_output_table         => p_etp_excl_result_table
6307         );
6308 
6309             if p_etp_excl_result_table.count > 0 then
6310                 if p_etp_excl_result_table(4).balance_value <> 0 then
6311 
6312                         open etp_prepayment (r_BA_BI.assignment_action_id, r_BA_BI.payroll_action_id);
6313                         fetch etp_prepayment into etp_payment_date;
6314                         if etp_prepayment%FOUND then
6315                            tab_etp_excl_dtls(l_indx1).payment_date := etp_payment_date;
6316                         else
6317                              tab_etp_excl_dtls(l_indx1).payment_date := r_BA_BI.run_effective_date;
6318                         end if;
6319                         close etp_prepayment;
6320 
6321                         tab_etp_excl_dtls(l_indx1).etp_paid := 'Y';
6322 
6323                        tab_etp_excl_dtls(l_indx1).taxfree_value := p_etp_excl_result_table(1).balance_value;
6324                          tab_etp_excl_dtls(l_indx1).taxable_value := p_etp_excl_result_table(2).balance_value;
6325                          tab_etp_excl_dtls(l_indx1).deduction_value := p_etp_excl_result_table(4).balance_value;
6326                          l_indx1 := l_indx1 + 1;
6327 
6328                 elsif p_etp_excl_result_table(5).balance_value <> 0 then
6329 
6330                             open etp_prepayment (r_BA_BI.assignment_action_id, r_BA_BI.payroll_action_id);
6331                             fetch etp_prepayment into etp_payment_date;
6332                             if etp_prepayment%FOUND then
6333                                tab_etp_excl_dtls(l_indx2).payment_date := etp_payment_date;
6334                             else
6335                                  tab_etp_excl_dtls(l_indx2).payment_date := r_BA_BI.run_effective_date;
6336                             end if;
6337                             close etp_prepayment;
6338 
6339                             tab_etp_excl_dtls(l_indx1).etp_paid := 'Y';
6340 
6341                         tab_etp_excl_pp_dtls(l_indx2).taxfree_value := p_etp_excl_result_table(1).balance_value;
6342                           tab_etp_excl_pp_dtls(l_indx2).taxable_value := p_etp_excl_result_table(2).balance_value;
6343                           tab_etp_excl_pp_dtls(l_indx2).deduction_value := p_etp_excl_result_table(5).balance_value;
6344                             l_indx2 := l_indx2 + 1;
6345 
6346                     elsif  p_etp_excl_result_table(2).balance_value <> 0 THEN
6347 
6348                             open etp_prepayment (r_BA_BI.assignment_action_id, r_BA_BI.payroll_action_id);
6349                             fetch etp_prepayment into etp_payment_date;
6350                             if etp_prepayment%FOUND then
6351                                tab_etp_excl_dtls(l_indx1).payment_date := etp_payment_date;
6352                             else
6353                                  tab_etp_excl_dtls(l_indx1).payment_date := r_BA_BI.run_effective_date;
6354                             end if;
6355                             close etp_prepayment;
6356 
6357                              tab_etp_excl_dtls(l_indx1).etp_paid := 'Y';
6358 
6359                            tab_etp_excl_dtls(l_indx1).taxfree_value := p_etp_excl_result_table(1).balance_value;
6360                              tab_etp_excl_dtls(l_indx1).taxable_value := p_etp_excl_result_table(2).balance_value;
6361                              tab_etp_excl_dtls(l_indx1).deduction_value := p_etp_excl_result_table(4).balance_value;
6362                              l_indx1 := l_indx1 + 1;
6363                     end if;
6364                 end if;
6365 
6366         END LOOP;
6367 
6368 
6369         l_indx3 := nvl(tab_etp_non_excl_dtls.last,0) + 1;
6370         l_indx4 := nvl(tab_etp_non_excl_pp_dtls.last,0) + 1;
6371       FOR r_BA_BI in c_BA_BI(p_etp_non_excl_bal_value_tab(2).defined_balance_id) LOOP
6372 
6373       if g_debug then
6374         hr_utility.set_location('archive_prepost_details_2012 BA/BI',4);
6375                 hr_utility.set_location('r_BA_BI.run_effective_date '||r_BA_BI.run_effective_date,4);
6376                 hr_utility.set_location('r_BA_BI.assignment_action_id '||r_BA_BI.assignment_action_id,4);
6377                 hr_utility.set_location('r_BA_BI.payroll_action_id '||r_BA_BI.payroll_action_id,4);
6378         end if;
6379                  p_etp_non_excl_result_table.delete;
6380 
6381          pay_balance_pkg.get_value
6382         (p_assignment_action_id     => r_BA_BI.assignment_action_id
6383         ,p_defined_balance_lst  => p_etp_non_excl_bal_value_tab
6384         ,p_context_lst      => p_etp_context_table
6385         ,p_output_table         => p_etp_non_excl_result_table
6386         );
6387 
6388             if p_etp_non_excl_result_table.count > 0 then
6389                 if p_etp_non_excl_result_table(3).balance_value <> 0 then
6390 
6391                         open etp_prepayment (r_BA_BI.assignment_action_id, r_BA_BI.payroll_action_id);
6392                         fetch etp_prepayment into etp_payment_date;
6393                         if etp_prepayment%FOUND then
6394                            tab_etp_non_excl_dtls(l_indx3).payment_date := etp_payment_date;
6395                         else
6396                              tab_etp_non_excl_dtls(l_indx3).payment_date := r_BA_BI.run_effective_date;
6397                         end if;
6398                         close etp_prepayment;
6399 
6400                          tab_etp_non_excl_dtls(l_indx3).etp_paid := 'Y';
6401 
6402                        tab_etp_non_excl_dtls(l_indx3).taxfree_value := p_etp_non_excl_result_table(1).balance_value;
6403                          tab_etp_non_excl_dtls(l_indx3).taxable_value := p_etp_non_excl_result_table(2).balance_value;
6404                          tab_etp_non_excl_dtls(l_indx3).deduction_value := p_etp_non_excl_result_table(3).balance_value;
6405                          l_indx3 := l_indx3 + 1;
6406 
6407                 elsif p_etp_non_excl_result_table(4).balance_value <> 0 then
6408 
6409                             open etp_prepayment (r_BA_BI.assignment_action_id, r_BA_BI.payroll_action_id);
6410                             fetch etp_prepayment into etp_payment_date;
6411                             if etp_prepayment%FOUND then
6412                                tab_etp_non_excl_pp_dtls(l_indx4).payment_date := etp_payment_date;
6413                             else
6414                                  tab_etp_non_excl_pp_dtls(l_indx4).payment_date := r_BA_BI.run_effective_date;
6415                             end if;
6416                             close etp_prepayment;
6417 
6418                             tab_etp_non_excl_pp_dtls(l_indx4).etp_paid := 'Y';
6419 
6420                         tab_etp_non_excl_pp_dtls(l_indx4).taxfree_value := p_etp_non_excl_result_table(1).balance_value;
6421                           tab_etp_non_excl_pp_dtls(l_indx4).taxable_value := p_etp_non_excl_result_table(2).balance_value;
6422                           tab_etp_non_excl_pp_dtls(l_indx4).deduction_value := p_etp_non_excl_result_table(4).balance_value;
6423                             l_indx4 := l_indx4 + 1;
6424 
6425                     end if;
6426                 end if;
6427         END LOOP;
6428 
6429 ----------------
6430 -- ETP EXCLUDED Archive
6431 ----------------
6432     if tab_etp_excl_dtls.count > 0 then
6433         idx := 1;
6434         l_diff_payment_date_flag := FALSE;
6435         tab_etp_excl_arch.delete;
6436         FOR i in tab_etp_excl_dtls.FIRST .. tab_etp_excl_dtls.LAST LOOP
6437             if g_debug then
6438               hr_utility.set_location('ETP EXCLUDED DETAILS',11);
6439               hr_utility.trace('tab_etp_excl_dtls('||i||').payment_date = '||tab_etp_excl_dtls(i).payment_date);
6440                 hr_utility.trace('tab_etp_excl_dtls('||i||').etp_paid = '||tab_etp_excl_dtls(i).etp_paid);
6441                 hr_utility.trace('tab_etp_excl_dtls('||i||').deduction_value = '||nvl(tab_etp_excl_dtls(i).deduction_value,0));
6442                 hr_utility.trace('tab_etp_excl_dtls('||i||').taxable_value = '||nvl(tab_etp_excl_dtls(i).taxable_value,0));
6443                 hr_utility.trace('tab_etp_excl_dtls('||i||').taxfree_value = '||nvl(tab_etp_excl_dtls(i).taxfree_value,0));
6444                 hr_utility.trace('tab_etp_excl_dtls('||i||').invalid_value = '||nvl(tab_etp_excl_dtls(i).invalid_value,0));
6445             end if;
6446 
6447             /* summing the amounts by prepayment date */
6448             IF tab_etp_excl_arch.count > 0 then
6449                 l_diff_payment_date_flag := TRUE;
6450                 FOR j in tab_etp_excl_arch.FIRST .. tab_etp_excl_arch.LAST LOOP
6451 
6452                     if tab_etp_excl_arch(j).payment_date = tab_etp_excl_dtls(i).payment_date then
6453                       if tab_etp_excl_dtls(i).etp_paid = 'N' THEN
6454                              tab_etp_excl_arch(j).etp_paid := tab_etp_excl_dtls(i).etp_paid;
6455                         end if;
6456                         tab_etp_excl_arch(j).deduction_value := tab_etp_excl_arch(j).deduction_value + nvl(tab_etp_excl_dtls(i).deduction_value,0);
6457                         tab_etp_excl_arch(j).taxable_value := tab_etp_excl_arch(j).taxable_value + nvl(tab_etp_excl_dtls(i).taxable_value,0);
6458                         tab_etp_excl_arch(j).taxfree_value := tab_etp_excl_arch(j).taxfree_value + nvl(tab_etp_excl_dtls(i).taxfree_value,0);
6459                         tab_etp_excl_arch(j).invalid_value := tab_etp_excl_arch(j).taxfree_value + nvl(tab_etp_excl_dtls(i).invalid_value,0);
6460                         l_diff_payment_date_flag := FALSE;
6461                     end if;
6462 
6463                 END LOOP;
6464             ELSE
6465                     tab_etp_excl_arch(idx).payment_date := tab_etp_excl_dtls(i).payment_date;
6466                     tab_etp_excl_arch(idx).etp_paid := tab_etp_excl_dtls(i).etp_paid;
6467                     tab_etp_excl_arch(idx).deduction_value := nvl(tab_etp_excl_dtls(i).deduction_value,0);
6468                     tab_etp_excl_arch(idx).taxable_value := nvl(tab_etp_excl_dtls(i).taxable_value,0);
6469                     tab_etp_excl_arch(idx).taxfree_value := nvl(tab_etp_excl_dtls(i).taxfree_value,0);
6470                     tab_etp_excl_arch(idx).invalid_value := nvl(tab_etp_excl_dtls(i).invalid_value,0);
6471             END IF;
6472 
6473             IF l_diff_payment_date_flag THEN
6474                         idx := idx+1;
6475                         tab_etp_excl_arch(idx).payment_date := tab_etp_excl_dtls(i).payment_date;
6476                         tab_etp_excl_arch(idx).etp_paid := tab_etp_excl_dtls(i).etp_paid;
6477                         tab_etp_excl_arch(idx).deduction_value := nvl(tab_etp_excl_dtls(i).deduction_value,0);
6478                         tab_etp_excl_arch(idx).taxable_value := nvl(tab_etp_excl_dtls(i).taxable_value,0);
6479                         tab_etp_excl_arch(idx).taxfree_value := nvl(tab_etp_excl_dtls(i).taxfree_value,0);
6480                         tab_etp_excl_arch(idx).invalid_value := nvl(tab_etp_excl_dtls(i).invalid_value,0);
6481         END IF;
6482 
6483         END LOOP;
6484     end if;
6485 
6486     if tab_etp_excl_arch.count > 0 then
6487         l_inv_pay_amt_excl := 0;
6488         g_le_etp_flag := 'Y';
6489         FOR i in 1 .. tab_etp_excl_arch.count LOOP
6490             if g_debug then
6491                 hr_utility.trace('tab_etp_excl_arch-----------------------------');
6492                 hr_utility.trace('tab_etp_excl_arch('||i||').payment_date = '||tab_etp_excl_arch(i).payment_date);
6493                     hr_utility.trace('tab_etp_excl_arch('||i||').etp_paid = '||tab_etp_excl_arch(i).etp_paid);
6494                     hr_utility.trace('tab_etp_excl_arch('||i||').deduction_value = '||tab_etp_excl_arch(i).deduction_value);
6495                     hr_utility.trace('tab_etp_excl_arch('||i||').taxable_value = '||tab_etp_excl_arch(i).taxable_value);
6496                     hr_utility.trace('tab_etp_excl_arch('||i||').taxfree_value = '||tab_etp_excl_arch(i).taxfree_value);
6497                     hr_utility.trace('tab_etp_excl_arch('||i||').invalid_value = '||tab_etp_excl_arch(i).invalid_value);
6498               hr_utility.trace('tab_etp_excl_arch-----------------------------');
6499            end if;
6500 
6501        if i < 6 then
6502                      create_extract_archive_details(p_assignment_action_id,
6503                                           'X_ETP_EXCL_PAYMENT_DATE_'||i,
6504                                           tab_etp_excl_arch(i).payment_date);
6505 
6506            create_extract_archive_details(p_assignment_action_id,
6507                                           'X_ETP_EXCL_PAID_'||i,
6508                                           tab_etp_excl_arch(i).etp_paid);
6509                      if tab_etp_excl_arch(i).etp_paid = 'N' then
6510                         l_etp_paid := 'N';
6511                      end if;
6512 
6513            create_extract_archive_details(p_assignment_action_id,
6514                                           'X_ETP_DED_EXCL_'||i,
6515                                           tab_etp_excl_arch(i).deduction_value);
6516 
6517            create_extract_archive_details(p_assignment_action_id,
6518                                           'X_POST_JUN_83_TAXED_EXCL_'||i,
6519                                           tab_etp_excl_arch(i).taxable_value);
6520            l_lsC_post_83_amt_excl := l_lsC_post_83_amt_excl + tab_etp_excl_arch(i).taxable_value;
6521 
6522            create_extract_archive_details(p_assignment_action_id,
6523                                           'X_PRE_JUL_83_COMP_EXCL_'||i,
6524                                           tab_etp_excl_arch(i).taxfree_value);
6525                      l_lsC_pre_83_amt_excl := l_lsC_pre_83_amt_excl + tab_etp_excl_arch(i).taxfree_value;
6526 
6527                      create_extract_archive_details(p_assignment_action_id,
6528                                           'X_INV_PAY_EXCL_'||i,
6529                                           tab_etp_excl_arch(i).invalid_value);
6530            l_inv_pay_amt_excl := l_inv_pay_amt_excl + tab_etp_excl_arch(i).invalid_value;
6531 
6532        else
6533                  raise too_many_etp_payments;
6534              end if;
6535     END LOOP;
6536           create_extract_archive_details(p_assignment_action_id,
6537                                        'X_PRE_JUL_83_COMP_EXCL_ASG_YTD',
6538                                         l_lsC_pre_83_amt_excl);
6539 
6540               create_extract_archive_details(p_assignment_action_id,
6541                                        'X_POST_JUN_83_TAXED_EXCL_ASG_YTD',
6542                                         l_lsC_post_83_amt_excl);
6543 
6544               create_extract_archive_details(p_assignment_action_id,
6545                                        'X_INV_PAY_EXCL_ASG_YTD',
6546                                         l_inv_pay_amt_excl);
6547 
6548     end if;
6549 
6550 ----------------
6551 -- ETP EXCLUDED PP Archive
6552 ----------------
6553   if tab_etp_excl_pp_dtls.count > 0 then
6554         idx := 1;
6555         l_diff_payment_date_flag := FALSE;
6556         tab_etp_excl_pp_arch.delete;
6557       FOR i in tab_etp_excl_pp_dtls.FIRST .. tab_etp_excl_pp_dtls.LAST LOOP
6558             if g_debug then
6559               hr_utility.set_location('ETP EXCLUDED Part of Prev DETAILS',21);
6560               hr_utility.trace('tab_etp_excl_pp_dtls('||i||').payment_date = '||tab_etp_excl_pp_dtls(i).payment_date);
6561                 hr_utility.trace('tab_etp_excl_pp_dtls('||i||').etp_paid = '||tab_etp_excl_pp_dtls(i).etp_paid);
6562                 hr_utility.trace('tab_etp_excl_pp_dtls('||i||').deduction_value = '||nvl(tab_etp_excl_pp_dtls(i).deduction_value,0));
6563             hr_utility.trace('tab_etp_excl_pp_dtls('||i||').taxable_value = '||nvl(tab_etp_excl_pp_dtls(i).taxable_value,0));
6564                 hr_utility.trace('tab_etp_excl_pp_dtls('||i||').taxfree_value = '||nvl(tab_etp_excl_pp_dtls(i).taxfree_value,0));
6565                 hr_utility.trace('tab_etp_excl_pp_dtls('||i||').invalid_value = '||nvl(tab_etp_excl_pp_dtls(i).invalid_value,0));
6566             end if;
6567 
6568             IF tab_etp_excl_pp_arch.count > 0 then
6569                 l_diff_payment_date_flag := TRUE;
6570                 FOR j in tab_etp_excl_pp_arch.FIRST .. tab_etp_excl_pp_arch.LAST LOOP
6571 
6572                     if tab_etp_excl_pp_arch(j).payment_date = tab_etp_excl_pp_dtls(i).payment_date then
6573                         if tab_etp_excl_pp_dtls(i).etp_paid = 'N' then
6574                              tab_etp_excl_pp_arch(j).etp_paid := tab_etp_excl_pp_dtls(i).etp_paid;
6575                         end if;
6576                         tab_etp_excl_pp_arch(j).deduction_value := tab_etp_excl_pp_arch(j).deduction_value + nvl(tab_etp_excl_pp_dtls(i).deduction_value,0);
6577                         tab_etp_excl_pp_arch(j).taxable_value := tab_etp_excl_pp_arch(j).taxable_value + nvl(tab_etp_excl_pp_dtls(i).taxable_value,0);
6578                         tab_etp_excl_pp_arch(j).taxfree_value := tab_etp_excl_pp_arch(j).taxfree_value + nvl(tab_etp_excl_pp_dtls(i).taxfree_value,0);
6579                         tab_etp_excl_pp_arch(j).invalid_value := tab_etp_excl_pp_arch(j).taxfree_value + nvl(tab_etp_excl_pp_dtls(i).invalid_value,0);
6580                         l_diff_payment_date_flag := FALSE;
6581                     end if;
6582 
6583                 END LOOP;
6584             ELSE
6585                     tab_etp_excl_pp_arch(idx).payment_date := tab_etp_excl_pp_dtls(i).payment_date;
6586                     tab_etp_excl_pp_arch(idx).etp_paid := tab_etp_excl_pp_dtls(i).etp_paid;
6587                     tab_etp_excl_pp_arch(idx).deduction_value := nvl(tab_etp_excl_pp_dtls(i).deduction_value,0);
6588                     tab_etp_excl_pp_arch(idx).taxable_value := nvl(tab_etp_excl_pp_dtls(i).taxable_value,0);
6589                     tab_etp_excl_pp_arch(idx).taxfree_value := nvl(tab_etp_excl_pp_dtls(i).taxfree_value,0);
6590                     tab_etp_excl_pp_arch(idx).invalid_value := nvl(tab_etp_excl_pp_dtls(i).invalid_value,0);
6591             END IF;
6592 
6593             IF l_diff_payment_date_flag THEN
6594                         idx := idx+1;
6595                         tab_etp_excl_pp_arch(idx).payment_date := tab_etp_excl_pp_dtls(i).payment_date;
6596                         tab_etp_excl_pp_arch(idx).etp_paid := tab_etp_excl_pp_dtls(i).etp_paid;
6597                         tab_etp_excl_pp_arch(idx).deduction_value := nvl(tab_etp_excl_pp_dtls(i).deduction_value,0);
6598                         tab_etp_excl_pp_arch(idx).taxable_value := nvl(tab_etp_excl_pp_dtls(i).taxable_value,0);
6599                         tab_etp_excl_pp_arch(idx).taxfree_value := nvl(tab_etp_excl_pp_dtls(i).taxfree_value,0);
6600                         tab_etp_excl_pp_arch(idx).invalid_value := nvl(tab_etp_excl_pp_dtls(i).invalid_value,0);
6601         END IF;
6602 
6603         END LOOP;
6604     end if;
6605 
6606   if tab_etp_excl_pp_arch.count > 0 then
6607         l_inv_pay_amt_pp_excl := 0;
6608         g_le_etp_flag := 'Y';
6609         FOR i in 1 .. tab_etp_excl_pp_arch.count LOOP
6610             if g_debug then
6611               hr_utility.trace('tab_etp_excl_pp_arch-----------------------------');
6612                   hr_utility.trace('tab_etp_excl_pp_arch('||i||').payment_date = '||tab_etp_excl_pp_arch(i).payment_date);
6613                     hr_utility.trace('tab_etp_excl_pp_arch('||i||').etp_paid = '||tab_etp_excl_pp_arch(i).etp_paid);
6614                     hr_utility.trace('tab_etp_excl_pp_arch('||i||').deduction_value = '||tab_etp_excl_pp_arch(i).deduction_value);
6615                     hr_utility.trace('tab_etp_excl_pp_arch('||i||').taxable_value = '||tab_etp_excl_pp_arch(i).taxable_value);
6616                     hr_utility.trace('tab_etp_excl_pp_arch('||i||').taxfree_value = '||tab_etp_excl_pp_arch(i).taxfree_value);
6617                     hr_utility.trace('tab_etp_excl_pp_arch('||i||').invalid_value = '||tab_etp_excl_pp_arch(i).invalid_value);
6618               hr_utility.trace('tab_etp_excl_pp_arch-----------------------------');
6619             end if;
6620 
6621                 if i < 6 then
6622 
6623            create_extract_archive_details(p_assignment_action_id,
6624                                           'X_ETP_EXCL_PP_PAYMENT_DATE_'||i,
6625                                           tab_etp_excl_pp_arch(i).payment_date);
6626 
6627            create_extract_archive_details(p_assignment_action_id,
6628                                           'X_ETP_EXCL_PP_PAID_'||i,
6629                                           tab_etp_excl_pp_arch(i).etp_paid);
6630                      if tab_etp_excl_pp_arch(i).etp_paid = 'N' then
6631                         l_etp_paid := 'N';
6632                      end if;
6633 
6634            create_extract_archive_details(p_assignment_action_id,
6635                                           'X_ETP_DED_EXCL_PP_'||i,
6636                                           tab_etp_excl_pp_arch(i).deduction_value);
6637 
6638            create_extract_archive_details(p_assignment_action_id,
6639                                           'X_POST_JUN_83_TAXED_EXCL_PP_'||i,
6640                                           tab_etp_excl_pp_arch(i).taxable_value);
6641            l_lsC_post_83_amt_excl_pp := l_lsC_post_83_amt_excl_pp + tab_etp_excl_pp_arch(i).taxable_value;
6642 
6643            create_extract_archive_details(p_assignment_action_id,
6644                                           'X_PRE_JUL_83_COMP_EXCL_PP_'||i,
6645                                           tab_etp_excl_pp_arch(i).taxfree_value);
6646            l_lsC_pre_83_amt_excl_pp := l_lsC_pre_83_amt_excl_pp + tab_etp_excl_pp_arch(i).taxfree_value;
6647 
6648                      create_extract_archive_details(p_assignment_action_id,
6649                                           'X_INV_PAY_EXCL_PP_'||i,
6650                                           tab_etp_excl_pp_arch(i).invalid_value);
6651                      l_inv_pay_amt_pp_excl := l_inv_pay_amt_pp_excl + tab_etp_excl_pp_arch(i).invalid_value;
6652 
6653        else
6654                  raise too_many_etp_payments;
6655              end if;
6656 
6657         END LOOP;
6658           create_extract_archive_details(p_assignment_action_id,
6659                                        'X_PRE_JUL_83_COMP_EXCL_PP_ASG_YTD',
6660                                         l_lsC_pre_83_amt_excl_pp);
6661 
6662               create_extract_archive_details(p_assignment_action_id,
6663                                        'X_POST_JUN_83_TAXED_EXCL_PP_ASG_YTD',
6664                                         l_lsC_post_83_amt_excl_pp);
6665 
6666               create_extract_archive_details(p_assignment_action_id,
6667                                        'X_INV_PAY_EXCL_PP_ASG_YTD',
6668                                         l_inv_pay_amt_pp_excl);
6669 
6670     end if;
6671 
6672 ----------------
6673 -- ETP NON EXCLUDED Archive
6674 ----------------
6675   if tab_etp_non_excl_dtls.count > 0 then
6676         idx := 1;
6677         l_diff_payment_date_flag := FALSE;
6678         tab_etp_non_excl_arch.delete;
6679       FOR i in tab_etp_non_excl_dtls.FIRST .. tab_etp_non_excl_dtls.LAST LOOP
6680             if g_debug then
6681               hr_utility.set_location('ETP NON EXCLUDED DETAILS',31);
6682                 hr_utility.trace('tab_etp_non_excl_dtls('||i||').payment_date = '||tab_etp_non_excl_dtls(i).payment_date);
6683                 hr_utility.trace('tab_etp_non_excl_dtls('||i||').etp_paid = '||tab_etp_non_excl_dtls(i).etp_paid);
6684                 hr_utility.trace('tab_etp_non_excl_dtls('||i||').deduction_value = '||nvl(tab_etp_non_excl_dtls(i).deduction_value,0));
6685                 hr_utility.trace('tab_etp_non_excl_dtls('||i||').taxable_value = '||nvl(tab_etp_non_excl_dtls(i).taxable_value,0));
6686                 hr_utility.trace('tab_etp_non_excl_dtls('||i||').taxfree_value = '||nvl(tab_etp_non_excl_dtls(i).taxfree_value,0));
6687             end if;
6688 
6689             IF tab_etp_non_excl_arch.count > 0 then
6690                 l_diff_payment_date_flag := TRUE;
6691                 FOR j in tab_etp_non_excl_arch.FIRST .. tab_etp_non_excl_arch.LAST LOOP
6692 
6693                     if tab_etp_non_excl_arch(j).payment_date = tab_etp_non_excl_dtls(i).payment_date then
6694                         if tab_etp_non_excl_dtls(i).etp_paid = 'N' then
6695                            tab_etp_non_excl_arch(j).etp_paid := tab_etp_non_excl_dtls(i).etp_paid;
6696                         end if;
6697                         tab_etp_non_excl_arch(j).deduction_value := tab_etp_non_excl_arch(j).deduction_value + nvl(tab_etp_non_excl_dtls(i).deduction_value,0);
6698                         tab_etp_non_excl_arch(j).taxable_value := tab_etp_non_excl_arch(j).taxable_value + nvl(tab_etp_non_excl_dtls(i).taxable_value,0);
6699                         tab_etp_non_excl_arch(j).taxfree_value := tab_etp_non_excl_arch(j).taxfree_value + nvl(tab_etp_non_excl_dtls(i).taxfree_value,0);
6700                         l_diff_payment_date_flag := FALSE;
6701                     end if;
6702 
6703                 END LOOP;
6704             ELSE
6705                     tab_etp_non_excl_arch(idx).payment_date := tab_etp_non_excl_dtls(i).payment_date;
6706                     tab_etp_non_excl_arch(idx).etp_paid := tab_etp_non_excl_dtls(i).etp_paid;
6707                     tab_etp_non_excl_arch(idx).deduction_value := nvl(tab_etp_non_excl_dtls(i).deduction_value,0);
6708                     tab_etp_non_excl_arch(idx).taxable_value := nvl(tab_etp_non_excl_dtls(i).taxable_value,0);
6709                     tab_etp_non_excl_arch(idx).taxfree_value := nvl(tab_etp_non_excl_dtls(i).taxfree_value,0);
6710             END IF;
6711 
6712             IF l_diff_payment_date_flag THEN
6713                         idx := idx+1;
6714                         tab_etp_non_excl_arch(idx).payment_date := tab_etp_non_excl_dtls(i).payment_date;
6715                         tab_etp_non_excl_arch(idx).etp_paid := tab_etp_non_excl_dtls(i).etp_paid;
6716                         tab_etp_non_excl_arch(idx).deduction_value := nvl(tab_etp_non_excl_dtls(i).deduction_value,0);
6717                         tab_etp_non_excl_arch(idx).taxable_value := nvl(tab_etp_non_excl_dtls(i).taxable_value,0);
6718                         tab_etp_non_excl_arch(idx).taxfree_value := nvl(tab_etp_non_excl_dtls(i).taxfree_value,0);
6719         END IF;
6720 
6721         END LOOP;
6722     end if;
6723 
6724   if tab_etp_non_excl_arch.count > 0 then
6725         g_le_etp_flag := 'Y';
6726         FOR i in 1 .. tab_etp_non_excl_arch.count LOOP
6727           if g_debug then
6728             hr_utility.trace('tab_etp_non_excl_arch-----------------------------');
6729               hr_utility.trace('tab_etp_non_excl_arch('||i||').payment_date = '||tab_etp_non_excl_arch(i).payment_date);
6730                 hr_utility.trace('tab_etp_non_excl_arch('||i||').etp_paid = '||tab_etp_non_excl_arch(i).etp_paid);
6731                 hr_utility.trace('tab_etp_non_excl_arch('||i||').deduction_value = '||tab_etp_non_excl_arch(i).deduction_value);
6732                 hr_utility.trace('tab_etp_non_excl_arch('||i||').taxable_value = '||tab_etp_non_excl_arch(i).taxable_value);
6733                 hr_utility.trace('tab_etp_non_excl_arch('||i||').taxfree_value = '||tab_etp_non_excl_arch(i).taxfree_value);
6734             hr_utility.trace('tab_etp_non_excl_arch-----------------------------');
6735           end if;
6736 
6737             if i < 6 then
6738 
6739            create_extract_archive_details(p_assignment_action_id,
6740                                           'X_ETP_NE_PAYMENT_DATE_'||i,
6741                                           tab_etp_non_excl_arch(i).payment_date);
6742 
6743            create_extract_archive_details(p_assignment_action_id,
6744                                           'X_ETP_NE_PAID_'||i,
6745                                           tab_etp_non_excl_arch(i).etp_paid);
6746                      if tab_etp_non_excl_arch(i).etp_paid = 'N' then
6747                         l_etp_paid := 'N';
6748                      end if;
6749 
6750            create_extract_archive_details(p_assignment_action_id,
6751                                           'X_ETP_DED_NE_'||i,
6752                                           tab_etp_non_excl_arch(i).deduction_value);
6753 
6754            create_extract_archive_details(p_assignment_action_id,
6755                                           'X_POST_JUN_83_TAXED_NE_'||i,
6756                                           tab_etp_non_excl_arch(i).taxable_value);
6757            l_lsC_post_83_amt_non_excl := l_lsC_post_83_amt_non_excl + tab_etp_non_excl_arch(i).taxable_value;
6758 
6759            create_extract_archive_details(p_assignment_action_id,
6760                                           'X_PRE_JUL_83_COMP_NE_'||i,
6761                                           tab_etp_non_excl_arch(i).taxfree_value);
6762            l_lsC_pre_83_amt_non_excl := l_lsC_pre_83_amt_non_excl + tab_etp_non_excl_arch(i).taxfree_value;
6763 
6764        else
6765                  raise too_many_etp_payments;
6766              end if;
6767 
6768         END LOOP;
6769           create_extract_archive_details(p_assignment_action_id,
6770                                        'X_PRE_JUL_83_COMP_NON_EXCL_ASG_YTD',
6771                                         l_lsC_pre_83_amt_non_excl);
6772 
6773               create_extract_archive_details(p_assignment_action_id,
6774                                        'X_POST_JUN_83_TAXED_NON_EXCL_ASG_YTD',
6775                                         l_lsC_post_83_amt_non_excl);
6776     end if;
6777 
6778 ---------------
6779 -- ETP NON EXCLUDED PP Archive
6780 ----------------
6781   if tab_etp_non_excl_pp_dtls.count > 0 then
6782         idx := 1;
6783         l_diff_payment_date_flag := FALSE;
6784         tab_etp_non_excl_pp_arch.delete;
6785       FOR i in tab_etp_non_excl_pp_dtls.FIRST .. tab_etp_non_excl_pp_dtls.LAST LOOP
6786             if g_debug then
6787               hr_utility.set_location('ETP NON EXCLUDED Part of Prev DETAILS',41);
6788                 hr_utility.trace('tab_etp_non_excl_pp_dtls('||i||').payment_date = '||tab_etp_non_excl_pp_dtls(i).payment_date);
6789                 hr_utility.trace('tab_etp_non_excl_pp_dtls('||i||').etp_paid = '||tab_etp_non_excl_pp_dtls(i).etp_paid);
6790                 hr_utility.trace('tab_etp_non_excl_pp_dtls('||i||').deduction_value = '||nvl(tab_etp_non_excl_pp_dtls(i).deduction_value,0));
6791                 hr_utility.trace('tab_etp_non_excl_pp_dtls('||i||').taxable_value = '||nvl(tab_etp_non_excl_pp_dtls(i).taxable_value,0));
6792                 hr_utility.trace('tab_etp_non_excl_pp_dtls('||i||').taxfree_value = '||nvl(tab_etp_non_excl_pp_dtls(i).taxfree_value,0));
6793             end if;
6794 
6795             IF tab_etp_non_excl_pp_arch.count > 0 then
6796                 l_diff_payment_date_flag := TRUE;
6797                 FOR j in tab_etp_non_excl_pp_arch.FIRST .. tab_etp_non_excl_pp_arch.LAST LOOP
6798 
6799                     if tab_etp_non_excl_pp_arch(j).payment_date = tab_etp_non_excl_pp_dtls(i).payment_date then
6800                         if tab_etp_non_excl_pp_dtls(i).etp_paid = 'N' then
6801                              tab_etp_non_excl_pp_arch(j).etp_paid := tab_etp_non_excl_pp_dtls(i).etp_paid;
6802                         end if;
6803                         tab_etp_non_excl_pp_arch(j).deduction_value := tab_etp_non_excl_pp_arch(j).deduction_value + nvl(tab_etp_non_excl_pp_dtls(i).deduction_value,0);
6804                         tab_etp_non_excl_pp_arch(j).taxable_value := tab_etp_non_excl_pp_arch(j).taxable_value + nvl(tab_etp_non_excl_pp_dtls(i).taxable_value,0);
6805                         tab_etp_non_excl_pp_arch(j).taxfree_value := tab_etp_non_excl_pp_arch(j).taxfree_value + nvl(tab_etp_non_excl_pp_dtls(i).taxfree_value,0);
6806                         l_diff_payment_date_flag := FALSE;
6807                     end if;
6808 
6809                 END LOOP;
6810             ELSE
6811                     tab_etp_non_excl_pp_arch(idx).payment_date := tab_etp_non_excl_pp_dtls(i).payment_date;
6812                     tab_etp_non_excl_pp_arch(idx).etp_paid := tab_etp_non_excl_pp_dtls(i).etp_paid;
6813                     tab_etp_non_excl_pp_arch(idx).deduction_value := nvl(tab_etp_non_excl_pp_dtls(i).deduction_value,0);
6814                     tab_etp_non_excl_pp_arch(idx).taxable_value := nvl(tab_etp_non_excl_pp_dtls(i).taxable_value,0);
6815                     tab_etp_non_excl_pp_arch(idx).taxfree_value := nvl(tab_etp_non_excl_pp_dtls(i).taxfree_value,0);
6816             END IF;
6817 
6818             IF l_diff_payment_date_flag THEN
6819                         idx := idx+1;
6820                         tab_etp_non_excl_pp_arch(idx).payment_date := tab_etp_non_excl_pp_dtls(i).payment_date;
6821                         tab_etp_non_excl_pp_arch(idx).etp_paid := tab_etp_non_excl_pp_dtls(i).etp_paid;
6822                         tab_etp_non_excl_pp_arch(idx).deduction_value := nvl(tab_etp_non_excl_pp_dtls(i).deduction_value,0);
6823                         tab_etp_non_excl_pp_arch(idx).taxable_value := nvl(tab_etp_non_excl_pp_dtls(i).taxable_value,0);
6824                         tab_etp_non_excl_pp_arch(idx).taxfree_value := nvl(tab_etp_non_excl_pp_dtls(i).taxfree_value,0);
6825         END IF;
6826 
6827         END LOOP;
6828     end if;
6829 
6830   if tab_etp_non_excl_pp_arch.count > 0 then
6831         g_le_etp_flag := 'Y';
6832         FOR i in 1 .. tab_etp_non_excl_pp_arch.count LOOP
6833           if g_debug then
6834             hr_utility.trace('tab_etp_non_excl_pp_arch-----------------------------');
6835                 hr_utility.trace('tab_etp_non_excl_pp_arch('||i||').payment_date = '||tab_etp_non_excl_pp_arch(i).payment_date);
6836                 hr_utility.trace('tab_etp_non_excl_pp_arch('||i||').etp_paid = '||tab_etp_non_excl_pp_arch(i).etp_paid);
6837                 hr_utility.trace('tab_etp_non_excl_pp_arch('||i||').deduction_value = '||tab_etp_non_excl_pp_arch(i).deduction_value);
6838                 hr_utility.trace('tab_etp_non_excl_pp_arch('||i||').taxable_value = '||tab_etp_non_excl_pp_arch(i).taxable_value);
6839                 hr_utility.trace('tab_etp_non_excl_pp_arch('||i||').taxfree_value = '||tab_etp_non_excl_pp_arch(i).taxfree_value);
6840             hr_utility.trace('tab_etp_non_excl_pp_arch-----------------------------');
6841         end if;
6842 
6843             if i < 6 then
6844 
6845            create_extract_archive_details(p_assignment_action_id,
6846                                           'X_ETP_NE_PP_PAYMENT_DATE_'||i,
6847                                           tab_etp_non_excl_pp_arch(i).payment_date);
6848 
6849            create_extract_archive_details(p_assignment_action_id,
6850                                           'X_ETP_NE_PP_PAID_'||i,
6851                                           tab_etp_non_excl_pp_arch(i).etp_paid);
6852                      if tab_etp_non_excl_pp_arch(i).etp_paid = 'N' then
6853                         l_etp_paid := 'N';
6854                      end if;
6855 
6856            create_extract_archive_details(p_assignment_action_id,
6857                                           'X_ETP_DED_NE_PP_'||i,
6858                                           tab_etp_non_excl_pp_arch(i).deduction_value);
6859 
6860            create_extract_archive_details(p_assignment_action_id,
6861                                           'X_POST_JUN_83_TAXED_NE_PP_'||i,
6862                                           tab_etp_non_excl_pp_arch(i).taxable_value);
6863            l_lsC_post_83_amt_non_excl_pp := l_lsC_post_83_amt_non_excl_pp + tab_etp_non_excl_pp_arch(i).taxable_value;
6864 
6865            create_extract_archive_details(p_assignment_action_id,
6866                                           'X_PRE_JUL_83_COMP_NE_PP_'||i,
6867                                           tab_etp_non_excl_pp_arch(i).taxfree_value);
6868            l_lsC_pre_83_amt_non_excl_pp := l_lsC_pre_83_amt_non_excl_pp + tab_etp_non_excl_pp_arch(i).taxfree_value;
6869 
6870        else
6871                  raise too_many_etp_payments;
6872              end if;
6873 
6874         END LOOP;
6875           create_extract_archive_details(p_assignment_action_id,
6876                                        'X_PRE_JUL_83_COMP_NON_EXCL_PP_ASG_YTD',
6877                                         l_lsC_pre_83_amt_non_excl_pp);
6878 
6879               create_extract_archive_details(p_assignment_action_id,
6880                                        'X_POST_JUN_83_TAXED_NON_EXCL_PP_ASG_YTD',
6881                                         l_lsC_post_83_amt_non_excl_pp);
6882     end if;
6883 
6884 
6885             l_lump_sum_c_post_83_amt := l_lsC_post_83_amt_excl+l_lsC_post_83_amt_excl_pp+l_lsC_post_83_amt_non_excl+l_lsC_post_83_amt_non_excl_pp;
6886           create_extract_archive_details(p_assignment_action_id,
6887                                        'X_POST_JUN_83_TAXED_ASG_YTD',
6888                                         l_lump_sum_c_post_83_amt);
6889 
6890             l_lump_sum_c_pre_83_amt := l_lsC_pre_83_amt_excl+l_lsC_pre_83_amt_excl_pp+l_lsC_pre_83_amt_non_excl+l_lsC_pre_83_amt_non_excl_pp;
6891           create_extract_archive_details(p_assignment_action_id,
6892                                        'X_PRE_JUL_83_COMPONENT_ASG_YTD',
6893                                         l_lump_sum_c_pre_83_amt);
6894 
6895 
6896 
6897   IF g_debug THEN
6898       hr_utility.set_location('calling pay_au_terminations.etp_prepost_ratios ',51);
6899   END if;
6900 
6901           l_result := pay_au_terminations.etp_prepost_ratios(
6902                      p_assignment_id              -- number                  in
6903                     ,p_date_start                 -- date                    in
6904                     ,p_actual_termination_date    -- date                    in
6905                     ,'N'                          -- Bug#2819479 Flag to check whether this function called by Termination Form.
6906                     ,l_pre01jul1983_days          -- number                  out
6907                     ,l_post30jun1983_days         -- number                  out
6908                     ,l_pre01jul1983_ratio         -- number                  out
6909                     ,l_post30jun1983_ratio        -- number                  out
6910                     ,l_etp_service_date           -- date                    out
6911                     ,l_le_etp_service_date);      -- date                    out
6912 
6913                     v_etp_service_date := to_char(l_etp_service_date,'DDMMYYYY');
6914 
6915               create_extract_archive_details(p_assignment_action_id,
6916                                        'X_ETP_EMPLOYEE_START_DATE',
6917                                         v_etp_service_date);
6918 
6919               create_extract_archive_details(p_assignment_action_id,
6920                                        'X_DAYS_PRE_JUL_83',
6921                                         l_pre01jul1983_days);
6922 
6923               create_extract_archive_details(p_assignment_action_id,
6924                                        'X_DAYS_POST_JUL_83',
6925                                         l_post30jun1983_days);
6926 
6927           create_extract_archive_details(p_assignment_action_id,
6928                                        'X_ETP_ON_TERMINATION_PAID',
6929                                        l_etp_paid );
6930 
6931 
6932      IF g_debug THEN
6933         hr_utility.set_location('End of archive_prepost_details_2012',99);
6934      END if;
6935 
6936 exception
6937   when too_many_etp_payments then
6938      IF g_debug THEN
6939         hr_utility.set_location('error for too many etp payments',999);
6940      END if;
6941   when others then
6942      IF g_debug THEN
6943         hr_utility.set_location('error in archive_prepost_details',999);
6944      END if;
6945   raise;
6946 
6947 end  archive_prepost_details_2012;
6948 
6949 
6950 ---------------------------------------------------------------------------------------+
6951 -- this procedure archives all the data related to employer.
6952 ---------------------------------------------------------------------------------------+
6953 
6954 Procedure archive_employer_details
6955           (p_business_group_id    in hr_organization_units.business_group_id%type,
6956            p_max_assignment_action_id  in pay_assignment_actions.assignment_action_id%TYPE, --2610141
6957            p_registered_employer  in hr_organization_units.organization_id%type,
6958        p_payroll_action_id    in pay_payroll_actions.payroll_action_id%type,
6959        p_assignment_action_id in pay_assignment_actions.assignment_action_id%type,
6960        p_assignment_id        in pay_assignment_actions.assignment_id%type,
6961        p_year_start           in pay_payroll_actions.effective_date%type,
6962        p_year_end             in pay_payroll_Actions.effective_date%type) is
6963 
6964 
6965 
6966   l_group_act_no     hr_organization_information.org_information1%type;
6967   l_business_name        hr_organization_information.org_information3%type;
6968   l_trading_name         hr_organization_information.org_information4%type;
6969   l_abn                  hr_organization_information.org_information12%type;
6970   l_branch_number        hr_organization_information.org_information13%type;
6971   l_contact_name         hr_organization_information.org_information7%type;
6972   l_tel_number           per_addresses.telephone_number_1%type;
6973   l_address_1            hr_locations.address_line_1%type;
6974   l_address_2            hr_locations.address_line_2%type;
6975   l_address_3            hr_locations.address_line_3%type;
6976   l_suburb               hr_locations.town_or_city%type;
6977   l_state                hr_locations.region_1%type;
6978   l_postcode             hr_locations.postal_code%type;
6979   l_country              fnd_territories_tl.territory_short_name%type;
6980   l_signatory            hr_organization_information.org_information8%type;
6981   e_employer_nf          EXCEPTION;
6982 
6983 
6984 ---------------------------------------------------------------------------------+
6985   --   cursor to get the employer details
6986   --   changed for bug# 1783245
6987 ---------------------------------------------------------------------------------+
6988 
6989   cursor   employer_details
6990       is
6991   select  hoi.org_information1      group_act_no
6992          ,hoi.org_information3      business_name
6993          ,hoi.org_information4      trading_name
6994          ,hoi.org_information12     abn
6995          ,hoi.org_information13     branch_no
6996          ,papcont.first_name || ' ' || papcont.last_name
6997                                     contact_name
6998          ,hoi.org_information14     telephone_number
6999          ,papsign.first_name || ' ' || papsign.last_name
7000                                     signatory
7001          ,hlc.address_line_1        address_1
7002          ,hlc.address_line_2        address_2
7003          ,hlc.address_line_3        address_3
7004          ,hlc.town_or_city          suburb
7005          ,hlc.region_1              state
7006          ,hlc.postal_code           postcode
7007          ,ftl.territory_short_name  country
7008    from   hr_organization_information hoi
7009          ,hr_locations            hlc
7010          ,fnd_territories_tl          ftl
7011          ,hr_organization_units       hou
7012          ,per_all_people_f            papcont
7013          ,per_all_people_f            papsign
7014    where  hou.business_group_id       = p_business_group_id
7015      and  hou.organization_id         = p_registered_employer
7016      and  hou.organization_id         = hoi.organization_id
7017      and  hoi.org_information_context = 'AU_LEGAL_EMPLOYER'
7018      and  ftl.territory_code          = hlc.country
7019      and  ftl.language                = userenv('LANG')
7020      and  hlc.location_id             = hou.location_id
7021      and  papcont.person_id           = hoi.org_information7
7022      and  papcont.effective_start_date    = (select max(effective_start_date)
7023                                           from  per_all_people_f p
7024                                          where  papcont.person_id=p.person_id)
7025      and  papsign.person_id           = hoi.org_information8
7026      and  papsign.effective_start_date    = (select max(effective_start_date)
7027                                           from  per_all_people_f p
7028                                          where  papsign.person_id=p.person_id);
7029 
7030 
7031 Begin
7032    IF g_debug THEN
7033        hr_utility.set_location('Start of archive employer details ..',0);
7034    END if;
7035    open employer_details ;
7036    fetch employer_details into
7037        l_group_act_no
7038       ,l_business_name
7039       ,l_trading_name
7040       ,l_abn
7041       ,l_branch_number
7042       ,l_contact_name
7043       ,l_tel_number
7044       ,l_signatory
7045       ,l_address_1
7046       ,l_address_2
7047       ,l_address_3
7048       ,l_suburb
7049       ,l_state
7050       ,l_postcode
7051       ,l_country ;
7052   if employer_details%found then
7053     close employer_details;
7054     IF g_debug THEN
7055         hr_utility.set_location('Creating archive Item  X_EMPLOYER_BUSINESS_NAME',1);
7056     END if;
7057     create_extract_archive_details(p_assignment_action_id,
7058                                    'X_EMPLOYER_BUSINESS_NAME',
7059                                    l_business_name);
7060     IF g_debug THEN
7061         hr_utility.set_location('Creating archive Item  X_EMPLOYER_GROUP_ACT_NO',2);
7062     END if;
7063         create_extract_archive_details(p_assignment_action_id,
7064                                    'X_EMPLOYER_GROUP_ACT_NO',
7065                                     l_group_act_no);
7066     IF g_debug THEN
7067         hr_utility.set_location('Creating archive Item  X_EMPLOYER_TRADING_NAME',3);
7068     END if;
7069     create_extract_archive_details(p_assignment_action_id,
7070                                    'X_EMPLOYER_TRADING_NAME',
7071                                     l_trading_name);
7072     IF g_debug THEN
7073         hr_utility.set_location('Creating archive Item  X_EMPLOYER_BRANCH_NUMBER',114);
7074     END if;
7075        create_extract_archive_details(p_assignment_action_id,
7076                                     'X_EMPLOYER_BRANCH_NUMBER',
7077                                    l_branch_number);
7078     IF g_debug THEN
7079         hr_utility.set_location('Creating archive Item  X_EMPLOYER_ABN',114);
7080     END if;
7081     create_extract_archive_details(p_assignment_action_id,
7082                                    'X_EMPLOYER_ABN',
7083                                    l_abn);
7084     IF g_debug THEN
7085         hr_utility.set_location('Creating archive Item  X_EMPLOYER_CONTACT_NAME',5);
7086     END if;
7087     create_extract_archive_details(p_assignment_action_id,
7088                                    'X_EMPLOYER_CONTACT_NAME',
7089                                     l_contact_name);
7090     IF g_debug THEN
7091         hr_utility.set_location('Creating archive Item  X_EMPLOYER_CONTACT_TELEPHONE',6);
7092     END if;
7093         create_extract_archive_details(p_assignment_action_id,
7094                                    'X_EMPLOYER_CONTACT_TELEPHONE',
7095                                    l_tel_number);
7096     IF g_debug THEN
7097         hr_utility.set_location('Creating archive Item  X_EMPLOYER_ADDRESS_1',7);
7098     END if;
7099         create_extract_archive_details(p_assignment_action_id,
7100                                    'X_EMPLOYER_ADDRESS_1',
7101                                    l_address_1);
7102     IF g_debug THEN
7103         hr_utility.set_location('Creating archive Item  X_EMPLOYER_ADDRESS_2',8);
7104     END if;
7105         create_extract_archive_details(p_assignment_action_id,
7106                                    'X_EMPLOYER_ADDRESS_2',
7107                                    l_address_2);
7108     IF g_debug THEN
7109         hr_utility.set_location('Creating archive Item  X_EMPLOYER_ADDRESS_3',8);
7110     END if;
7111         create_extract_archive_details(p_assignment_action_id,
7112                                    'X_EMPLOYER_ADDRESS_3',
7113                                    l_address_3);
7114     IF g_debug THEN
7115         hr_utility.set_location('Creating archive Item  X_EMPLOYER_SUBURB',9);
7116     END if;
7117        create_extract_archive_details(p_assignment_action_id,
7118                                     'X_EMPLOYER_SUBURB',
7119                                     l_suburb);
7120     IF g_debug THEN
7121         hr_utility.set_location('Creating archive Item  X_EMPLOYER_STATE',10);
7122     END if;
7123         create_extract_archive_details(p_assignment_action_id,
7124                                    'X_EMPLOYER_STATE',
7125                                     l_state);
7126     IF g_debug THEN
7127         hr_utility.set_location('Creating archive Item X_EMPLOYER_POSTCODE',11);
7128     END if;
7129        create_extract_archive_details(p_assignment_action_id,
7130                                    'X_EMPLOYER_POSTCODE',
7131                                    l_postcode);
7132     IF g_debug THEN
7133         hr_utility.set_location('Creating archive Item X_EMPLOYER_COUNTRY',12);
7134     END if;
7135        create_extract_archive_details(p_assignment_action_id,
7136                                     'X_EMPLOYER_COUNTRY',
7137                                    l_country);
7138     --------------------------------
7139     -- Added for Bug No :1789886
7140     --------------------------------
7141     IF g_debug THEN
7142         hr_utility.set_location('Creating archive Item X_PAYMENT_SUMMARY_SIGNATORY',12);
7143     END if;
7144         create_extract_archive_details(p_assignment_action_id,
7145                                    'X_PAYMENT_SUMMARY_SIGNATORY',
7146                                    l_signatory);
7147 
7148     --------------------------------
7149     -- Added for Bug No :1764017
7150     --------------------------------
7151     archive_union_name
7152     (p_assignment_id
7153     ,p_assignment_action_id
7154     ,p_max_assignment_action_id    --2610141
7155     ,p_registered_employer         --2610141
7156     ,p_year_start
7157     ,p_year_end
7158     );
7159   else
7160    close  employer_details;
7161     raise e_employer_nf;
7162   end if;
7163 exception
7164   when e_employer_nf then
7165     IF g_debug THEN
7166         hr_utility.set_location('No employer Details found for the assigment id  ',20);
7167     END if;
7168   when others then
7169     IF g_debug THEN
7170         hr_utility.set_location('Error in archive_employer_details ',99);
7171     END if;
7172     raise;
7173 End archive_employer_details ;
7174 
7175 ---------------------------------------------------------------------------------------+
7176 -- this procedure archives all the data related to employee.
7177 ---------------------------------------------------------------------------------------+
7178 
7179 Procedure archive_employee_details
7180       (p_business_group_id    in hr_organization_units.business_group_id%type,
7181        p_registered_employer  in hr_organization_units.organization_id%type,
7182        p_payroll_action_id    in pay_payroll_actions.payroll_action_id%type,
7183        p_assignment_action_id in pay_assignment_actions.assignment_action_id%type,
7184        p_assignment_id        in pay_assignment_actions.assignment_id%type,
7185        p_year_start           in pay_payroll_actions.effective_date%type,
7186        p_year_end             in pay_payroll_Actions.effective_date%type,
7187        p_end_date_flag        in varchar2,
7188        p_fbt_year_start       IN pay_payroll_Actions.effective_date%type) is /*Bug# 4653934*/
7189 
7190   l_tfn_no               pay_element_entry_values_f.screen_entry_value%type;
7191   l_first_name           per_all_people_f.first_name%type;
7192   l_middle_name          per_all_people_f.middle_names%type;
7193   l_surname              per_all_people_f.last_name%type;
7194   l_address_1            hr_locations.address_line_1%type;
7195   l_address_2            hr_locations.address_line_2%type;
7196   l_address_3            hr_locations.address_line_3%type;
7197   l_suburb               hr_locations.town_or_city%type;
7198   l_state                hr_locations.region_1%type;
7199   l_postcode             hr_locations.postal_code%type;
7200   l_country              fnd_territories_tl.territory_short_name%type;
7201   l_start_date           VARCHAR2(20);
7202   l_termination_date     VARCHAR2(20);
7203   l_dob                  VARCHAR2(20);
7204   l_asgmnt_loc           hr_locations.location_code%type;
7205   l_emp_no               per_all_people_f.employee_number%type;
7206   l_payroll              pay_all_payrolls_f.payroll_name%type;
7207   l_emp_type             per_all_people_f.current_employee_flag%type;
7208   l_address_date_from    date;
7209   l_date_earned          pay_payroll_actions.date_earned%type;
7210   l_effective_date       date; -- Bug3263659
7211   l_actual_termination_date per_periods_of_service.actual_termination_date%type;
7212   l_final_process_date   date; -- Bug3263659
7213   l_fpd_archive          VARCHAR2(20); -- Bug3098353
7214   l_le_start_date   VARCHAR2(20);
7215   l_le_end_date   VARCHAR2(20); -- Bug 2610141
7216   l_termination_type     VARCHAR2(4); -- Bug 8315198
7217 
7218   l_address_style        per_addresses.style%type;      -- Bug 5364017
7219   l_add_country          per_addresses.country%type;    -- Bug 5364017
7220 
7221   e_employee_nf          EXCEPTION;
7222 
7223   l_le_etp_service_date varchar2(20); /* Bug 4177679 */
7224   l_le_etp_start_date varchar2(20); /*Bug 5075662 */
7225 
7226   ---------------------------------------------------------------------------------+
7227   --      cursor to get the employee details
7228   ---------------------------------------------------------------------------------+
7229 
7230   /* Bug 1973978 -- If the Termination date of the employee is greater than the last date of the current financial
7231                     year then the employee should be treated as an Current Employee */
7232   /* Bug 2512431 -- Removed to_date for p_year_end in the select statement for emp_type  */
7233   /* Bug 2977533 - When a new address is created thro SS applications ,2 rows are created in
7234                 per_addresses table for the same primary address;the previous address is end dated.
7235                 This can also be simulated thro apps if a primary addresses is end dated and a new
7236                 primary address is created after the end date
7237                 The cursor fetches 2 rows and picks the old address first.To get the latest address
7238                 order by clause has been added to pick the latest row first  */
7239 
7240 
7241  /*Bug3019374 - Cursor employee_details broken up into 2 cursor -> cursor  employee_details
7242  and cursor tfn_number*/
7243 
7244 cursor tfn_number(c_assignment_id per_all_assignments.assignment_id%TYPE,
7245                   c_date_earned pay_payroll_actions.date_earned%TYPE)
7246 is
7247 select pev.screen_entry_value            tfn_no
7248 from
7249            pay_element_types_f          pet,
7250            pay_input_values_f           piv,
7251            pay_element_links_f          pel,
7252            pay_element_entries_f        pee,
7253            pay_element_entry_values_f   pev
7254 where      pet.element_name            = 'Tax Information'
7255       and  piv.name                    = 'Tax File Number'
7256       and  pet.element_type_id         = piv.element_type_id
7257       and  pet.element_type_id         = pel.element_type_id
7258       and  pel.element_link_id         = pee.element_link_id
7259       and  pee.element_entry_id        = pev.element_entry_id
7260       and  piv.input_value_id          = pev.input_value_id
7261       and  pee.assignment_id           = c_assignment_id
7262       and  c_date_earned between pel.effective_start_date and pel.effective_end_date
7263       and  c_date_earned between pev.effective_start_date and pev.effective_end_date
7264       and  c_date_earned between pet.effective_start_date and pet.effective_end_date
7265       and  c_date_earned between  pee.effective_start_date and pee.effective_end_date
7266       and  c_date_earned between  piv.effective_start_date and piv.effective_end_date;
7267 
7268 
7269  /*Bug3019374 - Cursor broken up into 2 cursor -> cursor  employee_details
7270  and cursor tfn_number*/
7271  /* Bug 4299506 - Join in subquery modified for Archiving details of employees
7272     terminated in previous years */
7273  /* Bug 7242551 - Modified sub query to fetch date earned based on max(action_sequence)
7274                   and not max(payroll_action_id)
7275  */
7276  /* Bug 8315198 - Modified cursor for fetching the termination type of the employee */
7277   cursor   employee_details
7278       is
7279     select  distinct
7280           pap.first_name                             first_name
7281          ,substr(pap.middle_names, 1,
7282           decode(instr(pap.middle_names,' '),
7283           0, 60, instr(pap.middle_names,' ',1)-1))    middle_name --Bug 10064551
7284          ,pap.last_name                              surname
7285          ,pad.address_line1                          address_1
7286          ,pad.address_line2                          address_2
7287          ,pad.address_line3                          address_3
7288          ,pad.town_or_city                           suburb
7289          ,pad.region_1                               state
7290          ,pad.postal_code                            postcode
7291          ,fta.territory_short_name                   country
7292          ,pad.style                                  address_style      -- Bug 5364017
7293          ,pad.country                                address_country    -- Bug 5364017
7294          ,to_char(pps.date_start,'DDMMYYYY')         start_date
7295          ,nvl(to_char(pps.actual_termination_date,
7296                      'DDMMYYYY'),'31124712')        termination_date
7297          ,pps.final_process_date                    final_process_date -- Bug3263659
7298          ,to_char(pap.date_of_birth,'DDMMYYYY')     dob
7299          ,hlc.location_code                         asgmnt_loc
7300          ,pap.employee_number                       emp_no
7301          ,decode(pps.actual_termination_date,null,'C',decode(sign(pps.actual_termination_date - p_year_end),1,'C','T')) emp_type         /* Bug #1973978 */
7302          ,pad.date_from
7303          ,ppa1.date_earned
7304          ,ppa1.effective_date               -- Bug3263659
7305          ,pps.actual_termination_date
7306          ,to_char(paaf.effective_start_date,'DDMMYYYY')
7307          ,to_char(paa.effective_end_date,'DDMMYYYY')                               -- Bug 2610141
7308          ,pps.pds_information1                      termination_type      --Bug 8315198
7309    from   hr_organization_information  hoi,
7310           hr_organization_units        hou,
7311           hr_soft_coding_keyflex       hsc,
7312           hr_locations                 hlc, /* Bug No : 2263587 */
7313           per_all_assignments_f        paa,
7314           per_all_assignments_f        paaf, /* Bug : 2610141 */
7315           per_all_people_f             pap,
7316           per_addresses                pad,
7317           fnd_territories_tl           fta,
7318           per_periods_of_service       pps,
7319           pay_payroll_actions          ppa,
7320           pay_assignment_actions       pac,
7321           pay_payroll_actions          ppa1, /* Bug# 2448441 */
7322           pay_assignment_actions       pac1  /* Bug# 2448441 */
7323    where  hou.business_group_id       = p_business_group_id
7324      and  hou.organization_id         = p_registered_employer
7325      and  ppa.action_type             = 'X'
7326      and  hou.organization_id         = hoi.organization_id
7327      and  hoi.org_information_context = 'AU_LEGAL_EMPLOYER'
7328      and  hou.business_group_id       = pap.business_group_id
7329      and  hsc.soft_coding_keyflex_id  = paa.soft_coding_keyflex_id
7330      and  paa.location_id             = hlc.location_id(+)
7331      and  to_char(hou.organization_id)= hsc.segment1
7332      and  pap.person_id               = paa.person_id
7333      and  pap.person_id               = pad.person_id(+)
7334      and  pad.primary_flag(+)         = 'Y' /*Added for bug 2774577*/
7335      and  fta.territory_code(+)       = pad.country
7336      and  fta.language(+)             = userenv('LANG')
7337      and  pap.person_id               = pps.person_id
7338      and pps.period_of_service_id     = paa.period_of_service_id /* Bug#2786146 */
7339      and  ppa.payroll_action_id       = pac.payroll_action_id
7340      and  ppa.payroll_action_id       = p_payroll_action_id
7341      and  paa.assignment_id           = pac.assignment_id
7342      and  paa.assignment_id           = p_assignment_id
7343      and  paaf.assignment_id          = paa.assignment_id -- Bug 2610141
7344      /* Added for Bug# 2448441 */
7345      and  paa.assignment_id           = pac1.assignment_id
7346      and  ppa1.payroll_action_id      = pac1.payroll_action_id
7347      and  pac1.assignment_action_id   = (select to_number(substr(max(lpad(paa2.action_sequence,15,'0')||paa2.assignment_action_id),16))         /*Bug 7242551 */
7348                                             from pay_payroll_actions ppa2,
7349                                                  pay_assignment_actions paa2
7350                                             where ppa2.action_type in ('R','Q','B','I') --Bug 2574186
7351                                             and ppa2.payroll_action_id = paa2.payroll_action_id
7352                                             and paa2.tax_unit_id  = p_registered_employer -- Bug 2610141
7353                                             and paa2.assignment_id = paa.assignment_id
7354                                             and ppa2.effective_date between add_months(p_year_start,-3) and p_year_end )/*Bug3048962 */
7355      /* End of Bug# 2448441 */
7356       and  (paa.effective_start_date, paaf.effective_start_date)
7357                                        = (select max(a.effective_Start_date),min(a.effective_start_date) -- Bug 2610141
7358                                           from per_all_assignments_f a
7359                                               , hr_soft_coding_keyflex hsc1                  --Added for bug 4177679
7360                                           where a.assignment_id = paa.assignment_id
7361                                           and  hsc1.soft_coding_keyflex_id  = a.soft_coding_keyflex_id    --Added for bug 4177679
7362                                           and  hsc1.segment1= p_registered_employer       --Added for bug 4177679
7363                                           and nvl(pps.actual_termination_date,p_year_end)
7364                                                 between a.effective_Start_date  and pap.effective_end_date  /*2689175*/
7365                                           and a.effective_end_date >= least(nvl(pps.actual_termination_date,p_year_start),p_year_start))--Added for bug 4177679,4299506
7366      and  pap.effective_start_date = (select max(effective_Start_date)
7367                                       from per_all_people_f p
7368                                       where p.person_id = pap.person_id
7369                                       and nvl(pps.actual_termination_date,p_year_end) between p.effective_Start_date and p.effective_end_date)
7370      ORDER BY pad.date_from DESC;/*Bug2977533*/
7371 
7372 
7373 /*Bug# 4653934 - Introduced a new cursor to get the payroll name for the employee. This has been done to take care of cases
7374                     where assignment has payroll attached to it for few months but is not attached at the end of year*/
7375  CURSOR c_get_payroll_name
7376  IS
7377  SELECT pay.payroll_name
7378  FROM per_all_assignments_f        paaf,
7379       pay_payrolls_f               pay
7380  WHERE paaf.assignment_id = p_assignment_id
7381  and   paaf.effective_end_date = (select max(effective_end_date)
7382                                From  per_assignments_f iipaf
7383                                      WHERE iipaf.assignment_id  = p_assignment_id
7384                                      and iipaf.effective_end_date >= p_fbt_year_start
7385                                      and iipaf.effective_start_date <= p_year_end
7386                                  AND iipaf.payroll_id IS NOT NULL)
7387  AND  pay.payroll_id = paaf.payroll_id
7388  AND  paaf.effective_end_date BETWEEN pay.effective_start_date AND pay.effective_end_date;
7389 
7390 
7391 Begin
7392 
7393     IF g_debug THEN
7394        hr_utility.set_location('Start of archive employee details ..',0);
7395      END if;
7396    open employee_details ;
7397    fetch employee_details into
7398       l_first_name
7399      ,l_middle_name
7400      ,l_surname
7401      ,l_address_1
7402      ,l_address_2
7403      ,l_address_3
7404      ,l_suburb
7405      ,l_state
7406      ,l_postcode
7407      ,l_country
7408      ,l_address_style      -- Bug 5364017
7409      ,l_add_country        -- Bug 5364017
7410      ,l_start_date
7411      ,l_termination_date
7412      ,l_final_process_date -- Bug3263659
7413      ,l_dob
7414      ,l_asgmnt_loc
7415      ,l_emp_no
7416 --     ,l_payroll
7417      ,l_emp_type
7418      ,l_address_date_from
7419      ,l_date_earned
7420      ,l_effective_date  -- Bug3263659
7421      ,l_actual_termination_date
7422      ,l_le_start_date
7423      ,l_le_end_date -- Bug 2610141
7424      ,l_termination_type; --Bug 8315198
7425 
7426 
7427     OPEN c_get_payroll_name;
7428     FETCH c_get_payroll_name INTO l_payroll;
7429     CLOSE c_get_payroll_name;
7430 
7431     IF g_debug THEN
7432         hr_utility.set_location('In Archive_employee_details    ',1000);
7433         hr_utility.set_location('l_first_name                   '||l_first_name,1000);
7434         hr_utility.set_location('l_surname                      '||l_surname,1000);
7435         hr_utility.set_location('l_address_1                    '||l_address_1,1000);
7436         hr_utility.set_location('l_address_style                '||l_address_style,1000);
7437         hr_utility.set_location('l_start_date                   '||l_start_date,1000);
7438         hr_utility.set_location('l_termination_date             '||l_termination_date,1000);
7439         hr_utility.set_location('l_final_process_date           '||l_final_process_date,1000);
7440         hr_utility.set_location('l_emp_no                       '||l_emp_no,1000);
7441         hr_utility.set_location('l_emp_type                     '||l_emp_type,1000);
7442         hr_utility.set_location('l_date_earned                  '||l_date_earned,1000);
7443         hr_utility.set_location('l_effective_date               '||l_effective_date,1000);
7444         hr_utility.set_location('l_actual_termination_date      '||l_actual_termination_date,1000);
7445         hr_utility.set_location('l_le_start_date                '||l_le_start_date,1000);
7446         hr_utility.set_location('l_le_end_date                  '||l_le_end_date,1000);
7447         hr_utility.set_location('l_termination_type             '||l_termination_type,1000); --Bug 8315198
7448     END if;
7449 
7450   if employee_details%found then
7451     close employee_details;
7452 
7453    /* - If the Employee is terminated in the current financial year then get the tax file number
7454         at the time of termination.
7455 
7456         Else use the date_earned of the last payroll action  to get the tax file number  Bug3019374
7457 
7458   */
7459 
7460     /* Bug3263659  - If termination date is in the last year then use final process date , otherwise current logic */
7461     /* Bug 7234263 - Added Least for end dated assignments where there may be no assignment record on Act Term Date */
7462 
7463     if l_emp_type = 'T' then
7464         if l_actual_termination_date < p_year_start AND l_effective_date >= p_year_start
7465         then
7466         l_actual_termination_date :=nvl(l_final_process_date,l_effective_date);/* Bug 3098353 To set the End Date as Payment Date if Final_process is null */
7467         l_termination_date:=  to_char(l_actual_termination_date,'DDMMYYYY');
7468         end if;
7469         l_date_earned:=least(l_actual_termination_date,l_date_earned);  /* Bug 7234263*/
7470     end if;
7471      /* End of Changes for Bug3263659 */
7472 
7473      /*Begin 8315198 - Removed the code for setting up the contexts,as the terminatin type is fetched from employee_details cursor.
7474                        The value of termination type is assigned to the package variable */
7475    g_termination_type := l_termination_type;
7476 
7477    IF g_debug THEN
7478          hr_utility.set_location('g_termination_type                 '||g_termination_type,1000);
7479    END IF;
7480 
7481       /*End 8315198 */
7482    l_le_etp_start_date := l_le_start_date; /*Bug 5075662 - Variable introduced to store the actual le start date which
7483                                              will be archived as X_ETP_EMPLOYEE_START_DATE if ETP service date is null*/
7484 ----------------------------2610141--------------------------------
7485    IF to_date(l_le_start_date,'DDMMYYYY') < p_year_start then
7486     l_le_start_date := to_char(p_year_start,'DDMMYYYY');
7487    ELSE
7488     l_le_start_date := l_le_start_date;
7489    END IF;
7490 
7491    IF to_date(l_le_end_date,'DDMMYYYY') > p_year_end THEN /*Bug 4063321*/
7492     l_le_end_date := to_char(p_year_end,'DDMMYYYY');
7493    ELSE
7494     l_le_end_date := l_le_end_date;
7495    END IF;
7496 ----------------------------------------------------------------
7497 
7498     IF g_debug THEN
7499         hr_utility.set_location('In Archive_employee_details, after adjsutments ',1000);
7500         hr_utility.set_location('l_termination_date             '||l_termination_date,1000);
7501         hr_utility.set_location('l_final_process_date           '||l_final_process_date,1000);
7502         hr_utility.set_location('l_date_earned                  '||l_date_earned,1000);
7503         hr_utility.set_location('l_effective_date               '||l_effective_date,1000);
7504         hr_utility.set_location('l_actual_termination_date      '||l_actual_termination_date,1000);
7505         hr_utility.set_location('l_le_start_date                '||l_le_start_date,1000);
7506         hr_utility.set_location('l_le_end_date                  '||l_le_end_date,1000);
7507     END if;
7508 
7509 /*Bug 13043357*/
7510   open tfn_number(p_assignment_id,p_year_end);
7511   fetch tfn_number into l_tfn_no;
7512 
7513  IF tfn_number%NOTFOUND THEN
7514   close tfn_number;
7515   open tfn_number(p_assignment_id,l_date_earned); /*Bug3019374*/
7516   fetch tfn_number into l_tfn_no;
7517  END IF;
7518 
7519   IF g_debug THEN
7520         hr_utility.set_location('tfn_number                     '||l_tfn_no,1000);
7521   END IF;
7522 
7523 
7524   If tfn_number%FOUND then   /*If Tax File details found then archive employe details*/
7525   close tfn_number;
7526 
7527     IF g_debug THEN
7528         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_TAX_FILE_NUMBER',1);
7529     END if;
7530     create_extract_archive_details(p_assignment_action_id,
7531                                    'X_EMPLOYEE_TAX_FILE_NUMBER',
7532                                    l_tfn_no);
7533 
7534     IF g_debug THEN
7535         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_FIRST_NAME',3);
7536     END if;
7537     create_extract_archive_details(p_assignment_action_id,
7538                                    'X_EMPLOYEE_FIRST_NAME',
7539                                     l_first_name);
7540     IF g_debug THEN
7541         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_MIDDLE_NAME',4);
7542     END if;
7543     create_extract_archive_details(p_assignment_action_id,
7544                                    'X_EMPLOYEE_MIDDLE_NAME',
7545                                    l_middle_name);
7546     IF g_debug THEN
7547         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_SURNAME',5);
7548     END if;
7549     create_extract_archive_details(p_assignment_action_id,
7550                                    'X_EMPLOYEE_SURNAME',
7551                                     l_surname);
7552     IF g_debug THEN
7553         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_ADDRESS_1',7);
7554     END if;
7555     create_extract_archive_details(p_assignment_action_id,
7556                                    'X_EMPLOYEE_ADDRESS_1',
7557                                    l_address_1);
7558     IF g_debug THEN
7559         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_ADDRESS_2',8);
7560     END if;
7561     create_extract_archive_details(p_assignment_action_id,
7562                                    'X_EMPLOYEE_ADDRESS_2',
7563                                    l_address_2);
7564     IF g_debug THEN
7565        hr_utility.set_location('Creating archive Item  X_EMPLOYEE_ADDRESS_3',8);
7566     END if;
7567     create_extract_archive_details(p_assignment_action_id,
7568                                    'X_EMPLOYEE_ADDRESS_3',
7569                                    l_address_3);
7570     IF g_debug THEN
7571         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_SUBURB',9);
7572     END if;
7573     create_extract_archive_details(p_assignment_action_id,
7574                                    'X_EMPLOYEE_SUBURB',
7575                                    l_suburb);
7576     IF g_debug THEN
7577         hr_utility.set_location('Creating archive Item  X_EMPLOYEE_STATE',10);
7578     END if;
7579     create_extract_archive_details(p_assignment_action_id,
7580                                    'X_EMPLOYEE_STATE',
7581                                     l_state);
7582     IF g_debug THEN
7583         hr_utility.set_location('Creating archive Item X_EMPLOYEE_POSTCODE',11);
7584     END if;
7585     create_extract_archive_details(p_assignment_action_id,
7586                                    'X_EMPLOYEE_POSTCODE',
7587                                    l_postcode);
7588 
7589     /* Bug 5364017 - Check for Generic Address
7590        IF Address Style is Generic - then get the country name as entered in Address form
7591     */
7592         IF l_address_style = 'GENERIC'
7593         THEN
7594               l_country := l_add_country;
7595         END IF;
7596 
7597     IF g_debug THEN
7598         hr_utility.set_location('Creating archive Item X_EMPLOYEE_COUNTRY',12);
7599     END if;
7600     create_extract_archive_details(p_assignment_action_id,
7601                                    'X_EMPLOYEE_COUNTRY',
7602                                    l_country);
7603     IF g_debug THEN
7604         hr_utility.set_location('Creating archive Item X_EMPLOYEE_START_DATE',12);
7605     END if;
7606     create_extract_archive_details(p_assignment_action_id,
7607                                    'X_EMPLOYEE_START_DATE',
7608                                    l_start_date);
7609 
7610     /* 3098353 Employee End Date Archived in main Archive_Code */
7611 
7612     IF g_debug THEN
7613         hr_utility.set_location('Creating archive Item X_EMPLOYEE_DATE_OF_BIRTH',12);
7614     END if;
7615     create_extract_archive_details(p_assignment_action_id,
7616                                    'X_EMPLOYEE_DATE_OF_BIRTH',
7617                                    l_dob);
7618    /* bug 3098353 */
7619     IF g_debug THEN
7620         hr_utility.set_location('Creating archive Item X_EMPLOYEE_FINAL_PROCESS_DATE',12);
7621     END if;
7622     l_fpd_archive := to_char(l_final_process_date,'DDMMYYYY');
7623     create_extract_archive_details(p_assignment_action_id,
7624                                    'X_EMPLOYEE_FINAL_PROCESS_DATE',
7625                                    l_fpd_archive);
7626     ---------------------------------------------------------------
7627     -- archive sort details
7628     ---------------------------------------------------------------
7629     IF g_debug THEN
7630         hr_utility.set_location('Creating archive Item X_SORT_EMPLOYEE_NUMBER',12);
7631     END if;
7632     create_extract_archive_details(p_assignment_action_id,
7633                                    'X_SORT_EMPLOYEE_NUMBER',
7634                                    l_emp_no );
7635     IF g_debug THEN
7636         hr_utility.set_location('Creating archive Item X_SORT_ASSIGNMENT_LOCATION',12);
7637     END if;
7638     create_extract_archive_details(p_assignment_action_id,
7639                                    'X_SORT_ASSIGNMENT_LOCATION',
7640                                    l_asgmnt_loc );
7641     IF g_debug THEN
7642         hr_utility.set_location('Creating archive Item X_SORT_PAYROLL',12);
7643     END if;
7644     create_extract_archive_details(p_assignment_action_id,
7645                                    'X_SORT_PAYROLL',
7646                                    l_payroll );
7647     IF g_debug THEN
7648         hr_utility.set_location('Creating archive Item X_SORT_EMPLOYEE_TYPE',12);
7649     END if;
7650     create_extract_archive_details(p_assignment_action_id,
7651                                    'X_SORT_EMPLOYEE_TYPE',
7652                                    l_emp_type );
7653     IF g_debug THEN
7654          hr_utility.set_location('Creating archive Item  X_SORT_EMPLOYEE_LAST_NAME',5);
7655     END if;
7656     create_extract_archive_details(p_assignment_action_id,
7657                                    'X_SORT_EMPLOYEE_LAST_NAME',
7658                                     l_surname);
7659 
7660 
7661 ----------------------------2610141--------------------------------
7662     IF g_debug THEN
7663          hr_utility.set_location('Creating archive Item  X_EMPLOYEE_LE_START_DATE',5);
7664     END if;
7665     l_le_etp_service_date := l_le_etp_start_date; /* Bug 4177679, Bug# 5075662 */
7666 
7667   /*Bug# 4363057 - If condition introduced so that the X_EMPLOYEE_LE_START_DATE gets archived only once.
7668                      If for submitted legal employer employee is not active in the financial year then
7669                      X_EMPLOYEE_LE_START_DATE is archived in the archive_code*/
7670 
7671    IF p_end_date_flag <> 'B' THEN
7672       create_extract_archive_details(p_assignment_action_id,
7673                                    'X_EMPLOYEE_LE_START_DATE',
7674                                     l_le_start_date);
7675     END IF;
7676    /* Bug 4177679 ETP start date to be archived */
7677  if g_le_etp_flag ='N' then
7678         create_extract_archive_details(p_assignment_action_id,
7679                                        'X_ETP_EMPLOYEE_START_DATE',
7680                                         l_le_etp_service_date);
7681    IF g_debug THEN
7682         hr_utility.set_location('End of archive_prepost_details',18);
7683    END if;
7684      g_le_etp_flag := 'Y';
7685   end if ;
7686 
7687 
7688     IF g_debug THEN
7689          hr_utility.set_location('Creating archive Item  X_EMPLOYEE_LE_END_DATE',5);
7690     END if;
7691 
7692     IF p_end_date_flag = 'N' THEN
7693     create_extract_archive_details(p_assignment_action_id,
7694                                    'X_EMPLOYEE_LE_END_DATE',
7695                                 l_le_end_date);
7696     END IF;
7697 ----------------------------2610141--------------------------------
7698     end if;
7699 
7700   else
7701    close  employee_details;
7702     raise e_employee_nf;
7703   end if;
7704 exception
7705   when e_employee_nf then
7706     IF g_debug THEN
7707         hr_utility.set_location('No employee Details found for the assigment id  ',20);
7708     END if;
7709   when others then
7710     IF g_debug THEN
7711         hr_utility.set_location('Error in archive_employee_details ',99);
7712     END if;
7713     raise;
7714 End archive_employee_details;
7715 
7716 
7717   ------------------------------------------------------------------------+
7718   -- procedure to archive supplier details
7719   -- calls create_extract_archive_Details
7720   ------------------------------------------------------------------------+
7721 Procedure archive_supplier_details
7722             (p_business_group_id       in hr_organization_units.business_group_id%type,
7723          p_registered_employer     in hr_organization_units.organization_id%type,
7724          p_payroll_action_id       in pay_payroll_actions.payroll_action_id%type,
7725          p_assignment_Action_id    in pay_assignment_actions.assignment_action_id%type,
7726          p_assignment_id           in pay_assignment_actions.assignment_id%type,
7727          p_year_start              in pay_payroll_actions.effective_date%type,
7728          p_year_end            in pay_payroll_Actions.effective_date%type) is
7729 
7730 
7731   l_report_end_date             varchar2(20);
7732   l_supplier_number     hr_organization_information.org_information1%type;
7733   l_supplier_name               hr_organization_information.org_information3%type;
7734   l_supplier_abn                hr_organization_information.org_information5%type;
7735   l_supplier_contact_name       per_all_people_f.full_name%type;
7736   l_supplier_contact_phone      per_addresses.telephone_number_1%type;
7737   l_supplier_address_1          hr_locations.address_line_1%type;
7738   l_supplier_address_2          hr_locations.address_line_2%type;
7739   l_supplier_address_3          hr_locations.address_line_3%type;
7740   l_supplier_suburb             hr_locations.town_or_city%type;
7741   l_supplier_state              hr_locations.region_1%type;
7742   l_supplier_postcode           hr_locations.postal_code%type;
7743   l_supplier_country            fnd_territories_tl.territory_short_name%type;
7744 
7745   ------------------------------------------------------------------------------+
7746   -- cursor to get supplier details
7747   ------------------------------------------------------------------------------+
7748 
7749    cursor supplier_details
7750        is
7751       select  '3006' ||to_char(p_year_end,'YYYY')      report_end_date
7752               ,hoi.org_information1                   supplier_number
7753               ,hoi.org_information3                   supplier_name
7754               ,hoi.org_information12                  supplier_abn
7755               ,pap.first_name || ' ' || pap.last_name supplier_contact_name
7756               ,hoi.org_information14                  supplier_contact_phone
7757               ,hrl.address_line_1                     supplier_address_1
7758               ,hrl.address_line_2                     supplier_address_2
7759               ,hrl.address_line_3                     supplier_address_3
7760               ,hrl.town_or_city                       supplier_suburb
7761               ,hrl.region_1                           supplier_state
7762               ,hrl.postal_code                        supplier_postcode
7763               ,ftl.territory_short_name               supplier_country
7764               ,pap.email_address                      email_address
7765        from    hr_organization_information  hoi
7766               ,hr_organization_units        hou
7767               ,hr_locations                 hrl
7768               ,fnd_territories_tl           ftl
7769               ,per_all_people_f             pap
7770        where  hou.business_group_id       = p_business_group_id
7771          and  hou.organization_id         = p_registered_employer
7772          and  hou.organization_id         = hoi.organization_id
7773          and  hoi.org_information_context = 'AU_LEGAL_EMPLOYER'
7774          and  hrl.location_id             = hou.location_id
7775          and  ftl.territory_code          = hrl.country
7776          and  ftl.language                = userenv('LANG')
7777          and  hoi.org_information7       = pap.person_id
7778          and  pap.effective_start_date    = (select max(effective_start_date)
7779                                              from  per_all_people_f p
7780                                             where pap.person_id=p.person_id);
7781 
7782 Begin
7783     IF g_debug THEN
7784         hr_utility.set_location('Archiving Supplier Details ',1);
7785         hr_utility.set_location('Assignments action id is  '||p_assignment_action_id,2);
7786     END if;
7787 
7788 
7789   for sd in supplier_details
7790   loop
7791 
7792     IF g_debug THEN
7793         hr_utility.set_location('Creating Archive Item  X_REPORT_END_DATE',3);
7794     END if;
7795     create_extract_archive_details (p_assignment_action_id
7796                                     ,'X_REPORT_END_DATE'
7797                                     ,sd.report_end_date);
7798 
7799     IF g_debug THEN
7800         hr_utility.set_location('Creating archive Item  X_SUPPLIER_NUMBER',3);
7801     END if;
7802     create_extract_archive_details(p_assignment_action_id,
7803                                    'X_SUPPLIER_NUMBER',
7804                                    sd.supplier_number);
7805     IF g_debug THEN
7806         hr_utility.set_location('Creating archive Item  X_SUPPLIER_NAME',3);
7807     END if;
7808     create_extract_archive_details(p_assignment_action_id,
7809                                    'X_SUPPLIER_NAME',
7810                                     sd.supplier_name);
7811     IF g_debug THEN
7812         hr_utility.set_location('Creating archive Item  X_SUPPLIER_ABN',3);
7813     END if;
7814     create_extract_archive_details(p_assignment_action_id,
7815                                    'X_SUPPLIER_ABN',
7816                                     sd.supplier_abn);
7817     IF g_debug THEN
7818         hr_utility.set_location('Creating archive Item  X_SUPPLIER_CONTACT_NAME',3);
7819     END if;
7820     create_extract_archive_details(p_assignment_action_id,
7821                                    'X_SUPPLIER_CONTACT_NAME',
7822                                    sd.supplier_contact_name);
7823     IF g_debug THEN
7824         hr_utility.set_location('Creating archive Item  X_SUPPLIER_CONTACT_PHONE',3);
7825     END if;
7826     create_extract_archive_details(p_assignment_action_id,
7827                                    'X_SUPPLIER_CONTACT_TELEPHONE',
7828                                     sd.supplier_contacT_phone);
7829     IF g_debug THEN
7830         hr_utility.set_location('Creating archive Item  X_SUPPLIER_EMAIL',3);
7831     END if;
7832     create_extract_archive_details(p_assignment_action_id,
7833                                    'X_SUPPLIER_EMAIL',
7834                                    sd.email_address);
7835     IF g_debug THEN
7836        hr_utility.set_location('Creating archive Item  X_SUPPLIER_ADDRESS_1',3);
7837     END if;
7838     create_extract_archive_details(p_assignment_action_id,
7839                                    'X_SUPPLIER_ADDRESS_1',
7840                                    sd.supplier_address_1);
7841     IF g_debug THEN
7842         hr_utility.set_location('Creating archive Item  X_SUPPLIER_ADDRESS_2',3);
7843     END if;
7844     create_extract_archive_details(p_assignment_action_id,
7845                                    'X_SUPPLIER_ADDRESS_2',
7846                                    sd.supplier_address_2);
7847 
7848     IF g_debug THEN
7849         hr_utility.set_location('Creating archive Item  X_SUPPLIER_ADDRESS_3',3);
7850     END if;
7851     create_extract_archive_details(p_assignment_action_id,
7852                                    'X_SUPPLIER_ADDRESS_3',
7853                                    sd.supplier_address_3);
7854     IF g_debug THEN
7855         hr_utility.set_location('Creating archive Item  X_SUPPLIER_SUBURB',3);
7856     END if;
7857     create_extract_archive_details(p_assignment_action_id,
7858                                    'X_SUPPLIER_SUBURB',
7859                                    sd.supplier_suburb);
7860     IF g_debug THEN
7861         hr_utility.set_location('Creating archive Item  X_SUPPLIER_STATE',3);
7862     END if;
7863     create_extract_archive_details(p_assignment_action_id,
7864                                    'X_SUPPLIER_STATE',
7865                                    sd.supplier_state);
7866     IF g_debug THEN
7867         hr_utility.set_location('Creating archive Item  X_SUPPLIER_POSTCODE',3);
7868     END if;
7869     create_extract_archive_details(p_assignment_action_id,
7870                                    'X_SUPPLIER_POSTCODE',
7871                                     sd.supplier_postcode);
7872 
7873     IF g_debug THEN
7874         hr_utility.set_location('Creating archive Item  X_SUPPLIER_COUNTRY',3);
7875     END if;
7876     create_extract_archive_details(p_assignment_action_id,
7877                                    'X_SUPPLIER_COUNTRY',
7878                                    sd.supplier_country);
7879 
7880 
7881   end loop;
7882     IF g_debug THEN
7883         hr_utility.set_location('Archived all the supplier details',200);
7884     END if;
7885 exception
7886   when others then
7887     IF g_debug THEN
7888         hr_utility.set_location('Error in archiving supplier details ',10);
7889     END if;
7890     raise;
7891 
7892 End archive_supplier_details;
7893 
7894 /*-------------------------------------------------------------------------------
7895 Bug 6192381 Procedure to adjust old etp deduction and old Invalidity Payments
7896 
7897 -------------------------------------------------------------------------------*/
7898 procedure adjust_old_etp_values
7899 (p_trans_etp_flag               in Varchar2
7900 ,p_part_of_prev_etp_flag        in Varchar2
7901 ) is
7902 
7903   l_procedure           constant varchar2(80) := g_package || '.adjust_old_etp_values';
7904 
7905   l_old_etp_ded             NUMBER;
7906   l_old_inv_pay             NUMBER;
7907 
7908 Begin
7909   g_debug :=  hr_utility.debug_enabled;
7910 
7911   if g_debug then
7912      hr_utility.set_location(l_procedure, 1);
7913       hr_utility.trace('In p_trans_etp_flag           '||p_trans_etp_flag);
7914       hr_utility.trace('In p_part_of_prev_etp_flag    '||p_part_of_prev_etp_flag);
7915       hr_utility.trace('------------------------------------------------');
7916       hr_utility.trace('Lump Sum C Deductions                                  ===>' || p_result_table(7).balance_value);
7917       hr_utility.trace('Invalidity Payments                                    ===>' || p_result_table(14).balance_value);
7918       hr_utility.trace('ETP Deductions Transitional Not Part of Prev Term      ===>' || p_result_table(18).balance_value);
7919       hr_utility.trace('ETP Deductions Transitional Part of Prev Term          ===>' || p_result_table(19).balance_value);
7920       hr_utility.trace('ETP Deductions Life Benefit Not Part of Prev Term      ===>' || p_result_table(20).balance_value);
7921       hr_utility.trace('ETP Deductions Life Benefit Part of Prev Term          ===>' || p_result_table(21).balance_value);
7922       hr_utility.trace('Invalidity Payments Life Benefit Not Part of Prev Term  ===>' || p_result_table(22).balance_value);
7923       hr_utility.trace('Invalidity Payments Life Benefit Part of Prev Term      ===>' || p_result_table(23).balance_value);
7924       hr_utility.trace('Invalidity Payments Transitional Not Part of Prev Term  ===>' || p_result_table(24).balance_value);
7925       hr_utility.trace('Invalidity Payments Transitional Part of Prev Term      ===>' || p_result_table(25).balance_value);
7926 
7927   end if;
7928 
7929       l_old_etp_ded := 0;
7930       l_old_inv_pay := 0;
7931 
7932 /* Bug 6192381  For Termination Payments before Multiple ETP Payment Enhancement the new balances for each combination
7933 does not exist , therefore to find out the values difference of Old balance which has total values and sum of new balances
7934 is taken to archive the old values . this is done for ETP Deduction and Invalidity Payments
7935 
7936   l_old_etp_ded  :=  Lump Sum C Deductions - ( ETP Deductions Transitional Not Part of Prev Term  +
7937                                                ETP Deductions Transitional Part of Prev Term      +
7938                                                ETP Deductions Life Benefit Not Part of Prev Term  +
7939                                                ETP Deductions Life Benefit Part of Prev Term )
7940 
7941   l_old_inv_pay  :=  Invalidity Payments  -  ( Invalidity Payments Life Benefit Not Part of Prev ETP  +
7942                                                Invalidity Payments Life Benefit Part of Prev ETP      +
7943                                                Invalidity Payments Transitional Not Part of Prev ETP  +
7944                                                Invalidity Payments Transitional Part of Prev ETP )
7945 
7946  If these values are greater then 0 then based on Inputs Transitional ETP and Part of Previously Paid ETP , these values are
7947  added to corresponding Balances .
7948 */
7949 
7950       l_old_etp_ded :=  p_result_table(7).balance_value - (p_result_table(18).balance_value + p_result_table(19).balance_value +
7951                                                             p_result_table(20).balance_value + p_result_table(21).balance_value );
7952 
7953       l_old_inv_pay :=  p_result_table(14).balance_value - (p_result_table(22).balance_value + p_result_table(23).balance_value +
7954                                                             p_result_table(24).balance_value + p_result_table(25).balance_value );
7955 
7956   if g_debug then
7957       hr_utility.trace('l_old_etp_ded   ===>' || l_old_etp_ded);
7958       hr_utility.trace('l_old_inv_pay   ===>' || l_old_inv_pay);
7959   end if;
7960 
7961 
7962       if ( l_old_etp_ded > 0 ) then
7963                if (p_trans_etp_flag = 'Y' and p_part_of_prev_etp_flag ='N') then
7964                     p_result_table(18).balance_value := p_result_table(18).balance_value + l_old_etp_ded ;
7965                end if;
7966 
7967                if (p_trans_etp_flag = 'Y' and p_part_of_prev_etp_flag ='Y') then
7968                     p_result_table(19).balance_value := p_result_table(19).balance_value + l_old_etp_ded ;
7969                end if;
7970 
7971                if (p_trans_etp_flag = 'N' and p_part_of_prev_etp_flag ='N') then
7972                     p_result_table(20).balance_value := p_result_table(20).balance_value + l_old_etp_ded ;
7973                end if;
7974 
7975                if (p_trans_etp_flag = 'N' and p_part_of_prev_etp_flag ='Y') then
7976                     p_result_table(21).balance_value := p_result_table(21).balance_value + l_old_etp_ded ;
7977                end if;
7978 
7979       end if;
7980 
7981       if ( l_old_inv_pay > 0 ) then
7982                if (p_trans_etp_flag = 'N' and p_part_of_prev_etp_flag ='N') then
7983                     p_result_table(22).balance_value := p_result_table(22).balance_value + l_old_inv_pay ;
7984                end if;
7985 
7986                if (p_trans_etp_flag = 'N' and p_part_of_prev_etp_flag ='Y') then
7987                     p_result_table(23).balance_value := p_result_table(23).balance_value + l_old_inv_pay ;
7988                end if;
7989 
7990                if (p_trans_etp_flag = 'Y' and p_part_of_prev_etp_flag ='N') then
7991                     p_result_table(24).balance_value := p_result_table(24).balance_value + l_old_inv_pay ;
7992                end if;
7993 
7994                if (p_trans_etp_flag = 'Y' and p_part_of_prev_etp_flag ='Y') then
7995                     p_result_table(25).balance_value := p_result_table(25).balance_value + l_old_inv_pay ;
7996                end if;
7997 
7998       end if;
7999 
8000 exception
8001   when others then
8002     IF g_debug THEN
8003         hr_utility.set_location('Error in adjust_old_etp_values ',10);
8004     END if;
8005 
8006 End adjust_old_etp_values;
8007 
8008  --------------------------------------------------------------------+
8009   -- This procedure is actually used to archive data . It
8010   -- internally calls private procedures to archive balances ,
8011   -- employee details, employer details and supplier details .
8012   -- Calls the following procedures
8013   -- 1. archive_balance_details
8014   -- 2. archive_etp_details
8015   -- 3. archive_prepost_details
8016   -- 4. archive_employer_details
8017   -- 5. archive_supplier_details
8018   -- 6. archive_employee_details
8019   --------------------------------------------------------------------+
8020 procedure archive_code
8021 (p_assignment_action_id  in pay_assignment_actions.assignment_action_id%type
8022 ,p_effective_date        in date
8023 ) is
8024   --
8025   l_procedure           constant varchar2(80) := g_package || '.archive_code';
8026   l_assignment_id               pay_assignment_actions.assignment_id%type;
8027   l_business_group_id           pay_payroll_actions.business_group_id%type ;
8028   l_registered_employer         hr_organization_units.organization_id%type;
8029   l_current_le                  hr_organization_units.organization_id%type;         --4363057
8030   l_payroll_action_id           pay_payroll_actions.payroll_action_id%type ;
8031   l_year_start                  pay_payroll_Actions.effective_date%type;
8032   l_year_end                    pay_payroll_actions.effective_date%type;
8033   l_employee_type               per_all_people_f.current_Employee_Flag%type;
8034   l_current_employee_flag       per_all_people_f.current_employee_flag%type  :='Y';
8035   l_actual_termination_date     per_periods_of_service.actual_termination_date%TYPE;
8036   l_date_start                  per_periods_of_service.date_start%TYPE;
8037   l_asg_start                   pay_payroll_actions.effective_date%type;
8038   l_asg_end                     pay_payroll_actions.effective_date%type;
8039   l_effective_date              pay_payroll_actions.effective_date%type;
8040   l_death_benefit_type          varchar2(100);
8041   l_assignment_action_id        pay_assignment_actions.assignment_action_id%type;
8042   l_max_assignment_action_id    pay_assignment_actions.assignment_action_id%type;
8043   l_bbr_assignment_action_id    pay_assignment_actions.assignment_action_id%type;
8044   l_fbt_assignment_action_id    pay_assignment_actions.assignment_action_id%type;       --2610141
8045   lump_sum_c_found              boolean := false;
8046   l_final_process_date          date;                                                   --263659
8047   l_term_date                   varchar2(10);                                           --3263659
8048   l_fetched_termination_date    per_periods_of_service.actual_termination_date%TYPE;    --3263659
8049   l_reporting_flag              varchar2(5) := 'YES';                   --3098353
8050   l_bal_value                   varchar2(20);                       --3098353
8051   l_alw_bal_exist               varchar2(20);                       --3098353
8052   l_lst_yr_term                 varchar2(10);                       --3661230
8053   v_lst_year_start              date ;                          --3661230
8054   l_fbt_year_start              date;
8055   l_net_balance                 number := 0;                        --3098353
8056   l_fbt_balance                 number := 0;                        --3098353
8057   l_dummy                       number;                             --4363057
8058   l_pay_start                   varchar2(10);                       --4363057
8059   l_pay_end                     varchar2(10);                       --4363057
8060   l_curr_term_0_bal_flag        varchar2(5) :='NO';                     --3937976
8061   l_le_end_date_flag        varchar2(5) := 'N';                     --2610141
8062   l_lump_sum_a_pymt_type        char(1);                            /*Bug#8315198*/
8063   l_lump_sum_a_value            number := 0;                        /*Bug#8315198*/
8064   l_etp_effective_date          DATE ;                              /*Bug#9872891*/
8065   --
8066   -------------------------------------------------+
8067   -- Cursor to get details for assignment action id
8068   -------------------------------------------------+
8069   --
8070   cursor c_action(c_assignment_action_id number) is
8071   select pay_core_utils.get_parameter('BUSINESS_GROUP_ID',ppa.legislative_parameters)
8072   ,      pay_core_utils.get_parameter('REGISTERED_EMPLOYER',ppa.legislative_parameters)
8073   ,      pay_core_utils.get_parameter('EMPLOYEE_TYPE',ppa.legislative_parameters)
8074   ,      ppa.payroll_action_id
8075   ,      paa.assignment_id
8076   ,      to_date('01-07-'|| substr(pay_core_utils.get_parameter('FINANCIAL_YEAR',ppa.legislative_parameters),1,4),'DD-MM-YYYY')
8077   ,      to_date('30-06-'|| substr(pay_core_utils.get_parameter('FINANCIAL_YEAR',ppa.legislative_parameters),6,4),'DD-MM-YYYY')
8078   ,      pay_core_utils.get_parameter('LST_YR_TERM',ppa.legislative_parameters)                   /*Bug3661230*/
8079   from   pay_assignment_actions     paa
8080   ,      pay_payroll_actions        ppa
8081   where  paa.assignment_action_id   = c_assignment_action_id
8082   and    ppa.payroll_action_id      = paa.payroll_action_id ;
8083   --
8084   -------------------------------------------------+
8085   -- Cursor to get current employee flag
8086   -- Bug 2856638 : added parameter c_business_group_id
8087   -------------------------------------------------+
8088   --
8089   cursor etp_code
8090   (c_assignment_id     in pay_assignment_actions.assignment_id%type
8091   ,c_lst_year_start    in pay_payroll_actions.effective_date%type                   --3263659
8092   ,c_year_start        in pay_payroll_actions.effective_date%type
8093   ,c_year_end          in pay_payroll_actions.effective_date%type
8094   ,c_def_bal_id        in pay_defined_balances.defined_balance_id%type
8095   ,c_business_group_id in pay_payroll_actions.business_group_id%type
8096   ) is
8097   select distinct nvl(current_employee_flag,'N') current_employee_flag,
8098          actual_termination_date,
8099          date_start,
8100          pps.pds_information2,
8101          to_number(substr(max(lpad(ppa.action_sequence,15,'0')||ppa.assignment_action_id),16)),         --3755305
8102          pps.final_process_date                    final_process_date                       --3263659
8103   from   per_all_people_f          p,
8104          per_all_assignments_f     a,
8105          per_periods_of_service    pps,
8106          pay_all_payrolls_f        papf,                                    --4281290
8107          pay_payroll_actions       pa,
8108          pay_assignment_actions ppa
8109   where  a.person_id            = p.person_id
8110   and    pps.person_id          = p.person_id
8111   and    a.assignment_id        = ppa.assignment_id
8112   and    papf.business_group_id     = p.business_group_id                       --4281290
8113   and    p.business_group_id = c_business_group_id
8114   and    a.business_group_id = c_business_group_id                       --4281290
8115   and    ppa.assignment_id = c_assignment_id
8116   and    papf.business_group_id = c_business_group_id
8117   and    pa.payroll_id          = papf.payroll_id                           --4281290
8118   and    pa.effective_date between papf.effective_start_date and papf.effective_end_date        --4281290
8119   and    pa.payroll_Action_id       = ppa.payroll_Action_id
8120   and    ppa.tax_unit_id            = l_registered_employer                     --2610141
8121   and    (
8122             (pps.actual_termination_date between c_lst_year_start and c_year_end
8123              and pa.effective_date       between c_year_start and c_year_end                    --3263659
8124              )
8125             or
8126             (pps.actual_termination_date between l_fbt_year_start
8127                                             and  to_date('30-06-'||to_char(c_year_start,'YYYY'),'DD-MM-YYYY')
8128              and pa.effective_date       between to_date('01-04-'||to_char(c_year_start,'YYYY'),'DD-MM-YYYY')
8129                                             and  to_date('30-06-'||to_char(c_year_start,'YYYY'),'DD-MM-YYYY')
8130             )
8131          )
8132   and    a.assignment_id = c_assignment_id
8133   and    p.effective_start_date = (select max(pp.effective_start_date)
8134                                    from   per_all_people_f pp
8135                                    where  p.person_id = pp.person_id
8136                                    and    p.business_group_id = c_business_group_id
8137                                    ) -- Bug 2856638
8138   and    a.effective_start_date = (select  max(aa.effective_start_date)
8139                                    from  per_all_assignments_f aa
8140                                    where  aa.assignment_id = c_assignment_id
8141                                    )                        --4281290
8142   and    pa.action_type in ('R','Q','I','B','V')                --2646912, 4063321
8143   and    a.period_of_service_id = pps.period_of_service_id          --3586388
8144   group by nvl(current_employee_flag,'N')                           --3019374
8145   ,        actual_termination_date
8146   ,        date_start
8147   ,        pps.pds_information2
8148   ,        pps.final_process_date;                              --3263659
8149   --
8150   --3263659
8151   cursor cr_effective_date (c_assignment_action_id pay_assignment_actions.assignment_action_id%type) IS
8152   select ppa.effective_date
8153   from   pay_payroll_actions      ppa,
8154          pay_assignment_actions   paa
8155   where  paa.assignment_action_id = c_assignment_action_id
8156   and    ppa.payroll_action_id    = paa.payroll_action_id;
8157   --
8158   -------------------------------------------------+
8159   -- Cursor to check if 'Lump Sum C Payments' Balance
8160   -- exists. This is used to decide if archive_etp_Details
8161   -- is to be called or not(Bug  - 2581436 )
8162   -------------------------------------------------+
8163   cursor balance_exists is
8164   select pdb.defined_balance_id
8165   from   pay_balance_types            pbt,
8166          pay_defined_balances         pdb,
8167          pay_balance_dimensions       pbd
8168   where  pbt.balance_name             ='Lump Sum C Payments'
8169   and    pbt.legislation_code         = g_legislation_code
8170   and    pbt.balance_type_id          = pdb.balance_type_id
8171   and    pdb.balance_dimension_id     = pbd.balance_dimension_id    --2501105
8172   and    pbd.legislation_code         = g_legislation_code
8173   and    pdb.legislation_code         = g_legislation_code
8174   and    pbd.dimension_name           = '_ASG_LE_YTD';          --2610141
8175   --
8176   ----------------------------------------------------------------------------------------+
8177   -- Cursor to get maximum assignment_action_id to use in BBR Call (Bug 4738470)
8178   ----------------------------------------------------------------------------------------+
8179   --
8180   cursor c_max_asg_action_id
8181   (c_assignment_id      per_all_assignments_f.assignment_id%TYPE
8182   ,c_business_group_id  hr_all_organization_units.organization_id%TYPE
8183   ,c_tax_unit_id        hr_all_organization_units.organization_id%TYPE
8184   ,c_year_start         date
8185   ,c_year_end           date
8186   ) is
8187   select to_number(substr(max(lpad(paa.action_sequence,15,'0')||paa.assignment_action_id),16)) assignment_action_id,
8188          max(paa.action_sequence)   action_sequence
8189   from   pay_assignment_actions         paa,
8190          pay_payroll_actions            ppa,
8191          per_assignments_f              paf
8192   where  paa.assignment_id              = paf.assignment_id
8193   and    paf.assignment_id          = c_assignment_id
8194   and    paa.assignment_id          = c_assignment_id
8195   and    ppa.payroll_action_id      = paa.payroll_action_id
8196   and    ppa.effective_date         between c_year_start and c_year_end
8197   and    ppa.payroll_id             =  paf.payroll_id
8198   and    ppa.action_type            in ('R', 'Q', 'I', 'V', 'B')
8199   and    ppa.effective_date         between paf.effective_start_date and paf.effective_end_date
8200   and    paa.action_status      = 'C'
8201   and    paa.tax_unit_id        = c_tax_unit_id;
8202 
8203 /*Bug# 4363057 - Three new cursors introduced for the Retro enhancement
8204    a)   csr_check_eff_le - This cursor will check whether the employee
8205                          is active in the current financial year for the submitted legal employer
8206    b)   csr_get_dates - This cursor will get the maximum and minimum payment dates for the employee
8207                       in the current financial year. This cursor should execute only when the
8208                       employee is not active in the current financial year for the submitted legal employer.
8209    c)   csr_get_end_le - This cursor will get the effective legal employer on the last payment date of the employee
8210                        and it will be passed as an argument to archive_employee_details procedure for archiving
8211                        employee details. This cursor should also execute only when the employee is not active in the current financial year for the submitted legal employer.
8212 */
8213   cursor csr_check_eff_le
8214   (c_assignment_id per_assignments_f.assignment_id%type
8215   ,c_legal_employer hr_organization_units.organization_id%type
8216   ,c_year_start pay_payroll_actions.effective_date%type
8217   ,c_year_end pay_payroll_actions.effective_date%type
8218   ) is
8219   select paaf.assignment_id
8220   from   per_assignments_f      paaf,
8221          hr_soft_coding_keyflex     hsck
8222   where  paaf.assignment_id         = c_assignment_id
8223   and    paaf.soft_coding_keyflex_id    = hsck.soft_coding_keyflex_id
8224   and    paaf.effective_start_date  <= c_year_end
8225   and    paaf.effective_end_date    >= c_year_start
8226   and    hsck.segment1          = c_legal_employer;
8227   --
8228 
8229   /* Bug 5371102 - Added joins on Payroll ID for better performance */
8230   cursor csr_get_dates
8231   (c_assignment_id per_assignments_f.assignment_id%type
8232   ,c_year_start pay_payroll_actions.effective_date%type
8233   ,c_year_end pay_payroll_actions.effective_date%type
8234   ,c_legal_employer hr_organization_units.organization_id%type
8235   ) is
8236   select to_char(min(ppa.effective_date),'DDMMYYYY'), to_char(max(ppa.effective_date),'DDMMYYYY')
8237   from   pay_assignment_actions     paa,
8238          pay_payroll_actions        ppa,
8239          per_assignments_f      paaf
8240   where  paa.assignment_id      = paaf.assignment_id
8241   and    paaf.assignment_id         = c_assignment_id
8242   and    paa.payroll_action_id      = ppa.payroll_action_id
8243   and    ppa.effective_date         between c_year_start and c_year_end
8244   and    ppa.payroll_id             = paaf.payroll_id
8245   and    ppa.effective_date         between paaf.effective_start_date and paaf.effective_end_date
8246   and    paa.tax_unit_id        = c_legal_employer
8247   and    paa.action_status      = 'C'
8248   and    ppa.action_type        in ('R','Q','V'); /*Bug 4387183*/
8249   --
8250   -- 4387183 - This cursor has been modified. It will now pick the maximum effective record for the
8251   --           assignment between FBT year start and financial year end.
8252   --
8253   cursor csr_get_end_le
8254   (c_assignment_id per_assignments_f.assignment_id%type
8255   ,c_year_end pay_payroll_actions.effective_date%type
8256   ,c_year_start pay_payroll_actions.effective_date%type
8257   ) is
8258   select hsck.segment1
8259   from   per_assignments_f      paaf,
8260          hr_soft_coding_keyflex     hsck
8261   where  paaf.assignment_id         = c_assignment_id
8262   and    paaf.soft_coding_keyflex_id    = hsck.soft_coding_keyflex_id
8263   and    paaf.effective_start_date  <= c_year_end
8264   and    paaf.effective_end_date    >= c_year_start
8265   order by paaf.effective_start_date desc;
8266 
8267  /* Bug 9872891 - Added new cursor to get effective date when ETP payments
8268                   have been made for previously terminated employees */
8269   CURSOR c_etp_max_pay_date
8270   (c_assignment_id      per_all_assignments_f.assignment_id%TYPE
8271   ,c_tax_unit_id        hr_all_organization_units.organization_id%TYPE
8272   ,c_year_start         DATE
8273   ,c_year_end           DATE
8274   ) is
8275   SELECT max(ppa.effective_date)   effective_date
8276   FROM   pay_assignment_actions         paa,
8277          pay_payroll_actions            ppa,
8278          per_assignments_f              paf,
8279          pay_element_types_f            pet,
8280          pay_run_results                prr
8281   WHERE  paa.assignment_id              = paf.assignment_id
8282   AND    paf.assignment_id              = c_assignment_id
8283   AND    ppa.payroll_action_id          = paa.payroll_action_id
8284   AND    ppa.effective_date             BETWEEN c_year_start AND c_year_end
8285   AND    ppa.payroll_id                 =  paf.payroll_id
8286   AND    ppa.action_type                IN ('R', 'Q', 'I', 'V', 'B')
8287   AND    ppa.effective_date             BETWEEN paf.effective_start_date AND paf.effective_end_date
8288   AND    paa.action_status              = 'C'
8289   AND    paa.tax_unit_id                = c_tax_unit_id
8290   AND    prr.assignment_action_id       = paa.assignment_action_id
8291   AND    prr.element_type_id            = pet.element_type_id
8292   AND    pet.element_name               = 'ETP on Termination'
8293   AND    pet.legislation_code           = 'AU'
8294   AND    pet.business_group_id          IS NULL
8295   AND    ppa.effective_date             BETWEEN pet.effective_start_date AND pet.effective_end_date;
8296 
8297   --
8298   -- End 4363057
8299   --
8300   ---------------------------------------------------------------------------+
8301   -- This table is used to store all the balance names
8302   -- for these, archive_balance_details procedure is called to
8303   -- create respective balance.
8304   ---------------------------------------------------------------------------+
8305   --
8306   type bal_type is table of varchar2(100) index by binary_integer;
8307   tab_bal_name bal_type ;
8308   --
8309   ---------------------------------------------------------------------------+
8310   --- This table is used to store the actual balance names(Bug #2454595)
8311   --- If any balances are added further, then corresponding archive item
8312   --- PL/SQL table should be updated.
8313   ---------------------------------------------------------------------------+
8314   --
8315   type bal_actual_type is table of varchar2(100) index by binary_integer;
8316   tab_bal_actual_name       bal_actual_type ;
8317   l_bbr_action_sequence     pay_assignment_actions.action_sequence%type;        --3701869
8318 
8319   lv_trans_etp_flag      varchar2(1);
8320   lv_part_of_prev_etp_flag varchar2(1);
8321 
8322   -- bug9147430
8323   l_payg_fw_type  varchar2(10) := 'P';
8324   l_fw_f_type varchar2(1);
8325   l_fw_j_type varchar2(1);
8326 
8327 begin
8328   g_debug :=  hr_utility.debug_enabled;
8329   if g_debug then
8330      hr_utility.set_location(l_procedure, 1);
8331      hr_utility.set_location(l_procedure ||' => assignment action:'||p_assignment_action_id,2);
8332   end if;
8333   --
8334   /*Begin 8315198 - Cursor code is moved here so that the new 2009 PS balances are initialized in pl/sql table
8335                   only if the year start date is FY 2009 or greater*/
8336   open  c_action(p_assignment_action_id);
8337   fetch c_action
8338   into  l_business_group_id
8339   ,     l_registered_employer
8340   ,     l_employee_type
8341   ,     l_payroll_action_id
8342   ,     l_assignment_id
8343   ,     l_year_start
8344   ,     l_year_end
8345   ,     l_lst_yr_term;              --3661230
8346   close c_action;
8347   /*End 8315198*/
8348 
8349   lv_trans_etp_flag        :='N';
8350   lv_part_of_prev_etp_flag :='N';
8351 
8352   tab_bal_name(1) :='X_FRINGE_BENEFITS_ASG_YTD';
8353   tab_bal_name(2) :='X_CDEP_ASG_YTD';
8354   tab_bal_name(3) :='X_EARNINGS_TOTAL_ASG_YTD';
8355   tab_bal_name(4) :='X_LUMP_SUM_A_DEDUCTIONS_ASG_YTD';
8356   tab_bal_name(5) :='X_LUMP_SUM_A_PAYMENTS_ASG_YTD';
8357   tab_bal_name(6) :='X_LUMP_SUM_B_DEDUCTIONS_ASG_YTD';
8358   tab_bal_name(7) :='X_LUMP_SUM_B_PAYMENTS_ASG_YTD';
8359   tab_bal_name(8) :='X_LUMP_SUM_D_PAYMENTS_ASG_YTD';
8360   tab_bal_name(9) :='X_TOTAL_TAX_DEDUCTIONS_ASG_YTD';
8361   tab_bal_name(10):='X_OTHER_INCOME_ASG_YTD';
8362   tab_bal_name(11):='X_UNION_FEES_ASG_YTD';
8363   tab_bal_name(12):='X_INVALIDITY_PAYMENTS_ASG_YTD';
8364   tab_bal_name(13):='X_LUMP_SUM_C_PAYMENTS_ASG_YTD';
8365   tab_bal_name(14):='X_LUMP_SUM_C_DEDUCTIONS_ASG_YTD';
8366   tab_bal_name(15):='X_WORKPLACE_GIVING_DEDUCTIONS_ASG_YTD'; /*4015082 */
8367 /* Begin 6192381
8368   tab_bal_name(16):='X_ETP_DED_TRANS_PPTERM_ASG_YTD';
8369   tab_bal_name(17):='X_ETP_DED_TRANS_NOT_PPTERM_ASG_YTD';
8370   tab_bal_name(18):='X_ETP_DED_NOT_TRANS_PPTERM_ASG_YTD';
8371   tab_bal_name(19):='X_ETP_DED_NOT_TRANS_NOT_PPTERM_ASG_YTD';
8372   tab_bal_name(20):='X_INV_PAY_NOT_TRANS_NOT_PPTERM_ASG_YTD';
8373   tab_bal_name(21):='X_INV_PAY_NOT_TRANS_PPTERM_ASG_YTD';
8374   tab_bal_name(22):='X_INV_PAY_TRANS_NOT_PPTERM_ASG_YTD';
8375   tab_bal_name(23):='X_INV_PAY_TRANS_PPTERM_ASG_YTD';
8376 End 6192381 */
8377 
8378 /* Begin 8315198  - Archive the new DB items for new balances from FY 2009 onwards*/
8379 if (to_number(to_char(l_year_start,'YYYY')) >= 2009) then
8380   tab_bal_name(16):='X_RPT_EMPLOYER_SUPERANN_CONTR_ASG_YTD';
8381   tab_bal_name(17):='X_EXEMPT_FOREIGN_EMPLOY_INC_ASG_YTD';
8382 end if;
8383 
8384 /* End 8315198 */
8385 
8386 /* bug 14703826 - Archive the new DB items for new balances from FY 2012 onwards */
8387 if (to_number(to_char(l_year_start,'YYYY')) >= 2012) then
8388   tab_bal_name(18):='X_ETP_DED_EXCL_ASG_YTD';
8389   tab_bal_name(19):='X_ETP_DED_EXCL_PP_ASG_YTD';
8390   tab_bal_name(20):='X_ETP_DED_NON_EXCL_ASG_YTD';
8391   tab_bal_name(21):='X_ETP_DED_NON_EXCL_PP_ASG_YTD';
8392 end if;
8393 
8394   --
8395   ---------------------------------------------------------------------------+
8396   --- Hard Coded Balance names(Bug #2454595)
8397   ---------------------------------------------------------------------------+
8398   tab_bal_actual_name(1) := 'Fringe Benefits';
8399   tab_bal_actual_name(2) := 'CDEP';
8400   tab_bal_actual_name(3) := 'Earnings_Total';
8401   tab_bal_actual_name(4) := 'Lump Sum A Deductions';
8402   tab_bal_actual_name(5) := 'Lump Sum A Payments';
8403   tab_bal_actual_name(6) := 'Lump Sum B Deductions';
8404   tab_bal_actual_name(7) := 'Lump Sum B Payments';
8405   tab_bal_actual_name(8) := 'Lump Sum D Payments';
8406   tab_bal_actual_name(9) := 'Total_Tax_Deductions';
8407   tab_bal_actual_name(10):= 'Other Income';
8408   tab_bal_actual_name(11):= 'Union Fees';
8409   tab_bal_actual_name(12):= 'Invalidity Payments';
8410   tab_bal_actual_name(13):= 'Lump Sum C Payments';
8411   tab_bal_actual_name(14):= 'Lump Sum C Deductions';
8412   tab_bal_actual_name(15):= 'Workplace Giving Deductions'; /* 4015082 */
8413 
8414 /* Begin 6192381
8415   tab_bal_actual_name(16):= 'ETP Deductions Transitional Part of Prev Term';
8416   tab_bal_actual_name(17):= 'ETP Deductions Transitional Not Part of Prev Term';
8417   tab_bal_actual_name(18):= 'ETP Deductions Life Benefit Part of Prev Term';
8418   tab_bal_actual_name(19):= 'ETP Deductions Life Benefit Not Part of Prev Term';
8419   tab_bal_actual_name(20):= 'Invalidity Payments Life Benefit Not Part of Prev Term';
8420   tab_bal_actual_name(21):= 'Invalidity Payments Life Benefit Part of Prev Term';
8421   tab_bal_actual_name(22):= 'Invalidity Payments Transitional Not Part of Prev Term';
8422   tab_bal_actual_name(23):= 'Invalidity Payments Transitional Part of Prev Term';
8423 End 6192381 */
8424 
8425 /* Begin 8315198 - Consider the new balances  for archival from FY 2009 onwards*/
8426 if (to_number(to_char(l_year_start,'YYYY')) >= 2009) then
8427   tab_bal_actual_name(16):= 'Reportable Employer Superannuation Contributions';
8428   tab_bal_actual_name(17):= 'Exempt Foreign Employment Income';
8429 end if;
8430 /* End 8315198 */
8431 
8432 /* bug 14703826 - new ETP balances from FY 2012 onwards */
8433 if (to_number(to_char(l_year_start,'YYYY')) >= 2012) then
8434   tab_bal_actual_name(18):= 'ETP Deductions Excluded';
8435   tab_bal_actual_name(19):= 'ETP Deductions Excluded Part of Prev';
8436   tab_bal_actual_name(20):= 'ETP Deductions Non Excluded';
8437   tab_bal_actual_name(21):= 'ETP Deductions Non Excluded Part of Prev';
8438 end if;
8439 
8440 
8441 /* Bug 6470581 - Added Changes for Amended Payment Summary
8442       i.  Initialized Amended PS PL/SQL table
8443       ii. Archive Payment Summary Flag DB Items
8444 */
8445 
8446 
8447         p_all_dbi_tab.delete;
8448 
8449 IF g_payment_summary_type = 'O'
8450 THEN
8451 
8452         create_extract_archive_details
8453               (p_assignment_action_id
8454               ,'X_PAYMENT_SUMMARY_TYPE'
8455               ,g_payment_summary_type
8456               );
8457 
8458         create_extract_archive_details
8459               (p_assignment_action_id
8460               ,'X_PAYG_PAYMENT_SUMMARY_TYPE'
8461               ,g_payment_summary_type
8462               );
8463 
8464         create_extract_archive_details
8465               (p_assignment_action_id
8466               ,'X_ETP1_PAYMENT_SUMMARY_TYPE'
8467               ,g_payment_summary_type
8468               );
8469 
8470         create_extract_archive_details
8471               (p_assignment_action_id
8472               ,'X_ETP2_PAYMENT_SUMMARY_TYPE'
8473               ,g_payment_summary_type
8474               );
8475 
8476         create_extract_archive_details
8477               (p_assignment_action_id
8478               ,'X_ETP3_PAYMENT_SUMMARY_TYPE'
8479               ,g_payment_summary_type
8480               );
8481 
8482         create_extract_archive_details
8483               (p_assignment_action_id
8484               ,'X_ETP4_PAYMENT_SUMMARY_TYPE'
8485               ,g_payment_summary_type
8486               );
8487 
8488         create_extract_archive_details  -- bug9147430
8489               (p_assignment_action_id
8490               ,'X_FW1_PAYMENT_SUMMARY_TYPE'
8491               ,g_payment_summary_type
8492               );
8493 
8494         create_extract_archive_details  --bug9147430
8495               (p_assignment_action_id
8496               ,'X_FW2_PAYMENT_SUMMARY_TYPE'
8497               ,g_payment_summary_type
8498               );
8499 
8500 END IF;
8501 
8502 /* End changes Bug 6470581 */
8503 
8504 /* bug9147430  - Initialize variables and check FW status from the financial year 2010/2011 */
8505      g_fw_check := 'N';
8506      g_fw_fbt_term_check := 'NN';
8507      g_fw_fbt_check := 'NN';
8508      g_net_balance := 0;  -- variable to store total eoy values including fw for targetted archive names
8509      g_fw_fbt_balance := 0;
8510      g_fw_union_value :=0;
8511 
8512 if (to_number(to_char(l_year_start,'YYYY')) >= 2010) then
8513 
8514     g_fw_check := pay_au_foreign_workers.check_foreign_worker(l_assignment_id, l_registered_employer, l_year_start, l_year_end);
8515 
8516     if g_debug then
8517       hr_utility.set_location('FW EMP check .. g_fw_check for assignment_id '||l_assignment_id||' is '||g_fw_check, 2);
8518     end if;
8519 
8520 end if;
8521 
8522   -- 8315198 - Commented and moved the c_acion cursor code before initalization of PS balances in PL/SQL table
8523 /*open  c_action(p_assignment_action_id);
8524   fetch c_action
8525   into  l_business_group_id
8526   ,     l_registered_employer
8527   ,     l_employee_type
8528   ,     l_payroll_action_id
8529   ,     l_assignment_id
8530   ,     l_year_start
8531   ,     l_year_end
8532   ,     l_lst_yr_term;              --3661230
8533   close c_action;*/
8534   --
8535   -- 3701869
8536   --
8537   l_lst_yr_term := nvl(l_lst_yr_term,'Y');
8538   --
8539   -- 3661230
8540   --
8541   if l_lst_yr_term = 'Y' then
8542     v_lst_year_start := add_months(l_year_start,-12); -- 3263659
8543     l_fbt_year_start := to_date('01-04-'||to_char(l_year_start,'YYYY'),'DD-MM-YYYY');
8544   else
8545     v_lst_year_start := to_date('01-01-1900','DD-MM-YYYY');
8546     l_fbt_year_start := to_date('01-01-1900','DD-MM-YYYY');
8547   end if;
8548   --
8549   if g_debug then
8550     hr_utility.set_location(l_procedure ||' => 1 assignment_id :' || l_assignment_id, 2);
8551   end if;
8552   --------------------------------------------------------+
8553   -- archival of data for terminated employees
8554   --------------------------------------------------------+
8555   --
8556   if (l_employee_type <> 'C') then
8557     --
8558     -- 2646912
8559     -- Store the value of defined balance id for using for next assignments
8560     -- lump_sum_c_found is used to indicate whether Lump Sum C Payment amount exists for the employee
8561     --
8562     if pkg_lump_sum_c_def_bal_id is null or pkg_lump_sum_c_def_bal_id = 0 then
8563       if g_debug then
8564          hr_utility.set_location(l_procedure ,6 );
8565       end if;
8566       --
8567       open  balance_exists;
8568       fetch balance_exists
8569       into  pkg_lump_sum_c_def_bal_id;
8570       --
8571       if balance_exists%notfound then
8572         pkg_lump_sum_c_def_bal_id := -1;
8573         if g_debug then
8574           hr_utility.set_location(l_procedure ,7 );
8575         end if;
8576       end if;
8577       close balance_exists;
8578     end if;
8579     --
8580     if pkg_lump_sum_c_def_bal_id <> -1 then
8581       open etp_code
8582       (l_assignment_id
8583       ,v_lst_year_start
8584       ,l_year_start
8585       ,l_year_end
8586       ,pkg_lump_sum_c_def_bal_id
8587       ,l_business_group_id
8588       );                    -- 2856638
8589       fetch etp_code
8590       into l_current_employee_flag
8591       ,    l_actual_termination_date
8592       ,    l_date_start
8593       ,    l_death_benefit_type
8594       ,    l_max_assignment_action_id       -- 3019374
8595       ,    l_final_process_date;            -- 3263659
8596       --
8597       if etp_code%found then            -- 3019374
8598         if (pay_balance_pkg.get_value(pkg_lump_sum_c_def_bal_id, l_max_assignment_action_id, l_registered_employer,null,null,null,null) > 0) then --2610141
8599           lump_sum_c_found := true;
8600           if g_debug then
8601             hr_utility.set_location('Lump Sum C Payment found.' ,9 );
8602           end if;
8603         end if;
8604         --
8605         -- 3263659
8606         -- To set the actual termination date so as to archive all balanced in case paid in
8607         -- this year and terminated in last year
8608         --
8609         open  cr_effective_date(l_max_assignment_action_id);
8610         fetch cr_effective_date
8611         into  l_effective_date;
8612         close cr_effective_date;
8613         --
8614         l_fetched_termination_date := l_actual_termination_date;  -- Bug3263659 To store actual date for Pre-post calculation
8615         if (l_actual_termination_date < l_year_start and l_effective_date >= l_year_start ) then
8616           l_actual_termination_date := nvl(l_final_process_date,l_effective_date);  /* Bug 3098353 To set the End Date as
8617                                                                                          Payment Date if final_process is null */
8618 
8619              /* Bug 9872891 - If ETP payments exist in the new financial year - the Term date should
8620                               be the pay date in which ETP payments have been made
8621              */
8622             IF (lump_sum_c_found = TRUE)
8623             THEN
8624                     OPEN c_etp_max_pay_date(l_assignment_id
8625                                            ,l_registered_employer
8626                                            ,l_year_start
8627                                            ,l_year_end);
8628                     FETCH c_etp_max_pay_date  INTO l_etp_effective_date;
8629                     CLOSE c_etp_max_pay_date;
8630 
8631                 l_actual_termination_date   := NVL(l_etp_effective_date,l_actual_termination_date);
8632 
8633             END IF;
8634             /* End Bug 9872891 */
8635 
8636         end if;
8637       end if;
8638       close etp_code;
8639     end if;
8640 
8641     if lump_sum_c_found = false then
8642       if g_debug then
8643         hr_utility.set_location('Lump Sum C Payment balance does not exists ',101);
8644       end if;
8645     end if;
8646     --
8647     /* Bug3263659 l_actual_termination_date AND clause added to prevent etp being archive in case its
8648                    terminated in last year and no runs in current year
8649     */
8650     /* Bug 6112527  Added condition to_number(to_char(l_year_start,'YYYY')) >= 2007 for archive death benefit type D only after Fin Year 2007/2008 */
8651 
8652 if (((l_death_benefit_type <>'D' or to_number(to_char(l_year_start,'YYYY')) >= 2007)  or l_death_benefit_type is null) and (lump_sum_c_found = true)
8653       and not (l_actual_termination_date between l_fbt_year_start and to_date('30-06-'||to_char(l_year_start,'YYYY'),'DD-MM-YYYY')) ) then   --Bug#3661230
8654       --
8655       -- 2448446 , 2646912
8656       --
8657       if g_debug then
8658         hr_utility.set_location('creating etp details for assignment id: ' ||l_assignment_id, 7);
8659       end if;
8660       --
8661       ------------------------------------------------------+
8662       -- call procedure to archive details of terminated employees
8663       -- archive etp details  procedure archives employee related details
8664       -- archive prespost details archives prejul83 and postjun83 information
8665       -------------------------------------------------------+
8666       archive_etp_details
8667       (l_business_group_id
8668       ,l_registered_employer
8669       ,l_payroll_action_id
8670       ,p_assignment_action_id
8671       ,l_assignment_id
8672       ,l_year_start
8673       ,l_year_end
8674       ,v_lst_year_start /*Bug3661230 Added one extra parameter*/
8675       ,lv_trans_etp_flag  /*Bug 6192381 Added New Parameters lv_trans_etp_flag and lv_part_of_prev_etp_flag */
8676       ,lv_part_of_prev_etp_flag
8677       );
8678       --
8679       l_term_date := to_char(l_actual_termination_date,'DDMMYYYY');       -- Bug3263659 TO Archive modified termination date
8680       --
8681       create_extract_archive_details
8682       (p_assignment_action_id
8683       ,'X_ETP_EMPLOYEE_END_DATE'
8684       ,l_term_date
8685       );
8686       --
8687       if g_debug then
8688         hr_utility.set_location('creating prejul83 and post jun 83 details for assignment id :' ||l_assignment_id, 7);
8689       end if;
8690       --
8691       /* bug 14703826 - the new procedure is called for ETP changes for FY 2012 onwards */
8692       if (to_number(to_char(l_year_start,'YYYY')) < 2012) then
8693           archive_prepost_details
8694           (p_assignment_action_id
8695           ,l_max_assignment_action_id           --2610141
8696           ,l_registered_employer                --2610141
8697           ,g_legislation_code
8698           ,l_assignment_id
8699           ,l_payroll_action_id
8700           ,l_fetched_termination_date               --3263659
8701           ,l_date_start
8702           ,l_year_start
8703           ,l_year_end
8704           ,lv_trans_etp_flag /*Bug 6192381 Added New Parameters lv_trans_etp_flag and lv_part_of_prev_etp_flag */
8705           ,lv_part_of_prev_etp_flag);
8706        else
8707           archive_prepost_details_2012
8708           (p_assignment_action_id
8709           ,l_max_assignment_action_id
8710           ,l_registered_employer
8711           ,g_legislation_code
8712           ,l_assignment_id
8713           ,l_payroll_action_id
8714           ,l_fetched_termination_date
8715           ,l_date_start
8716           ,l_year_start
8717           ,l_year_end);
8718              end if;
8719     end if;
8720   end if;   /*  (l_employee_type <> 'C')  */
8721   --
8722   ----------------------------------------------------------------------------
8723   -- if the employee has been terminated in the FBT year then archive only the
8724   -- Fringe Benefits balance , other balances should be zero
8725   -- else archive all balances
8726   ----------------------------------------------------------------------------
8727   if (l_actual_termination_date is not null) and
8728            (l_actual_termination_date between  l_fbt_year_start and to_date('30-06-'||to_char(l_year_start,'YYYY'),'DD-MM-YYYY')) then  --Bug#3661230
8729 
8730   /* bug9147430 - Check FW status for terminated employee with FB before the financial year */
8731    if (to_number(to_char(l_year_start,'YYYY')) >= 2010) then
8732 
8733               l_fw_f_type := pay_au_foreign_workers.check_foreign_worker(l_assignment_id, l_registered_employer, add_months(l_year_start,-3), (l_year_start - 1),
8734                                                   pay_au_foreign_workers.g_fw_f_type);
8735               l_fw_j_type := pay_au_foreign_workers.check_foreign_worker(l_assignment_id, l_registered_employer, add_months(l_year_start,-3), (l_year_start - 1),
8736                                                   pay_au_foreign_workers.g_fw_j_type);
8737 
8738                if l_fw_f_type = 'Y' then
8739                 if l_fw_j_type = 'Y' then
8740                   g_fw_fbt_term_check := 'YY';
8741                 else
8742                   g_fw_fbt_term_check := 'YN';
8743                 end if;
8744                elsif l_fw_j_type = 'Y' then
8745                 g_fw_fbt_term_check := 'NY';
8746                else
8747                 g_fw_fbt_term_check := 'NN';
8748                end if;
8749 
8750    end if;
8751 
8752     --
8753     -- 4738470 - Get the Maximum assignment_action_id for FBT employee
8754     --
8755     open  c_max_asg_action_id
8756     (     l_assignment_id
8757     ,     l_business_group_id
8758     ,     l_registered_employer
8759     ,     add_months(l_year_start,-3)
8760     ,     (l_year_start - 1)
8761     );
8762     fetch c_max_asg_action_id
8763     into  l_fbt_assignment_action_id
8764     ,     l_bbr_action_sequence;
8765     close c_max_asg_action_id;
8766     --
8767     -- 2610141
8768     --
8769     archive_balance_details
8770     (p_assignment_action_id
8771     ,l_fbt_assignment_action_id             --2610141
8772     ,l_registered_employer              --2610141
8773     ,tab_bal_name(1)                    -- X_FRINGE_BENEFITS_ASG_YTD
8774     ,tab_bal_actual_name(1)                 -- 2454595
8775     ,g_legislation_code
8776     ,l_year_start
8777     ,l_year_end
8778     ,l_assignment_id
8779     ,l_payroll_action_id
8780     ,l_bal_value
8781     );                          --3098353
8782     --
8783     l_net_balance := l_bal_value;           --2610141
8784     l_actual_termination_date := l_year_start;      --3098353 - Since in case of only FBT employee,
8785                                                         -- period dates should be year start date.
8786     --
8787     -- 4866934 - Set the Balance Values to 0 for FBT Employee
8788     --
8789     p_result_table.delete;
8790     for i in p_balance_value_tab.first..p_balance_value_tab.last
8791     loop
8792       p_result_table(i).balance_value := 0;
8793     end loop;
8794     --
8795   else              -- 3172963
8796     if g_debug then
8797       hr_utility.set_location('Get assignment_action_id for BBR call, for assignment id: ' || l_assignment_id, 11);
8798     end if;
8799     --
8800     -- 4738470 - Get the Maximum assignment_action_id
8801     --
8802     open c_max_asg_action_id
8803     (l_assignment_id
8804     ,l_business_group_id
8805     ,l_registered_employer
8806     ,l_year_start
8807     ,l_year_end
8808     );
8809     fetch c_max_asg_action_id
8810     into  l_bbr_assignment_action_id
8811     ,     l_bbr_action_sequence;
8812     close c_max_asg_action_id;
8813     --
8814     if g_debug then
8815       hr_utility.set_location(l_procedure, 12);
8816       hr_utility.trace('Using ASSIGNMENT_ACTION_ID: ' || l_bbr_assignment_action_id);
8817     end if;
8818     --
8819     -- 4866934 - Flush balance values in PL/SQL table
8820     --
8821     p_result_table.delete;
8822     --
8823     -- Changes made for bug 2610141 Start here
8824     --
8825     p_context_table(1).tax_unit_id := l_registered_employer;
8826     --
8827     pay_balance_pkg.get_value
8828     (p_assignment_action_id     => l_bbr_assignment_action_id
8829     ,p_defined_balance_lst  => p_balance_value_tab
8830     ,p_context_lst      => p_context_table
8831     ,p_output_table         => p_result_table
8832     );
8833     --
8834     -- Changes made for bug 2610141 Ends here
8835     --
8836     if g_debug then
8837       hr_utility.set_location(l_procedure, 13);
8838       hr_utility.trace('------------------------------------------------');
8839       hr_utility.trace('CDEP                    ===>' || p_result_table(1).balance_value);
8840       hr_utility.trace('Leave Payments Marginal ===>' || p_result_table(2).balance_value);
8841       hr_utility.trace('Lump Sum A Deductions   ===>' || p_result_table(3).balance_value);
8842       hr_utility.trace('Lump Sum A Payments     ===>' || p_result_table(4).balance_value);
8843       hr_utility.trace('Lump Sum B Deductions   ===>' || p_result_table(5).balance_value);
8844       hr_utility.trace('Lump Sum B Payments     ===>' || p_result_table(6).balance_value);
8845       hr_utility.trace('Lump Sum C Deductions   ===>' || p_result_table(7).balance_value);
8846       hr_utility.trace('Lump Sum C Payments     ===>' || p_result_table(8).balance_value);
8847       hr_utility.trace('Lump Sum D Payments     ===>' || p_result_table(9).balance_value);
8848       hr_utility.trace('Total_Tax_Deduction     ===>' || p_result_table(10).balance_value);
8849       hr_utility.trace('Termination Deductions  ===>' || p_result_table(11).balance_value);
8850       hr_utility.trace('Exempt Foreign Emp. Inc ===>' || p_result_table(12).balance_value);
8851       hr_utility.trace('Union Fees              ===>' || p_result_table(13).balance_value);
8852       hr_utility.trace('Invalidity Payments     ===>' || p_result_table(14).balance_value);
8853       hr_utility.trace('Lump Sum E Payments     ===>' || p_result_table(15).balance_value);
8854       hr_utility.trace('Earnings_Total          ===>' || p_result_table(16).balance_value);
8855       hr_utility.trace('Workplace Giving        ===>' || p_result_table(17).balance_value); /* 4015082 */
8856       hr_utility.trace('ETP Deductions Transitional Not Part of Prev Term      ===>' || p_result_table(18).balance_value);
8857       hr_utility.trace('ETP Deductions Transitional Part of Prev Term          ===>' || p_result_table(19).balance_value);
8858       hr_utility.trace('ETP Deductions Life Benefit Not Part of Prev Term      ===>' || p_result_table(20).balance_value);
8859       hr_utility.trace('ETP Deductions Life Benefit Part of Prev Term          ===>' || p_result_table(21).balance_value);
8860       hr_utility.trace('Invalidity Payments Life Benefit Not Part of Prev Term  ===>' || p_result_table(22).balance_value);
8861       hr_utility.trace('Invalidity Payments Life Benefit Part of Prev Term      ===>' || p_result_table(23).balance_value);
8862       hr_utility.trace('Invalidity Payments Transitional Not Part of Prev Term  ===>' || p_result_table(24).balance_value);
8863       hr_utility.trace('Invalidity Payments Transitional Part of Prev Term      ===>' || p_result_table(25).balance_value);
8864       hr_utility.trace('Reportable Employer Superannuation Contributions        ===>' || p_result_table(26).balance_value);
8865       hr_utility.trace('Retro Earnings Leave Loading GT 12 Mths Amount          ===>' || p_result_table(27).balance_value); -- start 8711855
8866       hr_utility.trace('Retro Earnings Spread GT 12 Mths Amount                 ===>' || p_result_table(28).balance_value);
8867       hr_utility.trace('Retro Pre Tax GT 12 Mths Amount                         ===>' || p_result_table(29).balance_value);        -- end 8711855
8868       hr_utility.trace('Foreign Leave Payments                                  ===>' || p_result_table(30).balance_value);
8869       hr_utility.trace('Foreign Leave Payments Marginal                         ===>' || p_result_table(31).balance_value);
8870       hr_utility.trace('Foreign Lump Sum A Payments                             ===>' || p_result_table(32).balance_value);
8871       hr_utility.trace('Foreign Leave Component Deduction                       ===>' || p_result_table(33).balance_value);
8872       hr_utility.trace('Foreign Lump Sum A Deduction                            ===>' || p_result_table(34).balance_value);
8873       hr_utility.trace('Retro Earnings Additional GT 12 Mths Amount             ===>' || p_result_table(35).balance_value);
8874         hr_utility.trace('ETP Deductions Excluded                                 ===>' || p_result_table(36).balance_value); /* start bug 14703826 */
8875       hr_utility.trace('ETP Deductions Excluded Part of Prev                    ===>' || p_result_table(37).balance_value);
8876         hr_utility.trace('ETP Deductions Non Excluded                             ===>' || p_result_table(38).balance_value);
8877         hr_utility.trace('ETP Deductions Non Excluded Part of Prev                ===>' || p_result_table(39).balance_value);  /* end bug 14703826 */
8878     end if;
8879 
8880       if g_debug then
8881         hr_utility.set_location('lv_trans_etp_flag :' ||lv_trans_etp_flag, 7);
8882         hr_utility.set_location('lv_part_of_prev_etp_flag:' ||lv_part_of_prev_etp_flag, 7);
8883       end if;
8884 
8885 /*Bug 6192381 Procedure to adjust old etp deduction and old Invalidity Payments */
8886       adjust_old_etp_values(lv_trans_etp_flag
8887                             ,lv_part_of_prev_etp_flag);
8888     --
8889     for cnt in 1..tab_bal_name.count
8890     loop
8891       if g_debug then
8892         hr_utility.set_location('creating '||tab_bal_name(cnt)||' balance for ass action id: '|| l_bbr_assignment_action_id,14);
8893         hr_utility.set_location('Death benefit type:'||l_death_benefit_type,113);
8894       end if;
8895       --
8896       ---------------------------------+
8897       -- call procedure to archive all balances
8898       ---------------------------------+
8899       ---------------------------------
8900       -- 1956018
8901       ---------------------------------
8902       --
8903 
8904     /* Bug 6112527  Modified  condition that for death benefit type archive balances if Fin Year >= 2007/2008
8905         From 01-Jul-2007 ETP Amount for termination type Death and Benefit type Dependent amount is taxable */
8906     /* Bug 8315198 - Added condition such that the 'Other Income' balance is not archived from 01-Jul-2009 onwards */
8907        if (((l_death_benefit_type = 'D' and to_number(to_char(l_year_start,'YYYY')) <= 2006) and (tab_bal_name(cnt) in
8908                           ('X_LUMP_SUM_C_PAYMENTS_ASG_YTD','X_LUMP_SUM_C_DEDUCTIONS_ASG_YTD'))) or
8909             ((to_number(to_char(l_year_start,'YYYY')) >= 2009) and (tab_bal_name(cnt) in ('X_OTHER_INCOME_ASG_YTD')))) then
8910         null;
8911       else
8912 
8913         if tab_bal_name.exists(cnt) then
8914           --
8915           -- 2610141
8916           --
8917           if tab_bal_name(cnt) = 'X_FRINGE_BENEFITS_ASG_YTD' then
8918 
8919           /* bug9147430 - Check FW status for normal Fringe Benefit tax year */
8920            if (to_number(to_char(l_year_start,'YYYY')) >= 2010) then
8921 
8922               l_fw_f_type := pay_au_foreign_workers.check_foreign_worker(l_assignment_id, l_registered_employer, add_months(l_year_start,-3), (l_year_end - 3),
8923                                                   pay_au_foreign_workers.g_fw_f_type);
8924               l_fw_j_type := pay_au_foreign_workers.check_foreign_worker(l_assignment_id, l_registered_employer, add_months(l_year_start,-3), (l_year_end - 3),
8925                                                   pay_au_foreign_workers.g_fw_j_type);
8926 
8927                if l_fw_f_type = 'Y' then
8928                 if l_fw_j_type = 'Y' then
8929                   g_fw_fbt_check := 'YY';
8930                 else
8931                   g_fw_fbt_check := 'YN';
8932                 end if;
8933                elsif l_fw_j_type = 'Y' then
8934                 g_fw_fbt_check := 'NY';
8935                else
8936                 g_fw_fbt_check := 'NN';
8937                end if;
8938 
8939            end if;
8940 
8941             --
8942             -- 4738470 - Get the FBT Maximum assignment_action_id
8943             --
8944             open c_max_asg_action_id
8945             (l_assignment_id
8946             ,l_business_group_id
8947             ,l_registered_employer
8948             ,add_months(l_year_start,-3)
8949             ,add_months(l_year_end,-3)
8950             );
8951             fetch c_max_asg_action_id
8952             into  l_fbt_assignment_action_id
8953             ,     l_bbr_action_sequence;
8954             close c_max_asg_action_id;
8955             --
8956             archive_balance_details
8957             (p_assignment_action_id
8958             ,l_fbt_assignment_action_id         -- 2610141
8959             ,l_registered_employer          -- 2610141
8960             ,tab_bal_name(1)                -- X_FRINGE_BENEFITS_ASG_YTD
8961             ,tab_bal_actual_name(1)             -- 2454595 Fringe Benefits
8962             ,g_legislation_code
8963             ,l_year_start
8964             ,l_year_end
8965             ,l_assignment_id
8966             ,l_payroll_action_id
8967             ,l_bal_value
8968             ); --3098353
8969           else
8970             --
8971             -- 2610141
8972             --
8973             archive_balance_details
8974             (p_assignment_action_id
8975             ,l_bbr_assignment_action_id             -- 2610141
8976             ,l_registered_employer          -- 2610141
8977             ,tab_bal_name(cnt)
8978             ,tab_bal_actual_name(cnt)               -- 2454595
8979             ,g_legislation_code
8980             ,l_year_start
8981             ,l_year_end
8982             ,l_assignment_id
8983             ,l_payroll_action_id
8984             ,l_bal_value
8985             );                              --3098353
8986           end if;
8987           --
8988           -- 3098353
8989           --
8990           if cnt = 1 then                   -- To store FBT balance value
8991             l_fbt_balance := l_bal_value;
8992           end if;
8993 
8994           /*Begin 8315198 - Added code to get the value of X_LUMP_SUM_A_PAYMENTS_ASG_YTD balance*/
8995 
8996           if cnt = 5 then
8997             l_lump_sum_a_value := l_bal_value;
8998           end if;
8999 
9000           /*End 8315198*/
9001           --
9002           -- 3937976
9003           --
9004           l_net_balance:= l_net_balance + to_number(nvl(l_bal_value,0));
9005           --
9006         end if;
9007       end if;
9008     end loop;
9009     --
9010     -- By default reporting_flag is 'YES' i.e the employee will be displayed in any of the reports.
9011     -- The following statement checks that if employee is terminated in last year and the sum of
9012     -- balances is zero then reporting_flag will be 'NO' i.e. in this case the employee
9013     -- should not be reported in any of the reports.
9014     --
9015     if l_fetched_termination_date < l_year_start and l_net_balance = 0 then
9016       l_reporting_flag := 'NO';
9017     end if;
9018     --
9019     -- 3937976
9020     --
9021     if nvl(l_fetched_termination_date,to_date('31/12/4712','DD/MM/YYYY')) >= l_year_start and l_net_balance = 0 then
9022       l_curr_term_0_bal_flag:='YES';
9023     end if;
9024   end if;
9025 
9026 /* bug9147430 - Call archive_fw_details procedure when FW status is returned 'Y' */
9027 if (to_number(to_char(l_year_start,'YYYY')) >= 2010)
9028     and ( g_fw_check = 'Y'  or g_fw_fbt_check <> 'NN' or g_fw_fbt_term_check <> 'NN') then
9029 
9030      t_fw_gross_type.delete;
9031 
9032      archive_fw_details
9033        (p_assignment_id              => l_assignment_id
9034        ,p_assignment_action_id       => p_assignment_action_id
9035        ,p_registered_employer        => l_registered_employer
9036        ,p_year_start                 => l_year_start
9037        ,p_year_end                   => l_year_end
9038        ,p_fw_check                   => g_fw_check
9039        ,p_actual_termination_date    => l_fetched_termination_date
9040        ,p_fw_alw_bal_exist           => 'N');
9041 
9042 else  -- Cases for negative FW status
9043 
9044              create_extract_archive_details(p_assignment_action_id
9045                                      ,'X_FW_PAYG_TYPE'
9046                                      ,l_payg_fw_type);  -- default to have Individual Payment Summary only
9047 
9048 end if;
9049 
9050   --
9051   -----------------------------------------------
9052   --* Archive Allowance details
9053   -----------------------------------------------
9054   archive_allowance_details
9055   (p_assignment_action_id
9056   ,l_bbr_assignment_action_id       --2610141
9057   ,l_registered_employer            --2610141
9058   ,l_year_start
9059   ,l_year_end
9060   ,l_assignment_id
9061   ,l_alw_bal_exist
9062   );
9063   --
9064   -- Perform archive of 2006 unions setup
9065   --
9066   /*
9067   archive_2006_unions
9068   (p_assignment_id              => l_assignment_id
9069   ,p_assignment_action_id       => p_assignment_action_id
9070   ,p_max_assignment_action_id   => l_bbr_assignment_action_id
9071   ,p_registered_employer        => l_registered_employer
9072   ,p_year_start                 => l_year_start
9073   ,p_year_end                   => l_year_end
9074   ,p_alw_bal_exist              => l_alw_bal_exist
9075   );
9076   */
9077   --
9078   -- 3098353
9079   --
9080   -- This  flag is sets the reporting_flag if the allowance are paid to employee
9081   -- in the current year.
9082   --
9083   if l_alw_bal_exist = 'TRUE' then
9084     l_reporting_flag := 'YES';
9085     l_curr_term_0_bal_flag :='NO';            -- 3937976
9086   end if;
9087   --
9088   -- If only FBT is reported than period end date should be year start date
9089   --
9090   -- Modified for bug 9846470 - Below piece of code must run only for terminated employees.
9091   -- Earlier below code ran even for live employees and archived the end date incorrectly.
9092   -- Also when termination date falls between Apr and June of previous financial year, then
9093   -- end date must be archived as current financial year start date.
9094 IF l_actual_termination_date IS NOT NULL THEN
9095   if (l_net_balance - l_fbt_balance) = 0 and l_alw_bal_exist = 'FALSE' and l_fbt_balance <> 0
9096   and l_actual_termination_date between l_fbt_year_start and to_date('30-06-'||to_char(l_year_start,'YYYY'),'DD-MM-YYYY') then
9097     l_actual_termination_date := l_year_start;
9098     l_term_date := to_char(l_actual_termination_date,'DDMMYYYY');
9099 
9100     if g_debug then
9101       hr_utility.set_location('Creating archive Item X_EMPLOYEE_END_DATE',12);
9102     end if;
9103     --
9104     -- 2610141
9105     --
9106     create_extract_archive_details
9107     (p_assignment_action_id
9108     ,'X_EMPLOYEE_LE_END_DATE'
9109     ,l_term_date
9110     );
9111     l_le_end_date_flag := 'Y';
9112   end if;
9113   --
9114   -- 2610141
9115   --
9116   if l_actual_termination_date <> l_fetched_termination_date and l_le_end_date_flag = 'N' then
9117     l_term_date := to_char(l_actual_termination_date,'DDMMYYYY');
9118     create_extract_archive_details
9119     (p_assignment_action_id
9120     ,'X_EMPLOYEE_LE_END_DATE'
9121     ,l_term_date
9122     );
9123     l_le_end_date_flag := 'Y';
9124   end if;
9125 END IF;
9126   --
9127   -- 2610141
9128   --
9129   if g_debug then
9130     hr_utility.set_location('Creating archive Item X_EMPLOYEE_END_DATE',12);
9131   end if;
9132   --
9133   l_term_date := to_char(l_actual_termination_date,'DDMMYYYY');
9134   create_extract_archive_details
9135   (p_assignment_action_id
9136   ,'X_EMPLOYEE_END_DATE'
9137   ,l_term_date
9138   );
9139   --
9140   create_extract_archive_details
9141   (p_assignment_action_id
9142   ,'X_REPORTING_FLAG'
9143   ,l_reporting_flag
9144   );
9145   --
9146   --3937976
9147   --
9148   create_extract_archive_details
9149   (p_assignment_action_id
9150   ,'X_CURR_TERM_0_BAL_FLAG'
9151   ,l_curr_term_0_bal_flag
9152   );
9153 
9154   --
9155   ------------------------------------------------------+
9156   --call  procedure to archive supplier details
9157   -------------------------------------------------------+
9158   archive_supplier_details
9159   (l_business_group_id
9160   ,l_registered_employer
9161   ,l_payroll_action_id
9162   ,p_assignment_action_id
9163   ,l_assignment_id
9164   ,l_year_start
9165   ,l_year_end
9166   );
9167   --
9168   if g_debug then
9169     hr_utility.set_location('creating employee details for  assignment id: ' || l_assignment_id, 9);
9170   end if;
9171   --
9172   ------------------------------------------------------+
9173   --call  procedure to archive employee details
9174   -------------------------------------------------------+
9175   /*Bug# 4363057 - This logic is introduced to check if the employee is active for the submitted
9176                  legal employer in the current financial year.
9177                  If employee is active for the submitted legal employer then pass the value to
9178                  archive_employee_details.
9179                  If employee is not active for the submitted legal employer then get the maximum and
9180                  minimum payment dates for the legal employer. Get the legal employer effective at the maximum
9181                  payment date and pass it to archive_employee_details
9182   */
9183   /*Bug 4387183 -  Above logic has been modified to take care of terminated employees.
9184                  - FBT employees terminated in the FBT period will be archived with le start and end dates as
9185                    financial year start dates.
9186                  - When Payment Summary is submitted for Employees terminated with final process date as null and
9187                    for legal employer in which they have recieved retropayments but are not active in the current
9188                    financial year then the le start date will be set as financial year start, while the le end date
9189                    will be set as the Retropayment date.
9190                  - When Payment Summary is submitted for Employees terminated with final process date not null and
9191                    for legal employer in which they have recieved retropayments but are not active in the current
9192                    financial year then the le start date will be set as financial year start, while the le end date
9193                    will be set as the final process date.
9194   */
9195   --
9196   open csr_check_eff_le(l_assignment_id, l_registered_employer, l_year_start, l_year_end);
9197   fetch csr_check_eff_le into l_dummy;
9198   if csr_check_eff_le%notfound then
9199     open csr_get_dates(l_assignment_id, l_year_start,l_year_end,l_registered_employer);
9200     fetch csr_get_dates into l_pay_start, l_pay_end;
9201     close csr_get_dates;
9202     --
9203     -- 4387183
9204     -- If the le end date has been archived already, that is for terminated
9205     -- employees then don't archive the start and end dates here. Le start
9206     -- date will be archived in archive_employee_detail
9207     --
9208     if l_le_end_date_flag = 'N' then
9209       --
9210       create_extract_archive_details
9211       (p_assignment_action_id
9212       ,'X_EMPLOYEE_LE_START_DATE'
9213       ,l_pay_start
9214       );
9215       create_extract_archive_details
9216       (p_assignment_action_id
9217       ,'X_EMPLOYEE_LE_END_DATE'
9218       ,l_pay_end
9219       );
9220       l_le_end_date_flag := 'B';
9221     end if;
9222     --
9223     open csr_get_end_le(l_assignment_id, l_year_end,l_fbt_year_start);
9224     fetch csr_get_end_le into l_current_le;
9225     close csr_get_end_le;
9226     --
9227     archive_employee_details
9228     (l_business_group_id
9229     ,l_current_le
9230     ,l_payroll_action_id
9231     ,p_assignment_action_id
9232     ,l_assignment_id
9233     ,l_year_start
9234     ,l_year_end
9235     ,l_le_end_date_flag
9236     ,l_fbt_year_start                       -- 4653934
9237     );
9238   else
9239     archive_employee_details
9240     (l_business_group_id
9241     ,l_registered_employer                  --2610141
9242     ,l_payroll_action_id
9243     ,p_assignment_action_id
9244     ,l_assignment_id
9245     ,l_year_start
9246     ,l_year_end
9247     ,l_le_end_date_flag
9248     ,l_fbt_year_start                       -- 4653934
9249     );
9250   end if;
9251   close csr_check_eff_le;
9252   --
9253   -- 4363057
9254   --
9255   if g_debug then
9256     hr_utility.set_location('creating employer details for assignment id: ' || l_assignment_id, 10);
9257   end if;
9258   --
9259   ------------------------------------------------------+
9260   --call  procedure to archive employer details
9261   -------------------------------------------------------+
9262   --
9263   archive_employer_details
9264   (l_business_group_id
9265   ,l_bbr_assignment_action_id   --2610141
9266   ,l_registered_employer        --2610141
9267   ,l_payroll_action_id
9268   ,p_assignment_action_id
9269   ,l_assignment_id
9270   ,l_year_start
9271   ,l_year_end
9272   );
9273 
9274   /*Begin 8315198 - Added code to assign the value of lump sum a payment type depending on termination type
9275                     and archive the value using the X_LUMP_SUM_A_PAYMENT_TYPE db item from FY 2009 onwards*/
9276 
9277 if (to_number(to_char(l_year_start,'YYYY')) >= 2009) then
9278   IF l_lump_sum_a_value > 0 THEN
9279       IF g_termination_type = 'AU_B' or g_termination_type = 'AU_I' THEN
9280         l_lump_sum_a_pymt_type := 'R';
9281       ELSE
9282         l_lump_sum_a_pymt_type := 'T';
9283       END IF;
9284   END IF;
9285 
9286   create_extract_archive_details(p_assignment_action_id
9287                                 ,'X_LUMP_SUM_A_PAYMENT_TYPE'
9288                                 ,l_lump_sum_a_pymt_type);
9289 
9290   if g_debug then
9291     hr_utility.set_location('Creating archive Item X_LUMP_SUM_A_PAYMENT_TYPE',12);
9292   end if;
9293 end if;
9294  /*End 8315198*/
9295 
9296 /* Bug 6470581 -Call Amended Payment Summary manipulation logic
9297                 if this is Amended Payment Summary Run */
9298 
9299 IF g_payment_summary_type = 'A'
9300 THEN
9301 
9302     pay_au_payment_summary_amend.modify_and_archive_code
9303                 (p_assignment_action_id   => p_assignment_action_id
9304                 ,p_effective_date         => p_effective_date
9305                 ,p_all_tab_new            => p_all_dbi_tab);
9306 
9307 END IF;
9308 
9309   if g_debug then
9310     hr_utility.set_location('End of archive code', 37);
9311   end if;
9312 
9313 exception
9314   when others then
9315   if g_debug then
9316      hr_utility.set_location('error in archive code - assignment id :' ||l_assignment_id,11);
9317   end if;
9318   raise;
9319 end archive_code;
9320 
9321   --------------------------------------------------------------------+
9322   -- This function is used to get end of year values for archive items
9323   -- called from validation report and payment summary report
9324   --------------------------------------------------------------------+
9325 
9326 
9327 function get_archive_value(p_user_entity_name     in ff_user_entities.user_entity_name%type,
9328                            p_assignment_action_id in pay_assignment_actions.assignment_action_id%type)
9329 return varchar2 is
9330 
9331 
9332   -- cursor to fetch the archive value
9333 
9334   cursor   csr_get_value(p_user_entity_name varchar2,
9335                          p_assignment_action_id number) is
9336   select   fai.value
9337     from   ff_archive_items fai,
9338            ff_user_entities fue
9339    where   fai.context1         = p_assignment_action_id
9340      and   fai.user_entity_id   = fue.user_entity_id
9341      and   fue.user_entity_name = p_user_entity_name;
9342 
9343 
9344   l_value            ff_archive_items.value%type;
9345   e_no_value_found   exception;
9346 
9347 
9348 begin
9349 
9350    g_debug := hr_utility.debug_enabled;
9351 
9352    IF g_debug THEN
9353        hr_utility.set_location('Start of get archive value ',1);
9354    END if;
9355 
9356   open   csr_get_value(p_user_entity_name,
9357                        p_assignment_action_id);
9358   fetch  csr_get_value into l_value;
9359 
9360   if  csr_get_value%notfound then
9361     l_value := null;
9362     close csr_get_value;
9363     raise e_no_value_found;
9364   else
9365     close csr_get_value;
9366   end if;
9367   return(l_value);
9368     IF g_debug THEN
9369       hr_utility.set_location('End of get archive value ',2);
9370      END if;
9371 
9372 
9373 exception
9374   when e_no_value_found then
9375    IF g_debug THEN
9376         hr_utility.set_location('error in get archive value  - assignment_action_id:' ||p_assignment_action_id,3);
9377         hr_utility.set_location('error in get archive value  - user entity name    :' ||p_user_entity_name,3);
9378     END if;
9379     return (null);
9380   when others then
9381     IF g_debug THEN
9382         hr_utility.set_location('error in get archive value  - assignment_action_id:' ||p_assignment_action_id,3);
9383         hr_utility.set_location('error in get archive value  - user entity name    :' ||p_user_entity_name,3);
9384     END if;
9385     return (null);
9386     Raise;
9387 end  get_archive_value;
9388 
9389 
9390 
9391 -- Following Procedure is created as per enhancement - Bug#3132178
9392 -- This procedure checks if TEST_EFILE flag is TRUE then submit a request for Magtape Process
9393 
9394 procedure spawn_data_file
9395 (p_payroll_action_id in pay_payroll_actions.payroll_action_id%type)
9396   is
9397 
9398  ps_request_id          NUMBER;
9399  l_payroll_action_id    pay_payroll_actions.payroll_action_id%TYPE;
9400  l_business_group_id    number;
9401  l_start_date       date;
9402  l_end_date         date;
9403  l_effective_date   date;
9404  l_legal_employer   number;
9405  l_FINANCIAL_YEAR_code  varchar2(10);
9406  l_TEST_EFILE       varchar2(10);
9407  l_FINANCIAL_YEAR   varchar2(10);
9408  l_legislative_param    varchar2(200);
9409 
9410   --------------------------------------------------------------------+
9411   -- Cursor      : csr_params
9412   -- Description : Fetches User Parameters from Legislative_paramters
9413   --               column.
9414   --------------------------------------------------------------------+
9415 
9416    CURSOR csr_magtape_params(c_payroll_action_id  pay_payroll_actions.payroll_action_id%TYPE)
9417       IS
9418         SELECT pay_core_utils.get_parameter('TEST_EFILE',legislative_parameters)        TEST_EFILE,
9419                pay_core_utils.get_parameter('BUSINESS_GROUP_ID',legislative_parameters)        BUSINESS_GROUP_ID,
9420            pay_core_utils.get_parameter('FINANCIAL_YEAR',legislative_parameters)  FINANCIAL_YEAR,
9421                pay_core_utils.get_parameter('REGISTERED_EMPLOYER',legislative_parameters)    REGISTERED_EMPLOYER,
9422                to_date(pay_core_utils.get_parameter('START_DATE',legislative_parameters),'YYYY/MM/DD') start_date,
9423                to_date(pay_core_utils.get_parameter('END_DATE',legislative_parameters),'YYYY/MM/DD')   end_date,
9424                to_date(pay_core_utils.get_parameter('EFFECTIVE_DATE',legislative_parameters),'YYYY/MM/DD')   EFFECTIVE_DATE
9425        FROM   pay_payroll_actions ppa
9426        WHERE ppa.payroll_action_id  =  c_payroll_action_id;
9427 
9428 
9429   CURSOR csr_lookup_code (c_financial_year varchar2)
9430     IS
9431        SELECT LOOKUP_CODE
9432        FROM HR_LOOKUPS
9433        WHERE lookup_type = 'AU_PS_FINANCIAL_YEAR'
9434        AND enabled_flag = 'Y'
9435        AND meaning =c_financial_year;
9436 
9437  Begin
9438 
9439    ps_request_id :=-1;
9440    l_TEST_EFILE :='N';
9441    g_debug := hr_utility.debug_enabled;
9442 
9443    OPEN  csr_magtape_params(p_payroll_action_id);
9444    FETCH csr_magtape_params
9445    INTO  l_TEST_EFILE,
9446      l_business_group_id,
9447      l_FINANCIAL_YEAR,
9448      l_legal_employer,
9449      l_start_date,
9450      l_end_date,
9451      l_EFFECTIVE_DATE;
9452    CLOSE csr_magtape_params;
9453 
9454    IF l_TEST_EFILE = 'Y' THEN
9455        OPEN  csr_lookup_code(l_financial_year);
9456        FETCH csr_lookup_code
9457        INTO  l_financial_year_code;
9458        CLOSE csr_lookup_code;
9459 
9460    /* Bug 6470581 - Added the Payment Summary Type parameter for Datafile call */
9461 
9462     l_legislative_param := 'BUSINESS_GROUP_ID='      || l_business_group_id         ||' '
9463                 || 'FINANCIAL_YEAR='         || l_FINANCIAL_YEAR            ||' '
9464                 || 'REGISTERED_EMPLOYER='    || l_legal_employer            ||' '
9465                 || 'IS_TESTING='             || 'Y'                         ||' '
9466                 || 'ARCHIVE_PAYROLL_ACTION=' || to_char(p_payroll_action_id)||' '
9467                 || 'END_DATE='               || to_char(l_end_date,'YYYY/MM/DD HH:MI:SS')||' '
9468                 || 'PAYMENT_SUMMARY_TYPE='   || 'O'; /* Bug 6470581 */
9469 
9470      ps_request_id := fnd_request.submit_request
9471      ('PAY',
9472       'PYAUPSDF',
9473       null,
9474       null,
9475       false,
9476       'ARCHIVE',
9477       'AU_PS_DATA_FILE_VAL',                 -- Report_format of magtape process
9478       'AU',
9479       to_char(l_start_date,'YYYY/MM/DD HH:MI:SS'),
9480       to_char(l_EFFECTIVE_DATE,'YYYY/MM/DD HH:MI:SS'),
9481       'REPORT',
9482       l_business_group_id,
9483       null,
9484       null,
9485       l_legal_employer,
9486       l_FINANCIAL_YEAR_code,
9487       'END_DATE='||to_char(l_end_date,'YYYY/MM/DD HH:MI:SS'),
9488       'Y',                                   -- IS_TESTING Parameter
9489       'O',                                      /* Bug 6470581 */
9490       'AU_PAYMENT_SUMMARY',                     /* Bug 6470581 */
9491       to_number(p_payroll_action_id),        -- Archive_PAyroll_Action
9492       l_legislative_param                    -- Legislative parameters
9493      );
9494 
9495 
9496     END IF;
9497 
9498 
9499 end spawn_data_file;
9500 
9501 /*bug8711855 - The function adjusts Lump Sum E balance by less than 400 PTD value*/
9502 function get_lumpsumE_value
9503      (p_registered_employer     in   NUMBER
9504      ,p_assignment_id           in   pay_assignment_actions.ASSIGNMENT_ID%type
9505      ,p_year_start              in   DATE
9506      ,p_year_end                in   DATE
9507      ,p_lump_sum_E_ptd_tab in pay_balance_pkg.t_balance_value_tab
9508      ,p_lump_sum_E_ytd in number
9509      ,p_adj_lump_sum_E_ptd out nocopy number
9510      ,p_adj_lump_sum_pre_tax out nocopy NUMBER) return number IS
9511 
9512    /* Bug No: 3603495 - Performance Fix in c_get_pay_effective_date - Introduced per_assignments_f and its joins */
9513    /* Bug 4363057 - Cursor has been modified so that the Lump Sum E Payments given to previous legal employers
9514                    can be taken into account while calculating payment summary gross.*/
9515    /*Bug 8441044 - Cursor is modified to include action types 'B' and 'I', so that when Lump Sum E payments are
9516                    fed through Balance adjustment and Balance initialization processes,Lump Sum E Payments are
9517                    taken into account for calculating payment summary gross */
9518    CURSOR c_get_pay_effective_date(c_assignment_id   pay_assignment_actions.assignment_id%type
9519                                                      ,c_year_start              in   DATE
9520                                                      ,c_year_end                in   DATE)
9521    IS
9522     select /*+ USE_NL(ptp) */      -- Bug 4925650
9523     max(paa.assignment_action_id) -- Bug: 3095919, Bug 2610141
9524     from    per_assignments_f   paf,
9525                 pay_payroll_Actions ppa,
9526             pay_assignment_Actions paa,
9527         per_time_periods ptp
9528     where ppa.payroll_Action_id = paa.payroll_Action_id
9529         and paa.assignment_id = c_assignment_id
9530     and paf.assignment_id = c_assignment_id
9531                 and paf.assignment_id = paa.assignment_id
9532         and paa.tax_unit_id = p_registered_employer --2610141
9533         and action_type in ('Q','R','V','B','I')
9534         AND (paa.source_action_id IS NULL
9535                      OR (paa.source_action_id IS NOT NULL AND ppa.run_type_id IS NULL)) /*Bug 4363057*/
9536         and ppa.effective_date between c_year_start and c_year_end /*bug 4063321*/
9537 /* Bug# 5377624 */
9538 --        AND ptp.time_period_id = ppa.time_period_id
9539           and  ppa.payroll_id       = ptp.payroll_id
9540           and ppa.payroll_id=paf.payroll_id  /* Added for bug 5371102 , query 1 */
9541            and ppa.date_earned between ptp.start_date and ptp.end_date
9542 /* Bug# 5377624 */
9543           and ppa.date_earned between paf.effective_start_date and paf.effective_end_date
9544         GROUP BY ptp.time_period_id;
9545 
9546   l_procedure           constant varchar2(80) := 'get_lumpsumE_value';
9547   l_retro_lse_ytd number := 0;
9548   v_lump_sum_E_ytd number;
9549   v_lump_sum_E_ptd number;
9550   l_assignment_action_id pay_assignment_actions.assignment_action_id%type;
9551   v_adj_lump_sum_E_ptd number := 0;
9552   v_adj_lump_sum_pre_tax NUMBER := 0;        /* Bug 9190980 */
9553   p_result_lsE_ptd_table pay_balance_pkg.t_detailed_bal_out_tab;
9554 
9555 begin
9556   g_debug :=  hr_utility.debug_enabled;
9557 
9558   if g_debug then
9559      hr_utility.set_location('Entering ' || l_procedure, 1);
9560      hr_utility.trace('p_assignment_id......= ' || p_assignment_id);
9561      hr_utility.trace('p_lump_sum_E_ytd......= ' || p_lump_sum_E_ytd);
9562 
9563   end if;
9564 
9565   v_lump_sum_E_ytd := p_lump_sum_E_ytd;
9566 
9567                OPEN c_get_pay_effective_date(p_assignment_id, p_year_start, p_year_end) ;
9568                LOOP
9569                   fetch  c_get_pay_effective_date into l_assignment_action_id; --2610141
9570                   EXIT WHEN c_get_pay_effective_date%NOTFOUND;
9571 
9572                    p_result_lsE_ptd_table.delete;
9573                    v_lump_sum_E_ptd := 0;
9574 
9575                    p_context_table(1).tax_unit_id := p_registered_employer;
9576                    --
9577                    pay_balance_pkg.get_value
9578                    (p_assignment_action_id     => l_assignment_action_id
9579                    ,p_defined_balance_lst  => p_lump_sum_E_ptd_tab
9580                    ,p_context_lst      => p_context_table
9581                    ,p_output_table         => p_result_lsE_ptd_table
9582                    );
9583 
9584 
9585                    v_lump_sum_E_ptd :=  p_result_lsE_ptd_table(1).balance_value
9586                                       + p_result_lsE_ptd_table(2).balance_value
9587                                       + p_result_lsE_ptd_table(3).balance_value
9588                                       - p_result_lsE_ptd_table(4).balance_value
9589                                       + p_result_lsE_ptd_table(5).balance_value;
9590 
9591 
9592                   /* Bug: 3095919  If single lump sum E payment is less than $400 then the amount is included in gross earnings.
9593                            If single lump sum E payment is greater than $400 then it is included in Lump sum E payments.
9594                      bug8711855 -  v_adj_lump_sum_E_ptd is only used in pay_au_rec_det_paysum_mode.Adjust_lumpsum_E_payments.
9595              Bug 9190980 - Added v_adj_lump_sum_pre_tax variable to store the value of Retro GT12 Prepayment < $ 400
9596                            and the value will be used in pay_au_rec_det_paysum_mode package.
9597                   */
9598 
9599                        if v_lump_sum_E_ptd < 400 then
9600                           v_lump_sum_E_ytd       := v_lump_sum_E_ytd - v_lump_sum_E_ptd;
9601                           v_adj_lump_sum_E_ptd   := v_adj_lump_sum_E_ptd   + v_lump_sum_E_ptd + p_result_lsE_ptd_table(4).balance_value ; /* Start 9190980 */
9602                           v_adj_lump_sum_pre_tax := v_adj_lump_sum_pre_tax + p_result_lsE_ptd_table(4).balance_value;   /* End 9190980 */
9603                       end if;
9604 
9605                END LOOP;
9606                CLOSE c_get_pay_effective_date;
9607 
9608          p_adj_lump_sum_E_ptd   := v_adj_lump_sum_E_ptd  ;
9609          p_adj_lump_sum_pre_tax := v_adj_lump_sum_pre_tax;
9610 
9611   if g_debug then
9612     hr_utility.trace('out v_adj_lump_sum_E_ptd ........ =' || p_adj_lump_sum_E_ptd);
9613     hr_utility.trace('out v_adj_lump_sum_pre_tax .......=' || p_adj_lump_sum_pre_tax);
9614     hr_utility.trace('return v_lump_sum_E_ytd ........ =' || v_lump_sum_E_ytd);
9615     hr_utility.set_location('Leaving  '|| l_procedure,999);
9616   end if;
9617 
9618      return v_lump_sum_E_ytd;
9619 
9620 exception
9621   when others then
9622     if g_debug then
9623       hr_utility.set_location(l_procedure,000);
9624     end if;
9625     raise;
9626 
9627 end;
9628 
9629 /* bug9950136 FW retro adjustmnet */
9630 function adjust_retro_fw
9631          (p_assignment_id IN per_assignments_f.assignment_id%TYPE
9632          ,p_tax_unit_id   IN pay_assignment_actions.tax_unit_id%TYPE
9633          ,p_start_date in date
9634          ,p_end_date in date
9635          ,p_fw_type in varchar2
9636          ,p_retro_fw_gross out nocopy number
9637          ,p_retro_fw_tax out nocopy number) return number is
9638 
9639   /* Cost 35 on hr2rw102 */
9640   cursor csr_get_retro_entry  is
9641   SELECT  pee.element_entry_id element_entry_id,
9642           prv.result_value,
9643           ppa.date_earned date_earned,
9644           ppa.effective_date,
9645           pec.classification_name
9646   FROM    per_all_assignments_f  paa
9647        ,per_periods_of_service pps
9648        ,pay_assignment_actions pac
9649        ,pay_payroll_actions    ppa
9650        ,pay_element_entries_f pee
9651        ,pay_run_results        prr
9652        ,pay_run_result_values  prv
9653        ,pay_element_types_f    pet
9654        ,pay_input_values_f piv
9655        ,pay_element_classifications pec
9656      WHERE paa.assignment_id        = p_assignment_id
9657      AND   pps.PERIOD_OF_SERVICE_ID = paa.PERIOD_OF_SERVICE_ID
9658      AND   NVL(pps.actual_termination_date, p_end_date)
9659            BETWEEN paa.effective_start_date AND paa.effective_end_date
9660      AND   pac.assignment_id = paa.assignment_id
9661      AND   pac.assignment_id = pee.assignment_id
9662      AND   pac.tax_unit_id   = p_tax_unit_id
9663      AND   pac.payroll_action_id = ppa.payroll_action_id
9664      AND   ppa.effective_date BETWEEN p_start_date AND p_end_date
9665      AND   pac.assignment_action_id = prr.assignment_action_id
9666      AND   prr.element_type_id=pet.element_type_id
9667      and   prr.run_result_id = prv.run_result_id
9668      and   prv.input_value_id = piv.input_value_id
9669      and   piv.name = 'Pay Value'
9670      and   pec.classification_id = pet.classification_id
9671      and   (pec.classification_name in ('Earnings', 'Pre Tax Deductions')
9672            OR
9673            (piv.element_type_id = pet.element_type_id
9674            and pet.element_name = 'Tax Deduction'))
9675      AND   pee.element_entry_id=prr.element_entry_id
9676      AND   pee.creator_type in ('EE','RR')
9677      AND   pee.assignment_id = paa.assignment_id
9678      AND   ppa.action_status='C'
9679      AND   pac.action_status='C'
9680      AND   ppa.date_earned between pee.effective_start_date and pee.effective_end_date
9681      AND   ppa.date_earned BETWEEN pet.effective_start_date AND  pet.effective_end_date
9682      AND   ppa.date_earned between piv.effective_start_date and piv.effective_end_date;
9683 
9684 
9685   cursor csr_get_fw_status_exist (c_date date) is
9686   select 'Y'
9687   from   per_all_assignments_f paa
9688         ,per_time_periods ptp
9689         ,per_assignment_status_types past
9690   where paa.assignment_id = p_assignment_id
9691   and paa.payroll_id = ptp.payroll_id
9692   and c_date = ptp.end_date
9693   and past.assignment_status_type_id = paa.assignment_status_type_id
9694   and past.user_status = p_fw_type
9695   and c_date between paa.effective_start_date and paa.effective_end_date
9696   ;
9697 
9698 
9699   retro_start date;
9700   retro_end date;
9701   x number;
9702   l_found varchar2(2);
9703   l_earnings_fw number :=0 ;
9704   l_pretax_fw number :=0 ;
9705   l_retro_adj_fw number :=0 ;
9706   l_tax_fw number := 0;
9707   l_proc_name varchar2(100);
9708 
9709   g_debug                 boolean;
9710 
9711 begin
9712 
9713     g_debug :=  hr_utility.debug_enabled;
9714 
9715     IF g_debug THEN
9716             l_proc_name    := g_package||'.adjust_retro_fw';
9717             hr_utility.set_location('Entering   '||l_proc_name,1);
9718             hr_utility.set_location('IN p_assignment_id     '||p_assignment_id,1);
9719             hr_utility.set_location('IN p_tax_unit_id       '||p_tax_unit_id,1);
9720     END IF;
9721 
9722 
9723   for rec_ret_entry_ids in csr_get_retro_entry loop
9724 
9725       x:=pay_au_paye_ff.get_retro_period(rec_ret_entry_ids.element_entry_id,
9726                                           rec_ret_entry_ids.date_earned,
9727                                           p_tax_unit_id,
9728                                           retro_start,
9729                                           retro_end);
9730       IF (months_between(rec_ret_entry_ids.effective_date,retro_end) <= 12  -- LT12_CURR
9731              AND rec_ret_entry_ids.effective_date >= p_start_date
9732              AND retro_end >= p_start_date) THEN
9733 
9734           open csr_get_fw_status_exist(retro_end);
9735           fetch csr_get_fw_status_exist into l_found;
9736 
9737              if csr_get_fw_status_exist%FOUND then  -- FW case
9738 
9739                  if rec_ret_entry_ids.classification_name = 'Earnings' then
9740                     l_earnings_fw := l_earnings_fw + rec_ret_entry_ids.result_value ;
9741                  elsif rec_ret_entry_ids.classification_name = 'Pre Tax Deductions' then
9742                     l_pretax_fw := l_pretax_fw + rec_ret_entry_ids.result_value  ;
9743                  else -- Tax Deduction
9744                     l_tax_fw := l_tax_fw + rec_ret_entry_ids.result_value;
9745                  end if;
9746 
9747                     IF g_debug THEN
9748                             hr_utility.set_location(rec_ret_entry_ids.classification_name,15);
9749                             hr_utility.set_location('FW Date Earned '||rec_ret_entry_ids.date_earned,15);
9750                             hr_utility.set_location('Fw Retro End '||retro_end,15);
9751                             hr_utility.set_location('FW result value '||rec_ret_entry_ids.result_value,15);
9752                     END IF;
9753 
9754              end if;
9755 
9756           close csr_get_fw_status_exist;
9757       END IF;
9758 
9759         IF g_debug THEN
9760                 hr_utility.set_location('l_earnings_fw '||l_earnings_fw,20);
9761                 hr_utility.set_location('l_pretax_fw '||l_pretax_fw,20);
9762                 hr_utility.set_location('l_tax_fw '||l_tax_fw,20);
9763         END IF;
9764 
9765   end loop;
9766 
9767   l_retro_adj_fw := l_earnings_fw - l_pretax_fw;
9768 
9769         IF g_debug THEN
9770                 hr_utility.set_location('Returned Adjusted Retro value '||l_retro_adj_fw,99);
9771                 hr_utility.set_location('Returned Adjusted Retro Tax value '||l_tax_fw,99);
9772                 hr_utility.set_location('Leaving   l_proc_name '||l_proc_name,99);
9773         END IF;
9774 
9775 
9776   p_retro_fw_gross := l_retro_adj_fw;
9777   p_retro_fw_tax := l_tax_fw;
9778 
9779   return 0;
9780 
9781 end adjust_retro_fw;
9782 
9783 end pay_au_payment_summary;