Skip to content

Commit

Permalink
[IMP] l10n_br_fiscal: add icms difal regulation
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemotter authored and antoniospneto committed Jul 14, 2023
1 parent fac1765 commit ad726e8
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 41 deletions.
1 change: 1 addition & 0 deletions l10n_br_fiscal/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"data/res_partner_data.xml",
"data/l10n_br_fiscal.tax.group.csv",
"data/l10n_br_fiscal.icms.relief.csv",
"data/l10n_br_fiscal_icms_difal_definition_data.xml",
"data/l10n_br_fiscal.document.type.csv",
"data/l10n_br_fiscal.product.genre.csv",
"data/l10n_br_fiscal.cst.csv",
Expand Down
35 changes: 0 additions & 35 deletions l10n_br_fiscal/constants/icms.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,4 @@
}


ICMS_DIFAL_UNIQUE_BASE = [
"DF",
"ES",
"MA",
"MS",
"PE",
"RJ",
"RN",
"RR",
]


ICMS_DIFAL_DOUBLE_BASE = [
"AC",
"AL",
"AP",
"AM",
"BA",
"CE",
"GO",
"MG",
"MT",
"PA",
"PB",
"PI",
"PR",
"RO",
"RS",
"SC",
"SE",
"SP",
"TO",
]


ICSM_CST_CSOSN_ST_BASE = ["10", "30", "70", "90", "201", "202", "203", "900"]
39 changes: 39 additions & 0 deletions l10n_br_fiscal/data/l10n_br_fiscal_icms_difal_definition_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="True">
<record id="tax_icms_difal_regulation" model="l10n_br_fiscal.icms.difal.regulation">
<field name="name">Regulamento do ICMS Difal</field>
<field
name="unique_base_state_ids"
eval="[(6, 0, [
ref('base.state_br_ac'),
ref('base.state_br_al'),
ref('base.state_br_am'),
ref('base.state_br_ap'),
ref('base.state_br_ba'),
ref('base.state_br_ce'),
ref('base.state_br_df'),
ref('base.state_br_es'),
ref('base.state_br_go'),
ref('base.state_br_ma'),
ref('base.state_br_mg'),
ref('base.state_br_ms'),
ref('base.state_br_mt'),
ref('base.state_br_pa'),
ref('base.state_br_pb'),
ref('base.state_br_pe'),
ref('base.state_br_pi'),
ref('base.state_br_pr'),
ref('base.state_br_rj'),
ref('base.state_br_rn'),
ref('base.state_br_ro'),
ref('base.state_br_rr'),
ref('base.state_br_rs'),
ref('base.state_br_sc'),
ref('base.state_br_se'),
ref('base.state_br_sp'),
ref('base.state_br_to')
])]"
/>
</record>

</odoo>
1 change: 1 addition & 0 deletions l10n_br_fiscal/demo/company_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<field name="is_industry" eval="True" />
<field name="ripi" eval="True" />
<field name="icms_regulation_id" ref="tax_icms_regulation" />
<field name="icms_difal_regulation_id" ref="tax_icms_difal_regulation" />
<field name="cnae_main_id" ref="l10n_br_fiscal.cnae_3101200" />
<field name="piscofins_id" ref="l10n_br_fiscal.tax_pis_cofins_columativo" />
<field name="document_type_id" ref="l10n_br_fiscal.document_55" />
Expand Down
1 change: 1 addition & 0 deletions l10n_br_fiscal/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from . import tax_definition_partner_profile
from . import icms_regulation
from . import icms_relief
from . import icms_difal_regulation
from . import document_type
from . import document_serie
from . import product_genre
Expand Down
37 changes: 37 additions & 0 deletions l10n_br_fiscal/models/icms_difal_regulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (C) 2023 Felipe Motter Pereira - Akretion <[email protected]>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import _, api, fields, models
from odoo.exceptions import UserError


class ICMSDifalRegulation(models.Model):
_name = "l10n_br_fiscal.icms.difal.regulation"
_description = "ICMS Difal Regulation"

name = fields.Text(required=True, index=True)

unique_base_state_ids = fields.Many2many(
comodel_name="res.country.state",
relation="icms_difal_regulation_unique_base_state_rel",
column1="icms_difal_regulation",
column2="state_id",
string="States with Unique Base",
domain=[("country_id.code", "=", "BR")],
)

double_base_state_ids = fields.Many2many(
comodel_name="res.country.state",
relation="icms_difal_regulation_double_base_state_rel",
column1="icms_difal_regulation",
column2="state_id",
string="States with Double Base",
domain=[("country_id.code", "=", "BR")],
)

@api.constrains("unique_base_state_ids", "double_base_state_ids")
def _check_duplicity(self):
for state in self.unique_base_state_ids:
if state in self.double_base_state_ids:
raise UserError(_("You cannot have two bases for same state."))
return True
5 changes: 5 additions & 0 deletions l10n_br_fiscal/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ def _compute_simplified_tax(self):
comodel_name="l10n_br_fiscal.icms.regulation", string="ICMS Regulation"
)

