SAP ABAP MARD和MARDH计算逻辑

来源:互联网 发布:剑三怎么走淘宝链接 编辑:程序博客网 时间:2024/05/19 22:49

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,供应商寄售库存

自由库存是空

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