Thursday, September 12, 2013

To convert the Unit of Measure to alternate UOM

Skip to end of metadata
Go to start of metadata
Hi Friends,
Below function module code help you to convert the unit of measure of a material quantity to another unit of measure.

FUNCTION Z_CONVERT_TO_ALTERNATE_UOM  :

       Helps to convert the UOM of material quantity based on the parameters # Alternate UOM to be converted to
  1. Material Quantity
  2. Material Number 
                    and export the converted material quantity.

FUNCTION Z_CONV_ALTERNATE_UOM :

        Helps to convert the UOM of material quantity based on the parameters # Alternate UOM to be converted to
  1. Current UOM of the material quantity
  2. Material Quantity
  3. Material Number 
                    and export the converted material quantity along the Numerator & Denominator for conversion to base units of measure.

 Code:

Main Program Code: SAPLZUOM
*******************************************************************
*   System-defined Include-files.                                 *
*******************************************************************
  INCLUDE LZUOMTOP.                          " Global Data
  INCLUDE LZUOMUXX.                          " Function Modules
*******************************************************************
*   User-defined Include-files (if necessary).                    *
*******************************************************************
* INCLUDE LZUOMF...                          " Subprograms
* INCLUDE LZUOMO...                          " PBO-Modules
* INCLUDE LZUOMI...                          " PAI-Modules
--------------------------------------------------------------------------
Include Program Code:
--------------------------------------------------------------------------
INCLUDE LZUOMTOP.                         
FUNCTION-POOL ZUOM.
DATA :  WS_C_CF TYPE   P DECIMALS 5,
        WA_C_CF1 TYPE  MARM,
        WA_C_CF2 TYPE  MARM,
        WS_C_MEINS TYPE  MARA.
DATA :  WS_BASE_CF TYPE   P DECIMALS 5,
        WS_CUR_QTY TYPE   P DECIMALS 5,
        WA_BASE_CF1 TYPE  MARM,
        WA_CURR_CF2 TYPE  MARM,
        WS_ALT_CF3  TYPE  MARM,
        WS_BASE_MEINS TYPE MARA-MEINS,
        WS_ALT_UOM    TYPE MARA-MEINS,
        WS_CURR_UOM   TYPE MARA-MEINS.
*****************************************************************
*   THIS FILE IS GENERATED BY THE FUNCTION LIBRARY.             *
*   NEVER CHANGE IT MANUALLY, PLEASE!                           *
*****************************************************************
INCLUDE LZUOMU01.   "Z_CONVERT_TO_ALTERNATE_UOM
INCLUDE LZUOMU02.   "Z_CONV_ALTERNATE_UOM
INCLUDE LZUOMU01.   
FUNCTION Z_CONVERT_TO_ALTERNATE_UOM .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(P_ALT_UOM) TYPE  MARM-MEINH
*"     REFERENCE(P_CUR_QTY) TYPE  P
*"     REFERENCE(P_MATNR) TYPE  MARM-MATNR
*"  EXPORTING
*"     REFERENCE(P_ALT_QTY) TYPE  P
*"----------------------------------------------------------------------
  CLEAR : WS_C_MEINS,WS_C_CF.
  CLEAR : WS_C_MEINS,WS_C_CF.
  SELECT SINGLE MEINS
      FROM MARA
      INTO CORRESPONDING FIELDS OF WS_C_MEINS
      WHERE MATNR = P_MATNR.
*** sales unit of measure not equal to base unit of measure
  IF P_ALT_UOM <> WS_C_MEINS-MEINS.
    WS_ALT_UOM = P_ALT_UOM.
    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
      EXPORTING
        INPUT          = WS_ALT_UOM
      IMPORTING
        OUTPUT         = WS_ALT_UOM
      EXCEPTIONS
        UNIT_NOT_FOUND = 1
        OTHERS         = 2.
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    CLEAR : WA_C_CF1.       " Conversion Factor for Base unit measure
    SELECT SINGLE
           MATNR
           MEINH
           UMREZ     "Numerator for Conversion to Base UnitsMeasure
           UMREN     "Denominator for conversion to base unitsmeasure
      FROM MARM
      INTO CORRESPONDING FIELDS OF WA_C_CF1
     WHERE MATNR = P_MATNR
       AND MEINH   = WS_C_MEINS-MEINS.
    CLEAR : WA_C_CF2  .
    SELECT SINGLE
           MATNR
           MEINH
           UMREZ
           UMREN
      FROM MARM
      INTO CORRESPONDING FIELDS OF WA_C_CF2
     WHERE MATNR = P_MATNR
       AND MEINH = WS_ALT_UOM.
***Calculate the conversion factor
    WS_C_CF = ( WA_C_CF2-UMREZ / WA_C_CF2-UMREN )
            * ( WA_C_CF1-UMREZ / WA_C_CF1-UMREN ).
  ELSE.
    WS_C_CF = 1.
  ENDIF.
  P_ALT_QTY = P_CUR_QTY * WS_C_CF.
