Thursday, May 10, 2012

Useful Tips MM

UoM Conversion Logic Explained
*&---------------------------------------------------------------------*
*&      Form  CONVERSION_UOM
*&---------------------------------------------------------------------*
* IF BUoM is CS: CWM field will contains LB value and normal in CS
* IF BUoM is LB: CWM fields will contain CS value and normal in LB
*----------------------------------------------------------------------*
FORM conversion_uom.
  DATA:   ls_mara       TYPE ty_mara,
          ls_mchb       TYPE ty_mchb,
          ls_mchb_temp  TYPE ty_mchb,
          lt_mchb       TYPE TABLE OF ty_mchb,
          l_index       TYPE sy-tabix.
  LOOP AT gt_mara INTO ls_mara.
    l_index = sy-tabix.
    READ TABLE gt_mchb INTO ls_mchb WITH KEY matnr = ls_mara-matnr.
    IF ls_mara-/cwm/xcwmat = c_x AND ls_mara-meins = c_lb.
      ls_mchb-clabs = ls_mchb-/cwm/clabs.
      MODIFY gt_mchb INDEX l_index FROM ls_mchb TRANSPORTING clabs.
    ELSEIF ls_mara-meins NE c_cs AND ls_mara-meins NE c_lb.
      CALL METHOD zcl_pin_convert_new_uom=>convert_uom
        EXPORTING
          i_matnr              = ls_mara-matnr
          i_from_uom           = ls_mara-meins
          i_to_uom             = c_csmeins
          i_qty                = ls_mchb-clabs
        IMPORTING
          e_qty                = ls_mchb-clabs
        EXCEPTIONS
          meins_not_maintained = 1
          meinh_not_maintained = 2
          unexpected_error     = 3
          OTHERS               = 4.
      IF sy-subrc = 0.
        MODIFY gt_mchb INDEX l_index FROM ls_mchb TRANSPORTING clabs.
      ENDIF.
    ENDIF.
    CLEAR: l_index, ls_mara.
  ENDLOOP.
ENDFORM.                    " CONVERSION_UOM
******

METHOD convert_uom.

  DATA : lv_from TYPE meins,
         lv_to TYPE meins,
         l_value_meins_tmp TYPE f,
         lv_input1 TYPE cd0fltp,
         lv_output1 TYPE cd0fltp.

  IF i_from_uom = i_to_uom
    AND i_from_uom IS NOT INITIAL
    AND i_to_uom   IS NOT INITIAL
    AND i_from_uom IS SUPPLIED
    AND i_to_uom   IS SUPPLIED.
    CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4
                              OTHERS = 8.
      e_qty = i_qty.
    ENDCATCH.
    IF sy-subrc <> 0.
      RAISE unexpected_error.
    ENDIF.
  ELSE.

    IF i_qty IS SUPPLIED.
      CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4
                              OTHERS = 8.
        lv_input1 = i_qty.
      ENDCATCH.
      IF sy-subrc <> 0.
        RAISE unexpected_error.
      ENDIF.
    ELSE.
      lv_input1 = 1.
    ENDIF.

    IF i_to_uom IS NOT SUPPLIED.
      SELECT SINGLE meins FROM mara INTO lv_to WHERE matnr = i_matnr.
      IF sy-subrc <> 0.
        RAISE meinh_not_maintained.
      ENDIF.
    ELSE.
      lv_to = i_to_uom.
    ENDIF.

    IF i_from_uom IS NOT SUPPLIED.
      SELECT SINGLE meins FROM mara INTO lv_from WHERE matnr = i_matnr.
      IF sy-subrc <> 0.
        RAISE meins_not_maintained.
      ENDIF.
    ELSE.
      lv_from = i_from_uom.
    ENDIF.
    CLEAR lv_output1.

*   Umrechnung in Basismenge
    CALL FUNCTION 'MATERIAL_UNIT_CONVERSION'
      EXPORTING
        matnr                = i_matnr
        input                = lv_input1
        meinh                = lv_from
        kzmeinh              = 'X'
      IMPORTING
        output               = l_value_meins_tmp
      EXCEPTIONS
        conversion_not_found = 01
        input_invalid        = 02
        material_not_found   = 03
        meinh_not_found      = 04
        meins_missing        = 05
        no_meinh             = 06
        output_invalid       = 07
        overflow             = 08.
    IF sy-subrc <> 0.
      RAISE unexpected_error.
    ENDIF.
*   Umrechnung auf neue Alternativmengeneinheit
    CALL FUNCTION 'MATERIAL_UNIT_CONVERSION'
      EXPORTING
        matnr                = i_matnr
        input                = l_value_meins_tmp
        meinh                = lv_to
        kzmeinh              = space
      IMPORTING
        output               = lv_output1
      EXCEPTIONS
        conversion_not_found = 01
        input_invalid        = 02
        material_not_found   = 03
        meinh_not_found      = 04
        meins_missing        = 05
        no_meinh             = 06
        output_invalid       = 07
        overflow             = 08.

    IF sy-subrc <> 0.
      RAISE unexpected_error.
    ELSE.
      CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4
                              OTHERS = 8.
        e_qty = lv_output1.
      ENDCATCH.
      IF sy-subrc <> 0.
        RAISE unexpected_error.
      ENDIF.
    ENDIF.
  ENDIF.
ENDMETHOD.


**********
Code to Explode or find Material where used
FORM explode_material .
   DATA: gt_shadow    TYPE TABLE OF chvwshadow,
        gs_dfchvw       TYPE dfchvw,
        gt_plants       TYPE qc_werks_range.

  DATA: ls_mara TYPE ty_mara,
        l_index TYPE sy-tabix,         
        ls_mchb TYPE ty_mchb.          
* Call of function to read batch-where-used list
  gs_dfchvw-alstf = '00'.
  gs_dfchvw-kznul = 'X'.
  gs_dfchvw-trfpo = 'X'.
  gs_dfchvw-exptp = 'B'.
  gs_dfchvw-dsptp = 'L'.

  CALL FUNCTION 'CHVW_EXPLODE_ALL'
    EXPORTING
      i_matnr  = matnr
      i_werks  = plant
      i_charg  = batch
      i_dfchvw = gs_dfchvw
    TABLES
      t_shadow = gt_shadow
      t_plants = gt_plants
    EXCEPTIONS
      OTHERS   = 1.


*************
MARA - Material Master
MCHB - Material details on basis of CHARG - mapped to Tcode MMBE*
CHVW - Used Materials details
********

No comments:

Post a Comment