SAP ABAP MARD和MARDH计算逻辑
MARD里記載的是當前庫存的數量,但是期間并不一定是當月。比如你物料4月一整月都沒有庫存數量變化(沒收也沒發),那么5月初你看MARD表里的條目期間數還是4月而非5月。
當某個期間發生貨物移動的時候,系統在更新MARD數據的之前(這個表是實時更新的),會檢查此筆業務過賬期間和MARD里對應記錄的期間是否一致,也就是看這是不是本期間第一筆移動。如果是,copy表MARD里對應記錄到MARDH,然后把MARD記錄改成當期(也可能是先刪后建),然后再作更新數量數據的操作。如果不是第一筆記錄,也就是MARD期間和MSEG期間一致,則不作copy記錄只更新MARD數量。
這樣處理貌似減少了冗余數據,不過給編程取歷史庫存增加了很大的工作量,個人覺得不算明智之舉,但是可以做幾個精明的函數取值一勞永逸,具體可以跟本人聯系。
涉及透明表:
? ? ? ? ? MARD:物料倉儲位置的當前庫存數據
? ? ? ? ? MARDH:物料倉儲庫存的歷史數據
其存數邏輯如下:
Scenario?方案
At the start of period 02, there are 10 pieces of material A100 in stock.
Goods receipt 收貨
5 pieces are received in period 02.
System response
The system records(生成一條記錄) a stock of 10 pieces in the history table (MARDH) for period 01. At the same time, the system increases the current stock to 15 pieces (MARD).
Goods receipt?收貨
2 more pieces are received in period 02.
System response
The history table is unaffected by this event because an entry already exists for period 01. The system increases the current stock to 17 pieces (MARD).
Goods issue 發貨
4 pieces are withdrawn from stock in period 04.
System response
The system records(生成一條記錄) a stock of 17 pieces in the history table (MARDH) for period 03. At the same time, the system reduces the current stock to 13 pieces.
注:The history table (MARDH)does not contain an entry for period 02 because there were no goods movements in period 03.
?
到此為止,MARD中:物料 A100 期間 04 數量 13
MARDH中:物料 A100 期間 01 數量 10
? ? ? ? ? ? ? ? ?物料 A100 期間 03 數量 17
由此可見,如果要查詢物料A100在期間02的庫存,應是17;在期間05的庫存則是13。
下面函數可實現查詢任一工廠下、任一庫存地點中、任一物料、在任一期間末的庫存(非限制+質檢中+凍結)。
FUNCTION Z_GET_COMM_STOCK.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(WERKS) LIKE MARD-WERKS
*" REFERENCE(LGORT) LIKE MARD-LGORT
*" REFERENCE(MATNR) LIKE MARD-MATNR
*" REFERENCE(LFGJA) LIKE MARD-LFGJA
*" REFERENCE(LFMON) LIKE MARD-LFMON
*" EXPORTING
*" REFERENCE(CURR_STOCK) LIKE MARD-LABST
*"----------------------------------------------------------------------
data: cyear(4),
? ? ? ? ?cmonth(2),
? ? ? ? ?currmonth(6),
? ? ? ? ?mardmonth(6).
data: h_tab like mardh occurs 0 with header line.
cyear = lfgja.?
cmonth = lfmon.
concatenate cyear cmonth into currmonth."查詢庫存的年月
*----------------------------------------------------------------
比如上面的例子,我們這個時候查詢02期間的庫存數量
select single *?
from mard?
where matnr = matnr?
? ?and lgort = lgort and
? ? ? ? ?werks = werks.
if sy-subrc = 0.
"當前庫存表里面的年月
cyear = mard-lfgja.?
cmonth = mard-lfmon.
concatenate cyear cmonth into mardmonth.
if mardmonth > currmonth.
*-----本期期末庫存已經存入MARDH
select *?
into table h_tab
from mardh?
where matnr = matnr
? ? and lgort = lgort
? ? and werks = werks
? ? and ( ( lfgja = lfgja and
? ? ? ? ? lfmon >= lfmon ) or lfgja > lfgja )
? ? order by lfgja ascending lfmon ascending .
if sy-subrc = 0.
loop at h_tab.
cyear = h_tab-lfgja.?
cmonth = h_tab-lfmon.
concatenate cyear cmonth into mardmonth.
比如上面的例子,這里查出來的結果應該是下圖
依次比較01 月 和 03 月 ?發現在03月的時候滿足條件 即庫存為17
if mardmonth >= currmonth.
curr_stock = h_tab-labst + h_tab-insme + h_tab-speme.
exit.
endif.
endloop.
endif.
else.
*-----本期期末庫存還未存入MARDH
curr_stock?= mard-labst + mard-insme + mard-speme.
endif.
endif.
ENDFUNCTION.
上面函數在報表程序中的調用方法如下:
CALL FUNCTION?'Z_GET_COMM_STOCK'
EXPORTING
? ? ?WERKS = '工廠'
? ? ?LGORT = '倉儲地點'
? ? ?MATNR = '物料號'
? ? ?LFGJA = '會計年度'
? ? ?LFMON = '會計期間'
IMPORTING
? ? ?CURR_STOCK = GV_STOCK.
其中GV_STOCK為存儲最終結果的變量,即查詢物料在指定工廠、指定倉儲地點、指定會計期間末的庫存。
類似的透明表:
庫存類型 ? ? ? ?描述 ? ? ? ? ?表 ? ? ? ? ? ?歷史表
空 ? ? ? ? ? ? ? ? 自由庫存 ? ? mard ? ? ? mardh
K ? ? ? ? ? ? ? ? ?供應商寄售 ?mkol ? ? ? mkolh
E ? ? ? ? ? ? ? ? ?銷售訂單 ? ? mska ? ? ?mskah
W ? ? ? ? ? ? ? ? 寄售到客戶 ?msku ? ? ?mskuh
Q ? ? ? ? ? ? ? ? ?項目庫存 ? ? mspr ? ? ? msprh
O ? ? ? ? ? ? ? ? ?發貨給供應商 ?mslb ? ? mslbh
? ? ? ? ? ? ? ? ? ? 物料價格 ? ? ?MBEW ? ? MBEWH
這幾種特殊庫存與mseg表中的操作記錄的對應的關系
庫存類型是O,外發商庫存
庫存類型是Q,生產批次庫存
庫存類型是W,寄售給客戶
庫存類型是E,銷售訂單庫存
庫存類型是K,供應商寄售庫存
自由庫存是空
由于其物料性質與常用料不同,在計算其期末庫存時跟常用料的計算方法有些許差異。
總結
以上是生活随笔為你收集整理的SAP ABAP MARD和MARDH计算逻辑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP中的容差介绍
- 下一篇: SAP ABAP 特性相关表取数逻辑