Skip to Content
Author's profile photo Sijin Chandran

Getting Opening and Closing quantities of Stock with accuracy of MB5B

Dear All ,

Its a very common requirement in almost all scenarios wherever SAP MM is there , that we need to calculate or get the Opening and Closing quantities of the Stock of Material(s) in Custom reports ( for eg. Stock Reconciliation Report , Stock Movement Report etc ) , Forms …. for a particular date.

And nothing can provide us with accuracy of MB5B ( Standard Report for Stock on Posting Date ) in this case.

Although there are many approaches for this like , can be calculated by doing operations over all the Material Documents generated for a Material on a particular date , field  MARD-LABST provides with the opening of stock on particular date etc. But getting these detail with the accuracy of MB5B is more desirable , accurate and informative as well.

While viewing Standard Report MB5B’s code (RM07MLBD) you can see an internal table g_t_totals_flat , this is the internal table which is displayed as the output of MB5B for a particular selections made.

So its very obvious that it contains all the necessary data such as Opening Stock Qty , Closing Stock Qty , Plant , Valution Class (Same as Plant ) , Dates ,Unit Of Material etc.

So am using EXPORT IMPORT between MEMORY ID technique to get this data wherever required.

Steps for accomplishing this task are as follows :

1. Exporting the contents of g_t_totals_flat to a particular MEMORY ID from MB5B.

  

For this purpose you will need to Implicitly enhance the standard code of MB5B (RM07MLBD).

For learning how to IMPLICIT ENHANCEMENT you can refer this thread http://wiki.sdn.sap.com/wiki/display/ABAP/How+To+Do+Implicit+Enhancement

Write your code from line number 1995 ( after the loop inside which append g_s_totals_flat to g_t_totals_flat command is executed )

For eg :

Capture 2.JPG

NOTE : Its recommended to write a LEAVE or LEAVE PROGRAM statement followed by EXPORT statement to avoid unwanted execution of the called program after getting the EXPORT done ( Can be written inside SY-SUBRC check ).

Now whenever MB5B will be executed g_t_totals_flat data will be copied to IT_TOTAL and EXPORTED to this MEMORY ID.

2. Importing the contents of IT_TOTAL from a particular MEMORY ID through a Custom Report ( Code ).

For this purpose you will need to use SUBMIT command and thereafter use IMPORT command.

For learning about SUBMIT in detail you can refer this thread http://help.sap.com/abapdocu_70/en/ABAPSUBMIT_LIST_OPTIONS.htm

Write your code like this example :

Capture 3.JPG

Here SUBMIT command with EXPORTING LIST TO MEMORY AND RETURN addition will call MB5B in background and thereby  g_t_totals_flat will be copied to IT_TOTAL and Exported to MEMORY ID.

And after this SUBMIT command we can IMPORT IT_TOTAL from MEMORY ID.

WITH addition in SUBMIT command can be used to provide the various desired selections which we provide to MB5B.

Eg : we set LGBST EQ ‘X’ in the above code to get the below selection.

Capture 4.JPG

Likewise you can add more as per your requirement.

Fields ANFMENGE and ENDMENGE of IT_TOTAL will give you the OPENING and CLOSING quantities repectively.

Rest of the material movement quantities can be calculated and segregated By fetching data from MSEG and processing them using different MOVEMENT TYPES.

I think that performance issue will not be prevailing that much by calling MB5B using SUBMIT , reason being its a very quickly executed report accessing mainly MKPF and MSEG and the output is also using a Basic List.

Hope this will provide with solutions to many.

Thanks and Regards ,

Sijin.

