Skip to content

Commit ee1a87b

Browse files
committed
[IMP] l10n_br_fiscal: tax docstring
1 parent e8f79d5 commit ee1a87b

File tree

1 file changed

+119
-39
lines changed

1 file changed

+119
-39
lines changed

l10n_br_fiscal/models/tax.py

Lines changed: 119 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,53 @@
6868

6969

7070
class Tax(models.Model):
71+
"""
72+
Represents a specific Brazilian fiscal tax (e.g., ICMS, IPI, PIS, COFINS)
73+
and acts as the core engine for calculating its value in a given context.
74+
75+
This model is distinct from Odoo's generic `account.tax`. Each record
76+
defines a particular tax, its domain (e.g., 'icms', 'ipi'), how its
77+
base is calculated (percentage, fixed value, per quantity), applicable
78+
rates, percentage reductions, and links to its Tax Group and default
79+
CST codes (Código de Situação Tributária).
80+
81+
Key Responsibilities:
82+
1. **Tax Definition**: Stores the parameters for a single fiscal tax,
83+
including its calculation method, rates (percent_amount,
84+
value_amount), base reduction (percent_reduction), and specific
85+
attributes for complex taxes like ICMS (e.g., icms_base_type,
86+
icmsst_base_type, icmsst_mva_percent).
87+
88+
2. **Tax Calculation Engine (`compute_taxes` method)**:
89+
The primary entry point for tax computation. When called on a
90+
recordset of `l10n_br_fiscal.tax` objects (representing all taxes
91+
potentially applicable to a transaction line), it iterates through
92+
them in a defined sequence. For each tax, it:
93+
a. Initializes a standard dictionary (`TAX_DICT_VALUES`) to hold
94+
results.
95+
b. Invokes a specialized internal method (e.g., `_compute_icms`,
96+
`_compute_ipi`, or the generic `_compute_tax`) to perform
97+
the actual calculation logic for that tax domain. These
98+
internal methods determine the tax base, apply reductions,
99+
and calculate the tax value.
100+
c. Handles inter-tax dependencies (e.g., IPI value affecting
101+
ICMS base).
102+
d. Aggregates results, including total tax included in price,
103+
tax not included, and withholding amounts.
104+
105+
3. **Contextual Adaptation**: The calculation methods
106+
(`_compute_<tax_domain>`) take numerous keyword arguments
107+
(`**kwargs`) representing the full fiscal context (company, partner,
108+
product, operation details, other calculated taxes, etc.) to ensure
109+
taxes are computed according to specific scenarios (e.g., interstate
110+
operations, final consumer, import/export).
111+
112+
This model, in conjunction with `l10n_br_fiscal.tax.definition` (which
113+
determines *which* of these `l10n_br_fiscal.tax` records apply),
114+
forms the heart of the Brazilian tax calculation system in this
115+
localization.
116+
"""
117+
71118
_name = "l10n_br_fiscal.tax"
72119
_order = "sequence, tax_domain, name"
73120
_description = "Fiscal Tax"
@@ -627,14 +674,20 @@ def _compute_ipi(self, tax, taxes_dict, **kwargs):
627674

