Skip to content

Commit

Permalink
[IMP] added mockup of avatax anwser
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisOForgeFlow committed Oct 8, 2024
1 parent a69a2d8 commit d0abfa2
Show file tree
Hide file tree
Showing 3 changed files with 538 additions and 18 deletions.
44 changes: 27 additions & 17 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,29 +251,40 @@ 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)
self = move_form.save()
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, [])]})
line.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
# 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

0 comments on commit d0abfa2

Please sign in to comment.