From a2f14030ff1a1ebf06e4703122feca73bc1e9032 Mon Sep 17 00:00:00 2001 From: RLeeOSI <51208020+RLeeOSI@users.noreply.github.com> Date: Thu, 3 Aug 2023 12:43:49 -0700 Subject: [PATCH 1/2] [16.0][FIX] account_avatax_oca: tax calculation improvements * [FIX] compound rate computation adjustment * [FIX] model inconsistencies in migration * [FIX] replace _recompute_dynamic_lines with _sync_dynamic_lines * [FIX] unbalanced entries error --- account_avatax_oca/models/account_move.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/account_avatax_oca/models/account_move.py b/account_avatax_oca/models/account_move.py index 87f20d3e9..9592b2eb9 100644 --- a/account_avatax_oca/models/account_move.py +++ b/account_avatax_oca/models/account_move.py @@ -202,6 +202,7 @@ def _avatax_prepare_lines(self, doc_type=None): # Same as v12 def _avatax_compute_tax(self, commit=False): """Contact REST API and recompute taxes for a Sale Order""" + # Override to handle lines with split taxes (e.g. TN) self and self.ensure_one() avatax_config = self.company_id.get_avatax_config_company() if not avatax_config: @@ -255,18 +256,26 @@ def _avatax_compute_tax(self, commit=False): for index, line in enumerate(lines): tax_result_line = tax_result_lines.get(line.id) if tax_result_line: - rate = tax_result_line.get("rate", 0.0) + # rate = tax_result_line.get("rate", 0.0) + tax_calculation = 0.0 + if tax_result_line["taxableAmount"]: + tax_calculation = ( + tax_result_line["taxCalculated"] + / tax_result_line["taxableAmount"] + ) + rate = round(tax_calculation * 100, 4) tax = Tax.get_avalara_tax(rate, doc_type) if tax and tax not in line.tax_ids: - line_taxes = ( - tax - if avatax_config - else line.tax_ids.filtered(lambda x: not x.is_avatax) - ) + line_taxes = line.tax_ids.filtered(lambda x: not x.is_avatax) taxes_to_set.append((index, line_taxes | tax)) line.avatax_amt_line = tax_result_line["tax"] - self.avatax_amount = tax_result["totalTax"] + 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 From 16f7d84307e7602f75fa125e3e9f0dc2301228e9 Mon Sep 17 00:00:00 2001 From: RLeeOSI <51208020+RLeeOSI@users.noreply.github.com> Date: Thu, 3 Aug 2023 12:44:27 -0700 Subject: [PATCH 2/2] [16.0][FIX] account_avatax_sale_oca: tax calculation improvements * [FIX] compound rate computation adjustment --- account_avatax_sale_oca/models/sale_order.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/account_avatax_sale_oca/models/sale_order.py b/account_avatax_sale_oca/models/sale_order.py index ba8d6cf1f..62244047b 100644 --- a/account_avatax_sale_oca/models/sale_order.py +++ b/account_avatax_sale_oca/models/sale_order.py @@ -170,6 +170,7 @@ def _avatax_prepare_lines(self, order_lines, doc_type=None): def _avatax_compute_tax(self): """Contact REST API and recompute taxes for a Sale Order""" + # Override to handle lines with split taxes (e.g. TN) self and self.ensure_one() doc_type = self._get_avatax_doc_type() Tax = self.env["account.tax"] @@ -201,7 +202,14 @@ def _avatax_compute_tax(self): # Should we check the rate with the tax amount? # tax_amount = tax_result_line["taxCalculated"] # rate = round(tax_amount / line.price_subtotal * 100, 2) - rate = tax_result_line["rate"] + # rate = tax_result_line["rate"] + tax_calculation = 0.0 + if tax_result_line["taxableAmount"]: + tax_calculation = ( + tax_result_line["taxCalculated"] + / tax_result_line["taxableAmount"] + ) + rate = round(tax_calculation * 100, 4) tax = Tax.get_avalara_tax(rate, doc_type) if tax not in line.tax_id: line_taxes = (