How to get costs breakup in production order for MTO scenario.

Let us understand SAP’s product costing & how to get costs breakup in production order for Make to Order scenario

SAP product costing by order process:

For any manufacturing organization, it is utmost important to control the costs of manufacturing. In SAP product costing by order, we calculate the planned cost before production and then during production, we book the actual costs of raw material, machines, labor and overhead costs.

Once the production is over, we must settle the production cost against material master, sales order or WBS element. For more details please follow SAP’s documentation on product costing. An analysis of planned vs actual cost will give you an overview that something went wrong either in planning or in production.

In SAP you can see these costs in production order by following menu: Goto – Costs – Itemization

Costing-items-with-cost-elements
Costing-items-with-cost-elements

How to get costs breakup:

Due to various reasons ( export control, management reporting etc.),  we have to provide the break-up of the cost into various categories or a specific cost category. Function module K_TARGETCOSTS_OBJECT_RECALC will provide cost break up & populate data into 3 internal tables with costs split into primary and secondary costs. Using these 3 internal tables you can find any component/category of product costing in SAP.

Demo implementation:

My requirement was to find total cost ( primary + secondary ) and value Add ( secondary costs only ). I created a method with Sales order and item number as input & costs ( total & value add ) as output. Your calculation requirement may vary so you can adjust the calculation part as per your conditions:

METHOD get_production_order_cost.
  TYPES: BEGIN OF ts_afpo,
          aufnr TYPE aufnr,
          meins TYPE meins,
          psmng TYPE lstxx,
         END OF ts_afpo.

   TYPES: BEGIN OF ts_aufk,
           bukrs TYPE bukrs,
           kokrs TYPE kokrs,
           objnr TYPE cosla-objnr,
          END OF ts_aufk.

*-- Local data declaration
  DATA: lt_cosla TYPE TABLE OF cosla,
        lt_cospa TYPE TABLE OF cospa,
        lt_cossa TYPE TABLE OF cossa,
        ls_afpo  TYPE ts_afpo,
        ls_aufk  TYPE ts_aufk,
        ls_cospa TYPE cospa,
        ls_cossa TYPE cossa,
        lv_gstrs TYPE sy-datum,
        lv_gjahr TYPE gjahr,
        lv_buper TYPE monat,
        lv_field TYPE char10,
        lv_cost  TYPE stprs,
        lv_poper TYPE t009b-poper.

  FIELD-SYMBOLS <fs_cost> TYPE any.

*-- Clear variables
  CLEAR:lt_cosla,
        lt_cospa,
        lt_cossa,
        ls_cospa,
        ls_cossa,
        ls_afpo,
        ls_aufk,
        lv_gstrs,
        lv_gjahr,
        lv_buper,
        lv_field,
        lv_cost,
        lv_poper.

*-- Get Production Order data based on selection parameters.
  SELECT SINGLE aufnr
         meins
         psmng
         FROM afpo
         INTO ls_afpo
         WHERE kdauf = iv_sales_order
         AND kdpos   = iv_posnr.
  IF sy-subrc = 0.
    SELECT SINGLE bukrs kokrs objnr
           FROM aufk
           INTO ls_aufk
           WHERE aufnr = ls_afpo-aufnr.
    IF sy-subrc = 0.
      SELECT SINGLE gstrs
             FROM afko
             INTO lv_gstrs
             WHERE aufnr = ls_afpo-aufnr.
      IF sy-subrc = 0.
*-- Get fiscal year & period
        CALL FUNCTION 'BAPI_COMPANYCODE_GET_PERIOD'
          EXPORTING
            companycodeid = ls_aufk-bukrs
            posting_date  = lv_gstrs
          IMPORTING
            fiscal_year   = lv_gjahr
            fiscal_period = lv_buper.
        IF lv_gjahr IS NOT INITIAL AND lv_buper IS NOT INITIAL.
          lv_poper = lv_buper.
*-- Get production order costs
          CALL FUNCTION 'K_TARGETCOSTS_OBJECT_RECALC'
            EXPORTING
              par_awvrs              = '000'
              par_gjahr1             = lv_gjahr
              par_gjahr2             = lv_gjahr
              par_kokrs              = ls_aufk-kokrs
              par_meinh              = ls_afpo-meins
              par_menge              = ls_afpo-psmng
              par_objnr              = ls_aufk-objnr
              par_poper1             = lv_poper
              par_poper2             = lv_poper
            TABLES
              pta_cosla              = lt_cosla
              pta_cospa              = lt_cospa
              pta_cossa              = lt_cossa
            EXCEPTIONS
              targetcosts_impossible = 1
              version_invalid        = 2
              system_error           = 3
              OTHERS                 = 4.
          IF sy-subrc <> 0.
            CLEAR: ev_total_cost,
                   ev_value_add.
          ELSE.
*-- Cost Calculation
            CONCATENATE 'WTG0' lv_buper INTO lv_field.
            LOOP AT lt_cospa INTO ls_cospa.
              ASSIGN COMPONENT lv_field OF STRUCTURE ls_cospa TO <fs_cost>.
              IF <fs_cost> IS ASSIGNED.
                lv_cost = <fs_cost>.
                ev_total_cost = ev_total_cost + lv_cost.
                CLEAR: lv_cost.
                UNASSIGN: <fs_cost>.
              ENDIF.
            ENDLOOP.

            LOOP AT lt_cossa INTO ls_cossa.
              ASSIGN COMPONENT lv_field OF STRUCTURE ls_cossa TO <fs_cost>.
              IF <fs_cost> IS ASSIGNED.
                lv_cost = <fs_cost>.
                ev_total_cost = ev_total_cost + lv_cost.
                ev_value_add  = ev_value_add + lv_cost.
                CLEAR: lv_cost.
                UNASSIGN: <fs_cost>.
              ENDIF.
            ENDLOOP.

            IF ls_afpo-psmng IS NOT INITIAL.
              ev_total_cost = ev_total_cost / ls_afpo-psmng.
              ev_value_add  = ev_value_add / ls_afpo-psmng.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.
ENDMETHOD.

I hope you like this post on how to get costs breakup in production order for Make to Order scenario. You can write to us in case of any query/suggestion/feedback. Happy learning !

Scroll to Top