Showing posts with label ABAP HR. Show all posts
Showing posts with label ABAP HR. Show all posts

Friday, July 25, 2008

Unified Access to All SAP ABAP HR Infotypes

The code snippet shows how to access any HR infotype using unified class-based methods.
The SAP standard class CL_PT_EMPLOYEE provides us with two methods for reading either a default set of infotypes or any other required infotype(s).

The infotypes are returned either in transparent form (default infotypes) or in semi-transparent form (structure PRELP "HR Master Data Buffer") which can be easily converted into their corresponding transparent form (table PAnnnn).

*&---------------------------------------------------------------------*
*& Report Z_SDN_CL_PT_EMPLOYEE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT z_sdn_cl_pt_employee.

TABLES: pa0000.

DATA:
go_employee TYPE REF TO cl_pt_employee,
* generic variables for retrieving infotype data
gd_infty TYPE infty,
gt_infty TYPE tim_tmw_itlist_tab,
gt_result TYPE tim_blp_request_tab,
gd_result LIKE LINE OF gt_result,
go_data TYPE REF TO cl_pt_td_itnnnn,
* specific infotype variables
gs_prelp TYPE prelp, " HR Master Data Buffer
gs_p0009 TYPE pa0009. " HR Master Record: Infotype 0009 (Bank
" Details)

FIELD-SYMBOLS:
TYPE ANY.

PARAMETERS:
p_pernr TYPE pa0000-pernr DEFAULT '00900222',
p_begda TYPE begda DEFAULT syst-datum,
p_endda TYPE endda DEFAULT syst-datum.



START-OF-SELECTION.

* Create an instance of the employee
go_employee ?= cl_pt_employee=>get_employee( p_pernr ).

* The class has a method GET_MASTER_DATA which returns several
* basic infotypes of the employee in a transparent form.
CALL METHOD go_employee->get_master_data
EXPORTING
im_begda = p_begda
im_endda = p_endda
* IMPORTING
* EX_I0000 = " infotype 0000 = Actions
* EX_I0001 = " infotype 0001 = Organizational Assignment
* EX_I0002 = " infotype 0002 = Personal Data
* EX_I0007 = " infotype 0007 = Planned Working Time
* EX_I0008 = " infotype 0008 = Basic Pay
.


* Using this method you can access any infotype(s)
APPEND '0009' TO gt_infty. " Bank Details
* APPEND '0010' TO gt_infty. " Capital Formation
CALL METHOD go_employee->if_pt_employee~get_infotypes
EXPORTING
i_itlist = gt_infty
i_fromdate = '20000101'
i_todate = syst-datum
* I_FILTER =
i_noauthcheck = ' ' " do authority check (if required)
IMPORTING
e_result = gt_result.
* E_RETCD =
.

* Method GET_INFOTYPES returns a list of infotype objects
* containing the infotype data in semi-transparent form
LOOP AT gt_result INTO gd_result.
go_data ?= gd_result->data.

* Get infotype in semi-transparent form
gs_prelp = go_data->if_pt_td_infotype~get_prelp( ).

* Convert: semi-transparent -> transparent infotype
CALL METHOD cl_hr_pnnnn_type_cast=>prelp_to_pnnnn
EXPORTING
prelp = gs_prelp
IMPORTING
pnnnn = gs_p0009.
ENDLOOP.

* Print-Out infotype data
DO.
ASSIGN COMPONENT syst-index OF STRUCTURE gs_p0009 TO .
IF ( syst-subrc NE 0 ).
EXIT.
ELSE.
WRITE: / .
ENDIF.
ENDDO.

END-OF-SELECTION.

SAP HR ABAP Logical Database

Logical database
A logical database is a special ABAP/4 program which combines the contents of certain database tables. Using logical databases facilitates the process of reading database tables.

HR Logical Database is PNP

Main Functions of the logical database PNP:

Standard Selection screen
Data Retrieval
Authorization check

To use logical database PNP in your program, specify in your program attributes.

Standard Selection Screen

Date selection
Date selection delimits the time period for which data is evaluated. GET PERNR retrieves all records of the relevant infotypes from the database. When you enter a date selection period, the PROVIDE loop retrieves the infotype records whose validity period overlaps with at least one day of this period.
Person selection
Person selection is the 'true' selection of choosing a group of employees for whom the report is to run.
Sorting Data
· The standard sort sequence lists personnel numbers in ascending order.
· SORT function allows you to sort the report data otherwise. All the sorting fields are from infotype 0001.
Report Class
· You can suppress input fields which are not used on the selection screen by assigning a report class to your program.
· If SAP standard delivered report classes do not satisfy your requirements, you can create your own report class through the IMG.

Data Retrieval from LDB

1. Create data structures for infotypes.

INFOTYPES: 0001, "ORG ASSIGNMENT
0002, "PERSONAL DATA
0008. "BASIC PAY

2. Fill data structures with the infotype records.

Start-of-selection.
GET PERNR.
End-0f-selection.

Read Master Data

