Skip to content

Commit 4c195ad

Browse files
atchuthanAlexPForgeFlow
authored andcommitted
[FIX] Avatax: Use Odoo Tax option
1 parent 288a0b0 commit 4c195ad

File tree

10 files changed

+963
-2
lines changed

10 files changed

+963
-2
lines changed

account_avatax_exemption/models/avalara_salestax.py

Lines changed: 866 additions & 0 deletions
Large diffs are not rendered by default.

account_avatax_oca/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"wizard/avalara_get_company_code_view.xml",
1818
"wizard/avalara_salestax_address_validate_view.xml",
1919
"wizard/avalara_salestax_ping_view.xml",
20+
"wizard/account_move_reversal.xml",
2021
"views/avalara_salestax_view.xml",
2122
"views/partner_view.xml",
2223
"views/product_view.xml",

account_avatax_oca/models/account_move.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from odoo import _, api, fields, models
44
from odoo.exceptions import UserError
55
from odoo.tests.common import Form
6+
from odoo.tools.safe_eval import safe_eval
67

78
_logger = logging.getLogger(__name__)
89

@@ -109,6 +110,11 @@ def onchange_warehouse_id(self):
109110
avatax_response_log = fields.Text(
110111
"Avatax API Response Log", readonly=True, copy=False
111112
)
113+
avatax_amt_line_override = fields.Boolean(
114+
string="Use Odoo Tax",
115+
default=False,
116+
help="The Odoo tax will be uploaded to Avatax",
117+
)
112118

113119
@api.model
114120
@api.depends("company_id")
@@ -193,6 +199,9 @@ def _avatax_compute_tax(self, commit=False):
193199
if not avatax_config:
194200
# Skip Avatax computation if no configuration is found
195201
return
202+
avatax_line_override = (
203+
self.avatax_amt_line_override and self.move_type == "out_refund"
204+
)
196205
doc_type = self._get_avatax_doc_type(commit=commit)
197206
tax_date = self.get_origin_tax_date() or self.invoice_date
198207
taxable_lines = self._avatax_prepare_lines(doc_type)
@@ -214,6 +223,7 @@ def _avatax_compute_tax(self, commit=False):
214223
# TODO: can we report self.invoice_doc_no?
215224
self.name if self.move_type == "out_refund" else "",
216225
self.location_code or "",
226+
avatax_line_override,
217227
is_override=self.move_type == "out_refund",
218228
currency_id=self.currency_id,
219229
ignore_error=300 if commit else None,
@@ -233,7 +243,7 @@ def _avatax_compute_tax(self, commit=False):
233243
avatax_config.commit_transaction(self.name, doc_type)
234244
return tax_result
235245

236-
if self.state == "draft":
246+
if self.state == "draft" and not avatax_line_override:
237247
Tax = self.env["account.tax"]
238248
tax_result_lines = {int(x["lineNumber"]): x for x in tax_result["lines"]}
239249
taxes_to_set = []
@@ -254,6 +264,7 @@ def _avatax_compute_tax(self, commit=False):
254264
line_taxes = line.tax_ids.filtered(lambda x: not x.is_avatax)
255265
taxes_to_set.append((index, line_taxes | tax))
256266
line.avatax_amt_line = tax_result_line["tax"]
267+
line.avatax_tax_type = tax_result_line["details"][0]["taxSubTypeId"]
257268
self.with_context(check_move_validity=False).avatax_amount = tax_result[
258269
"totalTax"
259270
]
@@ -285,6 +296,7 @@ def avatax_compute_taxes(self, commit=False):
285296
invoice.move_type in ["out_invoice", "out_refund"]
286297
and invoice.fiscal_position_id.is_avatax
287298
and (invoice.state == "draft" or commit)
299+
and (not invoice.avatax_amt_line_override or commit)
288300
):
289301
invoice._avatax_compute_tax(commit=commit)
290302
return True
@@ -428,11 +440,24 @@ def create(self, vals_list):
428440
move.avatax_compute_taxes()
429441
return moves
430442

443+
def action_reverse(self):
444+
action = super().action_reverse()
445+
avatax_tax_type = self.invoice_line_ids.filtered(lambda t: t.avatax_tax_type)
446+
action["context"] = safe_eval(action.get("context", "{}"))
447+
action["context"].update(
448+
{
449+
"default_avatax_amt_line_override": self.avatax_amt_line_override,
450+
"hide_override": 1 if avatax_tax_type else 0,
451+
}
452+
)
453+
return action
454+
431455

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

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

