FUNCTION ZWMS_PRODUCT_STOCKIN.
*"----------------------------------------------------------------------
*"*"Local interface:
*" TABLES
*" INPUT STRUCTURE ZWMS_PRODUCT_STOCKIN_INPUT OPTIONAL
*" OUTPUT STRUCTURE ZWMS_PRODUCT_STOCKIN_OUPUT OPTIONAL
*"----------------------------------------------------------------------
DATA: GOODSMVT_HEADER TYPE BAPI2017_GM_HEAD_01,
GOODSMVT_CODE TYPE BAPI2017_GM_CODE,
GOODSMVT_HEADRET TYPE BAPI2017_GM_HEAD_RET,
MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
MATDOCUMENTYEAR TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR,
GOODSMVT_ITEM TYPE STANDARD TABLE OF BAPI2017_GM_ITEM_CREATE,
GOODSMVT_SERIALNUMBER TYPE STANDARD TABLE OF BAPI2017_GM_SERIALNUMBER,
RETURN TYPE STANDARD TABLE OF BAPIRET2 ,
W_CRETURN TYPE BAPIRET2,
W_GOODSMVT_ITEM TYPE BAPI2017_GM_ITEM_CREATE,
W_GOODSMVT_SERIALNUMBER TYPE BAPI2017_GM_SERIALNUMBER,
W_RETURN TYPE BAPIRET2.
CLEAR:GOODSMVT_HEADER,GOODSMVT_CODE,GOODSMVT_HEADRET,MATERIALDOCUMENT,MATDOCUMENTYEAR, GOODSMVT_ITEM ,RETURN,W_RETURN,GOODSMVT_SERIALNUMBER,W_GOODSMVT_SERIALNUMBER."LT_TPMSG,LT_RETMSG.
REFRESH:GOODSMVT_ITEM,RETURN,GOODSMVT_SERIALNUMBER.
"事物码
GOODSMVT_CODE-GM_CODE = '02'. "MB31
"头
GOODSMVT_HEADER-HEADER_TXT = INPUT-YWLX.
GOODSMVT_HEADER-PSTNG_DATE = SY-DATUM.
GOODSMVT_HEADER-DOC_DATE = SY-DATUM.
"明细
DATA:LV_ENTRY_QNT TYPE I.
CLEAR LV_ENTRY_QNT.
LV_ENTRY_QNT = LINES( INPUT ).
READ TABLE INPUT INDEX 1.
IF SY-SUBRC EQ 0.
W_GOODSMVT_ITEM-MOVE_TYPE = '101'.
W_GOODSMVT_ITEM-STCK_TYPE = 'X'.
W_GOODSMVT_ITEM-MVT_IND = 'F'.
W_GOODSMVT_ITEM-PLANT = INPUT-WERKS.
W_GOODSMVT_ITEM-ORDERID = INPUT-AUFNR.
W_GOODSMVT_ITEM-ENTRY_QNT = LV_ENTRY_QNT.
SELECT SINGLE
PWERK
LGORT
MATNR
MEINS
INTO (W_GOODSMVT_ITEM-PLANT,W_GOODSMVT_ITEM-STGE_LOC,W_GOODSMVT_ITEM-MATERIAL,W_GOODSMVT_ITEM-ENTRY_UOM)
FROM AFPO
WHERE AUFNR = INPUT-AUFNR.
APPEND W_GOODSMVT_ITEM TO GOODSMVT_ITEM.
ENDIF.
LOOP AT INPUT.
W_GOODSMVT_SERIALNUMBER-MATDOC_ITM = '0001'.
W_GOODSMVT_SERIALNUMBER-SERIALNO = INPUT-SERNR.
APPEND W_GOODSMVT_SERIALNUMBER TO GOODSMVT_SERIALNUMBER.
ENDLOOP.
"执行操作
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
GOODSMVT_HEADER = GOODSMVT_HEADER
GOODSMVT_CODE = GOODSMVT_CODE
IMPORTING
GOODSMVT_HEADRET = GOODSMVT_HEADRET
MATERIALDOCUMENT = MATERIALDOCUMENT
MATDOCUMENTYEAR = MATDOCUMENTYEAR
TABLES
GOODSMVT_ITEM = GOODSMVT_ITEM
GOODSMVT_SERIALNUMBER = GOODSMVT_SERIALNUMBER
RETURN = RETURN.
READ TABLE RETURN WITH KEY TYPE = 'E' INTO W_RETURN.
IF SY-SUBRC <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
IF MATERIALDOCUMENT IS NOT INITIAL.
OUTPUT-FLAG = 'S'.
OUTPUT-NOTES = '成品入库成功'.
OUTPUT-MBLNR = MATERIALDOCUMENT.
APPEND OUTPUT.
CLEAR OUTPUT.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
OUTPUT-FLAG = 'E'.
CONCATENATE '成品入库失败:' W_RETURN-MESSAGE INTO OUTPUT-NOTES.
APPEND OUTPUT.
CLEAR OUTPUT.
ENDIF.
ENDFUNCTION.
*"----------------------------------------------------------------------
*"*"Local interface:
*" TABLES
*" INPUT STRUCTURE ZWMS_PRODUCT_STOCKIN_INPUT OPTIONAL
*" OUTPUT STRUCTURE ZWMS_PRODUCT_STOCKIN_OUPUT OPTIONAL
*"----------------------------------------------------------------------
DATA: GOODSMVT_HEADER TYPE BAPI2017_GM_HEAD_01,
GOODSMVT_CODE TYPE BAPI2017_GM_CODE,
GOODSMVT_HEADRET TYPE BAPI2017_GM_HEAD_RET,
MATERIALDOCUMENT TYPE BAPI2017_GM_HEAD_RET-MAT_DOC,
MATDOCUMENTYEAR TYPE BAPI2017_GM_HEAD_RET-DOC_YEAR,
GOODSMVT_ITEM TYPE STANDARD TABLE OF BAPI2017_GM_ITEM_CREATE,
GOODSMVT_SERIALNUMBER TYPE STANDARD TABLE OF BAPI2017_GM_SERIALNUMBER,
RETURN TYPE STANDARD TABLE OF BAPIRET2 ,
W_CRETURN TYPE BAPIRET2,
W_GOODSMVT_ITEM TYPE BAPI2017_GM_ITEM_CREATE,
W_GOODSMVT_SERIALNUMBER TYPE BAPI2017_GM_SERIALNUMBER,
W_RETURN TYPE BAPIRET2.
CLEAR:GOODSMVT_HEADER,GOODSMVT_CODE,GOODSMVT_HEADRET,MATERIALDOCUMENT,MATDOCUMENTYEAR, GOODSMVT_ITEM ,RETURN,W_RETURN,GOODSMVT_SERIALNUMBER,W_GOODSMVT_SERIALNUMBER."LT_TPMSG,LT_RETMSG.
REFRESH:GOODSMVT_ITEM,RETURN,GOODSMVT_SERIALNUMBER.
"事物码
GOODSMVT_CODE-GM_CODE = '02'. "MB31
"头
GOODSMVT_HEADER-HEADER_TXT = INPUT-YWLX.
GOODSMVT_HEADER-PSTNG_DATE = SY-DATUM.
GOODSMVT_HEADER-DOC_DATE = SY-DATUM.
"明细
DATA:LV_ENTRY_QNT TYPE I.
CLEAR LV_ENTRY_QNT.
LV_ENTRY_QNT = LINES( INPUT ).
READ TABLE INPUT INDEX 1.
IF SY-SUBRC EQ 0.
W_GOODSMVT_ITEM-MOVE_TYPE = '101'.
W_GOODSMVT_ITEM-STCK_TYPE = 'X'.
W_GOODSMVT_ITEM-MVT_IND = 'F'.
W_GOODSMVT_ITEM-PLANT = INPUT-WERKS.
W_GOODSMVT_ITEM-ORDERID = INPUT-AUFNR.
W_GOODSMVT_ITEM-ENTRY_QNT = LV_ENTRY_QNT.
SELECT SINGLE
PWERK
LGORT
MATNR
MEINS
INTO (W_GOODSMVT_ITEM-PLANT,W_GOODSMVT_ITEM-STGE_LOC,W_GOODSMVT_ITEM-MATERIAL,W_GOODSMVT_ITEM-ENTRY_UOM)
FROM AFPO
WHERE AUFNR = INPUT-AUFNR.
APPEND W_GOODSMVT_ITEM TO GOODSMVT_ITEM.
ENDIF.
LOOP AT INPUT.
W_GOODSMVT_SERIALNUMBER-MATDOC_ITM = '0001'.
W_GOODSMVT_SERIALNUMBER-SERIALNO = INPUT-SERNR.
APPEND W_GOODSMVT_SERIALNUMBER TO GOODSMVT_SERIALNUMBER.
ENDLOOP.
"执行操作
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
GOODSMVT_HEADER = GOODSMVT_HEADER
GOODSMVT_CODE = GOODSMVT_CODE
IMPORTING
GOODSMVT_HEADRET = GOODSMVT_HEADRET
MATERIALDOCUMENT = MATERIALDOCUMENT
MATDOCUMENTYEAR = MATDOCUMENTYEAR
TABLES
GOODSMVT_ITEM = GOODSMVT_ITEM
GOODSMVT_SERIALNUMBER = GOODSMVT_SERIALNUMBER
RETURN = RETURN.
READ TABLE RETURN WITH KEY TYPE = 'E' INTO W_RETURN.
IF SY-SUBRC <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
IF MATERIALDOCUMENT IS NOT INITIAL.
OUTPUT-FLAG = 'S'.
OUTPUT-NOTES = '成品入库成功'.
OUTPUT-MBLNR = MATERIALDOCUMENT.
APPEND OUTPUT.
CLEAR OUTPUT.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
OUTPUT-FLAG = 'E'.
CONCATENATE '成品入库失败:' W_RETURN-MESSAGE INTO OUTPUT-NOTES.
APPEND OUTPUT.
CLEAR OUTPUT.
ENDIF.
ENDFUNCTION.