赞
踩
SAP 提供如下表存储关于存货 (stock) 的相关数据:
从 4.5 版开始,库存表(比如 MBEW) 和所谓的历史数据表 (比如 MBEWH) 就是分开的,这么设计是为了加速月结的操作,但作为用户来说,如果想查看某一期间的物料库存数量和库存金额,却没有一个直观的方法。
对于历史表来说,每个期间最多只有一条记录 (one entry),并且不是每个历史期间都有记录,只有在某个期间有库存和评估数据变化的时候,更新上一期的存储记录。
举一个例子。有一个物料 A, 存在如下数据移动:
不难计算出各期间 A 物料的库存数量:
说明:数据计入 MARD 表,数量为 10,当前期间 (LFMON) 为 01。
第 02 期入库 2 pc,此时 MARD 表库存数变为 17,LFMON 不变,因为 MARDH 表 01 期已经有数据,所以也不会更新。大致模型如下:
第 03 期和第 04 期没有出入库,第 05 期出库 4 pcs, 库存变为 13。此时,MARD 表库存数变为 13, LFMON 变为 05;同时 MARDH 表增加一笔记录,期间为 04,库存数为 17。
下面,我们来看一看如何计算各期的库存数量:
这样,我们可以总结出库存数量计算的算法:
以下代码说明了上述算法:
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.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。