DBA Data[Home] [Help]

PACKAGE BODY: APPS.HRASSACT

Source


1 package body hrassact as
2 /* $Header: pyassact.pkb 120.30.12010000.4 2008/11/24 05:09:29 priupadh ship $ */
3 /*
4  Copyright (c) Oracle Corporation 1991,1992,1993. All rights reserved
5 --
6 /*
7 --
8  Name         : hrassact
9  Author       : $Author: priupadh $
10  Synopsis     : Payroll and Assignment action processing.
11  Contents     : applied_interlocks
12                 bal_adjust
13                 bal_adjust_actions
14                 del_latest_balances
15                 do_assact_rollback
16                 do_pact_rollback
17                 ensure_assact_rolled_back
18                 ensure_pact_rolled_back
19                 ext_man_payment
20                 inassact
21                 inassact_main
22                 irbaact
23                 reversal
24                 rollback_ass_action
25                 rollback_payroll_action
26                 trash_latest_balances
27                 trash_latest_balances
28                 trash_quickpay
29                 undo_stop_update
30                 val_assact_rollback
31                 val_pact_rollback
32                 validate_rollback_mode
33 --
34  Change List
35  -----------
36  Date        Name          Vers    Bug No     Description
37  -----------+-------------+-------+----------+-------------------------------+
38  21-Nov-2008 ubhat         120.30. 7584883     Divided locking code for
39                         12010000.3             per_all_assignment_f and
40                                                per_periods_of_service.
41  10-Oct-2008 salogana	   115.143 7371362     Added bulk delete in
42 					       trash_latest_balances FOR
43 					       pay_assignment_latest_balances
44  05-Aug-2008 salogana	   115.142 6595092     Added bulk delete code in
45                                                trash_latest_balances for
46 					       performance improvement.
47  26-Feb-2008 Ckesanap      115.141 6820127     Added another definition of
48                                                ext_man_payment to be called as
49 					       a concurrent request for Void and
50 					       Reversal enhancement.
51  19-DEC-2007 AYegappa      115.140 6676706     Performance fix to Balance
52                                                Adjustments (added some hints)
53  05-NOV-2007 alogue        115.139             Performance fix to
54                                                trash_latest_balances.
55  11-JUN-2007 KKawol        115.138             Set the entry path on reversal
56                                                run results.
57  12-FEB-2007 SuSivasu      115.137             Set the time period to be based
58                                                on the period earned for the
59                                                Balance Adjustment when the
60                                                TIME_PERIOD_ID leg rule is set.
61  16-JAN-2007 nbristow      115.136             Changes for
62                                                maintain_balances_for_action
63  30-NOV-2006 divicker      115.135             Retain old reversal sig order
64  23-NOV-2006 divicker      115.134             Multi reversal parameter update
65  10-NOV-2006 alogue        115.133 5410515     Stop balance adjustments or
66                                                reversals on assignments with
67                                                allready incomplete actions.
68  09-NOV-2006 divicker      115.132             Further batch reversal changes
69  03-NOV-2006 nbristow      115.131             Now performing
70                                                a distinct for pog joins
71  03-NOV-2006 divicker      115.130             Revert to 115.128 pending
72                                                further check on 115.129
73  02-NOV-2006 divicker      115.129             Add loop for processing multi
74                                                reverse run balances
75  31-OCT-2006 divicker      115.128             Need correct refactoring of
76                                                reversal proc.
77  30-OCT-2006 divicker      115.126 5616882     Reversal by assignment set func
78                                                Bug fix 5410515
79  19-OCT-2006 alogue        115.125 5612247     Performance fix to POG
80                                                resequence statements.
81  07-AUG-2006 alogue        115.124 5441737     Resequence G object group actions
82                                                - Retropay matser assignment actions
83                                                have null assignment_id.
84  02-AUG-2006 alogue        115.123 5416668     Performance fix to albc_selective
85                                                in del_latest_balances.
86  30-JUN-2006 nbristow      115.122             Context length limited to 30
87                                                chars
88  19-JUN-2006 SuSivasu      115.121             Enabled the Balance Date and Time
89                                                Definition ID contexts to be stored
90                                                in pay_action_contexts.
91  14-MAR-2006 alogue        115.120 5094068     Fix lb_context_match_check
92                                                to correctly spot if context
93                                                wasn't in the udca.
94  07-MAR-2006 alogue        115.119 5082050     Further fix to POG retro
95                                                resequencing for assignments
96                                                started in the future.
97  17-FEB-2006 alogue        115.118             Further fix to previous change.
98  17-FEB-2006 nbristow      115.117             Using the interlock rule G, the
99                                                Retro actions were not
100                                                correctly resequencing for
101                                                terminated employees.
102  14-FEB-2006 alogue        115.116 5034395     Radix issue in maintain_lat_bal.
103  04-OCT-2005 alogue        115.115 4644738     Load cxt_id in udca for Reversals
104                                                as used by context_match_check.
105  05-SEP-2005 alogue        115.114             Performance CBO hints in
106                                                trash_latest_balances.
107  27-JUL-2005 nbristow      115.113             Resequence_actions was not
108                                                joining to the assignment table
109                                                correctly under certain date
110                                                track conditions.
111  08-JUN-2005 alogue        115.112 4372751     Performance fix: hint in seqasg
112                                                in resequence_actions.
113  06-MAY-2005 NBristow      115.111             Added new contexts
114                                                LOCAL_UNIT_ID, ORGANIZATION_ID
115                                                and SOURCE_NUMBER2.
116  29-APR-2005 SuSivasu      115.110             Added p_reason parameter for
117                                                ext_man_payment.
118  29-APR-2005 alogue        115.109 4337565     Support of ENABLE_RR_SPARSE
119                                                upgrade.
120  14-APR-2005 alogue        115.108 3465844     Use PAY_ASSIGNMENT_ACTIONS_N51
121                                                instead of PAY_ASSIGNMENT_ACTIONS_N1
122                                                in index hints.
123  04-APR-2005 alogue        115.107             Latest Balance Maintenance by
124                                                Reversals.
125  23-FEB-2005 nbristow      115.106             Changes for Period Allocation.
126  10-DEC-2004 alogue        115.105             g_ba_lat_bal_maintenance global
127                                                for BAL_ADJ_LAT_BAL legislation
128                                                rule.
129  25-NOV-2004 nbristow      115.104             Retropay multi assignments
130  25-OCT-2004 thabara       115.103 3966979     Added p_element_type_id to
131                                                del_latest_balances().
132                                                Modified maintain_lat_bal().
133  24-SEP-2004 nbristow      115.102             Changes for process group
134                                                actions.
135  20-SEP-2004 thabara       115.101 3482270     Original Entry ID support for
136                                                balance adjustments.
137                                                Modified set_action_context(),
138                                                bal_adjust_actions() and
139                                                maintain_lat_bal().
140  16-SEP-2004 alogue        115.100 3863038     More Performance fixes in
141                                                del_latest_balances.
142  09-AUG-2004 tbattoo       115.99  3724695     Support for reversals and retropay
143  08-JUL-2004 nbristow      115.98              Changes for Sparse Matrix JC.
144  30-APR-2004 alogue        115.97              More Performance fixes in
145                                                del_latest_balances.
146  28-APR-2004 thabara       115.96              Modified irbaact not to call
147                                                get_tax_unit when taxunt is set
148                                                for action type B and I.
149  27-APR-2004 alogue        115.95              Performance fix to albc_selective
150                                                in del_latest_balances.
151  09-Mar-2004 swinton       115.94              Enhancement 3368211 -
152                                                Amended Trash_Quickpay() to
153                                                support new QuickPay Exclusions
154                                                model.
155  02-FEB-2004 nbristow      115.93              Reversal was not correctly
156                                                setting the run balances, when
157                                                the run is prior to the
158                                                balance validation date.
159  06-JAN-2004 alogue        115.92  3354185     Avoid execution of
160                                                del_latest_balances() for balance
161                                                initialisations.
162  16-DEC-2003 tbattoo       115.91              Fix to mantain latest balances
163  11-DEC-2003 tbattoo       115.90              Fix to mantain latest balances
164  09-DEC-2003 nbristow      115.89              Changes to maintain
165                                                pay_latest_balances.
166  24-NOV-2003 alogue        115.88  3262314     Performance enhancement to
167                                                previous change.
168  24-NOV-2003 alogue        115.87  3262314     Performance enhancement to
169                                                previous change.
170  21-NOV-2003 alogue        115.86  3262314     Avoid corruption of latest
171                                                balance value by balance
172                                                adjustments.
173  13-NOV-2003 nbristow      115.85              Reversal was not correctly
174                                                setting the jurisdiction code
175                                                in sparse mode.
176  04-NOV-2003 alogue        115.84  3176709     Use of per_business_groups_perf,
177                                                per_all_people_f and
178                                                pay_all_payrolls_f. Performance
179                                                fix to seqper cursors.
180  03-NOV-2003 tbattoo       115.83              support for sparse matrix and
181 					       pay_latest_balances table
182  17-OCT-2003 alogue        115.82              Tuned get_rr_values cursor
183                                                in set_action_context.
184  14-OCT-2003 alogue        115.81  3166638     Performance fix to quickpay
185                                                assignment action creation.
186  05-SEP-2003 alogue        115.80  3130030     Performance fix to plbc_selective
187                                                in del_latest_balances.
188  05-SEP-2003 alogue        115.79  3130030     Performance fix to trash_quickpay.
189  03-SEP-2003 thabara       115.78              Correction of 115.77.
190  03-SEP-2003 thabara       115.77  3105028     Modified set_action_context not
191                                                to set defaults to dynamic
192                                                contexts for Balance Upload.
193                                                Modified the process order of
194                                                create_all_asg/group_balances
195                                                in bal_adjust_actions to call
196                                                them after setting contexts.
197  03-SEP-2003 nbristow      115.76              RETRO_DELETE is now a
198                                                legislation rule.
199  29-AUG-2003 nbristow      115.75              Action sequence on
200                                                pay_run_balances was not being
201                                                updated correctly.
202  53-JUN-2003 alogue        115.74  2960902     New trash_latest_balances
203                                                only passed balance_type_id
204                                                and trash_date. Overloads
205                                                original version.
206  12-MAY-2003 alogue        115.73  2911448     Avoid locking issues in Batch
207                                                Balance Adjustments by only
208                                                updating action_population_status
209                                                in inassact_main if need to.
210  11-MAR-2004 rthirlby      115.72  2822429     Altered reversal, so that run
211                                                result values for SOURCE_IV
212                                                and SOURCE_NUMBER input values
213                                                are not negated. These rrvs will
214                                                be ids/numbers that should not be
215                                                reversed.
216  05-MAR-2003 sdhole        115.71  2805195     Added parameter tax_unit_id
217                                                with default value null to
218                                                bal_adjust procedure.
219  07-FEB-2003 nbristow      115.70              Reversal was not correctly
220                                                working for the new contexts.
221  05-FEB-2003 nbristow      115.69              Further new context changes.
222  05-FEB-2003 nbristow      115.68              Added contexts for
223                                                source_number and source_text2
224  24-JAN-2003 alogue        115.67              Reverse bug 2453546 changes.
225  20-JAN-2003 alogue        115.66  2758499     Fixed trash_latest_balances
226                                                to handle latest balances
227                                                earlier than trash_date.
228  14-JAN-2003 nbristow      115.65              Now Jurisdiction input value
229                                                can have any name.
230  10-JAN-2003 alogue        115.64  2266326     Use of CHECK_LATEST_BALANCES,
231                                                CHECK_RRVS_FIRST and
232                                                CHECK_LAT_BALS_FIRST package
233                                                globals to tune behaviour of
234                                                trash_latest_balances.
235  09-JAN-2003 alogue        115.63  2692195     Use of hr_utility.debug_enabled
236                                                for performance of trace
237                                                statements.
238  06-DEC-2002 alogue        115.62              Subtle-paranoid fix to
239                                                bal_adjust_actions to avoid
240                                                possible issue with NOCOPY
241                                                changes.
242  03-DEC-2002 scchakra      115.61  2613838     Added overloaded procedure
243                                                get_default_leg_value to return
244                                                the default run type for a
245                                                legislation. Included
246                                                NOCOPY Performance Changes.
247  15-NOV-2002 alogue        115.60  2667222     Re-implement 2492007 cartesian
248                                                join (in a different way!).
249  07-NOV-2002 alogue        115.59  2453546     Optimise performance of seqper
250                                                cursor in inassact be breaking
251                                                into 2.
252  24-OCT-2002 alogue        115.57  2641336     Optimise performance for
253                                                statements in del_latest_balances.
254  16-OCT-2002 alogue        115.56  2581887     Optimise performance for
255                                                Balance Initialisation.
256  24-SEP-2002 alogue        115.55  2587443     Further enhanced Reversals so
257                                                handle result values with up
258                                                to 38 decimal places.
259  19-SEP-2002 nbristow      115.54              Changes in 115.50 introduced
260                                                a bug in Retropay, reversed
261                                                the change to run_results
262                                                cursor.
263  09-SEP-2002 alogue        115.53  2529691     Performance fix to
264                                                resequence_actions.
265  09-AUG-2002 alogue        115.52              Performance fix to element
266                                                entries statement in
267                                                do_assact_rollback that was
268                                                using FTS.
269  09-AUG-2002 alogue        115.51  2362454     Enhance Reversals so handle
270                                                result values with up to 35
271                                                decimal places.  Previously
272                                                there was a restriction of up
273                                                to 20 dcs within the fnd_number
274                                                code.
275  01-AUG-2002 dsaxby        115.50  2492007     Prevent ora-01403
276                                                no data found from within
277                                                bal_adjust_actions.
278                                                Also, fix select of run results
279                                                to reverse to remove apparent
280                                                cartesian join.
281  21-JUN-2002 alogue        115.49              Fix CA TAX_GROUP value fetch
282                                                in udca.
283  30-APR-2002 RThirlby      115.48              Support for pay_run_balances -
284                                                balance reporting architecture,
285                                                for reversals and balance
286                                                adjustments.
287  26-APR-2002 alogue        115.47  2346351     Support of payroll_id in udca
288                                                for balances that use payroll_id
289                                                context.
290  09-APR-2002 nbristow      115.46              Added get_default_leg_rule to
291                                                allow the defaulting of run
292                                                type.
293  03-APR-2002 tbattoo       115.45              added p_run_type_id parameter
294  18-DEC-2001 dsaxby        115.44              GSCC standards fix.
295  26-NOV-2001 dsaxby        115.43  1682940     Changes for Purge.
296                                                Do not shuffle assignment
297                                                actions in purge mode and allow
298                                                insert of upload action before
299                                                a Purge.
300                                                Added commit at end of file.
301                                                Added purge_mode parameter to
302                                                bal_adjust_actions.
303                                                Added dbdrv line.
304  13-NOV-2001 jtomkins      115.42              Added prepay_flag parameter to
305                                                bal_adjust and bal_adjust_actions
306  01-NOV-2001 nbristow      115.41              Set Action Context exist
307                                                statement now in the correct
308                                                format.
309  29-OCT-2001 nbristow      115.40              Set Action Context changed to
310                                                check that a row does not
311                                                already exist in the action
312                                                context table.
313  11-OCT-2001 nbristow      115.39              Added hints to statements
314                                                in rev_pre_inserted_rr.
315  04-SEP-2001 nbristow      115.38              Added the resequence_chunk
316                                                procedure.
317  12-JUL-2001 kkawol        115.37              Change to inassact_main. If
318                                                rule_type 'I' does not exist for
319                                                the legislation, we default rule
320                                                mode to N. Bug 1337853.
321  25-JUN-2001 alogue        115.36              Performance fix to balance
322                                                adjustment latest balance
323                                                maintenance.
324  25-JUN-2001 alogue        115.35              Added CBO hints
325  22-JUN-2001 alogue        115.34              Added CBO hints
326  12-JUN-2001 nbristow      115.33              Initialising retro_purge.
327  04-JUN-2001 kkawol        115.32              Added changes required for
328                                                Quickpay Prepay to work with
329                                                master and sub actions (iter eng).
330                                                Changed qpppassact and inassact.
331  29-MAY-2001 nbristow      115.31              Changes to Retropay so that
332                                                the RRs are not deleted.
333  08-MAY-2001 alogue        115.30  1763446     Added CBO hints
334  24-APR-2001 mreid         115.29  1518951     Added CBO hints
335  06-APR-2001 alogue        115.28              Changes for source text iv
336                                                context.
337  26-JAN-2001 alogue        115.26  1614003     Balance Adjustment lat bal
338                                                maintenance handle multiple
339                                                feeds by same adjustment.
340  16-JAN-2001 alogue        115.26              Handle null result values  in
341                                                balance adjustment latest
342                                                balance maintenance.
343  10-JAN-2001 alogue        115.25  1571313     Handle -9999 balance values in
344                                                balance adjustment latest
345                                                balance maintenance.
346  26-NOV-2000 nbristow      115.24              Changes for source text context
347  21-NOV-2000 alogue        115.23  887061      Skipped terminated assignments
348                                                support.
349  02-OCT-2000 alogue        115.22  1421447     Avoid a PLS-00365 in latest
350                                                balance maintenance inm balance
351                                                adjustments.
352  29-SEP-2000 nbristow      115.21              Now passing tax unit id to
353                                                balance adjustments.
354  18-SEP-2000 nbristow      115.20              Maintenance of latest balances
355                                                was not taking into account
356                                                null rrv.
357  14-SEP-2000 NBRISTOW      115.19              Changes to trash_latest_balances
358                                                to remove full pl/sql feed
359                                                checking balances.
360  30-AUG-2000 ALOGUE        115.17              Deletion of latest balances
361                                                by balance adjustment if
362                                                leg rule for lat bal maintenance
363                                                is not defined.
364  04-AUG-2000 ALOGUE        115.16              Maintenance of latest balances
365                                                within balance adjustments.
366  19-MAY-2000 nbristow      115.15              Added procedures to resequence
367                                                sequenced actions.
368  22-FEB-2000 dsaxby        115.14  #1168142    Remove the need for a period of
369                                                service row to exist when
370                                                processing a balance adjustment
371                                                action.  This is for OAB.
372  13-JAN-2000 alogue        115.13              Ensure that error_messages
373                                                inserted into pay_message_lines
374                                                are at max 240 in length.
375  16-NOV-1999 nbristow      115.12              Now reversals and balance
376                                                adjustments populate
377                                                pay_action_contexts.
378  26-JUL-1999 ALOGUE        115.11              Enhancement in Reversal to
379                                                get reversals results in same
380                                                order as run being reversed.
381                                                Optimises behaviour of the
382                                                retrocosting of a reversal.
383  02-JUN-1999 ALOGUE        115.10              Fix in bal_adjust_actions to
384                                                ensure only update this balance
385                                                adjustments run results.
386  22-APR-1999 ALOGUE        115.8               Changed rollback_payroll_action
387  12-APR-1999 ALOGUE        115.7               Fix to reversals to support
388                                                canonical numbers.
389  07-APR-1999 SDOSHI        115.6               Flexible Dates Conversion
390  04-JAN-1999 NBRISTOW      110.8               Changed rollback_payroll_action
391                                                to use the rollback package.
392  04-SEP-1998 KKAWOL        40.62   #721925     Reversals run results: select
393                                                actual status instead of
394                                                forcing it to be 'P'.
395  27-NOV-1997 MFENDER       110.3   #589767     Modified inassact to populate
396                                                tax_unit_id for quickpay
397                                                prepayments.
398  05-SEP-1997 KKAWOL        40.61   #547578     Period dates fix.  Set the
399                                                date_earned column as
400                                                appropriate on the
401                                                pay_payroll_actions table.
402  19-JUN-1997 ALOGUE        40.60   #507602     Reversals run results: source_id
403                                                = run_result_id of the run
404                                                result being reversed.
405  04-APR-1997 NBRISTOW      40.59   #473685     Segment1 was not being decoded
406                                                for US legislation to convert
407                                                it to a number.
408  01-APR-1997 ALOGUE        40.58               Fixed previous change.
409  27-MAR-1997 ALOGUE        40.57               US reversal GRE Fix #459662
410  15-JAN-1996 NBRISTOW      40.56               EOY performance fix for W2.
411  13-JAN-1996 NBRISTOW      40.55               Reverse Backport.
412  13-JAN-1996 NBRISTOW      40.54               Backport EOY performance fix.
413  23-DEC-1996 NBRISTOW      40.53               Uncommented exit.
414  09-DEC-1996 SSINHA        40.52               Fixed previous change
415  05-DEC-1996 NBRISTOW      40.51               Fixed previous change.
416  29-NOV-1996 DSAXBY        40.50   #366215     New Reversal functionality.
417 
418  20-NOV-1996 NBRISTOW      40.49               Now passing a flag to inassact
419                                                to indicate that the assignment
420                                                needs to be locked.
421  18-JUN-1996 NBRISTOW      40.48   #374931     Now when a balance adjustment
422                                                is performed only the latest
423                                                balances feed by the adjustment
424                                                are deleted.
425  14-JUN-1996 DSAXBY        40.47   #374389     When reversing an indirect
426                                                result, set source_type of new
427                                                result to 'V' (required for
428                                                Costing).
429  08-MAY-1996 NBRISTOW      40.46   #359005     Performance problem in
430                                                del_latest_balances, now using
431                                                a cursor to delete context
432                                                values.
433  22-APR-1996 DSAXBY        40.45   #360386     Change joins to the
434                                                per_business_groups view.
435  13-APR-1996 DKERR         40.44               Modified inassact for external
436                                                manual payments to allow
437                                                the insert of an assignment
438                                                action where a payment action
439                                                have been voided.
440  14-MAR-1996 DSAXBY        40.43               Make 'X' actions alter action
441                                                sequence where necessary.
442  27-FEB-1996 DSAXBY        40.42               Added support for 'X' actions.
443  17-JAN-1996 NBRISTOW      40.41   #335099     Altered sql statements in
444                                                inassact to improve performance
445                                                of US Check report. US Check
446                                                Report is using balance
447                                                user exit thus calling inassact.
448  10-JAN-1996 DSAXBY        40.40   #333428     Changed trash_latest_balances
449                                                procedure to avoid trashing
450                                                balances un-necessarily. This
451                                                required the addition of a new
452                                                parameter.
453  11-DEC-1995 NBRISTOW      40.39               Changed inassact to insert the
454                                                Tax Unit Id for quick pay
455                                                actions.
456  10-NOV-1995 NBRISTOW      40.38               Changed name of bal_adjust to
457                                                bal_adjust_actions, added extra
458                                                out arguments. Created new
459                                                procedure bal_adjust for
460                                                existing bal_adjust calls.
461  06-NOV-1995 NBRISTOW      40.37               Tax Unit Id now placed on the
462                                                assignment_action for balance
463                                                adjustments and resversals.
464                                                Also the jurisdiction is placed
465                                                on the run result.
466  13-SEP-1995 DSAXBY        40.35   #307123     New parameter to reversal
467                                                procedure, indicating that we do
468                                                not need to insert assact and
469                                                interlock. Introduced for
470                                                backpay.
471  16-AUG-1995 DSAXBY        40.34   #301528     Removed unnecessary check from
472                                                ensure_assact_rolled_back
473                                                procedure.
474  25-JUL-1995 AMILLS        40.33               Amended selection statement
475                                                substituting clause :-
476                                                'HR_6075_ELE_ENTRY_REC_EXIST'
477                                                 with the following 2 hard
478                                                 coded error messages:
479                                                 'HR_7699_ELE_ENTRY_REC_EXISTS'
480                                                 'HR_7700_ELE_ENTRY_REC_EXISTS'
481  10-JUL-1995 DSAXBY        40.32   #292828     Set date_earned for bal adjust.
482  05-JUL-1995 NBRISTOW      40.31               Added intial balance load
483                                                payroll action type.
484  19-APR-1995 DSAXBY        40.30               #277088 : trash_latest_balances
485                                                now deletes latest balances
486                                                correctly again!
487  18-APR-1995 DSAXBY        40.29               Removed 'nowait' statement to
488                                                avoid immediate failure on lock
489                                                when called as part of w2
490                                                report work.
491  04-APR-1995 DSAXBY        40.28               Reverse 'H_HHMM' uom.
492  31-MAR-1995 DSAXBY        40.27               Now insert assignment action
493                                                with status = 'U' for a non
494                                                tracked action type ('N').
495  24-MAR-1995 DSAXBY        40.26               Now call undo_stop_update
496                                                procedure instead of hr_ent_api
497                                                delete_element_entry.
498  13-FEB-1995 DSAXBY        40.25               Fix problem causing 7010 error
499                                                when inserting quickpay assact.
500  31-JAN-1995 DSAXBY        40.24               Must not trash latest balances
501                                                for an action type of 'N'.
502  16-DEC-1994 DSAXBY        40.23               Added qpppassact.
503                                                Delete pre_payment rows for
504                                                QuickPay Pre-Payment process.
505  25-NOV-1994 DSAXBY        40.22               Change in params to qpassact.
506  26-OCT-1994 DSAXBY        40.21               Delete from the
507                                                pay_balance_context_values
508                                                table where necessary.
509                                                Set time_period_id for
510                                                balance adjustments.
511                                                Created public versions of
512                                                validate_pact_rollback and
513                                                validate_assact_rollback
514                                                procedures. (To be called from
515                                                forms).
516                                                Prevent mark for retry for
517                                                Balance Adjustment. Note this
518                                                check has been moved
519                                                val_pact_rollback procedure.
520                                                Insert value for the
521                                                time_period_id column when
522                                                processing balance adjustment.
523                                                Alter rules and strategy for
524                                                checking rules for rolling back
525                                                and marking for retry assignment
526                                                and payroll actions.
527                                                Disabled all uses of the
528                                                business_group_id index.
529  05-OCT-1994 DKERR         40.20               Set the OBJECT_VERSION_NUMBER
530                                                to 1 for inserts into
531                                                PAY_PAYROLL_ACTIONS and
532                                                PAY_ASSIGNMENT_ACTIONS for
533                                                all action types.
534  28-JUN-1994 DSAXBY        40.19               Added line to decode statement
535                                                in reversal routine, to
536                                                prevent invalid number errors.
537                                                Delete from pay_costs and
538                                                pay_quickpay_inclusions when
539                                                we roll back.
540                                                Do not attempt to delete from
541                                                pay_pre_payments, unless we are
542                                                rolling back a Pre-Payment!
543  21-JUN-1994 CSWAN         40.18               For reversal RRVs, flip the
544                                                sign of the RRVs being reversed,
545                                                rather than prepending a '-'
546                                                character, which leads to
547                                                balance errors.
548  28-MAR-1994 DSAXBY        40.17    G622       Set updating_action_id to null
549                                                when rolling back update rec
550                                                entries which were corrections.
551  18-MAR-1994 DSAXBY        40.16    G172       Improved messaging for rollback
552                                                assignment action. Added message
553                                                for rollback payroll action.
554  28-FEB-1994 DSAXBY        40.15    G585       Allow nested mark for retry.
555                                                Prevent marking payment action
556                                                types for retry.
557  05-JAN-1994 DSAXBY        40.14    G481       Only process NEE when reversing
558                                                effective of stop and update
559                                                rules.
560  20-DEC-1993 DSAXBY        40.13    G454       Delete pre-payment rows.
561                                     G283/      Ensure interlock rows are
562                                     G272       deleted if rollback assact is
563                                                called from form.
564                                     ----       Altered DELETE_NEXT_CHANGE to
565                                                FUTURE_CHANGE for rollback of
566                                                REE update feature.
567  17-DEC-1993 DSAXBY        40.12    G277       Updated incorrect comments.
568  14-DEC-1993 DSAXBY        40.11    G277       Rolling back of Update and
569                                                stop REE rules.
570                                                pay_element_entry_values table).
571  13-DEC-1993 CSWAN         40.10    G410       Removed reference to removed
572                                                PLANNED_PAYMENT_DATE column.
573  13-DEC-1993 AFRITH        40.9     G396       Unsequenced assignment actions
574                                                not interlocked by sequenced
575                                                actions  .
576  09-DEC-1993 DSAXBY        40.8     G320       Handle ND unit of measure.
577  06-DEC-1993 DSAXBY        40.7     G296       Prevent looping on rollback or
578                                                mark for retry when assignment
579                                                actions are not to be
580                                                (correctly) rolled back or
581                                                marked for retry.
582  12-NOV-1993 DSAXBY        40.6     G36        Handled non tracked action
583                                                properly again.
584  29-OCT-1993 RPATEL        40.5                Added functionality for
585                                                Manual/External Payments,
586                                                error handling invalid action.
587  26-OCT-1993 DSAXBY        40.4                Added some missing error mesg.
588                                                Prevent Reversal of something
589                                                that is already reversed.
590                                                Trash latest balances for Rev
591                                                and Balance Adjustment. (Needed
592                                                to add del_latest_balances).
593  20-OCT-1993 AFRITH        40.3                Added BACKPAY mode.
594  19-OCT-1993 DSAXBY        40.2                Altered bal_adjust. No longer
595                                                a function, now procedure and
596                                                updates creator_id.
597                                                Fixed payroll action roll back.
598  13-AUG-1993 DSAXBY        40.1                Fix interlock delete.
599  28-JUL-1993 DSAXBY        30.8                Work on reversal and bal_adjust
600  20-JUL-1993 DSAXBY        30.7                Added bal_adjust.
601  24-FEB-1999 J. Moyano    115.5                MLS Changes. Reference to
602                                                pay_payment_types_tl included in
603                                                procedure do_pact_rollback.
604  11-SEP-2000 divicker     115.18               Performance changes
605 -----------+-------------+-------+----------+-------------------------------+
606 */
607 --
608 --
609 /*--------------------------  RECORD types ---------------------------*/
610 --
611 type context_cache_type is record
612 (
613  cxt_id                    number_tbl,
614  cxt_name                  varchar_60_tbl,
615  sz                        number
616 );
617 --
618 type assact_details is record
619 (assact_id                 pay_assignment_actions.assignment_action_id%type,
620  assignment_id             pay_assignment_actions.assignment_id%type,
621  full_name                 per_all_people_f.full_name%type,
622  assignment_number         per_all_assignments_f.assignment_number%type,
623  payroll_id                per_all_assignments_f.payroll_id%type);
624 --
625 type pact_details is record
626 (pact_id                   pay_payroll_actions.payroll_action_id%type,
627  action_name               hr_lookups.meaning%type,
628  action_type               pay_payroll_actions.action_type%type,
629  sequenced_flag            boolean,
630  action_date               date,
631  current_date              date, -- this is sysdate.
632  payroll_name              pay_all_payrolls_f.payroll_name%type,
633  bg_name                   hr_organization_units.name%type,
634  independent_periods_flag  boolean);
635 --
636  c_eot constant date := to_date('31/12/4712','DD/MM/YYYY');
637  g_context_cache context_cache_type;
638  contexts_cached boolean := FALSE;
639  g_lat_bal_check_mode pay_action_parameters.parameter_value%TYPE := null;
640  g_debug boolean := hr_utility.debug_enabled;
641  g_dynamic_contexts          pay_core_utils.t_contexts_tab;
642 --
643 --
644 --
645 /*------------------------ val_pact_rr_rules ---------------------------*/
646 --
647 procedure val_pact_rr_rules (p_pact_rec      in pact_details,
648                              p_rollback_mode in varchar2) is
649 begin
650    if p_rollback_mode = 'RETRY' then
651       if p_pact_rec.action_type in ('V', 'B', 'Z', 'E', 'H') then
652          hr_utility.set_message(801, 'HR_7093_ACTION_CANT_RETPAY');
653          hr_utility.set_message_token('ACTION_NAME',p_pact_rec.action_name);
654          hr_utility.raise_error;
655       end if;
656    else
657       if p_pact_rec.action_type = 'Z' then
658          hr_utility.set_message(801, 'HR_7212_ACTION_RBACK_RULE');
659          hr_utility.set_message_token('ACTION_NAME',p_pact_rec.action_name);
660          hr_utility.raise_error;
661       end if;
662    end if;
663 end val_pact_rr_rules;
664 --
665 --
666 /*------------------------ val_assact_rr_rules -------------------------*/
667 --
668 function val_assact_rr_rules (p_pact_rec      in pact_details,
669                               p_rollback_mode in varchar2)
670                               return boolean is
671 begin
672 --
673    -- Validate the rollback and mark for retry rules for
674    -- assignment actions.
675    if g_debug then
676       hr_utility.set_location('hrassact.val_assact_rr_rules', 10);
677    end if;
678    if p_rollback_mode = 'RETRY' then
679       if p_pact_rec.action_type in ('V', 'B', 'Z', 'E', 'M', 'H', 'T') then
680          return false;
681       end if;
682    else
683       if p_pact_rec.action_type in ('Q', 'V', 'B', 'Z', 'U', 'E', 'M', 'T')
684       then
685          return false;
686       end if;
687    end if;
688 --
689    return true;
690 end val_assact_rr_rules;
691 --
692 --
693 /*----------------------- validate_rollback_mode ----------------------*/
694 --
695 procedure validate_rollback_mode(p_rollback_mode in varchar2) is
696 begin
697    if p_rollback_mode not in ('RETRY', 'ROLLBACK', 'BACKPAY') then
698       hr_utility.set_message(801, 'HR_7000_ACTION_BAD_ROLL_MODE');
699       hr_utility.raise_error;
700    end if;
701 end validate_rollback_mode;
702 --
703 --
704 /*-----------------------  ensure_assact_rolled_back ----------------------*/
705 /*
706  *   This routine checks that the target assignment action appears to
707  *   have been rolled back prior to it's deletion (i.e, must have no
708  *   attached run results, latest balances, quickpay inclusions, etc).
709  *
710  *   There are two levels of checking, since this routine is also
711  *   used to check whether a RETRY can be performed (in which case any
712  *   EEs created by a QuickPay are allowed to remain).
713  */
714 procedure ensure_assact_rolled_back (p_assact_id in number,
715                                      p_rollback_mode in varchar2) is
716 --
717    cursor c1 is
718    select null from dual
719    where exists
720       (select null                 --  check for any RRs
721        from   pay_run_results
722        where  assignment_action_id = p_assact_id)
723    or exists
724       (select null                 --  check for any MESSAGEs
725        from   pay_message_lines
726        where  source_type = 'A'
727        and    source_id = p_assact_id)
728 --   or exists                       --  check for any latest balances
729 --      (select null
730 --       from   pay_assignment_latest_balances
731 --       where  assignment_action_id = p_assact_id)
732 --   or exists                       --  check for any latest balances
733 --      (select null
734 --       from   pay_person_latest_balances
735 --       where  assignment_action_id = p_assact_id)
736 --   or exists                       --  check for any latest balances
737 --      (select null
738 --       from   pay_latest_balances
739 --       where  assignment_action_id = p_assact_id)
740    or exists                       --  check for updates to REEs
741        (select null
742         from   pay_element_entries_f
743         where  updating_action_id = p_assact_id);
744 --
745    cursor c2 is
746    select null from dual
747    where exists
748       (select null                 --  check for any RRs
749        from   pay_run_results
750        where  assignment_action_id = p_assact_id
751          and  status <> 'B')
752    or exists
753       (select null                 --  check for any MESSAGEs
754        from   pay_message_lines
755        where  source_type = 'A'
756        and    source_id = p_assact_id)
757 --   or exists                       --  check for any latest balances
758 --      (select null
759 --       from   pay_assignment_latest_balances
760 --       where  assignment_action_id = p_assact_id)
761 --   or exists                       --  check for any latest balances
762 --      (select null
763 --       from   pay_person_latest_balances
764 --       where  assignment_action_id = p_assact_id)
765 --   or exists                       --  check for any latest balances
766 --      (select null
767 --       from   pay_latest_balances
768 --       where  assignment_action_id = p_assact_id)
769    or exists                       --  check for updates to REEs
770        (select null
771         from   pay_element_entries_f
772         where  updating_action_id = p_assact_id);
773 --
774 begin
775    g_debug := hr_utility.debug_enabled;
776 --
777    if g_debug then
778       hr_utility.set_location('hrassact.ensure_assact_rolled_back',10);
779    end if;
780    validate_rollback_mode(p_rollback_mode);
781 --
782    if (p_rollback_mode = 'BACKPAY') then
783 --
784      if g_debug then
785         hr_utility.set_location('hrassact.ensure_assact_rolled_back',20);
786      end if;
787      for c2rec in c2 loop
788         --  any record fetched is an error
789         hr_utility.set_message(801, 'HR_7001_ACTION_MUST_ROLL_FIRST');
790         hr_utility.set_message_token('ASSACTID',p_assact_id);
791         hr_utility.raise_error;
792      end loop;
793 --
794    else
795 --
796      if g_debug then
797         hr_utility.set_location('hrassact.ensure_assact_rolled_back',30);
798      end if;
799      for c1rec in c1 loop
800         --  any record fetched is an error
801         hr_utility.set_message(801, 'HR_7001_ACTION_MUST_ROLL_FIRST');
802         hr_utility.set_message_token('ASSACTID',p_assact_id);
803         hr_utility.raise_error;
804      end loop;
805 --
806    end if;
807 --
808    return;
809 end ensure_assact_rolled_back;
810 --
811 --
812 /*-----------------------  ensure_pact_rolled_back ----------------------*/
813 /*
814  *   This routine checks that the payroll action about to be rolled
815  *   back has no assignment actions attached to it which would indicate that
816  *   the rollback_payroll_action procedure has not been called.
817  */
818 procedure ensure_pact_rolled_back (p_pact_id in number) is
819    cursor c1 is
820    select null
821    from   dual
822    where exists
823       (select null                 --  check for any ASSACTs
824        from   pay_assignment_actions
825        where  payroll_action_id = p_pact_id)
826    or exists
827       (select null                 --  check for any MESSAGEs
828        from   pay_message_lines
829        where  source_type = 'P'
830        and    source_id = p_pact_id);
831 begin
832    g_debug := hr_utility.debug_enabled;
833 --
834    if g_debug then
835       hr_utility.set_location('hrassact.ensure_pact_rolled_back',10);
836    end if;
837    for c1rec in c1 loop
838       --  any record fetched is an error
839       hr_utility.set_message(801, 'HR_7007_ACTION_ROLL_ASSACTS');
840       hr_utility.raise_error;
841    end loop;
842    return;
843 end ensure_pact_rolled_back;
844 --
845 --
846 /*-----------------------  val_assact_rollback  ----------------------*/
847 /*
848  *   This routine checks whether the specified assignment action can be
849  *   rolled back, but relies on parameters for all information at a
850  *   higher level than the ass action (e.g, for action type). This is
851  *   more efficient when testing a lot of actions on the same payroll
852  *   action.
853  *
854  *   This means that any rollback checks at a higher level (e.g, can't
855  *   rollback a purge) must also have been performed.
856  *
857  *   This routine is not needed to validate a mark for BACKPAY operation.
858  */
859 function val_assact_rollback (p_pact_rec in pact_details,
860                               p_assact_rec in out nocopy assact_details,
861                               p_rollback_mode in varchar2)
862    return boolean is
863 --
864    l_action_sequence     pay_assignment_actions.action_sequence%type;
865    l_action_status       pay_assignment_actions.action_status%type;
866    l_person_id           per_all_people_f.person_id%type;
867 begin
868 --
869    g_debug := hr_utility.debug_enabled;
870    --  Obtain information about this assignment action which we will
871    --  need later on.
872    --  Some of this is required for messages.
873    if g_debug then
874       hr_utility.set_location('hrassact.val_assact_rollback',10);
875    end if;
876    select ACT.assignment_id,
877           ACT.action_sequence,
878           ACT.action_status,
879           ASS.payroll_id,
880           POS.person_id,
881           substr(PEO.full_name,1,80),
882           ASS.assignment_number
883    into   p_assact_rec.assignment_id,
884           l_action_sequence,
885           l_action_status,
886           p_assact_rec.payroll_id,
887           l_person_id,
888           p_assact_rec.full_name,
889           p_assact_rec.assignment_number
890    from   per_periods_of_service POS,
891           per_all_assignments_f  ASS,
892           per_all_people_f       PEO,
893           pay_assignment_actions ACT
894    where  ACT.assignment_action_id = p_assact_rec.assact_id
895    and    ASS.assignment_id        = ACT.assignment_id
896    and    p_pact_rec.action_date between
897           ASS.effective_start_date and ASS.effective_end_date
898    and    PEO.person_id            = ASS.person_id
899    and    p_pact_rec.action_date between
900           PEO.effective_start_date and PEO.effective_end_date
901    and    POS.period_of_service_id = ASS.period_of_service_id;
902 --
903    --  can only retry if already complete
904    if p_rollback_mode = 'RETRY' and l_action_status not in ('C', 'S') then
905       return FALSE;
906    end if;
907 --
908 --
909    --
910    -- If rolling back or retrying, we need to know if assignments
911    -- can be considered in isolation (as prescribed by the
912    -- independent time periods flag for this legislation). Assignments
913    -- with no Payroll are independent.
914    --
915    -- Operation is disallowed if this is a sequenced action AND there
916    -- exists any sequenced actions in the future. Also disallowed
917    -- if any child action exists (e.g can't rollback a run if already
918    -- costed).
919    -- Note - exception is if are attempting to roll back Reversal or
920    -- Balance Adjustment actions, where we do not bother to perform
921    -- the future actions check.
922    --
923    declare
924       dummy number;
925    begin
926          -- For either legislation, examine the assignment action
927          -- to see if it is locked by another action. Peform
928          -- slightly different checks for RETRY and ROLLBACK
929          -- modes. See comments below.
930          if g_debug then
931             hr_utility.set_location('hrassact.val_assact_rollback',20);
932          end if;
933          if(p_rollback_mode = 'RETRY') then
934             -- Case for RETRY mode.
935             -- Check that the assignment action we are attempting to
936             -- mark for retry is not locked by an assignment action
937             -- that has an action_status that is not mark for retry.
938             select null
939             into   dummy
940             from   dual
941             where  not exists (
942                    select null
943                    from   pay_action_interlocks int,
944                           pay_assignment_actions act
945                    where  int.locked_action_id     =  p_assact_rec.assact_id
946                    and    act.assignment_action_id =  int.locking_action_id
947                    and    act.action_status        <> 'M');
948          else
949             -- Case for ROLLBACK mode.
950             -- Check that the assignment action we are attempting to
951             -- roll back is not locked by an assignment action.
952             select null
953             into   dummy
954             from   dual
955             where  not exists (
956                    select null
957                    from   pay_action_interlocks int
958                    where  int.locked_action_id = p_assact_rec.assact_id);
959          end if;
960 --
961          --   Now, if we have a balance adjustment or Reversal,
962          --   we do not bother to check for actions in the future,
963          --   because these are special cases.
964 --
965          --   Referring to above comments, the reversal has been
966          --   extended to incorporate new functionality to now
967          --   check for future payroll run actions that now interlock
968          if (p_pact_rec.action_type <> 'B'
969              and p_pact_rec.action_type <> 'I'
970              and p_pact_rec.action_type <> 'V')
971          then
972             -- Check the legislation case.
973             if p_pact_rec.independent_periods_flag then
974 --
975                --   check for other actions on this ASSIGNMENT
976                --   Perform different checks for RETRY or ROLLBACK.
977                if g_debug then
978                   hr_utility.set_location('hrassact.val_assact_rollback',30);
979                end if;
980                if(p_rollback_mode = 'RETRY') then
981                   -- Case for RETRY mode.
982                   -- Disallow mark for retry assignment action
983                   -- if there are future SEQUENCED assignment actions
984                   -- for the assignment and these actions are not
985                   -- marked for retry.
986                   select null into dummy
987                   from   dual
988                   where  not exists
989                      (select null
990                      from   pay_assignment_actions      ACT,
991                              pay_payroll_actions         PACT,
992                              pay_action_classifications CLASS,
993                              pay_action_classifications CLAS2
994                       where  ACT.assignment_id  = p_assact_rec.assignment_id
995                       and    ACT.action_sequence       > l_action_sequence
996                       and    ACT.action_status        <> 'M'
997                       and    ACT.payroll_action_id     = PACT.payroll_action_id
998                       and    PACT.action_type          = CLASS.action_type
999                       and    CLASS.classification_name = 'SEQUENCED'
1000                       and    CLAS2.action_type         = p_pact_rec.action_type
1001                       and    CLAS2.classification_name = 'SEQUENCED' );
1002                else
1003                   -- Case for ROLLBACK mode.
1004                   -- Disallow rollback assignment action
1005                   -- if there are future SEQUENCED assignment actions
1006                   -- for the assignment.
1007                   select null into dummy
1008                   from   dual
1009                   where  not exists
1010                      (select null
1011                       from   pay_assignment_actions     ACT,
1012                              pay_payroll_actions        PACT,
1013                              pay_action_classifications CLASS,
1014                              pay_action_classifications CLAS2
1015                       where  ACT.assignment_id     = p_assact_rec.assignment_id
1016                       and    ACT.action_sequence       > l_action_sequence
1017                       and    ACT.payroll_action_id     = PACT.payroll_action_id
1018                       and    PACT.action_type          = CLASS.action_type
1019                       and    CLASS.classification_name = 'SEQUENCED'
1020                       and    CLAS2.action_type         = p_pact_rec.action_type
1021                       and    CLAS2.classification_name = 'SEQUENCED' );
1022                end if;
1023              else
1024                --   check for other actions on this PERSON
1025                --   As above, perform different checks for
1026                --   RETRY and ROLLBACK modes.
1027                if g_debug then
1028                   hr_utility.set_location('hrassact.val_assact_rollback',40);
1029                end if;
1030                if(p_rollback_mode = 'RETRY') then
1031                   -- Case for RETRY mode.
1032                   select null into dummy
1033                   from   dual
1034                   where  not exists
1035                      (select null
1036                       from   pay_action_classifications CLASS,
1037                              pay_action_classifications CLAS2,
1038                              pay_payroll_actions        PACT,
1039                              pay_assignment_actions     ACT,
1040                              per_all_assignments_f          ASS,
1041                              per_periods_of_service     POS
1042                       where  POS.person_id             = l_person_id
1043                       and    ASS.period_of_service_id = POS.period_of_service_id
1044                       and    ACT.assignment_id         = ASS.assignment_id
1045                       and    ACT.action_sequence       > l_action_sequence
1046                       and    ACT.action_status        <> 'M'
1047                       and    ACT.payroll_action_id     = PACT.payroll_action_id
1048                       and    PACT.action_type          = CLASS.action_type
1049                       and    CLASS.classification_name = 'SEQUENCED'
1050                       and    CLAS2.action_type         = p_pact_rec.action_type
1051                       and    CLAS2.classification_name = 'SEQUENCED' );
1052                else
1053                   -- Case for ROLLBACK mode.
1054                   select null into dummy
1055                   from   dual
1056                   where  not exists
1057                      (select null
1058                       from   pay_action_classifications CLASS,
1059                              pay_action_classifications CLAS2,
1060                              pay_payroll_actions        PACT,
1061                              pay_assignment_actions     ACT,
1062                              per_all_assignments_f          ASS,
1063                              per_periods_of_service     POS
1064                       where  POS.person_id             = l_person_id
1065                       and    ASS.period_of_service_id = POS.period_of_service_id
1066                       and    ACT.assignment_id         = ASS.assignment_id
1067                       and    ACT.action_sequence       > l_action_sequence
1068                       and    ACT.payroll_action_id     = PACT.payroll_action_id
1069                       and    PACT.action_type          = CLASS.action_type
1070                       and    CLASS.classification_name = 'SEQUENCED'
1071                       and    CLAS2.action_type         = p_pact_rec.action_type
1072                       and    CLAS2.classification_name = 'SEQUENCED' );
1073                end if;
1074             end if;
1075          end if;
1076 --
1077    exception
1078       when no_data_found then
1079          if g_debug then
1080             hr_utility.set_location('hrassact.val_assact_rollback',50);
1081          end if;
1082          return FALSE;
1083 --
1084    end;
1085 --
1086    if g_debug then
1087       hr_utility.set_location('hrassact.val_assact_rollback',60);
1088    end if;
1089    return TRUE;
1090 --
1091 end val_assact_rollback;
1092 --
1093 --
1094 /*-----------------------  val_pact_rollback -------------------------*/
1095 /*
1096  *   This routine is called before a rollback to get any payroll action level
1097  *   information (e.g, action type) whch will be needed. This routine also
1098  *   performs some validation as to whether the action can be rolled back,
1099  *   and so may fail.
1100  */
1101 procedure val_pact_rollback (p_pact_rec in out nocopy pact_details,
1102                                   p_rollback_mode in varchar2 ) is
1103 --
1104    l_business_group_id   hr_organization_units.business_group_id%type;
1105 begin
1106 --
1107    validate_rollback_mode(p_rollback_mode);
1108 --
1109    --  get payroll action level information
1110    if g_debug then
1111       hr_utility.set_location('hrassact.val_pact_rollback', 10);
1112    end if;
1113    select pac.business_group_id,
1114           pac.effective_date,
1115           hrl.meaning,
1116           pac.action_type,
1117           trunc(sysdate),
1118           pay.payroll_name,
1119           grp.name
1120    into   l_business_group_id,
1121           p_pact_rec.action_date,
1122           p_pact_rec.action_name,
1123           p_pact_rec.action_type,
1124           p_pact_rec.current_date,
1125           p_pact_rec.payroll_name,
1126           p_pact_rec.bg_name
1127    from   pay_payroll_actions pac,
1128           pay_all_payrolls_f  pay,
1129           per_business_groups_perf grp,
1130           hr_lookups          hrl
1131    where  pac.payroll_action_id     = p_pact_rec.pact_id
1132    and    hrl.lookup_code           = pac.action_type
1133    and    hrl.lookup_type           = 'ACTION_TYPE'
1134    and    grp.business_group_id     = pac.business_group_id
1135    and    pay.payroll_id (+)        = pac.payroll_id
1136    and    pac.effective_date between
1137           pay.effective_start_date (+) and pay.effective_end_date (+);
1138    if g_debug then
1139       hr_utility.trace('action type is ' || p_pact_rec.action_type );
1140    end if;
1141 --
1142 --
1143    --  some types (e.g, purge) of action can't be rolled back full stop.
1144    declare
1145       dummy number;
1146    begin
1147       if g_debug then
1148          hr_utility.set_location('hrassact.val_pact_rollback', 20);
1149       end if;
1150       select null
1151       into   dummy
1152       from   dual
1153       where  not exists
1154          (select null
1155           from   pay_action_classifications
1156           where  action_type = p_pact_rec.action_type
1157           and    classification_name = 'NONREMOVEABLE');
1158    exception
1159       when no_data_found then
1160          hr_utility.set_message(801, 'HR_6216_ACTION_CANT_PURGE');
1161          hr_utility.set_message_token('PACT_ID',
1162                                     to_char(p_pact_rec.pact_id));
1163          hr_utility.raise_error;
1164    end;
1165 --
1166 --
1167    --  get some more info needed to roll back actions
1168    if g_debug then
1169       hr_utility.set_location('hrassact.val_pact_rollback', 30);
1170    end if;
1171    if upper( hr_leg_rule.get_independent_periods(l_business_group_id))
1172       like 'Y%' then
1173       p_pact_rec.independent_periods_flag := TRUE;
1174    else
1175       p_pact_rec.independent_periods_flag := FALSE;
1176    end if;
1177 --
1178 --
1179    --  see if this type of action is sequenced or not
1180    declare
1181       dummy number;
1182    begin
1183       p_pact_rec.sequenced_flag := TRUE;
1184 --
1185       if g_debug then
1186          hr_utility.set_location('hrassact.val_pact_rollback', 40);
1187       end if;
1188       select null
1189       into   dummy
1190       from   pay_action_classifications CLASS
1191       where  CLASS.action_type = p_pact_rec.action_type
1192       and    CLASS.classification_name = 'SEQUENCED';
1193       if g_debug then
1194          hr_utility.trace('this action type IS sequenced');
1195       end if;
1196    exception
1197       when no_data_found then
1198          p_pact_rec.sequenced_flag := FALSE;
1199          if g_debug then
1200             hr_utility.trace('this action type NOT sequenced');
1201          end if;
1202    end;
1203 end val_pact_rollback;
1204 --
1205 --
1206 procedure validate_pact_rollback
1207 (
1208    p_payroll_action_id in number,
1209    p_rollback_mode     in varchar2
1210 ) is
1211    l_pact_rec pact_details;
1212 begin
1213    g_debug := hr_utility.debug_enabled;
1214 --
1215    -- We simply call the internal validate rollback procedure
1216    -- to give us the information we need.
1217    if g_debug then
1218       hr_utility.set_location('hrassact.validate_pact_rollback', 10);
1219    end if;
1220    l_pact_rec.pact_id := p_payroll_action_id;
1221 --
1222    val_pact_rollback(p_pact_rec      => l_pact_rec,
1223                      p_rollback_mode => p_rollback_mode);
1224 --
1225    -- Rollback and Mark for retry rules
1226    val_pact_rr_rules(l_pact_rec, p_rollback_mode);
1227 --
1228 end validate_pact_rollback;
1229 --
1230 /*-----------------------  validate_assact_rollback -------------------------*/
1231 /*
1232  *   This procedure is an overloaded public procedure to validate a
1233  *   particular assignment action.
1234  */
1235 function validate_assact_rollback
1236 (
1237    p_payroll_action_id    in number,
1238    p_assignment_action_id in number,
1239    p_rollback_mode        in varchar2
1240 ) return boolean is
1241    l_pact_rec   pact_details;
1242    l_assact_rec assact_details;
1243    result       boolean;
1244 begin
1245    g_debug := hr_utility.debug_enabled;
1246 --
1247    -- Call the validate payroll action routine to get pact details.
1248    if g_debug then
1249       hr_utility.set_location('hrassact.validate_assact_rollback',10);
1250    end if;
1251    l_pact_rec.pact_id := p_payroll_action_id;
1252    val_pact_rollback(p_pact_rec      => l_pact_rec,
1253                      p_rollback_mode => p_rollback_mode);
1254 --
1255    -- Call the validate assignment action routine to validate assact.
1256    if g_debug then
1257       hr_utility.set_location('hrassact.validate_assact_rollback',20);
1258    end if;
1259    l_assact_rec.assact_id := p_assignment_action_id;
1260 --
1261    if val_assact_rollback(l_pact_rec, l_assact_rec, p_rollback_mode) and
1262       val_assact_rr_rules(l_pact_rec, p_rollback_mode)
1263    then
1264       return true;
1265    else
1266       return false;
1267    end if;
1268 end validate_assact_rollback;
1269 --
1270 --
1271 --
1272 /*-----------------------  trash_quickpay  ----------------------------*/
1273 /*
1274  *   This procedure removes any entries inserted for a Quickpay action
1275  */
1276 procedure trash_quickpay (p_action_id number) is
1277    cursor c1 is
1278    select pee.element_entry_id
1279    from   pay_element_entries_f pee,
1280           pay_assignment_actions paa
1281    where  pee.creator_type = 'Q'
1282    and    pee.creator_id = paa.assignment_action_id
1283    and    pee.assignment_id = paa.assignment_id
1284    and    paa.assignment_action_id = p_action_id
1285    for update of pee.element_entry_id;
1286 --
1287 begin
1288    --  For QuickPay actions, we delete the entries which were
1289    --  inserted as part of the Quickpay transaction. Any processed run
1290    --  results were trashed in a previous step.
1291    begin
1292       -- We wish to remove the QuickPay inclusions.
1293       if g_debug then
1294          hr_utility.set_location('hrassact.trash_quickpay',10);
1295       end if;
1296 --
1297       -- Enhancement 3368211
1298       -- Delete from both PAY_QUICKPAY_INCLUSIONS and
1299       -- PAY_QUICKPAY_EXCLUSIONS.
1300       --
1301       -- There is a chance the assignment action id exists in both tables if
1302       -- the assignment action was created before the QuickPay Exclusions
1303       -- data model was in use.
1304       delete from pay_quickpay_exclusions
1305       where  assignment_action_id = p_action_id;
1306 --
1307       delete from pay_quickpay_inclusions
1308       where  assignment_action_id = p_action_id;
1309 --
1310       if g_debug then
1311          hr_utility.set_location('hrassact.trash_quickpay',20);
1312       end if;
1313       for c1rec in c1 loop
1314 --
1315          --  delete any unprocessed run result attached to the entry.
1316          if g_debug then
1317             hr_utility.set_location('hrassact.trash_quickpay',30);
1318          end if;
1319          delete from pay_run_result_values RRV
1320          where  RRV.run_result_id in
1321             (select RR.run_result_id
1322              from   pay_run_results RR
1323              where  RR.source_type = 'E'
1324              and    RR.source_id = c1rec.element_entry_id);
1325 --
1326          if g_debug then
1327             hr_utility.set_location('hrassact.trash_quickpay',40);
1328          end if;
1329          delete from pay_run_results RR
1330          where  RR.source_type = 'E'
1331          and    RR.source_id = c1rec.element_entry_id;
1332 --
1333          --  delete any element entry values
1334          if g_debug then
1335             hr_utility.set_location('hrassact.trash_quickpay',50);
1336          end if;
1337          delete from pay_element_entry_values_f EEV
1338          where  EEV.element_entry_id = c1rec.element_entry_id;
1339 --
1340          --  delete the entry itself
1341          if g_debug then
1342             hr_utility.set_location('hrassact.trash_quickpay',60);
1343          end if;
1344          delete from pay_element_entries_f
1345          where  current of c1;
1346 --
1347       end loop;
1348    end;
1349 --
1350    if g_debug then
1351       hr_utility.set_location('hrassact.trash_quickpay',60);
1352    end if;
1353    return;
1354 --
1355 end trash_quickpay;
1356 --
1357 --
1358 --
1359 
1360 /*------------------------ undo_stop_update -----------------------------*/
1361 /*
1362  * This procedure is called when we have detected the need to undo the
1363  * effect of a stop or update recurring entry formula result rule.
1364  * Note that, due to the complexity of calculating entry end dates, we
1365  * call the existing routine, but trap error messages that are
1366  * inappropriate for our application.
1367  */
1368 procedure undo_stop_update(
1369    p_ee_id in number,
1370    p_mult  in varchar,
1371    p_date  in date,
1372    p_mode  in varchar2) is
1373 --
1374    -- Local variables.
1375    effstart   date;
1376    effend     date;
1377    val_start  date;
1378    val_end    date;
1379    next_end   date;
1380    orig_ee_id number;
1381    asg_id     number;
1382    el_id      number;
1383    c_indent   constant varchar2(30) := 'pyassact.undo_stop_update';
1384 begin
1385    -- Select some information about the entry we are operating on.
1386    if g_debug then
1387       hr_utility.set_location(c_indent, 10);
1388    end if;
1389    select pee.effective_start_date,
1390           pee.effective_end_date,
1391           pee.original_entry_id,
1392           pee.assignment_id,
1393           pee.element_link_id
1394    into   effstart, effend, orig_ee_id, asg_id, el_id
1395    from   pay_element_entries_f pee
1396    where  pee.element_entry_id = p_ee_id
1397    and    p_date between
1398           pee.effective_start_date and pee.effective_end_date;
1399 --
1400    -- Do nothing if the entry end date is end of time.
1401    if(effend = c_eot) then
1402       return;
1403    end if;
1404 --
1405    -- For undo update, we have to get next effective start date.
1406    if(p_mode = 'DELETE_NEXT_CHANGE') then
1407       begin
1408          if g_debug then
1409             hr_utility.set_location(c_indent, 20);
1410          end if;
1411          select min(ee.effective_end_date)
1412          into   next_end
1413          from   pay_element_entries_f ee
1414          where  ee.element_entry_id     = p_ee_id
1415          and    ee.effective_start_date > effend;
1416       exception
1417          when no_data_found then null;
1418       end;
1419 --
1420       val_start := effend + 1;
1421 --
1422       if next_end is null then
1423          val_end := c_eot;
1424       else
1425          val_end := next_end;
1426       end if;
1427    elsif(p_mode = 'FUTURE_CHANGE') then
1428       val_start := effend + 1;
1429       val_end   := c_eot;
1430    end if;
1431 --
1432    -- For either mode, we need to obtain the date to which
1433    -- we may legally extend the entry.
1434    declare
1435       message    varchar2(200);
1436       applid     varchar2(200);
1437    begin
1438       val_end := hr_entry.recurring_entry_end_date (
1439                   asg_id, el_id, p_date, 'Y', p_mult, p_ee_id, orig_ee_id);
1440    exception
1441       -- Several error messages can be raised from this procedure.
1442       -- We wish to trap a number of them, as they should be ignored
1443       -- for our purposes.
1444       when hr_utility.hr_error then
1445       hr_utility.get_message_details(message,applid);
1446 --
1447       if(message in ('HR_7699_ELE_ENTRY_REC_EXISTS',
1448                      'HR_7700_ELE_ENTRY_REC_EXISTS',
1449                      'HR_6281_ELE_ENTRY_DT_DEL_LINK',
1450                      'HR_6283_ELE_ENTRY_DT_ELE_DEL',
1451                      'HR_6284_ELE_ENTRY_DT_ASG_DEL')
1452       ) then
1453          -- We cannot extend the entry.
1454          if g_debug then
1455             hr_utility.set_location(c_indent, 25);
1456          end if;
1457          return;
1458       else
1459          -- Should fail if it is anything else.
1460          raise;
1461       end if;
1462    end;
1463 --
1464    -- May need to check for entry overlap.
1465    if(p_mult = 'N') then
1466 --
1467       declare dummy number;
1468       begin
1469          if g_debug then
1470             hr_utility.set_location(c_indent, 30);
1471          end if;
1472          select null
1473          into   dummy
1474          from   pay_element_entries_f ee
1475          where  ee.entry_type = 'E'
1476          and    ee.element_entry_id <> p_ee_id
1477          and    ee.assignment_id     = asg_id
1478          and    ee.element_link_id   = el_id
1479          and   (ee.effective_start_date <= val_end and
1480                 ee.effective_end_date   >= val_start);
1481 --
1482          -- If row returned, we are in trouble.
1483          hr_utility.set_message(801, 'HR_6956_ELE_ENTRY_OVERLAP');
1484          hr_utility.raise_error;
1485 --
1486      exception
1487        when no_data_found then null;
1488      end;
1489    end if;
1490 --
1491    -- May need to set validation end date to the end of time.
1492    if((p_mode = 'FUTURE_CHANGE') or
1493        (p_mode = 'DELETE_NEXT_CHANGE' and
1494         val_end = c_eot)
1495    ) then
1496       effend := val_end;
1497    end if;
1498 --
1499    -- Process the delete of element entries.
1500    if(p_mode = 'DELETE_NEXT_CHANGE') then
1501       if g_debug then
1502          hr_utility.set_location(c_indent, 40);
1503       end if;
1504       delete from pay_element_entries_f ee
1505       where  ee.element_entry_id     = p_ee_id
1506       and    ee.effective_start_date = val_start;
1507 --
1508       if g_debug then
1509          hr_utility.set_location(c_indent, 50);
1510       end if;
1511       update pay_element_entries_f ee
1512       set    ee.effective_end_date = decode(val_end, c_eot, effend, val_end)
1513       where  ee.element_entry_id     = p_ee_id
1514       and    ee.effective_start_date = effstart;
1515 --
1516    elsif(p_mode = 'FUTURE_CHANGE') then
1517 --
1518       if g_debug then
1519          hr_utility.set_location(c_indent, 60);
1520       end if;
1521       delete from pay_element_entries_f ee
1522       where  ee.element_entry_id     = p_ee_id
1523       and    ee.effective_start_date > effstart;
1524 --
1525       if g_debug then
1526          hr_utility.set_location(c_indent, 70);
1527       end if;
1528       update pay_element_entries_f ee
1529       set    ee.effective_end_date = effend
1530       where  ee.element_entry_id   = p_ee_id
1531       and  ee.effective_start_date = effstart;
1532    end if;
1533 --
1534    -- Now, delete the entry values between validation start/end dates..
1535 --
1536    if g_debug then
1537       hr_utility.set_location(c_indent, 80);
1538    end if;
1539    delete from pay_element_entry_values_f eev
1540    where  eev.element_entry_id = p_ee_id
1541    and    ((eev.effective_end_date between val_start and val_end)
1542       or    (eev.effective_start_date between val_start and val_end));
1543 --
1544    -- Update the effective end date as appropriate.
1545    if g_debug then
1546       hr_utility.set_location(c_indent, 90);
1547    end if;
1548    update  pay_element_entry_values_f eev
1549    set     eev.effective_end_date = val_end
1550    where   eev.element_entry_id   = p_ee_id
1551    and     p_date between
1552            eev.effective_start_date and eev.effective_end_date;
1553 --
1554 end undo_stop_update;
1555 --
1556 /*-----------------------  do_assact_rollback  ---------------------------*/
1557 /*
1558  *
1559  *   This procedure performs all third party DML and validation
1560  *   needed to support the rollback, mark for retry or mark
1561  *   for backpay of an assignment action.
1562  *
1563  *   For the definitive list of action types, see the ACTION TYPE
1564  *   domain in CASE.
1565  *
1566  *   Any deletes of child records which should only be performed for
1567  *   rollback, rather than for both retry and rollback, are performed
1568  *   either via cascading constraints or via the delete trigger on
1569  *   assignment actions.
1570  *
1571  */
1572 procedure do_assact_rollback (p_pact_rec in pact_details,
1573                               p_assact_rec in assact_details,
1574                               p_rollback_mode in varchar2,
1575                               p_leave_base_table_row in boolean) is
1576 begin
1577 --
1578    if p_pact_rec.sequenced_flag then
1579 --
1580       --  Delete any Run Results which were created by this action. This
1581       --  will have no effect for unsequenced actions such as
1582       --  Pre-Payments. For efficiency there should be no cascade
1583       --  trigger on Run Results so we need to trash the values as well.
1584       if g_debug then
1585          hr_utility.set_location('hrassact.do_assact_rollback',10);
1586       end if;
1587       delete from pay_run_result_values RRV
1588       where  RRV.run_result_id in
1589          (select RR.run_result_id
1590           from   pay_run_results RR
1591           where  RR.assignment_action_id = p_assact_rec.assact_id);
1592 --
1593       if g_debug then
1594          hr_utility.set_location('hrassact.do_assact_rollback',20);
1595       end if;
1596       delete from pay_run_results RR
1597       where  RR.assignment_action_id = p_assact_rec.assact_id;
1598 --
1599       --  Delete latest balances. Not deleted via constraint due to
1600       --  performance requirements on Insert and Update, and also because
1601       --  they need to go for retry as well as rollback.
1602       --  Start with any balance context values.
1603       if g_debug then
1604          hr_utility.set_location('hrassact.do_assact_rollback',25);
1605       end if;
1606       delete from pay_balance_context_values VAL
1607       where  exists (
1608              select null
1609              from   pay_person_latest_balances PLB
1610              where  PLB.assignment_action_id = p_assact_rec.assact_id
1611              and    VAL.latest_balance_id    = PLB.latest_balance_id);
1612 --
1613       if g_debug then
1614          hr_utility.set_location('hrassact.do_assact_rollback',30);
1615       end if;
1616       delete from pay_person_latest_balances PLB
1617       where  PLB.assignment_action_id = p_assact_rec.assact_id;
1618 --
1619       if g_debug then
1620          hr_utility.set_location('hrassact.do_assact_rollback',35);
1621       end if;
1622       delete from pay_balance_context_values VAL
1623       where  exists (
1624              select null
1625              from   pay_assignment_latest_balances ALB
1626              where  ALB.assignment_action_id = p_assact_rec.assact_id
1627              and    VAL.latest_balance_id    = ALB.latest_balance_id);
1628 --
1629       if g_debug then
1630          hr_utility.set_location('hrassact.do_assact_rollback',40);
1631       end if;
1632       delete from pay_assignment_latest_balances ALB
1633       where  ALB.assignment_action_id = p_assact_rec.assact_id;
1634 --
1635       -- If the payroll action that is being deleted is
1636       -- a Balance Adjustment (type = 'B'), then we attempt to
1637       -- delete the element entry row and element entry rows
1638       -- that are associated with it.
1639       if p_pact_rec.action_type = 'B' then
1640          -- Do the business for Balance Adjustment.
1641          declare
1642             v_eeid number; -- the element entry id.
1643          begin
1644             -- Start by selecting the element_entry_id
1645             -- that we need to delete.
1646             -- We are joining effective dates only to hit the whole
1647             -- index, because there should only be the one row anyway.
1648 --            hr_utility.set_location('hrassact.do_assact_rollback',50);
1649             select pee.element_entry_id
1650             into   v_eeid
1651             from   pay_element_entries_f pee
1652             where  pee.creator_id = p_assact_rec.assact_id
1653             and    pee.assignment_id = p_assact_rec.assignment_id
1654             and    p_pact_rec.action_date between
1655                    pee.effective_start_date and pee.effective_end_date;
1656 --
1657             -- Now, we attempt to delete the entry values.
1658 --            hr_utility.set_location('hrassact.do_assact_rollback',60);
1659             delete from pay_element_entry_values_f pev
1660             where  pev.element_entry_id = v_eeid
1661             and    p_pact_rec.action_date between
1662                    pev.effective_start_date and pev.effective_end_date;
1663 --
1664             -- Now we attempt to delete the element entry row.
1665             -- Note, if this procedure is called from the balance
1666             -- adjustment row, the form may be attempting to delete
1667             -- this row. However, this could be called from the
1668             -- actions form, in which case we do need to do the delete.
1669 --            hr_utility.set_location('hrassact.do_assact_rollback',70);
1670             delete from pay_element_entries_f pee
1671             where  pee.element_entry_id = v_eeid
1672             and    p_pact_rec.action_date between
1673                    pee.effective_start_date and pee.effective_end_date;
1674          end;
1675       else
1676          -- Now process for Non balance adjustment case.
1677 --
1678          -- The following section is designed to undo any changes
1679          -- to Recurring element entries that have been made by
1680          -- the Payroll Run, via Update and Stop REE rules.
1681          -- Note, we have to perform some extra joins to check
1682          -- that we are only picking up recurring element entries.
1683          declare
1684             cursor c1 is
1685             select pet.multiple_entries_allowed_flag,
1686                    pee.element_entry_id,
1687                    pee.updating_action_id,
1688                    pee.effective_start_date,
1689                    pee.effective_end_date
1690             from   pay_element_types_f   pet,
1691                    pay_element_links_f   pel,
1692                    pay_element_entries_f pee
1693             where  pee.assignment_id   = p_assact_rec.assignment_id
1694             and    pee.entry_type      = 'E'
1695             and    p_pact_rec.action_date between
1696                    pee.effective_start_date and pee.effective_end_date
1697             and    pel.element_link_id = pee.element_link_id
1698             and    p_pact_rec.action_date between
1699                    pel.effective_start_date and pel.effective_end_date
1700             and    pet.element_type_id = pel.element_type_id
1701             and    p_pact_rec.action_date between
1702                    pel.effective_start_date and pel.effective_end_date
1703             and    pet.processing_type = 'R';
1704 --
1705             v_max_date date; -- max effective date of element entry.
1706          begin
1707             -- Loop through all the standard entries for the assignment
1708             -- and attempt to undo changes that have been made by
1709             -- stop and update REE rules. We cannot be guaranteed
1710             -- to undo changes in their entirety, since we do not
1711             -- have all the information we need to hand, but we
1712             -- perform delete next change.
1713             if g_debug then
1714                hr_utility.set_location('hrassact.do_assact_rollback',80);
1715             end if;
1716             for c1rec in c1 loop
1717                -- First, look for possible stop rule case.
1718                if(c1rec.effective_end_date = p_pact_rec.action_date) then
1719                   -- We may have a stopped entry, but we need to
1720                   -- see if this really is the case.
1721                   if g_debug then
1722                      hr_utility.set_location('hrassact.do_assact_rollback',90);
1723                   end if;
1724                   select max(pee.effective_end_date)
1725                   into   v_max_date
1726                   from   pay_element_entries_f pee
1727                   where  pee.element_entry_id = c1rec.element_entry_id;
1728 --
1729                   if(v_max_date = p_pact_rec.action_date) then
1730                      -- This entry has been chopped off. We assume
1731                      -- it has been performed by the Payroll Run.
1732                      -- Call the ee api to delete next change.
1733                      undo_stop_update (c1rec.element_entry_id,
1734                                        c1rec.multiple_entries_allowed_flag,
1735                                        p_pact_rec.action_date,
1736                                        'FUTURE_CHANGE');
1737                   end if;
1738                end if; -- stop rule.
1739 --
1740                -- Now, we look for an Update Rule Case.
1741                -- Here we need to see if there is a record
1742                -- whoes effective start date is the same as
1743                -- that of the run, and the updating_action_id of
1744                -- the entry is the same as the assignment_action_id
1745                -- and there exists a previous record with effective
1746                -- end date of the previous day (i.e. a date effective
1747                -- update has occurred).
1748                if(c1rec.effective_start_date = p_pact_rec.action_date
1749                     and c1rec.updating_action_id = p_assact_rec.assact_id)
1750                then
1751                   -- Note, in following select, use max to avoid
1752                   -- having to deal with no data found error.
1753                   if g_debug then
1754                      hr_utility.set_location('hrassact.do_assact_rollback',110);
1755                   end if;
1756                   select max(pee.effective_end_date)
1757                   into   v_max_date
1758                   from   pay_element_entries_f pee
1759                   where  pee.element_entry_id   = c1rec.element_entry_id
1760                   and    pee.effective_end_date = (p_pact_rec.action_date - 1);
1761 --
1762                   if(v_max_date is not null) then
1763                      -- Ok, there is a previous record. We now wish
1764                      -- to delete future changes on that record.
1765                      undo_stop_update (c1rec.element_entry_id,
1766                                        c1rec.multiple_entries_allowed_flag,
1767                                        (p_pact_rec.action_date - 1),
1768                                        'DELETE_NEXT_CHANGE');
1769                   else
1770                      -- In the case where there is no previous record,
1771                      -- the update was previously a correction. This
1772                      -- means we need to set the updating_action_id
1773                      -- to null.
1774                      if g_debug then
1775                         hr_utility.set_location('hrassact.do_assact_rollback',130);
1776                      end if;
1777                      update pay_element_entries_f pee
1778                      set    pee.updating_action_id = null
1779                      where  pee.element_entry_id   = c1rec.element_entry_id
1780                      and    p_pact_rec.action_date between
1781                             pee.effective_start_date and pee.effective_end_date;
1782                   end if;
1783                end if;
1784             end loop;
1785          end;
1786       end if; -- end of Balance Adjustment specific stuff.
1787 --
1788    end if;   --  end of actions specific to SEQUENCED actions
1789 --
1790 --
1791    --  delete any messages associated with the assignment action.
1792    if g_debug then
1793       hr_utility.set_location('hrassact.do_assact_rollback',140);
1794    end if;
1795    delete from pay_message_lines ML
1796    where  ML.source_type = 'A'
1797    and    ML.source_id = p_assact_rec.assact_id;
1798 --
1799    if(p_pact_rec.action_type in ('P','U')) then
1800       -- Need to delete pre-payment rows.
1801       -- Originally left it to the data base, but
1802       -- that meant that rows were not deleted
1803       -- in retry mode.
1804       -- Note, the delete of a pre-payment row causes
1805       -- a cascade delete from pay_coin_anal_elements.
1806       if g_debug then
1807          hr_utility.set_location('hrassact.do_assact_rollback',150);
1808       end if;
1809       delete from pay_pre_payments ppp
1810       where  ppp.assignment_action_id = p_assact_rec.assact_id;
1811    end if;
1812 --
1813    if(p_pact_rec.action_type = 'C') then
1814       -- Delete from the costing table.
1815       delete from pay_costs
1816       where  assignment_action_id = p_assact_rec.assact_id;
1817    end if;
1818 --
1819    /*
1820     *   ROLLBACK specific code
1821     */
1822    if p_rollback_mode = 'ROLLBACK' then
1823       --
1824       --  Write a message to payroll action level to indicate
1825       --  that the action has been rolled back.
1826       --
1827       declare
1828          mesg_text     pay_message_lines.line_text%type;
1829       begin
1830          hr_utility.set_message (801, 'HR_ACTION_ASACT_ROLLOK');
1831          hr_utility.set_message_token
1832               ('ASG_NUMBER',p_assact_rec.assignment_number);
1833          hr_utility.set_message_token
1834               ('FULL_NAME',p_assact_rec.full_name);
1835          hr_utility.set_message_token
1836               ('SYSDATE',fnd_date.date_to_canonical(p_pact_rec.current_date));
1837          mesg_text := substrb(hr_utility.get_message,1,240);
1838 --
1839          --  now write our message to the payroll_action level. We
1840          --  want to spit out the asssignment's current Payroll ID as well.
1841          if g_debug then
1842             hr_utility.set_location('hrassact.do_assact_rollback',160);
1843          end if;
1844          insert into pay_message_lines
1845          (line_sequence,
1846           payroll_id,
1847           message_level,
1848           source_id,
1849           source_type,
1850           line_text
1851          )
1852          values
1853          (pay_message_lines_s.nextval,
1854           p_assact_rec.payroll_id,
1855           'I',           -- Information level
1856           p_pact_rec.pact_id,
1857           'P',
1858           mesg_text
1859          );
1860       end;
1861 --
1862       if p_pact_rec.action_type = 'Q' then
1863          trash_quickpay (p_action_id => p_assact_rec.assact_id);
1864       end if;
1865 --
1866    end if;
1867 --
1868    --   see if we want to alter the assignment action itself (we wouldn't
1869    --   if we were being called from a form)
1870    if not p_leave_base_table_row then
1871       if p_rollback_mode = 'RETRY' then
1872             if g_debug then
1873                hr_utility.set_location('hrassact.do_assact_rollback',170);
1874             end if;
1875             update pay_assignment_actions
1876             set    action_status = 'M'
1877             where  assignment_action_id = p_assact_rec.assact_id;
1878 --
1879       elsif p_rollback_mode = 'BACKPAY' then
1880             if g_debug then
1881                hr_utility.set_location('hrassact.do_assact_rollback',180);
1882             end if;
1883             update pay_assignment_actions
1884             set    action_status = 'B'
1885             where  assignment_action_id = p_assact_rec.assact_id;
1886 --
1887       elsif p_rollback_mode = 'ROLLBACK' then
1888             -- there may be pay_action_interlock rows.
1889             -- which are locking other assignment actions.
1890             if g_debug then
1891                hr_utility.set_location('hrassact.do_assact_rollback',190);
1892             end if;
1893             delete from pay_action_interlocks lck
1894             where  lck.locking_action_id = p_assact_rec.assact_id;
1895 --
1896             if g_debug then
1897                hr_utility.set_location('hrassact.do_assact_rollback',200);
1898             end if;
1899             delete from pay_assignment_actions
1900             where  assignment_action_id = p_assact_rec.assact_id;
1901       end if;
1902    else
1903       -- In the case of rolling back (from the form), we
1904       -- still need to delete interlock rows. Of course,
1905       -- in this case we do not delete the action.
1906       if(p_rollback_mode = 'ROLLBACK') then
1907          if g_debug then
1908             hr_utility.set_location('hrassact.do_assact_rollback',210);
1909          end if;
1910          delete from pay_action_interlocks lck
1911          where  lck.locking_action_id = p_assact_rec.assact_id;
1912       end if;
1913    end if;
1914 --
1915    if g_debug then
1916       hr_utility.set_location('hrassact.do_assact_rollback',220);
1917    end if;
1918    return;
1919 --
1920 end do_assact_rollback;
1921 --
1922 --
1923 --
1924 /*-----------------------  do_pact_rollback  ----------------------*/
1925 /*
1926  *   This routine performs the actual work of rolling back a
1927  *   payroll action. See the description of the three overloaded
1928  *   calls which use this internal function for details.
1929  *
1930  */
1931 procedure do_pact_rollback
1932               (p_payroll_action_id in number,
1933                p_chunk_size in number default 200,
1934                p_all_or_nothing in boolean default TRUE,
1935                p_failed_assact in out nocopy number,
1936                p_rollback_mode in varchar2,
1937                p_leave_base_table_row in boolean) is
1938 --
1939    l_pact_rec   pact_details;     --  payroll action details
1940    l_assact_rec assact_details;   --  assignment action details
1941    l_cur_aseq   number;           --  Current action_sequence.
1942    l_counter    number;           --  counts number of actions processed
1943                                   --  within one chunk.
1944    c_indent     constant varchar2(30) := 'pyassact.do_pact_rollback';
1945 --
1946 begin
1947 
1948    --  populate payroll action details and perform high level validation
1949    l_pact_rec.pact_id := p_payroll_action_id;
1950    if g_debug then
1951       hr_utility.set_location(c_indent, 10);
1952    end if;
1953    val_pact_rollback(p_pact_rec => l_pact_rec,
1954                           p_rollback_mode => p_rollback_mode);
1955 --
1956    -- Rollback and Mark for retry rules
1957    val_pact_rr_rules(l_pact_rec, p_rollback_mode);
1958 --
1959    --  assume things will go well for us
1960    p_failed_assact := null;
1961 --
1962    -- If a process has failed disasterously, there could
1963    -- be range rows still hanging around. Need to trash these.
1964    delete from pay_population_ranges ppr
1965    where  ppr.payroll_action_id = p_payroll_action_id;
1966 --
1967    -- We need to roll back assignment actions in reverse order
1968    -- from high action_sequence down (for multiple assignments).
1969    -- Therefore, start by selecting the max action_sequence.
1970    if g_debug then
1971       hr_utility.set_location(c_indent, 20);
1972    end if;
1973    select max(act.action_sequence) + 1
1974    into   l_cur_aseq
1975    from   pay_assignment_actions act
1976    where  act.payroll_action_id = p_payroll_action_id;
1977 --
1978    --  now roll back each assignment action in turn (lock as we go)
1979    declare
1980       more_actions boolean;
1981 --
1982       -- This cursor retrieves all target assignment actions which
1983       -- have not already been considered for rolling back.
1984       -- It used to use rownum to restrict the fetch to a chunk of
1985       -- assignment actions. However, rownum and the ordering worked
1986       -- in opposite directions, leading to the code only processing
1987       -- the first chunk.
1988       cursor c1 is
1989       select act.assignment_action_id,
1990              act.action_sequence
1991       from   pay_assignment_actions act
1992       where  act.payroll_action_id = p_payroll_action_id
1993       and    act.action_sequence   < l_cur_aseq
1994       order by act.action_sequence desc
1995       for update of act.action_status;
1996    begin
1997 --
1998       --
1999       -- The outer loop handles chunks of assignment actions,
2000       -- We delete a chunk of assignment actions at a time,
2001       -- using supplied parameter value, we do not use the
2002       -- existing chunks.
2003       -- To keep commit unit size down, a local counter is incremented
2004       -- each time an assignment action is rolled back or marked for
2005       -- retry (as opposed to when the action is considered for
2006       -- processing, but rejected, and hence, no db change ensues).
2007       --
2008       more_actions := TRUE;  -- Set this to satisfy the while condition.
2009       while(more_actions) loop
2010          more_actions := FALSE;
2011          l_counter := 0;
2012 --
2013          if g_debug then
2014             hr_utility.set_location(c_indent,30);
2015          end if;
2016          for c1rec in c1 loop
2017             exit when l_counter = p_chunk_size;
2018             more_actions := TRUE;
2019 --
2020             -- Set this variable so that when we re-open cursor c1, for
2021             -- the next chunk, we don't reprocess the actions we've
2022             -- dealt with already.
2023             l_cur_aseq := c1rec.action_sequence;
2024 --
2025             --  see if OK to roll back or retry this assignment action
2026             l_assact_rec.assact_id := c1rec.assignment_action_id;
2027             if g_debug then
2028                hr_utility.set_location(c_indent,40);
2029             end if;
2030             if val_assact_rollback
2031                         (p_pact_rec => l_pact_rec,
2032                          p_assact_rec => l_assact_rec,
2033                          p_rollback_mode => p_rollback_mode) then
2034 --
2035                --  OK, clean up all child records for the action
2036                if g_debug then
2037                   hr_utility.set_location(c_indent,50);
2038                end if;
2039                do_assact_rollback
2040                          (p_pact_rec => l_pact_rec,
2041                           p_assact_rec => l_assact_rec,
2042                           p_rollback_mode => p_rollback_mode,
2043                           p_leave_base_table_row => FALSE);
2044                -- We've just processed another assignment action, so
2045                -- increment the counter.
2046                l_counter := l_counter + 1;
2047 --
2048             else
2049                -- We have detected that an assignment action
2050                -- should not be rolled back. We set a parameter
2051                -- value to tell the outside world.
2052                -- Only set if null, because several assignment
2053                -- actions could fail, and we wish to report on
2054                -- the first of these
2055                if p_failed_assact is null then
2056                   p_failed_assact := c1rec.assignment_action_id;
2057                end if;
2058 --
2059                --  decide whether to leap out entirely
2060                if p_all_or_nothing then
2061                   hr_utility.set_message(801, 'HR_7008_ACTION_CANT_ROLLBACK');
2062                   hr_utility.set_message_token
2063                     ('FAILING_ASSACT', to_char(c1rec.assignment_action_id));
2064                   hr_utility.raise_error;
2065                end if;
2066             end if;
2067 --
2068          end loop; -- cursor loop.
2069 --
2070          if not p_all_or_nothing then
2071             commit;
2072          end if;
2073 --
2074       end loop; -- loop round for next chunk.
2075 --
2076    end;
2077 --
2078    --  trash messages
2079    if g_debug then
2080       hr_utility.set_location(c_indent,60);
2081    end if;
2082    delete from pay_message_lines ML
2083    where  ML.source_type = 'P'
2084    and    ML.source_id = p_payroll_action_id;
2085 --
2086    --  now trash the payroll action itself if the user so desires (and if
2087    --  all assignment actions where successfully rolled back).
2088    if g_debug then
2089       hr_utility.set_location(c_indent,70);
2090    end if;
2091    if not p_leave_base_table_row and p_failed_assact is null then
2092 --
2093       --
2094       -- Now we need to have a message to indicate
2095       -- that the payroll action was rolled back.
2096       -- We start by checking the payroll action type.
2097       -- If that is Magnetic Transfer, we wish to
2098       -- select the actual payment type (thus giving
2099       -- us BACS or NACHA or whatever, before generating
2100       -- the message.
2101       if(l_pact_rec.action_type = 'M') then
2102          if g_debug then
2103             hr_utility.set_location(c_indent,90);
2104          end if;
2105          select ppt_tl.payment_type_name
2106          into   l_pact_rec.action_name
2107          from   pay_payroll_actions  pac,
2108                 pay_payment_types_tl ppt_tl,
2109                 pay_payment_types    ppt
2110          where  pac.payroll_action_id = l_pact_rec.pact_id
2111          and    ppt.payment_type_id = pac.payment_type_id
2112          and    ppt_tl.payment_type_id = ppt.payment_type_id
2113          and    userenv('LANG') = ppt_tl.language;
2114       end if;
2115 --
2116       -- We now need to set up the message, depending on whether
2117       -- the payroll_id is null or not.
2118       declare
2119          mesg_text pay_message_lines.line_text%type;
2120       begin
2121          if(l_pact_rec.payroll_name is null) then
2122             -- Set up message for no payroll case.
2123             hr_utility.set_message(801,'HR_ACTION_PACT_ROLLNOPAY');
2124             hr_utility.set_message_token('ACTION_TYPE',l_pact_rec.action_name);
2125             hr_utility.set_message_token('BG_NAME',l_pact_rec.bg_name);
2126             hr_utility.set_message_token('SYSDATE',
2127                  fnd_date.date_to_canonical(l_pact_rec.current_date));
2128          else
2129             -- Set up message for payroll case.
2130             hr_utility.set_message(801,'HR_ACTION_PACT_ROLLPAY');
2131             hr_utility.set_message_token('ACTION_TYPE',l_pact_rec.action_name);
2132             hr_utility.set_message_token('PAYROLL_NAME',
2133                  l_pact_rec.payroll_name);
2134             hr_utility.set_message_token('BG_NAME',l_pact_rec.bg_name);
2135             hr_utility.set_message_token('SYSDATE',
2136                  fnd_date.date_to_canonical(l_pact_rec.current_date));
2137          end if;
2138          mesg_text := substrb(hr_utility.get_message,1,240);
2139 --
2140          if g_debug then
2141             hr_utility.set_location(c_indent,100);
2142          end if;
2143          insert into pay_message_lines (
2144                 line_sequence,
2145                 payroll_id,
2146                 message_level,
2147                 source_id,
2148                 source_type,
2149                 line_text)
2150          select pay_message_lines_s.nextval,
2151                 pac.payroll_id,
2152                 'I',  -- information.
2153                 pac.business_group_id,
2154                 'B',
2155                 mesg_text
2156          from   pay_payroll_actions pac
2157          where  pac.payroll_action_id = l_pact_rec.pact_id;
2158 
2159       end;
2160 --
2161       if g_debug then
2162          hr_utility.set_location(c_indent,80);
2163       end if;
2164       delete from pay_payroll_actions
2165       where  payroll_action_id = p_payroll_action_id;
2166 --
2167       if not p_all_or_nothing then
2168          if g_debug then
2169             hr_utility.set_location(c_indent,110);
2170          end if;
2171          commit;
2172       end if;
2173 --
2174    end if;
2175 --
2176    if g_debug then
2177       hr_utility.set_location(c_indent,120);
2178    end if;
2179    return;
2180 --
2181 end do_pact_rollback;
2182 --
2183 --
2184 --
2185 /*-----------------------  rollback_payroll_action  ----------------------*/
2186 /*
2187  *   This routine rolls back an entire payroll action.
2188  *
2189  *   Three forms are available:
2190  *
2191  *   1) Rollback entire action without committing, fail if any individual
2192  *      assignment action couldn't be rolled back.
2193  *
2194  *   2) Rollback entire action without committing, continue if any
2195  *      assignment action couldn't be rolled back, setting
2196  *      p_failed_assact to indicate the problematic action.
2197  *
2198  *   3) Same as previous form, but commit in chunks as processing
2199  *      continues to avoid huge rollback segments.
2200  *
2201  *    p_rollback_mode must be either ROLLBACK or RETRY
2202  */
2203 --
2204 procedure rollback_payroll_action
2205                        (p_payroll_action_id    in number,
2206                         p_rollback_mode        in varchar2,
2207                         p_leave_base_table_row in boolean) is
2208 begin
2209    g_debug := hr_utility.debug_enabled;
2210 --
2211    if g_debug then
2212       hr_utility.set_location('hrassact.rollback_payroll_action',10);
2213    end if;
2214    py_rollback_pkg.rollback_payroll_action
2215         (p_payroll_action_id => p_payroll_action_id,
2216          p_rollback_mode => p_rollback_mode,
2217          p_leave_base_table_row => p_leave_base_table_row);
2218 end rollback_payroll_action;
2219 --
2220 --
2221 procedure rollback_payroll_action
2222                        (p_payroll_action_id    in number,
2223                         p_failed_assact        in out nocopy number,
2224                         p_rollback_mode        in varchar2,
2225                         p_leave_base_table_row in boolean) is
2226 begin
2227    g_debug := hr_utility.debug_enabled;
2228 --
2229    if g_debug then
2230       hr_utility.set_location('hrassact.rollback_payroll_action',20);
2231    end if;
2232    py_rollback_pkg.rollback_payroll_action
2233         (p_payroll_action_id => p_payroll_action_id,
2234          p_rollback_mode => p_rollback_mode,
2235          p_leave_base_table_row => p_leave_base_table_row);
2236 end rollback_payroll_action;
2237 --
2238 --
2239 procedure rollback_payroll_action
2240                        (p_payroll_action_id    in number,
2241                         p_chunk_size           in number,
2242                         p_failed_assact        in out nocopy number,
2243                         p_rollback_mode        in varchar2,
2244                         p_leave_base_table_row in boolean) is
2245 begin
2246    g_debug := hr_utility.debug_enabled;
2247 --
2248    if g_debug then
2249       hr_utility.set_location('hrassact.rollback_payroll_action',30);
2250    end if;
2251    py_rollback_pkg.rollback_payroll_action
2252         (p_payroll_action_id => p_payroll_action_id,
2253          p_all_or_nothing    => FALSE,
2254          p_rollback_mode => p_rollback_mode,
2255          p_leave_base_table_row => p_leave_base_table_row);
2256 end rollback_payroll_action;
2257 --
2258 --
2259 --
2260 --
2261 /*---------------------------  rollback_ass_action  -----------------------*/
2262 /*
2263  *   This routine performs the actual work of rolling back a
2264  *   assignment action.
2265  */
2266 procedure rollback_ass_action
2267               (p_assignment_action_id in number,
2268                p_rollback_mode        in varchar2,
2269                p_leave_base_table_row in boolean) is
2270 --
2271    l_pact_rec    pact_details;     --  payroll action details
2272    l_assact_rec  assact_details;   --  assignment action details
2273    cursor c1 is
2274    select payroll_action_id
2275    from   pay_assignment_actions
2276    where  assignment_action_id = p_assignment_action_id
2277    for update of action_status;
2278 --
2279 --
2280 begin
2281    g_debug := hr_utility.debug_enabled;
2282 --
2283    if g_debug then
2284       hr_utility.set_location('hrassact.rollback_ass_action',20);
2285    end if;
2286    for c1rec in c1 loop
2287 --
2288       --  populate payroll action details and perform high level validation
2289       l_pact_rec.pact_id := c1rec.payroll_action_id;
2290       if g_debug then
2291          hr_utility.set_location('hrassact.rollback_ass_action',30);
2292       end if;
2293       val_pact_rollback(p_pact_rec => l_pact_rec,
2294                              p_rollback_mode => p_rollback_mode);
2295 --
2296       --  see if OK to roll back or retry this assignment action
2297       l_assact_rec.assact_id := p_assignment_action_id;
2298 --
2299       if g_debug then
2300          hr_utility.set_location('hrassact.rollback_ass_action',40);
2301       end if;
2302       if  p_rollback_mode = 'BACKPAY' or
2303          (val_assact_rollback (l_pact_rec, l_assact_rec, p_rollback_mode) and
2304           val_assact_rr_rules (l_pact_rec, p_rollback_mode))
2305       then
2306 --
2307          --  OK, clean up all child records for the action
2308          if g_debug then
2309             hr_utility.set_location('hrassact.rollback_ass_action',50);
2310          end if;
2311          do_assact_rollback
2312                    (p_pact_rec => l_pact_rec,
2313                     p_assact_rec => l_assact_rec,
2314                     p_rollback_mode => p_rollback_mode,
2315                     p_leave_base_table_row => p_leave_base_table_row);
2316       else
2317          hr_utility.set_message(801, 'HR_7008_ACTION_CANT_ROLLBACK');
2318          hr_utility.set_message_token('FAILING_ASSACT',l_assact_rec.assact_id);
2319          hr_utility.raise_error;
2320       end if;
2321 --
2322    end loop;
2323 --
2324    if g_debug then
2325       hr_utility.set_location('hrassact.rollback_ass_action',60);
2326    end if;
2327    return;
2328 --
2329 end rollback_ass_action;
2330 --
2331 --
2332 /*-------------------------  trash_latest_balances  -----------------------*/
2333 /*
2334  *    This procedure trashes any latest balances
2335  *    invalidated for the given balance type and input value
2336  *    on or after the given date where there exists at least
2337  *    one processed, non zero result value. This is done to
2338  *    avoid trashing latest balances that could not have
2339  *    been affected by the change in the balance feed.
2340  */
2341 procedure trash_latest_balances(l_balance_type_id number,
2342                                 l_input_value_id number,
2343                                 l_trash_date date) is
2344 --
2345    -- Select all person latest balances to delete.
2346    cursor plbc is
2347    select /*+ ORDERED INDEX (PLB PAY_PERSON_LATEST_BALANCES_FK1)
2348               USE_NL (PLB) */
2349           plb.latest_balance_id
2350    from   pay_defined_balances       pdb,
2351           pay_person_latest_balances plb
2352    where  pdb.balance_type_id      = l_balance_type_id
2353    and    plb.defined_balance_id   = pdb.defined_balance_id
2354    and    exists (
2355           select null
2356           from   pay_run_results       prr,
2357                  pay_run_result_values rrv
2358           where  rrv.input_value_id  = l_input_value_id
2359           and    prr.run_result_id   = rrv.run_result_id
2360           and    prr.status          in ('P', 'PA')
2361           and    nvl(rrv.result_value, '0') <> '0');
2362 --
2363    cursor lbc is
2364    select
2365           lb.latest_balance_id
2366    from   pay_defined_balances       pdb,
2367           pay_latest_balances lb
2368    where  pdb.balance_type_id      = l_balance_type_id
2369    and    lb.defined_balance_id   = pdb.defined_balance_id
2370    and    exists (
2371           select null
2372           from   pay_run_results       prr,
2373                  pay_run_result_values rrv
2374           where  rrv.input_value_id  = l_input_value_id
2375           and    prr.run_result_id   = rrv.run_result_id
2376           and    prr.status          in ('P', 'PA')
2377           and    nvl(rrv.result_value, '0') <> '0');
2378 --
2379    -- Select all assignment latest balances to delete.
2380    cursor albc is
2381    select /*+ ORDERED INDEX (PLB PAY_ASSIGNMENT_LATEST_BALA_FK2)
2382               USE_NL (PLB) */
2383           plb.latest_balance_id
2384    from   pay_defined_balances           pdb,
2385           pay_assignment_latest_balances plb
2386    where  pdb.balance_type_id      = l_balance_type_id
2387    and    plb.defined_balance_id   = pdb.defined_balance_id
2388    and    exists (
2389           select null
2390           from   pay_run_results       prr,
2391                  pay_run_result_values rrv
2392           where  rrv.input_value_id  = l_input_value_id
2393           and    prr.run_result_id   = rrv.run_result_id
2394           and    prr.status          in ('P', 'PA')
2395           and    nvl(rrv.result_value, '0') <> '0');
2396 --
2397    cursor platbalc is
2398    select /*+ ORDERED INDEX (PLB PAY_PERSON_LATEST_BALANCES_FK1)
2399               USE_NL (PLB) */
2400           plb.latest_balance_id
2401    from   pay_defined_balances       pdb,
2402           pay_person_latest_balances plb
2403    where  pdb.balance_type_id      = l_balance_type_id
2404    and    plb.defined_balance_id   = pdb.defined_balance_id;
2405 --
2406    -- Select all assignment latest balances to delete.
2407    cursor alatbalc is
2408    select /*+ ORDERED INDEX (PLB PAY_ASSIGNMENT_LATEST_BALA_FK2)
2409               USE_NL (PLB) */
2410           plb.latest_balance_id
2411    from   pay_defined_balances           pdb,
2412           pay_assignment_latest_balances plb
2413    where  pdb.balance_type_id      = l_balance_type_id
2414    and    plb.defined_balance_id   = pdb.defined_balance_id;
2415 --
2416    -- Select all latest balances to delete.
2417    cursor latbalc is
2418    select /*+ ORDERED INDEX (PLB PAY_LATEST_BALANCES_FK1)
2419               USE_NL (PLB) */
2420           plb.latest_balance_id
2421    from   pay_defined_balances           pdb,
2422           pay_latest_balances            plb
2423    where  pdb.balance_type_id      = l_balance_type_id
2424    and    plb.defined_balance_id   = pdb.defined_balance_id;
2425 --
2426    -- Select if run result value exists for input value
2427    cursor ivchk is
2428    select '1' from dual
2429     where exists (select 1
2430      from pay_run_results prr,
2431           pay_run_result_values rrv
2432     where rrv.input_value_id = l_input_value_id
2433       and prr.run_result_id  = rrv.run_result_id
2434       and prr.status         in ('P', 'PA')
2435       and nvl(rrv.result_value, '0') <> '0');
2436 --
2437    -- Select the balances that are PL/SQL fed.
2438    cursor pl_feed_chk_a is
2439    select alb.latest_balance_id
2440      from pay_assignment_latest_balances alb,
2441           pay_defined_balances pdb,
2442           pay_balance_dimensions pbd
2443     where pdb.balance_type_id = l_balance_type_id
2444       and pdb.defined_balance_id = alb.defined_balance_id
2445       and pdb.balance_dimension_id = pbd.balance_dimension_id
2446       and pbd.feed_checking_type = 'F';
2447 
2448    cursor pl_feed_chk is
2449    select plb.latest_balance_id,
2450           'P' balance_type
2451      from pay_person_latest_balances plb,
2452           pay_defined_balances pdb,
2453           pay_balance_dimensions pbd
2454     where pdb.balance_type_id = l_balance_type_id
2455       and pdb.defined_balance_id = plb.defined_balance_id
2456       and pdb.balance_dimension_id = pbd.balance_dimension_id
2457       and pbd.feed_checking_type = 'F'
2458     union
2459     select plb.latest_balance_id,
2460            'B' balance_type
2461       from pay_latest_balances plb,
2462            pay_defined_balances pdb,
2463            pay_balance_dimensions pbd
2464      where pdb.balance_type_id = l_balance_type_id
2465        and pdb.defined_balance_id = plb.defined_balance_id
2466        and pdb.balance_dimension_id = pbd.balance_dimension_id
2467        and pbd.feed_checking_type = 'F';
2468 
2469   l_ivchk varchar2(2);
2470   l_rrv_found number := -1;
2471   --Added following type for Bug:6595092 bulk delete
2472   Type t_latbal is table of pay_assignment_latest_balances.latest_balance_id%type;
2473   lat_bal_list t_latbal;
2474 --
2475 begin
2476    g_debug := hr_utility.debug_enabled;
2477 --
2478    if g_debug then
2479       hr_utility.set_location('hrassact.trash_latest_balances',10);
2480    end if;
2481 
2482    if (g_lat_bal_check_mode is null) then
2483       begin
2484          if g_debug then
2485             hr_utility.set_location('hrassact.trash_latest_balances',15);
2486          end if;
2487          select parameter_value
2488          into   g_lat_bal_check_mode
2489          from   pay_action_parameters
2490          where  parameter_name = 'LAT_BAL_CHECK_MODE';
2491 
2492       exception
2493          when others then
2494             g_lat_bal_check_mode := 'N';
2495       end;
2496 
2497       if (g_lat_bal_check_mode = 'B') then
2498          HRASSACT.CHECK_LAT_BALS_FIRST := TRUE;
2499       elsif (g_lat_bal_check_mode = 'R') then
2500          HRASSACT.CHECK_RRVS_FIRST := TRUE;
2501       end if;
2502    end if;
2503 --
2504  if HRASSACT.CHECK_LATEST_BALANCES = TRUE then
2505 
2506   if HRASSACT.CHECK_RRVS_FIRST = TRUE then
2507 
2508    if g_debug then
2509       hr_utility.set_location('hrassact.trash_latest_balances',20);
2510    end if;
2511    --
2512    -- Check for existance of run result value for input value
2513    --
2514    open ivchk;
2515    fetch ivchk
2516    into l_ivchk;
2517 
2518    if ivchk%FOUND then
2519 --
2520      if g_debug then
2521         hr_utility.set_location('hrassact.trash_latest_balances',30);
2522      end if;
2523      -- Delete all balance context values and
2524      -- person latest balances.
2525      for plbcrec in platbalc loop
2526         delete from pay_balance_context_values BCV
2527         where  BCV.latest_balance_id = plbcrec.latest_balance_id;
2528 --
2529         delete from pay_person_latest_balances PLB
2530         where  PLB.latest_balance_id = plbcrec.latest_balance_id;
2531      end loop;
2532 
2533      if g_debug then
2534         hr_utility.set_location('hrassact.trash_latest_balances',40);
2535      end if;
2536      -- Delete all balance context values and
2537      -- assignment latest balances.
2538 
2539      --Commented the following and added a block with cusrsor and bulk delete
2540      --for Bug:6595092
2541   /*   for albcrec in alatbalc loop
2542        delete from pay_balance_context_values BCV
2543         where  BCV.latest_balance_id = albcrec.latest_balance_id;
2544 --
2545         delete from pay_assignment_latest_balances ALB
2546         where  ALB.latest_balance_id = albcrec.latest_balance_id;
2547      end loop; */
2548 
2549      open alatbalc;
2550       loop
2551         fetch alatbalc bulk collect into lat_bal_list limit 100000;
2552 
2553         forall i in 1..lat_bal_list.count
2554           delete from pay_balance_context_values BCV
2555           where  BCV.latest_balance_id = lat_bal_list(i);
2556 
2557         forall i in 1..lat_bal_list.count
2558          delete from pay_assignment_latest_balances ALB
2559          where  ALB.latest_balance_id =lat_bal_list(i);
2560 
2561         exit when alatbalc%notfound;
2562       end loop;
2563 
2564 IF alatbalc%ISOPEN
2565 THEN
2566    CLOSE alatbalc;
2567 END IF;
2568 
2569 --
2570      -- Delete all latest Balanaces.
2571      for lbcrec in latbalc loop
2572 --
2573         delete from pay_latest_balances LB
2574         where  LB.latest_balance_id = lbcrec.latest_balance_id;
2575      end loop;
2576 --
2577    end if;
2578    close ivchk;
2579 
2580   elsif HRASSACT.CHECK_LAT_BALS_FIRST = TRUE then
2581 
2582    if g_debug then
2583       hr_utility.set_location('hrassact.trash_latest_balances',50);
2584    end if;
2585    --
2586    -- Check for any latest balances before relevant run result value
2587    --
2588    for plbcrec in platbalc loop
2589       if l_rrv_found = -1 then
2590          open ivchk;
2591 
2592          fetch ivchk
2593          into l_ivchk;
2594 
2595          if ivchk%FOUND then
2596             l_rrv_found := 1;
2597          else
2598             l_rrv_found := 0;
2599             close ivchk;
2600             exit;
2601          end if;
2602          close ivchk;
2603       end if;
2604       if l_rrv_found = 1 then
2605          delete from pay_balance_context_values BCV
2606          where  BCV.latest_balance_id = plbcrec.latest_balance_id;
2607 --
2608          delete from pay_person_latest_balances PLB
2609          where  PLB.latest_balance_id = plbcrec.latest_balance_id;
2610       end if;
2611    end loop;
2612 --
2613   if g_debug then
2614       hr_utility.set_location('hrassact.trash_latest_balances',60);
2615    end if;
2616    -- Delete all balance context values and
2617    -- assignment latest balances.
2618    if l_rrv_found <> 0 then
2619          if l_rrv_found = -1 then
2620             open ivchk;
2621             fetch ivchk
2622             into l_ivchk;
2623 
2624             if ivchk%FOUND then
2625                l_rrv_found := 1;
2626             else
2627                l_rrv_found := 0;
2628                close ivchk;
2629             end if;
2630             close ivchk;
2631          end if;
2632          if l_rrv_found = 1 then
2633             open alatbalc;
2634 		loop
2635 			fetch alatbalc bulk collect into lat_bal_list limit 100000;
2636 
2637 			 forall i in 1..lat_bal_list.count
2638 			 delete from pay_balance_context_values BCV
2639 			 where  BCV.latest_balance_id = lat_bal_list(i);
2640 
2641 			 forall i in 1..lat_bal_list.count
2642 			 delete from pay_assignment_latest_balances ALB
2643 			 where  ALB.latest_balance_id =lat_bal_list(i);
2644 
2645 			 exit when alatbalc%notfound;
2646 		end loop;
2647 
2648 		IF alatbalc%ISOPEN
2649 		THEN
2650 		   CLOSE alatbalc;
2651 		END IF;
2652          end if;
2653    end if;
2654 --
2655    for lbcrec in latbalc loop
2656       if l_rrv_found = -1 then
2657          open ivchk;
2658 
2659          fetch ivchk
2660          into l_ivchk;
2661 
2662          if ivchk%FOUND then
2663             l_rrv_found := 1;
2664          else
2665             l_rrv_found := 0;
2666             close ivchk;
2667             exit;
2668          end if;
2669          close ivchk;
2670       end if;
2671       if l_rrv_found = 1 then
2672          delete from pay_latest_balances ALB
2673          where  ALB.latest_balance_id = lbcrec.latest_balance_id;
2674       end if;
2675    end loop;
2676 
2677   else
2678    --
2679    -- Original Code
2680    --
2681    if g_debug then
2682       hr_utility.set_location('hrassact.trash_latest_balances',70);
2683    end if;
2684    -- Delete all balance context values and
2685    -- person latest balances.
2686    for plbcrec in plbc loop
2687       delete from pay_balance_context_values BCV
2688       where  BCV.latest_balance_id = plbcrec.latest_balance_id;
2689 --
2690       delete from pay_person_latest_balances PLB
2691       where  PLB.latest_balance_id = plbcrec.latest_balance_id;
2692    end loop;
2693 --
2694    if g_debug then
2695       hr_utility.set_location('hrassact.trash_latest_balances',80);
2696    end if;
2697    -- Delete all balance context values and
2698    -- assignment latest balances.
2699 		open albc;
2700 		loop
2701 			fetch albc bulk collect into lat_bal_list limit 100000;
2702 
2703 			 forall i in 1..lat_bal_list.count
2704 			 delete from pay_balance_context_values BCV
2705 			 where  BCV.latest_balance_id = lat_bal_list(i);
2706 
2707 			 forall i in 1..lat_bal_list.count
2708 			 delete from pay_assignment_latest_balances ALB
2709 			 where  ALB.latest_balance_id =lat_bal_list(i);
2710 
2711 			 exit when albc%notfound;
2712 		end loop;
2713 
2714 		IF albc%ISOPEN
2715 		THEN
2716 		   CLOSE albc;
2717 		END IF;
2718 
2719 --
2720    if g_debug then
2721       hr_utility.set_location('hrassact.trash_latest_balances',70);
2722    end if;
2723 --
2724    for lbcrec in lbc loop
2725       delete from pay_latest_balances ALB
2726       where  ALB.latest_balance_id = lbcrec.latest_balance_id;
2727    end loop;
2728 --
2729   end if;
2730 --
2731    if g_debug then
2732       hr_utility.set_location('hrassact.trash_latest_balances',90);
2733    end if;
2734 --
2735    for plrec in pl_feed_chk loop
2736 --
2737      if g_debug then
2738         hr_utility.set_location('hrassact.trash_latest_balances',100);
2739      end if;
2740 
2741      delete from pay_balance_context_values BCV
2742       where  BCV.latest_balance_id = plrec.latest_balance_id;
2743 --
2744      if (plrec.balance_type = 'P') then
2745        delete from pay_person_latest_balances PLB
2746        where  PLB.latest_balance_id = plrec.latest_balance_id;
2747      else
2748        delete from pay_latest_balances PLB
2749        where  PLB.latest_balance_id = plrec.latest_balance_id;
2750      end if;
2751 --
2752    end loop;
2753 
2754 
2755 		open pl_feed_chk_a;
2756 		loop
2757 			fetch pl_feed_chk_a bulk collect into lat_bal_list limit 100000;
2758 
2759 			 forall i in 1..lat_bal_list.count
2760 			 delete from pay_balance_context_values BCV
2761 			 where  BCV.latest_balance_id = lat_bal_list(i);
2762 
2763 			 forall i in 1..lat_bal_list.count
2764 			 delete from pay_assignment_latest_balances ALB
2765 			 where  ALB.latest_balance_id =lat_bal_list(i);
2766 
2767 			 exit when pl_feed_chk_a%notfound;
2768 		end loop;
2769 
2770 		IF pl_feed_chk_a%ISOPEN
2771 		THEN
2772 		   CLOSE pl_feed_chk_a;
2773 		END IF;
2774 
2775 
2776 
2777 
2778 
2779 
2780 
2781 
2782 
2783 
2784 
2785 --
2786  end if;
2787 --
2788    if g_debug then
2789       hr_utility.set_location('hrassact.trash_latest_balances',110);
2790    end if;
2791 --
2792    return;
2793 --
2794 end trash_latest_balances;
2795 --
2796 --
2797 /*-------------------------  trash_latest_balances  -----------------------*/
2798 /*
2799  *    This procedure trashes any latest balances
2800  *    invalidated for the given balance type
2801  */
2802 procedure trash_latest_balances(l_balance_type_id number,
2803                                 l_trash_date date) is
2804    -- Select all person latest balances to delete.
2805    cursor plbc is
2806    select /*+ ORDERED INDEX (PLB PAY_PERSON_LATEST_BALANCES_FK1)
2807               USE_NL (PLB) */
2808           plb.latest_balance_id
2809    from   pay_defined_balances       pdb,
2810           pay_person_latest_balances plb
2811    where  pdb.balance_type_id      = l_balance_type_id
2812    and    plb.defined_balance_id   = pdb.defined_balance_id;
2813 --
2814    -- Select all assignment latest balances to delete.
2815    cursor albc is
2816    select /*+ ORDERED INDEX (PLB PAY_ASSIGNMENT_LATEST_BALA_FK2)
2817               USE_NL (PLB) */
2818           plb.latest_balance_id
2819    from   pay_defined_balances           pdb,
2820           pay_assignment_latest_balances plb
2821    where  pdb.balance_type_id      = l_balance_type_id
2822    and    plb.defined_balance_id   = pdb.defined_balance_id;
2823    -- Select all latest balances to delete.
2824    cursor lbc is
2825    select /*+ ORDERED INDEX (LB PAY_LATEST_BALANCES_FK1)
2826               USE_NL (LB) */
2827           lb.latest_balance_id
2828    from   pay_defined_balances           pdb,
2829           pay_latest_balances            lb
2830    where  pdb.balance_type_id      = l_balance_type_id
2831    and    lb.defined_balance_id    = pdb.defined_balance_id;
2832 
2833   Type t_latbal is table of pay_assignment_latest_balances.latest_balance_id%type;
2834   lat_bal_list t_latbal;
2835 
2836 begin
2837    hr_utility.set_location('hrassact.trash_latest_balances',10);
2838 --
2839    -- Delete all balance context values and
2840    -- person latest balances.
2841    for plbcrec in plbc loop
2842       delete from pay_balance_context_values BCV
2843       where  BCV.latest_balance_id = plbcrec.latest_balance_id;
2844 --
2845       delete from pay_person_latest_balances PLB
2846       where  PLB.latest_balance_id = plbcrec.latest_balance_id;
2847    end loop;
2848 --
2849    hr_utility.set_location('hrassact.trash_latest_balances',20);
2850    -- Delete all balance context values and
2851    -- assignment latest balances.
2852    open albc;
2853 		loop
2854 			fetch albc bulk collect into lat_bal_list limit 100000;
2855 
2856 			 forall i in 1..lat_bal_list.count
2857 			 delete from pay_balance_context_values BCV
2858 			 where  BCV.latest_balance_id = lat_bal_list(i);
2859 
2860 			 forall i in 1..lat_bal_list.count
2861 			 delete from pay_assignment_latest_balances ALB
2862 			 where  ALB.latest_balance_id =lat_bal_list(i);
2863 
2864 			 exit when albc%notfound;
2865 		end loop;
2866 
2867 		IF albc%ISOPEN
2868 		THEN
2869 		   CLOSE albc;
2870 		END IF;
2871 
2872 --
2873    hr_utility.set_location('hrassact.trash_latest_balances',30);
2874    for lbcrec in lbc loop
2875       delete from pay_latest_balances ALB
2876       where  ALB.latest_balance_id = lbcrec.latest_balance_id;
2877    end loop;
2878    hr_utility.set_location('hrassact.trash_latest_balances',40);
2879 --
2880    return;
2881 --
2882 end trash_latest_balances;
2883 --
2884 /*-------------------------  del_latest_balances  -----------------------*/
2885 /*
2886  *     This procedure trashes any latest balances for the person.
2887  */
2888 procedure del_latest_balances
2889 (
2890    p_assignment_id  in number,
2891    p_effective_date in date,    -- allow date effective join.
2892    p_element_entry  in number default null,
2893    p_element_type_id in number default null
2894 ) is
2895    --
2896    -- Cursors to delete all the latest balances for an assignment.
2897    --
2898    cursor plbc (p_person_id number) is
2899    select plb.latest_balance_id
2900      from pay_person_latest_balances plb
2901     where plb.person_id = p_person_id;
2902    --
2903    cursor lbc (p_person_id number) is
2904    select lb.latest_balance_id
2905      from pay_latest_balances lb
2906     where lb.person_id = p_person_id;
2907    --
2908    cursor albc (p_person_id number) is
2909    select /*+ ORDERED */ alb.latest_balance_id
2910    from   per_periods_of_service         pos,
2911           per_all_assignments_f          asg,
2912           pay_assignment_actions         act,
2913           pay_payroll_actions            pac,
2914           pay_assignment_latest_balances alb
2915    where  pos.person_id            = p_person_id
2916    and    asg.period_of_service_id = pos.period_of_service_id
2917    and    asg.person_id            = p_person_id
2918    and    act.assignment_id        = asg.assignment_id
2919    and    alb.assignment_action_id = act.assignment_action_id
2920    and    pac.payroll_action_id    = act.payroll_action_id
2921    and    pac.effective_date between
2922           asg.effective_start_date and asg.effective_end_date;
2923 
2924 
2925    --
2926    -- Cursors to selectively delete the latest balances for an assignment.
2927    --
2928    cursor plbc_selective (p_person_id      number,
2929                           p_eletyp_id      number
2930                           ) is
2931    select
2932           plb.latest_balance_id
2933      from
2934           pay_input_values_f         piv,
2935           pay_balance_feeds_f        pbf,
2936           pay_defined_balances       pdb,
2937           pay_person_latest_balances plb,
2938           pay_assignment_actions     paa,
2939           pay_payroll_actions        ppa
2940     where plb.person_id = p_person_id
2941     and   piv.element_type_id = p_eletyp_id
2942     and   piv.input_value_id = pbf.input_value_id
2943     and   plb.defined_balance_id = pdb.defined_balance_id
2944     and   pdb.balance_type_id = pbf.balance_type_id
2945     and   paa.assignment_action_id = plb.assignment_action_id
2946     and   paa.payroll_action_id = ppa.payroll_action_id
2947     and   ppa.effective_date between pbf.effective_start_date
2948                                and pbf.effective_end_date
2949     and   ppa.effective_date between piv.effective_start_date
2950                                and piv.effective_end_date;
2951 --
2952    cursor albc_selective (p_person_id number,
2953                           p_eletyp_id number) is
2954    select /*+ ORDERED */
2955           alb.latest_balance_id
2956    from   pay_assignment_latest_balances alb,
2957           pay_assignment_actions         act,
2958           pay_payroll_actions            pac,
2959           pay_defined_balances           pdb,
2960           pay_balance_feeds_f            pbf,
2961           pay_input_values_f             piv
2962    where  act.assignment_id        = alb.assignment_id
2963    and    alb.assignment_id in
2964           (select distinct asg.assignment_id
2965            from per_all_assignments_f asg
2966            where asg.person_id     = p_person_id)
2967    and    alb.assignment_action_id = act.assignment_action_id
2968    and    piv.element_type_id      = p_eletyp_id
2969    and    piv.input_value_id       = pbf.input_value_id
2970    and    alb.defined_balance_id   = pdb.defined_balance_id
2971    and    pdb.balance_type_id      = pbf.balance_type_id
2972    and    pac.payroll_action_id    = act.payroll_action_id
2973    and    pac.effective_date between pbf.effective_start_date
2974                                  and pbf.effective_end_date
2975    and    pac.effective_date between piv.effective_start_date
2976                                  and piv.effective_end_date;
2977 --
2978    cursor lbc_selective (p_person_id      number,
2979                          p_eletyp_id      number
2980                           ) is
2981    select
2982           plb.latest_balance_id
2983      from
2984           pay_input_values_f         piv,
2985           pay_balance_feeds_f        pbf,
2986           pay_defined_balances       pdb,
2987           pay_latest_balances plb,
2988           pay_assignment_actions     paa,
2989           pay_payroll_actions        ppa
2990     where plb.person_id = p_person_id
2991     and   piv.element_type_id = p_eletyp_id
2992     and   piv.input_value_id = pbf.input_value_id
2993     and   plb.defined_balance_id = pdb.defined_balance_id
2994     and   pdb.balance_type_id = pbf.balance_type_id
2995     and   paa.assignment_action_id = plb.assignment_action_id
2996     and   paa.payroll_action_id = ppa.payroll_action_id
2997     and   ppa.effective_date between pbf.effective_start_date
2998                                and pbf.effective_end_date
2999     and   ppa.effective_date between piv.effective_start_date
3000                                and piv.effective_end_date;
3001 --
3002    l_person_id number;
3003    l_element_type_id number;
3004 begin
3005    g_debug := hr_utility.debug_enabled;
3006 --
3007    -- Simply return the person_id for the assignment.
3008    if g_debug then
3009       hr_utility.set_location('hrassact.del_latest_balances',10);
3010    end if;
3011    select asg.person_id
3012    into   l_person_id
3013    from   per_all_assignments_f asg
3014    where  asg.assignment_id = p_assignment_id
3015    and    p_effective_date between
3016           asg.effective_start_date and asg.effective_end_date;
3017 --
3018    if (p_element_entry is null) and (p_element_type_id is null) then
3019       -- Delete (person) balance context values.
3020       if g_debug then
3021          hr_utility.set_location('hrassact.del_latest_balances',20);
3022       end if;
3023       for plbcrec in plbc(l_person_id) loop
3024          delete from pay_balance_context_values BCV
3025          where  BCV.latest_balance_id = plbcrec.latest_balance_id;
3026 --
3027          delete from pay_person_latest_balances PLB
3028          where  PLB.latest_balance_id = plbcrec.latest_balance_id;
3029       end loop;
3030 --
3031       -- We need to delete all latest balances for the
3032       -- person's period of service.
3033       if g_debug then
3034          hr_utility.set_location('hrassact.del_latest_balances',40);
3035       end if;
3036       for albcrec in albc(l_person_id) loop
3037          delete from pay_balance_context_values BCV
3038          where  BCV.latest_balance_id = albcrec.latest_balance_id;
3039 --
3040          delete from pay_assignment_latest_balances ALB
3041          where  ALB.latest_balance_id = albcrec.latest_balance_id;
3042       end loop;
3043 --
3044       if g_debug then
3045          hr_utility.set_location('hrassact.del_latest_balances',45);
3046       end if;
3047       for lbcrec in lbc(l_person_id) loop
3048          delete from pay_latest_balances ALB
3049          where  ALB.latest_balance_id = lbcrec.latest_balance_id;
3050       end loop;
3051    else
3052 --
3053       l_element_type_id := p_element_type_id;
3054       --
3055       -- Check if the element type id is specified.
3056       --
3057       if l_element_type_id is null then
3058         --
3059         -- Derive the element type id from the entry id.
3060         --
3061         if g_debug then
3062            hr_utility.set_location('hrassact.del_latest_balances',60);
3063         end if;
3064         select pel.element_type_id into l_element_type_id
3065         from pay_element_entries_f pee
3066             ,pay_element_links_f   pel
3067         where
3068             pee.element_entry_id = p_element_entry
3069         and p_effective_date between pee.effective_start_date
3070                                  and pee.effective_end_date
3071         and pel.element_link_id = pee.element_link_id
3072         and p_effective_date between pel.effective_start_date
3073                                  and pel.effective_end_date
3074         ;
3075       end if;
3076       -- Delete (person) balance context values.
3077       if g_debug then
3078          hr_utility.set_location('hrassact.del_latest_balances',70);
3079       end if;
3080       for plbcrec in plbc_selective(l_person_id, l_element_type_id) loop
3081          delete from pay_balance_context_values BCV
3082          where  BCV.latest_balance_id = plbcrec.latest_balance_id;
3083 --
3084          delete from pay_person_latest_balances PLB
3085          where  PLB.latest_balance_id = plbcrec.latest_balance_id;
3086       end loop;
3087 --
3088       -- Delete contexts and latest balances for assignment.
3089       -- We need to delete all latest balances for the
3090       -- person's period of service.
3091       if g_debug then
3092          hr_utility.set_location('hrassact.del_latest_balances',80);
3093       end if;
3094       for albcrec in albc_selective(l_person_id, l_element_type_id) loop
3095          delete from pay_balance_context_values BCV
3096          where  BCV.latest_balance_id = albcrec.latest_balance_id;
3097 --
3098          delete from pay_assignment_latest_balances ALB
3099          where  ALB.latest_balance_id = albcrec.latest_balance_id;
3100       end loop;
3101 --
3102       if g_debug then
3103          hr_utility.set_location('hrassact.del_latest_balances',85);
3104       end if;
3105       for lbcrec in lbc_selective(l_person_id, l_element_type_id) loop
3106          delete from pay_latest_balances ALB
3107          where  ALB.latest_balance_id = lbcrec.latest_balance_id;
3108       end loop;
3109    end if;
3110 --
3111 end del_latest_balances;
3112 --
3113       --------------------------- update_action_sequence ------------------------------
3114       /*
3115          NAME
3116             update_action_sequence
3117          DESCRIPTION
3118             Update the action sequence of a particular action.
3119          NOTES
3120       */
3121       procedure update_action_sequence (p_assact in number,
3122                                         rmode    in varchar2)
3123       is
3124          pact number;
3125          actseq number;
3126          asgid  number;
3127          newseq number;
3128       begin
3129         select payroll_action_id,
3130                action_sequence,
3131                assignment_id,
3132                pay_assignment_actions_s.nextval
3133           into pact,
3134                actseq,
3135                asgid,
3136                newseq
3137           from pay_assignment_actions
3138          where assignment_action_id = p_assact;
3139 --
3140         resequence_actions(pact, asgid, actseq, rmode);
3141 --
3142         update pay_assignment_actions
3143            set action_sequence = newseq
3144          where assignment_action_id = p_assact;
3145 --
3146         update pay_run_balances
3147            set action_sequence = newseq
3148          where assignment_action_id = p_assact;
3149 --
3150       end update_action_sequence;
3151 --
3152       --------------------------- resequence_chunk ----------------------
3153       /*
3154          NAME
3155             resequence_chunk
3156          DESCRIPTION
3157             Resequence sequenced actions for a whole chunk of assignments.
3158          NOTES
3159       */
3160       procedure resequence_chunk
3161       (
3162          pactid    in number,
3163          cnkno     in number,
3164          rmode    in varchar2, -- rule_mode (time period independent Y or N)
3165          chldact  in varchar2 default 'N' -- update child actions (Y or N)
3166       ) is
3167       --
3168         cursor cnkasg is
3169         select ppa.effective_date,
3170                paa.assignment_action_id,
3171                ppa.action_type
3172           from pay_payroll_actions ppa,
3173                pay_assignment_actions paa
3174          where ppa.payroll_action_id = pactid
3175            and paa.payroll_action_id = ppa.payroll_action_id
3176            and paa.source_action_id is null
3177            and paa.chunk_number = cnkno;
3178       --
3179       begin
3180       --
3181          for asgrec in cnkasg loop
3182             resequence_actions(
3183                                asgrec.assignment_action_id,
3184                                rmode,
3185                                chldact,
3186                                asgrec.action_type);
3187          end loop;
3188       --
3189       end resequence_chunk;
3190 --
3191       --------------------------- resequence_actions ---------------------------
3192 ---
3193       /*
3194          NAME
3195             resequence_actions
3196          DESCRIPTION
3197             Resequence sequenced actions.
3198          NOTES
3199       */
3200       procedure resequence_actions
3201       (
3202          aaid      in number,
3203          rmode     in varchar2, -- rule_mode (time period independent Y or N)
3204          chldact   in varchar2 default 'N', -- update child actions (Y or N)
3205          actype    in varchar2  -- action_type
3206       ) is
3207 --
3208          --
3209          cursor seqasg (
3210                         aaid  number,
3211                         chldact varchar2
3212                         ) is
3213          select ac2.rowid,
3214                 ac2.assignment_action_id
3215          from   pay_action_classifications acl,
3216                 pay_assignment_actions     ac2,
3217                 pay_assignment_actions     paa,
3218                 pay_payroll_actions        ppa,
3219                 pay_payroll_actions        pa2
3220          where paa.assignment_action_id = aaid
3221          and   ppa.payroll_action_id = paa.payroll_action_id
3222          and    ac2.assignment_id       = paa.assignment_id
3223          and    pa2.payroll_action_id   = ac2.payroll_action_id
3224          and    acl.classification_name = 'SEQUENCED'
3225          and    pa2.effective_date      > ppa.effective_date
3226          and    pa2.action_type         = acl.action_type
3227          and    ((     chldact = 'N'
3228                   and  ac2.source_action_id is null)
3229                  or
3230                   (    chldact = 'Y')
3231                 )
3232          order by pa2.effective_date, ac2.action_sequence
3233          for update of ac2.assignment_action_id;
3234    --
3235          cursor seqper (
3236                         aaid  number,
3237                         chldact varchar2
3238                         ) is
3239          select ac2.rowid,
3240                 ac2.assignment_action_id
3241          from   pay_action_classifications acl,
3242                 pay_assignment_actions     ac2,
3243                 pay_assignment_actions     act,
3244                 per_all_assignments_f          as2,
3245                 per_all_assignments_f          asg,
3246                 pay_payroll_actions        pa2,
3247                 pay_payroll_actions        pac
3248          where act.assignment_action_id = aaid
3249          and   pac.payroll_action_id    = act.payroll_action_id
3250          and    asg.assignment_id        = act.assignment_id
3251          and    pac.effective_date between
3252                 asg.effective_start_date and asg.effective_end_date
3253          and    as2.person_id            = asg.person_id
3254          and    as2.effective_end_date   > pac.effective_date
3255          and    ac2.assignment_id        = as2.assignment_id
3256          and    pa2.payroll_action_id    = ac2.payroll_action_id
3257          and    as2.effective_start_date = (select max(paf3.effective_start_date)
3258                                               from per_all_assignments_f paf3
3259                                              where paf3.assignment_id = ac2.assignment_id
3260                                                and paf3.effective_start_date <= pa2.effective_date
3261                                            )
3262          and    acl.classification_name  = 'SEQUENCED'
3263          and    pa2.action_type          = acl.action_type
3264          and    pa2.effective_date       > pac.effective_date
3265          and    ((     chldact = 'N'
3266                   and  ac2.source_action_id is null)
3267                  or
3268                   (    chldact = 'Y')
3269                 )
3270          order by pa2.effective_date, ac2.action_sequence
3271          for update of ac2.assignment_action_id;
3272 --
3273 --       G when aaid is not a retropay (ie act.assignment is not null)
3274 --
3275          cursor seqgrp (
3276                         aaid  number,
3277                         chldact varchar2
3278                         ) is
3279          select ac2.rowid,
3280                 ac2.assignment_action_id
3281          from
3282              (
3283          select distinct ac3.assignment_action_id
3284          from   pay_assignment_actions     act,
3285                 pay_object_groups          pog,
3286                 pay_object_groups          pog2,
3287                 pay_object_groups          pog3,
3288                 pay_payroll_actions        pac,
3289                 pay_object_groups          pog4,
3290                 pay_assignment_actions     ac3,
3291                 pay_payroll_actions        pa3,
3292                 pay_action_classifications acl
3293          where  act.assignment_action_id = aaid
3294          and    pac.payroll_action_id    = act.payroll_action_id
3295          and    pog.source_id            = act.assignment_id
3296          and    pog.source_type          = 'PAF'
3297          and    pog2.object_group_id     = pog.parent_object_group_id
3298          and    pog2.source_type         = 'PPF'
3299          and    pog3.source_id           = pog2.source_id
3300          and    pog3.source_type         = 'PPF'
3301          and    pog4.parent_object_group_id = pog3.object_group_id
3302          and    ac3.assignment_id     = pog4.source_id
3303          and    pa3.payroll_action_id = ac3.payroll_action_id
3304          and    pa3.effective_date    > pac.effective_date
3305          and    pa3.action_type      <> 'L'
3306          and    pa3.action_type       = acl.action_type
3307          and    acl.classification_name = 'SEQUENCED'
3308                 union all
3309          select distinct ac3.assignment_action_id
3310          from   pay_assignment_actions     act,
3311                 pay_object_groups          pog,
3312                 pay_object_groups          pog2,
3313                 pay_object_groups          pog3,
3314                 pay_payroll_actions        pac,
3315                 pay_assignment_actions     ac3,
3316                 pay_payroll_actions        pa3
3317          where  act.assignment_action_id = aaid
3318          and    pac.payroll_action_id    = act.payroll_action_id
3319          and    pog.source_id            = act.assignment_id
3320          and    pog.source_type          = 'PAF'
3321          and    pog2.object_group_id     = pog.parent_object_group_id
3322          and    pog2.source_type         = 'PPF'
3323          and    pog3.source_id           = pog2.source_id
3324          and    pog3.source_type         = 'PPF'
3325          and    ac3.object_id         = pog3.object_group_id
3326          and    ac3.object_type       = 'POG'
3327          and    pa3.payroll_action_id = ac3.payroll_action_id
3328          and    pa3.effective_date    > pac.effective_date
3329          and    pa3.action_type       = 'L') V,
3330                   pay_assignment_actions     ac2,
3331                   pay_payroll_actions        pa2
3332          where ac2.assignment_action_id = V.assignment_action_id
3333          and    pa2.payroll_action_id    = ac2.payroll_action_id
3334          and    ((     chldact = 'N'
3335                   and  ac2.source_action_id is null)
3336                  or
3337                   (    chldact = 'Y')
3338                 )
3339          order by pa2.effective_date, ac2.action_sequence
3340          for update of ac2.assignment_action_id;
3341 --
3342 --       G when aaid is a retropay (ie act.assignment is null)
3343 --
3344          cursor seqgrpret (
3345                         aaid  number,
3346                         chldact varchar2
3347                         ) is
3348          select ac2.rowid,
3349                 ac2.assignment_action_id
3350          from
3351              (
3352          select distinct ac3.assignment_action_id
3353          from   pay_assignment_actions     act,
3354                 pay_object_groups          pog,
3355                 pay_object_groups          pog2,
3356                 pay_payroll_actions        pac,
3357                 pay_object_groups          pog3,
3358                 pay_assignment_actions     ac3,
3359                 pay_payroll_actions        pa3,
3360                 pay_action_classifications acl
3361          where  act.assignment_action_id = aaid
3362          and    pac.payroll_action_id    = act.payroll_action_id
3363          and    pog.object_group_id      = act.object_id
3364          and    pog.source_type          = 'PPF'
3365          and    pog2.source_id           = pog.source_id
3366          and    pog2.source_type         = 'PPF'
3367          and    pog3.parent_object_group_id = pog2.object_group_id
3368          and    ac3.assignment_id     = pog3.source_id
3369          and    pa3.payroll_action_id = ac3.payroll_action_id
3370          and    pa3.effective_date    > pac.effective_date
3371          and    pa3.action_type      <> 'L'
3372          and    pa3.action_type       = acl.action_type
3373          and    acl.classification_name = 'SEQUENCED'
3374                 union all
3375          select distinct ac3.assignment_action_id
3376          from   pay_assignment_actions     act,
3377                 pay_object_groups          pog,
3378                 pay_object_groups          pog2,
3379                 pay_payroll_actions        pac,
3380                 pay_assignment_actions     ac3,
3381                 pay_payroll_actions        pa3
3382          where  act.assignment_action_id = aaid
3383          and    pac.payroll_action_id    = act.payroll_action_id
3384          and    pog.object_group_id      = act.object_id
3385          and    pog.source_type          = 'PPF'
3386          and    pog2.source_id           = pog.source_id
3387          and    pog2.source_type         = 'PPF'
3388          and    ac3.object_id         = pog2.object_group_id
3389          and    ac3.object_type       = 'POG'
3390          and    pa3.payroll_action_id = ac3.payroll_action_id
3391          and    pa3.effective_date    > pac.effective_date
3392          and    pa3.action_type       = 'L') V,
3393                   pay_assignment_actions     ac2,
3394                   pay_payroll_actions        pa2
3395          where ac2.assignment_action_id = V.assignment_action_id
3396          and    pa2.payroll_action_id    = ac2.payroll_action_id
3397          and    ((     chldact = 'N'
3398                   and  ac2.source_action_id is null)
3399                  or
3400                   (    chldact = 'Y')
3401                 )
3402          order by pa2.effective_date, ac2.action_sequence
3403          for update of ac2.assignment_action_id;
3404 --
3405          my_rowid rowid;
3406          upd_aa_id pay_assignment_actions.assignment_action_id%type;
3407 --
3408       begin
3409 --
3410          g_debug := hr_utility.debug_enabled;
3411 --
3412          if g_debug then
3413             hr_utility.set_location('hrassact.resequence_actions', 10);
3414          end if;
3415          if(rmode = 'Y') then
3416             open seqasg(aaid, chldact);
3417          elsif (rmode = 'N') then
3418             open seqper(aaid, chldact);
3419          else
3420             -- G mode
3421             if (actype = 'L') then
3422                open seqgrpret(aaid, chldact);
3423             else
3424                open seqgrp(aaid, chldact);
3425             end if;
3426          end if;
3427 --
3428          loop
3429             -- fetch the rowid and then update.
3430             if(rmode = 'Y') then
3431                fetch seqasg into my_rowid, upd_aa_id;
3432                exit when seqasg%notfound;
3433             elsif (rmode = 'N') then
3434                fetch seqper into my_rowid, upd_aa_id;
3435                exit when seqper%notfound;
3436             else
3437                -- G mode
3438                if (actype = 'L') then
3439                   fetch seqgrpret into my_rowid, upd_aa_id;
3440                   exit when seqgrpret%notfound;
3441                else
3442                   fetch seqgrp into my_rowid, upd_aa_id;
3443                   exit when seqgrp%notfound;
3444                end if;
3445             end if;
3446 --
3447             --
3448             -- Now, update with new action_sequence.
3449             update pay_assignment_actions act
3450             set    act.action_sequence = pay_assignment_actions_s.nextval
3451             where  act.rowid           = my_rowid;
3452 --
3453             update pay_run_balances
3454                set action_sequence = pay_assignment_actions_s.currval
3455              where assignment_action_id = upd_aa_id;
3456 --
3457          end loop;
3458 --
3459          --
3460          -- we are finished with the cursor, so we close it here.
3461          if(rmode = 'Y') then
3462             close seqasg;
3463          elsif(rmode = 'N') then
3464             close seqper;
3465          else
3466             -- G mode
3467             if (actype = 'L') then
3468                close seqgrpret;
3469             else
3470                close seqgrp;
3471             end if;
3472          end if;
3473 --
3474          if g_debug then
3475             hr_utility.set_location('hrassact.resequence_actions', 20);
3476          end if;
3477       end resequence_actions;
3478 --
3479       procedure resequence_children(p_asg_action in number)
3480       is
3481 --
3482          cursor get_chld (p_aa_id number)
3483          is
3484          select assignment_action_id
3485            from pay_assignment_actions
3486           where source_action_id = p_aa_id
3487           order by action_sequence;
3488 --
3489       begin
3490 --
3491          for chdrec in get_chld(p_asg_action) loop
3492 --
3493            resequence_children(chdrec.assignment_action_id);
3494 --
3495            update pay_assignment_actions
3496               set action_sequence = pay_assignment_actions_s.nextval
3497             where assignment_action_id = chdrec.assignment_action_id;
3498 --
3499          end loop;
3500 --
3501       end resequence_children;
3502 --
3503       procedure resequence_actions
3504       (
3505          pactid    in number,
3506          asgid    in number,
3507          actseq    in number,
3508          rmode    in varchar2  -- rule_mode (time period independent Y or N)
3509       ) is
3510 --
3511          --
3512          cursor seqasg (
3513                         asgid  number,
3514                         actseq number) is
3515          select /*+ INDEX (pa2 PAY_PAYROLL_ACTIONS_PK) */
3516                 ac2.rowid,
3517                 ac2.assignment_action_id,
3518                 ac2.source_action_id,
3519                 ac2.object_type
3520          from   pay_action_classifications acl,
3521                 pay_assignment_actions     ac2,
3522                 pay_payroll_actions        pa2
3523          where  ac2.assignment_id       = asgid
3524          and    pa2.payroll_action_id   = ac2.payroll_action_id
3525          and    acl.classification_name = 'SEQUENCED'
3526          and    pa2.action_type         = acl.action_type
3527          and    ac2.action_sequence > actseq
3528          and    (ac2.source_action_id is null
3529                  or
3530                    (ac2.object_id is not null
3531                     and ac2.object_type = 'POG')
3532                 )
3533          order by pa2.effective_date, ac2.action_sequence
3534          for update of ac2.assignment_action_id;
3535    --
3536          cursor seqper (pactid number,
3537                         asgid  number,
3538                         actseq number,
3539                         rmode  varchar2) is
3540          select /*+ ORDERED
3541                     INDEX (ac2 PAY_ASSIGNMENT_ACTIONS_N51)
3542                           (pa2 PAY_PAYROLL_ACTIONS_PK) */
3543                 ac2.rowid,
3544                 ac2.assignment_action_id,
3545                 ac2.source_action_id,
3546                 ac2.object_type
3547          from   pay_payroll_actions        pac,
3548                 per_all_assignments_f          asg,
3549                 per_all_assignments_f          as2,
3550                 pay_assignment_actions     act,
3551                 pay_assignment_actions     ac2,
3552                 pay_payroll_actions        pa2,
3553                 pay_action_classifications acl
3554          where  pac.payroll_action_id    = pactid
3555          and    act.payroll_action_id    = pac.payroll_action_id
3556          and    act.assignment_id        = asgid
3557          and    act.source_action_id is null
3558          and    asg.assignment_id        = act.assignment_id
3559          and    pac.effective_date between
3560                 asg.effective_start_date and asg.effective_end_date
3561          and    as2.person_id            = asg.person_id
3562          and    decode(rmode, 'G', pac.effective_date,
3563                                    as2.effective_end_date) >= pac.effective_date
3564          and    ac2.assignment_id        = as2.assignment_id
3565          and    pa2.payroll_action_id    = ac2.payroll_action_id
3566          and    as2.effective_start_date = (select max(paf3.effective_start_date)
3567                                               from per_all_assignments_f paf3
3568                                              where paf3.assignment_id = ac2.assignment_id
3569                                            )
3570          and    acl.classification_name  = 'SEQUENCED'
3571          and    pa2.action_type          = acl.action_type
3572          and    ac2.action_sequence > actseq
3573          and    (ac2.source_action_id is null
3574                  or
3575                    (ac2.object_id is not null
3576                     and ac2.object_type = 'POG')
3577                 )
3578          order by pa2.effective_date, ac2.action_sequence
3579          for update of ac2.assignment_action_id;
3580   --
3581   --     G nb pactid is never a retropay
3582   --
3583          cursor seqgrp (pactid number,
3584                         asgid  number,
3585                         actseq number,
3586                         rmode  varchar2) is
3587          select /*+ ORDERED
3588                     INDEX (ac2 PAY_ASSIGNMENT_ACTIONS_PK)
3589                           (pa2 PAY_PAYROLL_ACTIONS_PK) */
3590                 ac2.rowid,
3591                 ac2.assignment_action_id,
3592                 ac2.source_action_id,
3593                 ac2.object_type
3594         from
3595             (
3596          select /*+ ORDERED */
3597                 distinct ac3.assignment_action_id
3598          from   pay_payroll_actions        pac,
3599                 pay_assignment_actions     act,
3600                 pay_object_groups          pog,
3601                 pay_object_groups          pog2,
3602                 pay_object_groups          pog3,
3603                 pay_object_groups          pog4,
3604                 pay_assignment_actions     ac3,
3605                 pay_payroll_actions        pa3,
3606                 pay_action_classifications acl
3607          where  pac.payroll_action_id    = pactid
3608          and    act.payroll_action_id    = pac.payroll_action_id
3609          and    act.assignment_id        = asgid
3610          and    act.source_action_id is null
3611          and    pog.source_id            = act.assignment_id
3612          and    pog.source_type          = 'PAF'
3613          and    pac.effective_date between
3614                 pog.start_date and pog.end_date
3615          and    pog2.object_group_id     = pog.parent_object_group_id
3616          and    pog2.source_type         = 'PPF'
3617          and    pog3.source_id           = pog2.source_id
3618          and    pog3.source_type         = 'PPF'
3619          and    pog4.parent_object_group_id = pog3.object_group_id
3620          and    ac3.assignment_id     = pog4.source_id
3621          and    ac3.action_sequence   > actseq
3622          and    pa3.payroll_action_id = ac3.payroll_action_id
3623          and    pa3.action_type      <> 'L'
3624          and    pa3.action_type       = acl.action_type
3625          and    acl.classification_name = 'SEQUENCED'
3626                 union all
3627          select /*+ ORDERED */
3628                 distinct ac3.assignment_action_id
3629          from   pay_payroll_actions        pac,
3630                 pay_assignment_actions     act,
3631                 pay_object_groups          pog,
3632                 pay_object_groups          pog2,
3633                 pay_object_groups          pog3,
3634                 pay_assignment_actions     ac3,
3635                 pay_payroll_actions        pa3
3636          where  pac.payroll_action_id    = pactid
3637          and    act.payroll_action_id    = pac.payroll_action_id
3638          and    act.assignment_id        = asgid
3639          and    act.source_action_id is null
3640          and    pog.source_id            = act.assignment_id
3641          and    pog.source_type          = 'PAF'
3642          and    pac.effective_date between
3643                 pog.start_date and pog.end_date
3644          and    pog2.object_group_id     = pog.parent_object_group_id
3645          and    pog2.source_type         = 'PPF'
3646          and    pog3.source_id           = pog2.source_id
3647          and    pog3.source_type         = 'PPF'
3648          and    ac3.object_id         = pog3.object_group_id
3649          and    ac3.object_type       = 'POG'
3650          and    ac3.action_sequence   > actseq
3651          and    pa3.payroll_action_id = ac3.payroll_action_id
3652          and    pa3.action_type       = 'L') V,
3653                   pay_assignment_actions     ac2,
3654                   pay_payroll_actions        pa2
3655          where ac2.assignment_action_id = V.assignment_action_id
3656          and    pa2.payroll_action_id    = ac2.payroll_action_id
3657          and    (ac2.source_action_id is null
3658                  or
3659                    (ac2.object_id is not null
3660                     and ac2.object_type = 'POG')
3661                 )
3662          order by pa2.effective_date, ac2.action_sequence
3663          for update of ac2.assignment_action_id;
3664 
3665 --
3666          my_rowid rowid;
3667          upd_aa_id pay_assignment_actions.assignment_action_id%type;
3668          src_aa_id pay_assignment_actions.source_action_id%type;
3669          obj_type pay_assignment_actions.object_type%type;
3670 --
3671       begin
3672 --
3673          g_debug := hr_utility.debug_enabled;
3674 --
3675 hr_utility.trace('passing pact = '||pactid);
3676 hr_utility.trace('passing asgid = '||asgid);
3677 hr_utility.trace('passing actseq = '||actseq);
3678 --
3679          if g_debug then
3680             hr_utility.set_location('hrassact.resequence_actions', 10);
3681          end if;
3682          if(rmode = 'Y') then
3683             open seqasg(asgid,actseq);
3684          elsif(rmode = 'N') then
3685             open seqper(pactid, asgid,actseq,rmode);
3686          else
3687             open seqgrp(pactid, asgid,actseq,rmode);
3688          end if;
3689 --
3690          loop
3691             -- fetch the rowid and then update.
3692             if(rmode = 'Y') then
3693                fetch seqasg into my_rowid, upd_aa_id, src_aa_id, obj_type;
3694                exit when seqasg%notfound;
3695             elsif(rmode = 'N') then
3696                fetch seqper into my_rowid, upd_aa_id, src_aa_id, obj_type;
3697                exit when seqper%notfound;
3698             else
3699                fetch seqgrp into my_rowid, upd_aa_id, src_aa_id, obj_type;
3700                exit when seqgrp%notfound;
3701             end if;
3702 --
3703 hr_utility.trace('Resequenceing '||upd_aa_id);
3704             resequence_children(upd_aa_id);
3705 --
3706             --
3707             -- Now, update with new action_sequence.
3708             update pay_assignment_actions act
3709             set    act.action_sequence = pay_assignment_actions_s.nextval
3710             where  act.rowid           = my_rowid;
3711 --
3712             update pay_run_balances
3713                set action_sequence = pay_assignment_actions_s.currval
3714              where assignment_action_id = upd_aa_id;
3715 --
3716             /* If it's an assignment action thats created due to a
3717                POG then we need to up the POGs action sequence
3718             */
3719             if (obj_type is not null
3720                 and obj_type = 'POG'
3721                 and src_aa_id is not null) then
3722 --
3723               update pay_assignment_actions act
3724               set    act.action_sequence = pay_assignment_actions_s.nextval
3725               where  act.assignment_action_id = src_aa_id;
3726 --
3727             end if;
3728 --
3729          end loop;
3730 --
3731          --
3732          -- we are finished with the cursor, so we close it here.
3733          if(rmode = 'Y') then
3734             close seqasg;
3735          elsif(rmode = 'N') then
3736             close seqper;
3737          else
3738             close seqgrp;
3739          end if;
3740          if g_debug then
3741             hr_utility.set_location('hrassact.resequence_actions', 20);
3742          end if;
3743       end resequence_actions;
3744 --
3745 --
3746 /*---------------------------  applied_interlocks  -------------------------*/
3747 /*
3748  *   Returns a string of the assignment actions ids which are locked by
3749  *   the assignment action p_locking_action_id.
3750  */
3751 --
3752 function applied_interlocks(p_locking_action_id number) return varchar2 is
3753 --
3754     -- Cursor definitions
3755     cursor interlocks(p_locking_action_id number) is
3756         select int.locked_action_id locked_action_id
3757           from pay_action_interlocks int
3758          where int.locking_action_id = p_locking_action_id
3759       order by int.locked_action_id;
3760 --
3761     -- Local variables
3762     v_string_max_length  constant number := 240;
3763     v_string             varchar2(240)   := NULL;
3764     v_max_length_reached boolean         := false;
3765 --
3766   begin
3767      g_debug := hr_utility.debug_enabled;
3768 --
3769      -- Find interlocks for this assignment action
3770      if g_debug then
3771         hr_utility.set_location('hrassact.applied_interlocks',10);
3772       end if;
3773      <<interlocks_loop>>
3774      for locked IN interlocks(p_locking_action_id) loop
3775 --
3776        if length(rtrim(v_string)) is null then
3777          -- If this is the first locked action id set the string to the id
3778          v_string := locked.locked_action_id;
3779        else
3780 --
3781          if length(rtrim(v_string)) + 1 +
3782             length(rtrim(locked.locked_action_id)) <= v_string_max_length then
3783            -- For the second and subsequent locked action ids, append them to
3784            -- the current string, if there is enough space left for the dash
3785            -- and the whole of the id.
3786            if g_debug then
3787               hr_utility.set_location('hrassact.applied_interlocks',20);
3788            end if;
3789            v_string := v_string || '-' || locked.locked_action_id;
3790          else
3791 --
3792            if length(rtrim(v_string))+2 <= v_string_max_length then
3793               -- If there is no space left at the end of the string for the
3794               -- current locked action id, add the arrow symbol to the end
3795               -- of the string
3796               if g_debug then
3797                  hr_utility.set_location('hrassact.applied_interlocks',30);
3798               end if;
3799               v_string := v_string || '->';
3800            else
3801               -- If the end of the string has already been reached,
3802               -- replace the last id with the arrow symbol
3803               if g_debug then
3804                  hr_utility.set_location('hrassact.applied_interlocks',40);
3805               end if;
3806               v_string := substr(v_string, 1, instr(v_string, '-', -1, 1)-1)
3807                           || '->';
3808            end if;
3809 --
3810            v_max_length_reached := true;
3811 --
3812          end if;                         -- end if length(v_string) + 1 ...
3813 --
3814        end if;                           -- end if v_string = ''
3815 --
3816        -- If the end of the string has been reached then end the loop,
3817        -- as there is no space in v_string to list any more ids
3818        exit interlocks_loop when v_max_length_reached;
3819 --
3820      end loop interlocks_loop;           -- end for locked IN interlocks
3821 --
3822      if g_debug then
3823         hr_utility.set_location('hrassact.applied_interlocks',40);
3824      end if;
3825      return v_string;
3826 --
3827   end applied_interlocks;
3828 --
3829    ------------------- get_default_leg_value -------------------
3830    /*
3831       NAME
3832          get_default_leg_value
3833       DESCRIPTION
3834          Given a legislative procedure name, identifier
3835          and an element entry id. This procedure calculates
3836          the default value.
3837    */
3838 procedure get_default_leg_value (p_plsql_proc in varchar2,
3839                                  p_identifier in number,
3840                                  p_entry      in number,
3841                                  p_effdate    in date,
3842                                  p_value      out nocopy varchar2)
3843 is
3844 l_def_rt_str        varchar2(2000);  -- used with dynamic pl/sql
3845 sql_cursor           integer;
3846 l_rows               integer;
3847 l_value             varchar2(2000);
3848 begin
3849    l_def_rt_str := 'begin '||p_plsql_proc||' (';
3850    l_def_rt_str := l_def_rt_str || ':p_identifier, ';
3851    l_def_rt_str := l_def_rt_str || ':p_entry, ';
3852    l_def_rt_str := l_def_rt_str || ':p_effdate, ';
3853    l_def_rt_str := l_def_rt_str || ':l_value); end; ';
3854    --
3855    sql_cursor := dbms_sql.open_cursor;
3856    --
3857    dbms_sql.parse(sql_cursor, l_def_rt_str, dbms_sql.v7);
3858    --
3859    --
3860    dbms_sql.bind_variable(sql_cursor, 'p_identifier', p_identifier);
3861    --
3862    dbms_sql.bind_variable(sql_cursor, 'p_entry', p_entry);
3863    --
3864    dbms_sql.bind_variable(sql_cursor, 'p_effdate', p_effdate);
3865    --
3866    dbms_sql.bind_variable(sql_cursor, 'l_value', l_value, 30);
3867    --
3868    l_rows := dbms_sql.execute (sql_cursor);
3869    --
3870    if (l_rows = 1) then
3871       dbms_sql.variable_value(sql_cursor, 'l_value',
3872                               l_value);
3873       dbms_sql.close_cursor(sql_cursor);
3874 --
3875    else
3876       l_value := null;
3877       dbms_sql.close_cursor(sql_cursor);
3878    end if;
3879 --
3880    p_value := l_value;
3881 --
3882 end get_default_leg_value;
3883 --
3884    ------------------- get_default_leg_value -------------------
3885    /*
3886       NAME
3887          get_default_leg_value
3888       DESCRIPTION
3889          Given a legislative procedure name this procedure
3890          gets the default run type id.
3891    */
3892 procedure get_default_leg_value
3893   (p_plsql_proc     in  varchar2
3894   ,p_effective_date in  varchar2
3895   ,p_run_type_id    out nocopy number
3896   ) is
3897 --
3898 begin
3899   --
3900   get_default_leg_value
3901     (p_plsql_proc => p_plsql_proc
3902     ,p_identifier => null
3903     ,p_entry      => null
3904     ,p_effdate    => p_effective_date
3905     ,p_value      => p_run_type_id
3906     );
3907 
3908   p_run_type_id := to_number(p_run_type_id);
3909   --
3910 end;
3911 --
3912    --------------------------- cache_contexts------------------------
3913    /*
3914       NAME
3915          cache_contexts - This simply sets up a cache of the context
3916                           names
3917       DESCRIPTION
3918       NOTES
3919    */
3920 procedure cache_contexts
3921 is
3922 --
3923 cursor get_cxt is
3924 select context_name,
3925        context_id
3926   from ff_contexts;
3927 --
3928 begin
3929 --
3930    g_context_cache.sz := 0;
3931 --
3932    for cxtrec in get_cxt loop
3933 --
3934      g_context_cache.sz := g_context_cache.sz + 1;
3935      g_context_cache.cxt_id(g_context_cache.sz) := cxtrec.context_id;
3936      g_context_cache.cxt_name(g_context_cache.sz) := cxtrec.context_name;
3937 --
3938    end loop;
3939 --
3940    contexts_cached := TRUE;
3941 --
3942 end cache_contexts;
3943 --
3944    --------------------------- get_cache_context------------------------
3945    /*
3946       NAME
3947          get_cache_context - This retrieves the context id given the
3948                              context name from the cache.
3949       DESCRIPTION
3950       NOTES
3951    */
3952 procedure get_cache_context(p_cxt_name in     varchar2,
3953                             p_cxt_id   out    nocopy number)
3954 is
3955 --
3956 cxt_num number;
3957 cxt_id  number;
3958 found   boolean;
3959 begin
3960 --
3961    if (contexts_cached = FALSE) then
3962       cache_contexts;
3963    end if;
3964 --
3965    found := FALSE;
3966    for cxt_num in 1..g_context_cache.sz loop
3967 --
3968      if (g_context_cache.cxt_name(cxt_num) = p_cxt_name) then
3969        found := TRUE;
3970        cxt_id := g_context_cache.cxt_id(cxt_num);
3971      end if;
3972    end loop;
3973 --
3974    if (found = TRUE) then
3975      p_cxt_id := cxt_id;
3976    else
3977       hr_general.assert_condition(FALSE);
3978    end if;
3979 --
3980 end get_cache_context;
3981 
3982 
3983    --------------------------- inassact ------------------------------
3984    /*
3985       NAME
3986          inassact - INsert ASSignment Action
3987       DESCRIPTION
3988          Inserts and validates the insert of an assignment
3989          action. This is called
3990          a) from the QuickPay processing procedure (qpassact).
3991          b) from the Reversal procedure (reversal).
3992          c) from external/manual payment procedure (ext_man_payment).
3993          d) from the Balance Adjustment procedure (bal_adjust).
3994          e) from the hrbaldtm package (connected to balance user exit).
3995       NOTES
3996          This procedure handles the interlock rules
3997          required in the case of QuickPay, and the
3998          re-sequencing of action_sequence for
3999          Reversal and Balance Adjustment and External/Manual
4000          Payments.
4001          inassact is a cover for inassact_main, to ensure a
4002          taxunt is passed to inassact_main.
4003    */
4004    procedure inassact
4005    (
4006       pactid in number,  -- payroll_action_id.
4007       asgid  in number,   -- assignment_id to create action for.
4008       p_ass_action_seq in number   default null, --action sequence
4009       p_serial_number  in varchar2 default null, --cheque number
4010       p_pre_payment_id in number   default null, --pre payment id
4011       p_element_entry  in number   default null,
4012       p_asg_lock       in boolean  default TRUE,  --lock assignment
4013       p_purge_mode     in boolean  default FALSE, --purge mode
4014       run_type_id      in number   default null
4015    ) is
4016    --
4017    begin
4018       hrassact.inassact_main(pactid, asgid, p_ass_action_seq,
4019                              p_serial_number, p_pre_payment_id,
4020                              p_element_entry, p_asg_lock, null,
4021                              p_purge_mode,run_type_id);
4022    end inassact;
4023    --
4024    procedure inassact_main
4025    (
4026       pactid in number,  -- payroll_action_id.
4027       asgid  in number,   -- assignment_id to create action for.
4028       p_ass_action_seq in number   default null, --action sequence
4029       p_serial_number  in varchar2 default null, --cheque number
4030       p_pre_payment_id in number   default null, --pre payment id
4031       p_element_entry  in number   default null,
4032       p_asg_lock       in boolean  default TRUE, --lock assignment
4033       taxunt           in number   default null, -- tax unit id
4034       p_purge_mode     in boolean  default FALSE --purge mode
4035    ) is
4036    --
4037    begin
4038           hrassact.inassact_main(pactid, asgid, p_ass_action_seq,
4039                              p_serial_number, p_pre_payment_id,
4040                              p_element_entry, p_asg_lock, taxunt,
4041                              p_purge_mode,null);
4042    end inassact_main;
4043 
4044    procedure inassact_main
4045    (
4046       pactid in number,  -- payroll_action_id.
4047       asgid  in number,   -- assignment_id to create action for.
4048       p_ass_action_seq in number   default null, --action sequence
4049       p_serial_number  in varchar2 default null, --cheque number
4050       p_pre_payment_id in number   default null, --pre payment id
4051       p_element_entry  in number   default null,
4052       p_asg_lock       in boolean  default TRUE, --lock assignment
4053       taxunt           in number   default null, -- tax unit id
4054       p_purge_mode     in boolean  default FALSE, --purge mode
4055       p_run_type_id    in number   default null
4056    ) is
4057     aa_id number;
4058    begin
4059       aa_id := inassact_main
4060                     (
4061                        pactid           => pactid,
4062                        asgid            => asgid,
4063                        p_ass_action_seq => p_ass_action_seq,
4064                        p_serial_number  => p_serial_number,
4065                        p_pre_payment_id => p_pre_payment_id,
4066                        p_element_entry  => p_element_entry,
4067                        p_asg_lock       => p_asg_lock,
4068                        taxunt           => taxunt,
4069                        p_purge_mode     => p_purge_mode,
4070                        p_run_type_id    => p_run_type_id
4071                     );
4072    end inassact_main;
4073 --
4074    function inassact_main
4075    (
4076       pactid in number,  -- payroll_action_id.
4077       asgid  in number,   -- assignment_id to create action for.
4078       p_ass_action_seq in number   default null, --action sequence
4079       p_serial_number  in varchar2 default null, --cheque number
4080       p_pre_payment_id in number   default null, --pre payment id
4081       p_element_entry  in number   default null,
4082       p_asg_lock       in boolean  default TRUE, --lock assignment
4083       taxunt           in number   default null, -- tax unit id
4084       p_purge_mode     in boolean  default FALSE, --purge mode
4085       p_run_type_id    in number   default null ,
4086       p_mode           in varchar2 default 'STANDARD'
4087    ) return number
4088    is
4089       rmode pay_legislation_rules.rule_mode%type;
4090       posid per_periods_of_service.period_of_service_id%type;
4091       actyp pay_payroll_actions.action_type%type;
4092       bgid  pay_payroll_actions.business_group_id%type;
4093       l_action_population_status pay_payroll_actions.action_population_status%type;
4094       aa_id number;
4095    --
4096       --------------------------- irbaact ------------------------------
4097       /*
4098          NAME
4099             irbaact - Insert Reversal or Balance Adjustment ACTion.
4100          DESCRIPTION
4101             Insert an assignment action for a Reversal,
4102             Balance Adjustment or non tracked action.
4103          NOTES
4104             This procedure copes with the re-sequencing
4105             that may be required.
4106             Note the call from the balance user exit code
4107             via the hrbaldtm.get_bal_ass_action procedure.
4108       */
4109       procedure irbaact
4110       (
4111          pactid   in number,    -- payroll_action_id.
4112          asgid    in number,    -- assignment_id of action to create.
4113          rmode    in varchar2,  -- rule_mode (time period independent Y or N)
4114          actyp    in varchar2,  -- action type.
4115          ee_id    in number,    -- element entry id.
4116          taxunt   in number,    -- tax unit id
4117          pmode    in boolean,    -- purge mode
4118          p_run_type_id   in number,    -- tax unit id
4119          aa_id    in number,
4120          p_mode   in varchar2
4121       ) is
4122 --
4123          --
4124          -- This cursor selects the rowid values of all
4125          -- the sequenced assignment actions for the
4126          -- specified assignment only, that are later
4127          -- than the effective_date of the specified
4128          -- payroll action.
4129          cursor seqasg (pactid number,
4130                         asgid  number) is
4131          select ac2.rowid,
4132                 ac2.assignment_action_id
4133          from   pay_action_classifications acl,
4134                 pay_assignment_actions     ac2,
4135                 pay_assignment_actions     act,
4136                 pay_payroll_actions        pa2,
4137                 pay_payroll_actions        pac
4138          where  pac.payroll_action_id   = pactid
4139          and    act.payroll_action_id   = pac.payroll_action_id
4140          and    act.assignment_id       = asgid
4141          and    ac2.assignment_id       = act.assignment_id
4142          and    pa2.payroll_action_id   = ac2.payroll_action_id
4143          and    acl.classification_name = 'SEQUENCED'
4144          and    pa2.action_type         = acl.action_type
4145          and    pa2.effective_date      > pac.effective_date
4146          order by pa2.effective_date, ac2.action_sequence
4147          for update of ac2.assignment_action_id;
4148    --
4149          --
4150          -- This cursor selects the rowid values of all
4151          -- the sequenced assignment actions for the
4152          -- specified assignment and any other assignments
4153          -- for that person, that are later than the
4154          -- effective_date of the specified payroll action.
4155          -- Grabs all assignments for person, regardless of
4156          -- period of service.
4157          --
4158          cursor seqper (pactid number,
4159                         asgid  number) is
4160          select /*+ ORDERED
4161                     INDEX(ac2 PAY_ASSIGNMENT_ACTIONS_N51) */
4162                 ac2.rowid,
4163                 ac2.assignment_action_id
4164          from   pay_payroll_actions        pac,
4165                 pay_assignment_actions     act,
4166                 per_all_assignments_f          asg,
4167                 per_all_assignments_f          as2,
4168                 pay_assignment_actions     ac2,
4169                 pay_payroll_actions        pa2,
4170                 pay_action_classifications acl
4171          where  pac.payroll_action_id    = pactid
4172          and    act.payroll_action_id    = pac.payroll_action_id
4173          and    act.assignment_id        = asgid
4174          and    asg.assignment_id        = act.assignment_id
4175          and    pac.effective_date between
4176                 asg.effective_start_date and asg.effective_end_date
4177          and    as2.person_id            = asg.person_id
4178          and    as2.effective_end_date   > pac.effective_date
4179          and    ac2.assignment_id        = as2.assignment_id
4180          and    pa2.payroll_action_id    = ac2.payroll_action_id
4181          and    pa2.effective_date between
4182                 as2.effective_start_date and as2.effective_end_date
4183          and    acl.classification_name  = 'SEQUENCED'
4184          and    pa2.action_type          = acl.action_type
4185          and    pa2.effective_date       > pac.effective_date
4186          order by pa2.effective_date, ac2.action_sequence
4187          for update of ac2.assignment_action_id;
4188 --
4189          --
4190          -- Pay Object Group (G itpflag):
4191          -- This cursor selects the rowid values of all
4192          -- the sequenced assignment actions for the
4193          -- specified assignment and any other assignments
4194          -- for that person, that are later than the
4195          -- effective_date of the specified payroll action.
4196          -- Grabs all assignments for person, regardless of
4197          -- period of service.
4198          --
4199          cursor seqgrp (pactid number,
4200                         asgid  number) is
4201          select /*+ ORDERED
4202                     INDEX(ac2 PAY_ASSIGNMENT_ACTIONS_PK) */
4203                 ac2.rowid,
4204                 ac2.assignment_action_id
4205          from
4206             (
4207          select /*+ ORDERED */
4208                 distinct ac3.assignment_action_id
4209          from   pay_payroll_actions        pac,
4210                 pay_assignment_actions     act,
4211                 pay_object_groups          pog,
4212                 pay_object_groups          pog2,
4213                 pay_object_groups          pog3,
4214                 pay_object_groups          pog4,
4215                 pay_assignment_actions     ac3,
4216                 pay_payroll_actions        pa3,
4217                 pay_action_classifications acl
4218          where  pac.payroll_action_id    = pactid
4219          and    act.payroll_action_id    = pac.payroll_action_id
4220          and    act.assignment_id        = asgid
4221          and    pog.source_id            = act.assignment_id
4222          and    pog.source_type          = 'PAF'
4223          and    pog2.object_group_id     = pog.parent_object_group_id
4224          and    pog2.source_type         = 'PPF'
4225          and    pog3.source_id           = pog2.source_id
4226          and    pog3.source_type         = 'PPF'
4227          and    pog4.parent_object_group_id = pog3.object_group_id
4228          and    ac3.assignment_id     = pog4.source_id
4229          and    pa3.payroll_action_id = ac3.payroll_action_id
4230          and    pa3.effective_date    > pac.effective_date
4231          and    pa3.action_type      <> 'L'
4232          and    pa3.action_type       = acl.action_type
4233          and    acl.classification_name = 'SEQUENCED'
4234                 union all
4235          select /*+ ORDERED */
4236                 distinct ac3.assignment_action_id
4237          from   pay_payroll_actions        pac,
4238                 pay_assignment_actions     act,
4239                 pay_object_groups          pog,
4240                 pay_object_groups          pog2,
4241                 pay_object_groups          pog3,
4242                 pay_assignment_actions     ac3,
4243                 pay_payroll_actions        pa3
4244          where  pac.payroll_action_id    = pactid
4245          and    act.payroll_action_id    = pac.payroll_action_id
4246          and    act.assignment_id        = asgid
4247          and    pog.source_id            = act.assignment_id
4248          and    pog.source_type          = 'PAF'
4249          and    pog2.object_group_id     = pog.parent_object_group_id
4250          and    pog2.source_type         = 'PPF'
4251          and    pog3.source_id           = pog2.source_id
4252          and    pog3.source_type         = 'PPF'
4253          and    ac3.object_id         = pog3.object_group_id
4254          and    ac3.object_type       = 'POG'
4255          and    pa3.payroll_action_id = ac3.payroll_action_id
4256          and    pa3.effective_date    > pac.effective_date
4257          and    pa3.action_type       = 'L') V,
4258                   pay_assignment_actions     ac2,
4259                   pay_payroll_actions        pa2
4260          where ac2.assignment_action_id = V.assignment_action_id
4261          and    pa2.payroll_action_id    = ac2.payroll_action_id
4262          order by pa2.effective_date, ac2.action_sequence
4263          for update of ac2.assignment_action_id;
4264 --
4265          cursor perasg (persid number) is
4266          select distinct paf.assignment_id
4267          from per_all_assignments_f paf
4268          where paf.person_id = persid;
4269 --
4270          cursor perobj (asgid number) is
4271          select distinct pog_grp.source_id assignment_id
4272          from pay_object_groups      pog_act,
4273               pay_object_groups      pog_grp
4274          where pog_act.source_id = asgid
4275          and   pog_act.source_type = 'PAF'
4276          and   pog_act.parent_object_group_id = pog_grp.parent_object_group_id
4277          and   pog_grp.source_type = 'PAF';
4278 --
4279          my_rowid rowid;
4280          upd_aa_id pay_assignment_actions.assignment_action_id%type;
4281          actype   pay_payroll_actions.action_type%type;
4282          effdate  date;  -- the effective date of the payroll action.
4283          lgcode   varchar2(30);  -- Used to check if the US legislation.
4284          persid    number; -- assignments person_id
4285          taxunt2   number;        -- The tax unit id if legislation is US
4286          l_run_type_id number;
4287          aa_exists number;
4288 --
4289       begin
4290          g_debug := hr_utility.debug_enabled;
4291 --
4292          taxunt2 := null;
4293          l_run_type_id := p_run_type_id;
4294 --
4295          -- Need the effective date of the payroll action.
4296          -- Also want to see the action_type.
4297          if(actyp <> 'X') then
4298             -- Do not need either effective date or leg code for 'X'.
4299             -- Need the effective date of the payroll action.
4300             -- Also want to see the action_type.
4301             -- The legislation and the tax unit is also needed for the US.
4302             if g_debug then
4303                hr_utility.set_location('hrassact.irbaact',10);
4304 --
4305                hr_utility.trace('pactid = '||pactid);
4306                hr_utility.trace('assignment_id = '||asgid);
4307             end if;
4308 --
4309             select pac.effective_date,
4310                    pac.action_type,
4311                    pbg.legislation_code,
4312                    asg.person_id
4313             into   effdate, actype, lgcode, persid
4314             from
4315                    per_business_groups_perf pbg,
4316                    per_all_assignments_f   asg,
4317                    pay_payroll_actions pac
4318             where  pac.payroll_action_id      = pactid
4319             and    pbg.business_group_id      = asg.business_group_id
4320             and    asg.assignment_id          = asgid
4321             and    pac.effective_date between
4322                         asg.effective_start_date and asg.effective_end_date;
4323 --
4324             if    (actyp not in ('V','B','I'))
4325                or ((actyp in ('B','I')) and (taxunt is null)) then
4326               --
4327               taxunt2 := hr_dynsql.get_tax_unit(asgid, effdate);
4328             end if;
4329 --
4330             if g_debug then
4331                hr_utility.set_location('hrassact.irbaact',11);
4332             end if;
4333          end if;
4334 --
4335          if (actyp = 'V') then
4336             taxunt2 := taxunt;
4337          end if;
4338 --
4339          -- Need to set the run type for Bal Adj.
4340          -- Note, this is not done for Initialisation. These
4341          -- Have to be set explicitly on initialisation.
4342          if (actyp = 'B') then
4343 --
4344           declare
4345             l_rt_id varchar2(10);
4346             default_run_type pay_legislation_rules.rule_mode%type;
4347           begin
4348            if (p_run_type_id is null) then
4349              begin
4350                select rule_mode
4351                into default_run_type
4352                from pay_legislation_rules
4353                where legislation_code = lgcode
4354                and   rule_type        = 'DEFAULT_RUN_TYPE';
4355              exception
4356                 when no_data_found then
4357                   default_run_type := 'N';
4358              end;
4359 --
4360              if (default_run_type = 'Y') then
4361                get_default_leg_value('pay_' || lgcode || '_rules.get_default_run_type',
4362                                      asgid,
4363                                      ee_id,
4364                                      effdate,
4365                                      l_rt_id);
4366                l_run_type_id := to_number(l_rt_id);
4367              end if;
4368            end if;
4369           end;
4370          end if;
4371 --
4372          if (actyp in ('B', 'I')) then
4373            if (taxunt is not null) then
4374               taxunt2 := taxunt;
4375            end if;
4376          end if;
4377 --
4378          -- Before we go further, trash latest balances.
4379          -- Don't trash for non tracked action.
4380          -- Note that this is used for the balance function.
4381 
4382          if(actype <> 'N' and actype <> 'X' and actype <> 'B' and
4383             actype <> 'I' and actype <> 'V') then
4384             del_latest_balances(asgid, effdate, p_element_entry);
4385          end if;
4386 --
4387          -- insert an assigment action.
4388         if g_debug then
4389             hr_utility.set_location('hrassact.irbaact',20);
4390          end if;
4391          if((actype = 'V' or actype = 'I' or actype = 'B')
4392              and p_mode <> 'BACKPAY') then
4393          -- we need to check that there isn't a failed action for this
4394          -- assignment/person/group
4395          if (rmode = 'Y') then
4396             begin
4397                select 1
4398                into aa_exists
4399                from dual
4400                where exists (
4401                       select /*+ INDEX (pay_pa2 pay_payroll_actions_pk) */ null
4402                       from   pay_action_classifications pay_acl,
4403                              pay_payroll_actions        pay_pa2,
4404                              pay_assignment_actions     pay_ac2
4405                       where  pay_ac2.assignment_id       = asgid
4406                       and    pay_pa2.payroll_action_id   = pay_ac2.payroll_action_id
4407                       and    pay_acl.classification_name = 'SEQUENCED'
4408                       and    pay_pa2.action_type         = pay_acl.action_type
4409                       and    pay_ac2.action_status not in ('C', 'S', 'B'));
4410             exception
4411                when no_data_found then
4412                   aa_exists := 0;
4413             end;
4414          elsif (rmode = 'N') then
4415             aa_exists := 0;
4416             for dprec in perasg(persid) loop
4417                begin
4418                   select 1
4419                   into aa_exists
4420                   from sys.dual
4421                   where exists (
4422                       select /*+ INDEX (pay_pa2 pay_payroll_actions_pk) */ null
4423                       from   pay_action_classifications pay_acl,
4424                              pay_payroll_actions        pay_pa2,
4425                              pay_assignment_actions     pay_ac2
4426                       where  pay_ac2.assignment_id       = dprec.assignment_id
4427                       and    pay_pa2.payroll_action_id   = pay_ac2.payroll_action_id
4428                       and    pay_acl.classification_name = 'SEQUENCED'
4429                       and    pay_pa2.action_type         = pay_acl.action_type
4430                       and    pay_ac2.action_status not in ('C', 'S', 'B'));
4431 
4432                   exit;
4433                exception
4434                   when no_data_found then
4435                      null;
4436                end;
4437             end loop;
4438          else
4439             -- rmode = G
4440             aa_exists := 0;
4441             for dprec in perobj(asgid) loop
4442                begin
4443                   select 1
4444                   into aa_exists
4445                   from sys.dual
4446                   where exists (
4447                       select /*+ INDEX (pay_pa2 pay_payroll_actions_pk) */ null
4448                       from   pay_action_classifications pay_acl,
4449                              pay_payroll_actions        pay_pa2,
4450                              pay_assignment_actions     pay_ac2
4451                       where  pay_ac2.assignment_id       = dprec.assignment_id
4452                       and    pay_pa2.payroll_action_id   = pay_ac2.payroll_action_id
4453                       and    pay_acl.classification_name = 'SEQUENCED'
4454                       and    pay_pa2.action_type         = pay_acl.action_type
4455                       and    pay_ac2.action_status not in ('C', 'S', 'B'));
4456 
4457                   exit;
4458                exception
4459                   when no_data_found then
4460                      null;
4461                end;
4462             end loop;
4463          end if;
4464          if(aa_exists <> 0) then
4465             hr_utility.set_message(801,'HR_7010_ACTION_INTLOCK_FAIL');
4466             hr_utility.raise_error;
4467          end if;
4468          end if;
4469          -- we need to check that there is not a purge action
4470          -- with an effective date later than the effective
4471          -- date of the assignment action we are about to
4472          -- insert. If so, raise an error.
4473          -- An exception is the 'I' action.  It can be inserted
4474          -- before a purge, because a Purge uses balance
4475          -- initialisation to perform balance rollup.
4476          if g_debug then
4477             hr_utility.set_location('hrassact.irbaact',25);
4478          end if;
4479          insert into pay_assignment_actions (
4480                 assignment_action_id,
4481                 assignment_id,
4482                 payroll_action_id,
4483                 action_status,
4484                 chunk_number,
4485                 action_sequence,
4486                 object_version_number,
4487                 tax_unit_id,
4488                 run_type_id)
4489          select aa_id,
4490                 asgid,
4491                 pactid,
4492                 decode(actyp, 'N', 'U', 'X', 'U', 'C'),
4493                 1,
4494                 aa_id,
4495                 1,
4496                 taxunt2,
4497                 l_run_type_id
4498          from   sys.dual
4499          where  not exists (
4500                 select null
4501                 from   pay_payroll_actions    pac,
4502                        pay_payroll_actions    pa2,
4503                        pay_assignment_actions act,
4504                        pay_assignment_actions ac2
4505                  where pac.payroll_action_id = pactid
4506                  and   pac.action_type      <> 'I'
4507                  and   act.payroll_action_id = pac.payroll_action_id
4508                  and   act.assignment_id     = asgid
4509                  and   ac2.assignment_id     = act.assignment_id
4510                  and   pa2.payroll_action_id = ac2.payroll_action_id
4511                  and   pa2.action_type       = 'Z'
4512                  and   pa2.effective_date    > pac.effective_date);
4513 --
4514          if(sql%rowcount = 0) then
4515             hr_utility.set_message(801,'HR_7009_ACTION_FUTURE_PURGE');
4516             hr_utility.raise_error;
4517          end if;
4518 --
4519          -- If we are really getting called from purge, we do not
4520          -- want to perform any re-sequencing.  Purge mode is actually
4521          -- only set when we are creating balance initialzation actions
4522          -- but we DO want to contine re-sequencing when performing
4523          -- a "normal" initialization.
4524          -- When purge mode is operating, the purge process itself
4525          -- takes responsibility for any re-sequencing required.
4526          if(not pmode) then
4527             -- method is to loop round the selected assignment
4528             -- action rows, updating the action sequence.
4529             -- We do not attempt a correlated update, since we
4530             -- need to aquire and hold locks on all the rows.
4531             -- NOTE - the update of sequences for 'X' actions will
4532             -- work correctly because the seqasg cursor does not
4533             -- join to per_all_assignments_f.
4534             if g_debug then
4535                hr_utility.set_location('hrassact.irbaact',30);
4536             end if;
4537             if(rmode = 'Y') then
4538                open seqasg(pactid, asgid);
4539             elsif(rmode = 'N') then
4540                open seqper(pactid, asgid);
4541             else
4542                open seqgrp(pactid, asgid);
4543             end if;
4544 --
4545             loop
4546                -- fetch the rowid and then update.
4547                if(rmode = 'Y') then
4548                   fetch seqasg into my_rowid, upd_aa_id;
4549                   exit when seqasg%notfound;
4550                elsif(rmode = 'N') then
4551                   fetch seqper into my_rowid, upd_aa_id;
4552                   exit when seqper%notfound;
4553                else
4554                   fetch seqgrp into my_rowid, upd_aa_id;
4555                   exit when seqgrp%notfound;
4556                end if;
4557 --
4558                --
4559                -- Now, update with new action_sequence.
4560                update pay_assignment_actions act
4561                set    act.action_sequence = pay_assignment_actions_s.nextval
4562                where  act.rowid           = my_rowid;
4563 --
4564                update pay_run_balances
4565                   set action_sequence = pay_assignment_actions_s.currval
4566                 where assignment_action_id = upd_aa_id;
4567             end loop;
4568 --
4569             --
4570             -- we are finished with the cursor, so we close it here.
4571             if(rmode = 'Y') then
4572                close seqasg;
4573             elsif(rmode = 'N') then
4574                close seqper;
4575             else
4576                close seqgrp;
4577             end if;
4578          end if;
4579       end irbaact;
4580 --
4581    begin
4582       g_debug := hr_utility.debug_enabled;
4583 --
4584       -- Start by grabbing the type of the action we are dealing with.
4585       select pac.action_type,
4586              pac.business_group_id,
4587              pay_assignment_actions_s.nextval
4588       into   actyp,
4589              bgid,
4590              aa_id
4591       from   pay_payroll_actions pac
4592       where  pac.payroll_action_id = pactid;
4593 --
4594       -- Most of the action types require that we look for the legislation
4595       -- rule, i.e. time period dependent (i.e. UK) or time period
4596       -- independent (i.e. US) time periods.  However, we ignore this for
4597       -- E(X)tract actions, which are a special case.
4598       begin
4599         if(actyp <> 'X') then
4600            -- take the opportunity here to lock the assignment
4601            -- and period of service of the person.
4602            if g_debug then
4603               hr_utility.set_location('hrassact.inassact_main',10);
4604            end if;
4605            if (p_asg_lock) then
4606               -- We wish to lock the assignment and period of service.
4607               declare
4608                  posid number;
4609               begin
4610                  select /*+ ORDERED
4611                             INDEX (asg PER_ASSIGNMENTS_F_PK)
4612                             USE_NL(asg) */
4613                            asg.business_group_id,
4614                            asg.period_of_service_id
4615                  into   bgid, posid
4616                  from   pay_payroll_actions   pac,
4617                      per_all_assignments_f     asg
4618                  where  pac.payroll_action_id     = pactid
4619                  and    asg.assignment_id         = asgid
4620                  and    pac.effective_date between
4621                         asg.effective_start_date and asg.effective_end_date
4622                  for update of asg.assignment_id;
4623 
4624                  if posid is not null then
4625                     begin
4626                        select 1
4627                        into   posid
4628                        from   per_periods_of_service   pos
4629                        where  pos.period_of_service_id = posid
4630                        for update of pos.period_of_service_id;
4631                     exception
4632                        when no_data_found then
4633                           null;
4634                     end;
4635                  end if;
4636               end;
4637            end if;
4638            --
4639            if g_debug then
4640               hr_utility.set_location('hrassact.inassact_main',15);
4641            end if;
4642            --
4643            -- get the rule_mode
4644            select /*+ ORDERED*/ plr.rule_mode
4645            into   rmode
4646            from   per_business_groups_perf grp,
4647                   pay_legislation_rules plr
4648            where  grp.business_group_id     = bgid
4649            and    plr.legislation_code      = grp.legislation_code
4650            and    plr.rule_type             = 'I'
4651            and    plr.rule_mode            in ('Y','N','G');
4652            --
4653         end if;
4654         --
4655         exception
4656           when no_data_found then
4657           rmode := 'N';
4658       end;
4659 --
4660       --
4661       -- Different processing, depending on the type of action.
4662       if(actyp = 'Q') then
4663          -- process QuickPay.
4664          -- Defferent processing depending on the rule mode.
4665          -- the statements below both check that the assignment
4666          -- is date effective for date_earned and date_paid
4667          -- (i.e. effective_date). This is is exactly the same
4668          -- as for the run.
4669          if(rmode = 'Y') then
4670             -- time period independent legislation.
4671             if g_debug then
4672                hr_utility.set_location('hrassact.inassact_main',20);
4673             end if;
4674             insert into pay_assignment_actions (
4675                    assignment_action_id,
4676                    assignment_id,
4677                    payroll_action_id,
4678                    action_status,
4679                    chunk_number,
4680                    action_sequence,
4681                    object_version_number,
4682                    tax_unit_id)
4683             select aa_id,
4684                    asgid,
4685                    pac.payroll_action_id,
4686                    'U',
4687                    1,
4688                    aa_id,
4689                    1,
4690                    hr_dynsql.get_tax_unit(asg.assignment_id,
4691                                           pac.effective_date)
4692             from
4693                    per_business_groups_perf pbg,
4694                    per_all_assignments_f asg,
4695                    per_all_assignments_f as2,
4696                    pay_payroll_actions pac
4697             where  pac.payroll_action_id = pactid
4698             and    asg.payroll_id        = pac.payroll_id
4699             and    asg.assignment_id     = asgid
4700             and    pac.effective_date between
4701                    asg.effective_start_date and asg.effective_end_date
4702             and    as2.assignment_id     = asg.assignment_id
4703             and    pac.date_earned between
4704                    as2.effective_start_date and as2.effective_end_date
4705             and    pbg.business_group_id = pac.business_group_id
4706             and    not exists (
4707                    select null
4708                    from   pay_action_classifications acl,
4709                           pay_payroll_actions        pa2,
4710                           pay_assignment_actions     ac2
4711                    where  ac2.assignment_id       = asg.assignment_id
4712                    and    pa2.payroll_action_id   = ac2.payroll_action_id
4713                    and    acl.classification_name = 'SEQUENCED'
4714                    and    pa2.action_type         = acl.action_type
4715                    and   (pa2.effective_date > pac.effective_date
4716                       or (ac2.action_status not in ('C', 'S')
4717                    and    pa2.effective_date <= pac.effective_date)));
4718          else
4719             -- time period dependent legislation.
4720             if g_debug then
4721                hr_utility.set_location('hrassact.inassact_main',30);
4722             end if;
4723             insert into pay_assignment_actions (
4724                    assignment_action_id,
4725                    assignment_id,
4726                    payroll_action_id,
4727                    action_status,
4728                    chunk_number,
4729                    action_sequence,
4730                    object_version_number,
4731                    tax_unit_id)
4732             select aa_id,
4733                    asgid,
4734                    pac.payroll_action_id,
4735                    'U',
4736                    1,
4737                    aa_id,
4738                    1,
4739                    hr_dynsql.get_tax_unit(asg.assignment_id,
4740                                           pac.effective_date)
4741             from   per_business_groups_perf pbg,
4742                    per_all_assignments_f  asg,
4743                    per_all_assignments_f  as2,
4744                    per_periods_of_service pos,
4745                    pay_payroll_actions    pac
4746             where  pac.payroll_action_id    = pactid
4747             and    asg.payroll_id           = pac.payroll_id
4748             and    asg.assignment_id        = asgid
4749             and    pos.period_of_service_id = asg.period_of_service_id
4750             and    pac.effective_date between
4751                    asg.effective_start_date and asg.effective_end_date
4752             and    as2.assignment_id        = asg.assignment_id
4753             and    pac.date_earned between
4754                    as2.effective_start_date and as2.effective_end_date
4755             and    pbg.business_group_id    = pac.business_group_id
4756             and    not exists (
4757                    select null
4758                    from   pay_action_classifications acl,
4759                           pay_assignment_actions     ac2,
4760                           pay_payroll_actions        pa2,
4761                           per_all_assignments_f          as2
4762                    where  as2.period_of_service_id = pos.period_of_service_id
4763                    and    ac2.assignment_id        = as2.assignment_id
4764                    and    pa2.payroll_action_id    = ac2.payroll_action_id
4765                    and    acl.classification_name  = 'SEQUENCED'
4766                    and    pa2.action_type          = acl.action_type
4767                    and    (pa2.effective_date > pac.effective_date
4768                        or (ac2.action_status not in ('C', 'S')
4769                    and    pa2.effective_date <= pac.effective_date)));
4770          end if;
4771 --
4772          -- Check if a row was inserted.
4773          -- If a row was not inserted, it must mean interlock
4774          -- rule failure for that particular assignment.
4775          -- This condition is reported via an error message.
4776          if(sql%rowcount = 0) then
4777             hr_utility.set_message(801,'HR_7010_ACTION_INTLOCK_FAIL');
4778             hr_utility.raise_error;
4779          end if;
4780       elsif (actyp in ('V','B','N','X','I')) then
4781          --
4782          -- The following called to create assignment action for
4783          -- Reversal, Balance Adjustment Non Tracked, Archive and
4784          -- Balance Upload actions.
4785          -- The non tracked case is for the balance user exit.
4786          irbaact(pactid,asgid,rmode,actyp, p_element_entry, taxunt,
4787                  p_purge_mode,p_run_type_id,aa_id, p_mode);
4788       elsif (actyp = 'E') then
4789          --
4790          -- Insert a pay_assignment_actions record for Pre-payment
4791          -- External/Manual payments.
4792          -- Only do this if the pre-payment has not already
4793          -- been paid.
4794          -- This is detected if there is any assignment action
4795          -- with a pre_payment_id the same as that we are attempting
4796          -- to process which is not locked by a void action.
4797          insert into PAY_ASSIGNMENT_ACTIONS (
4798                 ASSIGNMENT_ACTION_ID,
4799                 ASSIGNMENT_ID,
4800                 PAYROLL_ACTION_ID,
4801                 ACTION_STATUS,
4802                 CHUNK_NUMBER,
4803                 ACTION_SEQUENCE,
4804                 PRE_PAYMENT_ID,
4805                 SERIAL_NUMBER,
4806                 OBJECT_VERSION_NUMBER)
4807          select p_ass_action_seq,
4808                 asgid,
4809                 pactid,
4810                 'C',
4811                 1,
4812                 PAY_ASSIGNMENT_ACTIONS_S.nextval,
4813                 p_pre_payment_id,
4814                 p_serial_number,
4815                 1
4816          from   sys.dual
4817          where  not exists (
4818                 select null
4819                 from   pay_assignment_actions act
4820                 where  act.pre_payment_id = p_pre_payment_id
4821                 and not exists
4822                     ( select null
4823                       from   pay_action_interlocks loc1,
4824                              pay_assignment_actions actv,
4825                              pay_payroll_actions    pactv
4826                       where  loc1.locked_action_id  = act.assignment_action_id
4827                       and    loc1.locking_action_id = actv.assignment_action_id
4828                       and    pactv.payroll_action_id = actv.payroll_action_id
4829                       and    pactv.action_type       = 'D'
4830                     )
4831            ) ;
4832 
4833          --
4834          -- Check that a row has been inserted.
4835          if g_debug then
4836             hr_utility.set_location('hrassact.inassact_main',40);
4837          end if;
4838          if(sql%rowcount = 0) then
4839             hr_utility.set_message(801,'HR_7010_ACTION_INTLOCK_FAIL');
4840             hr_utility.raise_error;
4841          end if;
4842          --
4843       elsif (actyp = 'U') then
4844          -- Insert an assignment action for a QuickPay Pre-Payment.
4845          -- We perform validation of the interlock rules here.
4846          -- kkawol: Only return rows for master assignment action.
4847          --
4848          if g_debug then
4849             hr_utility.set_location('hrassact.inassact_main',45);
4850          end if;
4851          insert into PAY_ASSIGNMENT_ACTIONS (
4852                 ASSIGNMENT_ACTION_ID,
4853                 ASSIGNMENT_ID,
4854                 PAYROLL_ACTION_ID,
4855                 ACTION_STATUS,
4856                 CHUNK_NUMBER,
4857                 ACTION_SEQUENCE,
4858                 OBJECT_VERSION_NUMBER,
4859                 TAX_UNIT_ID)
4860          select aa_id,
4861                 act.assignment_id,
4862                 pac.payroll_action_id,   -- qpprepay pact.
4863                 'U',
4864                 1,
4865                 aa_id,
4866                 1,
4867                 hr_dynsql.get_tax_unit(asg.assignment_id,
4868                                        pac.effective_date)
4869          from   per_business_groups_perf pbg,
4870                 pay_assignment_actions act,
4871                 pay_payroll_actions    pac,  -- prepay action.
4872                 pay_payroll_actions    pa2,  -- the QuickPay action.
4873                 per_periods_of_service pos,
4874                 per_all_assignments_f      asg
4875          where  pac.payroll_action_id      = pactid
4876          and    pa2.payroll_action_id      = pac.target_payroll_action_id
4877          and    act.payroll_action_id      = pa2.payroll_action_id
4878          and    act.source_action_id is null  /* master assignment action */
4879          and    asg.assignment_id          = act.assignment_id
4880          and    pa2.effective_date between
4881                 asg.effective_start_date and asg.effective_end_date
4882          and    pos.period_of_service_id   = asg.period_of_service_id
4883          and    pbg.business_group_id      = pac.business_group_id
4884          and    not exists (
4885                 select null
4886                 from   pay_assignment_actions ac2,
4887                        pay_payroll_actions    pa3,
4888                        pay_action_interlocks  int
4889                 where  int.locked_action_id     = act.assignment_action_id
4890                 and    ac2.assignment_action_id = int.locking_action_id
4891                 and    pa3.payroll_action_id    = ac2.payroll_action_id
4892                 and    pa3.action_type          in ('P', 'U'))
4893          and    not exists (
4894                 select null
4895                 from   per_all_assignments_f  as3,
4896                        pay_assignment_actions ac3
4897                 where  rmode                 <> 'Y'
4898                 and    ac3.payroll_action_id = pa2.payroll_action_id
4899                 and    ac3.action_status    not in ('C', 'S')
4900                 and    as3.assignment_id     = ac3.assignment_id
4901                 and    pa2.effective_date between
4902                        as3.effective_start_date and as3.effective_end_date
4903                 and    as3.person_id         = pos.person_id);
4904       else
4905          -- If we get here, we are attempting to process
4906          -- an illegal action type.
4907          hr_utility.set_message(801,'HR_7034_ACTION_TYP_INV_VBN');
4908          hr_utility.raise_error;
4909       end if;
4910 --
4911       --
4912       -- update the action_population_status to indicate
4913       -- an action has been successfully inserted.
4914       if g_debug then
4915          hr_utility.set_location('hrassact.inassact_main',50);
4916       end if;
4917 
4918       begin
4919          select pac.action_population_status
4920          into l_action_population_status
4921          from pay_payroll_actions pac
4922          where pac.payroll_action_id = pactid;
4923       exception
4924          when others then
4925             l_action_population_status := 'U';
4926       end;
4927 
4928       if l_action_population_status <> 'C' then
4929          update pay_payroll_actions pac
4930          set    pac.action_population_status = 'C'
4931          where  pac.payroll_action_id        = pactid;
4932       end if;
4933 --
4934       return aa_id;
4935 --
4936    end inassact_main;
4937 --
4938    ----------------------------- qpassact ------------------------------
4939    /*
4940       NAME
4941          qpassact - insert QuickPay ASSignment Action
4942       DESCRIPTION
4943          Inserts and validates the insert of an assignment
4944          action for the QuickPay user exit.
4945       NOTES
4946          This procedure directly calls inassact.
4947    */
4948    procedure qpassact
4949    (
4950       p_payroll_action_id     in  number, -- payroll_action_id.
4951       p_assignment_id         in  number, -- assignment_id to create action for.
4952       p_assignment_action_id  out nocopy number,
4953       p_object_version_number out nocopy number
4954    ) is
4955       assactid number;
4956       c_indent constant varchar2(30) := 'hrassact.qpassact';
4957    begin
4958       g_debug := hr_utility.debug_enabled;
4959 --
4960       -- Simply call the inassact procedure.
4961       if g_debug then
4962          hr_utility.set_location(c_indent,5);
4963       end if;
4964       inassact(p_payroll_action_id, p_assignment_id, null, null, null);
4965 --
4966       -- Get the assignment_action_id
4967       -- that has just been inserted
4968       select pay_assignment_actions_s.currval
4969       into   assactid
4970       from   sys.dual;
4971 --
4972       -- Return information.
4973       p_assignment_action_id := assactid;
4974       p_object_version_number := 1;
4975    end qpassact;
4976 --
4977    --------------------------- qpppassact ------------------------------
4978    /*
4979       NAME
4980          qpppassact - Insert a QuickPay Pre-Payment action.
4981       DESCRIPTION
4982          Process a QuickPay Pre-Payment action.
4983       NOTES
4984          This procedure is meant to be called via the QuickPay form.
4985    */
4986    procedure qpppassact
4987    (
4988       p_payroll_action_id     in  number, -- of QuickPay pre-payment.
4989       p_assignment_action_id  out nocopy number,
4990       p_object_version_number out nocopy number
4991    ) is
4992       l_assignment_id     number;
4993       l_locking_action_id number;
4994       l_locked_action_id  number;
4995    begin
4996       g_debug := hr_utility.debug_enabled;
4997 --
4998       -- We have to get the assignment_id
4999       -- of the assignment that is going
5000       -- to be prepaid, so it may be passed on.
5001       -- kkawol: query restricted to only return the assignment id
5002       -- of the master action.
5003       --
5004       if g_debug then
5005          hr_utility.set_location('hrassact.qpppassact',10);
5006       end if;
5007       select act.assignment_id
5008       into   l_assignment_id
5009       from   pay_assignment_actions act,
5010              pay_payroll_actions    pac
5011       where  pac.payroll_action_id = p_payroll_action_id
5012       and    act.payroll_action_id = pac.target_payroll_action_id
5013       and    act.source_action_id is null;
5014 --
5015       -- Start by simply inserting an assignment action.
5016       if g_debug then
5017          hr_utility.set_location('hrassact.qpppassact',20);
5018       end if;
5019       hrassact.inassact(p_payroll_action_id,l_assignment_id,null,null,null);
5020 --
5021       -- Get some information for insert to interlocks.
5022       -- kkawol: Only returning details for the master action.
5023       --
5024       if g_debug then
5025          hr_utility.set_location('hrassact.qpppassact',30);
5026       end if;
5027       select pay_assignment_actions_s.currval,
5028              act.assignment_action_id
5029       into   l_locking_action_id,
5030              l_locked_action_id
5031       from   pay_payroll_actions    pac,
5032              pay_assignment_actions act
5033       where  pac.payroll_action_id = p_payroll_action_id
5034       and    act.payroll_action_id = pac.target_payroll_action_id
5035       and    act.source_action_id is null;
5036 --
5037       -- We can now insert interlock row.
5038       if g_debug then
5039          hr_utility.set_location('hrassact.qpppassact',40);
5040       end if;
5041       insert  into pay_action_interlocks (
5042               locking_action_id,
5043               locked_action_id)
5044       values (l_locking_action_id,
5045               l_locked_action_id);
5046 --
5047       -- Update the payroll actions table with the
5048       -- appropriate date_earned value.
5049       if g_debug then
5050          hr_utility.set_location('hrassact.qpppassact',50);
5051       end if;
5052       update pay_payroll_actions pac
5053       set    pac.date_earned = (
5054              select pa2.date_earned
5055              from   pay_payroll_actions pa2,
5056                     pay_assignment_actions act
5057              where  act.assignment_action_id = l_locked_action_id
5058              and    pa2.payroll_action_id = act.payroll_action_id)
5059 
5060       where  pac.payroll_action_id =p_payroll_action_id;
5061 --
5062 
5063       p_assignment_action_id := l_locking_action_id;
5064       p_object_version_number := 1;
5065 --
5066    end qpppassact;
5067 --
5068    --------------------------- reversal ------------------------------
5069    /*
5070       NAME
5071          reversal - Process a reversal.
5072       DESCRIPTION
5073          Process a reversal for an assignment action.
5074       NOTES
5075          - This is called directly from the Reversal form.
5076            This fucntion reverses all appropriate run results.
5077            By 'appropriate', we mean all numeric unit of measure.
5078            This currently leads us to negate input values such as
5079            'rate' and so on, as there is currently no way of telling
5080            the reversal not to do so. This may be altered in a
5081            subsequent version.
5082          - The redo parameter indicates whether or not we need to
5083            insert an assignment action and interlock. If redo is
5084            set true, we do not need to. This was introduced to
5085            cope with the requirements for BackPay.
5086          - The multi flag indicates if we are calling this as part of
5087            the assignment set reversal procedure in which case we want to
5088            skip the initial setup part of this procedure
5089    */
5090    procedure reversal
5091    (
5092       pactid   in number,               -- payroll_action_id.
5093       assactid in number,               -- assignment_action_id to be reversed.
5094       redo     in boolean default false, -- ins assact and interlock if false
5095       rev_aaid in number  default 0,    -- reversal aa id of parent assactid
5096       multi    in boolean default false
5097    ) is
5098   --
5099   -- Cursors used for latest balance maintenance
5100   --
5101   cursor rev_rrs (revassactid number, p_si_needed varchar2, p_st_needed varchar2,
5102                   p_sn_needed varchar2, p_st2_needed varchar2,
5103                   p_sn2_needed varchar2, p_org_needed varchar2) is
5104   select prr.run_result_id,
5105          paa.tax_unit_id,
5106          prr.local_unit_id,
5107          prr.jurisdiction_code,
5108          prr.source_id original_entry_id,
5109          ppa.payroll_id,
5110          decode(p_si_needed,
5111               'Y', pay_balance_pkg.find_context('SOURCE_ID', prr.run_result_id),
5112               null)  source_id,
5113          decode(p_st_needed,
5114               'Y', pay_balance_pkg.find_context('SOURCE_TEXT', prr.run_result_id),
5115               null)  source_text,
5116          decode(p_sn_needed,
5117               'Y', pay_balance_pkg.find_context('SOURCE_NUMBER', prr.run_result_id),
5118               null)  source_number,
5119          decode(p_st2_needed,
5120               'Y', pay_balance_pkg.find_context('SOURCE_TEXT2', prr.run_result_id),
5121               null)  source_text2,
5122          decode(p_sn2_needed,
5123               'Y', pay_balance_pkg.find_context('SOURCE_NUMBER2', prr.run_result_id),
5124               null)  source_number2,
5125          decode(p_org_needed,
5126               'Y', pay_balance_pkg.find_context('ORGANIZATION_ID', prr.run_result_id),
5127               null)  organization_id,
5128          ppa.effective_date
5129     from pay_assignment_actions paa,
5130          pay_run_results        prr,
5131          pay_payroll_actions    ppa
5132    where paa.assignment_action_id = revassactid
5133      and ppa.payroll_action_id    = paa.payroll_action_id
5134      and paa.assignment_action_id = prr.assignment_action_id;
5135   --
5136   -- cursor and variables used in reversal of pay_run_balances
5137   --
5138     rev_asgact_id pay_assignment_actions.assignment_action_id%type;
5139     rev_act_seq   pay_assignment_actions.action_sequence%type;
5140     rev_eff_date  pay_payroll_actions.effective_date%type;
5141     --
5142       asgid number; -- assignment_id.
5143       taxunt number; -- tax unit id if legislation is US
5144       run_type_id number;
5145       udca      context_details;
5146       cxt_id number;
5147 
5148       found number;
5149       rev_assact number; -- reversal assignment_action_id.
5150       leg_code   per_business_groups_perf.legislation_code%type;
5151       l_process_path   pay_assignment_actions.process_path%type;
5152       l_found    boolean;
5153       l_inp_val_name pay_input_values_f.name%type;
5154       l_rev_lat_bal_maintenance boolean;
5155       l_rule_mode pay_legislation_rules.rule_mode%type;
5156       l_si_needed pay_legislation_rules.rule_mode%type;
5157       l_st_needed pay_legislation_rules.rule_mode%type;
5158       l_sn_needed pay_legislation_rules.rule_mode%type;
5159       l_sn2_needed pay_legislation_rules.rule_mode%type;
5160       l_org_needed pay_legislation_rules.rule_mode%type;
5161       l_st2_needed pay_legislation_rules.rule_mode%type;
5162       l_value     pay_action_parameters.parameter_value%type;
5163       l_tax_group hr_organization_information.org_information5%type;
5164    begin
5165       g_debug := hr_utility.debug_enabled;
5166       --
5167       -- To process the reversal, we need to
5168       -- obtain information about the assignment.
5169       -- Note : in addition, the following select
5170       -- will only return a row if the reversal
5171       -- effective date is equal to or later than
5172       -- the effective date of the assignment action
5173       -- being reversed, and that action is a legal
5174       -- action to be reversed.
5175       -- We also get the tax unit id (US only) which is
5176       -- required for the reversal assigment action.
5177       -- not part of assignment set reversal so perform normal logic
5178       begin
5179          if g_debug then
5180             hr_utility.trace('pactid: '||to_char(pactid));
5181             hr_utility.trace('assactid: '||to_char(assactid));
5182             hr_utility.set_location('hrassact.reversal',10);
5183          end if;
5184          select ac2.assignment_id,
5185                 ac2.tax_unit_id,
5186                 ac2.run_type_id,
5187                 pbg.legislation_code
5188          into   asgid, taxunt,run_type_id, leg_code
5189          from   pay_action_classifications acl,
5190                 pay_assignment_actions     ac2,
5191                 pay_payroll_actions        pa2,
5192                 per_business_groups_perf   pbg,
5193                 pay_payroll_actions        pac
5194          where  pac.payroll_action_id    = pactid
5195          and    pbg.business_group_id    = pac.business_group_id
5196          and    ac2.assignment_action_id = assactid
5197          and    pa2.payroll_action_id    = ac2.payroll_action_id
5198          and    acl.classification_name  = 'REVERSED'
5199          and    acl.action_type          = pa2.action_type
5200          and    pa2.effective_date      <= pac.effective_date;
5201       exception
5202          when no_data_found then
5203             hr_utility.set_message(801,'HR_7011_ACTION_ILLEGAL_REV');
5204             hr_utility.raise_error;
5205       end;
5206 --
5207      if not multi then
5208       -- If redo is true, we do not not need to insert
5209       -- either an assignment action or an interlock.
5210       -- the BackPay process needs this.
5211       if(not redo) then
5212          --
5213          -- start by inserting an assignment action row.
5214          hrassact.inassact_main(pactid,asgid,null,null,null,null,TRUE,taxunt,FALSE,run_type_id);
5215 --
5216             hr_utility.set_location('hrassact.reversal',10);
5217          -- Return the Reversal's assignment_action_id
5218          select act.assignment_action_id
5219          into   rev_assact
5220          from   pay_assignment_actions act
5221          where  act.payroll_action_id = pactid
5222          and    act.assignment_id     = asgid;
5223 --
5224          select pay_core_utils.get_process_path(assactid)
5225          into l_process_path
5226          from dual;
5227          -- need to set process path for reversals
5228          update pay_assignment_actions
5229          set process_path =l_process_path
5230          where assignment_action_id=rev_assact;
5231 
5232          -- insert an interlock row.
5233          -- Take this opportunity to do a specific check that we
5234          -- have not already reversed the assignment action.
5235          -- Do this by checking a reversal does not already lock
5236          -- this row.
5237          if g_debug then
5238             hr_utility.set_location('hrassact.reversal',20);
5239          end if;
5240          insert into pay_action_interlocks (
5241                 locking_action_id,
5242                 locked_action_id)
5243          select rev_assact,
5244                 assactid
5245          from   dual
5246          where  not exists (
5247                 select null
5248                 from   pay_assignment_actions ac2,
5249                        pay_payroll_actions    pa2,
5250                        pay_action_interlocks  pai
5251                 where  pai.locked_action_id     = assactid
5252                 and    ac2.assignment_action_id = pai.locking_action_id
5253                 and    pa2.payroll_action_id    = ac2.payroll_action_id
5254                 and    pa2.action_type          = 'V');
5255 --
5256          if(sql%rowcount = 0) then
5257             hr_utility.set_message(801,'HR_7013_ACTION_IS_REVERSED');
5258             hr_utility.raise_error;
5259          end if;
5260             hr_utility.set_location('hrassact.reversal',10);
5261 --
5262         -- insert lock to master action if this is a sub action
5263 
5264         insert into pay_action_interlocks (
5265                 locking_action_id,
5266                 locked_action_id)
5267         select rev_assact, paa1.assignment_action_id
5268         from pay_assignment_actions paa,pay_assignment_actions paa1
5269         where paa.assignment_action_id=assactid
5270         and paa1.payroll_action_id=paa.payroll_action_id
5271         and paa1.source_action_id is null
5272         and paa1.assignment_id=paa.assignment_id
5273         and paa1.assignment_action_id<>paa.assignment_action_id;
5274 
5275 
5276       else
5277          -- Return the Reversal's assignment_action_id
5278          select act.assignment_action_id
5279          into   rev_assact
5280          from   pay_assignment_actions act
5281          where  act.payroll_action_id = pactid
5282          and    act.assignment_id     = asgid;
5283 
5284            --create new interlock if needed
5285            insert into pay_action_interlocks
5286            (locking_action_id,locked_action_id)
5287            select rev_assact,assactid
5288            from dual where not exists (select 1
5289                                        from pay_action_interlocks
5290                                        where locking_action_id=rev_assact
5291                                        and locked_action_id=assactid);
5292 
5293 
5294       end if;
5295      else -- multi flag
5296        rev_assact := rev_aaid;
5297      end if;
5298 --
5299       --
5300       -- Now we need to process the reversal itself.
5301       -- We insert 'copies' of the run results and values
5302       -- from the run to be reversed, except we negate the
5303       -- values of result values that have numeric unit of
5304       -- measure. Note that there is no intelligence involved
5305       -- here, the routine does not know anything about
5306       -- what the values are being used for.
5307       -- Approach : select all the relevant run result
5308       -- rows and then insert the run result values
5309       -- for each one at a time.
5310       --
5311       -- Note the order by run run_result_id was introduced to
5312       -- ensure that the reversals results are in the same order
5313       -- as the reversed runs results.
5314       -- This slight change in behaviour will result in optimising
5315       -- the results of a retrocosting of a reversal.
5316                 --
5317                 -- Note - when the run result that is being reversed is
5318                 -- an indirect we change the source_type of the new
5319                 -- result to 'V' (#374389)
5320       declare
5321          cursor run_results(pactid number,assactid number) is
5322          select prr.run_result_id,         -- original run_result_id.
5323                 prr.element_type_id,
5324                 prr.entry_type,
5325                 prr.source_id,
5326                 decode(prr.source_type, 'I', 'V', 'R'),   -- source_type
5327                 prr.status,       ---'P'                   -- status
5328                 prr.jurisdiction_code,
5329                 prr.start_date,
5330                 prr.end_date,
5331                 prr.time_definition_id,
5332                 prr.entry_process_path
5333          from   pay_run_results        prr
5334          where  prr.assignment_action_id = assactid
5335          order by prr.run_result_id;
5336       --
5337          -- hold a run result record row.
5338          oldrrid   pay_run_results.run_result_id%type;
5339          etypid    pay_run_results.element_type_id%type;
5340          enttype   pay_run_results.entry_type%type;
5341          srcid     pay_run_results.source_id%type;
5342          stype     pay_run_results.source_type%type;
5343          stat      pay_run_results.status%type;
5344          v_jcode   pay_run_results.jurisdiction_code%type;
5345          start_date pay_run_results.start_date%type;
5346          end_date  pay_run_results.end_date%type;
5347          time_def_id  pay_run_results.time_definition_id%type;
5348          entrypath pay_run_results.entry_process_path%type;
5349 --
5350          -- format mask for result value
5351          mask_38_dec_places varchar2(100);
5352          l_src_iv    varchar2(30);
5353          l_src_num   varchar2(30);
5354          l_src_num2  varchar2(30);
5355          l_org_id_iv varchar2(30);
5356          l_iv_found  boolean;
5357          l_num_found boolean;
5358       begin
5359 
5360          -- intialise mask for a number with 35 decimal places
5361          -- fnd_number.number_to_canonical uses a mask with only 20 decimal places
5362          -- bug 2362454
5363          -- increased to 38 decimal places and redelieverd under bug 2587443
5364          mask_38_dec_places := 'FM999999999999999999999.99999999999999999999999999999999999999';
5365                                 --123456789012345678901 12345678901234567890123456789012345678
5366 
5367          --
5368          -- open the cursor.
5369          open run_results(pactid,assactid);
5370 --
5371          loop
5372             fetch run_results
5373             into oldrrid,etypid,enttype,srcid,stype,stat,v_jcode, start_date, end_date, time_def_id, entrypath;
5374 --
5375             exit when run_results%notfound;
5376 --
5377             -- we want to know if element type is
5378             -- date effective for Reversal Date.
5379             declare
5380                dummy number; -- need this for syntax.
5381             begin
5382                if g_debug then
5383                   hr_utility.set_location('hrassact.reversal',25);
5384                end if;
5385                select 1
5386                into   dummy
5387                from   pay_payroll_actions pac,
5388                       pay_element_types_f pet
5389                where  pac.payroll_action_id = pactid
5390                and    pet.element_type_id   = etypid
5391                and    pac.effective_date between
5392                       pet.effective_start_date and pet.effective_end_date;
5393                exception
5394                   when no_data_found then
5395                   hr_utility.set_message(801,'HR_7012_ACTION_ELE_NOT_EFF');
5396                   hr_utility.raise_error;
5397             end;
5398 --
5399             if g_debug then
5400                hr_utility.set_location('hrassact.reversal',30);
5401             end if;
5402 
5403             insert  into pay_run_results (
5404                     run_result_id,
5405                     element_type_id,
5406                     assignment_action_id,
5407                     entry_type,
5408                     source_id,
5409                     source_type,
5410                     status,
5411                     jurisdiction_code,
5412                     start_date,
5413                     end_date,
5414                     time_definition_id,
5415                     entry_process_path)
5416             values (pay_run_results_s.nextval,
5417                     etypid,
5418                     rev_assact, -- the reversal assignment action.
5419                     enttype,
5420                     oldrrid,
5421                     stype,
5422                     stat,
5423                     v_jcode,
5424                     start_date,
5425                     end_date,
5426                     time_def_id,
5427                     entrypath);
5428 --
5429             -- now we need to insert the result values
5430             -- for the result just inserted.
5431             -- NOTE - the first line in the decode is a
5432             -- dummy statement that prevents invalid
5433             -- number errors, caused by attempted
5434             -- implicit conversions from characters to
5435             -- numbers.
5436             --
5437             -- Bug 2822429 - Input values that represent the context SOURCE_ID
5438             -- or SOURCE_NUMBER must not have their run_result_value negated,
5439             -- as they are ids, not number values. Hence, where uom is I or N
5440             -- an extra decode is performed. If the piv.name is the SOURCE_ID
5441             -- or SOURCE_NUMBER input value, then multiply by 1, otherwise by
5442             -- -1 as before.
5443             -- Need to get input_value name for SOURCE_ID and SOURCE_NUMBER
5444             --
5445             hr_utility.trace('leg_code: '||leg_code);
5446             pay_core_utils.get_leg_context_iv_name('SOURCE_ID'
5447                                                ,leg_code
5448                                                ,l_src_iv
5449                                                ,l_iv_found);
5450             if (not l_iv_found) then
5451                l_src_iv := null;
5452             else
5453               l_si_needed := 'Y';
5454               hr_utility.trace('l_src_iv: '||l_src_iv);
5455             end if;
5456             --
5457             pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER'
5458                                                ,leg_code
5459                                                ,l_src_num
5460                                                ,l_num_found);
5461             if (not l_num_found) then
5462                l_src_num := null;
5463             else
5464                l_sn_needed := 'Y';
5465                hr_utility.trace('l_src_num: '||l_src_num);
5466             end if;
5467             --
5468             pay_core_utils.get_leg_context_iv_name('SOURCE_NUMBER2'
5469                                                ,leg_code
5470                                                ,l_src_num2
5471                                                ,l_num_found);
5472             if (not l_num_found) then
5473                l_src_num2 := null;
5474             else
5475                l_sn2_needed := 'Y';
5476                hr_utility.trace('l_src_num2: '||l_src_num2);
5477             end if;
5478 --
5479             pay_core_utils.get_leg_context_iv_name('ORGANIZATION_ID'
5480                                                ,leg_code
5481                                                ,l_org_id_iv
5482                                                ,l_num_found);
5483             if (not l_num_found) then
5484                l_org_id_iv := null;
5485             else
5486                l_org_needed := 'Y';
5487                hr_utility.trace('l_org_id_iv: '||l_org_id_iv);
5488             end if;
5489             --
5490             if g_debug then
5491                hr_utility.set_location('hrassact.reversal',35);
5492             end if;
5493             insert into pay_run_result_values (
5494                    input_value_id,
5495                    run_result_id,
5496                    result_value)
5497             select piv.input_value_id,
5498                    pay_run_results_s.currval,
5499                    decode(piv.uom,
5500                       'XXXXXXXXXX', '***********************',
5501                       'H_DECIMAL1', rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5502                       'H_DECIMAL2', rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5503                       'H_DECIMAL3', rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5504                       'H_HH'      , rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5505                       'H_HHMM'    , rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5506                       'H_HHMMSS'  , rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5507                       'I' , rtrim(to_char(decode(piv.name, l_src_iv , (1)
5508                                                          , l_src_num, (1)
5509                                                          , l_src_num2, (1)
5510                                                          , l_org_id_iv, (1)
5511                                                          , (-1))
5512                             * to_number(rrv.result_value, mask_38_dec_places)
5513                             , mask_38_dec_places), '.'),
5514                       'M'         , rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5515                       'N' , rtrim(to_char(decode(piv.name, l_src_iv , (1)
5516                                                          , l_src_num, (1)
5517                                                          , l_src_num2, (1)
5518                                                          , l_org_id_iv, (1)
5519                                                          , (-1))
5520                             * to_number(rrv.result_value, mask_38_dec_places)
5521                               , mask_38_dec_places), '.'),
5522                       'ND'        , rtrim(to_char((-1) * to_number(rrv.result_value, mask_38_dec_places), mask_38_dec_places), '.'),
5523                       rrv.result_value)
5524             from   pay_payroll_actions   pac,
5525                    pay_run_result_values rrv,
5526                    pay_input_values_f    piv
5527             where  pac.payroll_action_id = pactid
5528             and    rrv.run_result_id     = oldrrid
5529             and    piv.input_value_id    = rrv.input_value_id
5530             and    pac.effective_date between
5531                    piv.effective_start_date and piv.effective_end_date;
5532          end loop;
5533 --
5534          close run_results;
5535 --
5536          -- Now setup the action contexts, if there are any
5537         -- if not multi then
5538          insert into pay_action_contexts
5539                    (ASSIGNMENT_ACTION_ID,
5540                     ASSIGNMENT_ID,
5541                     CONTEXT_ID,
5542                     CONTEXT_VALUE)
5543          select distinct rev_assact,
5544                 pac.assignment_id,
5545                 pac.context_id,
5546                 pac.context_value
5547            from pay_action_contexts pac
5548           where pac.assignment_action_id = assactid;
5549         -- end if;
5550 --
5551          -- Added for fix 366215.
5552          -- Now we have inserted the negative run results for
5553          -- the Reversal itself, we call a procedure to create
5554          -- "pre-inserted" run results and values to allow the
5555          -- correct re-processing of element entries in a
5556          -- subsequent run.
5557          --hrassact.rev_pre_inserted_rr(pactid);
5558          --
5559          -- To signal that processing has been successfully
5560          -- completed, update the action_status to (C)omplete.
5561          hr_utility.set_location('hrassact.reversal',40);
5562          update pay_payroll_actions pac
5563          set    pac.action_status     = 'C'
5564          where  pac.payroll_action_id = pactid;
5565       end;
5566 --
5567 --
5568 -- Now do the reversal for pay_run_balances. 1st the asg level balances
5569 --
5570       if g_debug then
5571          hr_utility.set_location('hrassact.reversal',45);
5572       end if;
5573 --
5574    /* currently for multi reversal we are using pactid as the reversal
5575       pactid. If multi then the ass_act_id is the rev_act_id we want to
5576       use and is already set */
5577    if not multi then
5578 
5579    select paa.assignment_action_id
5580    ,      paa.action_sequence
5581    ,      ppa.effective_date
5582    into   rev_asgact_id
5583    ,      rev_act_seq
5584    ,      rev_eff_date
5585    from   pay_assignment_actions paa
5586    ,      pay_payroll_actions ppa
5587    where  paa.payroll_action_id = pactid
5588    and    paa.payroll_action_id = ppa.payroll_action_id;
5589 
5590    else
5591 
5592    select paa.assignment_action_id
5593    ,      paa.action_sequence
5594    ,      ppa.effective_date
5595    into   rev_asgact_id
5596    ,      rev_act_seq
5597    ,      rev_eff_date
5598    from   pay_assignment_actions paa,
5599           pay_payroll_actions ppa
5600    where  paa.assignment_action_id = assactid
5601    and    ppa.payroll_action_id = paa.payroll_action_id;
5602 
5603    end if;
5604 --
5605    pay_balance_pkg.maintain_balances_for_action(rev_assact);
5606 --
5607    if g_debug then
5608       hr_utility.set_location('Leaving: hrassact.reversal', 100);
5609    end if;
5610 end reversal;
5611 --
5612    ----------------- multi_assignment_reversal ------------------------------
5613    /*
5614       NAME
5615          multi_assignment_reversal - Process a reversal by assignment set.
5616       DESCRIPTION
5617          Process a reversal for an assignment action, version called via PYUGEN
5618          as opposed to that called via Reversal Form version.
5619          This version omits the assignment action creation that has been already
5620          prepared via the c code routines called as part of PYUGEN prior.
5621       NOTES
5622          - This is called directly from PYUGEN REVERSAL
5623    */
5624 procedure multi_assignment_reversal
5625 (
5626    pactid   in number,               -- payroll_action_id.
5627    assactid in number,               -- assignment_action_id to be reversed.
5628    rev_aaid in number                -- locking action
5629 ) is
5630 begin
5631   reversal(pactid,        -- call reversal with PYUGEN payroll_action_id
5632            assactid,      -- call reversal with PYUGEN assignment action id
5633            false,         -- redo flag set to false for reversal thru PYUGEN
5634            rev_aaid,      -- locking action id
5635            true           -- multi flag to indicate assignment set reversal
5636           );
5637 end multi_assignment_reversal;
5638 --
5639    ----------------------- rev_pre_inserted_rr --------------------------
5640    /* COMMENETED OUT AS THIS PROC IS NO LONGER USED
5641       NAME
5642          rev_pre_inserted_rr - Reversal create pre-inserted run results.
5643       DESCRIPTION
5644          Creates pre-inserted run results when a Reversal is processed
5645          These are created for any non-recurring or additional entry
5646          type for the assignment, where there is not already an
5647          unprocessed entry.
5648       NOTES
5649          <none>
5650    */
5651 /*   procedure rev_pre_inserted_rr
5652    (
5653       p_payroll_action_id in number    -- payroll_action_id of reversal.
5654    ) is
5655       cursor c1 is
5656       select + ORDERED
5657                  INDEX(PEE PAY_ELEMENT_ENTRIES_F_PK,
5658                        PEL PAY_ELEMENT_LINKS_F_PK,
5659                        PET PAY_ELEMENT_TYPES_F_PK,
5660                        ACT2 PAY_ASSIGNMENT_ACTIONS_PK)
5661                  USE_NL(PAC ACT PAI ACT2 RR2 PEE)
5662              pet.element_type_id,
5663              act.assignment_action_id,
5664              pee.entry_type,
5665              pee.element_entry_id
5666        from  pay_payroll_actions        pac,
5667              pay_assignment_actions    act,
5668              pay_action_interlocks      pai,
5669              pay_assignment_actions    act2,
5670              pay_run_results            rr2,
5671              pay_element_entries_f      pee,
5672              pay_element_links_f        pel,
5673              pay_element_types_f        pet
5674       where  pac.payroll_action_id = p_payroll_action_id
5675       and    act.payroll_action_id = pac.payroll_action_id
5676       and    pee.assignment_id     = act.assignment_id
5677       and    pai.locking_action_id = act.assignment_action_id
5678       and    pai.locked_action_id  = act2.assignment_action_id
5679       and    pac.date_earned between
5680              pee.effective_start_date and pee.effective_end_date
5681       and    pel.element_link_id   = pee.element_link_id
5682       and    pac.date_earned between
5683              pel.effective_start_date and pel.effective_end_date
5684       and    pet.element_type_id   = pel.element_type_id
5685       and    pac.date_earned between
5686              pet.effective_start_date and pet.effective_end_date
5687       and    ((pet.processing_type = 'N' and pee.entry_type <> 'B')
5688                or pee.entry_type = 'D')
5689       and    rr2.source_id            = pee.element_entry_id
5690       and    rr2.source_type  <> 'I'-- exclude indirects
5691       and    rr2.assignment_action_id = act2.assignment_action_id
5692       and    rr2.status               <> 'U'
5693       and    not exists (
5694              select null
5695              from   pay_run_results rr3
5696              where  rr3.source_id = pee.element_entry_id
5697              and    rr3.status    = 'U')
5698       order by pee.element_entry_id;
5699 --
5700       c_indent constant varchar2(30) := 'hrassact.rev_pre_inserted_rr';
5701    begin
5702       g_debug := hr_utility.debug_enabled;
5703       -- Insert un-processed run results and values as appropriate
5704       if g_debug then
5705          hr_utility.set_location(c_indent, 10);
5706       end if;
5707       for c1rec in c1 loop
5708          -- Start with insertion of run result.
5709          insert  into pay_run_results (
5710                  run_result_id,
5711                  element_type_id,
5712                  assignment_action_id,
5713                  entry_type,
5714                  source_id,
5715                  source_type,
5716                  status,
5717                  jurisdiction_code)
5718          values (pay_run_results_s.nextval,
5719                  c1rec.element_type_id,
5720                  c1rec.assignment_action_id,
5721                  c1rec.entry_type,
5722                  c1rec.element_entry_id,
5723                  'E',
5724                  'U',
5725                  null);
5726 --
5727          -- Now, insert the appropriate run result values.
5728          -- All the values should be null, the Payroll Run
5729          -- will fill these in later.
5730          insert into pay_run_result_values (
5731                 input_value_id,
5732                 run_result_id,
5733                 result_value)
5734          select pev.input_value_id,
5735                 pay_run_results_s.currval,
5736                 null
5737          from   pay_payroll_actions        pac,
5738                 pay_assignment_actions     act,   -- reversal
5739                 pay_element_entry_values_f pev
5740          where  act.assignment_action_id = c1rec.assignment_action_id --rev
5741          and    act.payroll_action_id   = pac.payroll_action_id
5742          and    pev.element_entry_id  = c1rec.element_entry_id
5743          and    pac.date_earned between
5744                 pev.effective_start_date and pev.effective_end_date;
5745       end loop;
5746    end rev_pre_inserted_rr;
5747 */
5748    -------------------------- ext_man_payment --------------------------
5749    /*
5750       NAME
5751          ext_man_payment - Performs External/Manual Payments
5752       DESCRIPTION
5753          Process a External/Manual Payment.
5754       NOTES
5755          This is called directly from the Pre-Payment External/Manual
5756          Payment form.
5757    */
5758    procedure ext_man_payment
5759    (
5760       p_payroll_id           in number,
5761       p_eff_date             in date,
5762       p_assignment_action_id in number,
5763       p_assignment_id        in number,
5764       p_comments             in varchar2,
5765       p_serial_number        in varchar2,
5766       p_pre_payment_id       in number,
5767       p_reason               in varchar2 default null
5768    ) is
5769       c_indent constant varchar2(30) := 'hrassact.ext_man_payment';
5770       l_payroll_action_id    number;
5771       l_assignment_action_id number;
5772       l_business_group_id    number;
5773       l_consolidation_set_id number;
5774      --
5775       cursor C_CON1 is
5776        select pay_payroll_actions_s.nextval,
5777               pay_assignment_actions_s.nextval,
5778               pa.CONSOLIDATION_SET_ID,
5779               pa.BUSINESS_GROUP_ID
5780        from   PAY_ASSIGNMENT_ACTIONS paa,
5781               PAY_PAYROLL_ACTIONS pa
5782        where  paa.ASSIGNMENT_ACTION_ID = p_assignment_action_id
5783        and    paa.PAYROLL_ACTION_ID    = pa.PAYROLL_ACTION_ID;
5784      --
5785    begin
5786      g_debug := hr_utility.debug_enabled;
5787      --
5788      -- Open the cursor and retrieve the payroll action details
5789      if g_debug then
5790         hr_utility.set_location(c_indent,5);
5791      end if;
5792      open C_CON1;
5793      fetch C_CON1 into l_payroll_action_id,
5794                        l_assignment_action_id,
5795                        l_consolidation_set_id,
5796                        l_business_group_id;
5797      close C_CON1;
5798      --
5799      -- insert the payroll action row
5800      if g_debug then
5801         hr_utility.set_location(c_indent,10);
5802      end if;
5803      insert into PAY_PAYROLL_ACTIONS
5804              (PAYROLL_ACTION_ID,
5805               ACTION_TYPE,
5806               BUSINESS_GROUP_ID,
5807               CONSOLIDATION_SET_ID,
5808               PAYROLL_ID,
5809               ACTION_POPULATION_STATUS,
5810               ACTION_STATUS,
5811               EFFECTIVE_DATE,
5812               COMMENTS,
5813               OBJECT_VERSION_NUMBER,
5814               PAY_ADVICE_MESSAGE)
5815          values
5816              (l_payroll_action_id,
5817               'E',
5818               l_business_group_id,
5819               l_consolidation_set_id,
5820               p_payroll_id,
5821               'C',
5822               'C',
5823               p_eff_date,
5824               p_comments,
5825               1,
5826               p_reason);
5827      --
5828      -- call procedure to insert assignment action record
5829      if g_debug then
5830         hr_utility.set_location(c_indent,20);
5831      end if;
5832      inassact(pactid           => l_payroll_action_id,
5833               asgid            => p_assignment_id,
5834               p_ass_action_seq => l_assignment_action_id,
5835               p_pre_payment_id => p_pre_payment_id,
5836               p_serial_number  => p_serial_number);
5837      --
5838      -- insert a pay action interlock record.
5839      if g_debug then
5840         hr_utility.set_location(c_indent,30);
5841      end if;
5842      insert into PAY_ACTION_INTERLOCKS
5843              (LOCKING_ACTION_ID,
5844               LOCKED_ACTION_ID)
5845      values
5846              (l_assignment_action_id,
5847               p_assignment_action_id);
5848 --
5849      -- Update the payroll action table with
5850      -- the appropriate date earned value.
5851      if g_debug then
5852         hr_utility.set_location(c_indent,40);
5853      end if;
5854      update pay_payroll_actions pac
5855         set    pac.date_earned = (
5856                select pa2.date_earned
5857                from   pay_payroll_actions    pa2,
5858                       pay_assignment_actions act
5859                where  act.assignment_action_id = p_assignment_action_id
5860                and    pa2.payroll_action_id    = act.payroll_action_id
5861 )
5862         where  pac.payroll_action_id      = l_payroll_action_id;
5863 --
5864    end ext_man_payment;
5865 --
5866 -- Added for bug 6820127
5867  -------------------------- ext_man_payment --------------------------
5868    /*
5869       NAME
5870          ext_man_payment - Performs External/Manual Payments
5871       DESCRIPTION
5872          Process a External/Manual Payment.
5873       NOTES
5874          This procedure is called from the executable PYEXMNPT within the 'Cancel Check' flow.
5875    */
5876    procedure ext_man_payment
5877    (
5878       p_errmsg               OUT NOCOPY VARCHAR2,
5879       p_errcode              OUT NOCOPY NUMBER,
5880       p_payroll_id           in number, -- payroll id of assign
5881       p_eff_date             in varchar2,   -- session date
5882       p_assignment_action_id in number, -- pre-payment assign action
5883       p_assignment_id        in number, -- assign id
5884       p_comments             in varchar2,-- comments
5885       p_serial_number        in varchar2,-- serial number
5886       p_pre_payment_id       in number,   -- pre-payment id
5887       p_reason               in varchar2 default null -- Reason
5888    )
5889    IS
5890    c_indent constant varchar2(30) := 'hrassact.ext_man_payment2';
5891 
5892    BEGIN
5893 
5894     g_debug := hr_utility.debug_enabled;
5895 
5896     if g_debug then
5897         hr_utility.set_location('Entering '||c_indent,10);
5898     end if;
5899 
5900      ext_man_payment(p_payroll_id,
5901                      TRUNC(FND_DATE.canonical_to_date(P_EFF_DATE)),
5902                      p_assignment_action_id,
5903                      p_assignment_id,
5904                      p_comments,
5905                      p_serial_number,
5906                      p_pre_payment_id,
5907                      p_reason);
5908 
5909     if g_debug then
5910         hr_utility.set_location('Leaving '||c_indent,20);
5911     end if;
5912 
5913    END ext_man_payment;
5914    --------------------------- set_action_contexts------------------------
5915    /*
5916       NAME
5917          set_action_contexts - This sets up the action contexts for a
5918                                given element entry.
5919       DESCRIPTION
5920       NOTES
5921    */
5922 procedure set_action_context (p_assact   in number,
5923                               p_rrid     in number, -- run_result_id
5924                               p_entry    in number,
5925                               p_tax_unit in number,
5926                               p_asgid    in number,
5927                               p_busgrp   in number,
5928                               p_legcode  in varchar2,
5929                               p_oentry   in number, -- original entry id
5930                               udca       out nocopy context_details
5931                              )
5932 is
5933 --
5934 cursor get_rr_values (p_run_result_id  number,
5935                       p_effective_date date
5936                      )
5937 is
5938 select piv.name,
5939        prrv.result_value,
5940        prrv.input_value_id,
5941        prrv.run_result_id
5942   from pay_run_results       prr,
5943        pay_run_result_values prrv,
5944        pay_input_values_f    piv
5945  where prr.run_result_id  = p_run_result_id
5946    and prrv.run_result_id = prr.run_result_id
5947    and piv.input_value_id = prrv.input_value_id
5948    and p_effective_date between piv.effective_start_date
5949                             and piv.effective_end_date;
5950 --
5951 cxt_id number;
5952 cnt    number;
5953 pay_id number;
5954 source_iv pay_legislation_rules.rule_type%type;
5955 source_text_iv pay_legislation_rules.rule_type%type;
5956 jurisdiction_iv pay_legislation_rules.rule_type%type;
5957 action_contexts boolean;
5958 l_def_jur_str        varchar2(2000);  -- used with dynamic pl/sql
5959 l_ctx_balance_date   date;
5960 l_ctx_time_def_id    number;
5961 sql_cursor           integer;
5962 l_rows               integer;
5963 dummy number;
5964 l_found              boolean;
5965 l_action_type        varchar2(30);
5966 l_effective_date     date;
5967       c_indent constant varchar2(32) := 'hrassact.set_action_context';
5968 --
5969 begin
5970    g_debug := hr_utility.debug_enabled;
5971 --
5972    if g_debug then
5973       hr_utility.set_location(c_indent,10);
5974    end if;
5975    udca.sz := 0;
5976    pay_core_utils.get_dynamic_contexts(p_busgrp,
5977                                        g_dynamic_contexts);
5978 --
5979    --
5980    -- Get Payroll ID and Action Type
5981    --
5982    select
5983       pac.payroll_id
5984      ,pac.action_type
5985      ,pac.effective_date
5986    into
5987       pay_id
5988      ,l_action_type
5989      ,l_effective_date
5990    from
5991       pay_payroll_actions pac
5992      ,pay_assignment_actions aa
5993    where
5994        aa.assignment_action_id = p_assact
5995    and pac.payroll_action_id = aa.payroll_action_id;
5996 
5997    --
5998    -- Do we need to perform the ACTION_CONTEXT code
5999    --
6000    begin
6001 --
6002       if g_debug then
6003          hr_utility.set_location(c_indent,20);
6004       end if;
6005       select 1
6006         into dummy
6007         from pay_legislation_rules plr
6008        where p_legcode = plr.legislation_code
6009          and plr.rule_type = 'ACTION_CONTEXTS'
6010          and plr.rule_mode = 'Y';
6011 --
6012       if g_debug then
6013          hr_utility.set_location(c_indent,30);
6014       end if;
6015       action_contexts := TRUE;
6016 --
6017    exception
6018       when no_data_found then
6019         hr_utility.set_location(c_indent,40);
6020         action_contexts := FALSE;
6021    end;
6022 --
6023      if g_debug then
6024         hr_utility.set_location(c_indent,50);
6025      end if;
6026      --
6027      -- OK get the contexts cached
6028      --
6029      if not contexts_cached then
6030        cache_contexts;
6031      end if;
6032      --
6033      -- Start setting up the contexts.
6034      --
6035      if (p_tax_unit is not null) then
6036         get_cache_context('TAX_UNIT_ID', cxt_id);
6037         udca.sz := udca.sz + 1;
6038         udca.assact_id(udca.sz) := p_assact;
6039         udca.asg_id(udca.sz)    := p_asgid;
6040         udca.cxt_id(udca.sz)    := cxt_id;
6041         udca.cxt_name(udca.sz)  := 'TAX_UNIT_ID';
6042         udca.cxt_value(udca.sz) := p_tax_unit;
6043         udca.valid(udca.sz)     := TRUE;
6044      end if;
6045      --
6046      -- Setting up the Balance Date and Time Definition ID contexts.
6047      --
6048      if p_rrid is not null then
6049         select end_date,
6050                time_definition_id
6051           into l_ctx_balance_date,
6052                l_ctx_time_def_id
6053           from pay_run_results
6054          where run_result_id = p_rrid;
6055 --
6056         if l_ctx_balance_date is not null then
6057            get_cache_context('BALANCE_DATE', cxt_id);
6058            udca.sz := udca.sz + 1;
6059            udca.assact_id(udca.sz) := p_assact;
6060            udca.asg_id(udca.sz)    := p_asgid;
6061            udca.cxt_id(udca.sz)    := cxt_id;
6062            udca.cxt_name(udca.sz)  := 'BALANCE_DATE';
6063            udca.cxt_value(udca.sz) := fnd_date.date_to_canonical(l_ctx_balance_date);
6064            udca.valid(udca.sz)     := TRUE;
6065         end if;
6066 --
6067         if l_ctx_time_def_id is not null then
6068            get_cache_context('TIME_DEFINITION_ID', cxt_id);
6069            udca.sz := udca.sz + 1;
6070            udca.assact_id(udca.sz) := p_assact;
6071            udca.asg_id(udca.sz)    := p_asgid;
6072            udca.cxt_id(udca.sz)    := cxt_id;
6073            udca.cxt_name(udca.sz)  := 'TIME_DEFINITION_ID';
6074            udca.cxt_value(udca.sz) := l_ctx_time_def_id;
6075            udca.valid(udca.sz)     := TRUE;
6076         end if;
6077 --
6078      end if;
6079 --
6080      for rrvrec in get_rr_values (p_rrid, l_effective_date) loop
6081        for l_cnt in 1..g_dynamic_contexts.count loop
6082 --
6083         hr_utility.set_location(c_indent,90);
6084 --
6085         --Run Result Contexts
6086         if (rrvrec.name = g_dynamic_contexts(l_cnt).input_value_name) then
6087 --
6088          hr_utility.set_location(c_indent,95);
6089          declare
6090           l_ctx_value pay_run_result_values.result_value%type;
6091          begin
6092           l_ctx_value := null;
6093 --
6094           if (rrvrec.result_value is not null) then
6095 --
6096             l_ctx_value := rrvrec.result_value;
6097 
6098           elsif l_action_type <> 'I' then
6099 --
6100             --
6101             -- setup defaults of contexts if not Balance Initialization
6102             --
6103             if g_dynamic_contexts(l_cnt).is_context_def = TRUE then
6104 
6105                l_def_jur_str := 'begin ' || g_dynamic_contexts(l_cnt).default_plsql || ' (';
6106                l_def_jur_str := l_def_jur_str || ':p_assact, ';
6107                l_def_jur_str := l_def_jur_str || ':p_entry, ';
6108                l_def_jur_str := l_def_jur_str || ':l_ctx_value); end; ';
6109                --
6110                sql_cursor := dbms_sql.open_cursor;
6111                --
6112                dbms_sql.parse(sql_cursor, l_def_jur_str, dbms_sql.v7);
6113                --
6114                --
6115                dbms_sql.bind_variable(sql_cursor, 'p_assact', p_assact);
6116                --
6117                dbms_sql.bind_variable(sql_cursor, 'p_entry', p_entry);
6118                --
6119                dbms_sql.bind_variable(sql_cursor, 'l_ctx_value', l_ctx_value, 30);
6120                --
6121                l_rows := dbms_sql.execute (sql_cursor);
6122                --
6123                if (l_rows = 1) then
6124                   dbms_sql.variable_value(sql_cursor, 'l_ctx_value',
6125                                           l_ctx_value);
6126                   dbms_sql.close_cursor(sql_cursor);
6127 --
6128                   -- OK we got the default, we need to set it
6129                   update pay_run_result_values
6130                      set result_value = l_ctx_value
6131                    where run_result_id = rrvrec.run_result_id
6132                      and input_value_id = rrvrec.input_value_id;
6133 --
6134                else
6135                   l_ctx_value := null;
6136                   dbms_sql.close_cursor(sql_cursor);
6137                end if;
6138             end if;
6139 --
6140           end if;
6141 
6142           hr_utility.set_location(c_indent,96);
6143 --
6144           if l_ctx_value is not null then
6145 --
6146             -- Set the jurisdiction on the run_result.
6147             if (g_dynamic_contexts(l_cnt).context_name = 'JURISDICTION_CODE') then
6148               update pay_run_results
6149               set jurisdiction_code      = l_ctx_value
6150               where run_result_id          = p_rrid;
6151 --
6152             elsif (g_dynamic_contexts(l_cnt).context_name = 'ORGANIZATION_ID') then
6153 --
6154               -- Need to ensure that its a Third Party Organization
6155 --
6156                   declare
6157                      l_org_id number;
6158                   begin
6159                      select organization_id
6160                        into l_org_id
6161                        from hr_organization_information hoi
6162                       where hoi.organization_id = l_ctx_value
6163                         and hoi.org_information_context = 'CLASS'
6164                         and hoi.org_information1 = 'HR_PAYEE';
6165 
6166                   exception
6167                      when no_data_found then
6168                        pay_core_utils.assert_condition('set_action_context:1',
6169                                             1 = 2);
6170                   end;
6171             end if;
6172 --
6173             if g_debug then
6174                hr_utility.set_location(c_indent,100);
6175             end if;
6176             get_cache_context(g_dynamic_contexts(l_cnt).context_name, cxt_id);
6177             udca.sz := udca.sz + 1;
6178             udca.assact_id(udca.sz) := p_assact;
6179             udca.asg_id(udca.sz)    := p_asgid;
6180             udca.cxt_name(udca.sz)  := g_dynamic_contexts(l_cnt).context_name;
6181             udca.cxt_id(udca.sz)    := cxt_id;
6182             udca.cxt_value(udca.sz) := l_ctx_value;
6183             udca.valid(udca.sz)     := TRUE;
6184           end if;
6185          end;
6186          if g_debug then
6187            hr_utility.set_location(c_indent,105);
6188          end if;
6189 --
6190         end if;
6191         if g_debug then
6192            hr_utility.set_location(c_indent,106);
6193         end if;
6194        end loop;
6195      end loop;
6196 
6197      --
6198      --   Payroll ID
6199      --
6200      if g_debug then
6201         hr_utility.set_location(c_indent,130);
6202      end if;
6203      get_cache_context('PAYROLL_ID', cxt_id);
6204      udca.sz := udca.sz + 1;
6205      udca.assact_id(udca.sz) := p_assact;
6206      udca.asg_id(udca.sz)    := p_asgid;
6207      udca.cxt_name(udca.sz)  := 'PAYROLL_ID';
6208      udca.cxt_id(udca.sz)    := cxt_id;
6209      udca.cxt_value(udca.sz) := pay_id;
6210      udca.valid(udca.sz)     := TRUE;
6211 
6212      --
6213      --   Original Entry ID
6214      --
6215      if p_oentry is not null then
6216        if g_debug then
6217           hr_utility.set_location(c_indent,135);
6218        end if;
6219 
6220        get_cache_context('ORIGINAL_ENTRY_ID', cxt_id);
6221        udca.sz := udca.sz + 1;
6222        udca.assact_id(udca.sz) := p_assact;
6223        udca.asg_id(udca.sz)    := p_asgid;
6224        udca.cxt_name(udca.sz)  := 'ORIGINAL_ENTRY_ID';
6225        udca.cxt_id(udca.sz)    := cxt_id;
6226        udca.cxt_value(udca.sz) := p_oentry;
6227        udca.valid(udca.sz)     := TRUE;
6228 
6229      end if;
6230      --
6231      --   Local Unit ID
6232      --
6233      declare
6234         l_local_unit_id number;
6235      begin
6236         select local_unit_id
6237           into l_local_unit_id
6238           from pay_run_results
6239          where run_result_id = p_rrid;
6240 --
6241         if l_local_unit_id is not null then
6242           if g_debug then
6243              hr_utility.set_location(c_indent,136);
6244           end if;
6245 
6246           get_cache_context('LOCAL_UNIT_ID', cxt_id);
6247           udca.sz := udca.sz + 1;
6248           udca.assact_id(udca.sz) := p_assact;
6249           udca.asg_id(udca.sz)    := p_asgid;
6250           udca.cxt_name(udca.sz)  := 'LOCAL_UNIT_ID';
6251           udca.cxt_id(udca.sz)    := cxt_id;
6252           udca.cxt_value(udca.sz) := l_local_unit_id;
6253           udca.valid(udca.sz)     := TRUE;
6254 
6255         end if;
6256      end;
6257 --
6258      if (p_legcode = 'US' or p_legcode = 'CA') then
6259 --
6260        if g_debug then
6261           hr_utility.set_location(c_indent,140);
6262        end if;
6263        declare
6264        tax_group hr_organization_information.org_information5%type;
6265        asg_id    number;
6266        pay_id    number;
6267        begin
6268 --
6269         if (p_legcode = 'US') then
6270          if g_debug then
6271             hr_utility.set_location(c_indent,150);
6272          end if;
6273          select hoi.org_information5,
6274                 paa.assignment_id
6275            into tax_group,
6276                 asg_id
6277            from hr_organization_information hoi,
6278                 pay_assignment_actions      paa
6279           where UPPER(hoi.org_information_context) = 'FEDERAL TAX RULES'
6280             and hoi.organization_id = paa.tax_unit_id
6281             and paa.assignment_action_id = p_assact
6282             and hoi.org_information5 is not null;
6283         else
6284          if g_debug then
6285             hr_utility.set_location(c_indent,153);
6286          end if;
6287          select hoi.org_information4,
6288                 paa.assignment_id
6289            into tax_group,
6290                 asg_id
6291            from hr_organization_information hoi,
6292                 pay_assignment_actions      paa
6293           where UPPER(hoi.org_information_context) = 'CANADA EMPLOYER IDENTIFICATION'
6294             and hoi.organization_id = paa.tax_unit_id
6295             and paa.assignment_action_id = p_assact
6296             and hoi.org_information4 is not null;
6297          end if;
6298 --
6299           if g_debug then
6300              hr_utility.set_location(c_indent,155);
6301           end if;
6302           get_cache_context('TAX_GROUP', cxt_id);
6303           udca.sz := udca.sz + 1;
6304           udca.assact_id(udca.sz) := p_assact;
6305           udca.asg_id(udca.sz)    := asg_id;
6306           udca.cxt_name(udca.sz)  := 'TAX_GROUP';
6307           udca.cxt_id(udca.sz)    := cxt_id;
6308           udca.cxt_value(udca.sz) := tax_group;
6309           udca.valid(udca.sz)     := TRUE;
6310 --
6311        exception
6312           when no_data_found then
6313              if g_debug then
6314                 hr_utility.set_location(c_indent,160);
6315              end if;
6316              null;
6317        end;
6318 --
6319      end if;
6320 --
6321      if g_debug then
6322         hr_utility.set_location(c_indent,170);
6323      end if;
6324      -- Now do all the inserts.
6325    if action_contexts = TRUE then
6326      for cnt in 1..udca.sz loop
6327 --
6328         if (udca.cxt_name(cnt) in ('SOURCE_ID'
6329                                   ,'JURISDICTION_CODE'
6330                                   ,'SOURCE_TEXT'
6331                                   ,'SOURCE_NUMBER'
6332                                   ,'SOURCE_TEXT2'
6333                                   ,'BALANCE_DATE'
6334                                   ,'TIME_DEFINITION_ID'
6335                                   ,'TAX_GROUP'
6336                                   ,'ORIGINAL_ENTRY_ID')) then
6337           if g_debug then
6338              hr_utility.set_location(c_indent,180);
6339           end if;
6340 --
6341           -- The row could already be in the table due
6342           -- to batch balance adjustments
6343           --
6344             insert into pay_action_contexts
6345                            (assignment_action_id,
6346                             assignment_id,
6347                             context_id,
6348                             context_value)
6349             select udca.assact_id(cnt),
6350                    udca.asg_id(cnt),
6351                    udca.cxt_id(cnt),
6352                    udca.cxt_value(cnt)
6353               from sys.dual
6354              where not exists (select ''
6355                                  from pay_action_contexts
6356                                 where assignment_action_id = udca.assact_id(cnt)
6357                                   and assignment_id = udca.asg_id(cnt)
6358                                   and context_id = udca.cxt_id(cnt)
6359                                   and context_value = udca.cxt_value(cnt));
6360         end if;
6361 --
6362      end loop;
6363    end if;
6364    if g_debug then
6365       hr_utility.set_location(c_indent,190);
6366    end if;
6367 exception
6368     when others then
6369       hr_utility.trace(sqlerrm);
6370       raise;
6371 --
6372 end set_action_context;
6373 --
6374    --------------------------- bal_adjust_actions ----------------------
6375    /*
6376       NAME
6377          bal_adjust_actions - perform balance adjustment.
6378       DESCRIPTION
6379          Process a balance adjustment.
6380       NOTES
6381    */
6382    procedure bal_adjust_actions
6383    (
6384       consetid in number,              -- consolidation_set_id.
6385       eentryid in number,              -- element_entry_id.
6386       effdate  in date,                -- effective_date of bal adjust.
6387       pyactid out nocopy number,              -- payroll action id.
6388       asactid out nocopy number,              -- assignment action id.
6389       act_type in varchar2 default 'B',-- payroll_action_type.
6390       prepay_flag in varchar2 default null, -- include in prepay process?
6391       taxunit  in number default null, -- tax unit id.
6392       purge_mode in boolean default false,  -- are we calling in purge mode?
6393       run_type_id in number default null
6394    ) is
6395       c_indent constant varchar2(32) := 'hrassact.bal_adjust_actions';
6396       pactid    number;  -- payroll_action_id.
6397       busgrp    number;  -- business_group_id.
6398       legcode   pay_legislation_rules.legislation_code%TYPE; -- leg code
6399       asgid     number;  -- assignment_id.
6400       payid     number;  -- payroll_id.
6401       tperiod   number;  -- time_period_id.
6402       dtearned  date;    -- date_earned.
6403       creatby   number;  -- created_by.
6404       creatdate date;    -- creation_date.
6405       assactid  number;  -- assignment_action_id of inserted action.
6406       tax_unit  number;  -- tax_unit_id
6407       udca      context_details;
6408       l_run_result_id         number;
6409       l_jc_name               varchar2(30);
6410       l_rr_sparse             boolean;
6411       l_rr_sparse_jc          boolean;
6412       l_found boolean;
6413       l_rule_mode varchar2(30);
6414       l_status    varchar2(30);
6415       l_original_entry_id     number;
6416       l_dummy varchar2(1);
6417 --
6418       cursor csr_time_period_leg (p_leg_code in varchar2) is
6419          select 'Y'
6420            from pay_legislation_rules
6421           where rule_type = 'TIME_PERIOD_ID'
6422             and legislation_code = p_leg_code
6423             and rule_mode = 'Y';
6424 --
6425    begin
6426       g_debug := hr_utility.debug_enabled;
6427       --
6428       --
6429       -- Select a number of values, including assignment
6430       -- and payroll action details.
6431       -- take this opportunity to check that the assignment
6432       -- and element type is date effective. In the case of
6433       -- the assignment, this should be confirming what has
6434       -- already been checked by the form.
6435       -- In addition, select a sequence value for
6436       -- pay_run_results.
6437       --
6438       -- Bug #3482270 - original entry id support.
6439       -- Original_entry_id will be also derived from the entry
6440       -- record here, which must have already been validated and
6441       -- attached to the record by the form or entry API before
6442       -- calling this process.
6443       --
6444       if g_debug then
6445          hr_utility.trace('effdate='||to_char(effdate, 'DD-MON-YYYY'));
6446          hr_utility.set_location(c_indent,5);
6447       end if;
6448       select /*+ ordered use_nl(pee asg ptp pbg)
6449                  index(pee PAY_ELEMENT_ENTRIES_F_PK)
6450                  index(asg PER_ASSIGNMENTS_F_PK)
6451                  index(ptp PER_TIME_PERIODS_N50) */
6452              pay_payroll_actions_s.nextval,
6453              asg.business_group_id,
6454              pbg.legislation_code,
6455              asg.assignment_id,
6456              asg.payroll_id,
6457              ptp.time_period_id,
6458              pee.created_by,
6459              pee.creation_date,
6460              pee.original_entry_id
6461       into   pactid,
6462              busgrp,
6463              legcode,
6464              asgid,
6465              payid,
6466              tperiod,
6467              creatby,
6468              creatdate,
6469              l_original_entry_id
6470       from   pay_element_entries_f pee,
6471              per_all_assignments_f asg,
6472              per_time_periods      ptp,
6473              per_business_groups_perf  pbg
6474       where  pee.element_entry_id = eentryid
6475       and    effdate between
6476              pee.effective_start_date and pee.effective_end_date
6477       and    asg.assignment_id    = pee.assignment_id
6478       and    effdate between
6479              asg.effective_start_date and asg.effective_end_date
6480       and    pbg.business_group_id = asg.business_group_id
6481       and    ptp.payroll_id       = asg.payroll_id
6482       and    effdate between
6483              ptp.start_date and ptp.end_date;
6484 --
6485       open csr_time_period_leg(legcode);
6486       fetch csr_time_period_leg into l_dummy;
6487 --
6488       if (csr_time_period_leg%found) then
6489          close csr_time_period_leg;
6490          select pt2.time_period_id, pt2.end_date
6491          into   tperiod, dtearned
6492          from   per_time_periods pt2
6493          where  pt2.time_period_id in
6494                 (select min(time_period_id)
6495                  from   per_time_periods ptp
6496                  where  ptp.payroll_id = payid
6497                  and    effdate between ptp.start_date
6498                                 and greatest(ptp.end_date, ptp.regular_payment_date));
6499       else
6500          close csr_time_period_leg;
6501          dtearned := effdate;
6502       end if;
6503 --
6504       -- insert payroll action row.
6505       if g_debug then
6506          hr_utility.set_location(c_indent,10);
6507       end if;
6508       insert  into pay_payroll_actions (
6509               payroll_action_id,
6510               action_type,
6511               business_group_id,
6512               consolidation_set_id,
6513               payroll_id,
6514               action_population_status,
6515               action_status,
6516               effective_date,
6517               date_earned,
6518               action_sequence,
6519               time_period_id,
6520               future_process_mode,
6521               created_by,
6522               creation_date,
6523               object_version_number)
6524       values (pactid,
6525               act_type,
6526               busgrp,
6527               consetid,
6528               payid,
6529               'C',
6530               'C',
6531               effdate,
6532               dtearned,
6533               pay_payroll_actions_s.nextval,
6534               tperiod,
6535               prepay_flag,
6536               creatby,
6537               creatdate,
6538               1);
6539 --
6540       -- now, we need to insert the assignment action.
6541       inassact_main(pactid,asgid,null,null,null,eentryid,
6542                     TRUE, taxunit,purge_mode,run_type_id);
6543 --
6544       --
6545       -- We now need to get the id of the inserted action,
6546       -- so that it can be used for update purposes.
6547       if g_debug then
6548          hr_utility.set_location(c_indent,15);
6549       end if;
6550       select act.assignment_action_id, act.tax_unit_id
6551       into   assactid, tax_unit
6552       from   pay_assignment_actions act
6553       where  act.payroll_action_id = pactid;
6554 --
6555       --
6556       -- we need to insert the run result row
6557       if g_debug then
6558          hr_utility.set_location(c_indent,20);
6559       end if;
6560 
6561 
6562       -- calc jur code name
6563         pay_core_utils.get_leg_context_iv_name
6564                       ('JURISDICTION_CODE',
6565                        legcode,
6566                        l_jc_name,
6567                        l_found);
6568 
6569         if (l_found = FALSE) then
6570           l_jc_name := 'Jurisdiction';
6571         end if;
6572 
6573 
6574         -- set rr sparse leg_rule
6575         pay_core_utils.get_legislation_rule('RR_SPARSE',
6576                                             legcode,
6577                                             l_rule_mode,
6578                                             l_found
6579                                            );
6580         if (l_found = FALSE) then
6581           l_rule_mode := 'N';
6582         end if;
6583 
6584         if upper(l_rule_mode)='Y'
6585         then
6586            -- Confirm Enabling Upgrade has been made by customer
6587            pay_core_utils.get_upgrade_status(busgrp,
6588                                     'ENABLE_RR_SPARSE',
6589                                     l_status);
6590 
6591            if upper(l_status)='N'
6592            then
6593               l_rule_mode := 'N';
6594            end if;
6595         end if;
6596 
6597         if upper(l_rule_mode)='Y'
6598         then
6599          l_rr_sparse:=TRUE;
6600         else
6601          l_rr_sparse :=FALSE;
6602         end if;
6603 --
6604        pay_core_utils.get_upgrade_status(busgrp,
6605                                     'RR_SPARSE_JC',
6606                                     l_status);
6607 --
6608         if upper(l_status)='Y'
6609         then
6610          l_rr_sparse_jc :=TRUE;
6611         else
6612          l_rr_sparse_jc :=FALSE;
6613         end if;
6614 
6615         -- create run result
6616         pay_run_result_pkg.create_run_result(
6617                             p_element_entry_id  => eentryid,
6618                             p_session_date      => effdate,
6619                             p_business_group_id => busgrp,
6620                             p_jc_name           => l_jc_name,
6621                             p_rr_sparse         => l_rr_sparse,
6622                             p_rr_sparse_jc      => l_rr_sparse_jc,
6623                             p_asg_action_id     => assactid,
6624                             p_run_result_id     => l_run_result_id);
6625 
6626 
6627 --
6628       if(sql%notfound) then
6629          if g_debug then
6630             hr_utility.trace('Update of pay_run_results has failed');
6631          end if;
6632          raise no_data_found;
6633       end if;
6634 --
6635       -- Update the element entry creator_id column
6636       -- with the assignment_action_id of balance
6637       -- adjustment action.
6638       if g_debug then
6639          hr_utility.set_location(c_indent,25);
6640       end if;
6641       update pay_element_entries_f pee
6642       set    pee.creator_id       = assactid
6643       where  pee.element_entry_id = eentryid;
6644 --
6645       if(sql%notfound) then
6646          if g_debug then
6647             hr_utility.trace('Update of pay_element_entries_f has failed');
6648          end if;
6649          raise no_data_found;
6650       end if;
6651 --
6652 --
6653       if (act_type = 'B') then
6654 
6655         -- Setup the action contexts
6656          set_action_context (assactid,
6657                              l_run_result_id,
6658                              eentryid,
6659                              tax_unit,
6660                              asgid,
6661                              busgrp,
6662                              legcode,
6663                              l_original_entry_id,
6664                              udca
6665                             );
6666 --
6667 --       Call to start of latest balance maintenance code
6668 --
6669          maintain_lat_bal(assactid => assactid,
6670                           rrid     => l_run_result_id,
6671                           eentryid => eentryid,
6672                           effdate  => effdate,
6673                           udca     => udca,
6674                           act_type => act_type);
6675 
6676       end if;
6677 
6678       --
6679       -- Perform balance adjustment for pay_run_balances, 1st for asg level
6680       -- run balances, then group run balances.
6681       --
6682       pay_balance_pkg.create_all_asg_balances(p_asgact_id => assactid);
6683       --
6684       pay_balance_pkg.create_all_group_balances(p_pact_id => pactid);
6685       --
6686 
6687 --
6688 --    Set out variables
6689       pyactid := pactid;
6690       asactid := assactid;
6691 --
6692    end bal_adjust_actions;
6693    --------------------------- bal_adjust ------------------------------
6694    /*
6695       NAME
6696          bal_adjust - perform balance adjustment.
6697       DESCRIPTION
6698          Process a balance adjustment.
6699       NOTES
6700          This is called directly from the Balance Adjustment form.
6701          This is used as a cover for bal_adjust_actions.
6702    */
6703    procedure bal_adjust
6704    (
6705       consetid in number, -- consolidation_set_id.
6706       eentryid in number, -- element_entry_id.
6707       effdate  in date,   -- effective_date of bal adjust.
6708       act_type in varchar2 default 'B', -- payroll_action_type.
6709       prepay_flag in varchar2 default null, -- include in prepay process?
6710       run_type_id in number default null,
6711       tax_unit_id in number default null
6712    ) is
6713    --
6714       l_pay_act_id  number;
6715       l_asg_act_id  number;
6716       c_indent constant varchar2(22) := 'hrassact.bal_adjust';
6717    begin
6718       g_debug := hr_utility.debug_enabled;
6719 --
6720       if g_debug then
6721          hr_utility.set_location(c_indent,10);
6722       end if;
6723       bal_adjust_actions(consetid, eentryid, effdate, l_pay_act_id,
6724                           l_asg_act_id, act_type, prepay_flag,tax_unit_id,false,run_type_id);
6725       if g_debug then
6726          hr_utility.set_location(c_indent,20);
6727       end if;
6728    end;
6729 --
6730    --------------------------- maintain_lat_bal ------------------------
6731    /*
6732       NAME
6733          maintain_lat_bal - maintenace of latest balances.
6734       DESCRIPTION
6735          Perform maintenace of latest balances within balance adjustment.
6736       NOTES
6737          This is called from the balance adjustment code above and from the
6738          batch balance adjustment code.
6739    */
6740 
6741    procedure maintain_lat_bal
6742    (
6743       assactid in number,  -- assignment_action_id of inserted action.
6744       rrid     in number,  -- run_result_id
6745       eentryid in number,  -- element_entry_id.
6746       effdate  in date,    -- effective_date of bal adjust.
6747       udca     in context_details, -- The UDCA
6748       act_type in varchar2 default 'B' -- payroll_action_type.
6749    ) is
6750    --
6751       --
6752       -- balance dimensions cache
6753       --
6754       type bal_dims_cache is record
6755       (dim_id                    number_tbl,
6756        dim_name                  varchar_80_tbl,
6757        feed_chk_type             varchar_tbl,
6758        feed_chk_code             varchar_80_tbl,
6759        exp_chk_lvl               varchar_tbl,
6760        exp_chk_code              varchar_80_tbl,
6761        sz                        number
6762       );
6763       --
6764       l_element_type_id          number;
6765       --
6766       -- cursor to get the run result values of the balance adjustment
6767       --
6768       -- Modified to retrieve result values with run_result_id.
6769       -- Bug 3482270.
6770       --
6771       cursor run_result_values is
6772       select rrv.input_value_id,
6773              rrv.result_value,
6774              rrv.run_result_id
6775       from   pay_run_result_values rrv
6776       where  rrv.run_result_id        = rrid
6777       and    rrv.result_value is not null;
6778 
6779      cursor fed_latest_balances(p_inp_val_id number,
6780                                 p_person_id  number,
6781                                 p_asg_id     number) is
6782       select plb.latest_balance_id,
6783              plb.assignment_action_id,
6784              plb.value,
6785              nvl(plb.expired_assignment_action_id, -9999) expired_assignment_action_id,
6786              nvl(plb.expired_value, -9999) expired_value,
6787              nvl(plb.prev_balance_value, -9999) prev_balance_value,
6788              nvl(plb.prev_assignment_action_id, -9999) prev_assignment_action_id,
6789              plb.expiry_date,
6790              plb.expired_date,
6791              plb.prev_expiry_date,
6792              pdb.balance_dimension_id,
6793              pdb.balance_type_id,
6794              pbf.scale
6795       from   pay_latest_balances plb,
6796              pay_defined_balances           pdb,
6797              pay_balance_feeds_f            pbf
6798       where  pbf.input_value_id      = p_inp_val_id
6799       and    effdate between pbf.effective_start_date
6800                          and pbf.effective_end_date
6801       and    pdb.balance_type_id     = pbf.balance_type_id
6802       and    plb.defined_balance_id = pdb.defined_balance_id
6803       and    plb.person_id          = p_person_id
6804       and    (   plb.assignment_id     = p_asg_id
6805               or plb.assignment_id is null)
6806       and    (   plb.process_group_id     = (select distinct parent_object_group_id
6807                                                from pay_object_groups pog
6808                                               where pog.source_id = p_asg_id
6809                                                 and pog.source_type = 'PAF')
6810               or plb.process_group_id is null);
6811 
6812       --
6813       -- cursor to get the assignment latest balances that may be fed
6814       -- ie pay_balance feeds_f (subject to feed checking)
6815       --
6816       cursor fed_assignment_balances(p_inp_val_id number,
6817                                      p_asgid      number) is
6818       select palb.latest_balance_id,
6819              palb.assignment_action_id,
6820              palb.value,
6821              nvl(palb.expired_assignment_action_id, -9999) expired_assignment_action_id,
6822              nvl(palb.expired_value, -9999) expired_value,
6823              nvl(palb.prev_balance_value, -9999) prev_balance_value,
6824              nvl(palb.prev_assignment_action_id, -9999) prev_assignment_action_id,
6825              pdb.balance_dimension_id,
6826              pdb.balance_type_id,
6827              pbf.scale
6828       from   pay_assignment_latest_balances palb,
6829              pay_defined_balances           pdb,
6830              pay_balance_feeds_f            pbf
6831       where  pbf.input_value_id      = p_inp_val_id
6832       and    effdate between pbf.effective_start_date
6833                          and pbf.effective_end_date
6834       and    pdb.balance_type_id     = pbf.balance_type_id
6835       and    palb.defined_balance_id = pdb.defined_balance_id
6836       and    palb.assignment_id      = p_asgid;
6837       --
6838       -- cursor to get the person latest balances that may be fed
6839       -- ie pay_balance feeds_f (subject to feed checking)
6840       --
6841       cursor fed_person_balances(p_inp_val_id number,
6842                                  p_person_id  number) is
6843       select pplb.latest_balance_id,
6844              pplb.assignment_action_id,
6845              pplb.value,
6846              nvl(pplb.expired_assignment_action_id, -9999) expired_assignment_action_id,
6847              nvl(pplb.expired_value, -9999) expired_value,
6848              nvl(pplb.prev_balance_value, -9999) prev_balance_value,
6849              nvl(pplb.prev_assignment_action_id, -9999) prev_assignment_action_id,
6850              pdb.balance_dimension_id,
6851              pdb.balance_type_id,
6852              pbf.scale
6853       from   pay_person_latest_balances pplb,
6854              pay_defined_balances       pdb,
6855              pay_balance_feeds_f        pbf
6856       where  pbf.input_value_id      = p_inp_val_id
6857       and    effdate between pbf.effective_start_date
6858                          and pbf.effective_end_date
6859       and    pdb.balance_type_id     = pbf.balance_type_id
6860       and    pplb.defined_balance_id = pdb.defined_balance_id
6861       and    pplb.person_id          = p_person_id;
6862 
6863       bal_dims bal_dims_cache;
6864 
6865       c_indent constant varchar2(30) := 'hrassact.maintain_lat_bal';
6866       pactid               pay_payroll_actions.payroll_action_id%TYPE;
6867       asgid                per_all_assignments_f.assignment_id%TYPE;
6868       bus_grp_id           per_all_assignments_f.business_group_id%TYPE;
6869       l_person_id          per_all_assignments_f.person_id%TYPE;
6870       cxt_id               number;
6871       tax_unit_id          number;
6872       bal_dim_name         pay_balance_dimensions.dimension_name%TYPE;
6873       balance_expiry_code  pay_balance_dimensions.expiry_checking_code%TYPE;
6874       balance_expiry_level pay_balance_dimensions.expiry_checking_level%TYPE;
6875       bal_context_string   varchar2(2000);
6876       bal_fed              boolean;
6877       l_change_flag        boolean;
6878       l_rule_mode          pay_legislation_rules.rule_mode%TYPE;
6879       not_supported        boolean;
6880       l_status             varchar2(30);
6881       dummy_date           date;
6882 
6883       --
6884       -- Name : create_context_string
6885       --
6886       -- Get context values and also returns context string that is passed
6887       -- to feed and expiry checking if the expiry level is A (Assignment Action
6888       -- Level) or D (Date Level).
6889       --
6890       function get_contexts(udca in context_details
6891       ) return varchar2 is
6892       --
6893          l_context_string varchar2(2000);
6894       --
6895          c_indent constant varchar2(35) := 'hrassact.create_context_string';
6896       begin
6897          --
6898          if g_debug then
6899             hr_utility.set_location(c_indent,10);
6900          end if;
6901          --
6902          l_context_string := '';
6903          for cnt in 1..udca.sz loop
6904 --
6905             l_context_string := l_context_string||udca.cxt_name(cnt)||
6906                                 '='||udca.cxt_value(cnt)||' ';
6907 --
6908          end loop;
6909          return(l_context_string);
6910 
6911       end get_contexts;
6912       --
6913       -- Name : proc_feed_check - Execute balance feeding procedure
6914       --
6915       -- Returns TRUE if balance is to be fed
6916       --
6917       -- The pl/sql function name that checks whether the balance is fed is
6918       -- passed to this routine as 'p_feed_checking_code'.  The call to the
6919       -- function is done using dynamic pl/sql.
6920       --
6921       function proc_feed_check
6922       (
6923          p_feed_checking_code  in     varchar2,  -- feed checking procedure name
6924          p_dimension_name      in     varchar2,  -- dimension name
6925          p_bal_context_str     in     varchar2   -- list of context values.
6926       ) return boolean is
6927          l_feed_balance boolean := FALSE;
6928       --
6929          c_indent constant varchar2(35) := 'hrassact.proc_feed_check';
6930          l_feed_chk_str    varchar2(2000);  -- used with dynamic pl/sql
6931          sql_cursor        integer;
6932          l_rows            integer;
6933          l_feed_flag       integer;
6934       --
6935       begin
6936          --
6937          if g_debug then
6938             hr_utility.set_location (c_indent, 1);
6939             hr_utility.trace ('Feed checking code = ' || p_feed_checking_code);
6940             hr_utility.trace ('context string = ' || p_bal_context_str);
6941          end if;
6942          --
6943          -- we build up the sql string to call the balance
6944          -- feed checking pl/sql procedure:
6945          --
6946          l_feed_chk_str := 'begin ' || p_feed_checking_code || ' (';
6947          l_feed_chk_str := l_feed_chk_str || ':pactid, ';
6948          l_feed_chk_str := l_feed_chk_str || ':assactid, ';
6949          l_feed_chk_str := l_feed_chk_str || ':asgid, ';
6950          l_feed_chk_str := l_feed_chk_str || ':effdate, ';
6951          l_feed_chk_str := l_feed_chk_str || ':p_dimension_name, ';
6952          l_feed_chk_str := l_feed_chk_str || ':p_bal_context_str, ';
6953          l_feed_chk_str := l_feed_chk_str || ':l_feed_flag); end;';
6954          --
6955          -- now execute the SQL statement using dynamic pl/sql:
6956          --
6957          -- Dynamic sql steps:
6958          -- ==================
6959          -- 1. Open dynamic sql cursor
6960          -- 2. Parse dynamic sql
6961          -- 3. bind variables
6962          -- 4. Execute dynamic sql
6963          -- 5. Get the variable value (providing there are rows returned)
6964          -- 6. Close the dynamic sql cursor
6965          --
6966          if g_debug then
6967             hr_utility.set_location (c_indent, 20);
6968          end if;
6969          sql_cursor := dbms_sql.open_cursor;                      -- step 1
6970          --
6971          if g_debug then
6972             hr_utility.set_location (c_indent, 25);
6973          end if;
6974          dbms_sql.parse(sql_cursor, l_feed_chk_str, dbms_sql.v7); -- step 2
6975          --
6976          if g_debug then
6977             hr_utility.set_location (c_indent, 30);
6978          end if;
6979          dbms_sql.bind_variable(sql_cursor, 'pactid', pactid);    -- step 3:
6980          --
6981          dbms_sql.bind_variable(sql_cursor, 'assactid', assactid);
6982          --
6983          dbms_sql.bind_variable(sql_cursor, 'asgid', asgid);
6984          --
6985          dbms_sql.bind_variable(sql_cursor, 'effdate', effdate);
6986          --
6987          dbms_sql.bind_variable(sql_cursor, 'p_dimension_name', p_dimension_name);
6988          --
6989          dbms_sql.bind_variable(sql_cursor, 'p_bal_context_str', p_bal_context_str);
6990          --
6991          dbms_sql.bind_variable(sql_cursor, 'l_feed_flag', l_feed_flag);
6992          --
6993          if g_debug then
6994             hr_utility.set_location (c_indent, 35);
6995          end if;
6996          l_rows := dbms_sql.execute (sql_cursor);                 -- step 4
6997          --
6998          if (l_rows = 1) then
6999             if g_debug then
7000                hr_utility.set_location (c_indent, 40);
7001             end if;
7002             dbms_sql.variable_value(sql_cursor, 'l_feed_flag',   -- step 5
7003                                                 l_feed_flag);
7004             --
7005             if l_feed_flag = 1 then
7006                l_feed_balance := TRUE;
7007             end if;
7008             --
7009             if g_debug then
7010                hr_utility.set_location (c_indent, 45);
7011             end if;
7012             dbms_sql.close_cursor(sql_cursor);                   -- step 6
7013          else
7014             --
7015             -- None or more than 1 row has been returned. We must error as package
7016             -- call can only return 1 row, so this condition should never occur !
7017             --
7018             if g_debug then
7019                hr_utility.set_location (c_indent, 111);
7020             end if;
7021             dbms_sql.close_cursor(sql_cursor);
7022             hr_utility.raise_error;
7023          end if;
7024          --
7025          return(l_feed_balance);
7026          --
7027       end proc_feed_check;
7028       --
7029       -- Name : context_match_check - Perform context match feed checking
7030       --
7031       -- Returns TRUE if balance is to be fed
7032       --
7033       -- Performs matching check between latest balance context values
7034       -- and context values held inthe udca.  Type of checking on
7035       -- jurisdiction_code context is dependant upon p_feed_checking_type.
7036       --
7037       function lb_context_match_check
7038       (
7039          p_lat_bal_id          in  number,   -- latest balance_id
7040          p_feed_checking_type  in  varchar2, -- feed checking type ie 'E' or 'J'
7041          p_bal_type_id         in  number,   -- balance type id
7042          udca                  in  context_details
7043       ) return boolean is
7044          l_feed_flag boolean;
7045          l_match     boolean;
7046 
7047          c_indent constant varchar2(35) := 'hrassact.lb_context_match_check';
7048          l_jurisdiction_level pay_balance_types.jurisdiction_level%TYPE;
7049          cnt      number;
7050          l_tax_unit_id number;
7051          l_jurisdiction_code pay_run_result_values.result_value%type;
7052          l_original_entry_id number;
7053          l_source_id number;
7054          l_source_text pay_run_result_values.result_value%type;
7055          l_source_text2 pay_run_result_values.result_value%type;
7056          l_source_number number;
7057          l_source_number2 number;
7058          l_tax_group pay_run_result_values.result_value%type;
7059          l_local_unit_id number;
7060          l_organization_id number;
7061          l_payroll_id number;
7062       --
7063       begin
7064          --
7065          select tax_unit_id,
7066 		jurisdiction_code,
7067 		original_entry_id,
7068 		source_id,
7069 		source_text,
7070 		source_text2,
7071 		source_number,
7072 		source_number2,
7073 		tax_group,
7074 		payroll_id,
7075                 local_unit_id,
7076                 organization_id
7077          into l_tax_unit_id,
7078               l_jurisdiction_code,
7079               l_original_entry_id,
7080               l_source_id,
7081               l_source_text,
7082               l_source_text2,
7083               l_source_number,
7084               l_source_number2,
7085               l_tax_group,
7086               l_payroll_id,
7087               l_local_unit_id,
7088               l_organization_id
7089          from pay_latest_balances
7090          where p_lat_bal_id = latest_balance_id;
7091 
7092          if g_debug then
7093             hr_utility.set_location (c_indent, 1);
7094          end if;
7095 
7096          l_feed_flag := TRUE;
7097          if udca.sz = 0 then
7098              l_feed_flag := FALSE;
7099          else
7100 
7101            if (l_jurisdiction_code is not null) then
7102              l_match := FALSE;
7103              for cnt in 1..udca.sz loop
7104 		if (udca.cxt_name(cnt) = 'JURISDICTION_CODE') then
7105                   l_match := TRUE;
7106                   select nvl(jurisdiction_level, 0)
7107                   into   l_jurisdiction_level
7108                   from   pay_balance_types
7109                   where  balance_type_id = p_bal_type_id;
7110                    if substr(udca.cxt_value(cnt), 1, l_jurisdiction_level) <>
7111                              l_jurisdiction_code then
7112                          -- jurisdiction_code to required level does not match
7113                         l_feed_flag := FALSE;
7114 			exit;
7115                    end if;
7116                 end if;
7117              end loop;
7118 
7119              if (l_feed_flag=FALSE or
7120                  l_match=FALSE) then
7121               return FALSE;
7122              end if;
7123            end if;
7124 
7125            if (l_tax_unit_id is not null) then
7126              l_match := FALSE;
7127              for cnt in 1..udca.sz loop
7128                 if  udca.cxt_name(cnt) = 'TAX_UNIT_ID' then
7129                   l_match := TRUE;
7130                   if  udca.cxt_value(cnt)<>to_char(l_tax_unit_id) then
7131                         l_feed_flag := FALSE;
7132 			exit;
7133                   end if;
7134                 end if;
7135              end loop;
7136 
7137              if (l_feed_flag=FALSE or
7138                  l_match=FALSE) then
7139               return FALSE;
7140              end if;
7141            end if;
7142 
7143            if (l_original_entry_id is not null) then
7144              l_match := FALSE;
7145              for cnt in 1..udca.sz loop
7146                 if  udca.cxt_name(cnt) = 'ORIGINAL_ENTRY_ID' then
7147                   l_match := TRUE;
7148                   if  udca.cxt_value(cnt)<>to_char(l_original_entry_id) then
7149                         l_feed_flag := FALSE;
7150                         exit;
7151                   end if;
7152                 end if;
7153              end loop;
7154 
7155              if (l_feed_flag=FALSE or
7156                  l_match=FALSE) then
7157               return FALSE;
7158              end if;
7159            end if;
7160 
7161            if (l_source_id is not null) then
7162              l_match := FALSE;
7163              for cnt in 1..udca.sz loop
7164                 if  udca.cxt_name(cnt) = 'SOURCE_ID' then
7165                   l_match := TRUE;
7166                   if  udca.cxt_value(cnt)<>to_char(l_source_id) then
7167                         l_feed_flag := FALSE;
7168                         exit;
7169                   end if;
7170                 end if;
7171              end loop;
7172 
7173              if (l_feed_flag=FALSE or
7174                  l_match=FALSE) then
7175               return FALSE;
7176              end if;
7177            end if;
7178 
7179            if (l_source_text is not null) then
7180              l_match := FALSE;
7181              for cnt in 1..udca.sz loop
7182                 if  udca.cxt_name(cnt) = 'SOURCE_TEXT' then
7183                   l_match := TRUE;
7184                   if  udca.cxt_value(cnt)<>l_source_text then
7185                         l_feed_flag := FALSE;
7186                         exit;
7187                   end if;
7188                 end if;
7189              end loop;
7190 
7191              if (l_feed_flag=FALSE or
7192                  l_match=FALSE) then
7193               return FALSE;
7194              end if;
7195            end if;
7196 
7197            if (l_source_text2 is not null) then
7198              l_match := FALSE;
7199              for cnt in 1..udca.sz loop
7200                 if  udca.cxt_name(cnt) = 'SOURCE_TEXT2' then
7201                   l_match := TRUE;
7202                   if  udca.cxt_value(cnt)<>l_source_text2 then
7203                         l_feed_flag := FALSE;
7204                         exit;
7205                   end if;
7206                 end if;
7207              end loop;
7208 
7209              if (l_feed_flag=FALSE or
7210                  l_match=FALSE) then
7211               return FALSE;
7212              end if;
7213            end if;
7214 
7215            if (l_source_number is not null) then
7216              l_match := FALSE;
7217              for cnt in 1..udca.sz loop
7218                 if  udca.cxt_name(cnt) = 'SOURCE_NUMBER' then
7219                   l_match := TRUE;
7220                   if  udca.cxt_value(cnt)<>to_char(l_source_number) then
7221                         l_feed_flag := FALSE;
7222                         exit;
7223                   end if;
7224                 end if;
7225               end loop;
7226 
7227              if (l_feed_flag=FALSE or
7228                  l_match=FALSE) then
7229               return FALSE;
7230              end if;
7231            end if;
7232 
7233            if (l_source_number2 is not null) then
7234              l_match := FALSE;
7235              for cnt in 1..udca.sz loop
7236                 if  udca.cxt_name(cnt) = 'SOURCE_NUMBER2' then
7237                   l_match := TRUE;
7238                   if  udca.cxt_value(cnt)<>to_char(l_source_number2) then
7239                         l_feed_flag := FALSE;
7240                         exit;
7241                   end if;
7242                 end if;
7243              end loop;
7244 
7245              if (l_feed_flag=FALSE or
7246                  l_match=FALSE) then
7247               return FALSE;
7248              end if;
7249            end if;
7250 
7251            if (l_local_unit_id is not null) then
7252              l_match := FALSE;
7253              for cnt in 1..udca.sz loop
7254                 if  udca.cxt_name(cnt) = 'LOCAL_UNIT_ID' then
7255                   l_match := TRUE;
7256                   if  udca.cxt_value(cnt)<>to_char(l_local_unit_id) then
7257                         l_feed_flag := FALSE;
7258                         exit;
7259                   end if;
7260                 end if;
7261              end loop;
7262 
7263              if (l_feed_flag=FALSE or
7264                  l_match=FALSE) then
7265               return FALSE;
7266              end if;
7267            end if;
7268 
7269            if (l_organization_id is not null) then
7270              l_match := FALSE;
7271              for cnt in 1..udca.sz loop
7272                 if  udca.cxt_name(cnt) = 'ORGANIZATION_ID' then
7273                   l_match := TRUE;
7274                   if  udca.cxt_value(cnt)<>to_char(l_organization_id) then
7275                         l_feed_flag := FALSE;
7276                         exit;
7277                   end if;
7278                 end if;
7279              end loop;
7280 
7281              if (l_feed_flag=FALSE or
7282                  l_match=FALSE) then
7283               return FALSE;
7284              end if;
7285            end if;
7286 
7287            if (l_tax_group is not null) then
7288              l_match := FALSE;
7289              for cnt in 1..udca.sz loop
7290                 if  udca.cxt_name(cnt) = 'TAX_GROUP' then
7291                   l_match := TRUE;
7292                   if  udca.cxt_value(cnt)<>l_tax_group then
7293                         l_feed_flag := FALSE;
7294                         exit;
7295                   end if;
7296                 end if;
7297              end loop;
7298 
7299              if (l_feed_flag=FALSE or
7300                  l_match=FALSE) then
7301               return FALSE;
7302              end if;
7303            end if;
7304 
7305            if (l_payroll_id is not null) then
7306              l_match := FALSE;
7307              for cnt in 1..udca.sz loop
7308                 if  udca.cxt_name(cnt) = 'PAYROLL_ID' then
7309                   l_match := TRUE;
7310                   if  udca.cxt_value(cnt)<>to_char(l_payroll_id) then
7311                         l_feed_flag := FALSE;
7312                         exit;
7313                   end if;
7314                 end if;
7315              end loop;
7316 
7317              if (l_feed_flag=FALSE or
7318                  l_match=FALSE) then
7319               return FALSE;
7320              end if;
7321            end if;
7322 
7323          end if;
7324 
7325          if p_feed_checking_type = 'J' then
7326 
7327             for cnt in 1..udca.sz loop
7328                if udca.cxt_name(cnt) = 'JURISDICTION_CODE' then
7329                   l_feed_flag := FALSE;
7330                   exit;
7331                end if;
7332             end loop;
7333 
7334          end if;
7335          return(l_feed_flag);
7336          --
7337       end lb_context_match_check;
7338 
7339       function context_match_check
7340       (
7341          p_lat_bal_id          in  number,   -- latest balance_id
7342          p_feed_checking_type  in  varchar2, -- feed checking type ie 'E' or 'J'
7343          p_bal_type_id         in  number,   -- balance type id
7344          udca                  in  context_details
7345       ) return boolean is
7346          l_feed_flag boolean;
7347       --
7348          cursor balance_context_values is
7349          select context_id,
7350                 value
7351          from pay_balance_context_values
7352          where latest_balance_id = p_lat_bal_id;
7353 
7354          c_indent constant varchar2(35) := 'hrassact.context_match_check';
7355          l_jurisdiction_level pay_balance_types.jurisdiction_level%TYPE;
7356          cnt      number;
7357       --
7358       begin
7359          --
7360          if g_debug then
7361             hr_utility.set_location (c_indent, 1);
7362          end if;
7363          --
7364          l_feed_flag := TRUE;
7365 
7366          for bcv in balance_context_values loop
7367 
7368             if udca.sz = 0 then
7369                l_feed_flag := FALSE;
7370             else
7371                for cnt in 1..udca.sz loop
7372 
7373                   if udca.cxt_id(cnt) = bcv.context_id then
7374 
7375                      -- have to deal with jurisdiction code context
7376                      -- as a special case
7377                      if udca.cxt_name(cnt) = 'JURISDICTION_CODE' then
7378 
7379                           select nvl(jurisdiction_level, 0)
7380                           into   l_jurisdiction_level
7381                           from   pay_balance_types
7382                           where  balance_type_id = p_bal_type_id;
7383 
7384                           if substr(udca.cxt_value(cnt), 1, l_jurisdiction_level) <>
7385                              bcv.value then
7386                              -- jurisdiction_code to required level does not match
7387                              l_feed_flag := FALSE;
7388                           end if;
7389 
7390                      elsif udca.cxt_value(cnt) <> bcv.value then
7391                         -- balance context value doesn't match udca value
7392                         l_feed_flag := FALSE;
7393                      end if;
7394 
7395                      exit;
7396 
7397                   elsif cnt = udca.sz then
7398 
7399                      -- balance context not in udca
7400                      l_feed_flag := FALSE;
7401 
7402                   end if;
7403 
7404                end loop;
7405             end if;
7406 
7407          end loop;
7408 
7409          --
7410          if g_debug then
7411             hr_utility.set_location (c_indent, 20);
7412          end if;
7413          --
7414          -- jurisdiction_code check for 'J' feed checking type whereby
7415          -- jurisdiction_code must be undefined in the udca
7416          if p_feed_checking_type = 'J' then
7417 
7418             for cnt in 1..udca.sz loop
7419                if udca.cxt_name(cnt) = 'JURISDICTION_CODE' then
7420                   l_feed_flag := FALSE;
7421                   exit;
7422                end if;
7423             end loop;
7424 
7425          end if;
7426 
7427          return(l_feed_flag);
7428          --
7429       end context_match_check;
7430       --
7431       -- Name : feed_check - Function to control feed checking
7432       --                     on a particular latest balance
7433       --
7434       -- Returns TRUE if balance is to be fed
7435       --
7436       function feed_check
7437       (
7438          lat_bal_id      in  number,   -- latest balance_id
7439          lat_bal_type    in  varchar2, -- latest balance type (ie A or P or L)
7440          bal_dim_id      in  number,   -- balance dimension id
7441          bal_type_id     in  number,   -- balance type id
7442          bal_dim_name    in out nocopy varchar2, -- balance dimension name
7443          bal_exp_code    in out nocopy varchar2, -- balance dimension expiry code
7444          bal_exp_level   in out nocopy varchar2, -- balance dimension expiry level
7445          bal_context_str in  varchar2, -- balance dimension contexts string
7446          udca            in context_details
7447       ) return boolean is
7448          feed_balance boolean;
7449       --
7450          c_indent constant varchar2(30) := 'hrassact.feed_check';
7451          feed_checking_type pay_balance_dimensions.feed_checking_type%TYPE;
7452          feed_checking_code pay_balance_dimensions.feed_checking_code%TYPE;
7453          dim_num number;
7454          found   boolean := FALSE;
7455       --
7456       begin
7457          --
7458          if g_debug then
7459             hr_utility.set_location (c_indent, 10);
7460          end if;
7461          --
7462          -- get balance dimension info for the latest balance
7463          --
7464          -- check cache first
7465          --
7466          for dim_num in 1..bal_dims.sz loop
7467             if bal_dims.dim_id(dim_num) = bal_dim_id then
7468                bal_dim_name       := bal_dims.dim_name(dim_num);
7469                feed_checking_type := bal_dims.feed_chk_type(dim_num);
7470                feed_checking_code := bal_dims.feed_chk_code(dim_num);
7471                bal_exp_code       := bal_dims.exp_chk_code(dim_num);
7472                bal_exp_level      := bal_dims.exp_chk_lvl(dim_num);
7473                found              := TRUE;
7474             end if;
7475          end loop;
7476 
7477          if found = FALSE then
7478 
7479             select pbd.dimension_name,
7480                    pbd.feed_checking_type,
7481                    pbd.feed_checking_code,
7482                    pbd.expiry_checking_code,
7483                    pbd.expiry_checking_level
7484             into   bal_dim_name,
7485                    feed_checking_type,
7486                    feed_checking_code,
7487                    bal_exp_code,
7488                    bal_exp_level
7489             from  pay_balance_dimensions pbd
7490             where pbd.balance_dimension_id = bal_dim_id;
7491 
7492             -- put into cache
7493             bal_dims.sz                         := bal_dims.sz + 1;
7494             bal_dims.dim_id(bal_dims.sz)        := bal_dim_id;
7495             bal_dims.dim_name(bal_dims.sz)      := bal_dim_name;
7496             bal_dims.feed_chk_type(bal_dims.sz) := feed_checking_type;
7497             bal_dims.feed_chk_code(bal_dims.sz) := feed_checking_code;
7498             bal_dims.exp_chk_code(bal_dims.sz)  := bal_exp_code;
7499             bal_dims.exp_chk_lvl(bal_dims.sz)   := bal_exp_level;
7500 
7501          end if;
7502 
7503 
7504          --
7505          if feed_checking_type is null then
7506             -- default checking type whereby always feed balance
7507             if g_debug then
7508                hr_utility.set_location (c_indent, 20);
7509             end if;
7510             feed_balance := TRUE;
7511 
7512          elsif feed_checking_type not in ('P', 'E', 'J') then
7513             -- unhandled checking type is S-ubject
7514             -- we either just delete latest balance, or
7515             -- recalculate it
7516             -- YET TO BE DECIDED
7517 
7518             if g_debug then
7519                hr_utility.set_location (c_indent, 30);
7520             end if;
7521 
7522             if lat_bal_type = 'A' then
7523                delete from pay_balance_context_values
7524                where latest_balance_id = lat_bal_id;
7525 
7526                delete from pay_assignment_latest_balances
7527                where latest_balance_id = lat_bal_id;
7528             elsif lat_bal_type = 'P' then
7529                delete from pay_balance_context_values
7530                where latest_balance_id = lat_bal_id;
7531 
7532                delete from pay_person_latest_balances
7533                where latest_balance_id = lat_bal_id;
7534             else
7535                delete from pay_latest_balances
7536                where latest_balance_id = lat_bal_id;
7537 
7538             end if;
7539             -- or recalculate latest balance
7540             feed_balance := FALSE;
7541 
7542          elsif feed_checking_type = 'P' then
7543             -- legislative defined procedure feed_checking_code is
7544             -- used to perform feed checking
7545 
7546             feed_balance := proc_feed_check(feed_checking_code, bal_dim_name,
7547                                             bal_context_str);
7548 
7549          elsif feed_checking_type in ('E', 'J') then
7550 
7551           if (lat_bal_type='L') then
7552            feed_balance:= lb_context_match_check(lat_bal_id,feed_checking_type,
7553                                                 bal_type_id, udca);
7554           else
7555            feed_balance := context_match_check(lat_bal_id, feed_checking_type,
7556                                                 bal_type_id, udca);
7557           end if;
7558 
7559          end if;
7560 
7561          return(feed_balance);
7562 
7563       end feed_check;
7564       --
7565       -- Name : get_expiry_date - Execute balance expiry date procedure
7566       --
7567       -- Returns last date in period for dimension containing the
7568       -- effective_date of p_bal_owner_asg_actid
7569       --
7570       -- returns 01/01/1900 if p_bal_owner_asg_actid is the empty value
7571       -- ie -9999.  The calling code will then treats this as special balance
7572       -- value - and should leave it unchanged.
7573       --
7574       -- returns 01/01/1990 if is a never expired type of balance
7575       --
7576       -- The pl/sql function name that checks whether the balance is fed is
7577       -- passed to this routine as 'p_expiry_date_code'.  The call to the
7578       -- function is done using dynamic pl/sql.
7579       --
7580       -- Also returns effective date of latest balance in p_bal_owner_eff_date
7581       -- (to save refetching later on).
7582       --
7583       function get_expiry_date
7584       (
7585          p_expiry_date_code    in     varchar2,  -- expiry date procedure name
7586          p_ass_action_id       in     number,    -- balance adjustment asg action id
7587          p_bal_owner_asg_actid in     number,    -- latest balance asg action id
7588          p_dimension_name      in     varchar2,  -- dimension name
7589          p_expiry_check_level  in     varchar2,  -- expiry checking level
7590          p_bal_context_str     in     varchar2,  -- list of context values.
7591          p_bal_owner_eff_date  in out nocopy date       -- latest balance date
7592       ) return date is
7593          l_expiry_date date;
7594       --
7595          c_indent constant varchar2(35) := 'hrassact.get_expiry_date';
7596          l_payroll_action        pay_payroll_actions.payroll_action_id%type;
7597          l_effective_date        pay_payroll_actions.effective_date%type;
7598          l_bal_owner_pay_action  pay_payroll_actions.payroll_action_id%type;
7599          l_expiry_chk_str  varchar2(2000);  -- used with dynamic pl/sql
7600          sql_cursor        integer;
7601          l_rows            integer;
7602       --
7603       begin
7604          --
7605          if g_debug then
7606             hr_utility.set_location (c_indent, 5);
7607          end if;
7608          if p_bal_owner_asg_actid = -9999 then
7609             l_expiry_date := to_date('01/01/1900', 'DD/MM/YYYY');
7610             p_bal_owner_eff_date := l_expiry_date;
7611 
7612             return(l_expiry_date);
7613          end if;
7614          --
7615          if g_debug then
7616             hr_utility.trace ('Expiry date fetching code = ' || p_expiry_date_code);
7617             hr_utility.set_location (c_indent||p_bal_owner_asg_actid, 7);
7618          end if;
7619          --
7620          -- Get the payroll_action_id and eff date for the latest balance
7621          -- The expiry date returned is the last date in the period for
7622          -- the balance dimension that contains this date
7623          --
7624          select pay.payroll_action_id,
7625                 pay.effective_date
7626          into   l_bal_owner_pay_action,
7627                 p_bal_owner_eff_date
7628          from   pay_assignment_actions        asg
7629          ,      pay_payroll_actions           pay
7630          where  asg.assignment_action_id    = p_bal_owner_asg_actid
7631          and    pay.payroll_action_id       = asg.payroll_action_id;
7632          --
7633          -- Get the payroll_action_id and eff date for the adjustment if
7634          -- it is different
7635          --
7636          if g_debug then
7637             hr_utility.set_location (c_indent||p_ass_action_id, 10);
7638          end if;
7639          if p_ass_action_id <> p_bal_owner_asg_actid then
7640             select pay.payroll_action_id,
7641                    pay.effective_date
7642             into   l_payroll_action,
7643                    l_effective_date
7644             from   pay_assignment_actions        asg
7645             ,      pay_payroll_actions           pay
7646             where  asg.assignment_action_id    = p_ass_action_id
7647             and    pay.payroll_action_id       = asg.payroll_action_id;
7648          else
7649             l_payroll_action := l_bal_owner_pay_action;
7650             l_effective_date := p_bal_owner_eff_date;
7651          end if;
7652          --
7653          -- if Never expired balance we simply return an expiry date
7654          -- of beginning of time
7655          --
7656          if p_expiry_check_level = 'N' then
7657             if g_debug then
7658                hr_utility.set_location (c_indent, 12);
7659             end if;
7660             l_expiry_date := to_date('01/01/1900', 'DD/MM/YYYY');
7661 
7662             return(l_expiry_date);
7663          elsif p_expiry_date_code is null then
7664             --
7665             -- expiry date procedure doesn't exist
7666             --
7667             if g_debug then
7668                hr_utility.set_location (c_indent, 14);
7669             end if;
7670             dbms_sql.close_cursor(sql_cursor);
7671             hr_utility.set_message(801, 'HR_7274_PAY_NO_EXPIRY_CODE');
7672             hr_utility.set_message_token ('EXPIRY_CODE', p_expiry_date_code);
7673             hr_utility.raise_error;
7674          end if;
7675          --
7676          -- we build up the sql string to call the balance
7677          -- feed checking pl/sql procedure:
7678          --
7679          l_expiry_chk_str := 'begin ' || p_expiry_date_code || ' (';
7680          l_expiry_chk_str := l_expiry_chk_str || ':l_bal_owner_pay_action, ';
7681          l_expiry_chk_str := l_expiry_chk_str || ':l_payroll_action, ';
7682          l_expiry_chk_str := l_expiry_chk_str || ':p_bal_owner_asg_actid, ';
7683          l_expiry_chk_str := l_expiry_chk_str || ':p_ass_action_id, ';
7684          l_expiry_chk_str := l_expiry_chk_str || ':p_bal_owner_eff_date, ';
7685          l_expiry_chk_str := l_expiry_chk_str || ':l_effective_date, ';
7686          l_expiry_chk_str := l_expiry_chk_str || ':p_dimension_name, ';
7687          --
7688          if (p_expiry_check_level in ('A', 'D')) then
7689             l_expiry_chk_str := l_expiry_chk_str || ':p_bal_context_str, ';
7690          end if;
7691          --
7692          l_expiry_chk_str := l_expiry_chk_str || ':l_expiry_date); end;';
7693          --
7694          -- now execute the SQL statement using dynamic pl/sql:
7695          --
7696          -- Dynamic sql steps:
7697          -- ==================
7698          -- 1. Open dynamic sql cursor
7699          -- 2. Parse dynamic sql
7700          -- 3. bind variables
7701          -- 4. Execute dynamic sql
7702          -- 5. Get the variable value (providing there are rows returned)
7703          -- 6. Close the dynamic sql cursor
7704          --
7705          if g_debug then
7706             hr_utility.set_location (c_indent, 20);
7707          end if;
7708          sql_cursor := dbms_sql.open_cursor;                      -- step 1
7709          --
7710          if g_debug then
7711             hr_utility.set_location (c_indent, 25);
7712          end if;
7713          dbms_sql.parse(sql_cursor, l_expiry_chk_str, dbms_sql.v7); -- step 2
7714          --
7715          if g_debug then
7716             hr_utility.set_location (c_indent||to_char(p_bal_owner_eff_date, 'DD/MM/YYYY'), 30);
7717          end if;
7718          dbms_sql.bind_variable(sql_cursor, 'l_bal_owner_pay_action', l_bal_owner_pay_action);  -- step 3:
7719          --
7720          dbms_sql.bind_variable(sql_cursor, 'l_payroll_action', l_payroll_action);
7721          --
7722          dbms_sql.bind_variable(sql_cursor, 'p_bal_owner_asg_actid', p_bal_owner_asg_actid);
7723          --
7724          dbms_sql.bind_variable(sql_cursor, 'p_ass_action_id', p_ass_action_id);
7725          --
7726          dbms_sql.bind_variable(sql_cursor, 'p_bal_owner_eff_date', p_bal_owner_eff_date);
7727          --
7728          dbms_sql.bind_variable(sql_cursor, 'l_effective_date', l_effective_date);
7729          --
7730          dbms_sql.bind_variable(sql_cursor, 'p_dimension_name', p_dimension_name);
7731          --
7732          if (p_expiry_check_level in ('A', 'D')) then
7733             dbms_sql.bind_variable(sql_cursor, 'p_bal_context_str', p_bal_context_str);
7734          end if;
7735          --
7736          dbms_sql.bind_variable(sql_cursor, 'l_expiry_date', l_expiry_date);
7737          --
7738          if g_debug then
7739             hr_utility.set_location (c_indent, 35);
7740          end if;
7741          l_rows := dbms_sql.execute (sql_cursor);                 -- step 4
7742          --
7743          if (l_rows = 1) then
7744             if g_debug then
7745                hr_utility.set_location (c_indent, 40);
7746             end if;
7747             dbms_sql.variable_value(sql_cursor, 'l_expiry_date',   -- step 5
7748                                                 l_expiry_date);
7749             --
7750             if g_debug then
7751                hr_utility.set_location (c_indent||to_char(l_expiry_date, 'DD/MM/YYYY'), 45);
7752             end if;
7753             dbms_sql.close_cursor(sql_cursor);                   -- step 6
7754          elsif (l_rows = 0) then
7755             --
7756             -- expiry date procedure didn't exist
7757             --
7758             if g_debug then
7759                hr_utility.set_location (c_indent, 50);
7760             end if;
7761             dbms_sql.close_cursor(sql_cursor);
7762             hr_utility.set_message(801, 'HR_7274_PAY_NO_EXPIRY_CODE');
7763             hr_utility.set_message_token ('EXPIRY_CODE', p_expiry_date_code);
7764             hr_utility.raise_error;
7765          else
7766             --
7767             -- None or more than 1 row has been returned. We must error as package
7768             -- call can only return 1 row, so this condition should never occur !
7769             --
7770             if g_debug then
7771                hr_utility.set_location (c_indent, 60);
7772             end if;
7773             dbms_sql.close_cursor(sql_cursor);
7774             hr_utility.raise_error;
7775          end if;
7776          --
7777          return(l_expiry_date);
7778          --
7779       end get_expiry_date;
7780       --
7781       -- Name : feed_balance - Adjust latest/previous/expired balances
7782       --
7783       -- Called if balance adjustment does feed this latest balance.
7784       -- Have to adjust none of more of latest, previous or expired balance
7785       -- values and possibly their asg action ids depending upon their
7786       -- and the balance adjustments period
7787       --
7788       function feed_balance
7789       (
7790          p_expiry_date_code    in     varchar2,  -- expiry date procedure name
7791          p_dimension_name      in     varchar2,  -- dimension name
7792          p_expiry_check_level  in     varchar2,  -- expiry checking level
7793          p_bal_context_str     in     varchar2,  -- list of context values.
7794          assignment_action_id  in out nocopy number,    -- lb lat asg action id
7795          value                 in out nocopy number,    -- lb lat value
7796          expired_asg_action_id in out nocopy number,    -- lb exp asg action id
7797          expired_value         in out nocopy number,    -- lb exp value
7798          prev_asg_action_id    in out nocopy number,    -- lb prev al asg action id
7799          prev_balance_value    in out nocopy number,    -- lb prev value
7800          result_value          in     number,    -- balance adjustment rrv
7801          feed_scale            in     number,    -- balance dimension feed scale
7802          expiry_date           in out nocopy date  ,    -- lb expiry date
7803          expired_date          in out nocopy date  ,    -- lb expired date
7804          prev_expiry_date      in out nocopy date      -- lb prev expiry date
7805       ) return boolean is
7806          l_change_flag boolean;
7807       --
7808          c_indent constant varchar2(35) := 'hrassact.feed_balance';
7809          lat_bal_exp_date        date;
7810          l_bal_owner_eff_date    pay_payroll_actions.effective_date%type;
7811          bal_adj_exp_date        date;
7812          bal_adj_eff_date        pay_payroll_actions.effective_date%type;
7813          prev_bal_exp_date       date;
7814          p_bal_owner_eff_date    pay_payroll_actions.effective_date%type;
7815          exp_bal_exp_date        date;
7816          e_bal_owner_eff_date    pay_payroll_actions.effective_date%type;
7817       --
7818       begin
7819       --
7820          if g_debug then
7821             hr_utility.set_location (c_indent, 10);
7822          end if;
7823          --
7824          l_change_flag := TRUE;
7825        --
7826          -- Check if latest balance already owned by balance
7827          -- adjustment. If so simply increment the value
7828          --
7829          if assignment_action_id = assactid or
7830             expired_asg_action_id = assactid then
7831 
7832             if g_debug then
7833                hr_utility.set_location (c_indent, 15);
7834             end if;
7835 
7836             if assignment_action_id = assactid then
7837                value := value + (result_value * feed_scale);
7838             end if;
7839 
7840             if expired_asg_action_id = assactid then
7841                expired_value := expired_value + (result_value * feed_scale);
7842 
7843                if prev_asg_action_id = assactid then
7844                   prev_balance_value := prev_balance_value + (result_value * feed_scale);
7845                end if;
7846             end if;
7847 
7848             return(l_change_flag);
7849          end if;
7850 
7851          --
7852          -- Get expiry date for balance adjustment
7853          --
7854          bal_adj_exp_date := get_expiry_date(p_expiry_date_code, assactid,
7855                                              assactid, p_dimension_name,
7856                                              p_expiry_check_level, p_bal_context_str,
7857                                              bal_adj_eff_date);
7858          --
7859          if g_debug then
7860             hr_utility.set_location (c_indent, 20);
7861          end if;
7862          --
7863          -- Get expiry date for latest balance
7864          --
7865          lat_bal_exp_date := get_expiry_date(p_expiry_date_code, assignment_action_id,
7866                                              assignment_action_id, p_dimension_name,
7867                                              p_expiry_check_level, p_bal_context_str,
7868                                              l_bal_owner_eff_date);
7869          --
7870          if g_debug then
7871             hr_utility.set_location (c_indent||p_dimension_name, 30);
7872          end if;
7873 
7874          if bal_adj_exp_date > lat_bal_exp_date then
7875             -- balance adjustment expiry date is later than the latest balance
7876             -- expiry date, and hence in a later period so we copy the latest balance
7877             -- to the previous latest balance and expired latest balance, and set
7878             -- the latest balance to be the balance adjustment
7879 
7880             if g_debug then
7881                hr_utility.set_location (c_indent, 40);
7882             end if;
7883 
7884             prev_balance_value := value;
7885             prev_asg_action_id := assignment_action_id;
7886             expired_value := value;
7887             expired_asg_action_id := assignment_action_id;
7888 
7889             value := result_value * feed_scale;
7890             assignment_action_id := assactid;
7891             expired_date := lat_bal_exp_date;
7892             prev_expiry_date := lat_bal_exp_date;
7893             expiry_date := bal_adj_exp_date;
7894 
7895          elsif bal_adj_exp_date = lat_bal_exp_date then
7896             -- balance adjustment expiry date = latest balance expiry date
7897             -- so feed latest balance and possibly previous latest balance
7898 
7899             if g_debug then
7900                hr_utility.set_location (c_indent, 50);
7901             end if;
7902 
7903             if bal_adj_eff_date >= l_bal_owner_eff_date then
7904                -- balance adjustment effective date is later than the latest balance
7905                -- effective date so we copy the latest balance to the previous latest
7906                -- balance and adjust the latest balance value and its asg action id by
7907                -- the adjustment
7908 
7909                if g_debug then
7910                   hr_utility.set_location (c_indent, 60);
7911                end if;
7912 
7913                prev_balance_value := value;
7914                prev_asg_action_id := assignment_action_id;
7915 
7916                value := value + (result_value * feed_scale);
7917                assignment_action_id := assactid;
7918                prev_expiry_date := lat_bal_exp_date;
7919                expiry_date := bal_adj_exp_date;
7920 
7921             else
7922                -- the balance adjustment effective date is earlier than the latest balance
7923                -- effective date so we adjust the latest balance value (but nots its asg
7924                -- action id), and then see if we need to adjust the previous latest
7925                -- balance
7926 
7927                if g_debug then
7928                   hr_utility.set_location (c_indent, 70);
7929                end if;
7930 
7931                value := value + (result_value * feed_scale);
7932                expiry_date := bal_adj_exp_date;
7933 
7934                -- Get expiry date for previous latest balance
7935                prev_bal_exp_date := get_expiry_date(p_expiry_date_code, prev_asg_action_id,
7936                                                     prev_asg_action_id, p_dimension_name,
7937                                                     p_expiry_check_level, p_bal_context_str,
7938                                                     p_bal_owner_eff_date);
7939 
7940                if bal_adj_exp_date = prev_bal_exp_date then
7941                   -- need to adjust previous balance as in same period as balance adjustment
7942 
7943                   if g_debug then
7944                      hr_utility.set_location (c_indent, 80);
7945                   end if;
7946 
7947                   if bal_adj_eff_date >= p_bal_owner_eff_date then
7948                      -- as balance adjustment effective date is later than previous latest
7949                      -- balance effective date we also need to amend previous latest
7950                      -- balance asg action id
7951 
7952                      if g_debug then
7953                         hr_utility.set_location (c_indent, 90);
7954                      end if;
7955 
7956                      prev_balance_value := prev_balance_value + (result_value * feed_scale);
7957                      prev_asg_action_id := assactid;
7958                      prev_expiry_date :=  bal_adj_exp_date;
7959                   else
7960                      if g_debug then
7961                         hr_utility.set_location (c_indent, 100);
7962                      end if;
7963 
7964                      prev_balance_value := prev_balance_value + (result_value * feed_scale);
7965                      prev_expiry_date := prev_bal_exp_date;
7966                   end if;
7967 
7968                elsif prev_asg_action_id <> -9999 then
7969                   -- balance adjustment is in later period than previous latest balance
7970                   -- so replace previous latest balance value and its asg action id by
7971                   -- the balance adjustment
7972 
7973                   if g_debug then
7974                      hr_utility.set_location (c_indent, 110);
7975                   end if;
7976                   prev_balance_value := result_value * feed_scale;
7977                   prev_asg_action_id := assactid;
7978                   prev_expiry_date := bal_adj_exp_date;
7979 
7980                else
7981                   -- previous balance value is undefined (ie null or -9999)
7982 
7983                   if g_debug then
7984                      hr_utility.set_location (c_indent, 115);
7985                   end if;
7986 
7987                end if;
7988             end if;
7989 
7990          else
7991             -- balance adjusmtent expiry date <> latest balance expiry date
7992             -- so may need to adjust expired and previous latest balances
7993             if g_debug then
7994                hr_utility.set_location (c_indent, 120);
7995             end if;
7996 
7997             -- Get expiry date for expired latest balance
7998             exp_bal_exp_date := get_expiry_date(p_expiry_date_code, expired_asg_action_id,
7999                                                 expired_asg_action_id, p_dimension_name,
8000                                                 p_expiry_check_level, p_bal_context_str,
8001                                                 e_bal_owner_eff_date);
8002 
8003             -- if balance adjusmtent expiry date = expired balance expiry date
8004             -- then adjust expired balance and possibly previous latest balance
8005             if bal_adj_exp_date = exp_bal_exp_date then
8006 
8007                if g_debug then
8008                   hr_utility.set_location (c_indent, 130);
8009                end if;
8010 
8011                if bal_adj_eff_date >= e_bal_owner_eff_date then
8012                   -- balance adjustment effective date is later than the expired balance
8013                   -- effective date so the expired value and asg action id needs to be
8014                   -- adjusted by the balance adjustment
8015                   --
8016                   -- before doing so we see if the previous latest balance is from the
8017                   -- same assignment action id and hence also needs adjusting
8018 
8019                   if g_debug then
8020                      hr_utility.set_location (c_indent, 140);
8021                   end if;
8022 
8023                   if expired_asg_action_id = prev_asg_action_id then
8024                      -- adjust the previous latest balance
8025                      if g_debug then
8026                         hr_utility.set_location (c_indent, 150);
8027                      end if;
8028 
8029                      prev_balance_value := prev_balance_value + (result_value * feed_scale);
8030                      prev_asg_action_id := assactid;
8031                      prev_expiry_date := bal_adj_exp_date;
8032                   end if;
8033 
8034                   expired_value := expired_value + (result_value * feed_scale);
8035                   expired_asg_action_id := assactid;
8036                   expired_date := bal_adj_exp_date;
8037 
8038                else
8039                   -- balance adjustment effective date is earlier than the expired balance
8040                   -- effective date so the expired value (not the asg action id) needs
8041                   -- to be adjusted by the balance adjustment
8042                   --
8043                   -- before doing so we see if the previous latest balance is from the
8044                   -- same assignment action id and hence also needs adjusting
8045 
8046                   if g_debug then
8047                      hr_utility.set_location (c_indent, 160);
8048                   end if;
8049 
8050                   if expired_asg_action_id = prev_asg_action_id then
8051                      -- adjust the previous latest balance
8052                      if g_debug then
8053                         hr_utility.set_location (c_indent||prev_balance_value, 170);
8054                      end if;
8055 
8056                      prev_balance_value := prev_balance_value + (result_value * feed_scale);
8057                      prev_expiry_date := exp_bal_exp_date;
8058                      if g_debug then
8059                         hr_utility.set_location (c_indent||prev_balance_value, 170);
8060                      end if;
8061                   end if;
8062 
8063                   if g_debug then
8064                      hr_utility.set_location (c_indent||expired_value, 170);
8065                   end if;
8066                   expired_value := expired_value + (result_value * feed_scale);
8067                   expired_date := exp_bal_exp_date;
8068                   if g_debug then
8069                      hr_utility.set_location (c_indent||expired_value, 170);
8070                   end if;
8071 
8072                end if;
8073 
8074             else
8075                -- the balance adjustment expiry date is not the same as either the
8076                -- latest balance expiry date or the expired balance expiry date,
8077                -- and is not later than the latest balance expiry date.
8078                -- it may be between the latest and expired balance periods - in
8079                -- which case we would want to adjust the expired, or it may be
8080                -- ealier than the expired.
8081                --
8082                if g_debug then
8083                   hr_utility.set_location (c_indent, 180);
8084                end if;
8085 
8086                if expired_asg_action_id <> -9999 and
8087                   bal_adj_exp_date > exp_bal_exp_date then
8088                   -- it is newer so we need to replace the expired (and possibly previous)
8089 
8090                   if g_debug then
8091                      hr_utility.set_location (c_indent, 190);
8092                   end if;
8093 
8094                   if expired_asg_action_id = prev_asg_action_id then
8095                      -- adjust the previous latest balance
8096                      if g_debug then
8097                         hr_utility.set_location (c_indent, 200);
8098                      end if;
8099 
8100                      prev_balance_value := result_value * feed_scale;
8101                      prev_asg_action_id := assactid;
8102                      prev_expiry_date := bal_adj_exp_date;
8103                   end if;
8104 
8105                   expired_value := result_value * feed_scale;
8106                   expired_asg_action_id := assactid;
8107                   expired_date := bal_adj_exp_date;
8108 
8109                else
8110                   -- the balance adjustment is in an older period than the expired
8111                   -- latest balance and therefore does not feed it
8112 
8113                    if g_debug then
8114                      hr_utility.set_location (c_indent, 210);
8115                   end if;
8116 
8117                   l_change_flag := FALSE;
8118 
8119                end if;
8120 
8121             end if;
8122          end if;
8123 
8124          return(l_change_flag);
8125       --
8126       end feed_balance;
8127       --
8128    begin
8129       g_debug := hr_utility.debug_enabled;
8130       --
8131       if g_debug then
8132          hr_utility.set_location(c_indent,10);
8133       end if;
8134       --
8135       -- initialise balance dimensions cache
8136       --
8137       bal_dims.sz := 0;
8138       --
8139       -- get info about assignment and action
8140       -- including tax_unit_id context value
8141       --
8142       select pera.assignment_id,
8143              pera.person_id,
8144              paa.payroll_action_id,
8145              paa.tax_unit_id,
8146              pera.business_group_id
8147       into   asgid,
8148              l_person_id,
8149              pactid,
8150              tax_unit_id,
8151              bus_grp_id
8152       from   pay_assignment_actions paa,
8153              per_all_assignments_f pera
8154       where  paa.assignment_action_id = assactid
8155       and    pera.assignment_id       = paa.assignment_id
8156       and    effdate between pera.effective_start_date
8157                          and pera.effective_end_date;
8158       --
8159       if g_debug then
8160          hr_utility.set_location(c_indent,20);
8161       end if;
8162       --
8163       if act_type = 'B' then
8164          --
8165          -- check for BAL_ADJ_LAT_BAL legislation rule to see if
8166          -- this functionality is supported for this balance adjustment
8167          --
8168          if g_ba_lat_bal_maintenance is null then
8169             begin
8170                select rule_mode
8171                into l_rule_mode
8172                from pay_legislation_rules plr,
8173                     per_business_groups_perf pbg
8174                where pbg.business_group_id = bus_grp_id
8175                and   plr.legislation_code  = pbg.legislation_code
8176                and   rule_type             = 'BAL_ADJ_LAT_BAL';
8177 
8178                if upper(l_rule_mode) = 'Y' then
8179                   g_ba_lat_bal_maintenance := FALSE;
8180                else
8181                   g_ba_lat_bal_maintenance := TRUE;
8182                end if;
8183             exception
8184                when others then
8185                   g_ba_lat_bal_maintenance := TRUE;
8186             end;
8187          end if;
8188          not_supported := g_ba_lat_bal_maintenance;
8189       else
8190          --
8191          -- Reversal - as entered maintain_lat_bal REV_LAT_BAL
8192          -- is set
8193          --
8194          not_supported := FALSE;
8195       end if;
8196       --
8197       --
8198       if not_supported = TRUE then
8199          --
8200          -- delete latest balances
8201          if g_debug then
8202             hr_utility.set_location(c_indent,25);
8203          end if;
8204          --
8205          -- Derive element type id from the run result.
8206          --
8207          select rr.element_type_id into l_element_type_id
8208          from pay_run_results rr
8209          where rr.run_result_id = rrid;
8210 
8211          del_latest_balances(asgid, effdate, eentryid, l_element_type_id);
8212          return;
8213       end if;
8214       --
8215       -- get the contexts cached
8216       if contexts_cached = FALSE then
8217          cache_contexts;
8218       end if;
8219       --
8220       if g_debug then
8221          hr_utility.set_location(c_indent,30);
8222       end if;
8223       --
8224       for rrv in run_result_values loop
8225 
8226          bal_fed := FALSE;
8227          l_change_flag := FALSE;
8228          -- return contents of udca
8229          bal_context_string := get_contexts(udca);
8230 
8231 
8232       pay_core_utils.get_upgrade_status(p_bus_grp_id=> bus_grp_id,
8233                              p_short_name=> 'SINGLE_BAL_TABLE',
8234                              p_status=>l_status);
8235 
8236       if (l_status='N')
8237       then
8238          -- feed check assignment latest balances
8239          for alb in fed_assignment_balances(rrv.input_value_id, asgid) loop
8240              bal_fed := feed_check(alb.latest_balance_id, 'A', alb.balance_dimension_id,
8241                                    alb.balance_type_id, bal_dim_name,
8242                                    balance_expiry_code, balance_expiry_level,
8243                                    bal_context_string, udca);
8244 
8245 
8246              if bal_fed = TRUE then
8247                 l_change_flag := feed_balance(balance_expiry_code, bal_dim_name,
8248                              balance_expiry_level, bal_context_string,
8249                              alb.assignment_action_id, alb.value,
8250                              alb.expired_assignment_action_id, alb.expired_value,
8251                              alb.prev_assignment_action_id, alb.prev_balance_value,
8252                              fnd_number.canonical_to_number(rrv.result_value),
8253                              alb.scale, dummy_date,dummy_date,dummy_date);
8254 
8255                 if l_change_flag = TRUE then
8256                    update pay_assignment_latest_balances
8257                    set assignment_action_id         = alb.assignment_action_id,
8258                        value                        = alb.value,
8259                        expired_assignment_action_id = alb.expired_assignment_action_id,
8260                        expired_value                = alb.expired_value,
8261                        prev_assignment_action_id    = alb.prev_assignment_action_id,
8262                        prev_balance_value           = alb.prev_balance_value
8263                    where latest_balance_id          = alb.latest_balance_id;
8264                 end if;
8265              end if;
8266          end loop;
8267       --
8268          -- feed check person latest balances
8269          for plb in fed_person_balances(rrv.input_value_id, l_person_id) loop
8270              bal_fed := feed_check(plb.latest_balance_id, 'P', plb.balance_dimension_id,
8271                                    plb.balance_type_id, bal_dim_name,
8272                                    balance_expiry_code, balance_expiry_level,
8273                                    bal_context_string, udca);
8274 
8275              if bal_fed = TRUE then
8276                 l_change_flag := feed_balance(balance_expiry_code, bal_dim_name,
8277                              balance_expiry_level, bal_context_string,
8278                              plb.assignment_action_id, plb.value,
8279                              plb.expired_assignment_action_id, plb.expired_value,
8280                              plb.prev_assignment_action_id, plb.prev_balance_value,
8281                              fnd_number.canonical_to_number(rrv.result_value),
8282                              plb.scale,dummy_date,dummy_date,dummy_date);
8283 
8284                 if l_change_flag = TRUE then
8285                    update pay_person_latest_balances
8286                    set assignment_action_id         = plb.assignment_action_id,
8287                        value                        = plb.value,
8288                        expired_assignment_action_id = plb.expired_assignment_action_id,
8289                        expired_value                = plb.expired_value,
8290                        prev_assignment_action_id    = plb.prev_assignment_action_id,
8291                        prev_balance_value           = plb.prev_balance_value
8292                    where latest_balance_id          = plb.latest_balance_id;
8293                 end if;
8294              end if;
8295          end loop;
8296       --
8297       else
8298          -- feed check person latest balances
8299          for lb in fed_latest_balances(rrv.input_value_id, l_person_id, asgid) loop
8300              hr_utility.set_location(c_indent,100);
8301              bal_fed := feed_check(lb.latest_balance_id, 'L', lb.balance_dimension_id,
8302                                    lb.balance_type_id, bal_dim_name,
8303                                    balance_expiry_code, balance_expiry_level,
8304                                    bal_context_string, udca);
8305 
8306              if bal_fed = TRUE then
8307                 l_change_flag := feed_balance(balance_expiry_code, bal_dim_name,
8308                              balance_expiry_level, bal_context_string,
8309                              lb.assignment_action_id, lb.value,
8310                              lb.expired_assignment_action_id, lb.expired_value,
8311                              lb.prev_assignment_action_id, lb.prev_balance_value,
8312                              fnd_number.canonical_to_number(rrv.result_value),
8313                              lb.scale,lb.expiry_date, lb.expired_date,lb.prev_expiry_date);
8314 
8315                 if l_change_flag = TRUE then
8316                    update pay_latest_balances
8317                    set assignment_action_id         = lb.assignment_action_id,
8318                        value                        = lb.value,
8319                        expiry_date                = lb.expiry_date,
8320                        expired_assignment_action_id = lb.expired_assignment_action_id,
8321                        expired_value                = lb.expired_value,
8322                        expired_date                = lb.expired_date,
8323                        prev_assignment_action_id    = lb.prev_assignment_action_id,
8324                        prev_expiry_date                = lb.prev_expiry_date,
8325                        prev_balance_value           = lb.prev_balance_value
8326                    where latest_balance_id          = lb.latest_balance_id;
8327                 end if;
8328              end if;
8329          end loop;
8330        end if;
8331       end loop;
8332       --
8333    end maintain_lat_bal;
8334 --
8335 -- this function retruns the assignment_action id of the
8336 -- reversal that is being retried
8337 -- the action passed is is the assignment action of the reversal
8338 
8339 function get_retry_revesal_action_id(p_act_id number)
8340 return number is
8341 
8342 l_mst_id number;
8343 new_assactid number;
8344 found number;
8345 begin
8346 
8347 -- determine the master action
8348    SELECT pai.locked_action_id
8349    INTO   l_mst_id
8350    FROM   pay_action_interlocks pai,pay_assignment_actions paa
8351    WHERE  pai.locking_action_id = p_act_id
8352    AND    paa.assignment_action_id = pai.locked_action_id
8353    AND    paa.source_action_id is null;
8354 
8355    select 1
8356    into found
8357    from dual
8358    where exists (select 1 from pay_assignment_actions paa
8359    where paa.source_action_id=l_mst_id);
8360 
8361    begin
8362    -- if there is no run for that interlocok cacluate
8363 
8364    -- what the new assignment action should be
8365       select child_asg.assignment_action_id
8366       into new_assactid
8367       from pay_assignment_actions mast_asg,
8368            pay_assignment_actions child_asg,
8369            pay_assignment_actions rev_asg
8370       where l_mst_id=mast_asg.assignment_action_id
8371       and child_asg.source_action_id is not null
8372       and child_asg.assignment_id=mast_asg.assignment_id
8373       and child_asg.payroll_action_id=mast_asg.payroll_action_id
8374       and child_asg.action_status<>'B'
8375       and pay_core_utils.get_process_path(child_asg.assignment_action_id)
8376                          =rev_asg.process_path
8377       and rev_asg.assignment_action_id=p_act_id;
8378 
8379       return new_assactid;
8380 
8381     exception
8382            when no_data_found then
8383              return -9999;
8384 
8385            when too_many_rows then
8386             hr_utility.set_message(801,'HR_34864_INTERLOCK_ERR_REV');
8387             hr_utility.raise_error;
8388 
8389     end;
8390 
8391 exception
8392   when no_data_found then
8393      return l_mst_id;
8394 
8395 end;
8396 --
8397 end hrassact;