SAP ABAP - Purchase Order Change Condition Value BADI ME_PROCESS_PO_CUST
METHOD if_ex_me_process_po_cust~process_item.
FIELD-SYMBOLS: <ekko> TYPE ekko.
DATA: f_mepoitem TYPE mepoitem.
DATA: lt_mepoitem TYPE TABLE OF mepoitem.
DATA: fo_header TYPE REF TO if_purchase_order_mm.
DATA: f_checkheader TYPE mepoheader.
DATA: f_checkheader_curr TYPE mepoheader.
DATA: f_ekkotxt TYPE string.
TYPES : ty_komv TYPE TABLE OF komv.
DATA:
lo_services_mm TYPE REF TO if_services_mm,
gt_komv TYPE ty_komv,
gt_komv_old TYPE ty_komv,
zkposn TYPE prcd_elements-kposn,
lv_amount TYPE komv-kbetr,
lv_count TYPE i.
FIELD-SYMBOLS:
<fs_acctv> TYPE tact-actvt,
<fs_tkomv> TYPE ty_komv.
DATA: BEGIN OF fs_condcheck,
set TYPE c,
ebelp TYPE ekpo-ebelp,
END OF fs_condcheck.
DATA: ft_condcheck LIKE STANDARD TABLE OF fs_condcheck.
CHECK im_item IS NOT INITIAL.
f_mepoitem = im_item->get_data( ).
f_ekkotxt = '(SAPLMEPO)EKKO'.
ASSIGN (f_ekkotxt) TO <ekko>.
IF sy-subrc EQ 0.
IF <ekko>-bsart = 'ZDOP'.
IMPORT gt_komv_old = gt_komv_old
FROM MEMORY ID 'ZPOCOND'.
IMPORT lt_mepoitem = lt_mepoitem
FROM MEMORY ID 'ZPOITM'.
ASSIGN ('(SAPLMEPO)TKOMV[]') TO <fs_tkomv>.
IF <fs_tkomv> IS ASSIGNED.
gt_komv = <fs_tkomv>.
ENDIF.
IF f_mepoitem-konnr IS NOT INITIAL.
SELECT SINGLE zzroute, zzdistance, netpr
FROM ekpo
INTO @DATA(ls_contract)
WHERE ebeln = @f_mepoitem-konnr AND
ebelp = @f_mepoitem-ktpnr.
IF f_mepoitem-zzroute IS INITIAL.
f_mepoitem-zzroute = ls_contract-zzroute.
ENDIF.
IF f_mepoitem-zzdistance IS INITIAL.
f_mepoitem-zzdistance = ls_contract-zzdistance.
ENDIF.
ENDIF.
IF f_mepoitem-anzpu IS INITIAL.
f_mepoitem-anzpu = f_mepoitem-zzdistance.
ELSE.
f_mepoitem-zzdistance = f_mepoitem-anzpu.
ENDIF.
CALL METHOD im_item->set_data
EXPORTING
im_data = f_mepoitem.
DATA lv_valid TYPE mmpur_bool.
CLEAR lv_valid.
lv_valid = im_item->is_valid( ).
SELECT SINGLE * FROM tvarvc
INTO @DATA(ls_matkl)
WHERE name = 'ZMATKL_DIST_PRICE' AND
low = @f_mepoitem-matkl.
IF ls_matkl IS NOT INITIAL AND lv_valid = 'X'.
CLEAR zkposn.
zkposn = f_mepoitem-ebelp.
CLEAR lv_amount.
DATA: ft_conditionsx TYPE mmpur_tkomv.
im_item->get_conditions( IMPORTING ex_conditions = ft_conditionsx[] ).
LOOP AT ft_conditionsx ASSIGNING FIELD-SYMBOL(<fs_check_cond>)
WHERE kschl = 'ZPBD'.
READ TABLE gt_komv_old ASSIGNING FIELD-SYMBOL(<fs_cond_old>)
WITH KEY kposn = zkposn kschl = 'ZPBD'.
IF sy-subrc = 0.
IF <fs_check_cond>-kbetr = <fs_cond_old>-kbetr.
READ TABLE lt_mepoitem ASSIGNING FIELD-SYMBOL(<fs_old_po_item>)
WITH KEY ebelp = f_mepoitem-ebelp.
IF sy-subrc = 0.
IF f_mepoitem-anzpu <> <fs_old_po_item>-anzpu.
lv_amount = ls_contract-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ELSE.
CLEAR fs_condcheck.
fs_condcheck-set = 'X'.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
ELSE.
CLEAR fs_condcheck.
fs_condcheck-set = 'X'.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
ELSE.
lv_amount = f_mepoitem-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
ELSE.
IF sy-tcode = 'ME21N'.
lv_amount = f_mepoitem-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ELSE.
SELECT SINGLE * FROM ekpo
INTO @DATA(ls_ekpo)
WHERE ebeln = @f_mepoitem-ebeln AND ebelp = @f_mepoitem-ebelp.
IF ls_ekpo-anzpu <> f_mepoitem-anzpu.
lv_amount = ls_contract-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ELSE.
CLEAR fs_condcheck.
fs_condcheck-set = 'X'.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
SELECT * FROM ekpo INTO CORRESPONDING FIELDS OF TABLE lt_mepoitem
WHERE ebeln = f_mepoitem-ebeln AND ebelp = f_mepoitem-ebelp.
ENDIF.
ENDIF.
ENDLOOP.
CLEAR ls_contract.
IF fs_condcheck-set IS INITIAL.
DATA: ft_conditions TYPE mmpur_tkomv.
DATA: f_cond_changed TYPE c.
FIELD-SYMBOLS: <komv> TYPE komv.
CLEAR f_cond_changed.
"get condition for item
im_item->get_conditions( IMPORTING ex_conditions = ft_conditions[] ).
"our manipulation of conditions (do your coding here)
LOOP AT ft_conditions ASSIGNING <komv>
WHERE kschl = 'ZPBD'.
CLEAR <komv>-kbetr.
<komv>-kbetr = lv_amount.
<komv>-kmprs = 'X'.
f_cond_changed = 'X'.
ENDLOOP.
"check if we changed something
IF f_cond_changed IS NOT INITIAL.
*
im_item->set_conditions( EXPORTING im_conditions = ft_conditions[] ).
fs_condcheck-set = 'X'.
READ TABLE lt_mepoitem
WITH KEY ebeln = f_mepoitem-ebeln ebelp = f_mepoitem-ebelp TRANSPORTING NO FIELDS.
IF sy-subrc <> 0.
APPEND f_mepoitem TO lt_mepoitem.
ELSE.
MODIFY lt_mepoitem FROM f_mepoitem
TRANSPORTING anzpu
WHERE ebelp = f_mepoitem-ebelp.
ENDIF.
LOOP AT ft_conditions ASSIGNING FIELD-SYMBOL(<fs_ins_cond>).
READ TABLE gt_komv_old WITH KEY
kschl = <fs_ins_cond>-kschl
kposn = <fs_ins_cond>-kposn TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
DELETE gt_komv_old WHERE
kschl = <fs_ins_cond>-kschl AND
kposn = <fs_ins_cond>-kposn.
ENDIF.
APPEND <fs_ins_cond> TO gt_komv_old.
ENDLOOP.
EXPORT gt_komv_old = gt_komv_old
TO MEMORY ID 'ZPOCOND'.
EXPORT lt_mepoitem = lt_mepoitem
TO MEMORY ID 'ZPOITM'.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDMETHOD.
FIELD-SYMBOLS: <ekko> TYPE ekko.
DATA: f_mepoitem TYPE mepoitem.
DATA: lt_mepoitem TYPE TABLE OF mepoitem.
DATA: fo_header TYPE REF TO if_purchase_order_mm.
DATA: f_checkheader TYPE mepoheader.
DATA: f_checkheader_curr TYPE mepoheader.
DATA: f_ekkotxt TYPE string.
TYPES : ty_komv TYPE TABLE OF komv.
DATA:
lo_services_mm TYPE REF TO if_services_mm,
gt_komv TYPE ty_komv,
gt_komv_old TYPE ty_komv,
zkposn TYPE prcd_elements-kposn,
lv_amount TYPE komv-kbetr,
lv_count TYPE i.
FIELD-SYMBOLS:
<fs_acctv> TYPE tact-actvt,
<fs_tkomv> TYPE ty_komv.
DATA: BEGIN OF fs_condcheck,
set TYPE c,
ebelp TYPE ekpo-ebelp,
END OF fs_condcheck.
DATA: ft_condcheck LIKE STANDARD TABLE OF fs_condcheck.
CHECK im_item IS NOT INITIAL.
f_mepoitem = im_item->get_data( ).
f_ekkotxt = '(SAPLMEPO)EKKO'.
ASSIGN (f_ekkotxt) TO <ekko>.
IF sy-subrc EQ 0.
IF <ekko>-bsart = 'ZDOP'.
IMPORT gt_komv_old = gt_komv_old
FROM MEMORY ID 'ZPOCOND'.
IMPORT lt_mepoitem = lt_mepoitem
FROM MEMORY ID 'ZPOITM'.
ASSIGN ('(SAPLMEPO)TKOMV[]') TO <fs_tkomv>.
IF <fs_tkomv> IS ASSIGNED.
gt_komv = <fs_tkomv>.
ENDIF.
IF f_mepoitem-konnr IS NOT INITIAL.
SELECT SINGLE zzroute, zzdistance, netpr
FROM ekpo
INTO @DATA(ls_contract)
WHERE ebeln = @f_mepoitem-konnr AND
ebelp = @f_mepoitem-ktpnr.
IF f_mepoitem-zzroute IS INITIAL.
f_mepoitem-zzroute = ls_contract-zzroute.
ENDIF.
IF f_mepoitem-zzdistance IS INITIAL.
f_mepoitem-zzdistance = ls_contract-zzdistance.
ENDIF.
ENDIF.
IF f_mepoitem-anzpu IS INITIAL.
f_mepoitem-anzpu = f_mepoitem-zzdistance.
ELSE.
f_mepoitem-zzdistance = f_mepoitem-anzpu.
ENDIF.
CALL METHOD im_item->set_data
EXPORTING
im_data = f_mepoitem.
DATA lv_valid TYPE mmpur_bool.
CLEAR lv_valid.
lv_valid = im_item->is_valid( ).
SELECT SINGLE * FROM tvarvc
INTO @DATA(ls_matkl)
WHERE name = 'ZMATKL_DIST_PRICE' AND
low = @f_mepoitem-matkl.
IF ls_matkl IS NOT INITIAL AND lv_valid = 'X'.
CLEAR zkposn.
zkposn = f_mepoitem-ebelp.
CLEAR lv_amount.
DATA: ft_conditionsx TYPE mmpur_tkomv.
im_item->get_conditions( IMPORTING ex_conditions = ft_conditionsx[] ).
LOOP AT ft_conditionsx ASSIGNING FIELD-SYMBOL(<fs_check_cond>)
WHERE kschl = 'ZPBD'.
READ TABLE gt_komv_old ASSIGNING FIELD-SYMBOL(<fs_cond_old>)
WITH KEY kposn = zkposn kschl = 'ZPBD'.
IF sy-subrc = 0.
IF <fs_check_cond>-kbetr = <fs_cond_old>-kbetr.
READ TABLE lt_mepoitem ASSIGNING FIELD-SYMBOL(<fs_old_po_item>)
WITH KEY ebelp = f_mepoitem-ebelp.
IF sy-subrc = 0.
IF f_mepoitem-anzpu <> <fs_old_po_item>-anzpu.
lv_amount = ls_contract-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ELSE.
CLEAR fs_condcheck.
fs_condcheck-set = 'X'.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
ELSE.
CLEAR fs_condcheck.
fs_condcheck-set = 'X'.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
ELSE.
lv_amount = f_mepoitem-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
ELSE.
IF sy-tcode = 'ME21N'.
lv_amount = f_mepoitem-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ELSE.
SELECT SINGLE * FROM ekpo
INTO @DATA(ls_ekpo)
WHERE ebeln = @f_mepoitem-ebeln AND ebelp = @f_mepoitem-ebelp.
IF ls_ekpo-anzpu <> f_mepoitem-anzpu.
lv_amount = ls_contract-netpr * f_mepoitem-zzdistance.
CLEAR fs_condcheck.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ELSE.
CLEAR fs_condcheck.
fs_condcheck-set = 'X'.
fs_condcheck-ebelp = f_mepoitem-ebelp.
ENDIF.
SELECT * FROM ekpo INTO CORRESPONDING FIELDS OF TABLE lt_mepoitem
WHERE ebeln = f_mepoitem-ebeln AND ebelp = f_mepoitem-ebelp.
ENDIF.
ENDIF.
ENDLOOP.
CLEAR ls_contract.
IF fs_condcheck-set IS INITIAL.
DATA: ft_conditions TYPE mmpur_tkomv.
DATA: f_cond_changed TYPE c.
FIELD-SYMBOLS: <komv> TYPE komv.
CLEAR f_cond_changed.
"get condition for item
im_item->get_conditions( IMPORTING ex_conditions = ft_conditions[] ).
"our manipulation of conditions (do your coding here)
LOOP AT ft_conditions ASSIGNING <komv>
WHERE kschl = 'ZPBD'.
CLEAR <komv>-kbetr.
<komv>-kbetr = lv_amount.
<komv>-kmprs = 'X'.
f_cond_changed = 'X'.
ENDLOOP.
"check if we changed something
IF f_cond_changed IS NOT INITIAL.
*
im_item->set_conditions( EXPORTING im_conditions = ft_conditions[] ).
fs_condcheck-set = 'X'.
READ TABLE lt_mepoitem
WITH KEY ebeln = f_mepoitem-ebeln ebelp = f_mepoitem-ebelp TRANSPORTING NO FIELDS.
IF sy-subrc <> 0.
APPEND f_mepoitem TO lt_mepoitem.
ELSE.
MODIFY lt_mepoitem FROM f_mepoitem
TRANSPORTING anzpu
WHERE ebelp = f_mepoitem-ebelp.
ENDIF.
LOOP AT ft_conditions ASSIGNING FIELD-SYMBOL(<fs_ins_cond>).
READ TABLE gt_komv_old WITH KEY
kschl = <fs_ins_cond>-kschl
kposn = <fs_ins_cond>-kposn TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
DELETE gt_komv_old WHERE
kschl = <fs_ins_cond>-kschl AND
kposn = <fs_ins_cond>-kposn.
ENDIF.
APPEND <fs_ins_cond> TO gt_komv_old.
ENDLOOP.
EXPORT gt_komv_old = gt_komv_old
TO MEMORY ID 'ZPOCOND'.
EXPORT lt_mepoitem = lt_mepoitem
TO MEMORY ID 'ZPOITM'.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDMETHOD.
Comments
Post a Comment