0% found this document useful (0 votes)
359 views

Formula 920 ICMI

The document contains code for tax calculation logic in SAP. It initializes various data structures and fields needed for tax calculation, including company code, document number, currency, amount, quantity and jurisdiction codes. It checks if condition-based tax calculation is active and calls the appropriate tax calculation function. For old external tax calculation, it populates tax amounts from the document lines. It also contains logic to handle tax exemptions and read additional master data for customers, vendors and plants.

Uploaded by

faelsmg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
359 views

Formula 920 ICMI

The document contains code for tax calculation logic in SAP. It initializes various data structures and fields needed for tax calculation, including company code, document number, currency, amount, quantity and jurisdiction codes. It checks if condition-based tax calculation is active and calls the appropriate tax calculation function. For old external tax calculation, it populates tax amounts from the document lines. It also contains logic to handle tax exemptions and read additional master data for customers, vendors and plants.

Uploaded by

faelsmg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

** criada com cpia na 320 - incluir esta frmula na linha do ICMI

*Esta frmula foi desenvolvida para condio ICMI no contemplar ICMS


*{ INSERT
DEVK902864
1
*} INSERT
FORM FRM_KONDI_WERT_920.
*{ INSERT
DEVK902864
DATA: xcom_tax LIKE com_tax,
xcom_err LIKE com_err,
br_komv LIKE komv.
DATA:

1
"XKOMV also contains Index !

DATA:

BEGIN OF xxkomv OCCURS 10.


INCLUDE STRUCTURE komv.
END OF xxkomv.

DATA:

br_konp LIKE konp OCCURS 10 WITH HEADER LINE.

DATA:

mwskz LIKE komp-mwskz,


"Steuerkennzeichen
plant LIKE t001w-werks,
"Werk
save_xkomv LIKE komv_index,
posnr LIKE komp-kposn,
exempt_flag,
copy_flag,
counter TYPE i,
amount TYPE p,
quantity TYPE p.

DATA:
DATA:

lv_cbt_active TYPE xfeld.


ls_t001w TYPE t001w,
ls_kna1 TYPE kna1.
lv_rounding TYPE boole.

DATA:

* Note 916487 : It's necessary to check if the ICMS value should be updated
DATA: lv_tax_data_update TYPE boole.
* Note 904935 : Rounding ICMS value on document header level
DATA: lv_base TYPE kawrt,
lv_rate TYPE kbetr.
DATA: w_netwr
w_dif
l_fator
l_scale

TYPE
TYPE
TYPE
TYPE

komp-netwr,
komp-netwr,
P,
I.

STATICS: j_1bbdata LIKE j_1bbranch,


j_1bplant LIKE komp-werks.
* Macros for default handling
DEFINE assign_default.
if &2 is initial.
xcom_tax-&1 = &3.
else.
xcom_tax-&1 = &2.
endif.
END-OF-DEFINITION.
DEFINE default_on_initial.
if xcom_tax-&1 is initial or xcom_tax-&1 = space.

xcom_tax-&1 = &2.
endif.
END-OF-DEFINITION.
* Check if taxes are calculated with the new condition-based logic
* or with the old external tax calculation
CALL FUNCTION 'J_1B_CHECK_CBT_CALC'
EXPORTING
iv_kappl = komk-kappl
iv_kalsm = komk-kalsm
IMPORTING
ev_active = lv_cbt_active.
*
*

IF lv_cbt_active = 'X'.
Condition-based tax calculation
Note 904935 : Rounding ICMS value on document header level
CALL FUNCTION 'J_1B_CBT'
EXPORTING
is_komk
= komk
is_komp
= komp
is_komv_frm
= xkomv
it_komv
= xkomv[]
IMPORTING
ev_result
= xkwert
ev_base
= lv_base
ev_rate
= lv_rate
ev_rounding
= lv_rounding
ev_tax_data_update = lv_tax_data_update.

IF xkomv-kschl eq 'ICMI'.
read company code in case not filled yet (to get local currency)
IF t001-bukrs <> komk-bukrs.
IF NOT komk-bukrs IS INITIAL.
CALL FUNCTION 'T001_SINGLE_READ'
EXPORTING
bukrs = komk-bukrs
IMPORTING
wt001 = t001
EXCEPTIONS
OTHERS = 0.
ENDIF.
ENDIF.

*
*

calculation of conversion factor not required anymore:


this is handled by increasing the rounding scale below

determine rounding correction factor and decimals from Branch


IF j_1bplant <> komp-werks.

read branch data to get number of decimals for the price rounding
j_1bplant = komp-werks.
CALL FUNCTION 'J_1BREAD_PLANT_DATA'
EXPORTING
plant
= j_1bplant
IMPORTING
branch_data = j_1bbdata

EXCEPTIONS
OTHERS
= 1.
IF sy-subrc <> 0.
CLEAR j_1bbdata.
ENDIF.
ENDIF.
*

if rounding at branch is not defined use maximum level of detail


IF j_1bbdata-nfdec IS INITIAL.
j_1bbdata-nfdec = 6.
ENDIF.
CALL FUNCTION 'J_1B_ROUNDING_SCALE_GET'
EXPORTING
iv_tax_base = komp-netwr
iv_nfdec
= j_1bbdata-nfdec
IMPORTING
ev_scale
= l_scale
ev_factor = l_fator.
w_netwr = 1000000.
if komp-netwr lt w_netwr.
w_netwr = komp-netwr * ( 10 ** l_scale ).
else.
w_netwr = komp-netwr.
endif.
w_dif = xkwert - w_netwr.
xkwert = xkwert - w_dif.
ENDIF.

IF lv_rounding = 'X'.
PERFORM j_1b_rounding_brasil.
* Note 904935 : Rounding ICMS value on document header level
* Only update ICMS tax value.
* Note 916487 : This new condition makes sure to only update the ICMS value
ELSEIF lv_tax_data_update = 'X'.
xkomv-kawrt = lv_base.
xkomv-kbetr = lv_rate.
ENDIF.

ELSE.
Old, external tax calculation

* Clear global workfields


CLEAR: taxpct0, taxamt0,
taxpct1, taxamt1,
taxpct2, taxamt2,
taxpct3, taxamt3,
taxpct4, taxamt4,
taxpct5, taxamt5,
taxpct6, taxamt6,
taxpct7, taxamt7,
taxpct8, taxamt8,
taxpct9, taxamt9,

taxbas0,
taxbas1,
taxbas2,
taxbas3,
taxbas4,
taxbas5,
taxbas6,
taxbas7,
taxbas8,
taxbas9,

xcom_tax, xcom_err.
CHECK komk-trtyp NE 'A' AND
xkomv-ksteu NE 'E' AND
komp-prsok = 'X'.
* Begin of Pricing Copy Logic
copy_flag = space.
IF xkomv-ksteu = 'F'.
copy_flag = 'X'.
save_xkomv = xkomv.
"save actual XKOMV-entry
counter = 0.
LOOP AT xkomv WHERE kntyp CA '1234'
AND kappl = 'TX'.
CASE xkomv-kntyp.
WHEN '1'.
amount = xkomv-kwert.
xcom_tax-taxamt1 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct1 = quantity.
WHEN '2'.
amount = xkomv-kwert.
xcom_tax-taxamt2 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct2 = quantity.
WHEN '3'.
amount = xkomv-kwert.
xcom_tax-taxamt3 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct3 = quantity.
WHEN '4'.
counter = counter + 1.
IF counter = 1.
amount = xkomv-kwert.
xcom_tax-taxamt4 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct4 = quantity.
ELSEIF counter = 2.
amount = xkomv-kwert.
xcom_tax-taxamt5 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct5 = quantity.
ELSEIF counter = 3.
amount = xkomv-kwert.
xcom_tax-taxamt6 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct6 = quantity.
ELSEIF counter = 4.
amount = xkomv-kwert.
xcom_tax-taxamt7 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct7 = quantity.
ELSEIF counter = 5.
amount = xkomv-kwert.
xcom_tax-taxamt8 = amount.
quantity = xkomv-kbetr.
xcom_tax-taxpct8 = quantity.
ELSEIF counter = 6.
amount = xkomv-kwert.
xcom_tax-taxamt9 = amount.

quantity = xkomv-kbetr.
xcom_tax-taxpct9 = quantity.
ENDIF.
ENDCASE.
CLEAR: amount, quantity.
ENDLOOP.
xkomv = save_xkomv.
"reset XKOMV entry
ENDIF.
* End of Pricing Copy Logic
posnr = komp-kposn.
* Initialize MWSKZ
IF xkomv-mwsk1 IS INITIAL.
mwskz = komp-mwskz.
ELSE.
mwskz = xkomv-mwsk1.
ENDIF.

"MWSKZ aus Position (MM/FI)


"MWSKZ aus Kondition (SD)

* Tax Exemptions
SELECT SINGLE * FROM t007a WHERE kalsm
AND mwskz
IF t007a-txrel = 2.
"
* in case of tax exempt customers skip the
exempt_flag = 'X'.
ENDIF.

= t005-kalsm
= mwskz.
Tax Exempt
call to ext. package

* Initialize TTXD
ON CHANGE OF t005-kalsm.
SELECT SINGLE * FROM ttxd WHERE kalsm = t005-kalsm.
CHECK sy-subrc = 0 AND NOT ttxd-xextn IS INITIAL.
ENDON.
* Read T007A
SELECT SINGLE * FROM t007a WHERE kalsm = t005-kalsm
AND mwskz = mwskz.
IF sy-subrc NE 0.
MESSAGE s873(fs) WITH t005-kalsm mwskz.
komp-prsok = space.
xkomv-kinak = 'X'.
xkomv-fxmsg = '899'.
CALL FUNCTION 'SET_ERROR_FLAG'.
ENDIF.
* Populate COM_TAX
xcom_tax-client
= sy-mandt.
" Client
xcom_tax-comp_code = komk-bukrs.
" Company Code
xcom_tax-country
= komk-aland.
" Country for Tax Determinatio
xcom_tax-doc_number = komk-belnr.
" Document Reference Number
xcom_tax-txjcd_l1 = ttxd-leng1.
" Length of the 1th part of Ju
xcom_tax-txjcd_l2 = ttxd-leng2.
" Length of the 2th part of Ju
xcom_tax-txjcd_l3 = ttxd-leng3.
" Length of the 3th part of Ju
xcom_tax-txjcd_l4 = ttxd-leng4.
" Length of the 4th part of Ju
xcom_tax-matnr
= komp-matnr.
" Material
xcom_tax-currency = komk-waerk.
" Currency Key
* MGLME + MEINS belong together / also in LTAX1U02 and LTAX1U07
xcom_tax-unit
= komp-meins.
IF xcom_tax-unit IS INITIAL.
xcom_tax-unit = komp-vrkme.
ENDIF.

amount = xkomv-kawrt.
IF NOT xkomv-kstat IS INITIAL OR xkomv-kschl IS INITIAL.
IF amount IS INITIAL.
amount = xkomv-kwert.
ENDIF.
ENDIF.
xcom_tax-amount
= amount.
" Gross Amount
quantity = komp-mglme.
xcom_tax-quantity = quantity.

" Quantity

IF komp-kposn <> '999999'.


xcom_tax-pos_no = komp-kposn.
ELSE.
IF komk-versi IS INITIAL.
xcom_tax-pos_no = space.
ELSE.
xcom_tax-pos_no = komk-versi.
posnr(3) = komk-versi.
posnr+3(3) = '999'.
ENDIF.
ENDIF.
IF xkomv-kappl(1) = 'V'.
assign_default tax_date xkomv-kdatu komk-prsdt.
ELSE.
assign_default tax_date komk-fbuda komk-prsdt.
ENDIF.

"v_n_596011
"^_n_596011
"n_596011

default_on_initial tax_date sy-datum.


* Determine decimals for currency
CLEAR tcurx.
SELECT SINGLE * FROM tcurx WHERE currkey = xcom_tax-currency.
assign_default curr_dec tcurx-currdec '002'.
* Determine Jurisdictioncodes for tax calculation
plant = t001w-werks.
IF plant IS INITIAL.
plant = komp-werks.
ENDIF.
CASE t007a-mwart.
WHEN 'A'.
* A/R taxes
xcom_tax-accnt_no = komk-kunnr." Customer Account Number
xcom_tax-apar_ind = 'A'.
SELECT SINGLE * FROM t001w WHERE werks = plant.
IF sy-subrc = 0.
MOVE: t001w-txjcd TO xcom_tax-txjcd_sf.
ELSE.
MOVE: komk-txjcd TO xcom_tax-txjcd_sf.
ENDIF.
default_on_initial txjcd_sf komk-txjcd.
MOVE: komk-txjcd TO xcom_tax-txjcd_st.
* fill jurisdiction-code for foreign customers
IF komk-kunnr <> space.
CALL FUNCTION 'J_1BFOREIGN_JURISDICTION'
EXPORTING
br_country
= komk-aland

foreign_country
= komk-land1
CHANGING
tax_jcd
= xcom_tax-txjcd_st
EXCEPTIONS
foreign_region_not_found = 4.
IF sy-subrc = 4.
MESSAGE s316(8b) WITH komk-land1
RAISING tax_determination_error.
EXIT.
ENDIF.
* ISS 2004, Note 766464
* Fill tax jurisdiction code of service recipient with customer's one
*
Read customer data
IF NOT komk-kunnr IS INITIAL.
"773600
CALL FUNCTION 'V_KNA1_SINGLE_READ'
EXPORTING
pi_kunnr = komk-kunnr
IMPORTING
pe_kna1 = ls_kna1
EXCEPTIONS
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
xcom_tax-txjcd_poo = ls_kna1-txjcd.
ENDIF.

"773600

ENDIF.
WHEN 'V'.
* A/P taxes
xcom_tax-accnt_no = komk-lifnr." Vendor Account Number
xcom_tax-apar_ind = 'V'.
CALL FUNCTION 'WY_LFA1_SINGLE_READ'
"new performance
EXPORTING
pi_lifnr
= komk-lifnr
IMPORTING
po_lfa1
= lfa1
EXCEPTIONS
no_records_found = 1
OTHERS
= 2.
IF sy-subrc = 0.
MOVE: lfa1-txjcd TO xcom_tax-txjcd_sf.
ELSE.
MOVE: komk-txjcd TO xcom_tax-txjcd_sf.
ENDIF.
default_on_initial txjcd_sf komk-txjcd.
MOVE: komk-txjcd TO xcom_tax-txjcd_st.
* fill jurisdiction-code for foreign vendors
IF komk-lifnr <> space.
CALL FUNCTION 'J_1BFOREIGN_JURISDICTION'
EXPORTING
br_country
= komk-aland
foreign_country
= lfa1-land1
CHANGING
tax_jcd
= xcom_tax-txjcd_sf
EXCEPTIONS
foreign_region_not_found = 4.

IF sy-subrc = 4.
MESSAGE s316(8b) WITH lfa1-land1
RAISING tax_determination_error.
EXIT.
ENDIF.
ENDIF.
* ISS 2004, Note 766464
* Fill tax jurisdiction code of service recipient with plant's one
*
Read plant data
IF NOT plant IS INITIAL.

"773600

CALL FUNCTION 'AIP01_PLANT_DETERMINE'


EXPORTING
i_werks = plant
IMPORTING
es_t001w = ls_t001w
EXCEPTIONS
OTHERS = 6.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
xcom_tax-txjcd_poo = ls_t001w-txjcd.
ENDIF.
WHEN OTHERS.
MESSAGE s874(fs) WITH t007a-mwart.
komp-prsok = space.
xkomv-kinak = 'X'.
xkomv-fxmsg = '899'.
CALL FUNCTION 'SET_ERROR_FLAG'.
EXIT.
ENDCASE.
* check tax jurisdictions
IF xcom_tax-txjcd_sf IS INITIAL.
MESSAGE s882(fs) RAISING tax_determination_error.
ENDIF.
IF xcom_tax-txjcd_st IS INITIAL.
MESSAGE s883(fs) RAISING tax_determination_error.
ENDIF.
* reset calculation buffer of brazil tax calculation
CALL FUNCTION 'J_1BNF_TAX_DATA_RESET_PART'
EXPORTING
knumv = komk-knumv
kposn = komp-kposn
buzei = komk-versi.
* do the calculation, if required
IF xcom_tax-amount NE 0 AND exempt_flag <> 'X'.
IF komk-hwaer = space.
komk-hwaer = t001-waers.
ENDIF.

"773600

* Determine DESTINATION for external system


ON CHANGE OF ttxd-xextn.
SELECT SINGLE * FROM ttxc WHERE xextn = ttxd-xextn
AND event = 'TAX'.
IF sy-subrc <> 0.
RAISE rfcdest_not_found.
ELSE.
TRANSLATE ttxc-rfcdest TO UPPER CASE.
"#EC SYNTCHAR
IF ttxc-funcname IS INITIAL.
ttxc-funcname = 'RFC_CALCULATE_TAXES'.
ENDIF.
ENDIF.
ENDON.
* Let the external system do the calculation
CLEAR: xcom_tax-taxamt1, xcom_tax-taxamt2,
xcom_tax-taxamt4, xcom_tax-taxamt5,
xcom_tax-taxamt7, xcom_tax-taxamt8,
xcom_tax-taxamov, xcom_tax-taxamt0.
CLEAR: xcom_tax-taxpct1, xcom_tax-taxpct2,
xcom_tax-taxpct4, xcom_tax-taxpct5,
xcom_tax-taxpct7, xcom_tax-taxpct8,
xcom_tax-taxpcov, xcom_tax-taxpct0.

xcom_tax-taxamt3,
xcom_tax-taxamt6,
xcom_tax-taxamt9,
xcom_tax-taxpct3,
xcom_tax-taxpct6,
xcom_tax-taxpct9,

* fill parameters for the calculation


br_komv = xkomv.
REFRESH xxkomv.
save_xkomv = xkomv.
LOOP AT xkomv.
xxkomv = xkomv.
APPEND xxkomv.
ENDLOOP.
xkomv = save_xkomv.
* fill BR_KONP
REFRESH br_konp.
LOOP AT xkonp.
br_konp = xkonp-konp.
APPEND br_konp.
ENDLOOP.
CALL FUNCTION
EXPORTING
i_komk =
i_komp =
i_t007a =
i_komv =
TABLES
t_komv =
t_konp =

'J_1BCOMPLETE_TAX_DATA'
komk
komp
t007a
br_komv
xxkomv
br_konp.

* process external tax calculation


CALL FUNCTION ttxc-funcname
DESTINATION ttxc-rfcdest
EXPORTING
tax_data
= xcom_tax
IMPORTING
tax_result
= xcom_tax
tax_err
= xcom_err

"#EC ENHOK
"#EC ENHOK
"#EC ENHOK

EXCEPTIONS
communication_failure = 01
system_error
= 02.
IF sy-subrc NE 0.
MESSAGE s870(fs).
komp-prsok = space.
xkomv-kinak = 'X'.
xkomv-fxmsg = '899'.
CALL FUNCTION 'SET_ERROR_FLAG'.
EXIT.
ELSEIF xcom_err-retcode <> 0.
MESSAGE s872(fs) WITH xcom_err-errcode xcom_err-errmsg.
komp-prsok = space.
xkomv-kinak = 'X'.
xkomv-fxmsg = '899'.
CALL FUNCTION 'SET_ERROR_FLAG'.
EXIT.
ENDIF.
ELSE.
" gross amount is zero
xcom_tax-taxamt0
= 0.
xcom_tax-taxamt1
= 0.
xcom_tax-taxamt2
= 0.
xcom_tax-taxamt3
= 0.
xcom_tax-taxamt4
= 0.
xcom_tax-taxamt5
= 0.
xcom_tax-taxamt6
= 0.
xcom_tax-taxamt7
= 0.
xcom_tax-taxamt8
= 0.
xcom_tax-taxamt9
= 0.
xcom_tax-taxpct0
= 0.
xcom_tax-taxpct1
= 0.
xcom_tax-taxpct2
= 0.
xcom_tax-taxpct3
= 0.
xcom_tax-taxpct4
= 0.
xcom_tax-taxpct5
= 0.
xcom_tax-taxpct6
= 0.
xcom_tax-taxpct7
= 0.
xcom_tax-taxpct8
= 0.
xcom_tax-taxpct9
= 0.
CALL FUNCTION 'J_1B_CLEAR_GS_COMTAX'.
ENDIF.
* Fill global
taxamt0 =
taxamt1 =
taxamt2 =
taxamt3 =
taxamt4 =
taxamt5 =
taxamt6 =
taxamt7 =
taxamt8 =
taxamt9 =

workfields from internal structure


xcom_tax-taxamt0.
xcom_tax-taxamt1.
xcom_tax-taxamt2.
xcom_tax-taxamt3.
xcom_tax-taxamt4.
xcom_tax-taxamt5.
xcom_tax-taxamt6.
xcom_tax-taxamt7.
xcom_tax-taxamt8.
xcom_tax-taxamt9.

taxpct0 = xcom_tax-taxpct0.
taxpct1 = xcom_tax-taxpct1.
taxpct2 = xcom_tax-taxpct2.

taxpct3
taxpct4
taxpct5
taxpct6
taxpct7
taxpct8
taxpct9

=
=
=
=
=
=
=

xcom_tax-taxpct3.
xcom_tax-taxpct4.
xcom_tax-taxpct5.
xcom_tax-taxpct6.
xcom_tax-taxpct7.
xcom_tax-taxpct8.
xcom_tax-taxpct9.

taxbas1
taxbas2
taxbas3
taxbas4
taxbas5
taxbas6
taxbas7
taxbas8
taxbas9

=
=
=
=
=
=
=
=
=

xcom_tax-taxbas1.
xcom_tax-taxbas2.
xcom_tax-taxbas3.
xcom_tax-taxbas4.
xcom_tax-taxbas5.
xcom_tax-taxbas6.
xcom_tax-taxbas7.
xcom_tax-taxbas8.
xcom_tax-taxbas9.

ENDIF.
*} INSERT
ENDFORM.

You might also like