点击此处---> 群内免费提供SAP练习系统(在群公告中)
加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)
SAP 提供如下表存储关于存货 (stock) 的相关数据:
存货类型 | 标准表 | 历史数据表 | FM |
---|---|---|---|
Material Valuation | MBEW | MBEWH | MBEW_EXTEND |
Storage Location Data for Material | MARD | MARDH | MARD_EXTEND |
Batch Stocks | MCHB | MCHBH | MCHB_EXTEND |
Project Stock Valuation | QBEW | QBEWH | QBEW_EXTEND |
Special Stocks with Customer | MSKU | MSKUH | MSKU_EXTEND |
Special Stocks with Vendor | MSLB | MSLBH | MSLB_EXTEND |
Sales Order Stock Valuation | EBEW | EBEWH | EBEW_EXTEND |
Special Stocks from Vendor | MKOL | MKOLH | MKOL_EXTEND |
Project Stock | MSPR | MSPRH | MSPR_EXTEND |
Material Master Plant data | MARC | MARCH | MARC_EXTEND |
… |
从 4.5 版开始,库存表(比如 MBEW) 和所谓的历史数据表 (比如 MBEWH) 就是分开的,这么设计是为了加速月结的操作,但作为用户来说,如果想查看某一期间的物料库存数量和库存金额,却没有一个直观的方法。
历史表的数据更新逻辑
对于历史表来说,每个期间最多只有一条记录 (one entry),并且不是每个历史期间都有记录,只有在某个期间有库存和评估数据变化的时候,才有存储的记录。
举一个例子。有一个物料 A, 存在如下数据出入库记录:
第 01 期入库 10 pc,那么 SAP 对数据存储的大致模型如下:
说明:数据计入 MARD 表,数量为 10,LFMON (当前期间) 标记为 01。
第 02 期 入库 5 pc,此时 MARD 库存表变为 15,LFMON 改变为 02, 同时触发在 MARDH 表中创建一条记录,01 期的库存数为 10。大致模型如下:
第 02 期入库 2 pc,此时 MARD 表库存数变为 17,LFMON 不变,因为 MARDH 表 01 期已经有数据,所以也不会更新。大致模型如下:
第 03 期和第 04 期没有变化,第 05 期出库 3 pcs, 库存变为 14。此时,MARD 表库存数变为 14, 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.
参考文档
作者:Stone0823
链接:https://www.jianshu.com/p/2d880f91c5b1
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。