SAP 中采用BAPI BAPI_GOODSMVT_CREATE 进行MB01 冲销功能
GMCODE Table T158G - 01 - MB01 - Goods Receipts for Purchase Order
* 02 - MB31 - Goods Receipts for Prod Order
* 03 - MB1A - Goods Issue
* 04 - MB1B - Transfer Posting
* 05 - MB1C - Enter Other Goods Receipt
* 06 - MB11
*
* Domain: KZBEW - Movement Indicator
* Goods movement w/o reference
* B - Goods movement for purchase order
* F - Goods movement for production order
* L - Goods movement for delivery note
* K - Goods movement for kanban requirement (WM - internal only)
* O - Subsequent adjustment of "material-provided" consumption
* W - Subsequent adjustment of proportion/product unit material
*
form frm_chongxiao_process.
DATA: ls_header_ret LIKE bapi2017_gm_head_ret,
ls_header LIKE bapi2017_gm_head_01,
ls_item LIKE bapi2017_gm_item_create,
lt_item LIKE TABLE OF bapi2017_gm_item_create,
lt_return LIKE TABLE OF bapiret2, " BAPI RETURN
ls_return like bapiret2,
ls_retcom LIKE bapiret2 " COMMIT RETURN
.
data:begin of itaby occurs 0,
ebeln like ekpo-ebeln,
ebelp like ekpo-ebelp,
matnr like ekpo-matnr,
end of itaby.
ls_header-pstng_date = p_budat. " 过账日期
ls_header-doc_date = p_bldat. " 凭证日期
ls_header-pr_uname = sy-uname.
select ebeln ebelp matnr into corresponding fields of table itaby from ekpo where ebeln = p_ebeln.
loop at purch_itab.
ls_item-material = purch_itab-matnr.
ls_item-plant = purch_itab-werks.
ls_item-stge_loc = purch_itab-lgort.
ls_item-batch = purch_itab-charg.
ls_item-entry_qnt = purch_itab-erfmg.
ls_item-po_number = p_ebeln.
ls_item-move_type = '102'.
"ls_item-stck_type = 'X'.
"ls_item-no_more_gr = 'X'.
ls_item-mvt_ind = 'B'.
read table itaby with key matnr = purch_itab-matnr.
if sy-subrc = 0.
ls_item-po_item = itaby-ebelp.
endif.
append ls_item to lt_item.
endloop.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = ls_header
goodsmvt_code = '01' " MB01
"testrun = p_test
IMPORTING
goodsmvt_headret = ls_header_ret
TABLES
goodsmvt_item = lt_item[]
return = lt_return[].
if lt_return[] is initial.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_retcom.
if strlen( ls_header_ret ) > 10.
data:msg(100) type c.
concatenate '冲销处理成功完成,物料凭证号' ls_header_ret+0(10) into msg.
refresh it_btns.
it_btns-fcode = 'BTN_PROC'.
append it_btns.
message msg type 'S'.
endif.
else.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
return = ls_retcom.
data:msge(100) type c.
loop at lt_return into ls_return.
concatenate msge ls_return-message into msge.
endloop.
message msge type 'S'.
endif.
endform.