Sample Code For FCO - COGS - SPLIT - BASIS

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

* -------------------------------------------------------------------------

* Example: Determine a basis for intercompany invoices to split the COGS.


* The basis is the current cost estimate for material / plant which is
* saved in the invoice item.
* -------------------------------------------------------------------------

DATA: lt_cogs_cust_all TYPE fcot_cogs_split,


lt_cogs_cust_rel TYPE fcot_cogs_split,
lt_bukrs TYPE fcot_bukrs,
lt_budat TYPE fcot_budat,
lts_curtp_info TYPE cl_fco_cogs_rwin=>gtsty_curtp_info,
lts_valutyp_pr_cost_key TYPE fcoys_valutyp_pr_cost_key,
lt_cost_comp_split TYPE fcott_cost_comp_split,
ls_split_factor LIKE LINE OF cts_split_factor,
ls_accit_mat TYPE accit,
ls_curtp_info TYPE cl_fco_cogs_rwin=>gsty_curtp_info,
ls_valutyp_pr_cost_key LIKE LINE OF lts_valutyp_pr_cost_key,
ls_cost_comp_split LIKE LINE OF lt_cost_comp_split,
ls_cogs_cust LIKE LINE OF lt_cogs_cust_all,
lv_curtp TYPE curtp,
lv_ktopl TYPE ktopl,
lb_calculation_found TYPE boole_d.

FIELD-SYMBOLS: <ls_cogs_cust> LIKE LINE OF lt_cogs_cust_all,


<ls_acccr> LIKE LINE OF it_acccr,
<ls_curtp_info> LIKE LINE OF lts_curtp_info,
<ls_valutyp_pr_cost_key> LIKE LINE OF lts_valutyp_pr_cost_key,
<ls_split_factor> LIKE LINE OF cts_split_factor.

* -------------------------------------------------------------------------
* Implement check when an own basis should be determined for COGS split !!!
* -------------------------------------------------------------------------

* Get relevant COGS split customizing


APPEND is_current_accit-bukrs TO lt_bukrs.
APPEND is_current_accit-budat TO lt_budat.
CALL FUNCTION 'FCO_COGS_READ_CUSTOMIZING_CC'
EXPORTING
it_bukrs = lt_bukrs
it_budat = lt_budat
IMPORTING
et_cogs_split = lt_cogs_cust_all.

* Only if for account COGS customizinbg is available determine basis


LOOP AT lt_cogs_cust_all ASSIGNING <ls_cogs_cust>
WHERE bukrs = is_current_accit-bukrs AND
budat = is_current_accit-budat AND
cogs_account = is_current_accit-hkont.
APPEND <ls_cogs_cust> TO lt_cogs_cust_rel.
ENDLOOP.

* Get basis only for items with COGS account maintained in COGS split customizing
CHECK lt_cogs_cust_rel IS NOT INITIAL.

* -------------------------------------------------------------------------
* Get basis info !!!
* -------------------------------------------------------------------------
* Determine new basis for COGS split if no split is available and basis BADI is
called
IF cts_split_factor[] IS INITIAL.

* Take over info from S item


ls_accit_mat-matnr = is_current_accit-matnr.
ls_accit_mat-werks = is_current_accit-werks.
ls_accit_mat-bwtar = is_current_accit-bwtar.
ls_accit_mat-sobkz = is_current_accit-sobkz.
ls_accit_mat-kzbws = is_current_accit-kzbws.

IF ls_accit_mat-matnr IS NOT INITIAL AND ls_accit_mat-werks IS NOT INITIAL.


* Determine cost estimate from material / plant info of current item

* Get relevant CURTPs for document - transaction currency + all CURTP relevant for
material ledger
LOOP AT it_acccr ASSIGNING <ls_acccr>
WHERE posnr = is_current_accit-posnr.
CLEAR: ls_curtp_info, lv_curtp.
IF <ls_acccr>-curtp = '00'.
* Create at least one entry for transaction currency
MOVE-CORRESPONDING <ls_acccr> TO ls_curtp_info.
INSERT ls_curtp_info INTO TABLE lts_curtp_info.
ELSE.
SELECT SINGLE curtp INTO lv_curtp FROM fmlt_curtp_ml
WHERE rbukrs = is_current_accit-bukrs AND
curtp = <ls_acccr>-curtp.
IF lv_curtp IS NOT INITIAL.
* CURTP is relevant for material ledger
MOVE-CORRESPONDING <ls_acccr> TO ls_curtp_info.
INSERT ls_curtp_info INTO TABLE lts_curtp_info.
ENDIF.
ENDIF.
ENDLOOP.

* Get relevant valuation views in order to fill table lts_valutyp_pr_cost_key.


* With this internal table you forward for which valuation views you want to have
* the costing estimates.
LOOP AT lts_curtp_info ASSIGNING <ls_curtp_info>.
ls_valutyp_pr_cost_key-valutyp = <ls_curtp_info>-valutyp.
INSERT ls_valutyp_pr_cost_key INTO TABLE lts_valutyp_pr_cost_key.
ENDLOOP.

