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_PERIOD_STOCK_3.
*"----------------------------------------------------------------------
*"*"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.
*----------------------------------------------------------------
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.
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_PERIOD_STOCK_3'
EXPORTING
WERKS = '工廠'
LGORT = '倉儲地點'
MATNR = '物料號'
LFGJA = '會計年度'
LFMON = '會計期間'
IMPORTING
CURR_STOCK = I_TAB-STOCK.
其中I_TAB-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,供應商寄售庫存
自由庫存是空
來自:hen20.com很愛你
由於其物料性質與常用料不同,在計算其期末庫存時跟常用料的計算方法有些許差異。
http://hi.baidu.com/mengxiangdemen/item/6480cf4af356fed6c1a59284