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

[FIX] Avatax: Use Odoo Tax option #449

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
866 changes: 866 additions & 0 deletions account_avatax_exemption/models/avalara_salestax.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions account_avatax_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"wizard/avalara_get_company_code_view.xml",
"wizard/avalara_salestax_address_validate_view.xml",
"wizard/avalara_salestax_ping_view.xml",
"wizard/account_move_reversal.xml",
"views/avalara_salestax_view.xml",
"views/partner_view.xml",
"views/product_view.xml",
Expand Down
32 changes: 31 additions & 1 deletion account_avatax_oca/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tests.common import Form
from odoo.tools.safe_eval import safe_eval

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -109,6 +110,11 @@
avatax_response_log = fields.Text(
"Avatax API Response Log", readonly=True, copy=False
)
avatax_amt_line_override = fields.Boolean(
string="Use Odoo Tax",
default=False,
help="The Odoo tax will be uploaded to Avatax",
)

@api.model
@api.depends("company_id")
Expand Down Expand Up @@ -193,6 +199,9 @@
if not avatax_config:
# Skip Avatax computation if no configuration is found
return
avatax_line_override = (

Check warning on line 202 in account_avatax_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_move.py#L202

Added line #L202 was not covered by tests
self.avatax_amt_line_override and self.move_type == "out_refund"
)
doc_type = self._get_avatax_doc_type(commit=commit)
tax_date = self.get_origin_tax_date() or self.invoice_date
taxable_lines = self._avatax_prepare_lines(doc_type)
Expand All @@ -214,6 +223,7 @@
# TODO: can we report self.invoice_doc_no?
self.name if self.move_type == "out_refund" else "",
self.location_code or "",
avatax_line_override,
is_override=self.move_type == "out_refund",
currency_id=self.currency_id,
ignore_error=300 if commit else None,
Expand All @@ -233,7 +243,7 @@
avatax_config.commit_transaction(self.name, doc_type)
return tax_result

if self.state == "draft":
if self.state == "draft" and not avatax_line_override:
Tax = self.env["account.tax"]
tax_result_lines = {int(x["lineNumber"]): x for x in tax_result["lines"]}
taxes_to_set = []
Expand All @@ -254,6 +264,7 @@
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"]
line.avatax_tax_type = tax_result_line["details"][0]["taxSubTypeId"]

Check warning on line 267 in account_avatax_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_move.py#L267

Added line #L267 was not covered by tests
self.with_context(check_move_validity=False).avatax_amount = tax_result[
"totalTax"
]
Expand Down Expand Up @@ -285,6 +296,7 @@
invoice.move_type in ["out_invoice", "out_refund"]
and invoice.fiscal_position_id.is_avatax
and (invoice.state == "draft" or commit)
and (not invoice.avatax_amt_line_override or commit)
):
invoice._avatax_compute_tax(commit=commit)
return True
Expand Down Expand Up @@ -428,11 +440,24 @@
move.avatax_compute_taxes()
return moves

def action_reverse(self):
action = super().action_reverse()

Check warning on line 444 in account_avatax_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_move.py#L444

Added line #L444 was not covered by tests
avatax_tax_type = self.invoice_line_ids.filtered(lambda t: t.avatax_tax_type)
action["context"] = safe_eval(action.get("context", "{}"))
action["context"].update(

Check warning on line 447 in account_avatax_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_move.py#L446-L447

Added lines #L446 - L447 were not covered by tests
{
"default_avatax_amt_line_override": self.avatax_amt_line_override,
"hide_override": 1 if avatax_tax_type else 0,
}
)
return action

Check warning on line 453 in account_avatax_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_move.py#L453

Added line #L453 was not covered by tests


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

avatax_amt_line = fields.Float(string="AvaTax Line", copy=False)
avatax_tax_type = fields.Char()

