Skip to content

Commit

Permalink
[IMP] l10n_br_fiscal: estimate tax without company
Browse files Browse the repository at this point in the history
  • Loading branch information
corredato committed Sep 19, 2024
1 parent d0d5492 commit 34d3b42
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 67 deletions.
63 changes: 24 additions & 39 deletions l10n_br_fiscal/models/data_ncm_nbs_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,32 @@ class DataNcmNbsAbstract(models.AbstractModel):
readonly=True,
)

estimate_tax_national = fields.Float(
string="Estimate Tax Nacional Percent",
store=True,
readonly=True,
digits="Fiscal Tax Percent",
compute="_compute_amount",
)

estimate_tax_imported = fields.Float(
string="Estimate Tax Imported Percent",
store=True,
readonly=True,
digits="Fiscal Tax Percent",
compute="_compute_amount",
)
def get_estimated_taxes(self):
self.ensure_one()
object_field = OBJECT_FIELDS.get(self._name)
last_estimated = self.env["l10n_br_fiscal.tax.estimate"].search(
[
(object_field, "=", self.id),
("state_id", "=", self.env.company.state_id.id),
],
order="create_date DESC",
limit=1,
)

@api.depends("tax_estimate_ids")
def _compute_amount(self):
for record in self:
object_field = OBJECT_FIELDS.get(record._name)
last_estimated = record.env["l10n_br_fiscal.tax.estimate"].search(
[
(object_field, "=", record.id),
("company_id", "=", record.env.company.id),
],
order="create_date DESC",
limit=1,
if last_estimated:
estimate_tax_imported = (
last_estimated.federal_taxes_import
+ last_estimated.state_taxes
+ last_estimated.municipal_taxes
)

if last_estimated:
record.estimate_tax_imported = (
last_estimated.federal_taxes_import
+ last_estimated.state_taxes
+ last_estimated.municipal_taxes
)

record.estimate_tax_national = (
last_estimated.federal_taxes_national
+ last_estimated.state_taxes
+ last_estimated.municipal_taxes
)
estimate_tax_national = (
last_estimated.federal_taxes_national
+ last_estimated.state_taxes
+ last_estimated.municipal_taxes
)
return estimate_tax_national, estimate_tax_imported
else:
return 0, 0

def _get_ibpt(self, config, code_unmasked):
return False
Expand Down
8 changes: 5 additions & 3 deletions l10n_br_fiscal/models/tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,19 @@ def _compute_estimate_taxes(self, **kwargs):
and op_line.fiscal_operation_id.fiscal_type == "sale"
):
if nbs:
estimate_tax_national, estimate_tax_imported = nbs.get_estimated_taxes()
amount_estimate_tax = currency.round(
amount_total * (nbs.estimate_tax_national / 100)
amount_total * (estimate_tax_national / 100)
)
elif ncm:
estimate_tax_national, estimate_tax_imported = ncm.get_estimated_taxes()
if icms_origin in ICMS_ORIGIN_TAX_IMPORTED:
amount_estimate_tax = currency.round(
amount_total * (ncm.estimate_tax_imported / 100)
amount_total * (estimate_tax_imported / 100)
)
else:
amount_estimate_tax = currency.round(
amount_total * (ncm.estimate_tax_national / 100)
amount_total * (estimate_tax_national / 100)
)

return amount_estimate_tax
Expand Down
26 changes: 20 additions & 6 deletions l10n_br_fiscal/models/tax_estimate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2012 Renato Lima - Akretion <[email protected]>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import fields, models
from odoo import api, fields, models


class TaxEstimate(models.Model):
Expand Down Expand Up @@ -43,8 +43,22 @@ class TaxEstimate(models.Model):

origin = fields.Char(string="Source", size=32)

company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
default=lambda self: self.env.company,
)
active = fields.Boolean(string="Ativo", default=True)

def _deactivate_old_estimates(self):
for estimate in self:
domain = [
("id", "!=", estimate.id),
("state_id", "=", estimate.state_id.id),
"|",
("ncm_id", "=", estimate.ncm_id.id),
("nbs_id", "=", estimate.nbs_id.id),
]
old_estimates = self.search(domain)
old_estimates.write({"active": False})

@api.model_create_multi
def create(self, vals_list):
estimates = super().create(vals_list)
estimates._deactivate_old_estimates()
return estimates
4 changes: 1 addition & 3 deletions l10n_br_fiscal/security/fiscal_security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
<field name="name">Fiscal Tax Estimate multi-company</field>
<field name="model_id" ref="model_l10n_br_fiscal_tax_estimate" />
<field eval="True" name="global" />
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
<field name="domain_force">[]</field>
</record>

<record id="l10n_br_fiscal_operation_line_rule" model="ir.rule">
Expand Down
8 changes: 0 additions & 8 deletions l10n_br_fiscal/views/nbs_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@
<field name="product_tmpl_ids" />
</page>
<page string="Estimate Taxes" name="tax_estimate">
<group>
<group>
<field name="estimate_tax_national" />
</group>
<group>
<field name="estimate_tax_imported" />
</group>
</group>
<field name="tax_estimate_ids" />
</page>
</notebook>
Expand Down
8 changes: 0 additions & 8 deletions l10n_br_fiscal/views/ncm_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@
<field name="piscofins_ids" />
</page>
<page string="Estimate Taxes" name="tax_estimate">
<group>
<group>
<field name="estimate_tax_national" />
</group>
<group>
<field name="estimate_tax_imported" />
</group>
</group>
<field name="tax_estimate_ids" />
</page>
</notebook>
Expand Down

0 comments on commit 34d3b42

Please sign in to comment.