* 定义过账BAPI的结构
DATA: lt_item TYPE TABLE OF bapi2017_gm_item_create,
lt_return TYPE TABLE OF bapiret2.
DATA: ls_item TYPE bapi2017_gm_item_create,
ls_return TYPE bapiret2,
ls_head TYPE bapi2017_gm_head_01,
ls_code TYPE bapi2017_gm_code.
DATA: ls_extensionin TYPE bapiparex,
lt_extensionin TYPE TABLE OF bapiparex.
DATA: lv_mblnr TYPE mkpf-mblnr,
lv_mjahr TYPE mkpf-mjahr,
lv_flag TYPE c,
lv_message TYPE string,
lv_testrun TYPE bapi2017_gm_gen-testrun,
lv_date TYPE sy-datum,
lv_meins TYPE lips-meins.
LOOP AT gt_doc ASSIGNING <gf_doc> WHERE sel = 'X'.
IF <gf_doc>-zstatus = 'S'.
CONTINUE.
ENDIF.
**clear:
CLEAR: lv_message,lv_mblnr,lv_mjahr,
ls_head,ls_code,
ls_item,lt_item,
lv_mblnr,lv_mjahr,
ls_extensionin,lt_extensionin,
ls_return,lt_return.
ls_code-gm_code = '01'.
**set head
ls_head-pstng_date = sy-datum. "过账日期
ls_head-doc_date = sy-datum. " ls_head-pr_uname = sy-uname. " ls_head-gr_gi_slip_no = <gf_doc>-vbeln.
ls_head-ref_doc_no = <gf_doc>-vbeln.
**set item
ls_item-move_type = '101'. "移动类型
ls_item-mvt_ind = 'B'. "移动标识:有关交货通知的货物移动
ls_item-material = <gf_doc>-zmatnr. "物料号码
ls_item-entry_qnt = <gf_doc>-zlfimg. "数量
ls_item-quantity = <gf_doc>-zlfimg.
* ls_item-batch = <gf_doc>-zcharg. "SAP批次* ls_item-move_stloc = <gf_doc>-zkcd. "库存地点
ls_item-deliv_numb = <gf_doc>-vbeln. "DN
ls_item-deliv_item = <gf_doc>-posnr. "DN item**输入采购订单*PO_NUMBER*PO_ITEM
CLEAR: lv_meins.
SELECT SINGLE vgbel vgpos charg meins
INTO (ls_item-po_number,ls_item-po_item,ls_item-batch,lv_meins)
FROM lips
WHERE vbeln = <gf_doc>-vbeln
AND posnr = <gf_doc>-posnr.
**工厂
SELECT SINGLE werks
INTO ls_item-plant
FROM ekpo
WHERE ebeln EQ ls_item-po_number
AND ebelp EQ ls_item-po_item.
ls_item-stge_loc = <gf_doc>-zkcd."收货库存地点
APPEND ls_item TO lt_item.
CLEAR: ls_item.
**LSMNG LSMEH
ls_extensionin-structure = 'BAPI_TE_XMSEG'.
ls_extensionin-valuepart1+14(4) = '0001'.
ls_extensionin-valuepart1+18(16) = <gf_doc>-zlfimg.
ls_extensionin-valuepart1+34(3) = lv_meins.
APPEND ls_extensionin TO lt_extensionin.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = ls_head
goodsmvt_code = ls_code
testrun = lv_testrun
IMPORTING
materialdocument = lv_mblnr
matdocumentyear = lv_mjahr
TABLES
goodsmvt_item = lt_item
return = lt_return
extensionin = lt_extensionin.
LOOP AT lt_return INTO ls_return WHERE type = 'E' OR type = 'A'.
lv_flag = 'E'.
lv_message = lv_message && ls_return-message.
EXIT.
ENDLOOP.
<gf_doc>-zendat = sy-datum.
<gf_doc>-zendtm = sy-uzeit.
<gf_doc>-usnam = sy-uname.
IF lv_flag = 'E'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
<gf_doc>-zstatus = 'E'.
<gf_doc>-zmessage = lv_message.
<gf_doc>-icon = '@5C@'.
ELSE.
<gf_doc>-zstatus = 'S'.
<gf_doc>-zmessage = '过账成功'.
<gf_doc>-icon = '@5B@'.
<gf_doc>-mblnr = lv_mblnr.
<gf_doc>-mjahr = lv_mjahr.
ENDIF.
PERFORM frm_update_db.
ENDLOOP.