DATA: MAT_DOC LIKE BAPI2017_GM_HEAD_RET-MAT_DOC. "物料凭证编号
DATA: GMHEAD LIKE BAPI2017_GM_HEAD_01.
DATA: BEGIN OF GMCODE.
INCLUDE STRUCTURE BAPI2017_GM_CODE.
DATA: END OF GMCODE.
GMCODE-GM_CODE = '04'.
***********gmcode-gm_code取值含义*******
********01 MB01
********02 MB31
********03 MB1A
********04 MB1B
********05 MB1C
********06 MB11
********07 MB04
DATA: BEGIN OF MTHEAD.
INCLUDE STRUCTURE BAPI2017_GM_HEAD_RET.
DATA: END OF MTHEAD.
DATA: BEGIN OF ITAB OCCURS 100.
INCLUDE STRUCTURE BAPI2017_GM_ITEM_CREATE.
DATA: END OF ITAB.
DATA: BEGIN OF ERRMSG OCCURS 10.
INCLUDE STRUCTURE BAPIRET2.
DATA: END OF ERRMSG.
"CLEAR: MAT_DOC, GMHEAD, GMCODE, MTHEAD, ITAB[], ERRMSG[].
GMHEAD-PSTNG_DATE = SY-DATUM. "凭证中的过帐日期
GMHEAD-DOC_DATE = SY-DATUM. "凭证中的凭证日期
GMHEAD-PR_UNAME = SY-UNAME.
LOOP AT SHOW_TAB WHERE SEL_COL = 'X'.
* CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
* EXPORTING
* INPUT = SHOW_TAB-MATNR
* IMPORTING
* OUTPUT = SHOW_TAB-MATNR.
* IF SY-SUBRC = 0..
*
* ENDIF.
ITAB-MATERIAL = SHOW_TAB-MATNR. "物料号
ITAB-PLANT = SHOW_TAB-WERKS. "工厂
ITAB-STGE_LOC = SHOW_TAB-ZLGORTO. "库存地点
"ITAB-COSTCENTER = '1000'. "成本中心
ITAB-BATCH = SHOW_TAB-CHARG. "批次
ITAB-ENTRY_QNT = SHOW_TAB-MENGE. "实领数量
ITAB-ENTRY_UOM = SHOW_TAB-MEINS. "单位
"ITAB-MOVE_TYPE = SHOW_TAB-ZLLTYPE. "移动类型
ITAB-MOVE_TYPE = '311'. "移动类型
* ITAB-mvt_ind = 'O'.
ITAB-MOVE_MAT = SHOW_TAB-MATNR. "收货/发货物料号
ITAB-MOVE_PLANT = SHOW_TAB-WERKS. "收货/发货工厂
ITAB-MOVE_STLOC = SHOW_TAB-ZLGORTI. "收货/发货物料仓库
* ITAB-no_more_gr = 'X'. "交货完成标识
APPEND ITAB.
ENDLOOP.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
GOODSMVT_HEADER = GMHEAD
GOODSMVT_CODE = GMCODE
* TESTRUN = ' '
* GOODSMVT_REF_EWM =
IMPORTING
GOODSMVT_HEADRET = MTHEAD
MATERIALDOCUMENT = MAT_DOC "所产生的物料凭证号 但是必须经过BAPI_TRANSACTION_COMMIT才能查询到该凭证号
* MATDOCUMENTYEAR =
TABLES
GOODSMVT_ITEM = ITAB "关键表 必须填写正确
* GOODSMVT_SERIALNUMBER =
RETURN = ERRMSG "报错信息表
* GOODSMVT_SERV_PART_DATA =
* EXTENSIONIN =
.
* IF ERRMSG[] IS INITIAL.
* CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
* WAIT = 'X'
** IMPORTING
** RETURN =
* .
* DATA: L_MESS(100).
* CONCATENATE '过账成功,凭证号:' MAT_DOC INTO L_MESS.
* MESSAGE L_MESS TYPE 'S' .
* ELSE.
* CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
** IMPORTING
** RETURN =
* .
* ENDIF.
READ TABLE ERRMSG WITH KEY TYPE = 'E'.
IF SY-SUBRC = 0.
MESSAGE ERRMSG-MESSAGE TYPE 'S' DISPLAY LIKE 'E'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
"SHOW_TAB-POST = ''.
ELSE.
"必须成功执行 BAPI_GOODSMVT_CREATE 不然会在MIGO查不到 生成的凭证号 MAT_DOC
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
DATA: L_MESS(100).
CONCATENATE '过账成功,凭证号:' MAT_DOC INTO L_MESS.
MESSAGE L_MESS TYPE 'S' .
"SHOW_TAB-POST = 'X'.
ENDIF.