含仓位之Table:MARD / MARDH
MARD里记载的是当前库存的数量,是实时变化的,但是期间并不一定是当月。比如:当前是2015年11月,料号A在2015年10月初至今都没有库存异动,那么现在看到MARD里料号A的期间数还是2015年09月,而非2015年11月。
当料号A发生异动时,系统将实时更新MARD数据。在更新之前,会检查此异动的过账期间和MARD里对应记录的期间是否一致。如果不是,则会copy表MARD里的库存数量到MARDH,存成上月的期末库存数据保存起来,然后把MARD中的期间改成当前期间,并更新数量数据。如果期间是一致的,则不作copy动作,只更新MARD数量。
MARD – Storage LocationData for Material
MARDH – Material Master Storage Location Segment: History
不含仓位之Table: MBEW / MBEWH
MBEW和MBEWH的原理和MARD/MARDH的原理是一样的,只是没有了仓位这个层级。
MBEWH – Material ValuationHistory(存储物料历史价格)
MBEW -Material Valuation (存储当前物料价格)
特殊库存之Table:
MKOL -Special stock: K (Special Stocks from Vendor)
MSLB -Special stock: O (Special Stocks with Vendor)
MSKU -Special Stocks with Customer V/W (V:Ret. pkg w.customer,W:Consignment (cust.))
MSPR -Project Stock: Q (Project stock)
MSSQ – Project StockTotal
MSKA -Sales Order Stock E (Orders on hand)
MSSA – Total CustomerOrders on Hand
MCHA – Batches
MCHB – Batch Stocks
EBEW – Sales Order Stock Valuation
EBEWH – Valuation of Sales Order Stock: History
理解了上面的原理之后,我们就可以快速取出期初库存和期末库存,甚至指定日期的库存了。(可以参考上一篇文章《库存管理报表(含无价厂)》)
CLEAR: gt_mardh.
SELECT matnr werks lgort lfgja lfmon
labst umlme insme einme speme retme “lvorm
INTO CORRESPONDING FIELDS OF TABLE gt_mardh
FROM mard
WHERE matnr IN s_matnr
AND werks IN gr_werks
AND lgort IN s_bptnr. “s_lgort.
SELECT matnr werks lgort lfgja lfmon
labst umlme insme einme speme retme “lvorm
APPENDING CORRESPONDING FIELDS OF TABLE gt_mardh
FROM mardh
WHERE matnr IN s_matnr
AND werks IN gr_werks
AND lgort IN s_bptnr “s_lgort.
AND ( lfgja > g_lfgja OR lfgja = g_lfgja AND lfmon >= g_lfmon ).
“删除重复行,仅保留最近一条记录
SORT gt_mardh BY matnr werks lgort lfgja lfmon.
DELETE ADJACENT DUPLICATES FROM gt_mardh COMPARING matnr werks lgort.
用上面这段代码就可以轻松快速读取到指定月份的期初期末库存了。如果还要具体到月中某一天,则就要涉及到MSEG和MKPF这两个大表了。不过其实也不用担心,其实我们只要抓取期初到该指定日期的异动数据就可以了,因为通过上面的代码已经读取到该月的期初库存,再减掉这段时间的异动就OK了。
CLEAR: gt_field.
PERFORM get_mseg_fields
TABLES gt_field “<— Field names used
USING gw_mseg. ” —> in structure here
CLEAR: gt_mseg.
SELECT (gt_field) INTO CORRESPONDING FIELDS OF TABLE gt_mseg
FROM mkpf
INNER JOIN mseg ON mkpf~mandt = mseg~mandt
AND mkpf~mblnr = mseg~mblnr
AND mkpf~mjahr = mseg~mjahr
WHERE mseg~werks IN gr_werks
AND mseg~matnr IN s_matnr
“AND mseg~matnr <> space
AND ( mkpf~budat >= l_sdate AND mkpf~budat <= l_edate )
AND mseg~sobkz = space “IN s_sobkz “非特殊库存部分
AND mseg~menge <> 0
%_HINTS
DB2 ‘&SUBSTITUTE VALUES&’
ORACLE ‘&SUBSTITUTE VALUES&’.
CLEAR: gt_mseg_sum.
LOOP AT gt_mseg INTO gw_mseg.
CLEAR: gw_mseg_sum.
IF gw_mseg-shkzg = ‘H’.
gw_mseg-menge = – gw_mseg-menge.
ENDIF.
MOVE-CORRESPONDING gw_mseg TO gw_mseg_sum.
COLLECT gw_mseg_sum INTO gt_mseg_sum.
ENDLOOP.
SORT gt_mseg_sum BY matnr werks lgort.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20224.html