*&---------------------------------------------------------------------*
*& Report YTST_RAINY015_03
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ytst_rainy015_03.
*----------------------------------------------------------------------
* TYPES
*----------------------------------------------------------------------
TYPES: BEGIN OF ty_output,
lifnr TYPE ekko-lifnr, "供應商
ebeln TYPE ekpo-ebeln, "採購文件號碼
ebelp TYPE ekpo-ebelp, "採購文件的項目號碼
txz01 TYPE ekpo-txz01, "短文
matnr TYPE ekpo-matnr, "物料號碼
werks TYPE ekpo-werks, "地點
lgort TYPE ekpo-lgort, "庫存地點
menge TYPE ekpo-menge, "採購單數量
meins TYPE ekpo-meins, "單位
wenge TYPE ekpo-menge, "未清數量
benge TYPE ekpo-menge, "本次收料數量
txt(50) TYPE c, "備註
sel TYPE c, "選擇標誌
END OF ty_output.
TYPES: ty_tab_output TYPE ty_output OCCURS 0.
TYPES: BEGIN OF ty_error,
pstng_date TYPE budat,
doc_date TYPE bldat,
gm_code TYPE gm_code,
plant TYPE werks_d,
stge_loc TYPE lgort_d,
move_type TYPE bwart,
entry_qnt TYPE erfmg,
entry_uom TYPE erfme,
po_number TYPE bstnr,
po_item TYPE ebelp,
mvt_ind TYPE kzbew,
message(100) TYPE c,
txt(50) TYPE c,
row TYPE bapi_line,
END OF ty_error.
TYPES: ty_tab_error TYPE ty_error OCCURS 0.
DATA:gt_error TYPE ty_tab_error,
wa_error TYPE ty_error.
DATA:gt_output TYPE ty_tab_output,
wa_output TYPE ty_output.
DATA:l_tabix TYPE sy-tabix,
l_row TYPE bapi_line,
wa_output_tmp TYPE ty_output.
DATA:l_flag_create_success TYPE c.
DATA:lw_bapi_goodsmvt_header TYPE bapi2017_gm_head_01,
lw_bapi_goodsmvt_code TYPE bapi2017_gm_code,
lw_bapi_goodsmvt_headret TYPE bapi2017_gm_head_ret,
l_bapi_materialdocument TYPE bapi2017_gm_head_ret-mat_doc,
l_bapi_matdocumentyear TYPE bapi2017_gm_head_ret-doc_year,
lt_bapi_goodsmvt_item TYPE STANDARD TABLE OF bapi2017_gm_item_create,
wa_bapi_goodsmvt_item TYPE bapi2017_gm_item_create,
lt_bapi_return TYPE STANDARD TABLE OF bapiret2,
wa_bapi_return TYPE bapiret2.
*&G0.整理取得数据
SORT gt_output BY ebeln ebelp.
LOOP AT gt_output INTO wa_output.
wa_output_tmp = wa_output.
AT NEW ebeln.
CLEAR:lw_bapi_goodsmvt_header, lw_bapi_goodsmvt_code,
lw_bapi_goodsmvt_headret, l_bapi_materialdocument,
l_bapi_matdocumentyear, lt_bapi_goodsmvt_item,
lt_bapi_return.
*&G1.bapi_goodsmvt_hear 赋值(at new)
lw_bapi_goodsmvt_header-pstng_date = sy-datum.
lw_bapi_goodsmvt_header-doc_date = sy-datum.
lw_bapi_goodsmvt_code-gm_code = '01'.
ENDAT.
CLEAR wa_bapi_goodsmvt_item.
*&G2.bapi_goodsmvt_item 赋值()
wa_bapi_goodsmvt_item-plant = wa_output_tmp-werks.
wa_bapi_goodsmvt_item-stge_loc = wa_output_tmp-lgort.
wa_bapi_goodsmvt_item-move_type = '101'.
wa_bapi_goodsmvt_item-entry_qnt = wa_output_tmp-benge.
wa_bapi_goodsmvt_item-entry_uom = wa_output_tmp-meins.
wa_bapi_goodsmvt_item-po_number = wa_output_tmp-ebeln.
wa_bapi_goodsmvt_item-po_item = wa_output_tmp-ebelp.
wa_bapi_goodsmvt_item-mvt_ind = 'B'.
APPEND wa_bapi_goodsmvt_item TO lt_bapi_goodsmvt_item.
AT END OF ebeln.
*&G3flag打上
l_flag_create_success = 'Y'.
*&G4 CALL BAPI
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = lw_bapi_goodsmvt_header
goodsmvt_code = lw_bapi_goodsmvt_code
* TESTRUN = ' '
* GOODSMVT_REF_EWM =
IMPORTING
goodsmvt_headret = lw_bapi_goodsmvt_headret
materialdocument = l_bapi_materialdocument
matdocumentyear = l_bapi_matdocumentyear
TABLES
goodsmvt_item = lt_bapi_goodsmvt_item
return = lt_bapi_return.
* GOODSMVT_SERIALNUMBER =
* GOODSMVT_SERV_PART_DATA =
* EXTENSIONIN =
* AFS_GOODSMVT_SKU =
*&G5.如果失败,改flag 为‘N’.
LOOP AT lt_bapi_return INTO wa_bapi_return WHERE type = 'A'
OR type = 'E'.
l_flag_create_success = 'N'.
EXIT.
ENDLOOP.
*&G6 BAPI后处理
IF l_flag_create_success = 'Y'.
*&G6.1成功commit work,并形成日志
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
LOOP AT lt_bapi_goodsmvt_item INTO wa_bapi_goodsmvt_item.
wa_error-pstng_date = lw_bapi_goodsmvt_header-pstng_date.
wa_error-doc_date = lw_bapi_goodsmvt_header-doc_date.
wa_error-gm_code = lw_bapi_goodsmvt_code-gm_code.
MOVE-CORRESPONDING wa_bapi_goodsmvt_item TO wa_error.
CONCATENATE '生成物料凭证编号为:' l_bapi_materialdocument INTO wa_error-message.
MOVE '更新成功!' TO wa_error-txt.
APPEND wa_error TO gt_error.
CLEAR wa_error.
ENDLOOP.
ELSE.
*&6.2 失败rollback,并形成日志GT_ERROR
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
CLEAR l_row.
LOOP AT lt_bapi_goodsmvt_item INTO wa_bapi_goodsmvt_item.
l_row = l_row + 1.
wa_error-pstng_date = lw_bapi_goodsmvt_header-pstng_date.
wa_error-doc_date = lw_bapi_goodsmvt_header-doc_date.
wa_error-gm_code = lw_bapi_goodsmvt_code-gm_code.
MOVE-CORRESPONDING wa_bapi_goodsmvt_item TO wa_error.
MOVE '更新失败!' TO wa_error-txt.
wa_error-row = l_row.
READ TABLE lt_bapi_return INTO wa_bapi_return WITH KEY row = wa_error-row.
IF sy-subrc = 0.
wa_error-message = wa_bapi_return-message.
ENDIF.
APPEND wa_error TO gt_error.
CLEAR wa_error.
ENDLOOP.
ENDIF.
IF l_flag_create_success = 'Y'.
* IMPORTING
* RETURN =
ELSE.
* IMPORTING
* RETURN =
ENDIF.
ENDAT.
ENDLOOP.