3 Replies Latest reply: Aug 10, 2007 10:18 AM by PRADIP PAWAR RSS

Calculate Opening and Closing Stock

PRADIP PAWAR
Currently Being Moderated

Hi all,

      Plz tell me logic how to calculate Opening Stock and Closing Stock of material of any particular date of month...........

 

Thanks in Advance.

Pradip Pawar

  • Re: Calculate Opening and Closing Stock
    Sneha Pacharne
    Currently Being Moderated

    Hi pradip,

     

    heres the code to calculate stock of previous period(closing stock)

     

    DATA : it_mard LIKE mard OCCURS 0 WITH HEADER LINE.

     

    DATA : BEGIN OF t_mard OCCURS 0 ,

            matnr LIKE mseg-matnr,

            labst LIKE mard-labst,

           END OF t_mard.

    DATA : it_mardh TYPE mardh.

     

    DATA : buper LIKE  t009b-poper,

           gjahr LIKE t009b-bdatj.

    DATA : p_buper LIKE  t009b-poper,

           p_gjahr LIKE t009b-bdatj.

     

    DATA : dt LIKE sy-datum.

     

    *CLOSING STOCK OF PREVIOUS PERIOD

     

          dt = sy-datum.

    *get current period

          CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'

            EXPORTING

              i_date         = dt

              i_periv        = 'M3'    "apr to march 4 spl periods

            IMPORTING

              e_buper        = buper

              e_gjahr        = gjahr

            EXCEPTIONS

              input_false    = 1

              t009_notfound  = 2

              t009b_notfound = 3

              OTHERS         = 4.

     

    *fetch previous period

          CALL FUNCTION 'CKML_F_GET_PREVIOUS_PERIOD'

            EXPORTING

              input_period    = buper

              input_year      = gjahr

              input_periv     = 'M3'

            IMPORTING

              previous_period = p_buper

              previous_year   = p_gjahr.

     

         

          SELECT SINGLE * FROM mardh

             WHERE matnr = p_matnr

            AND werks = p_werks

            AND lfmon = p_buper

            AND lfgja = p_gjahr.

     

          IF sy-subrc = 0.

            <b>CLOSING STOCK</b> = mardh-labst.

          ENDIF.

     

     

    *to calculate current stock-----

     

    SELECT * FROM mard

          INTO CORRESPONDING FIELDS OF TABLE it_mard

          WHERE matnr = it_marc-matnr

            AND werks = p_werks.

          IF NOT it_mard[] IS INITIAL.

            LOOP AT it_mard.

              t_mard-matnr = it_mard-matnr.

              t_mard-labst = it_mard-labst.

              COLLECT t_mard.

            ENDLOOP.

            READ TABLE t_mard INDEX 1.

            <b>CURRENT STOCK</b>= t_mard-labst.

          ENDIF.

     

    *do not forget to reward points if it helps

  • Re: Calculate Opening and Closing Stock
    Kiran Kumar Machavarapu
    Currently Being Moderated

    Hi Pradeep

     

    for a particular month for calculating opening stock is

     

    use this logic

     

            SELECT MBLNR BWART ZEILE MATNR WERKS LGORT SHKZG ERFMG ERFME

           FROM MSEG INTO CORRESPONDING FIELDS OF TABLE IT_QUAN FOR

              ALL ENTRIES IN IT_FINAL WHERE MATNR = IT_FINAL-MATNR.

     

      IF IT_QUAN[] IS NOT INITIAL.

    • GETTING THE MATERIAL DOCUMENTS FOR SPECIFIED PERIOD

          SELECT MBLNR BUDAT FROM MKPF

                     INTO CORRESPONDING FIELDS OF TABLE IT_BUDAT

                     FOR ALL ENTRIES IN IT_QUAN

                     WHERE ( BUDAT GE DATE1 AND BUDAT LT DATE2 )

                     AND MBLNR = IT_QUAN-MBLNR.

     

          SELECT MBLNR BUDAT FROM MKPF

                     INTO CORRESPONDING FIELDS OF TABLE IT_BUDAT1

                     FOR ALL ENTRIES IN IT_QUAN

                     WHERE MBLNR = IT_QUAN-MBLNR

                     AND BUDAT LT DATE1.

        ENDIF.

        CLEAR INDEX.

    • IF THE MATERIAL DOCUMENT NUMBER IS NOT FOUND IN THE SPECIFIED PERIOD

    *REMOVE IT

        IT_QUAN1[] = IT_QUAN[].

     

        LOOP AT IT_QUAN.

          INDEX = SY-TABIX.

          READ TABLE IT_BUDAT WITH KEY MBLNR = IT_QUAN-MBLNR.

          IF SY-SUBRC NE 0.

            DELETE IT_QUAN INDEX INDEX.

          ENDIF.

          CLEAR IT_QUAN.

          CLEAR IT_BUDAT.

        ENDLOOP.

     

     

        SORT IT_QUAN BY MATNR.

     

        LOOP AT IT_FINAL.

          INDEX = SY-TABIX.

     

          LOOP AT IT_QUAN WHERE MATNR = IT_FINAL-MATNR

                            AND WERKS = IT_FINAL-WERKS

                            AND LGORT = IT_FINAL-LGORT.

     

            IF SY-SUBRC = 0.

    • IF IT IS IN CALCULATE QUANTITY

              IF IT_QUAN-SHKZG = 'S'.

    • check the unit if it is gram then devide by 1000.

                IF IT_QUAN-ERFME = 'G'.

                  IT_QUAN-ERFMG = IT_QUAN-ERFMG / 1000.

                ENDIF.

                IN_QTY = IN_QTY + IT_QUAN-ERFMG.

              ENDIF.

    • IF IT IS OUT CALCULATE QUANTITY

              IF IT_QUAN-SHKZG = 'H'.

     

                IF IT_QUAN-ERFME = 'G'.

                  IT_QUAN-ERFMG = IT_QUAN-ERFMG / 1000.

                ENDIF.

                OUT_QTY = OUT_QTY + IT_QUAN-ERFMG.

              ENDIF.

     

            ENDIF.

          ENDLOOP.

     

          IT_FINAL-IN = IN_QTY.

          IT_FINAL-OUT = OUT_QTY.

     

          MODIFY IT_FINAL INDEX INDEX.

          CLEAR: IN_QTY,OUT_QTY.

          CLEAR IT_FINAL.

          CLEAR IT_QUAN.

          CLEAR INDEX.

        ENDLOOP.

     

        CLEAR: IN_QTY,OUT_QTY.

        CLEAR IT_FINAL.

        CLEAR IT_QUAN.

        CLEAR INDEX.

    • FOR OPENING BALANCE

        CLEAR INDEX.

        LOOP AT IT_QUAN1.

          INDEX = SY-TABIX.

          READ TABLE IT_BUDAT1 WITH KEY MBLNR = IT_QUAN1-MBLNR.

          IF SY-SUBRC NE 0.

            DELETE IT_QUAN1 INDEX INDEX.

          ENDIF.

          CLEAR IT_QUAN1.

          CLEAR IT_BUDAT1.

        ENDLOOP.

     

        SORT IT_QUAN1 BY MATNR.

     

        LOOP AT IT_FINAL.

          INDEX = SY-TABIX.

     

          LOOP AT IT_QUAN1 WHERE MATNR = IT_FINAL-MATNR

                            AND WERKS = IT_FINAL-WERKS

                            AND LGORT = IT_FINAL-LGORT.

     

            IF SY-SUBRC = 0.

    • IF IT IS IN CALCULATE QUANTITY

              IF IT_QUAN1-SHKZG = 'S'.

                IF IT_QUAN1-ERFME = 'G'.

                  IT_QUAN1-ERFMG = IT_QUAN1-ERFMG / 1000.

                ENDIF.

                IN_QTY = IN_QTY + IT_QUAN1-ERFMG.

              ENDIF.

    • IF IT IS OUT CALCULATE QUANTITY

              IF IT_QUAN1-SHKZG = 'H'.

                IF IT_QUAN1-ERFME = 'G'.

                  IT_QUAN1-ERFMG = IT_QUAN1-ERFMG / 1000.

                ENDIF.

                OUT_QTY = OUT_QTY + IT_QUAN1-ERFMG.

              ENDIF.

     

            ENDIF.

          ENDLOOP.

          OPEN_QUAN = IN_QTY - OUT_QTY.

          IT_FINAL-OPEN_QUAN = OPEN_QUAN.

          MODIFY IT_FINAL INDEX INDEX.

          CLEAR: IN_QTY,OUT_QTY.

          CLEAR IT_FINAL.

          CLEAR IT_QUAN1.

          CLEAR OPEN_QUAN.

          CLEAR INDEX.

        ENDLOOP.

     

        LOOP AT IT_FINAL.

          IT_FINAL-CLOS_QUAN = IT_FINAL-OPEN_QUAN +

                                       IT_FINAL-IN - IT_FINAL-OUT.

          MODIFY IT_FINAL INDEX SY-TABIX.

          CLEAR IT_FINAL.

        ENDLOOP.

     

     

    reward points to all helpful answers

    kiran.M

Actions