Home > ABAP > Material beginning stock

Material beginning stock

March 19th, 2009

If you go through MB5B, you can see beginning stock, receiving / issuing, and ending stock of a specified material in specified plant / storage location. Now, if you need to get that beginning stock of a specified date, sometimes it becomes quite tricky.  I don’t know if SAP provides any function module to read that beginning stock.. I made several trials but never got a correct result that runs correctly for every material.  So, what I did was turning ST05 and run MB5B to see what it really does in the program. It turned out that the program actually “traceback” the stock from the current condition! It doesn’t read any history table (MARDH or MBEWH). I thought that method will take me forever to get the result, but I was wrong! It turned out to be the fastest way compared with all the other methods I tried by reading the history tables!

Here’s my code to get that beginning stock of a material:

First, get the current stock:

***plant/storage location stock:

SELECT SINGLE MAX( LFGJA ) INTO D_MAX(4)
FROM MARD WHERE MATNR = _MATNR
AND   WERKS = _WERKS
AND   LGORT = T001L-LGORT.

SELECT SINGLE MAX( LFMON ) INTO D_MAX+4(2)
FROM MARD WHERE MATNR = _MATNR
AND   WERKS = _WERKS
AND   LGORT = T001L-LGORT.

select * from mard
where matnr = _matnr
and   werks = _werks
and   lgort = t001L-lgort
AND   LFGJA GE D_MAX(4)
AND   LFMON GE D_MAX+4(2).

add: mard-LABST to _begmard,
mard-UMLME to _begmard,
mard-INSME to _begmard,
mard-EINME to _begmard,
mard-SPEME to _begmard,
mard-RETME to _begmard.

ENDSELECT.

**********For valuated stock

SELECT SINGLE MAX( LFGJA ) INTO D_MAX(4)
FROM MBEW WHERE MATNR = _MATNR
AND   BWKEY = _WERKS.

SELECT SINGLE MAX( LFMON ) INTO D_MAX+4(2)
FROM MBEW WHERE MATNR = _MATNR
AND   BWKEY = _WERKS.

SELECT SINGLE *  FROM MBEW
WHERE MATNR = _MATNR
AND   BWKEY = _WERKS
AND   LFGJA EQ D_MAX(4)
AND   LFMON EQ D_MAX+4(2).

_BEGVALUE = MBEW-SALK3.

ENDSELECT.

******Now let’s find the beginning stock from material’s goods movements from a key date until now

select f~mblnr f~mjahr
SHKZG
MENGE
MEINS
into corresponding fields of MSEG
from mkpf as f inner join mseg as e
on ( f~mandt = e~mandt and
f~mblnr = e~mblnr and
f~mjahr = e~mjahr )
where f~mandt = sy-mandt
and   matnr = _matnr
and   werks = _werks
and   f~mjahr ge _keydate(4)
and   f~budat >= _keydate
and   f~budat <= sy-datum
and   kzstr NE ”.

IF MSEG-shkzg EQ ‘H’.
ADD MSEG-MENGE TO _issstock.
ADD MSEG-DMBTR TO _issvalue.
ELSE.
ADD MSEG-MENGE TO _recstock.
ADD MSEG-DMBTR TO _issvalue.
ENDIF.

ENDSELECT.

_BEGMARD = _BEGMARD + _ISSSTOCK – _RECSTOCK.

_BEGVALUE = _BEGVALUE + _ISSVALUE – _RECVALUE.
You’ll get your beginning stock and value for that specified material in a plant.

Enjoy!

vera ABAP

  1. No comments yet.
  1. No trackbacks yet.