Infotype structures (after GET PERNR) are internal tables loaded with data.
The infotype records (selected within the period) are processed sequentially by the PROVIDE - ENDPROVIDE loop.
GET PERNR.
PROVIDE * FROM Pnnnn BETWEEN PN/BEGDA AND PN/ENDDA
If Pnnnn-XXXX = ' '. write:/ Pnnnn-XXXX. endif.
ENDPROVIDE.

Period-Related Data
All infotype records are time stamped.

IT0006 (Address infotype)
01/01/1990 12/31/9999 present
Which record to be read depends on the date selection period specified on the
selection screen. PN/BEGDA PN/ENDDA.

Current Data
IT0006 Address - 01/01/1990 12/31/9999 present
RP-PROVIDE-FROM-LAST retrieves the record which is valid in the data selection period.

For example, pn/begda = '19990931' pn/endda = '99991231'
IT0006 subtype 1 is resident address
RP-PROVIDE-FROM-LAST P0006 1 PN/BEGDA PN/ENDDA.

Processing Payroll infotypes/Cluster in SAP HR ABAP

Database Tables in HR

1. Personnel Administration (PA) - master and time data infotype tables (transparent tables). PAnnnn: e.g. PA0001 for infotype 0001


2. Personnel Development (PD) - Org Unit, Job, Position, etc. (transparent tables). HRPnnnn: e.g. HRP1000 for infotype 1000


