Skip to content

Commit

Permalink
trabalho em andamento
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniospneto committed Dec 17, 2024
1 parent 7593d8e commit 664b340
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 174 deletions.
44 changes: 20 additions & 24 deletions l10n_br_account/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,11 @@ def _get_view(self, view_id=None, view_type="form", **options):
"line_ids.full_reconcile_id",
"state",
"ind_final",
"fiscal_operation_id",
)
def _compute_amount(self):
for move in self.filtered(lambda m: m.fiscal_operation_id):
move._compute_fiscal_amount()
for line in move.line_ids:
if (
move.is_invoice(include_receipts=True)
Expand All @@ -300,6 +302,7 @@ def _compute_amount(self):
sign = 1
inv_line_ids = move.line_ids.filtered(
lambda line: line.display_type == "product"
and (not line.cfop_id or line.cfop_id.finance_move)
)
move.amount_untaxed = sum(inv_line_ids.mapped("amount_untaxed"))
move.amount_tax = sum(inv_line_ids.mapped("amount_tax"))
Expand All @@ -321,9 +324,14 @@ def _compute_needed_terms(self):
"""
Similar to the _compute_needed_terms super method in the account module,
but ensure moves are balanced in Brazil when there is a fiscal_operation_id.
WARNING: it seems we might not be able to call the super method here....
"""
for invoice in self:
res = None
invoices_with_fiscal_op = self.filtered(lambda inv: inv.fiscal_operation_id)
invoices_without_fiscal_op = self - invoices_with_fiscal_op
if invoices_without_fiscal_op:
res = super(AccountMove, invoices_without_fiscal_op)._compute_needed_terms()

for invoice in invoices_with_fiscal_op:
is_draft = invoice.id != invoice._origin.id
invoice.needed_terms = {}
invoice.needed_terms_dirty = True
Expand All @@ -338,31 +346,18 @@ def _compute_needed_terms(self):
pass
else:
untaxed_amount_currency += line.price_subtotal
for tax_result in (line.compute_all_tax or {}).values():
tax_amount_currency += -sign * tax_result.get(
"amount_currency", 0.0
)
for tax_result in (line.compute_all_tax or {}).values():
tax_amount_currency += -sign * tax_result.get(
"amount_currency", 0.0
)
untaxed_amount = untaxed_amount_currency
tax_amount = tax_amount_currency
else:
tax_amount_currency = invoice.amount_tax * sign
tax_amount = invoice.amount_tax_signed
if invoice.fiscal_operation_id:
if invoice.fiscal_operation_id.deductible_taxes:
amount_currency = (
invoice.amount_total
+ invoice.amount_tax_withholding
)
else:
amount_currency = (
invoice.amount_total - invoice.amount_ipi_value
) * sign
untaxed_amount_currency = amount_currency * sign
untaxed_amount = amount_currency * sign

else:
untaxed_amount_currency = invoice.amount_untaxed * sign
untaxed_amount = invoice.amount_untaxed_signed
tax_amount_currency = tax_amount = 0.0
# TODO forçando o compute
# invoice._compute_amount()
untaxed_amount_currency = invoice.amount_financial_total * sign
untaxed_amount = invoice.amount_financial_total * sign
invoice_payment_terms = (
invoice.invoice_payment_term_id._compute_terms(
date_ref=invoice.invoice_date
Expand Down Expand Up @@ -418,6 +413,7 @@ def _compute_needed_terms(self):
"balance": invoice.amount_total_signed,
"amount_currency": invoice.amount_total_in_currency_signed,
}
return res

@contextmanager
def _sync_dynamic_lines(self, container):
Expand Down
74 changes: 56 additions & 18 deletions l10n_br_account/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,20 @@ def changed(fname):
else: # BRAZIL CASE:
if line.cfop_id and not line.cfop_id.finance_move:
unsigned_amount_currency = 0
if not line.move_id.fiscal_operation_id.deductible_taxes:
# Quando não há financeiro, mas há imposto, e não há
# dedutíveis, é necessário registrar a contrapartida dos
# impostos para equilibrar o balanço. Na versão 14, essa
# diferença era automaticamente alocada às contas dos
# termos de pagamento.
# TODO:
# o correto mesmo seria não lançar os impostos nesse caso,
# resolver depois.
unsigned_amount_currency = -(
line.amount_tax_included
+ line.amount_tax_not_included
- line.amount_tax_withholding
)
else:
if line.move_id.fiscal_operation_id.deductible_taxes:
unsigned_amount_currency = (
Expand All @@ -312,19 +326,30 @@ def changed(fname):
)
unsigned_amount_currency = line.currency_id.round(
amount_total
- (
line.amount_tax_included
- line.amount_tax_withholding
)
- line.amount_tax_included
- line.amount_tax_not_included
- line.icms_relief_value
if line.tax_ids
else amount_total
)
amount_currency = unsigned_amount_currency * line.move_id.direction_sign
if line.amount_currency != amount_currency or line not in before:
line.amount_currency = amount_currency
if line.currency_id == line.company_id.currency_id:
line.balance = amount_currency

# Os totais nas linhas foram atualizadas, mas o total da fatura
# não foi recalculado automaticamente, já que o método compute_amount
# não foi acionado após as alterações nas linhas.
# Por esse motivo, estou adicionando manualmente os campos no
# add_to_compute do account_move.
# Questão: Por que o compute_amount não foi acionado automaticamente?
# Isso ocorre apenas quando os valores são diretamente informados
# no create? Realizar um teste isolado para confirmar esse
# comportamento.
move_id = line.move_id
self.env.add_to_compute(move_id._fields["amount_total"], move_id)
self.env.add_to_compute(move_id._fields["amount_untaxed"], move_id)

after = existing()
for line in after:
if (
Expand Down Expand Up @@ -394,7 +419,6 @@ def _compute_totals(self):

line.price_subtotal = taxes_res["total_excluded"]
line.price_total = taxes_res["total_included"]
line._compute_balance()

line.price_total += (
line.insurance_value
Expand Down Expand Up @@ -521,19 +545,33 @@ def _onchange_fiscal_document_line_id(self):
# override the default product uom (set by the onchange):
self.product_uom_id = self.fiscal_document_line_id.uom_id.id

@api.onchange("fiscal_tax_ids")
def _onchange_fiscal_tax_ids(self):
"""Ao alterar o campo fiscal_tax_ids que contém os impostos fiscais,
são atualizados os impostos contábeis relacionados"""
result = super()._onchange_fiscal_tax_ids()
@api.depends("product_id", "product_uom_id", "fiscal_tax_ids")
def _compute_tax_ids(self):
# Adding 'fiscal_tax_ids' as a dependency to ensure that the taxes
# are recalculated when this field changes.
return super()._compute_tax_ids()

def _get_computed_taxes(self):
"""
Override the native method to load taxes from the fiscal module.
"""
self.ensure_one()

# Atualiza os impostos contábeis relacionados aos impostos fiscais
user_type = "sale"
if self.move_id.move_type in ("in_invoice", "in_refund"):
# If no fiscal operation is defined, fallback to the default implementation.
if not self.fiscal_operation_id:
return super()._get_computed_taxes()

# Determine the user type based on the document type.
user_type = None
if self.move_id.is_sale_document(include_receipts=True):
user_type = "sale"
elif self.move_id.is_purchase_document(include_receipts=True):
user_type = "purchase"

self.tax_ids = self.fiscal_tax_ids.account_taxes(
user_type=user_type, fiscal_operation=self.fiscal_operation_id
)
# Retrieve taxes based on user type and fiscal operation.
if user_type:
tax_ids = self.fiscal_tax_ids.account_taxes(
user_type=user_type, fiscal_operation=self.fiscal_operation_id
)

return result
return tax_ids
32 changes: 17 additions & 15 deletions l10n_br_account/tests/test_account_move_lc.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def test_venda_with_icms_reduction_with_relief(self):
# Testando com Alivio do ICMS
self.move_out_venda_with_icms_reduction.invoice_line_ids[0].icms_relief_id = 1
self.move_out_venda_with_icms_reduction.invoice_line_ids._onchange_fiscal_taxes()
self.move_out_venda_with_icms_reduction.line_ids._compute_amounts()
self.move_out_venda_with_icms_reduction.line_ids._compute_fiscal_amounts()

product_line_vals_1 = {
"name": self.product_a.display_name,
Expand Down Expand Up @@ -666,8 +666,8 @@ def test_simples_remessa(self):
"price_total": 1050.0,
"tax_line_id": False,
"currency_id": self.company_data["currency"].id,
"amount_currency": 0.0,
"debit": 0.0,
"amount_currency": 206.5,
"debit": 206.5,
"credit": 0.0,
"date_maturity": False,
}
Expand Down Expand Up @@ -775,6 +775,8 @@ def test_simples_remessa(self):
"date_maturity": False,
}

# Remessa não gera financeiro, as linhas das condições de pagamento
# devem estar zeradas!
term_line_vals_1 = {
"name": "",
"product_id": False,
Expand All @@ -789,8 +791,8 @@ def test_simples_remessa(self):
"tax_ids": [],
"tax_line_id": False,
"currency_id": self.company_data["currency"].id,
"amount_currency": 206.5,
"debit": 206.5,
"amount_currency": 0,
"debit": 0,
"credit": 0.0,
"date_maturity": fields.Date.from_string("2019-01-01"),
}
Expand All @@ -803,9 +805,9 @@ def test_simples_remessa(self):
"fiscal_position_id": False,
"payment_reference": "",
"invoice_payment_term_id": self.pay_terms_a.id,
"amount_untaxed": 1000,
"amount_tax": 50,
"amount_total": 206.5,
"amount_untaxed": 0.0,
"amount_tax": 0.0,
"amount_total": 0.0,
}

self.assertInvoiceValues(
Expand Down Expand Up @@ -1271,8 +1273,8 @@ def test_simples_remessa_tax_withholding(self):
"price_total": 1050.0,
"tax_line_id": False,
"currency_id": self.company_data["currency"].id,
"amount_currency": 0.0,
"debit": 0.0,
"amount_currency": 133.5,
"debit": 133.5,
"credit": 0.0,
"date_maturity": False,
}
Expand Down Expand Up @@ -1390,8 +1392,8 @@ def test_simples_remessa_tax_withholding(self):
"tax_ids": [],
"tax_line_id": False,
"currency_id": self.company_data["currency"].id,
"amount_currency": 133.5,
"debit": 133.5,
"amount_currency": 0.0,
"debit": 0.0,
"credit": 0.0,
"date_maturity": fields.Date.from_string("2019-01-01"),
}
Expand All @@ -1404,9 +1406,9 @@ def test_simples_remessa_tax_withholding(self):
"fiscal_position_id": False,
"payment_reference": "",
"invoice_payment_term_id": self.pay_terms_a.id,
"amount_untaxed": 1000.0, # FIXME is this correct for a simples remessa??
"amount_tax": 50.0,
"amount_total": 133.5,
"amount_untaxed": 0.0,
"amount_tax": 0.0,
"amount_total": 0.0,
}

