Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use company addres to send company address to avalara

Maciej Korycinskiafa30e729264
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index ba14d4be22..e47171a107 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -23,6 +23,7 @@ from ..core.taxes.interface import (
get_subtotal_gross,
get_total_gross,
postprocess_order_creation,
+ preprocess_order_creation,
)
from ..core.utils import to_local_currency
from ..core.utils.promo_code import (
@@ -1063,6 +1064,7 @@ def prepare_order_data(*, checkout: Checkout, tracking_code: str, discounts) ->
- checkout.discount_amount
).gross
+ preprocess_order_creation(checkout)
return order_data
diff --git a/saleor/core/taxes/avatax/__init__.py b/saleor/core/taxes/avatax/__init__.py
index 620647e28b..528151d96a 100644
--- a/saleor/core/taxes/avatax/__init__.py
+++ b/saleor/core/taxes/avatax/__init__.py
@@ -1,4 +1,5 @@
import json
+import logging
from datetime import date
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from urllib.parse import urljoin
@@ -18,6 +19,8 @@ if TYPE_CHECKING:
META_FIELD = "avatax"
+logger = logging.getLogger(__name__)
+
class TransactionType:
INVOICE = "SalesInvoice"
@@ -225,6 +228,15 @@ def generate_request_data(
commit=False,
):
company_address = get_company_address()
+ if company_address:
+ company_address = company_address.as_data()
+ else:
+ logging.warning(
+ "To correct calculate taxes by Avatax, company address should be provided "
+ "in dashboard.settings."
+ )
+ company_address = {}
+
data = {
"companyCode": settings.AVATAX_COMPANY_NAME,
"type": transaction_type,
@@ -241,12 +253,12 @@ def generate_request_data(
"addresses": {
# FIXME we should put company address here
"shipFrom": { # warehouse
- "line1": company_address.street_address_1,
- "line2": company_address.street_address_2,
- "city": company_address.city,
- "region": company_address.country_area,
- "country": company_address.country,
- "postalCode": company_address.postal_code,
+ "line1": company_address.get("street_address_1"),
+ "line2": company_address.get("street_address_2"),
+ "city": company_address.get("city"),
+ "region": company_address.get("country_area"),
+ "country": company_address.get("country"),
+ "postalCode": company_address.get("postal_code"),
},
"shipTo": {
"line1": address.get("street_address_1"),
@@ -302,6 +314,9 @@ def get_cached_response_or_fetch(data, token_in_cache):
response = api_post_request(transaction_url, data)
if response and "error" not in response:
cache.set(data_cache_key, (data, response), settings.AVATAX_CACHE_TIME)
+ else:
+ # cache failed response to limit hits to avatax.
+ cache.set(data_cache_key, (data, response), 10)
else:
_, response = cache.get(data_cache_key)
@@ -347,18 +362,18 @@ def get_cached_tax_codes_or_fetch(
):
"""Try to get cached tax codes. If cache is empty fetch the newest taxcodes from
avatax"""
- tax_codes = cache.get(settings.TAX_CODES_CACHE_KEY, {})
+ tax_codes = cache.get(settings.AVATAX_TAX_CODES_CACHE_KEY, {})
if not tax_codes:
tax_codes_url = urljoin(get_api_url(), "definitions/taxcodes")
response = api_get_request(tax_codes_url)
if response and "error" not in response:
tax_codes = generate_tax_codes_dict(response)
- cache.set(settings.TAX_CODES_CACHE_KEY, tax_codes, cache_time)
+ cache.set(settings.AVATAX_TAX_CODES_CACHE_KEY, tax_codes, cache_time)
return tax_codes
def retrieve_tax_code_from_meta(obj: Union["Product", "ProductVariant"]):
if not hasattr(obj, "meta"):
- return ""
+ return "O9999999" # "Temporary Unmapped Other SKU - taxable default"
tax = obj.meta.get("taxes", {}).get(META_FIELD, {})
- return tax.get("code", "")
+ return tax.get("code", "O9999999")
diff --git a/saleor/core/taxes/avatax/interface.py b/saleor/core/taxes/avatax/interface.py
index ba59379c45..6944dd5ce4 100644
--- a/saleor/core/taxes/avatax/interface.py
+++ b/saleor/core/taxes/avatax/interface.py
@@ -23,6 +23,7 @@ from . import (
)
if TYPE_CHECKING:
+ from ....checkout.models import Checkout
from ....order.models import Order, OrderLine
@@ -88,18 +89,16 @@ def get_shipping_gross(checkout: "checkout_models.Checkout", discounts):
return TaxedMoney(net=shipping_net, gross=shipping_gross)
-def postprocess_order_creation(order: "Order"):
- # FIXME this can be a celery task (?)
- # FIXME maybe we can figure out better name
+def preprocess_order_creation(checkout: "Checkout"):
+ """Confirm that all data is correct and we can proceed with creation of order.
+ Raise error when can't receive taxes"""
- # Fixme we can generate data directly from order
- checkout = checkout_models.Checkout.objects.get(token=order.checkout_token)
discounts = Sale.objects.active(date.today()).prefetch_related(
"products", "categories", "collections"
)
data = generate_request_data_from_checkout(
checkout,
- transaction_token=str(order.token),
+ transaction_token=str(checkout.token),
transaction_type=TransactionType.INVOICE,
discounts=discounts,
)
@@ -110,6 +109,25 @@ def postprocess_order_creation(order: "Order"):
raise TaxError(response.get("error", {}).get("message", ""))
+def postprocess_order_creation(order: "Order"):
+ # FIXME this should be a celery task. it should retry to send an order in case
+ # of fail
+
+ # Fixme we can generate data directly from order
+ checkout = checkout_models.Checkout.objects.get(token=order.checkout_token)
+ discounts = Sale.objects.active(date.today()).prefetch_related(
+ "products", "categories", "collections"
+ )
+ data = generate_request_data_from_checkout(
+ checkout,
+ transaction_token=str(order.token),
+ transaction_type=TransactionType.INVOICE,
+ discounts=discounts,
+ )
+ transaction_url = urljoin(get_api_url(), "transactions/createoradjust")
+ api_post_request(transaction_url, data)
+
+
def get_line_total_gross(checkout_line: "checkout_models.CheckoutLine", discounts):
checkout = checkout_line.checkout
taxes_data = get_checkout_tax_data(checkout, discounts)
diff --git a/saleor/core/taxes/interface.py b/saleor/core/taxes/interface.py
index 8dddaa3e1c..495c7d73a8 100644
--- a/saleor/core/taxes/interface.py
+++ b/saleor/core/taxes/interface.py
@@ -155,6 +155,11 @@ def apply_taxes_to_shipping_price_range(prices: MoneyRange, country: Country):
return TaxedMoneyRange(start=start, stop=stop)
+def preprocess_order_creation(checkout: "Checkout"):
+ if settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:
+ return avatax_interface.preprocess_order_creation(checkout)
+
+
# FIXME this should be converted to the plugin action after we introduce plugin
# architecture
def postprocess_order_creation(order: "Order"):