437462
def _get_avatax_amount(self, qty=None):
438463
"""
@@ -486,6 +511,9 @@ def _avatax_prepare_line(self, sign=1, doc_type=None):
486511
amount = sign * line._get_avatax_amount()
487512
if line.quantity < 0:
488513
amount = -amount
514+
avatax_amt = 0.0
515+
if line.move_id.move_type == "out_refund":
516+
avatax_amt = -(line.price_total - line.price_subtotal)
489517
res = {
490518
"qty": line.quantity,
491519
"itemcode": item_code,
@@ -495,6 +523,8 @@ def _avatax_prepare_line(self, sign=1, doc_type=None):
495523
"id": line,
496524
"account_id": line.account_id.id,
497525
"tax_id": line.tax_ids,
526+
"avatax_amt_line": round(avatax_amt, 2),
527+
"avatax_tax_type": line.avatax_tax_type,
498528
}
499529
return res
500530

account_avatax_oca/models/avalara_salestax.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ def create_transaction(
195195
invoice_date=None,
196196
reference_code=None,
197197
location_code=None,
198+
avatax_line_override=None,
198199
is_override=None,
199200
currency_id=None,
200201
ignore_error=None,
@@ -284,6 +285,7 @@ def create_transaction(
284285
location_code,
285286
currency_code,
286287
partner.vat or None,
288+
avatax_line_override,
287289
is_override,
288290
ignore_error=ignore_error,
289291
log_to_record=log_to_record,

account_avatax_oca/models/avatax_rest_api.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def get_tax(
222222
location_code=None,
223223
currency_code="USD",
224224
vat=None,
225+
avatax_line_override=None,
225226
is_override=False,
226227
ignore_error=None,
227228
log_to_record=False,
@@ -255,6 +256,18 @@ def get_tax(
255256
"quantity": line.get("qty", 1),
256257
"amount": line.get("amount", 0.0),
257258
"taxCode": line.get("tax_code"),
259+
"taxOverride": {
260+
"type": "TaxAmountByTaxType",
261+
"reason": "Refund",
262+
"taxAmountByTaxTypes": [
263+
{
264+
"taxTypeId": line.get("avatax_tax_type"),
265+
"TaxAmount": line.get("avatax_amt_line", 0.0),
266+
}
267+
],
268+
}
269+
if avatax_line_override and line.get("avatax_tax_type")
270+
else None,
258271
}
259272
for line in received_lines
260273
]
@@ -295,7 +308,7 @@ def get_tax(
295308
"type": doc_type,
296309
"commit": commit,
297310
}
298-
if is_override and invoice_date:
311+
if is_override and invoice_date and not avatax_line_override:
299312
create_transaction.update(
300313
{
301314
"taxOverride": {

account_avatax_oca/security/avalara_salestax_security.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@
2727
model="ir.rule"
2828
search="[('model_id', '=', ref('model_product_tax_code'))]"
2929
/>
30+
<record id="access_avatax_override_group" model="res.groups">
31+
<field name="name">Can View Avatax Override</field>
32+
<field
33+
name="users"
34+
eval="[(4, ref('base.user_root')),(4, ref('base.user_admin'))]"
35+
/>
36+
</record>
3037
</odoo>

account_avatax_oca/views/account_move_view.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
name="invoice_doc_no"
6161
attrs="{'invisible': [('move_type','!=','out_refund')]}"
6262
/>
63+
<field
64+
name="avatax_amt_line_override"
65+
groups="account_avatax.access_avatax_override_group"
66+
attrs="{'invisible': [('move_type','!=','out_refund')]}"
67+
/>
6368
</field>
6469
<xpath expr="//field[@name='partner_shipping_id']" position="after">
6570
<field name="so_partner_id" readonly="1" />

account_avatax_oca/wizard/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from . import avalara_salestax_ping
22
from . import avalara_salestax_address_validate
33
from . import avalara_get_company_code
4+
from . import account_move_reversal
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from odoo import fields, models
2+
3+
4+
class AccountMoveReversal(models.TransientModel):
5+
"""
6+
Account move reversal wizard, it cancel an account move by reversing it.
7+
"""
8+
9+
_inherit = "account.move.reversal"
10+
11+
avatax_amt_line_override = fields.Boolean(
12+
string="Use Odoo Tax",
13+
default=False,
14+
help="The Odoo tax will be uploaded to Avatax",
15+
)
16+
17+
def _prepare_default_reversal(self, move):
18+
res = super()._prepare_default_reversal(move)
19+
res.update({"avatax_amt_line_override": self.avatax_amt_line_override})
20+
return res
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<odoo>
2+
<record id="view_account_move_reversal" model="ir.ui.view">
3+
<field name="name">account.move.reversal.form</field>
4+
<field name="model">account.move.reversal</field>
5+
<field name="inherit_id" ref="account.view_account_move_reversal" />
6+
<field name="type">form</field>
7+
<field name="arch" type="xml">
8+
<xpath expr="//field[@name='date']" position="after">
9+
<field
10+
name="avatax_amt_line_override"
11+
invisible="not context.get('hide_override')"
12+
/>
13+
</xpath>
14+
</field>
15+
</record>
16+
</odoo>

0 commit comments

Comments
 (0)