3. Time/Travel expense/Payroll/Applicant Tracking data/HR work areas/Documents (cluster PCLn: e.g. PCL2 for time/payroll results.

Cluster Table
· Cluster tables combine the data from several tables with identical (or almost identical) keys
into one physical record on the database.
. Data is written to a database in compressed form.
· Retrieval of data is very fast if the primary key is known.
· Cluster tables are defined in the data dictionary as transparent tables.
· External programs can NOT interpret the data in a cluster table.
· Special language elements EXPORT TO DATABASE, IMPORT TO DATABASE and DELETE
FROM DATABASE are used to process data in the cluster tables.

PCL1 - Database for HR work area;
PCL2 - Accounting Results (time, travel expense and payroll);
PCL3 - Applicant tracking data;
PCL4 - Documents, Payroll year-end Tax data

Database Tables PCLn
· PCLn database tables are divided into subareas known as data clusters.
· Data Clusters are identified by a two-character code. e.g RU for US payroll result, B2 for
time evaluation result...
· Each HR subarea has its own cluster.
· Each subarea has its own key.

Database Table PCL1
· The database table PCL1 contains the following data areas:
B1 time events/PDC
G1 group incentive wages
L1 individual incentive wages
PC personal calendar
TE travel expenses/payroll results
TS travel expenses/master data
TX infotype texts
ZI PDC interface -> cost account

Database Table PCL2
· The database table PCL2 contains the following data areas:
B2 time accounting results
CD cluster directory of the CD manager
PS generated schemas
PT texts for generated schemas
RX payroll accounting results/international
Rn payroll accounting results/country-specific ( n = HR country indicator )
ZL personal work schedule

Database Table PCL3
· The database table PCL3 contains the following data areas:
AP action log / time schedule
TY texts for applicant data infotypes

Data Management of PCLn
· The ABAP commands IMPORT and EXPORT are used for management of read/write to
database tables PCLn.
· A unique key has to be used when reading data from or writing data to the PCLn.
Field Name KEY Length Text
MANDT X 3 Client
RELID X 2 Relation ID (RU,B2..)
SRTFD X 40 Work Area Key
SRTF2 X 4 Sort key for dup. key

Cluster Definition
· The data definition of a work area for PCLn is specified in separate programs which comply
with fixed naming conventions.
· They are defined as INCLUDE programs (RPCnxxy0). The following naming convention applies:
n = 1 or 2 (PCL1 or PCL2)
xx = Relation ID (e.g. RX)
y = 0 for international clusters or country indicator (T500L) for different country cluster

Exporting Data (I)
· The EXPORT command causes one or more 'xy' KEY data objects to be written to cluster xy.
· The cluster definition is integrated with the INCLUDE statement.

REPORT ZHREXPRT.
TABLES: PCLn.
INCLUDE: RPCnxxy0. "Cluster definition
* Fill cluster KEY
xy-key-field = .
* Fill data object
....
* Export record
EXPORT TABLE1 TO DATABASE PCLn(xy) ID xy-KEY.
IF SY-SUBRC EQ 0.
WRITE: / 'Update successful'.
ENDIF.


Exporting Data (II)
. Export data using macro RP-EXP-Cn-xy.
· When data records are exported using macro, they are not written to the database but to a
main memory buffer.
· To save data, use the PREPARE_UPDATE routine with the USING parameter 'V'.

REPORT ZHREXPRT.
*Buffer definition
INCLUDE RPPPXD00. INCLUDE RPPPXM00. "Buffer management
DATA: BEGIN OF COMMON PART 'BUFFER'.
INCLUDE RPPPXD10.
DATA: END OF COMMON PART 'BUFFER'.
...
RP-EXP-Cn-xy.
IF SY-SUBRC EQ 0.
PERFORM PREPARE_UPDATE USING 'V'..
ENDIF.


Importing Data (I)
· The IMPORT command causes data objects with the specified key values to be read from
PCLn.
· If the import is successful, SY-SUBRC is 0; if not, it is 4.

REPORT RPIMPORT.
TABLES: PCLn.
INCLUDE RPCnxxy0. "Cluster definition

* Fill cluster Key

* Import record
IMPORT TABLE1 FROM DATABASE PCLn(xy) ID xy-KEY.
IF SY-SUBRC EQ 0.
* Display data object
ENDIF.

Importing data (II)
· Import data using macro RP-IMP-Cn-xy.
· Check return code SY-SUBRC. If 0, it is successful. If 4, error.
· Need include buffer management routines RPPPXM00

REPORT RPIMPORT.
*Buffer definition
INCLUDE RPPPXD00.
DATA: BEGIN OF COMMON PART 'BUFFER'.
INCLUDE RPPPXD10.
DATA: END OF COMMON PART 'BUFFER'.
*import data to buffer
RP-IMP-Cn-xy.
....
*Buffer management routines
INCLUDE RPPPXM00.

Cluster Authorization
· Simple EXPORT/IMPORT statement does not check for cluster authorization.
· Use EXPORT/IMPORT via buffer, the buffer management routines check for cluster
authorization.

Payroll Results (I)
· Payroll results are stored in cluster Rn of PCL2 as field string and internal tables.
n - country identifier.
· Standard reports read the results from cluster Rn. Report RPCLSTRn lists all payroll results;
report RPCEDTn0 lists the results on a payroll form.

Payroll Results (II)
· The cluster definition of payroll results is stored in two INLCUDE reports:
include: rpc2rx09. "Definition Cluster Ru (I)
include: rpc2ruu0. "Definition Cluster Ru (II)

The first INCLUDE defines the country-independent part; The second INCLUDE defines the country-specific part (US).

· The cluster key is stored in the field string RX-KEY.

Payroll Results (III)
· All the field string and internal tables stored in PCL2 are defined in the ABAP/4 dictionary. This
allows you to use the same structures in different definitions and nonetheless maintain data
consistency.
· The structures for cluster definition comply with the name convention PCnnn. Unfortunately,
'nnn' can be any set of alphanumeric characters.

*Key definition
DATA: BEGIN OF RX-KEY.
INCLUDE STRUCTURE PC200.
DATA: END OF RX-KEY.

*Payroll directory
DATA: BEGIN OF RGDIR OCCURS 100.
INCLUDE STRUCTURE PC261.
DATA: END OF RGDIR.


Payroll Cluster Directory
· To read payroll results, you need two keys: pernr and seqno
. You can get SEQNO by importing the cluster directory (CD) first.

REPORT ZHRIMPRT.
TABLES: PERNR, PCL1, PCL2.
INLCUDE: rpc2cd09. "definition cluster CD
PARAMETERS: PERSON LIKE PERNR-PERNR.
...
RP-INIT-BUFFER.
*Import cluster Directory
CD-KEY-PERNR = PERNR-PERNR.
RP-IMP-C2-CU.
CHECK SY-SUBRC = 0.
LOOP AT RGDIR.
RX-KEY-PERNR = PERSON.
UNPACK RGDIR-SEQNR TO RX-KEY-SEQNO.
*Import data from PCL2
RP-IMP-C2-RU.
INLCUDE: RPPPXM00. "PCL1/PCL2 BUFFER HANDLING

Function Module (I)
CD_EVALUATION_PERIODS
· After importing the payroll directory, which record to read is up to the programmer.
· Each payroll result has a status.
'P' - previous result
'A' - current (actual) result
'O' - old result

· Function module CD_EVALUATION_PERIODS will restore the payroll result status for a period
when that payroll is initially run. It also will select all the relevant periods to be evaluated.

Function Module (II)
CD_EVALUATION_PERIODS

call function 'CD_EVALUATION_PERIODS'
exporting
bonus_date = ref_periods-bondt
inper_modif = pn-permo
inper = ref_periods-inper
pay_type = ref_periods-payty
pay_ident = ref_periods-payid
tables
rgdir = rgdir
evpdir = evp
iabkrs = pnpabkrs
exceptions
no_record_found = 1.

Authorization Check
Authorization for Persons
· In the authorization check for persons, the system determines whether the user has the
authorizations required for the organizational features of the employees selected with
GET PERNR.
· Employees for which the user has no authorization are skipped and appear in a list at the end
of the report.
· Authorization object: 'HR: Master data'

Authorization for Data
· In the authorization check for data, the system determines whether the user is authorized to
read the infotypes specified in the report.
· If the authorization for a particular infotype is missing, the evaluation is terminated and an error
message is displayed.

Deactivating the Authorization Check
· In certain reports, it may be useful to deactivate the authorization check in order to improve
performance. (e.g. when running payroll)
· You can store this information in the object 'HR: Reporting'.

Archives