sap批量建立盤點憑證以及盤點憑證過賬
業務場景:wms傳入單號到sap,sap通過bapi生成盤點憑證並過賬(實際業務是由財務過賬,這裡只說明技術)
前臺一步輸入物料號和盤點數量生成盤點憑證,在進行過賬,(2步),sap是先生成盤點憑證,再輸入資料最後差異過賬(3步)
注意:bapi:建立盤點憑證 :BAPI_MATPHYSINV_CREATE_MULT
盤點數量:BAPI_MATPHYSINV_COUNT(一定要記得傳入item行號)
差異過賬:BAPI_MATPHYSINV_POSTDIFF(傳入行號item)
*&---------------------------------------------------------------------*
*& Form CREATE_WSCODE
*&---------------------------------------------------------------------*
* 在這裡創鍵盤點憑證。
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_CREATE_WSCODE TABLES GT_ALL STRUCTURE GS_ALL
T_RETURN STRUCTURE ZSMM0005
CHANGING E_RTYPE E_RTMSG.
LOOP AT GT_ALL INTO GS_ALL.
GS_DATA2 = GS_ALL.
APPEND GS_DATA2 TO GT_DATA2.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM GT_DATA2 COMPARING WMSORD."刪除重複項
*IF SY-SUBRC = 0.
LOOP AT GT_DATA2 INTO GS_DATA2.
CLEAR: GS_HEAD_MUTT,
GS_ITEMS_MUTT,
GS_ITEMS_COUNT,
GS_ITEMS_POSTDIFF,
GS_RETURN.
CLEAR: GT_ITEMS_MUTT[],
GT_ITEMS_COUNT[],
GT_ITEMS_POSTDIFF[].
GV_INDEX2 = 0.
LOOP AT GT_ALL INTO GS_ALL WHERE WMSORD = GS_DATA2-WMSORD .
GV_INDEX2 = GV_INDEX2 + 1.
* -----------------------------
* 把同樣的訂單號對應的資料傳入到一個表中,然後統一處理一個倉庫中的所有物料號生成一個盤點憑證
* 處理wms傳進來的盤點單號
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = GS_ALL-WMSORD
IMPORTING
OUTPUT = GS_ALL-WMSORD.
GV_INDOC = GS_ALL-WMSORD."wms盤點單號
*根據物料號獲取對應單位
READ TABLE GT_ALL INTO GS_ALL WITH KEY MATNR = GS_ALL-MATNR.
SELECT MEINS
INTO TABLE GT_DETAIL
FROM MARA
WHERE MATNR = GS_ALL-MATNR.
CLEAR GS_DETAIL.
LOOP AT GT_DETAIL INTO GS_DETAIL.
“內外碼轉換”
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
EXPORTING
INPUT = GS_DETAIL-MEINS
IMPORTING
OUTPUT = GS_DETAIL-MEINS
EXCEPTIONS
UNIT_NOT_FOUND = 1
OTHERS = 2.
ENDLOOP.
* 獲取年度
GV_YEAR = GS_ALL-BLDAT+0(4).
* head
GS_HEAD_MUTT-PLANT = GS_ALL-WERKS. "工廠
GS_HEAD_MUTT-STGE_LOC = GS_ALL-LGORT. "庫存地點
GS_HEAD_MUTT-SPEC_STOCK = GS_ALL-SOBKZ. "特殊庫存標識
GS_HEAD_MUTT-DOC_DATE = GS_ALL-BLDAT. "系統時間"T_DATA-BLDAT. "憑證中的憑證日期
GS_HEAD_MUTT-PLAN_DATE = GS_ALL-ZLDAT. "存貨盤點的計劃日期
GS_HEAD_MUTT-PHYS_INV_REF = GS_ALL-WMSORD. "wms盤點單號
* items
CLEAR GS_ITEMS_MUTT.
GS_ITEMS_MUTT-MATERIAL_LONG = GS_ALL-MATNR. "物料編號(18位)
GS_ITEMS_MUTT-BATCH = GS_ALL-CHARG. "批號
APPEND GS_ITEMS_MUTT TO GT_ITEMS_MUTT.
** count
CLEAR GS_ITEMS_COUNT.
GS_ITEMS_COUNT-ENTRY_QNT = GS_ALL-ERFMG. "以錄入項單位表示的數量
GS_ITEMS_COUNT-ENTRY_UOM = GS_DETAIL-MEINS. "單位
GS_ITEMS_COUNT-MATERIAL_LONG = GS_ALL-MATNR. "物料編號(18位)
GS_ITEMS_COUNT-BATCH = GS_ALL-CHARG. "批號
GS_ITEMS_COUNT-ITEM = GV_INDEX2.
APPEND GS_ITEMS_COUNT TO GT_ITEMS_COUNT.
* 差異過賬
CLEAR GS_ITEMS_POSTDIFF.
GS_ITEMS_POSTDIFF-MATERIAL_LONG = GS_ALL-MATNR. "物料編號(18位)
GS_ITEMS_POSTDIFF-BATCH = GS_ALL-CHARG. "批號
GS_ITEMS_POSTDIFF-ITEM = GV_INDEX2.
APPEND GS_ITEMS_POSTDIFF TO GT_ITEMS_POSTDIFF.
**---------------------更新序列號--------------------------------------------------*
MODIFY GT_ALL INDEX GV_INDEX2.
CLEAR:GS_ALL.
ENDLOOP.
**----------------------------------建立盤點憑證------------------------*
CALL FUNCTION 'BAPI_MATPHYSINV_CREATE_MULT'
EXPORTING
HEAD = GS_HEAD_MUTT
TABLES
ITEMS = GT_ITEMS_MUTT
RETURN = GT_RETURN.
CLEAR GS_RETURN.
IF GT_RETURN IS NOT INITIAL.
READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'S' ID = 'M7' NUMBER = '710'.
IF SY-SUBRC <> 0.
READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'E' .
T_RETURN-MTYPE = 'E'.
T_RETURN-MBLNR = GS_RETURN-MESSAGE_V3 .
T_RETURN-MTMSG = '該盤點憑證還未過賬,請先過賬或者刪除,刪除時需傳入LSTAT為X,同時輸入新的資料可建立新的盤點憑證'.
APPEND T_RETURN.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = ABAP_TRUE.
GV_OUTDOC = GS_RETURN-MESSAGE_V1."獲取bapi產生的盤點憑證
"內外碼轉換
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = GV_OUTDOC
IMPORTING
OUTPUT = GV_OUTDOC.
T_RETURN-MTYPE = 'S'.
T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.
T_RETURN-MBLNR = GV_YEAR.
T_RETURN-MTMSG = GS_RETURN-MESSAGE.
E_RTYPE = 'S'.
E_RTMSG = GS_RETURN-MESSAGE.
APPEND T_RETURN.
CLEAR GS_RETURN.
ENDIF.
ELSE.
E_RTYPE = 'E'.
E_RTMSG = TEXT-002.
RETURN.
ENDIF.
****count-------
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_MATPHYSINV_COUNT'
EXPORTING
PHYSINVENTORY = GV_OUTDOC
FISCALYEAR = GV_YEAR
TABLES
ITEMS = GT_ITEMS_COUNT
RETURN = GT_RETURN.
CLEAR GS_RETURN.
IF GT_RETURN IS NOT INITIAL.
READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'S' ID = 'M7' NUMBER = '713'.
IF SY-SUBRC = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = ABAP_TRUE.
T_RETURN-MTYPE = 'S'.
T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.
T_RETURN-MBLNR = GV_YEAR.
T_RETURN-MTMSG = GS_RETURN-MESSAGE.
APPEND T_RETURN.
CLEAR GS_RETURN.
ENDIF.
ENDIF.
ENDIF.
****----------------------差異過賬-------------------------------------*
* IF SY-SUBRC = 0.
* CALL FUNCTION 'BAPI_MATPHYSINV_POSTDIFF'
* EXPORTING
* PHYSINVENTORY = GV_OUTDOC
* FISCALYEAR = GV_YEAR
* TABLES
* ITEMS = GT_ITEMS_POSTDIFF
* RETURN = GT_RETURN.
* IF GT_RETURN IS NOT INITIAL.
* LOOP AT GT_RETURN INTO GS_RETURN WHERE TYPE = 'S' AND ID ='M7' AND NUMBER = '716' OR NUMBER = '715'.
* IF SY-SUBRC <> 0.
* T_RETURN-MTYPE = 'E'.
* T_RETURN-MTMSG = GS_RETURN-MESSAGE.
* E_RTYPE = 'E'.
* E_RTMSG = GS_RETURN-MESSAGE.
* ELSE.
* CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
* WAIT = ABAP_TRUE.
* T_RETURN-MTYPE = 'S'.
* T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.
* T_RETURN-MBLNR = GS_RETURN-MESSAGE_V2.
* T_RETURN-MTMSG = GS_RETURN-MESSAGE.
* E_RTYPE = 'S'.
* E_RTMSG = GS_RETURN-MESSAGE.
* APPEND T_RETURN.
* ENDIF.
* ENDLOOP.
** READ TABLE GT_RETURN INTO GS_RETURN WITH KEY TYPE = 'S' ID = 'M7' NUMBER = '716'.
** IF SY-SUBRC <> 0.
** T_RETURN-MTYPE = 'E'.
** T_RETURN-MTMSG = GS_RETURN-MESSAGE.
** E_RTYPE = 'E'.
** E_RTMSG = GS_RETURN-MESSAGE.
** ELSE.
** CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
** EXPORTING
** WAIT = ABAP_TRUE.
** T_RETURN-MTYPE = 'S'.
** T_RETURN-WMSORD = GS_RETURN-MESSAGE_V1.
** T_RETURN-MBLNR = GS_RETURN-MESSAGE_V2.
** T_RETURN-MTMSG = GS_RETURN-MESSAGE.
** E_RTYPE = 'S'.
** E_RTMSG = GS_RETURN-MESSAGE.
** APPEND T_RETURN.
** ENDIF.
* ENDIF.
* ENDIF.
CLEAR:GS_ALL,GS_DATA2.
ENDLOOP.
CLEAR:GT_ALL,GS_ALL,GT_DATA2,GS_DATA2.
ENDFORM.