Posted on Apr. 22, 2010 at 01.40 PM - Chennai, India
What is the scenario?
When dealing with HR payroll data, we often have to handle retroactive payroll result. For example, an employee have got employer pension contribution in January for $10. However during the month of February, the employer decided to increase by another dollar starting from January. So in the month of February, the employee will get $11 for the month of February and another dollar for the month of January. So, in total the employer pension contribution for the month of February is $12 should be reported, not $11.
How do we handle that?
We can create a method GET_PAYROLL_DATA_RETRO to handle it so that it is reusable.
1. Parameters
IP_PERNR Importing Type PERNR_D Personnel Number
IP_IPBEG Importing Type BEGDA Start Date
IP_IPEND Importing Type ENDDA End Date
EP_RT Exporting Type HRPAY99_RT RT Table
2. Local Data Declaration
* data declaration
DATA: lt_rgdir TYPE TABLE OF pc260.
DATA: ls_rgdir TYPE pc260.
DATA: lt_rgdir_retro TYPE TABLE OF pc260.
DATA: ls_rgdir_retro TYPE pc260.
DATA: ls_result_active TYPE paygb_result.
DATA: ls_result_previous TYPE paygb_result.
DATA: lv_molga TYPE molga.
DATA: lv_relid TYPE relid_pcl.
DATA: lt_rt_active TYPE STANDARD TABLE OF pc207.
DATA: ls_rt_active TYPE pc207.
DATA: lt_rt_previous TYPE STANDARD TABLE OF pc207.
DATA: ls_rt_previous TYPE pc207.
DATA: lt_rt_final TYPE STANDARD TABLE OF pc207.
DATA: ls_rt_final TYPE pc207.
DATA: lv_betrg TYPE maxbt.
DATA: lv_count TYPE i.
DATA: lv_retro TYPE c.
FIELD-SYMBOLS: <fs_rt_final> TYPE pc207.
3. Get the payroll directory
* get the country grouping
CALL FUNCTION 'PYXX_GET_RELID_FROM_PERNR'
EXPORTING
employee = ip_pernr
IMPORTING
relid = lv_relid
molga = lv_molga
EXCEPTIONS
error_reading_infotype_0001 = 1
error_reading_molga = 2
error_reading_relid = 3
OTHERS = 4.
* Make sure the country grouping is Great Britain
CHECK lv_molga = '08' AND lv_relid = g_rg.
* get payroll data
CALL FUNCTION 'CD_READ_RGDIR'
EXPORTING
persnr = ip_pernr
TABLES
in_rgdir = lt_rgdir
EXCEPTIONS
no_record_found = 1
OTHERS = 2.
CHECK lt_rgdir IS NOT INITIAL.
* sort descending so that we get the current month result first
SORT lt_rgdir BY seqnr DESCENDING.
CLEAR lv_count.
4. Calculate the difference and add to the current month payroll
* get the current month result and the retroactive result of the previous month
LOOP AT lt_rgdir INTO ls_rgdir WHERE ipend = ip_ipend.
ADD 1 TO lv_count.
CLEAR ls_result_active.
CLEAR lt_rt_active.
* get payroll result
CALL FUNCTION 'PYXX_READ_PAYROLL_RESULT'
EXPORTING
clusterid = lv_relid
employeenumber = ip_pernr
sequencenumber = ls_rgdir-seqnr
CHANGING
payroll_result = ls_result_active
EXCEPTIONS
illegal_isocode_or_clusterid = 1
error_generating_import = 2
import_mismatch_error = 3
subpool_dir_full = 4
no_read_authority = 5
no_record_found = 6
versions_do_not_match = 7
error_reading_archive = 8
error_reading_relid = 9
OTHERS = 10.
IF sy-subrc = 0.
IF lv_count = 1.
* store the current month result to final rt table
MOVE ls_result_active-inter-rt TO lt_rt_final.
lt_rt_active = lt_rt_final.
ELSE.
MOVE ls_result_active-inter-rt TO lt_rt_active.
ENDIF.
* get retroactive value
CLEAR lv_retro.
CALL FUNCTION 'CD_RETROCALC_PERIOD'
EXPORTING
entry = ls_rgdir
IMPORTING
calcd = lv_retro.
CHECK lv_retro = 'X'.
* get previous sequence number
CALL FUNCTION 'CD_READ_PREVIOUS'
EXPORTING
in_record = ls_rgdir
* EXACT = ' '
* LAST = ' '
* ALL = ' '
* IGNORE_ABKRS = ' '
TABLES
rgdir = lt_rgdir
out_rgdir = lt_rgdir_retro
EXCEPTIONS
no_record_found = 1
OTHERS = 2
.
IF sy-subrc = 0.
CLEAR ls_result_previous.
CLEAR lt_rt_previous.
READ TABLE lt_rgdir_retro INTO ls_rgdir_retro INDEX 1.
* get payroll result from the previous month
CALL FUNCTION 'PYXX_READ_PAYROLL_RESULT'
EXPORTING
clusterid = lv_relid
employeenumber = ip_pernr
sequencenumber = ls_rgdir_retro-seqnr
CHANGING
payroll_result = ls_result_previous
EXCEPTIONS
illegal_isocode_or_clusterid = 1
error_generating_import = 2
import_mismatch_error = 3
subpool_dir_full = 4
no_read_authority = 5
no_record_found = 6
versions_do_not_match = 7
error_reading_archive = 8
error_reading_relid = 9
OTHERS = 10.
IF sy-subrc = 0.
MOVE ls_result_previous-inter-rt TO lt_rt_previous.
SORT lt_rt_previous BY lgart.
SORT lt_rt_final BY lgart.
* get the difference between active and previous payroll data and
* add the difference to the final rt table
LOOP AT lt_rt_active INTO ls_rt_active.
CLEAR lv_betrg.
READ TABLE lt_rt_previous INTO ls_rt_previous
WITH KEY lgart = ls_rt_active-lgart BINARY SEARCH
TRANSPORTING betrg.
* get the difference between active and previous payroll data
IF sy-subrc = 0.
lv_betrg = ls_rt_active-betrg - ls_rt_previous-betrg.
CHECK lv_betrg > 0.
READ TABLE lt_rt_final ASSIGNING <fs_rt_final>
WITH KEY lgart = ls_rt_active-lgart BINARY SEARCH.
* add the difference to the final rt table
IF sy-subrc = 0.
ADD lv_betrg TO <fs_rt_final>-betrg.
ELSE.
* append the wage type to the final rt table
* if the wage type doest not exist in the final rt table
ls_rt_active-betrg = lv_betrg.
APPEND ls_rt_active TO lt_rt_final.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
* the final rt table is current month rt result
* plus the difference of retroactive payroll from all previous months
ep_rt = lt_rt_final.
Life is beautiful! Let's make it meaningful and colorful!