You are on page 1of 7

*&---------------------------------------------------------------------*

*& Report ZHTTUM_REPAIR_BUT050


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZHTTUM_REPAIR_BUT050
LINE-SIZE 100.

TYPES: BEGIN OF bp_double_struct,


bp TYPE bu_partner,
pernr TYPE hrobjid,
cp TYPE hrobjid,
bp_org TYPE bu_partner,
ambigous TYPE flag,
differing TYPE flag,
END OF bp_double_struct.

PARAMETERS: bp_emp TYPE but050-partner2.


PARAMETERS: lv_test TYPE flag DEFAULT 'X'.

DATA: lt_but050 TYPE STANDARD TABLE OF but050,


ls_but050 TYPE but050,
lt_doubles TYPE STANDARD TABLE OF but050,
lt_delete_but052 TYPE STANDARD TABLE OF but052,
ls_temp1 TYPE but050,
lv_erp_p_bp_active TYPE flag,
lv_in_double_mode TYPE c,
lv_partner TYPE bu_partner,
lt_doubles_bp TYPE STANDARD TABLE OF bp_double_struct,
ls_doubles_bp TYPE bp_double_struct,
lt_objext TYPE STANDARD TABLE OF hrsobid,
ls_objext TYPE hrsobid,
lt_object TYPE hrtb_objkey,
ls_object TYPE hrobject,
lt_p1001 TYPE STANDARD TABLE OF p1001,
ls_p1001 TYPE p1001,
lt_i1001 TYPE STANDARD TABLE OF hri1001,
lv_plvar TYPE plvar,
lt_path TYPE STANDARD TABLE OF t77aw,
ls_path TYPE t77aw,
lv_wegid TYPE wegid,
ls_object_roots TYPE hrrootob,
lt_object_roots TYPE STANDARD TABLE OF hrrootob,
lt_sturc_result TYPE STANDARD TABLE OF objec,
lt_return TYPE bapiret2_t,
lv_counter TYPE i,
lv_len TYPE i.

FIELD-SYMBOLS: <ls_but050> TYPE but050,


<ls_bp_double_struct> TYPE bp_double_struct,
<ls_hri1001> TYPE hri1001,
<ls_p1001> TYPE p1001,
<ls_objec> TYPE objec,
<ls_return> TYPE bapiret2.

********* INITIALIZATION
***********************************************
INITIALIZATION.

CALL FUNCTION 'RH_GET_ACTIVE_WF_PLVAR'


IMPORTING
act_plvar = lv_plvar
EXCEPTIONS
OTHERS = 0.

CALL FUNCTION 'HR_ALEOX_GET_SETTINGS'


IMPORTING
erp_p_bp_active = lv_erp_p_bp_active.

********* PROCESSING
***************************************************
START-OF-SELECTION.

IF bp_emp IS INITIAL.
SELECT * FROM but050
INTO TABLE lt_but050
WHERE reltyp = 'BUR010'.
ELSE.
SELECT * FROM but050
INTO TABLE lt_but050
WHERE reltyp = 'BUR010'
AND partner2 = bp_emp.
ENDIF.

* get all BPemps with multiple entries in BUT050


* lt_doubles[] contains all but050 emtries of BPemps
* lt_doubles_bp[] is a list of all BPemps with multiple entries
IF bp_emp IS INITIAL.
SORT lt_but050 BY partner2.
LOOP AT lt_but050 ASSIGNING <ls_but050>.
IF lv_partner EQ <ls_but050>-partner2.
IF lv_in_double_mode IS INITIAL.
APPEND ls_temp1 TO lt_doubles. "first but050 entry
ls_doubles_bp-bp = <ls_but050>-partner2.
APPEND ls_doubles_bp TO lt_doubles_bp.
ENDIF.
APPEND <ls_but050> TO lt_doubles. "second...n but050 entry
lv_in_double_mode = 'X'.
ELSE.
lv_partner = <ls_but050>-partner2.
ls_temp1 = <ls_but050>.
CLEAR lv_in_double_mode.
ENDIF.
ENDLOOP.
ELSE.
READ TABLE lt_but050 ASSIGNING <ls_but050> INDEX 1.
IF sy-subrc EQ 0.
ls_doubles_bp-bp = <ls_but050>-partner2.
APPEND ls_doubles_bp TO lt_doubles_bp.
APPEND LINES OF lt_but050[] TO lt_doubles.
ENDIF.
ENDIF.

* build up lt_objext
LOOP AT lt_doubles_bp ASSIGNING <ls_bp_double_struct>.
ls_objext-plvar = lv_plvar.
ls_objext-otype = 'BP'.
ls_objext-sobid = <ls_bp_double_struct>-bp.
APPEND ls_objext TO lt_objext.
ENDLOOP.