ENDFUNCTION.
--------------------------------------------------------------------------
INCLUDE LZUOMU02.
FUNCTION Z_CONV_ALTERNATE_UOM .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(P_ALT_UOM) TYPE  MARM-MEINH
*"     REFERENCE(P_CURR_UOM) TYPE  MARM-MEINH
*"     REFERENCE(P_CUR_QTY) TYPE  P
*"     REFERENCE(P_MATNR) TYPE  MARM-MATNR
*"  EXPORTING
*"     REFERENCE(P_ALT_QTY) TYPE  P
*"     VALUE(UMREZ) TYPE  UMREZ
*"     VALUE(UMREN) TYPE  UMREN
*"  EXCEPTIONS
*"      CURR_UOM_NOT_FOUND
*"      ALT_UOM_NOT_FOUND
*"----------------------------------------------------------------------
  CLEAR: WS_CUR_QTY,
         WS_BASE_MEINS,
         WS_C_MEINS.
*** sales unit of measure not equal to Alt unit of measure
  IF P_ALT_UOM <> P_CURR_UOM.
    WS_CURR_UOM = P_CURR_UOM.
    WS_ALT_UOM  = P_ALT_UOM.
    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
      EXPORTING
        INPUT          = WS_ALT_UOM
      IMPORTING
        OUTPUT         = WS_ALT_UOM
      EXCEPTIONS
        UNIT_NOT_FOUND = 1
        OTHERS         = 2.
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
      EXPORTING
        INPUT          = WS_CURR_UOM
      IMPORTING
        OUTPUT         = WS_CURR_UOM
      EXCEPTIONS
        UNIT_NOT_FOUND = 1
        OTHERS         = 2.
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
*---  1ST GET THE BASE UOM
    SELECT SINGLE MEINS
      FROM MARA
      INTO WS_BASE_MEINS
      WHERE MATNR = P_MATNR.
*--- Conversion Factor for Base UOM
    CLEAR : WA_BASE_CF1.       " Conversion Factor for Base unit measure
    SELECT SINGLE MATNR
                  MEINH
                  UMREZ     "Numerator for Conversion to Base UnitsMeasure
                  UMREN     "Denominator for conversion to base unitsmeasure
             FROM MARM
             INTO CORRESPONDING FIELDS OF WA_BASE_CF1
            WHERE MATNR = P_MATNR
              AND MEINH = WS_BASE_MEINS.
*--- conversion Factor for Curr UOM
    CLEAR : WA_CURR_CF2.       " Conversion Factor for Current unit measure
    SELECT SINGLE MATNR
                  MEINH
                  UMREZ     "Numerator for Conversion to Base UnitsMeasure
                  UMREN     "Denominator for conversion to base unitsmeasure
             FROM MARM
             INTO CORRESPONDING FIELDS OF WA_CURR_CF2
            WHERE MATNR = P_MATNR
              AND MEINH = WS_CURR_UOM.
*--- Conversion Factor for Alt UOM
    CLEAR : WS_ALT_CF3.  " Conversion Factor for Alternate unit measure
    SELECT SINGLE MATNR
                  MEINH
                  UMREZ
                  UMREN
             FROM MARM
             INTO CORRESPONDING FIELDS OF WS_ALT_CF3
            WHERE MATNR = P_MATNR
            AND   MEINH = WS_ALT_UOM.
*** What is the Base Qty, convert from Alternate to Base.
*** Calculate the conversion factor from Current UOM to the Base UOM
    WS_BASE_CF =   (  WA_CURR_CF2-UMREZ / WA_CURR_CF2-UMREN )
                 * (  WA_BASE_CF1-UMREZ / WA_BASE_CF1-UMREN ).
*--- Qty value form Current UOM to Base UOM
    WS_CUR_QTY = P_CUR_QTY * WS_BASE_CF.
*** What is the new UOM Qty, Convert from Base to new Alternate
*** Calculate the conversin factor from Base UOM to Alternate UOM.
    WS_C_CF =   ( WS_ALT_CF3-UMREN  / WS_ALT_CF3-UMREZ )
              * ( WA_BASE_CF1-UMREN / WA_BASE_CF1-UMREZ ).
*--- Qty value from Base UOM to Alt UOM
    WS_CUR_QTY = WS_CUR_QTY * WS_C_CF.
  ELSE.
    WS_CUR_QTY = P_CUR_QTY.
  ENDIF.
  P_ALT_QTY = WS_CUR_QTY.
  UMREZ     = WS_ALT_CF3-UMREZ.
  UMREN     = WS_ALT_CF3-UMREN.
ENDFUNCTION.

No comments:

Post a Comment