REPORT zmb31.
TABLES:mseg, mkpf, rm07m, t158b,t001l, msegk.
DATA: mat_doc LIKE bapi2017_gm_head_ret-mat_doc.
DATA:save_code LIKE sy-ucomm,
ok_code LIKE sy-ucomm,
loc_msg(50),
gmnga LIKE afru-gmnga,
mtsnr LIKE rm07m-mtsnr,
errflag..
DATA:BEGIN OF gt_afpo OCCURS 0.
INCLUDE STRUCTURE afpo.
DATA:END OF gt_afpo.
DATA:gmhead LIKE bapi2017_gm_head_01.
DATA: BEGIN OF gmcode.
INCLUDE STRUCTURE bapi2017_gm_code.
DATA: END OF gmcode.
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.
INITIALIZATION.
t001l-lgort = ' BG01'.
mseg-lgort = ' BG01'.
mseg-werks = '1103'.
mkpf-bldat = sy-datum.
mkpf-budat = sy-datum.
t158b-bwart = '101'.
* rm07m-sobkz = ''. "特殊库存
rm07m-werks = '1103'.
rm07m-lgort = ' BG01'.
mseg-erfme = 'PC'.
gmcode-gm_code = '02'.
***********gmcode-gm_code取值含义*******
********01 MB01
********02 MB31
********03 MB1A
********04 MB1B
********05 MB1C
********06 MB11
********07 MB04
gmhead-pstng_date = sy-datum."mkpf-bldat.
gmhead-doc_date = sy-datum."mkpf-budat.
gmhead-pr_uname = sy-uname.
itab-material = '000000003000000098'.
itab-plant = '1103'.
itab-stge_loc = 'BG01'.
itab-move_type = '101'.
itab-spec_stock = 'BG01'..
itab-entry_qnt = '6'.
itab-entry_uom = 'EA'.
itab-po_number = '4500000097'.
itab-po_item = '00010'.
itab-mvt_ind = 'B'.
itab-move_mat = '3000000098'.
itab-move_plant = '1103'.
itab-move_stloc = 'BG01'.
itab-nb_slips = 1.
APPEND itab.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = gmhead
goodsmvt_code = gmcode
IMPORTING
goodsmvt_headret = mthead
materialdocument = mat_doc
TABLES
goodsmvt_item = itab
return = errmsg.
LOOP AT errmsg.
CLEAR loc_msg.
CONCATENATE '过帐成功!' errmsg-message INTO loc_msg.
IF errmsg-type EQ 'E'.
MESSAGE errmsg-message TYPE 'E'.
errflag = 'X'.
ELSE.
MESSAGE loc_msg TYPE 'S'.
ENDIF.
ENDLOOP.
IF errflag IS INITIAL.
COMMIT WORK AND WAIT.
CLEAR loc_msg.
IF sy-subrc NE 0.
MESSAGE '过帐操作出现错误,请稍后重试!' TYPE 'E'.
EXIT.
ELSE.
CONCATENATE '物料凭证:' mat_doc '已成功创建!' INTO loc_msg.
MESSAGE i000(znyj13) WITH loc_msg.
gmhead-ref_doc_no = ''.
gmhead-header_txt = ''.
mseg-matnr = ''.
msegk-mat_kdauf = ''.
msegk-mat_kdpos = ''.
mseg-erfmg = ''.
rm07m-mtsnr = ''.
gmnga = ''.
mkpf-bktxt = ''.
ENDIF.
ELSE.
MESSAGE '这不是正确的发料单或者已经创建凭证!' TYPE 'E'.
ENDIF.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
------------------------------261 /262 生产订单收退料------------------------
REPORT test.
*物料凭证的抬头数据
DATA:
li_gm_head TYPE STANDARD TABLE OF bapi2017_gm_head_01,
l_gm_head LIKE bapi2017_gm_head_01,
*为BAPI货物移动分配事务代码
li_gm_code TYPE STANDARD TABLE OF bapi2017_gm_code,
l_gm_code LIKE bapi2017_gm_code,
*返回单据号码
l_mat_doc LIKE bapi2017_gm_head_ret-mat_doc,
*凭证项目
li_gm_item TYPE STANDARD TABLE OF bapi2017_gm_item_create,
l_gm_item LIKE bapi2017_gm_item_create,
*-------------------------
li_return TYPE STANDARD TABLE OF bapiret2,
l_return TYPE bapiret2,
*Error flag
l_errflag(1) TYPE c.
*&---------------------------------------------------------------------*
*& Form CALLBAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*填写表头数据
l_gm_head-pstng_date = sy-datum.
l_gm_head-doc_date = sy-datum.
APPEND l_gm_head TO li_gm_head.
*分配事务代码
l_gm_code-gm_code = '03'.
APPEND l_gm_code TO li_gm_code.
*发货明细
l_gm_item-material = '000000003102000245'. "10 410A
l_gm_item-plant = '1201'.
l_gm_item-stge_loc = '1006'.
l_gm_item-move_type = '261'. "移动类型(库存管理)
* l_gm_item-mvt_ind = 'F'.
l_gm_item-entry_qnt = '1'.
l_gm_item-entry_uom = 'EA'.
l_gm_item-orderid = '000010003579'. "MO号
l_gm_item-reserv_no = '0000107118'. "预留/相关需求的编号 RESB~RSNUM
l_gm_item-res_item = '0019'. " 预留 / 相关需求的项目编号 RESB~RSPOS
* l_gm_item-XSTOB = 'X'. “冲销标志,不修改移动类型261,做完以后也是262;使用262,但此处不设置还是261.
gm_code = 03 和 05 此处都是对的,不知道是不是bugger
APPEND l_gm_item TO li_gm_item.
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = l_gm_head
goodsmvt_code = '03'
* RETURN
IMPORTING
* GOODSMVT_HEADRET = L_MAT_DOC
materialdocument = l_mat_doc
TABLES
goodsmvt_item = li_gm_item
return = li_return
.
CLEAR l_errflag.
MESSAGE i005(ymess) WITH l_mat_doc.
IF l_errflag IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ELSE.
MESSAGE i005(ymess) WITH 'ERROR'.
ENDIF.