* read corresponding CPs


IF NOT lt_objext IS INITIAL.
CALL FUNCTION 'RH_READ_INFTY_1001_EXT'
EXPORTING
authority = space
with_stru_auth = space
istat = '1'
extend = 'X'
subty = 'A207'
begda = sy-datum
endda = sy-datum
sort = ' '
with_ev_for_int = ' '
with_ev_for_ext = ' '
adata = ' '
TABLES
i1001 = lt_i1001
OBJECTS = lt_objext
EXCEPTIONS
nothing_found = 1
wrong_condition = 2
OTHERS = 3.
ENDIF.

* put CP number to lt_doubles_bp


SORT lt_doubles_bp BY bp.
LOOP AT lt_i1001 ASSIGNING <ls_hri1001>.
READ TABLE lt_doubles_bp WITH KEY bp = <ls_hri1001>-objid
ASSIGNING <ls_bp_double_struct>
BINARY SEARCH.
IF sy-subrc EQ 0.
<ls_bp_double_struct>-cp = <ls_hri1001>-sobid.
ENDIF.
ENDLOOP.

* build up lt_path for CP ->(P->) S -> O -> BP


IF lv_erp_p_bp_active IS INITIAL.
* CP -> S
ls_path-wegnr = '1'.
ls_path-otype = 'CP'.
ls_path-rsign = 'B'.
ls_path-relat = '008'.
ls_path-priox = '*'.
ls_path-sclas = 'S'.
ls_path-skipf = 'X'.
APPEND ls_path TO lt_path.
ELSE.
* CP -> P
ls_path-wegnr = '1'.
ls_path-otype = 'CP'.
ls_path-rsign = 'B'.
ls_path-relat = '209'.
ls_path-priox = '*'.
ls_path-sclas = 'P'.
ls_path-skipf = 'X'.
APPEND ls_path TO lt_path.
* P -> S
ls_path-wegnr = '1'.
ls_path-otype = 'P'.
ls_path-rsign = 'B'.
ls_path-relat = '008'.
ls_path-priox = '*'.
ls_path-sclas = 'S'.
ls_path-skipf = 'X'.
APPEND ls_path TO lt_path.
ENDIF.
* S -> O
ls_path-wegnr = '2'.
ls_path-otype = 'S'.
ls_path-rsign = 'A'.
ls_path-relat = '003'.
ls_path-priox = '*'.
ls_path-sclas = 'O'.
ls_path-skipf = 'X'.
APPEND ls_path TO lt_path.
* O -> BP
ls_path-wegnr = '3'.
ls_path-otype = 'O'.
ls_path-rsign = 'B'.
ls_path-relat = '207'.
ls_path-priox = '*'.
ls_path-sclas = 'BP'.
ls_path-skipf = ''.
APPEND ls_path TO lt_path.
* build path
CALL FUNCTION 'PATH_BUILD'
IMPORTING
path_id = lv_wegid
TABLES
77aw = lt_path.

* build up lt_object_roots
LOOP AT lt_doubles_bp ASSIGNING <ls_bp_double_struct>
WHERE NOT cp IS INITIAL.
ls_object_roots-otype = 'CP'.
ls_object_roots-objid = <ls_bp_double_struct>-cp.
APPEND ls_object_roots TO lt_object_roots.
ENDLOOP.

* get BPs in role org.-unit


IF NOT lt_object_roots IS INITIAL.
CALL FUNCTION 'RH_STRUC_GET_MULTIPLE_ROOTS'
EXPORTING
act_wegid = lv_wegid
act_int_flag = 'X'
act_plvar = lv_plvar
authority_check = space
TABLES
root_objects = lt_object_roots
result_objec = lt_sturc_result
EXCEPTIONS
no_plvar_found = 1
no_entry_found = 2
path_not_found = 3
root_not_found = 4
OTHERS = 5.
ENDIF.