self.assertInvoiceValues(
Expand Down
35 changes: 18 additions & 17 deletions l10n_br_account/tests/test_invoice_refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,19 @@ def test_refund(self):
with self.assertRaises(UserError):
move_reversal.reverse_moves()

invoice["fiscal_operation_id"] = (self.env.ref("l10n_br_fiscal.fo_venda").id,)
invoice.fiscal_operation_id = self.env.ref("l10n_br_fiscal.fo_venda")

with self.assertRaises(UserError):
move_reversal.reverse_moves()

for line_id in invoice.invoice_line_ids:
line_id["fiscal_operation_id"] = (
self.env.ref("l10n_br_fiscal.fo_venda").id,
)
line_id["fiscal_operation_line_id"] = self.env.ref(
"l10n_br_fiscal.fo_venda_venda"
).id
invoice.invoice_line_ids.write(
{
"fiscal_operation_id": self.env.ref("l10n_br_fiscal.fo_venda").id,
"fiscal_operation_line_id": (
self.env.ref("l10n_br_fiscal.fo_venda_venda").id,
),
}
)

reversal = move_reversal.reverse_moves()
reverse_move = self.env["account.move"].browse(reversal["res_id"])
Expand All @@ -137,15 +138,15 @@ def test_refund_force_fiscal_operation(self):
reverse_vals = self.reverse_vals
invoice = self.invoice

invoice["fiscal_operation_id"] = (self.env.ref("l10n_br_fiscal.fo_venda").id,)

for line_id in invoice.invoice_line_ids:
line_id["fiscal_operation_id"] = (
self.env.ref("l10n_br_fiscal.fo_venda").id,
)
line_id["fiscal_operation_line_id"] = self.env.ref(
"l10n_br_fiscal.fo_venda_venda"
).id
invoice.fiscal_operation_id = self.env.ref("l10n_br_fiscal.fo_venda")
invoice.invoice_line_ids.write(
{
"fiscal_operation_id": self.env.ref("l10n_br_fiscal.fo_venda").id,
"fiscal_operation_line_id": self.env.ref(
"l10n_br_fiscal.fo_venda_venda"
).id,
}
)

invoice.action_post()
self.assertEqual(
Expand Down
5 changes: 3 additions & 2 deletions l10n_br_fiscal/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def _compute_name(self):
r.name = r._compute_document_name()

@api.depends(
"fiscal_line_ids",
"fiscal_line_ids.estimate_tax",
"fiscal_line_ids.price_gross",
"fiscal_line_ids.amount_untaxed",
Expand All @@ -350,8 +351,8 @@ def _compute_name(self):
"fiscal_line_ids.amount_tax_not_included",
"fiscal_line_ids.amount_tax_withholding",
)
def _compute_amount(self):
return super()._compute_amount()
def _compute_fiscal_amount(self):
return super()._compute_fiscal_amount()

@api.model_create_multi
def create(self, vals_list):
Expand Down
Loading

0 comments on commit 664b340

Please sign in to comment.