Assigned Tags

      30 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Sijin,

      Great job!

      Thank you so much for putting such a valuable article on MB5B (RM07MLBD). It is really Excellent.

      I just want put a small comment here. I achieved the same in one of my earlier requirement w/o doing the enhancement. I was able to get the Opening Balance (ANFMENGE) and Closing Balance (ENDMENGE) by leveraging two function modules 'LIST_FROM_MEMORY" and 'LIST_TO_ASCII'. The last FM converts the memory line in text lines and then splitting the text line into fields like Plant, Material Number, Start Date, End Date, Opening Balance, Total Receipt, Total Issue and Closing balance. These fields are separated by Pipe '|'.

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Hi RITWIK ,

      Thanks.

      Actually for accomplishing my task I used LIST_FROM_MEMORY and LIST_TO_ASCII but I was not able to copy it to an internal table in a proper format.

      That's why I went for this option.

      Author's profile photo Former Member
      Former Member

      Dear Concern,

      Thanks For Prompt Reply ,

      Above Logic for Storage location / Batch Stock but i "Required for Valuated Stock T Code MB5B

      Please Do NeedFull

      Regards

      Suresh

      Author's profile photo Former Member
      Former Member

      Hi Sijin ,

      Great job!

      Am sure this will be helpful for many in SCN Forums.

      As I have seen many related threads asking for FMs and other codes for the calculating opening and closing of a material.

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Hello Prasanna K ,

      Thanks πŸ™‚

      Author's profile photo Former Member
      Former Member

      Dear Concern,

      Thanks For Prompt Reply ,

      Above Logic for Storage location / Batch Stock but i "Required for Valuated Stock T Code MB5B

      Please Do NeedFull

      Regards

      Suresh

      Author's profile photo Former Member
      Former Member

      Dear Sijin

      I think this report will called stock reconciliation because its a great way to run both MB5B and MB51 with column wise but the concept you used with MB5B will give  pin point accuracy and it will be very easy for the management to take decisions.

      In the end I would like to know can we go little deeper I mean storage location wise and Batch Wise.

      Thanking you!

      Ashish Mishra

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Hi Ashish ,

      Yes this can be used for preparing stock reconciliation report.

      Yeah we can get batchwise and storage location wise classification as well.

      You just need to call MB5B (using SUBMIT ) accordingly and likewise loop with MSEG inside the ZREPORT also.

      And it will give you the desired result.

      Author's profile photo Former Member
      Former Member

      Hi Sijin,

      I am facing a problem in Memory ID. I have followed the steps what you have mention in the document. In RM07MLBD program i m getting the values in the internal table and exporting the same to memory id but when i am going for custom program the memory id is not importing any values. I have written the enhancement point on form alv_flat_list_sums_only as i m getting values.Please have a look on my code below and suggest me on this.

      Custom Program:

      SUBMIT rm07mlbd WITH SELECTION-TABLE k_rsparams

                                WITH FREE SELECTIONS texpr

                                AND RETURN.

         IMPORT it_total_item FROM MEMORY ID 'ZMB5B_ITEM'.

      **RM07MLBD Enhancement Point

      DATA: IT_TOTAL TYPE STAB_MSEG_LEAN.

        IT_TOTAL[] = G_T_MSEG_LEAN.

        FREE MEMORY ID 'ZMB5B_ITEM'  .

      * EXPORT IT_TOTAL TO MEMORY id 'ZMB5B_ITEM'  .

        EXPORT IT_TOTAL TO MEMORY ID 'ZMB5B_ITEM'.

      thanks and regards,

      Zakir Appas A

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Hi Zakir ,

      Even I had also faced this problem .

      I did the complete execution of my custom zprogram using debugger and it started working fine.

      Try this it may work out , if data is getting exported properly and the problem is with import statement.

      Before that also run MB5B .

      Author's profile photo Former Member
      Former Member

      Hi Sijin,

      Thanks for your response. I have debugged my custom program and i m not able to sort the issue. First i m running MB5B program and exporting the values in MB5B through the enhancement point and values are properly populating there in enhancement, when i come back to my custom program after the enhancement the values are not importing from memory id.

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Zakir ,

      Check whether your program is able to call MB5B properly or not.

      Keep a breakpoint at your code in MB5B and after that run your zprogram and check.

      Author's profile photo Former Member
      Former Member

      Sijin,

      Ya my custom program is calling MB5B correctly .I have kept break point in MB5B and checked , its exporting the value properly to the memory id, after execution of MB5B in my custom program import is not getting the values. 

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Zakir ,

      EXPORT IT_TOTAL TO MEMORY ID 'ZMB5B_ITEM'.

      IMPORT it_total_item FROM MEMORY ID 'ZMB5B_ITEM'.

      Try with giving both names for internal same ,

      i.e

      EXPORT IT_TOTAL TO MEMORY ID 'ZMB5B_ITEM'.


      IMPORT IT_TOTAL FROM MEMORY ID 'ZMB5B_ITEM'.


      Author's profile photo Former Member
      Former Member

      Sijin,

      Thanks for your support. We have changed the internal table name and found it is working fine.

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Welcome Zakir πŸ™‚

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      It is as simple as that !

      While calling the submit ,

      SUBMIT RM07MLBD WITH MATNR  IN SO_MATNR                     " Material Number

                          WITH WERKS  IN SO_WERKS                     " Plant

                          WITH MTART  IN SO_MTART                     " Material type

      *                   WITH LGORT  IN SO_LGORT                     " Storage Location

                          WITH DATUM  IN SO_BUDAT                     " Selection Date

                          WITH LGBST  EQ 'X'                          " Storage Loc/ Batch Stock

                          WITH BWBST  EQ ' '                          " Valuated Stock

                          WITH SBBST  EQ ' '                          " Special Stock

                          WITH PA_SUMFL EQ 'X'                        " totals only - non heirarchical representation

                          WITH XCHAR EQ ' '

                          WITH XNOMCHB EQ ' '

      *                   WITH XCHAR EQ XCHAR                         " only matls handled in batches

      *                   WITH XNOMCHB EQ XNOMCHB                     " also batches w/o stock segment

                          EXPORTING LIST TO MEMORY

                          AND RETURN.

      Instead of setting of the value of LGBST  EQ 'X' set BWBST EQ 'X'

      Author's profile photo Former Member
      Former Member

      Actually Sijin i have this same code at line 1962.

      how about that. any Explanation ? 😎

      1.PNG

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Hello Abhishek ,

      When I posted this document I was in my earlier company and in that system that code was at line number 1995.

      Now following your statement when I checked this thing in my current organization I found that this code is at line number 1986.

      The first thing which came in my mind was that this could be due to any implicit enhancement carried out for MB5B code , as it will result in reordering of codes.

      But till now I am unable to find any such enhancements with MB5B in my current organization.

      In short am clueless for this .

      Author's profile photo Former Member
      Former Member

      same here, i checked same prog in my system, there exists no implicit or explicit enhancement.

      Author's profile photo Former Member
      Former Member

      Sijin and Abhishek, compare your support pack levels.

      Either that, or additional SAP note implementations would be the root cause for varying line number.

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Hello Manish ,

      Yeah this a point to be noted ,

      SAP note implementations would be the root cause

      Will check it for sure

      Author's profile photo Former Member
      Former Member

      Hello Sijin,

      Great job.

      Thank you for sharing this.

      Author's profile photo Former Member
      Former Member

      Hello sijin,

      i am getting this dump while importing internal table from memory id

      Runtime Errors :  CONNE_IMPORT_WRONG_OBJECT_TYPE

      Except.              CX_SY_IMPORT_MISMATCH_ERROR

      Author's profile photo Former Member
      Former Member

      free memory id 'zpp_daywise1'

      in this place itself am getting sy-subrc 4... how to solve this

      Author's profile photo Former Member
      Former Member

      Hi guys,

        i have added material type(MTART) in selection screen.Based on the material type have to fetch data from mb5b table.How to do this?

      Author's profile photo Mehmet Ozgur Unal
      Mehmet Ozgur Unal

      Good solution for stock reports. Keep it up !!

      M.Ozgur Unal

      Author's profile photo Ashok Chidipothu
      Ashok Chidipothu

      i have a requirement to create four additional new column

      opening stock value

      closing stock value

      total receipt value

      total issue value

      inΒ mb5b

       

      Author's profile photo Sijin Chandran
      Sijin Chandran
      Blog Post Author

      Hi Ashok,

      Right now I don't have access to any ECC systems.

      Please search further on this forum for your requirement or else raise a new question with ABAP Development tag.

      Thanks,

      Sijin

      Author's profile photo Azat Nuriakhmetov
      Azat Nuriakhmetov

      Sijin,

      Great job, sir, very helpful.

      Thank you very much for sharing your knowledge.