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 https://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 :
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 https://help.sap.com/abapdocu_70/en/ABAPSUBMIT_LIST_OPTIONS.htm
Write your code like this example :
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.
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 ,