Skip to Content

Hello Folks,

This document describes the code for getting the opening stock and the closing stock for a particular material ( MATNR ), plant ( WERKS ) and storage location ( LGORT ). In most of my projects i have seen Stock Report as a common requirement, so after lots of discussion i have created this subroutine for getting the stock details for a particular material, plant and storage location combination.

Here is the snipped code or say subroutine for making that

Use this as a input

PERFORM sub_get_open_close_stock USING wa_output-matnr
                                            wa_output-werks
                                            wa_output-lgort
                                     CHANGING wa_output-op_stk_total
                                              wa_output-cls_stk_tot
                                              wa_output-issue
                                              wa_output-receipt.

*&———————————————————————*
*&      Form  SUB_GET_OPEN_CLOSE_STOCK
*&———————————————————————*
*       text
*———————————————————————-*
*      –>P_WA_OUTPUT_MATNR  text
*      –>P_WA_OUTPUT_WERKS  text
*      –>P_WA_OUTPUT_LGORT  text
*      <–P_WA_OUTPUT_OP_STK_TOTAL  text
*      <–P_WA_OUTPUT_CLS_STK_TOT  text
*———————————————————————-*
FORM sub_get_open_close_stock  USING    p_wa_output_matnr
                                         p_wa_output_werks
                                         p_wa_output_lgort
                                CHANGING p_wa_output_op_stk_total
                                         p_wa_output_cls_stk_tot
                                         p_issue
                                         p_receipt.

   DATA : it_mardv TYPE STANDARD TABLE OF mard.
   DATA : it_mskav TYPE STANDARD TABLE OF mska.
   DATA : it_wb2   TYPE STANDARD TABLE OF wb2_v_mkpf_mseg2.
   DATA : it_wb3   TYPE STANDARD TABLE OF wb2_v_mkpf_mseg2.
   DATA : wa_wb2   TYPE wb2_v_mkpf_mseg2.
   DATA : wa_wb3   TYPE wb2_v_mkpf_mseg2.
   DATA : wa_mardv TYPE mard.
   DATA : wa_mskav TYPE mska.
   DATA : w_mard   TYPE labst.
   DATA : w_mska   TYPE labst.
   DATA : w_mardv  TYPE labst.
   DATA : w_mskav  TYPE labst.
   DATA : w_mard_mska  TYPE labst.
   DATA : w_menge1 TYPE menge_d.
   DATA : w_menge2 TYPE menge_d.
   DATA : w_menge3 TYPE menge_d.
   DATA : l_date_low TYPE sy-datum.

   SELECT * FROM mard INTO TABLE it_mardv
                      WHERE matnr = p_wa_output_matnr
                        AND werks = p_wa_output_werks
                        AND lgort = p_wa_output_lgort.
   IF sy-subrc = 0.
     LOOP AT it_mardv INTO wa_mardv.
       w_mard = wa_mardv-labst + wa_mardv-insme + wa_mardv-einme + wa_mardv-speme +
                 wa_mardv-retme + wa_mardv-vmlab +  wa_mardv-vmins +  wa_mardv-vmein +
                 wa_mardv-vmspe + wa_mardv-vmret.
       w_mardv = w_mardv + w_mard.
       CLEAR : w_mard.
     ENDLOOP.
   ENDIF.

   SELECT * FROM mska INTO TABLE it_mskav
                      WHERE matnr = p_wa_output_matnr
                        AND werks = p_wa_output_werks
                        AND lgort = p_wa_output_lgort.
   IF sy-subrc = 0.
     LOOP AT it_mskav INTO wa_mskav.
       w_mska = wa_mskav-kalab + wa_mskav-kains + wa_mskav-kaspe + wa_mskav-kavla + wa_mskav-kavin + wa_mskav-kavsp.
       w_mskav = w_mskav + w_mska.
       CLEAR : w_mska.
     ENDLOOP.
   ENDIF.

   w_mard_mska = w_mardv + w_mskav. ” X
* If User not entered the Date Range
   IF s_date-high IS INITIAL.
     s_date-high = s_date-low.
   ENDIF.

   l_date_low = s_date-high + 1.

   SELECT * FROM wb2_v_mkpf_mseg2 INTO TABLE it_wb2
            WHERE matnr_i = p_wa_output_matnr
              AND werks_i = p_wa_output_werks
              AND lgort_i = p_wa_output_lgort
              AND budat BETWEEN l_date_low AND sy-datum.
   IF sy-subrc = 0.
     LOOP AT it_wb2 INTO wa_wb2.
       IF wa_wb2-shkzg_i = ‘S’.
         w_menge1 = w_menge1 + wa_wb2-menge_i.
       ELSEIF wa_wb2-shkzg_i = ‘H’.
         w_menge2 = w_menge2 + wa_wb2-menge_i.
       ENDIF.
     ENDLOOP.
     w_menge3 = w_menge1 – w_menge2.  ” Y
     CLEAR : w_menge1, w_menge2.
   ENDIF.

*  Closing Stock Z = X – Y.

   p_wa_output_cls_stk_tot = w_mard_mska – w_menge3.
   CLEAR : w_menge3.

* Opening Stock Logic
   SELECT * FROM wb2_v_mkpf_mseg2 INTO TABLE it_wb3
          WHERE matnr_i = p_wa_output_matnr
            AND werks_i = p_wa_output_werks
            AND lgort_i = p_wa_output_lgort
            AND budat BETWEEN s_date-low AND s_date-high.
   IF sy-subrc = 0.
     LOOP AT it_wb3 INTO wa_wb3.
       IF wa_wb3-shkzg_i = ‘S’.
         w_menge1 = w_menge1 + wa_wb3-menge_i.
       ELSEIF wa_wb3-shkzg_i = ‘H’.
         w_menge2 = w_menge2 + wa_wb3-menge_i.
       ENDIF.
     ENDLOOP.
     w_menge3  = w_menge1 – w_menge2.  ” A
     p_issue   = w_menge2. ” Issue
     p_receipt = w_menge1. ” Receipt
     CLEAR : w_menge1, w_menge2.
   ENDIF.
* Calculation for Opening Stock Z – A = B
   p_wa_output_op_stk_total = p_wa_output_cls_stk_tot – w_menge3.
   CLEAR : w_menge3.

ENDFORM.                    ” SUB_GET_OPEN_CLOSE_STOCK

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. John K

    Hi,

    It’s very helpful. But I have one Doubt.

    For Closing balance 


    Closing Stock Z = X – Y.

    It is giving wrong data. But Z = X, is giving me a correct data.

    For Opening Balance

    * Calculation for Opening Stock Z – A = B


    B = Z – A is working fine. Is it correct ? else If I followed your logic by comparing MB5B it is not matching with closing & opening stock. Please suggest me.

    Regards,

    John.

    (0) 

Leave a Reply