Skip to content

Commit

Permalink
[ADD] Support for REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
murtuzasaleh authored and dreispt committed May 19, 2020
1 parent 1c0dfa6 commit 32de77b
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 66 deletions.
2 changes: 1 addition & 1 deletion avatax_connector/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Avalara Avatax Connector",
"version": "12.0.2.3.9",
"version": "12.0.2.4.0",
"author": "Fabrice Henrion, Sodexis"
", Open Source Integrators",
"summary": "Sales tax Calculation",
Expand Down
1 change: 1 addition & 0 deletions avatax_connector/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from . import account_tax
from . import res_config_settings
from . import res_company
from . import avatax_rest_api
108 changes: 70 additions & 38 deletions avatax_connector/models/account_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from odoo import api, fields, models, _
from odoo.exceptions import UserError
from .avalara_api import AvaTaxService, BaseAddress
from .avatax_rest_api import AvaTaxRESTService


_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -63,51 +64,82 @@ def _get_compute_tax(
raise UserError(_('Please validate the company address.'))

if avatax_config.disable_tax_calculation:
_logger.info('Avatax tax calculation is disabled. Skipping %s %s.', doc_code, doc_type)
_logger.info(
'Avatax tax calculation is disabled. Skipping %s %s.', doc_code, doc_type)
return False

#For check credential
avalara_obj = AvaTaxService(
avatax_config.account_number, avatax_config.license_key,
avatax_config.service_url, avatax_config.request_timeout, avatax_config.logging)
avalara_obj.create_tax_service()
addSvc = avalara_obj.create_address_service().addressSvc
origin = BaseAddress(addSvc, ship_from_address.street or None,
ship_from_address.street2 or None,
ship_from_address.city, ship_from_address.zip,
ship_from_address.state_id and ship_from_address.state_id.code or None,
ship_from_address.country_id and ship_from_address.country_id.code or None, 0).data
destination = BaseAddress(addSvc, shipping_address.street or None,
shipping_address.street2 or None,
shipping_address.city, shipping_address.zip,
shipping_address.state_id and shipping_address.state_id.code or None,
shipping_address.country_id and shipping_address.country_id.code or None, 1).data

# using get_tax method to calculate tax based on address
result = avalara_obj.get_tax(avatax_config.company_code, doc_date, doc_type,
partner.customer_code, doc_code, origin, destination,
lines, exemption_number,
exemption_code_name,
user and user.name or None, commit, invoice_date, reference_code,
location_code, currency_code, partner.vat_id or None, is_override)
if 'rest' in avatax_config.service_url:
avatax_restpoint = AvaTaxRESTService(
avatax_config.account_number,
avatax_config.license_key,
avatax_config.service_url,
avatax_config.request_timeout,
avatax_config.logging)
result = avatax_restpoint.get_tax(
avatax_config.company_code, doc_date, doc_type,
partner.customer_code, doc_code, ship_from_address,
shipping_address,
lines, exemption_number,
exemption_code_name,
user and user.name or None, commit,
invoice_date, reference_code,
location_code, currency_code,
partner.vat_id or None, is_override)
else:
# For check credential
avalara_obj = AvaTaxService(
avatax_config.account_number, avatax_config.license_key,
avatax_config.service_url, avatax_config.request_timeout, avatax_config.logging)
avalara_obj.create_tax_service()
addSvc = avalara_obj.create_address_service().addressSvc
origin = BaseAddress(addSvc, ship_from_address.street or None,
ship_from_address.street2 or None,
ship_from_address.city, ship_from_address.zip,
ship_from_address.state_id and ship_from_address.state_id.code or None,
ship_from_address.country_id and ship_from_address.country_id.code or None, 0).data
destination = BaseAddress(addSvc, shipping_address.street or None,
shipping_address.street2 or None,
shipping_address.city, shipping_address.zip,
shipping_address.state_id and shipping_address.state_id.code or None,
shipping_address.country_id and shipping_address.country_id.code or None, 1).data

# using get_tax method to calculate tax based on address
result = avalara_obj.get_tax(avatax_config.company_code, doc_date, doc_type,
partner.customer_code, doc_code, origin, destination,
lines, exemption_number,
exemption_code_name,
user and user.name or None, commit, invoice_date, reference_code,
location_code, currency_code, partner.vat_id or None, is_override)
return result

@api.model
def cancel_tax(self, avatax_config, doc_code, doc_type, cancel_code):
"""Sometimes we have not need to tax calculation, then method is used to cancel taxation"""
if avatax_config.disable_tax_calculation:
_logger.info('Avatax tax calculation is disabled. Skipping %s %s.', doc_code, doc_type)
_logger.info(
'Avatax tax calculation is disabled. Skipping %s %s.', doc_code, doc_type)
return False

avalara_obj = AvaTaxService(
avatax_config.account_number, avatax_config.license_key,
avatax_config.service_url, avatax_config.request_timeout,
avatax_config.logging)
avalara_obj.create_tax_service()
# Why the silent failure? Let explicitly raise the error.
#try:
result = avalara_obj.get_tax_history(avatax_config.company_code, doc_code, doc_type)
#except:
# return True
result = avalara_obj.cancel_tax(avatax_config.company_code, doc_code, doc_type, cancel_code)
if 'rest' in avatax_config.service_url:
avatax_restpoint = AvaTaxRESTService(
avatax_config.account_number,
avatax_config.license_key,
avatax_config.service_url,
avatax_config.request_timeout,
avatax_config.logging)
result = avatax_restpoint.cancel_tax(
avatax_config.company_code, doc_code, doc_type, cancel_code)
else:
avalara_obj = AvaTaxService(
avatax_config.account_number, avatax_config.license_key,
avatax_config.service_url, avatax_config.request_timeout,
avatax_config.logging)
avalara_obj.create_tax_service()
# Why the silent failure? Let explicitly raise the error.
# try:
result = avalara_obj.get_tax_history(
avatax_config.company_code, doc_code, doc_type)
# except:
# return True
result = avalara_obj.cancel_tax(
avatax_config.company_code, doc_code, doc_type, cancel_code)
return result
15 changes: 12 additions & 3 deletions avatax_connector/models/avalara_salestax.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from odoo import api, fields, models
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class ExemptionCode(models.Model):
Expand Down Expand Up @@ -40,8 +41,10 @@ def onchange_system_call2(self):
account_number = fields.Char('Account Number', required=True, help="Account Number provided by AvaTax")
license_key = fields.Char('License Key', required=True, help="License Key provided by AvaTax")
service_url = fields.Selection(
[('https://development.avalara.net', 'Test'),
('https://avatax.avalara.net', 'Production')],
[('https://development.avalara.net', 'SOAP API Test'),
('https://avatax.avalara.net', 'SOAP API Production'),
('https://sandbox-rest.avatax.com/api/v2', 'REST API Test'),
('https://rest.avatax.com/api/v2', 'REST API Production')],
string='Service URL',
default='https://development.avalara.net',
required=True,
Expand Down Expand Up @@ -92,6 +95,12 @@ def onchange_system_call2(self):
on_order = fields.Boolean('Order-level', default=True, help="It will calculate tax for order not line by line.")
upc_enable = fields.Boolean('Enable UPC Taxability', help="Allows ean13 to be reported in place of Item Reference as upc identifier.")

@api.constrains('service_url', 'on_line')
def _check_tax_by_line(self):
if 'rest' in self.service_url and self.on_line:
raise ValidationError(_(
'Calculate tax by line is not supported in Rest API.'))

#constraints on uniq records creation with account_number and company_id
_sql_constraints = [
('code_company_uniq', 'unique (company_code)', 'Avalara setting is already available for this company code'),
Expand Down
Loading

0 comments on commit 32de77b

Please sign in to comment.