Trước tiên để thực hiện việc cập nhật giá mua chúng ta cần đọc dữ liệu InfoRecord tại các bảng EINA và EINE, thông qua
EINA: Thông tin chung về InfoRecord
EINE: Dữ liệu về đơn vị tổ chức mua hàng
Về nguyên tắc mỗi một mặt hàng sẽ có một giá mua riêng tương ứng với từng nhà cung cấp. Cho nên trước tiên chúng ta cần đọc thông tin giá mua nhà cung cấp trong bảng EINA thông qua mã hàng hóa và mã nhà cung cấp
Code: Select all
SELECT * FROM eina
FOR ALL ENTRIES IN @lt_eina
WHERE matnr = @lv_matnr AND loekz IS INITIAL
Code: Select all
IF gt_eina IS NOT INITIAL.
SELECT * FROM eine
FOR ALL ENTRIES IN @gt_eina
WHERE infnr = @gt_eina-infnr AND ekorg = @gv_porg AND werks = @gv_plant
INTO TABLE @gt_eine.
ENDIF.
Khai báo các đổi tượng cần thiết
Code: Select all
DATA: lt_ret TYPE fs4mig_t_bapiret2,
lt_validity TYPE mewivalidity_tt,
lt_condition TYPE mewicondition_tt,
lt_eina TYPE mewieina_mig_t,
lt_eine TYPE mewieine_t,
lt_einax TYPE mewieinax_t,
lt_einex TYPE mewieinex_t.
DATA: ls_eina TYPE mewieina,
ls_einax TYPE mewieinax,
ls_eine TYPE mewieine,
ls_einex TYPE mewieinex.
DATA: ls_validity TYPE mewivalidity,
ls_condition TYPE mewicondition.
Code: Select all
ls_eina-material = is_eina-matnr.
ls_einax-material = 'X'.
ls_eina-vendor = is_eina-lifnr.
ls_einax-vendor = 'X'.
ls_eina-base_uom = is_eina-meins.
ls_einax-base_uom = 'X'.
APPEND INITIAL LINE TO lt_eina ASSIGNING FIELD-SYMBOL(<fs_eina>).
MOVE-CORRESPONDING ls_eina TO <fs_eina>.
APPEND INITIAL LINE TO lt_einax ASSIGNING FIELD-SYMBOL(<fs_einax>).
MOVE-CORRESPONDING ls_eina TO <fs_einax>.
Code: Select all
ls_eine-plant = gv_plant.
ls_einex-plant = 'X'.
ls_eine-purch_org = is_eine-ekorg.
ls_einex-purch_org = 'X'.
ls_eine-info_type = '0'.
ls_einex-info_type = 'X'.
ls_eine-pur_group = is_eine-ekgrp.
ls_einex-pur_group = 'X'.
ls_eine-plnd_delry = is_eine-aplfz.
ls_einex-plnd_delry = 'X'.
ls_eine-currency = is_eine-waers.
ls_einex-currency = 'X'.
ls_eine-net_price = is_eine-netpr.
ls_eine-nrm_po_qty = is_eine-norbm.
ls_einex-nrm_po_qty = 'X'.
APPEND INITIAL LINE TO lt_eine ASSIGNING FIELD-SYMBOL(<fs_eine>).
<fs_eine>-eine_indx = '01'.
MOVE-CORRESPONDING ls_eine TO <fs_eine>.
APPEND INITIAL LINE TO lt_einex ASSIGNING FIELD-SYMBOL(<fs_einex>).
<fs_einex>-eine_indx = '01'.
MOVE-CORRESPONDING ls_einex TO <fs_einex>.
Code: Select all
READ TABLE gt_eina INTO DATA(ls_eina) WITH KEY matnr = iv_matnr lifnr = iv_vendor.
DATA: lt_ekomd TYPE TABLE OF ekomd.
CALL FUNCTION 'ME_GET_INFORECORD_CONDITIONS'
EXPORTING
i_ekorg = iv_porg
i_esokz = '0'
i_infnr = is_eina-infnr
i_lifnr = is_eina-lifnr
i_matkl = is_eina-matkl
i_matnr = is_eina-matnr
i_werks = gv_plant
* I_VABME =
TABLES
tekomd = lt_ekomd.
LOOP AT lt_ekomd INTO DATA(ls_price) WHERE datab <= sy-datum AND datbi >= sy-datum.
EXIT.
ENDLOOP.
IF sy-subrc NE 0.
* Add your code to handle exception here
ENDIF.
Khởi tạo validity
Code: Select all
ls_validity-serial_id = ls_ekomd-knumh.
ls_validity-plant = gv_plant.
ls_validity-valid_from = ls_ekomd-datab.
ls_validity-valid_to = ls_ekomd-datbi.
APPEND INITIAL LINE TO lt_validity ASSIGNING FIELD-SYMBOL(<fs_val>).
<fs_val>-eine_indx = '01'.
MOVE-CORRESPONDING ls_validity TO <fs_val>.
Code: Select all
ls_condition-serial_id = ls_ekomd-knumh.
ls_condition-cond_type = ls_ekomd-kschl.
ls_condition-cond_count = ls_ekomd-kopos.
DATA: lv_bapicurr TYPE bapicurr-bapicurr.
CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
EXPORTING
currency = ls_ekomd-koein
amount_internal = iv_prce
IMPORTING
amount_external = lv_bapicurr.
ls_condition-cond_value = lv_bapicurr.
ls_condition-numerator = 1.
ls_condition-denominator = 1.
ls_condition-cond_p_unt = ls_ekomd-kpein.
ls_condition-currency = ls_ekomd-koein.
ls_condition-cond_unit = ls_ekomd-kmein.
ls_condition-vendor_no = is_eina-lifnr.
ls_condition-change_id = 'U'.
APPEND INITIAL LINE TO lt_condition ASSIGNING FIELD-SYMBOL(<fs_cond>).
<fs_cond>-eine_indx = '01'.
MOVE-CORRESPONDING ls_condition TO <fs_cond>.
Code: Select all
CALL FUNCTION 'ME_INFORECORD_MAINTAIN_MULTI'
IMPORTING
et_eina = lt_eina
et_eine = lt_eine
TABLES
t_eina = lt_eina
t_einax = lt_einax
t_eine = lt_eine
t_einex = lt_einex
* TXT_LINES =
cond_validity = lt_validity
condition = lt_condition
return = lt_ret.
Code: Select all
DATA: ls_bapi TYPE bapiret2.
LOOP AT lt_ret INTO DATA(ls_mess) WHERE type = 'E'.
MOVE-CORRESPONDING ls_mess TO ls_bapi.
DATA: lv_mess(128) TYPE c.
MESSAGE ID ls_bapi-id TYPE ls_bapi-type NUMBER ls_bapi-number WITH ls_bapi-message_v1 ls_bapi-message_v2 ls_bapi-message_v3 ls_bapi-message_v4
INTO lv_mess.
cl_rso_application_log=>add_text(
EXPORTING
i_text = lv_mess " Text
i_msgty = rs_c_error " Messages, Message Type
i_detlevel = '1'
).
ENDLOOP.
IF ls_bapi IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
* Raise exception to stop process
ENDIF.
DATA: ls_ret TYPE bapiret2.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
IMPORTING
return = ls_ret.
IF ls_ret IS NOT INITIAL.
* Raise exception to stop process
ENDIF.