SAP ABAP MARD和MARDH计算逻辑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/champaignwolf/article/details/85272396

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很爱你

由于其物料性质与常用料不同,在计算其期末库存时跟常用料的计算方法有些许差异。

猜你喜欢

转载自blog.csdn.net/champaignwolf/article/details/85272396