如何在 SAP 中计算物料在某期的库存数量和金额?

SAP 提供如下表存储关于存货 (stock) 的相关数据:

从 4.5 版开始,库存表(比如 MBEW) 和所谓的历史数据表 (比如 MBEWH) 就是分开的,这么设计是为了加速月结的操作,但作为用户来说,如果想查看某一期间的物料库存数量和库存金额,却没有一个直观的方法。

历史表的数据更新逻辑

对于历史表来说,每个期间最多只有一条记录 (one entry),并且不是每个历史期间都有记录,只有在某个期间有库存和评估数据变化的时候,更新上一期的存储记录。

举一个例子。有一个物料 A, 存在如下数据移动:

不难计算出各期间 A 物料的库存数量:

  1. 第 01 期入库 10 pc,那么 SAP 对数据存储的大致模型如下(假设库存为未限制库存):


说明:数据计入 MARD 表,数量为 10,当前期间 (LFMON) 为 01。

  1. 第 02 期 入库 5 pc,此时 MARD 库存表变为 15,LFMON 改变为 02, 同时触发在 MARDH 表中创建一笔记录,01 期的库存数为 10。大致模型如下:

  1. 第 02 期入库 2 pc,此时 MARD 表库存数变为 17,LFMON 不变,因为 MARDH 表 01 期已经有数据,所以也不会更新。大致模型如下:

  2. 第 03 期和第 04 期没有出入库,第 05 期出库 4 pcs, 库存变为 13。此时,MARD 表库存数变为 13, LFMON 变为 05;同时 MARDH 表增加一笔记录,期间为 04,库存数为 17。

库存计算的逻辑

下面,我们来看一看如何计算各期的库存数量:

  • 05 期库存数量:从 MARD 表读取,数量为 13
  • 04 期库存数量,从 MARDH 表读取,数量为 17
  • 03 期库存数量,MARDH 表 03 期为空,读取 MARDH 表 04 期的数量即可
  • 02 期库存数量,同 03 期原则相同,读取到 MARDH 表 04 期的数量
  • 01 期库存数量,从 MARDH 表读取

这样,我们可以总结出库存数量计算的算法:

  • 将 MARDH 表和 MARD 表数据合并在一起,并且按期间进行排序(假设按升序排序并且合并在一起的内表为 MAT_HISTORY)
  • 根据期间条件,在 MAT_HISTORY 中查找记录,如果找到,则库存为找到的那一笔记录,否则,查找下一期间的记录,直到找到为止。

以下代码说明了上述算法:

DATA: t_mardh TYPE TABLE OF mardh WITH HEADER LINE. 
DATA: s_mardh TYPE mardh.

FORM get_material_stock_history.
  SELECT werks    " plant
         matnr    " material number
         lgort    " storage location
         lfgja    " fiscal year
         lfmon    " month
         labst    " unrestricted stock
    FROM mard
    INTO CORRESPONDING FIELDS OF TABLE t_mardh
    WHERE matnr IN s_matnr
    AND   werks IN s_werks    " plant
    AND   lgort IN s_lgort.   "
 
  SELECT werks    " plant
         matnr    " material number
         lgort    " storage location
         lfgja    " fiscal year
         lfmon    " month
         labst    " unrestricted stock
    FROM mardh
    APPENDING CORRESPONDING FIELDS OF TABLE t_mardh
    WHERE matnr IN s_matnr
    AND   werks IN s_werks    " plant
    AND   lgort IN s_lgort   " storage location
    AND   ( lfgja > p_lfgja  OR ( lfgja = p_lfgja AND lfmon >= p_lfmon ) ) .
 
* Add material and storage location to t_mat_lgort
  LOOP AT t_mardh.
    CLEAR t_mat_lgort.
    MOVE-CORRESPONDING t_mardh TO t_mat_lgort.
    APPEND t_mat_lgort.
  ENDLOOP.
 
  SORT t_mat_lgort BY matnr werks lgort.
  DELETE ADJACENT DUPLICATES FROM t_mat_lgort.
ENDFORM.

FORM get_next_period USING a_lfgja a_lfmon.
  ADD 1 TO a_lfmon.
 
  IF a_lfmon = '13'.
    ADD 1 TO a_lfgja.
    a_lfmon = '01'.
  ENDIF.
ENDFORM.

FORM get_material_stock
  USING a_matnr a_werks a_lgort a_lfgja a_lfmon.
 
  DATA: max_year  LIKE mardh-lfgja,
        max_month LIKE mardh-lfmon,
 
        current_year LIKE mardh-lfgja,
        current_month LIKE mardh-lfmon,
 
        current_period(6) TYPE c,
        max_period(6)     TYPE c.
 
  current_year = a_lfgja.
  current_month = a_lfmon.
 
* First get latest fiscal year and period, which comes from MARD table
  READ TABLE t_mardh
    WITH KEY matnr = a_matnr  " material number
             werks = a_werks  " plant
             lgort = a_lgort. " storage location
 
* Record the year and month in MARD table
  IF sy-subrc IS INITIAL.
    max_year = t_mardh-lfgja.
    max_month = t_mardh-lfmon.
 
    MOVE-CORRESPONDING t_mardh TO s_mardh.
    s_mardh-lfgja = a_lfgja. " also change the year
    s_mardh-lfmon = a_lfmon. " also change the month
  ENDIF.
 
* Then find record according to fiscal year/month
* Make sure t_mardh is sorted by year and month DESCENDING
  READ TABLE t_mardh
    WITH KEY  matnr = a_matnr         " material number
              werks = a_werks         " plant
              lgort = a_lgort         " storage location
              lfgja = current_year    " fiscal year
              lfmon = current_month . " month
 
* If found, using this record
  IF sy-subrc IS INITIAL.
    MOVE-CORRESPONDING t_mardh TO s_mardh.
    s_mardh-lfgja = a_lfgja.
    s_mardh-lfmon = a_lfmon.
  ELSE. " not found, find in next period until entry is found
    CONCATENATE max_year max_month INTO max_period.
    CONCATENATE current_year current_month INTO current_period.
 
    WHILE current_period <= max_period.
      PERFORM get_next_period
        USING current_year  current_month.
 
      CONCATENATE current_year current_month INTO current_period.
 
      READ TABLE t_mardh
        WITH KEY  matnr = a_matnr         " material number
                  werks = a_werks         " plant
                  lgort = a_lgort         " storage location
                  lfgja = current_year    " fiscal year
                  lfmon = current_month . " month
 
      IF sy-subrc IS INITIAL.
        MOVE-CORRESPONDING t_mardh TO s_mardh.
        s_mardh-lfgja = a_lfgja.
        s_mardh-lfmon = a_lfmon.
        EXIT.
      ENDIF.
    ENDWHILE.
  ENDIF.
ENDFORM.  

参考文档

  • 4
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值