Conversión de unidades (UOM)

EDIT: descubrí que otra opción más sencilla es usar la función: “MD_CONVERT_MATERIAL_UNIT”  a la cual tan solo le tenemos que pasar MATNR, Unidad Origen, Unidad Destina y Cantidad

La función que utilizamos para convertir unidades es la “MATERIAL_UNIT_CONVERSION”

Navegando por inet encontre una subrutina que hizo un chico para que sea más entendible. Simplemente pasamos el material, cantidad y unidad origen y nos devuelve la cantidad y unidad destino que deseemos.

form convert_to_base_uom
  using    pf_matnr     type matnr
           pf_menge_in  type gsmng
           pf_meins_in  type meins
  changing pf_menge_out type gsmng
           pf_meins_out type meins. 

* define internal table to cache the base UOM
  types: begin of lty_meins_rec,
             matnr type matnr,
             meins type meins,
           end of lty_meins_rec. 

  types:
    lty_meins_tab type hashed table of lty_meins_rec
          with unique key matnr.
  data:
    ls_wa type lty_meins_rec. 

  statics:
    lt_meins type lty_meins_tab. 

* first, find the base UOM
  clear pf_meins_out.
  read table lt_meins into ls_wa
    with table key matnr = pf_matnr.
  if sy-subrc = 0.
    pf_meins_out = ls_wa-meins.
  else.
    select single meins
      from mara
      into ls_wa-meins
      where matnr = pf_matnr.
    if sy-subrc  0.  "doesn't exist. try PC
      ls_wa-meins = 'ST'.
    endif.
    ls_wa-matnr = pf_matnr.
    pf_meins_out = ls_wa-meins.
    insert ls_wa into table lt_meins.
  endif. 

* now convert the qty
  if pf_meins_in = pf_meins_out.
    pf_menge_out = pf_menge_in.
  else.
    call function 'MATERIAL_UNIT_CONVERSION'
         exporting
              input                = pf_menge_in
              kzmeinh              = 'X'
              matnr                = pf_matnr
              meinh                = pf_meins_in
              meins                = pf_meins_out
              type_umr             = '3'
         importing
              output               = pf_menge_out
         exceptions
              conversion_not_found = 1
              input_invalid        = 2
              material_not_found   = 3
              meinh_not_found      = 4
              meins_missing        = 5
              no_meinh             = 6
              output_invalid       = 7
              overflow             = 8
              others               = 9. 

  endif. 

endform.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: