Skip to content

Commit

Permalink
[IMP] account_avatax_exemption: Import tax rules
Browse files Browse the repository at this point in the history
In some cases, the tax rules have been entered in Avatax and you want to synchronize them with Odoo. We add the 'Import Tax Rules' feature that allows you to import the missing rules into Odoo.
  • Loading branch information
BernatPForgeFlow committed Nov 27, 2023
1 parent e6abbbc commit f6f9bd2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
68 changes: 68 additions & 0 deletions account_avatax_exemption/models/avalara_salestax.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,74 @@ def export_new_exemption_rules(self, rules=None):
description="Export Rule %s" % (rule.name),
)._export_base_rule_based_on_type(rule)

def map_rule_vals_to_fields(self, json_data):
avatax_rule_data = {

Check warning on line 291 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L291

Added line #L291 was not covered by tests
"avatax_id": json_data["id"],
"avatax_rate": json_data.get("value", ""),
"exemption_code_id": json_data.get("customerUsageType", ""),
"is_all_juris": json_data.get("isAllJuris", False),
"state": "done",
}
# Search for tax code and exemption code in Odoo models
tax_code_id = (

Check warning on line 299 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L299

Added line #L299 was not covered by tests
self.env["product.tax.code"].search(
[("name", "=", json_data.get("taxCode", "") or "")], limit=1
)
or None
)
if tax_code_id:
avatax_rule_data["avatax_tax_code"] = tax_code_id.id
exemption_code_id = (

Check warning on line 307 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L306-L307

Added lines #L306 - L307 were not covered by tests
self.env["exemption.code"].search(
[("code", "=", json_data.get("customerUsageType", "") or "")], limit=1
)
or None
)
if exemption_code_id:
avatax_rule_data["exemption_code_id"] = exemption_code_id.id

Check warning on line 314 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L314

Added line #L314 was not covered by tests
# Search for country and state in Odoo models
state_id = (

Check warning on line 316 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L316

Added line #L316 was not covered by tests
self.env["res.country.state"].search(
[("code", "=", json_data.get("region", "") or "")], limit=1
)
or None
)
if state_id:
avatax_rule_data["state_id"] = state_id.id
avatax_rule_data["taxable"] = state_id.avatax_nexus
return avatax_rule_data

Check warning on line 325 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L323-L325

Added lines #L323 - L325 were not covered by tests

def import_tax_rules(self):
avatax_custom_rule_model = self.env["exemption.code.rule"]
avatax_restpoint = AvaTaxRESTService(config=self)
rules = avatax_custom_rule_model.search(

Check warning on line 330 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L328-L330

Added lines #L328 - L330 were not covered by tests
[("avatax_id", "!=", False), ("state", "=", "done")],
)
include_option = None

Check warning on line 333 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L333

Added line #L333 was not covered by tests
if rules:
filter_rules = "id not in ("
filter_rules += ", ".join(map(str, rules.mapped("avatax_id")))
filter_rules += ")"
include_option = "$filter=" + filter_rules
r = avatax_restpoint.client.list_tax_rules(

Check warning on line 339 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L335-L339

Added lines #L335 - L339 were not covered by tests
self.avatax_company_id, include_option
)
result = r.json()

Check warning on line 342 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L342

Added line #L342 was not covered by tests
if "error" in result:
error = result["error"]
error_message = "Code: {}\nMessage: {}\nTarget: {}\nDetails;{}".format(

Check warning on line 345 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L344-L345

Added lines #L344 - L345 were not covered by tests
error.get("code", False),
error.get("message", False),
error.get("target", False),
error.get("details", False),
)
raise UserError(error_message)
mapped_data = []

Check warning on line 352 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L351-L352

Added lines #L351 - L352 were not covered by tests
for rule_vals in result["value"]:
if rule_vals.get("customerUsageType", "") and rule_vals.get("region", ""):
mapped_data.append(self.map_rule_vals_to_fields(rule_vals))
avatax_custom_rule_model.create(mapped_data)

Check warning on line 356 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L355-L356

Added lines #L355 - L356 were not covered by tests

def download_exemptions(self):
if not self.ids:
self = self.search([("exemption_export", "=", True)], limit=1)
Expand Down
5 changes: 5 additions & 0 deletions account_avatax_exemption/views/avalara_salestax_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
name="download_exemptions"
string="Import Exemptions"
/>
<button
type="object"
name="import_tax_rules"
string="Import Tax Rules"
/>
<button
type="object"
name="export_new_tax_items"
Expand Down

0 comments on commit f6f9bd2

Please sign in to comment.