icms_difal_regulation_id = fields.Many2one(
comodel_name="l10n_br_fiscal.icms.difal.regulation",
string="ICMS Difal Regulation",
)

tax_issqn_id = fields.Many2one(
comodel_name="l10n_br_fiscal.tax",
string="Default ISSQN",
Expand Down
28 changes: 22 additions & 6 deletions l10n_br_fiscal/models/tax.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright (C) 2013 Renato Lima - Akretion
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tools import float_is_zero

from ..constants.fiscal import (
Expand All @@ -21,9 +22,7 @@
from ..constants.icms import (
ICMS_BASE_TYPE,
ICMS_BASE_TYPE_DEFAULT,
ICMS_DIFAL_DOUBLE_BASE,
ICMS_DIFAL_PARTITION,
ICMS_DIFAL_UNIQUE_BASE,
ICMS_ORIGIN_TAX_IMPORTED,
ICMS_SN_CST_WITH_CREDIT,
ICMS_ST_BASE_TYPE,
Expand Down Expand Up @@ -388,7 +387,10 @@ def _compute_icms(self, tax, taxes_dict, **kwargs):
and partner.ind_ie_dest == NFE_IND_IE_DEST_9
and tax_dict.get("tax_value")
):
icms_tax_difal, _ = company.icms_regulation_id.map_tax_def_icms_difal(
(
icms_tax_difal,
tax_definitions,
) = company.icms_regulation_id.map_tax_def_icms_difal(
company, partner, product, ncm, nbm, cest, operation_line
)
icmsfcp_tax_difal = tax_dict_ipi = taxes_dict.get("icmsfcp", {})
Expand Down Expand Up @@ -416,10 +418,16 @@ def _compute_icms(self, tax, taxes_dict, **kwargs):
# Difal - ICMS Dest Value
icms_dest_value = currency.round(icms_base * (icms_dest_perc / 100))

if partner.state_id.code in ICMS_DIFAL_UNIQUE_BASE:
icms_difal_regulation = company.icms_difal_regulation_id
if not icms_difal_regulation:
raise UserError(
_("The company '%s' don't have a ICMS Difal Regulation defined.")
% (company.name)
)
if partner.state_id in icms_difal_regulation.unique_base_state_ids:
difal_icms_base = icms_base

if partner.state_id.code in ICMS_DIFAL_DOUBLE_BASE:
elif partner.state_id in icms_difal_regulation.double_base_state_ids:
difal_icms_base = currency.round(
(icms_base - icms_origin_value)
/ (1 - ((icms_dest_perc + icmsfcp_perc) / 100))
Expand All @@ -428,6 +436,14 @@ def _compute_icms(self, tax, taxes_dict, **kwargs):
icms_dest_value = currency.round(
difal_icms_base * (icms_dest_perc / 100)
)
else:
raise UserError(
_(
"The state of partner '%s' does not have a defined "
"base in the icms difal regulation."
)
% (partner.state_id.code)
)

difal_value = icms_dest_value - icms_origin_value

Expand Down
2 changes: 2 additions & 0 deletions l10n_br_fiscal/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"l10n_br_fiscal_cst_maintenance","Fiscal CST for Maintenance","model_l10n_br_fiscal_cst","l10n_br_fiscal.group_data_maintenance",1,1,1,1
"l10n_br_fiscal_tax_group_user","Fiscal Tax Group for User","model_l10n_br_fiscal_tax_group","l10n_br_fiscal.group_user",1,0,0,0
"l10n_br_fiscal_tax_group_manager","Fiscal Tax Group for Manager","model_l10n_br_fiscal_tax_group","l10n_br_fiscal.group_manager",1,1,1,1
"l10n_br_fiscal_icms_difal_regulation_user","Fiscal Tax ICMS Difal Regulation for User","model_l10n_br_fiscal_icms_difal_regulation","l10n_br_fiscal.group_user",1,0,0,0
"l10n_br_fiscal_icms_difal_regulation_manager","Fiscal Tax ICMS Difal Regulation for Manager","model_l10n_br_fiscal_icms_difal_regulation","l10n_br_fiscal.group_manager",1,1,1,1
"l10n_br_fiscal_icms_regulation_user","Fiscal Tax ICMS Regulation for User","model_l10n_br_fiscal_icms_regulation","l10n_br_fiscal.group_user",1,0,0,0
"l10n_br_fiscal_icms_regulation_manager","Fiscal Tax ICMS Regulation for Manager","model_l10n_br_fiscal_icms_regulation","l10n_br_fiscal.group_manager",1,1,1,1
"l10n_br_fiscal_icms_relief_user","Fiscal Tax ICMS Relief for User","model_l10n_br_fiscal_icms_relief","l10n_br_fiscal.group_user",1,0,0,0
Expand Down
43 changes: 43 additions & 0 deletions l10n_br_fiscal/tests/test_fiscal_tax.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2020 Akretion - Renato Lima <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import exceptions
from odoo.tests import SavepointCase
from odoo.tools import float_compare

Expand Down Expand Up @@ -303,3 +304,45 @@ def test_compute_taxes_03(self):
}

self._check_compute_taxes_result(test_result, compute_result, currency)

def test_difal(self):
"""Testa o calculo dos impostos de compra - entrada de importação"""

kwargs = self._create_compute_taxes_kwargs()
currency = kwargs["company"].currency_id

kwargs["partner"] = self.env.ref("l10n_br_base.res_partner_cliente10_mg")

fiscal_taxes = self.env["l10n_br_fiscal.tax"]
fiscal_taxes |= self.env.ref("l10n_br_fiscal.tax_icms_4")

difal_regulation = self.env.ref("l10n_br_fiscal.tax_icms_difal_regulation")
kwargs["company"].icms_difal_regulation_id = False

with self.assertRaises(exceptions.UserError):
fiscal_taxes.compute_taxes(**kwargs)

kwargs["company"].icms_difal_regulation_id = difal_regulation

compute_result = fiscal_taxes.compute_taxes(**kwargs)

test_result = {
"amount_included": 1.38,
"amount_not_included": 0.0,
"amount_withholding": 0.0,
"estimate_tax": 0.0,
"taxes": {
"icms": {
"icms_dest_base": 34.58,
"icms_dest_value": 4.84,
},
},
}

self._check_compute_taxes_result(test_result, compute_result, currency)

difal_regulation.unique_base_state_ids = [(5, 0, 0)]
difal_regulation.double_base_state_ids = [(5, 0, 0)]

with self.assertRaises(exceptions.UserError):
fiscal_taxes.compute_taxes(**kwargs)
14 changes: 14 additions & 0 deletions l10n_br_fiscal/tests/test_icms_regulation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from odoo.exceptions import UserError
from odoo.tests import SavepointCase, tagged

from ..constants.fiscal import FINAL_CUSTOMER_NO, FINAL_CUSTOMER_YES, TAX_DOMAIN_ICMS
Expand Down Expand Up @@ -71,3 +72,16 @@ def find_icms_tax(self, in_state_id, out_state_id, ncm_id, ind_final):
ind_final=ind_final,
)
return tax_icms.filtered(lambda t: t.tax_domain == TAX_DOMAIN_ICMS)

def test_state_difal_base_duplicity(self):

demo_state = self.env.ref("base.state_br_sc")

with self.assertRaises(UserError):
self.env["l10n_br_fiscal.icms.difal.regulation"].create(
{
"name": "Difal Test",
"unique_base_state_ids": [(4, demo_state.id, 0)],
"double_base_state_ids": [(4, demo_state.id, 0)],
}
)
16 changes: 16 additions & 0 deletions l10n_br_fiscal/views/l10n_br_fiscal_action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@
</field>
</record>

<!-- Tax ICMS Difal Regulation -->
<record id="tax_icms_difal_regulation_action" model="ir.actions.act_window">
<field name="name">ICMS Difal Regulation</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">l10n_br_fiscal.icms.difal.regulation</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Add a new ICMS Difal Regulation
</p><p>
A ICMS Difal Regulation is necessary to calc
ICMS Difal values on document lines.
</p>
</field>
</record>

<!-- Tax ICMS Regulation -->
<record id="tax_icms_regulation_action" model="ir.actions.act_window">
<field name="name">ICMS Regulation</field>
Expand Down
9 changes: 9 additions & 0 deletions l10n_br_fiscal/views/l10n_br_fiscal_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@
sequence="20"
/>

<!-- Tax ICMS Difal Regulation -->
<menuitem
id="tax_icms_difal_regulation_menu"
action="tax_icms_difal_regulation_action"
groups="l10n_br_fiscal.group_manager"
parent="tax_icms_config_menu"
sequence="30"
/>

<!-- Tax ISSQN Settings -->
<menuitem
id="tax_issqn_config_menu"
Expand Down
5 changes: 5 additions & 0 deletions l10n_br_fiscal/views/res_company_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
attrs="{'invisible': [('tax_framework', '!=', '3')], 'required': [('tax_framework', '=', '3')]}"
options="{'no_create': True, 'no_create_edit': True}"
/>
<field
name="icms_difal_regulation_id"
attrs="{'invisible': [('tax_framework', '!=', '3')], 'required': [('tax_framework', '=', '3')]}"
options="{'no_create': True, 'no_create_edit': True}"
/>
</group>
<group name="issqn_taxes" string="ISSQN">
<field name="tax_issqn_id" />
Expand Down

0 comments on commit ad726e8

Please sign in to comment.