BAPI_GOODSMVT_CREATE进行261投料的时候需注意的事项:
1、在项目中,利用BAPI(BAPI_GOODSMVT_CREATE)投料消耗(261)销售订单E库存,ITEM表中的销售订单一定要用 VAL_SALES_ORD(销售订单)、VAL_S_ORD_ITEM(行项目),而不能用SALES_ORD(销售订单)、S_ORD_ITEM(行项目),否则会提示,找不到对应的销售订单(提示:请输入一个特殊库存E的销售订单)。
2、ITEM参数里要输入预留号(RESERV_NO)和行项目参数(RES_ITEM),否则不能冲销提货数。
以下是项目中用到的代码,希望对用到的兄弟有帮助。
FUNCTION ZWMS_CO_FEEDING_RETURN.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(P_MOVE_TYPE) TYPE MSEG-BWART OPTIONAL
*" TABLES
*" INPUT STRUCTURE ZWMS_CO_FEEDING_RETURN_INPUT OPTIONAL
*" INPUT_SN STRUCTURE ZWMS_CO_FEEDING_RETURN_SN OPTIONAL
*" INPUT_QN STRUCTURE ZWMS_INPUT_QN OPTIONAL
*" OUTPUT STRUCTURE ZWMS_CO_FEEDING_RETURN_OUTPUT OPTIONAL
*"----------------------------------------------------------------------
*物料凭证的抬头数据
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,
W_GOODSMVT_SERIALNUMBER
TYPE BAPI2017_GM_SERIALNUMBER,
GOODSMVT_SERIALNUMBER
TYPE
STANDARD
TABLE
OF BAPI2017_GM_SERIALNUMBER,
*-------------------------
LI_RETURN
TYPE
STANDARD
TABLE
OF BAPIRET2
WITH
HEADER
LINE,
L_RETURN
TYPE BAPIRET2,
* ERROR FLAG
L_ERRFLAG(
1)
TYPE
C.
TYPES:
BEGIN
OF TY_INPUT_SN,
MBLPO
LIKE ZWMS_CO_FEEDING_RETURN_SN-MBLPO,
KDAUF
LIKE EQBS-KDAUF,
KDPOS
LIKE EQBS-KDPOS,
SOBKZ
LIKE EQBS-SOBKZ,
SERNR
LIKE ZWMS_CO_FEEDING_RETURN_SN-SERNR,
MATNR
LIKE ZWMS_CO_FEEDING_RETURN_INPUT-MATNR,
ENTRY_QNT
LIKE ZWMS_CO_FEEDING_RETURN_INPUT-ENTRY_QNT,
END
OF TY_INPUT_SN.
DATA:IT_INPUT_SN
TYPE
TABLE
OF TY_INPUT_SN
WITH
HEADER
LINE,
TIT_INPUT_SN
TYPE
TABLE
OF TY_INPUT_SN
WITH
HEADER
LINE.
TYPES:
BEGIN
OF TY_EQUI,
EQUNR
LIKE EQUI-EQUNR,
MATNR
LIKE EQUI-MATNR,
SERNR
LIKE EQUI-SERNR,
KDAUF
LIKE EQBS-KDAUF,
KDPOS
LIKE EQBS-KDPOS,
SOBKZ
LIKE EQBS-SOBKZ,
END
OF TY_EQUI.
DATA:ITEQUI
TYPE
TABLE
OF TY_EQUI
WITH
HEADER
LINE.
TYPES:
BEGIN
OF TY_EQBS,
EQUNR
LIKE EQBS-EQUNR,
KDAUF
LIKE EQBS-KDAUF,
KDPOS
LIKE EQBS-KDPOS,
SOBKZ
LIKE EQBS-SOBKZ,
END
OF TY_EQBS.
DATA:ITEQBS
TYPE
TABLE
OF TY_EQBS
WITH
HEADER
LINE.
DATA:WA
TYPE ZWMS_CO_FEEDING_RETURN_INPUT.
DATA:T_OUTPUT
LIKE ZWMS_CO_FEEDING_RETURN_OUTPUT
OCCURS
0
WITH
HEADER
LINE.
DATA:IT_ZBHGP
TYPE
TABLE
OF ZWMS_INPUT_QN
WITH
HEADER
LINE.
*DATA:ITEQUI TYPE TABLE OF EQUI WITH HEADER LINE.
DATA:IT_INPUT
TYPE
TABLE
OF ZWMS_CO_FEEDING_RETURN_INPUT
WITH
HEADER
LINE.
DATA:LV_ZEILE
TYPE MSEG-ZEILE.
*获取销售订单、行项目、特殊库存标识
SORT
INPUT
BY MBLPO.
REFRESH IT_INPUT_SN.
CLEAR IT_INPUT_SN.
LOOP
AT INPUT_SN.
MOVE-CORRESPONDING INPUT_SN
TO IT_INPUT_SN.
CLEAR
INPUT.
READ
TABLE
INPUT
WITH
KEY MBLPO = IT_INPUT_SN-MBLPO
BINARY
SEARCH.
IF SY-SUBRC
EQ
0.
IT_INPUT_SN-MATNR =
INPUT-MATNR.
ENDIF.
APPEND IT_INPUT_SN.
CLEAR IT_INPUT_SN.
ENDLOOP.
REFRESH ITEQUI.
CLEAR ITEQUI.
IF IT_INPUT_SN[]
IS
NOT
INITIAL.
SELECT EQUNR MATNR SERNR
INTO
TABLE ITEQUI
FROM EQUI
FOR
ALL ENTRIES
IN IT_INPUT_SN
WHERE MATNR = IT_INPUT_SN-MATNR
AND SERNR = IT_INPUT_SN-SERNR.
IF ITEQUI[]
IS
NOT
INITIAL.
SELECT EQUNR
KDAUF
KDPOS
SOBKZ
INTO
TABLE ITEQBS
FROM EQBS
FOR
ALL ENTRIES
IN ITEQUI
WHERE EQUNR = ITEQUI-EQUNR.
SORT ITEQBS
BY EQUNR.
LOOP
AT ITEQUI.
CLEAR ITEQBS.
READ
TABLE ITEQBS
WITH
KEY EQUNR = ITEQUI-EQUNR
BINARY
SEARCH.
IF SY-SUBRC
EQ
0.
ITEQUI-KDAUF = ITEQBS-KDAUF.
ITEQUI-KDPOS = ITEQBS-KDPOS.
ITEQUI-SOBKZ = ITEQBS-SOBKZ.
ENDIF.
MODIFY ITEQUI.
CLEAR ITEQUI.
ENDLOOP.
SORT ITEQUI
BY MATNR SERNR.
LOOP
AT IT_INPUT_SN.
CLEAR ITEQUI.
READ
TABLE ITEQUI
WITH
KEY MATNR = IT_INPUT_SN-MATNR SERNR = IT_INPUT_SN-SERNR
BINARY
SEARCH.
IF SY-SUBRC
EQ
0.
IT_INPUT_SN-KDAUF = ITEQUI-KDAUF.
IT_INPUT_SN-KDPOS = ITEQUI-KDPOS.
IT_INPUT_SN-SOBKZ = ITEQUI-SOBKZ.
ENDIF.
MODIFY IT_INPUT_SN.
CLEAR IT_INPUT_SN.
ENDLOOP.
ENDIF.
ENDIF.
SORT IT_INPUT_SN
BY MBLPO KDAUF KDPOS.
REFRESH TIT_INPUT_SN.
CLEAR TIT_INPUT_SN.
LOOP
AT IT_INPUT_SN.
TIT_INPUT_SN-MBLPO = IT_INPUT_SN-MBLPO.
TIT_INPUT_SN-KDAUF = IT_INPUT_SN-KDAUF.
TIT_INPUT_SN-KDPOS = IT_INPUT_SN-KDPOS.
TIT_INPUT_SN-SOBKZ = IT_INPUT_SN-SOBKZ.
TIT_INPUT_SN-ENTRY_QNT =
1.
COLLECT TIT_INPUT_SN.
CLEAR TIT_INPUT_SN.
ENDLOOP.
REFRESH IT_INPUT.
CLEAR IT_INPUT.
IT_INPUT[] =
INPUT[].
SORT TIT_INPUT_SN
BY MBLPO.
LOOP
AT
INPUT.
CLEAR TIT_INPUT_SN.
READ
TABLE TIT_INPUT_SN
WITH
KEY MBLPO =
INPUT-MBLPO
BINARY
SEARCH.
IF SY-SUBRC
EQ
0.
DELETE
INPUT.
ENDIF.
ENDLOOP.
CLEAR
INPUT.
SORT IT_INPUT
BY MBLPO.
LOOP
AT TIT_INPUT_SN.
CLEAR IT_INPUT.
READ
TABLE IT_INPUT
WITH
KEY MBLPO = TIT_INPUT_SN-MBLPO
BINARY
SEARCH.
IF SY-SUBRC
EQ
0.
MOVE-CORRESPONDING IT_INPUT
TO
INPUT.
INPUT-SALES_ORD = TIT_INPUT_SN-KDAUF.
INPUT-S_ORD_ITEM = TIT_INPUT_SN-KDPOS.
INPUT-SPEC_STOCK = TIT_INPUT_SN-SOBKZ.
INPUT-ENTRY_QNT = TIT_INPUT_SN-ENTRY_QNT.
APPEND
INPUT.
CLEAR
INPUT.
ENDIF.
ENDLOOP.
*填写表头数据
SORT
INPUT
BY YWLX AUFNR.
LOOP
AT
INPUT.
CLEAR WA.
MOVE-CORRESPONDING
INPUT
TO WA.
AT
NEW AUFNR.
CLEAR L_MAT_DOC.
CLEAR L_GM_HEAD.
CLEAR LI_GM_CODE.
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'.
L_GM_HEAD-HEADER_TXT =
INPUT-YWLX.
APPEND L_GM_CODE
TO LI_GM_CODE.
CLEAR L_GM_ITEM.
CLEAR LI_GM_ITEM.
REFRESH LI_GM_ITEM.
CLEAR T_OUTPUT.
REFRESH T_OUTPUT.
CLEAR IT_ZBHGP.
REFRESH IT_ZBHGP.
CLEAR GOODSMVT_SERIALNUMBER.
REFRESH GOODSMVT_SERIALNUMBER.
CLEAR LV_ZEILE.
ENDAT.
MOVE-CORRESPONDING WA
TO T_OUTPUT.
APPEND T_OUTPUT.
CLEAR T_OUTPUT.
* 发货明细
L_GM_ITEM-MATERIAL = WA-MATNR.
"10 410A
L_GM_ITEM-PLANT = WA-WERKS.
L_GM_ITEM-STGE_LOC = WA-LGORT.
L_GM_ITEM-MOVE_TYPE = P_MOVE_TYPE.
"移动类型(库存管理)
* L_GM_ITEM-MVT_IND = 'F'.
L_GM_ITEM-ENTRY_QNT = WA-ENTRY_QNT.
L_GM_ITEM-ENTRY_UOM = WA-ENTRY_UOM.
L_GM_ITEM-ORDERID = WA-AUFNR.
"MO号
" L_GM_ITEM-BATCH = WA-BATCH.
IF WA-SALES_ORD <>
''.
L_GM_ITEM-VAL_SALES_ORD = WA-SALES_ORD.
L_GM_ITEM-VAL_S_ORD_ITEM = WA-S_ORD_ITEM.
L_GM_ITEM-SPEC_STOCK = WA-SPEC_STOCK.
ENDIF.
L_GM_ITEM-RESERV_NO = WA-RSNUM.
"预留/相关需求的编号 RESB~RSNUM,加上后可以冲销提货数
L_GM_ITEM-RES_ITEM = WA-RSPOS.
" 预留 / 相关需求的项目编号 RESB~RSPOS,加上后可以冲销提货数
APPEND L_GM_ITEM
TO LI_GM_ITEM.
* 更新D类物资库存(不合格品库更新)
LOOP
AT INPUT_QN
WHERE QNDEX = WA-QNDEX.
MOVE-CORRESPONDING INPUT_QN
TO IT_ZBHGP.
APPEND IT_ZBHGP.
CLEAR IT_ZBHGP.
ENDLOOP.
* LV_ZEILE = LV_ZEILE + 1.
LOOP
AT INPUT_SN.
CLEAR W_GOODSMVT_SERIALNUMBER.
W_GOODSMVT_SERIALNUMBER-MATDOC_ITM = INPUT_SN-MBLPO.
W_GOODSMVT_SERIALNUMBER-SERIALNO = INPUT_SN-SERNR.
APPEND W_GOODSMVT_SERIALNUMBER
TO GOODSMVT_SERIALNUMBER.
ENDLOOP.
AT
END
OF AUFNR.
CALL
FUNCTION
'BAPI_GOODSMVT_CREATE'
EXPORTING
GOODSMVT_HEADER = L_GM_HEAD
GOODSMVT_CODE =
'03'
* RETURN
IMPORTING
MATERIALDOCUMENT = L_MAT_DOC
TABLES
GOODSMVT_SERIALNUMBER = GOODSMVT_SERIALNUMBER
GOODSMVT_ITEM = LI_GM_ITEM
RETURN = LI_RETURN.
IF L_MAT_DOC
IS
NOT
INITIAL.
CALL
FUNCTION
'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT =
'X'.
"更新D类物资库存(不合格品库更新)
IF IT_ZBHGP[]
IS
NOT
INITIAL.
"更新D类物资库存(不合格品库更新)
CALL
FUNCTION
'ZWMS_UNQUALIF_DTYPE_UPDATE'
EXPORTING
QNDEX =
'1000000000'
TABLES
INPUT = IT_ZBHGP.
ENDIF.
LOOP
AT T_OUTPUT.
MOVE-CORRESPONDING T_OUTPUT
TO
OUTPUT.
OUTPUT-FLAG =
'S'.
OUTPUT-NOTES =
'创建物料凭证成功'.
OUTPUT-MBLNR = L_MAT_DOC.
APPEND
OUTPUT.
CLEAR
OUTPUT.
ENDLOOP.
ELSE.
CALL
FUNCTION
'BAPI_TRANSACTION_ROLLBACK'.
LOOP
AT T_OUTPUT.
MOVE-CORRESPONDING T_OUTPUT
TO
OUTPUT.
OUTPUT-FLAG =
'E'.
CLEAR LI_RETURN.
READ
TABLE LI_RETURN
WITH
KEY
TYPE =
'E'.
IF SY-SUBRC
EQ
0.
CONCATENATE
'凭证创建失败:' LI_RETURN-
MESSAGE
'!'
INTO
OUTPUT-NOTES.
ENDIF.
APPEND
OUTPUT.
CLEAR
OUTPUT.
ENDLOOP.
ENDIF.
ENDAT.
ENDLOOP.
*--------------------------日志信息start--------------------------
*日志信息定义
DATA:LOG1
TYPE
TABLE
OF ZWMS_LOG03_01
WITH
HEADER
LINE,
LOG2
TYPE
TABLE
OF ZWMS_LOG03_02
WITH
HEADER
LINE,
LOG3
TYPE
TABLE
OF ZWMS_LOG03_03
WITH
HEADER
LINE.
DATA:LV_FKEY(
100),
LV_FINDX
TYPE
I,
LV_UUID
TYPE SYSUUID-
X,
TM_UUID(
100).
CLEAR LV_UUID.
CLEAR TM_UUID.
CALL
FUNCTION
'SYSTEM_UUID_CREATE'
IMPORTING
UUID = LV_UUID.
TM_UUID = LV_UUID.
CLEAR LV_FKEY.
CONCATENATE SY-UNAME SY-DATUM SY-UZEIT TM_UUID
INTO LV_FKEY.
CLEAR LV_FINDX.
REFRESH LOG1.
CLEAR LOG1.
LOOP
AT
INPUT.
LV_FINDX = LV_FINDX +
1.
MOVE-CORRESPONDING
INPUT
TO LOG1.
LOG1-FKEY = LV_FKEY.
LOG1-FINDX = LV_FINDX.
APPEND LOG1.
CLEAR LOG1.
ENDLOOP.
CLEAR LV_FINDX.
REFRESH LOG2.
CLEAR LOG2.
LOOP
AT INPUT_QN.
LV_FINDX = LV_FINDX +
1.
MOVE-CORRESPONDING INPUT_QN
TO LOG2.
LOG2-FKEY = LV_FKEY.
LOG2-FINDX = LV_FINDX.
APPEND LOG2.
CLEAR LOG2.
ENDLOOP.
CLEAR LV_FINDX.
REFRESH LOG3.
CLEAR LOG3.
LOOP
AT
OUTPUT.
LV_FINDX = LV_FINDX +
1.
MOVE-CORRESPONDING
OUTPUT
TO LOG3.
LOG3-FKEY = LV_FKEY.
LOG3-FINDX = LV_FINDX.
APPEND LOG3.
CLEAR LOG3.
ENDLOOP.
IF LOG1[]
IS
NOT
INITIAL.
MODIFY ZWMS_LOG03_01
FROM
TABLE LOG1.
ENDIF.
IF LOG2[]
IS
NOT
INITIAL.
MODIFY ZWMS_LOG03_02
FROM
TABLE LOG2.
ENDIF.
IF LOG3[]
IS
NOT
INITIAL.
MODIFY ZWMS_LOG03_03
FROM
TABLE LOG3.
ENDIF.
*--------------------------日志信息end--------------------------
ENDFUNCTION.