Skip to content

Commit

Permalink
[16.0][FIX] account_avatax_oca, skip exception raise on lines with no…
Browse files Browse the repository at this point in the history
…t product in display_type
  • Loading branch information
ChrisOForgeFlow authored and BernatPForgeFlow committed Dec 29, 2023
1 parent 5e36f50 commit 302c0b6
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 15 deletions.
2 changes: 1 addition & 1 deletion account_avatax_oca/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Avalara Avatax Certified Connector
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:e4c0622ec50dd385ed7a6fecfe2684759113981069eba0c5e7a5796f3dcc71c8
!! source digest: sha256:9dda51e8cf40c788c1b67e447cb58937fabb3d6c4163484df21d51b781fcc2db
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import models
from . import wizard
from .hooks import pre_init_hook
from .hooks import post_load_hook
1 change: 1 addition & 0 deletions account_avatax_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"website": "https://github.com/OCA/account-fiscal-rule",
"depends": ["sale_stock", "base_geolocalize"],
"pre_init_hook": "pre_init_hook",
"post_load": "post_load_hook",
"data": [
"security/avalara_salestax_security.xml",
"security/ir.model.access.csv",
Expand Down
87 changes: 87 additions & 0 deletions account_avatax_oca/hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api
from odoo.tools import frozendict

from odoo.addons.account.models.account_move_line import AccountMoveLine


def pre_init_hook(cr):
# Preserve key data when moving from account_avatax to account_avatax_oca
Expand All @@ -13,3 +18,85 @@ def pre_init_hook(cr):
WHERE name in ('avatax_fiscal_position_us', 'account_avatax')
"""
)


def post_load_hook(): # noqa: C901
@api.depends(
"tax_ids",
"currency_id",
"partner_id",
"analytic_distribution",
"balance",
"partner_id",
"move_id.partner_id",
"price_unit",
"quantity",
)
def _compute_all_tax_new(self):
for line in self:
sign = line.move_id.direction_sign
if line.display_type == "tax":
line.compute_all_tax = {}
line.compute_all_tax_dirty = False
continue
if line.display_type == "product" and line.move_id.is_invoice(True):
amount_currency = sign * line.price_unit * (1 - line.discount / 100)
handle_price_include = True
quantity = line.quantity
else:
amount_currency = line.amount_currency
handle_price_include = False
quantity = 1
compute_all_currency = line.tax_ids.with_context(
current_aml=line.id
).compute_all(
amount_currency,
currency=line.currency_id,
quantity=quantity,
product=line.product_id,
partner=line.move_id.partner_id or line.partner_id,
is_refund=line.is_refund,
handle_price_include=handle_price_include,
include_caba_tags=line.move_id.always_tax_exigible,
fixed_multiplicator=sign,
)
rate = line.amount_currency / line.balance if line.balance else 1
line.compute_all_tax_dirty = True
line.compute_all_tax = {
frozendict(
{
"tax_repartition_line_id": tax["tax_repartition_line_id"],
"group_tax_id": tax["group"] and tax["group"].id or False,
"account_id": tax["account_id"] or line.account_id.id,
"currency_id": line.currency_id.id,
"analytic_distribution": (
(tax["analytic"] or not tax["use_in_tax_closing"])
and line.move_id.state == "draft"
)
and line.analytic_distribution,
"tax_ids": [(6, 0, tax["tax_ids"])],
"tax_tag_ids": [(6, 0, tax["tag_ids"])],
"partner_id": line.move_id.partner_id.id or line.partner_id.id,
"move_id": line.move_id.id,
"display_type": line.display_type,
}
): {
"name": tax["name"]
+ (" " + _("(Discount)") if line.display_type == "epd" else ""),
"balance": tax["amount"] / rate,
"amount_currency": tax["amount"],
"tax_base_amount": tax["base"]
/ rate
* (-1 if line.tax_tag_invert else 1),
}
for tax in compute_all_currency["taxes"]
if tax["amount"]
}
if not line.tax_repartition_line_id:
line.compute_all_tax[frozendict({"id": line.id})] = {
"tax_tag_ids": [(6, 0, compute_all_currency["base_tags"])],
}

if not hasattr(AccountMoveLine, "_compute_all_tax_origin"):
AccountMoveLine._compute_all_tax_origin = AccountMoveLine._compute_all_tax
AccountMoveLine._compute_all_tax = _compute_all_tax_new
39 changes: 26 additions & 13 deletions account_avatax_oca/models/account_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,39 @@ def compute_all(
fixed_multiplicator,
)
avatax_invoice = self.env.context.get("avatax_invoice")
current_aml = False
if "current_aml" in self.env.context:
current_aml = self.env["account.move.line"].browse(
self.env.context.get("current_aml")
)
if not (
current_aml.display_type == "product"
and current_aml.account_type != "asset_receivable"
):
avatax_invoice = False
if avatax_invoice:
# Find the Avatax amount in the invoice Lines
# Looks up the line for the current product, price_unit, and quantity
# Note that the price_unit used must consider discount
base = res["total_excluded"]
digits = 6
avatax_amount = None
for line in avatax_invoice.invoice_line_ids:
price_unit = line.currency_id._convert(
price_unit,
avatax_invoice.company_id.currency_id,
avatax_invoice.company_id,
avatax_invoice.date,
)
if (
line.product_id == product
and float_compare(line.quantity, quantity, digits) == 0
):
avatax_amount = copysign(line.avatax_amt_line, base)
break
if current_aml:
avatax_amount = copysign(current_aml.avatax_amt_line, base)

Check warning on line 103 in account_avatax_oca/models/account_tax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_tax.py#L103

Added line #L103 was not covered by tests
else:
for line in avatax_invoice.invoice_line_ids:
price_unit = line.currency_id._convert(

Check warning on line 106 in account_avatax_oca/models/account_tax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_tax.py#L106

Added line #L106 was not covered by tests
price_unit,
avatax_invoice.company_id.currency_id,
avatax_invoice.company_id,
avatax_invoice.date,
)
if (
line.product_id == product
and float_compare(line.quantity, quantity, digits) == 0
):
avatax_amount = copysign(line.avatax_amt_line, base)
break

Check warning on line 117 in account_avatax_oca/models/account_tax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_tax.py#L116-L117

Added lines #L116 - L117 were not covered by tests
if avatax_amount is None:
avatax_amount = 0.0
raise exceptions.UserError(
Expand Down
3 changes: 2 additions & 1 deletion account_avatax_oca/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -366,7 +367,7 @@ <h1 class="title">Avalara Avatax Certified Connector</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:e4c0622ec50dd385ed7a6fecfe2684759113981069eba0c5e7a5796f3dcc71c8
!! source digest: sha256:9dda51e8cf40c788c1b67e447cb58937fabb3d6c4163484df21d51b781fcc2db
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/account-fiscal-rule/tree/16.0/account_avatax_oca"><img alt="OCA/account-fiscal-rule" src="https://img.shields.io/badge/github-OCA%2Faccount--fiscal--rule-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/account-fiscal-rule-16-0/account-fiscal-rule-16-0-account_avatax_oca"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/account-fiscal-rule&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://developer.avalara.com/certification/avatax/sales-tax-badge/"><img alt="Sales Tax Certification" src="https://raw.githubusercontent.com/OCA/account-fiscal-rule/16.0/account_avatax_oca/static/description/SalesTax.png" style="width: 250px;" /></a> <a class="reference external image-reference" href="https://developer.avalara.com/certification/avatax/refunds-credit-memos-badge/"><img alt="Refunds Certification" src="https://raw.githubusercontent.com/OCA/account-fiscal-rule/16.0/account_avatax_oca/static/description/Refunds.png" style="width: 250px;" /></a> <a class="reference external image-reference" href="https://developer.avalara.com/certification/avatax/address-validation-badge/"><img alt="Address Validation Certification" src="https://raw.githubusercontent.com/OCA/account-fiscal-rule/16.0/account_avatax_oca/static/description/AddressValidation.png" style="width: 250px;" /></a></p>
Expand Down

0 comments on commit 302c0b6

Please sign in to comment.