Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
venkateswaran_k
Active Contributor

Business Requirement

There is always a situation that Inventory Management wants to look back the stock of any given material for a given date (Past).  There are couple of Reports MB5B, MB52 which can shows the stock position.  However, there is no direct way/program that gives Stock position as on given date.

How We implement

We encapsulated the Transaction MB5B inside a Custom Function. Execute the function by passing the material code and date as input parameters.  In turn they are supplied to this Transaction code appropriately. The output was retrieved from the spool and parsed and it returns the value as a closing stock as on that date.

Technical Details

Definitions

T-code                  : SE37

Function Name     : ZMM_GET_MATNR_STOCK_BY_DATE.

Source Code - Data Declarations

types : begin of ty_list,
               text(256) type c,
              end of ty_list.

  types: begin of it_stock,
              blank type string,
              plant type werks,
              matnr type matnr,
              fdate type string,
              tdate type string,
              obal  type string,
              rcpt  type string,
              issu  type string,
              cbal  type string,
              unit  type string,
         END OF it_stock.

  DATA : IS_RSPAR type RSPARAMS,
              IT_RSPAR type table of RSPARAMS,
              IT_LIST type standard table of ABAPLIST,
              IT_LIST1 type standard table of ty_list.

  DATA:  it_stock1 type standard table of it_stock,
             wa_stock1 type it_stock,
             wa_list1  type ty_list.

  DATA:  stock type p decimals 2,
              stockc type c length 30.

Pass Parameters

clear : IS_RSPAR.
  IS_RSPAR-SELNAME = 'DATUM'.
  IS_RSPAR-KIND = 'S'.
  IS_RSPAR-LOW = I_DATE.
  IS_RSPAR-HIGH = I_DATE.
  IS_RSPAR-SIGN = 'I'.
  IS_RSPAR-OPTION = 'BT'.
  append IS_RSPAR to IT_RSPAR.

  IS_RSPAR-SELNAME = 'MATNR'.
  IS_RSPAR-KIND = 'S'.
  IS_RSPAR-LOW = I_MATNR.
  IS_RSPAR-HIGH = I_MATNR.
  IS_RSPAR-SIGN = 'I'.
  IS_RSPAR-OPTION = 'BT'.
  append IS_RSPAR to IT_RSPAR.

Call the Encapsulated Transaction MB5B

  submit RM07MLBD with selection-table IT_RSPAR exporting list to memory and return.

Read from Spool

  CALL FUNCTION 'LIST_FROM_MEMORY'
    TABLES
      listobject = it_list
    EXCEPTIONS
      not_found  = 1
      others     = 2.

  CALL FUNCTION 'LIST_TO_ASCI'
    TABLES
      listasci   = it_list1
      listobject = it_list.

Parse the output to get value  (parse the value and store it in internal table)

  loop at it_list1 into wa_list1.
    if sy-tabix = 4 .
      SPLIT wa_list1 AT '|' INTO wa_stock1-plant  wa_stock1-blank wa_stock1-matnr wa_stock1-fdate wa_stock1-tdate wa_stock1-obal
                                 wa_stock1-rcpt wa_stock1-issu wa_stock1-cbal wa_stock1-unit.
      APPEND wa_stock1 to it_stock1.
    endif.
  endloop.

  read table it_stock1 into wa_stock1 index 1.
  stockc = wa_stock1-cbal.

REPLACE ALL OCCURRENCES OF ',' in stockc WITH SPACE.
  stock = stockc.

E_STOCK = stock.

Now the E_STOCK is the value that will be returned by the function.

You can use this function to get the stock as on date of any given material.

Regards,

Venkat

2 Comments