def _get_avatax_amount(self, qty=None):
"""
Expand Down Expand Up @@ -486,6 +511,9 @@
amount = sign * line._get_avatax_amount()
if line.quantity < 0:
amount = -amount
avatax_amt = 0.0

Check warning on line 514 in account_avatax_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_move.py#L514

Added line #L514 was not covered by tests
if line.move_id.move_type == "out_refund":
avatax_amt = -(line.price_total - line.price_subtotal)

Check warning on line 516 in account_avatax_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/models/account_move.py#L516

Added line #L516 was not covered by tests
res = {
"qty": line.quantity,
"itemcode": item_code,
Expand All @@ -495,6 +523,8 @@
"id": line,
"account_id": line.account_id.id,
"tax_id": line.tax_ids,
"avatax_amt_line": round(avatax_amt, 2),
"avatax_tax_type": line.avatax_tax_type,
}
return res

Expand Down
2 changes: 2 additions & 0 deletions account_avatax_oca/models/avalara_salestax.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def create_transaction(
invoice_date=None,
reference_code=None,
location_code=None,
avatax_line_override=None,
is_override=None,
currency_id=None,
ignore_error=None,
Expand Down Expand Up @@ -284,6 +285,7 @@ def create_transaction(
location_code,
currency_code,
partner.vat or None,
avatax_line_override,
is_override,
ignore_error=ignore_error,
log_to_record=log_to_record,
Expand Down
15 changes: 14 additions & 1 deletion account_avatax_oca/models/avatax_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def get_tax(
location_code=None,
currency_code="USD",
vat=None,
avatax_line_override=None,
is_override=False,
ignore_error=None,
log_to_record=False,
Expand Down Expand Up @@ -255,6 +256,18 @@ def get_tax(
"quantity": line.get("qty", 1),
"amount": line.get("amount", 0.0),
"taxCode": line.get("tax_code"),
"taxOverride": {
"type": "TaxAmountByTaxType",
"reason": "Refund",
"taxAmountByTaxTypes": [
{
"taxTypeId": line.get("avatax_tax_type"),
"TaxAmount": line.get("avatax_amt_line", 0.0),
}
],
}
if avatax_line_override and line.get("avatax_tax_type")
else None,
}
for line in received_lines
]
Expand Down Expand Up @@ -295,7 +308,7 @@ def get_tax(
"type": doc_type,
"commit": commit,
}
if is_override and invoice_date:
if is_override and invoice_date and not avatax_line_override:
create_transaction.update(
{
"taxOverride": {
Expand Down
7 changes: 7 additions & 0 deletions account_avatax_oca/security/avalara_salestax_security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@
model="ir.rule"
search="[('model_id', '=', ref('model_product_tax_code'))]"
/>
<record id="access_avatax_override_group" model="res.groups">
<field name="name">Can View Avatax Override</field>
<field
name="users"
eval="[(4, ref('base.user_root')),(4, ref('base.user_admin'))]"
/>
</record>
</odoo>
5 changes: 5 additions & 0 deletions account_avatax_oca/views/account_move_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
name="invoice_doc_no"
attrs="{'invisible': [('move_type','!=','out_refund')]}"
/>
<field
name="avatax_amt_line_override"
groups="account_avatax.access_avatax_override_group"
attrs="{'invisible': [('move_type','!=','out_refund')]}"
/>
</field>
<xpath expr="//field[@name='partner_shipping_id']" position="after">
<field name="so_partner_id" readonly="1" />
Expand Down
1 change: 1 addition & 0 deletions account_avatax_oca/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import avalara_salestax_ping
from . import avalara_salestax_address_validate
from . import avalara_get_company_code
from . import account_move_reversal
20 changes: 20 additions & 0 deletions account_avatax_oca/wizard/account_move_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from odoo import fields, models


class AccountMoveReversal(models.TransientModel):
"""
Account move reversal wizard, it cancel an account move by reversing it.
"""

_inherit = "account.move.reversal"

avatax_amt_line_override = fields.Boolean(
string="Use Odoo Tax",
default=False,
help="The Odoo tax will be uploaded to Avatax",
)

def _prepare_default_reversal(self, move):
res = super()._prepare_default_reversal(move)
res.update({"avatax_amt_line_override": self.avatax_amt_line_override})
return res

Check warning on line 20 in account_avatax_oca/wizard/account_move_reversal.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_oca/wizard/account_move_reversal.py#L18-L20

Added lines #L18 - L20 were not covered by tests
16 changes: 16 additions & 0 deletions account_avatax_oca/wizard/account_move_reversal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<odoo>
<record id="view_account_move_reversal" model="ir.ui.view">
<field name="name">account.move.reversal.form</field>
<field name="model">account.move.reversal</field>
<field name="inherit_id" ref="account.view_account_move_reversal" />
<field name="type">form</field>
<field name="arch" type="xml">
<xpath expr="//field[@name='date']" position="after">
<field
name="avatax_amt_line_override"
invisible="not context.get('hide_override')"
/>
</xpath>
</field>
</record>
</odoo>
Loading