* Get cost estimate keys for all relevant valuation views for current material /
plant
CALL FUNCTION 'FCO_COGS_GET_ACTIVE_VERSION'
EXPORTING
i_material_ext = ls_accit_mat-matnr
i_plant = ls_accit_mat-werks
i_sales_order = is_current_accit-kdauf
i_sales_order_item = is_current_accit-kdpos
i_valuation_type = ls_accit_mat-bwtar
i_special_stock_ind = ls_accit_mat-sobkz
i_valuation_special_stock = ls_accit_mat-kzbws
i_posting_date = is_current_accit-budat
CHANGING
cts_valutyp_pr_cost_key = lts_valutyp_pr_cost_key
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
CLEAR: lts_valutyp_pr_cost_key.
ENDIF.

* Manual calculations should not be used for split => delete KKZMA
LOOP AT lts_valutyp_pr_cost_key ASSIGNING <ls_valutyp_pr_cost_key>
WHERE kalnr IS NOT INITIAL.
CLEAR: <ls_valutyp_pr_cost_key>-kkzma.
lb_calculation_found = abap_true.
ENDLOOP.

CHECK lb_calculation_found IS NOT INITIAL.

* Get cost estimate info


CALL FUNCTION 'FCO_COGS_GET_VALUE_OF_KEKO'
EXPORTING
its_valutyp_pr_cost_key = lts_valutyp_pr_cost_key
IMPORTING
e_cost_comp_split = lt_cost_comp_split
EXCEPTIONS
invalid_bzobj = 1
failed = 2
OTHERS = 3.
IF sy-subrc <> 0.
CLEAR: lb_calculation_found.
ENDIF.

CHECK lb_calculation_found IS NOT INITIAL AND lt_cost_comp_split IS NOT


INITIAL.

* -------------------------------------------------------------------------
* Fill split factor / export table !!!
* -------------------------------------------------------------------------
CALL FUNCTION 'FI_CHART_OF_ACCOUNT_DETERMINE'
EXPORTING
i_bukrs = is_current_accit-bukrs
IMPORTING
e_ktopl = lv_ktopl.

LOOP AT lts_valutyp_pr_cost_key ASSIGNING <ls_valutyp_pr_cost_key>.


LOOP AT lt_cost_comp_split INTO ls_cost_comp_split
WHERE valutyp = <ls_valutyp_pr_cost_key>-valutyp.
CLEAR: ls_cogs_cust.
READ TABLE lt_cogs_cust_rel INTO ls_cogs_cust
WITH KEY bukrs = is_current_accit-bukrs
budat = is_current_accit-budat
valutyp = ls_cost_comp_split-valutyp
ktopl = lv_ktopl
cogs_account = is_current_accit-hkont
cost_component_layout = <ls_valutyp_pr_cost_key>-elehk
cost_comp = ls_cost_comp_split-cost_comp.
IF sy-subrc <> 0.
READ TABLE lt_cogs_cust_rel INTO ls_cogs_cust
WITH KEY bukrs = is_current_accit-bukrs
budat = is_current_accit-budat
valutyp = ls_cost_comp_split-valutyp
ktopl = lv_ktopl
cogs_account = is_current_accit-hkont
cost_component_layout = <ls_valutyp_pr_cost_key>-elehk
cogs_default = 'X'.
ENDIF.
IF sy-subrc <> 0 OR ls_cogs_cust-cogs_target IS INITIAL.
* For current COGS account and for current valutyp no COGS split customizingt
available
EXIT.
ENDIF.

READ TABLE cts_split_factor ASSIGNING <ls_split_factor>


WITH KEY hkont = ls_cogs_cust-cogs_target
valutyp = ls_cost_comp_split-valutyp.
IF sy-subrc <> 0.
CLEAR: ls_split_factor.
ls_split_factor-hkont = ls_cogs_cust-cogs_target.
ls_split_factor-valutyp = ls_cost_comp_split-valutyp.
INSERT ls_split_factor INTO TABLE cts_split_factor ASSIGNING
<ls_split_factor>.
ENDIF.
* Add amount in local currency:
IF is_current_accit-shkzg = 'S'.
ADD ls_cost_comp_split-total_amt_cc TO <ls_split_factor>-
weight_total.
ADD ls_cost_comp_split-fixed_amt_cc TO <ls_split_factor>-weight_fix.
ELSE.
SUBTRACT ls_cost_comp_split-total_amt_cc FROM <ls_split_factor>-
weight_total.
SUBTRACT ls_cost_comp_split-fixed_amt_cc FROM <ls_split_factor>-
weight_fix.
ENDIF.
ENDLOOP. " lt_cost_comp_split
ENDLOOP. " lts_valutyp_pr_cost_key
ENDIF.
ENDIF.

You might also like