628675
@api.model
629676
def _compute_tax_sequence(self, taxes_dict, **kwargs):
630-
"""Método para calcular a ordem que os impostos serão calculados.
631-
Por padrão é utilizado o campo compute_sequence do objeto para
632-
ordenar a sequencia que os impostos serão calculados.
633-
Por padrão é obdecida a seguinte sequencia:
677+
"""
678+
Method to determine the order in which taxes will be calculated.
634679
635-
compute_sequence = {
636-
tax_domain: compute_sequence,
680+
By default, the `compute_sequence` field of the tax object is used
681+
to sort the sequence for tax calculation. The default processing
682+
order is based on these `compute_sequence` values, conceptually
683+
like:
684+
685+
{
686+
'tax_domain_A': its_compute_sequence_value,
687+
'tax_domain_B': its_compute_sequence_value,
688+
# ... and so on for other tax domains
637689
}
690+
# Lower compute_sequence values are typically processed earlier.
638691
"""
639692
# Pega por padrão os valores do campo compute_sequence
640693
compute_sequence = {t.tax_domain: t.compute_sequence for t in self}
@@ -654,39 +707,66 @@ def _compute_tax_sequence(self, taxes_dict, **kwargs):
654707

655708
def compute_taxes(self, **kwargs):
656709
"""
657-
arguments:
658-
company,
659-
partner,
660-
product,
661-
price_unit,
662-
quantity,
663-
uom_id,
664-
fiscal_price,
665-
fiscal_quantity,
666-
uot_id,
667-
discount_value,
668-
insurance_value,
669-
other_value,
670-
freight_value,
671-
ii_customhouse_charges,
672-
ii_iof_value,
673-
ncm,
674-
nbs,
675-
nbm,
676-
cest,
677-
operation_line,
678-
cfop,
679-
icmssn_range,
680-
icms_origin,
681-
ind_final,
682-
return
683-
{
684-
'amount_included': float
685-
'amount_not_included': float
686-
'amount_withholding': float
687-
'taxes': dict
688-
}
710+
Computes all applicable Brazilian taxes based on a set of input parameters.
711+
712+
This method orchestrates the calculation of various taxes (ICMS, IPI, PIS,
713+
COFINS, ISSQN, etc.) by calling specialized internal _compute_<tax_domain>
714+
methods. It respects a defined computation sequence for taxes and handles
715+
interdependencies, such as IPI influencing the ICMS base under certain
716+
conditions.
717+
718+
The results include the calculated tax values for each domain, amounts
719+
included in the price, amounts not included, and withholding amounts.
720+
It also calculates an estimated total tax amount as per "Lei da Transparência".
721+
722+
:param company: res.company record of the emitting company.
723+
:param partner: res.partner record of the recipient/customer.
724+
:param product: product.product record being taxed.
725+
:param price_unit: float, the unit price of the product/service before
726+
discounts.
727+
:param quantity: float, the quantity of the product/service.
728+
:param uom_id: uom.uom record, unit of measure for the quantity.
729+
:param fiscal_price: float, the fiscal price unit (e.g., for tax
730+
calculation if different from commercial price).
731+
:param fiscal_quantity: float, the fiscal quantity (e.g., for tax
732+
calculation if different from commercial quantity).
733+
:param uot_id: uom.uom record, unit of taxation if different from uom_id.
734+
:param discount_value: float, total discount amount for the line.
735+
:param insurance_value: float, total insurance amount for the line.
736+
:param other_value: float, total other costs/fees for the line.
737+
:param freight_value: float, total freight amount for the line.
738+
:param ii_customhouse_charges: float, customs house charges for Import Tax
739+
(II).
740+
:param ii_iof_value: float, IOF value related to Import Tax (II).
741+
:param ncm: l10n_br_fiscal.ncm record, NCM code for the product.
742+
:param nbs: l10n_br_fiscal.nbs record, NBS code for the service.
743+
:param nbm: l10n_br_fiscal.nbm record, NBM code for the product.
744+
:param cest: l10n_br_fiscal.cest record, CEST code for the product.
745+
:param operation_line: l10n_br_fiscal.operation.line record defining
746+
the fiscal context.
747+
:param cfop: l10n_br_fiscal.cfop record, the determined CFOP for the
748+
operation.
749+
:param icmssn_range: l10n_br_fiscal.simplified.tax.range record for
750+
Simples Nacional ICMS calculation.
751+
:param icms_origin: str, ICMS origin code for the product.
752+
:param icms_cst_id: l10n_br_fiscal.cst record, the ICMS CST code.
753+
:param icms_relief_id: l10n_br_fiscal.icms.relief record, if ICMS relief
754+
applies.
755+
:param ind_final: str, indicates if the operation is for a final
756+
consumer ('0' or '1').
757+
758+
:return: dict containing:
759+
- 'amount_included': float, sum of tax values included in the price.
760+
- 'amount_not_included': float, sum of tax values not included in
761+
the price (added on top).
762+
- 'amount_withholding': float, sum of withholding tax values.
763+
- 'estimate_tax': float, estimated total tax amount for transparency.
764+
- 'taxes': dict, where keys are tax domains (e.g., 'icms', 'ipi')
765+
and values are dictionaries with detailed calculation results for
766+
that tax (base, percent, value, cst, etc., as defined in
767+
TAX_DICT_VALUES).
689768
"""
769+
690770
result_amounts = {
691771
"amount_included": 0.00,
692772
"amount_not_included": 0.00,
@@ -704,7 +784,7 @@ def compute_taxes(self, **kwargs):
704784
fiscal_operation_type = operation_line.fiscal_operation_type or FISCAL_OUT
705785
kwargs.update({"cst": tax.cst_from_tax(fiscal_operation_type)})
706786
try:
707-
compute_method = getattr(self, "_compute_%s" % tax.tax_domain)
787+
compute_method = getattr(self, f"_compute_{tax.tax_domain}")
708788
taxes[tax.tax_domain].update(compute_method(tax, taxes, **kwargs))
709789

710790
except AttributeError:

0 commit comments

Comments
 (0)