Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] account_avatax_oca, force compute hearder taxes after onchange triggered #446

Open
wants to merge 3 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions account_avatax_oca/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tests.common import Form
from odoo.tools import float_compare

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -236,9 +236,8 @@ def _avatax_compute_tax(self, commit=False):
if self.state == "draft":
Tax = self.env["account.tax"]
tax_result_lines = {int(x["lineNumber"]): x for x in tax_result["lines"]}
taxes_to_set = []
lines = self.invoice_line_ids
for index, line in enumerate(lines):
taxes_to_set = {}
for line in self.invoice_line_ids:
tax_result_line = tax_result_lines.get(line.id)
if tax_result_line:
# rate = tax_result_line.get("rate", 0.0)
Expand All @@ -252,26 +251,42 @@ def _avatax_compute_tax(self, commit=False):
tax = Tax.get_avalara_tax(rate, doc_type)
if tax and tax not in line.tax_ids:
line_taxes = line.tax_ids.filtered(lambda x: not x.is_avatax)
taxes_to_set.append((index, line_taxes | tax))
taxes_to_set[line.id] = line_taxes | tax
line.avatax_amt_line = tax_result_line["tax"]
self.with_context(check_move_validity=False).avatax_amount = tax_result[
"totalTax"
]
container = {"records": self}
self.with_context(
avatax_invoice=self, check_move_validity=False
)._sync_dynamic_lines(container)
self.line_ids.mapped("move_id")._check_balanced(container)

# Set Taxes on lines in a way that properly triggers onchanges
# This same approach is also used by the official account_taxcloud connector
with Form(self) as move_form:
for index, taxes in taxes_to_set:
with move_form.invoice_line_ids.edit(index) as line_form:
line_form.tax_ids.clear()
for tax in taxes:
line_form.tax_ids.add(tax)

with self.with_context(
avatax_invoice=self, check_move_validity=False
)._sync_dynamic_lines(container), self.line_ids.mapped(
"move_id"
)._check_balanced(
container
):
for line_id in taxes_to_set.keys():
line = self.invoice_line_ids.filtered(lambda x: x.id == line_id)
line.tax_ids.write({"tax_ids": [(6, 0, [])]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raising error, I'm not sure which it's the purpose, but I suggest remove this line or change it for line.write({"tax_ids": [(6, 0, [])]})

line.with_context(
avatax_invoice=self, check_move_validity=False
).write({"tax_ids": taxes_to_set.get(line_id).ids})
# After taxes are changed is needed to force compute taxes again, in 16 version
# change of tax doesn't trigger compute of taxes on header for unknown reason
self._compute_amount()
if float_compare(
self.amount_untaxed + max(self.amount_tax, abs(self.avatax_amount)),
self.amount_residual,
precision_rounding=self.currency_id.rounding or 0.001,
):
taxes_data = {
iline.id: iline.tax_ids for iline in self.invoice_line_ids
}
self.invoice_line_ids.write({"tax_ids": [(6, 0, [])]})
for line in self.invoice_line_ids:
line.write({"tax_ids": taxes_data[line.id].ids})
return tax_result

# Same as v13
Expand Down
Loading
Loading