SAP讀書會:呆滯庫存的關鍵技術分享
呆滯庫存的報表原由:
呆滯庫存報表一般可以由MB5B依據過帳資訊來產生呆滯物料的資訊,但是如果遇到需要客製報表的需求,我們可以自己設計。
關鍵Table:
呆滯庫存的設計需要運用到過帳相關紀錄資訊,如 MSEG & MKPF,兩者透過文件號碼做關聯(MBLNR);另外其他的物料資訊可以由MARD 取得。
邏輯關鍵SQL:
首先要以物料主檔(MARD)為主要的Table並JOIN 物料異動紀錄(MSEG) & 過帳資訊紀錄(MKPF),其中物料主檔(MARD)與物料異動紀錄(MSEG)以物料號碼做為關聯(KEY),另外物料異動紀錄(MSEG)與過帳記錄資訊(MKPF)用物料文件號碼(MBLNR)做關聯。
輸入的主要參數有(Input):
P_WERKS FOR MARD-WERKS DEFAULT 'TW11', "工廠
P_MATNR FOR MARD-MATNR,"物料號碼
P_SDATE FOR MARD-ERSDA. "開始與結束日期
輸出的主要參數有(Output):
LIKE MARD-MATNR,"物料號碼
LIKE MARD-LABST,"未使用庫存數量
LIKE MARD-WERKS,"工廠
LIKE MARD-LGORT,"庫位
LIKE MBEW-VERPR,"異動平均價
LIKE MBEW-LBKUM,"庫存總值
主要的宣告:
DATA: I_SA TYPE P DECIMALS 4. "庫存總值
* 定義日期的字串月份擷取
DATA: STRING(10),
V_OFFSET_START TYPE I,
V_OFFSET_LENGTH TYPE I,
V_YMD(10) TYPE C,
T_YEAR(4) TYPE C,
V_YEAR(4) TYPE C.
主要SQL 有兩個ITAB,分別是ITAB3 & ITAB4:
ITAB3 主要是抓出所有的資訊,而ITAB4 主要抓出跟相關呆滯有關的關鍵異動類型,最後再用LOOP AT ITAB的方式,ITAB4就是把不符合呆滯的物料都抓出來放到ITAB4,這樣ITAB3 去比對ITAB4 就可以排出MSEG 跟非符合呆滯的物料。
不然單一一個SQL去排除,通常只會排除當筆的MSEG紀錄,其他的相同物料一樣會在列出來。
以下是ITAB3 主要SQL
SELECT
MARD~MATNR MARD~LABST MARD~WERKS MARD~LGORT
MSEG~BWART MKPF~BUDAT
FROM MARD
INNER JOIN MSEG ON MARD~MATNR = MSEG~MATNR
INNER JOIN MKPF ON MSEG~MBLNR = MKPF~MBLNR AND MSEG~MJAHR = MKPF~MJAHR
INTO CORRESPONDING FIELDS OF TABLE ITAB3
WHERE MARD~MATNR = MSEG~MATNR
AND MARD~WERKS = MSEG~WERKS
AND MSEG~MBLNR = MKPF~MBLNR
AND MSEG~MJAHR = MKPF~MJAHR
AND MSEG~BWART NE '261'
AND MSEG~BWART NE '311'
AND MSEG~BWART NE '309'
AND MSEG~BWART NE '901'
AND MSEG~BWART NE '903'
AND MSEG~BWART NE '913'
AND MSEG~BWART NE '915'
AND MSEG~BWART NE '917'
AND MSEG~BWART NE '919'
AND MSEG~BWART NE '101'
AND MARD~MATNR IN P_MATNR
AND MARD~WERKS IN P_WERKS
AND MARD~LGORT IN P_LGORT
AND MKPF~BUDAT IN P_SDATE.
以下是主要的ITAB4 SQL
*ITAB4
SELECT
MSEG~MBLNR "Number of Material Document
MSEG~MJAHR "Material Document Year
MSEG~BWART "Moving Type
MSEG~MATNR "Material Number
MSEG~WERKS "Plant
MSEG~LGORT "Storage Location
MSEG~LIFNR "Vendor Number
MKPF~BUDAT
FROM MSEG
INNER JOIN MKPF ON MSEG~MBLNR = MKPF~MBLNR AND MSEG~MJAHR = MKPF~MJAHR
INTO CORRESPONDING FIELDS OF TABLE ITAB4
WHERE MSEG~MBLNR = MKPF~MBLNR
AND MSEG~MJAHR = MKPF~MJAHR
AND ( MSEG~BWART = '101'
OR MSEG~BWART = '261'
OR MSEG~BWART = '311'
OR MSEG~BWART = '309'
OR MSEG~BWART = '901'
OR MSEG~BWART = '903'
OR MSEG~BWART = '913'
OR MSEG~BWART = '915'
OR MSEG~BWART = '917'
OR MSEG~BWART = '919' )
AND MSEG~MATNR IN P_MATNR
AND MSEG~WERKS IN P_WERKS
AND MSEG~LGORT IN P_LGORT
AND MKPF~BUDAT IN P_SDATE.
*將ITAB3 有涉及到的異動類型都Clear
SORT ITAB3 BY MATNR.
SORT ITAB4 BY MATNR.
DELETE ADJACENT DUPLICATES FROM ITAB4 COMPARING MATNR.
LOOP AT ITAB3.
LOOP AT ITAB4.
IF ITAB3-MATNR = ITAB4-MATNR.
CLEAR ITAB3.
MODIFY ITAB3.
ENDIF.
ENDLOOP.
ENDLOOP.
呆滯庫存報表一般可以由MB5B依據過帳資訊來產生呆滯物料的資訊,但是如果遇到需要客製報表的需求,我們可以自己設計。
關鍵Table:
呆滯庫存的設計需要運用到過帳相關紀錄資訊,如 MSEG & MKPF,兩者透過文件號碼做關聯(MBLNR);另外其他的物料資訊可以由MARD 取得。
邏輯關鍵SQL:
首先要以物料主檔(MARD)為主要的Table並JOIN 物料異動紀錄(MSEG) & 過帳資訊紀錄(MKPF),其中物料主檔(MARD)與物料異動紀錄(MSEG)以物料號碼做為關聯(KEY),另外物料異動紀錄(MSEG)與過帳記錄資訊(MKPF)用物料文件號碼(MBLNR)做關聯。
輸入的主要參數有(Input):
P_WERKS FOR MARD-WERKS DEFAULT 'TW11', "工廠
P_MATNR FOR MARD-MATNR,"物料號碼
P_SDATE FOR MARD-ERSDA. "開始與結束日期
輸出的主要參數有(Output):
LIKE MARD-MATNR,"物料號碼
LIKE MARD-LABST,"未使用庫存數量
LIKE MARD-WERKS,"工廠
LIKE MARD-LGORT,"庫位
LIKE MBEW-VERPR,"異動平均價
LIKE MBEW-LBKUM,"庫存總值
主要的宣告:
DATA: I_SA TYPE P DECIMALS 4. "庫存總值
* 定義日期的字串月份擷取
DATA: STRING(10),
V_OFFSET_START TYPE I,
V_OFFSET_LENGTH TYPE I,
V_YMD(10) TYPE C,
T_YEAR(4) TYPE C,
V_YEAR(4) TYPE C.
主要SQL 有兩個ITAB,分別是ITAB3 & ITAB4:
ITAB3 主要是抓出所有的資訊,而ITAB4 主要抓出跟相關呆滯有關的關鍵異動類型,最後再用LOOP AT ITAB的方式,ITAB4就是把不符合呆滯的物料都抓出來放到ITAB4,這樣ITAB3 去比對ITAB4 就可以排出MSEG 跟非符合呆滯的物料。
不然單一一個SQL去排除,通常只會排除當筆的MSEG紀錄,其他的相同物料一樣會在列出來。
以下是ITAB3 主要SQL
SELECT
MARD~MATNR MARD~LABST MARD~WERKS MARD~LGORT
MSEG~BWART MKPF~BUDAT
FROM MARD
INNER JOIN MSEG ON MARD~MATNR = MSEG~MATNR
INNER JOIN MKPF ON MSEG~MBLNR = MKPF~MBLNR AND MSEG~MJAHR = MKPF~MJAHR
INTO CORRESPONDING FIELDS OF TABLE ITAB3
WHERE MARD~MATNR = MSEG~MATNR
AND MARD~WERKS = MSEG~WERKS
AND MSEG~MBLNR = MKPF~MBLNR
AND MSEG~MJAHR = MKPF~MJAHR
AND MSEG~BWART NE '261'
AND MSEG~BWART NE '311'
AND MSEG~BWART NE '309'
AND MSEG~BWART NE '901'
AND MSEG~BWART NE '903'
AND MSEG~BWART NE '913'
AND MSEG~BWART NE '915'
AND MSEG~BWART NE '917'
AND MSEG~BWART NE '919'
AND MSEG~BWART NE '101'
AND MARD~MATNR IN P_MATNR
AND MARD~WERKS IN P_WERKS
AND MARD~LGORT IN P_LGORT
AND MKPF~BUDAT IN P_SDATE.
以下是主要的ITAB4 SQL
*ITAB4
SELECT
MSEG~MBLNR "Number of Material Document
MSEG~MJAHR "Material Document Year
MSEG~BWART "Moving Type
MSEG~MATNR "Material Number
MSEG~WERKS "Plant
MSEG~LGORT "Storage Location
MSEG~LIFNR "Vendor Number
MKPF~BUDAT
FROM MSEG
INNER JOIN MKPF ON MSEG~MBLNR = MKPF~MBLNR AND MSEG~MJAHR = MKPF~MJAHR
INTO CORRESPONDING FIELDS OF TABLE ITAB4
WHERE MSEG~MBLNR = MKPF~MBLNR
AND MSEG~MJAHR = MKPF~MJAHR
AND ( MSEG~BWART = '101'
OR MSEG~BWART = '261'
OR MSEG~BWART = '311'
OR MSEG~BWART = '309'
OR MSEG~BWART = '901'
OR MSEG~BWART = '903'
OR MSEG~BWART = '913'
OR MSEG~BWART = '915'
OR MSEG~BWART = '917'
OR MSEG~BWART = '919' )
AND MSEG~MATNR IN P_MATNR
AND MSEG~WERKS IN P_WERKS
AND MSEG~LGORT IN P_LGORT
AND MKPF~BUDAT IN P_SDATE.
*將ITAB3 有涉及到的異動類型都Clear
SORT ITAB3 BY MATNR.
SORT ITAB4 BY MATNR.
DELETE ADJACENT DUPLICATES FROM ITAB4 COMPARING MATNR.
LOOP AT ITAB3.
LOOP AT ITAB4.
IF ITAB3-MATNR = ITAB4-MATNR.
CLEAR ITAB3.
MODIFY ITAB3.
ENDIF.
ENDLOOP.
ENDLOOP.