Wednesday, November 5, 2008

FM to Get Manager

FUNCTION z_hr_wf_rule_get_manager.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" TABLES
*" ACTOR_TAB STRUCTURE SWHACTOR
*" AC_CONTAINER STRUCTURE SWCONT
*" EXCEPTIONS
*" NOBODY_FOUND
*"----------------------------------------------------------------------
*
* Author: Jon Amos
* Date: 2007-02-12
*
* Description:
* This FM was built based on SWX_GET_MANAGER.
* That FM gets the manager for an HR Object (i.e. UserID, etc).
* However, it only looks up one level in the reporting structure.
* This FM keeps searching up the reporting structure until it finds
* a filled position (or reachs the top of the reportning structure).
*
*---------------------------------------------------------------------

* Container macros
INCLUDE .

* Constants (for checking relationship config)
CONSTANTS: co_pprel TYPE t77s0-grpid VALUE 'PPREL',
co_orgha TYPE t77s0-semid VALUE 'ORGHA'.

* Relationship subtype for 'Reports to' (to account for customer specific config)
DATA: lv_orgha LIKE plog-subty.

* Evaluation Path for 'Holder'
DATA: lv_eval_path TYPE t77aw-wegid, "THY160604#746188
lv_ev_is_internal TYPE xflag. "THY160604#746188

* Variables stored in container
DATA: lv_plvar TYPE hrsobid-plvar.
DATA: lv_otype TYPE hrsobid-otype.
DATA: lv_sobid TYPE hrsobid-sobid.
DATA: ls_org_object TYPE swhactor.

* Data objects for leading positions
DATA: lw_leadpos TYPE hrobject,
lt_leadpos TYPE TABLE OF hrobject,
lt_currpos TYPE TABLE OF hrobject.

FIELD-SYMBOLS: TYPE hrobject.

* Data objects for HR relationships
DATA: lt_relations TYPE TABLE OF p1001.

FIELD-SYMBOLS: TYPE p1001.

* Holders table
DATA: result_tab TYPE TABLE OF swhactor.


* Initialize
CLEAR actor_tab.
REFRESH actor_tab.


* Get the HR object to start from
swc_get_element ac_container 'ORG_OBJECT' ls_org_object.
IF sy-subrc > 0 OR
ls_org_object IS INITIAL.
swc_get_element ac_container 'OTYPE' lv_otype.
swc_get_element ac_container 'OBJID' lv_sobid.
ELSE.
lv_otype = ls_org_object-otype.
lv_sobid = ls_org_object-objid.
ENDIF.

IF lv_otype IS INITIAL OR lv_sobid IS INITIAL.
* Msg: There is no chief position for & &.
MESSAGE e802(5w) WITH lv_otype lv_sobid RAISING nobody_found.
ENDIF.


* Get the Active WF Org Plan
swc_get_element ac_container 'PLVAR' lv_plvar.

IF lv_plvar IS INITIAL.
CALL FUNCTION 'RH_GET_ACTIVE_WF_PLVAR'
IMPORTING
act_plvar = lv_plvar
EXCEPTIONS
no_active_plvar = 1.

IF sy-subrc > 0.
* Msg: No active plan version exists
MESSAGE e300(5w) RAISING nobody_found.
ENDIF.
ENDIF.


* Check for customer specific 'Reports to' relationship (A002)
CALL FUNCTION 'HR_READ_T77S0'
EXPORTING
grpid = co_pprel
semid = co_orgha
IMPORTING
gsval = lv_orgha
EXCEPTIONS
entry_not_found = 1
OTHERS = 2.

* Default 'Reports to' relationship if not determined
IF sy-subrc <> 0 OR lv_orgha IS INITIAL.
lv_orgha = 'A002'.
ENDIF.


* Define 'Holder' relationship
* THY 180604 Note 746188 {
lv_eval_path = 'SAP_TAGT'.

CALL FUNCTION 'RH_PBUILD_WORKFLOW'
EXPORTING
iv_evaluation_path = lv_eval_path
IMPORTING
ev_mod_ev = lv_eval_path
ev_is_internal = lv_ev_is_internal.
* }


*-----
* (Jon Amos) NOTE:
* We never want to consider vacant positions since we will check this ourselves
*-----
* consider vacant positions flag in table T77S0
* wflow-vapos
* PERFORM re77s0(mstt77s0) USING 'WFLOW' 'VAPOS' lv_sw_vapos lv_subrc.

* Get leading position(s) for passed HR object -- one level up in Reporting Structure
CALL FUNCTION 'RH_GET_LEADING_POSITION'
EXPORTING
plvar = lv_plvar
otype = lv_otype
sobid = lv_sobid
date = sy-datum
auth = ' '
consider_vac_pos = space
TABLES
leading_pos = lt_leadpos
EXCEPTIONS
no_lead_pos_found = 1
OTHERS = 2.

* Search up the Reporting Structure for the first filled position
DO.

* Have we run out of positions to check (without finding a holder)?
IF lt_leadpos[] IS INITIAL.
* Msg: There is no chief position for & &.
MESSAGE e802(5w) WITH lv_otype lv_sobid RAISING nobody_found.
ENDIF.

* Process the leading position(s)
LOOP AT lt_leadpos ASSIGNING .

* Get the holder(s) of the position
CALL FUNCTION 'RH_STRUC_GET'
EXPORTING
act_otype = -otype
act_objid = -objid
act_wegid = lv_eval_path "THY180604#746188
act_int_flag = lv_ev_is_internal "THY180604#746188
TABLES
result_tab = result_tab
EXCEPTIONS
no_plvar_found = 1
no_entry_found = 2
OTHERS = 3.

* Add the holder(s) to the Actor_tab
APPEND LINES OF result_tab TO actor_tab.

ENDLOOP. "loop at lt_leadpos...

IF actor_tab[] IS NOT INITIAL.
* Stop looking
EXIT.

ELSE.
* The leading positions become the current positions
lt_currpos = lt_leadpos.

* Initialize
CLEAR lt_leadpos.

* Get the leading positions for current leading positions
CALL FUNCTION 'RH_READ_INFTY_1001'
EXPORTING
istat = '1'
subty = lv_orgha
begda = sy-datum
endda = sy-datum
TABLES
i1001 = lt_relations
OBJECTS = lt_currpos
EXCEPTIONS
nothing_found = 1
wrong_condition = 2
OTHERS = 3.

* Build table for next loop pass
IF sy-subrc = 0.
LOOP AT lt_relations ASSIGNING .
CLEAR lw_leadpos.
lw_leadpos-plvar = -plvar.
lw_leadpos-otype = -sclas.
lw_leadpos-objid = -sobid.
APPEND lw_leadpos TO lt_leadpos.
ENDLOOP.
ENDIF. "IF sy-subrc....
ENDIF. "IF actor_tab[]....
ENDDO.

* Only keep the Actors that are Persons or Users
* THY 180604 Note 746188
DELETE actor_tab WHERE otype <> 'P' AND
otype <> 'US'.
* }

ENDFUNCTION.

No comments: