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 97c7a8a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
72 changes: 72 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,78 @@ 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(
[
("country_id.code", "=", json_data.get("country", "") or ""),
("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 329 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L327-L329

Added lines #L327 - L329 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 334 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L332-L334

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

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

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L337

Added line #L337 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 343 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L339-L343

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

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

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L346

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

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

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L348-L349

Added lines #L348 - L349 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 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
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 360 in account_avatax_exemption/models/avalara_salestax.py

View check run for this annotation

Codecov / codecov/patch

account_avatax_exemption/models/avalara_salestax.py#L359-L360

Added lines #L359 - L360 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 97c7a8a

Please sign in to comment.