* analyze lt_sturc_result
SORT lt_doubles_bp BY cp.
SORT lt_doubles[] BY partner2 partner1.
LOOP AT lt_sturc_result ASSIGNING <ls_objec>.
CASE <ls_objec>-otype.
WHEN 'CP'.
lv_counter = 0.
READ TABLE lt_doubles_bp WITH KEY cp = <ls_objec>-objid
ASSIGNING <ls_bp_double_struct>
BINARY SEARCH.
IF sy-subrc NE 0.
UNASSIGN <ls_bp_double_struct>.
ENDIF.
WHEN 'BP'. "BPorg
IF <ls_bp_double_struct> IS ASSIGNED.
* set info about # existing assignments
lv_counter = lv_counter + 1.
IF lv_counter EQ 1.
* BPemp with single assignments (CP-S-O)
<ls_bp_double_struct>-ambigous = '1'.
<ls_bp_double_struct>-bp_org = <ls_objec>-realo.
ELSEIF lv_counter GE 2.
* BPemp with multiple assignments (CP-S-O)
<ls_bp_double_struct>-ambigous = 'X'.
CLEAR <ls_bp_double_struct>-bp_org.
ENDIF.
* remove coorect relationship from lt_doubles[]
LOOP AT lt_doubles[] ASSIGNING <ls_but050>
WHERE partner1 EQ <ls_objec>-realo AND
partner2 EQ <ls_bp_double_struct>-bp.
DELETE lt_doubles[].
ENDLOOP.
ENDIF.
ENDCASE.
ENDLOOP.

* get employee numbers (SRM@ERP)


CLEAR: lt_object[], lt_i1001[], lt_p1001[].
LOOP AT lt_doubles_bp ASSIGNING <ls_bp_double_struct>.
ls_object-plvar = lv_plvar.
ls_object-otype = 'CP'.
ls_object-objid = <ls_bp_double_struct>-cp.
APPEND ls_object TO lt_object.
ENDLOOP.
SORT lt_object BY plvar otype objid.
DELETE ADJACENT DUPLICATES FROM lt_object.
CALL FUNCTION 'RH_READ_INFTY_1001'
EXPORTING
authority = space
with_stru_auth = space
istat = '1'
subty = 'B209'
begda = sy-datum
endda = sy-datum
TABLES
i1001 = lt_p1001
OBJECTS = lt_object
EXCEPTIONS
nothing_found = 1
wrong_condition = 2
wrong_parameters = 3
OTHERS = 4.
SORT lt_p1001 BY objid.
IF NOT lt_p1001[] IS INITIAL.
LOOP AT lt_doubles_bp ASSIGNING <ls_bp_double_struct>.
READ TABLE lt_p1001 WITH KEY objid = <ls_bp_double_struct>-cp
ASSIGNING <ls_p1001>
BINARY SEARCH.
IF sy-subrc EQ 0.
<ls_bp_double_struct>-pernr = <ls_p1001>-sobid.
ENDIF.
ENDLOOP.
ENDIF.

********* OUTPUT ***************************************************


END-OF-SELECTION.
IF lv_test EQ 'X'.
WRITE: / 'test run, no data has been modified'.
WRITE: /.
WRITE: / 'business partners having inconsistent entries in BUT050: (to be
removed)'.
WRITE: /.
SORT lt_doubles_bp[] BY bp.
lv_counter = 0.
LOOP AT lt_doubles_bp ASSIGNING <ls_bp_double_struct>.
READ TABLE lt_doubles[] ASSIGNING <ls_but050>
WITH KEY partner2 = <ls_bp_double_struct>-bp
BINARY SEARCH.
IF sy-subrc EQ 0.
* inconsistent relationships exist for BPemp
WRITE: / 'BPorg:',<ls_but050>-partner1,'BPemp:',<ls_but050>-partner2,
'employee number: ',<ls_bp_double_struct>-pernr.
ENDIF.
ENDLOOP.

ELSE.
WRITE: / 'no test run, data has been modified'.
WRITE: /.
WRITE: / 'business partners having inconsistent entries in BUT050: (will be
deleted)'.
WRITE: /.

******* MODIFY DATABASE **********************************************


LOOP AT lt_doubles ASSIGNING <ls_but050>.
CLEAR lt_return[].
CALL FUNCTION 'BUPR_EMPLO_DELETE'
EXPORTING
iv_partner = <ls_but050>-partner1
iv_employee = <ls_but050>-partner2
iv_date_from = <ls_but050>-date_from
iv_date_to = <ls_but050>-date_to
TABLES
et_return = lt_return.
DELETE lt_return WHERE type <> 'E'
AND type <> 'A'
AND type <> 'X'.
IF lt_return[] IS INITIAL.
CALL FUNCTION 'BUPA_CENTRAL_EXPL_SAVE_HR'.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
WRITE: / 'BPorg:',<ls_but050>-partner1,'BPemp:',<ls_but050>-partner2.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LOOP AT lt_return[] ASSIGNING <ls_return>.
lv_len = strlen( <ls_return>-message ).
WRITE: / 'Error during deletion of relationship BPorg:',<ls_but050>-
partner1,
'BPemp:',<ls_but050>-
partner2,
/ 'Message:',<ls_return>-message(lv_len),'(',<ls_return>-
type,<ls_return>-number,<ls_return>-id,')'.
ENDLOOP.
ENDIF.
ENDLOOP.
ENDIF.