{"id":127,"date":"2011-08-11T09:40:27","date_gmt":"2011-08-11T08:40:27","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=127"},"modified":"2012-08-07T09:55:54","modified_gmt":"2012-08-07T08:55:54","slug":"converting-material-quantities-to-different-unit-of-measure-in-abap","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=127","title":{"rendered":"Converting material quantities to different unit of measure in ABAP"},"content":{"rendered":"<p>Each material in SAP has its standard, base unit of measure (stored in MARA-MEINS). To allow using alternative units, those can be maintained per material in table MARM. A common requirement for ABAPer is to convert material quantity from one unit to another, most likely from an alternative unit to the base one, to be able to summarize the report results.<!--more--><\/p>\n<p>To do the conversion, SAP provides the function module MATERIAL_UNIT_CONVERSION. Its parameter names are not self-explaining, and for my own purposes I have a simple wrapper form that I use to convert quantities from alternative to base UoM. The form is using data caching technique.<\/p>\n<pre>form convert_to_base_uom\r\n  using    pf_matnr     type matnr\r\n           pf_menge_in  type gsmng\r\n           pf_meins_in  type meins\r\n  changing pf_menge_out type gsmng\r\n           pf_meins_out type meins. \r\n\r\n* define internal table to cache the base UOM\r\n  types: begin of lty_meins_rec,\r\n             matnr type matnr,\r\n             meins type meins,\r\n           end of lty_meins_rec. \r\n\r\n  types:\r\n    lty_meins_tab type hashed table of lty_meins_rec\r\n          with unique key matnr.\r\n  data:\r\n    ls_wa type lty_meins_rec. \r\n\r\n  statics:\r\n    lt_meins type lty_meins_tab. \r\n\r\n* first, find the base UOM\r\n  clear pf_meins_out.\r\n  read table lt_meins into ls_wa\r\n    with table key matnr = pf_matnr.\r\n  if sy-subrc = 0.\r\n    pf_meins_out = ls_wa-meins.\r\n  else.\r\n    select single meins\r\n      from mara\r\n      into ls_wa-meins\r\n      where matnr = pf_matnr.\r\n    if sy-subrc  0.  \"doesn't exist. try PC\r\n      ls_wa-meins = 'ST'.\r\n    endif.\r\n    ls_wa-matnr = pf_matnr.\r\n    pf_meins_out = ls_wa-meins.\r\n    insert ls_wa into table lt_meins.\r\n  endif. \r\n\r\n* now convert the qty\r\n  if pf_meins_in = pf_meins_out.\r\n    pf_menge_out = pf_menge_in.\r\n  else.\r\n    call function 'MATERIAL_UNIT_CONVERSION'\r\n         exporting\r\n              input                = pf_menge_in\r\n              kzmeinh              = 'X'\r\n              matnr                = pf_matnr\r\n              meinh                = pf_meins_in\r\n              meins                = pf_meins_out\r\n              type_umr             = '3'\r\n         importing\r\n              output               = pf_menge_out\r\n         exceptions\r\n              conversion_not_found = 1\r\n              input_invalid        = 2\r\n              material_not_found   = 3\r\n              meinh_not_found      = 4\r\n              meins_missing        = 5\r\n              no_meinh             = 6\r\n              output_invalid       = 7\r\n              overflow             = 8\r\n              others               = 9. \r\n  endif. \r\nendform.<\/pre>\n<pre lang=\"&quot;abap\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Each material in SAP has its standard, base unit of measure (stored in MARA-MEINS). To allow using alternative units, those can be maintained per material in table MARM. A common requirement for ABAPer is to convert material quantity from one &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=127\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[16,9],"tags":[446,21,17],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","tag-abap","tag-material_unit_conversion","tag-sap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-23","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}