Skip to content

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

Fix incorrect tax calculation for Avatax. Recalculate draft order prices

Maciej Korycinski772b1b166917
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index a3f81d3bcf..b92c6fdc40 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -15,7 +15,7 @@ from ....order.actions import (
order_voided,
)
from ....order.error_codes import OrderErrorCode
-from ....order.utils import get_valid_shipping_methods_for_order
+from ....order.utils import get_valid_shipping_methods_for_order, update_order_prices
from ....payment import CustomPaymentChoices, PaymentError, TransactionKind, gateway
from ...account.types import AddressInput
from ...core.mutations import BaseMutation
@@ -249,6 +249,7 @@ class OrderUpdateShipping(BaseMutation):
"shipping_price_gross_amount",
]
)
+ update_order_prices(order, info.context.discounts)
# Post-process the results
order_shipping_updated(order)
return OrderUpdateShipping(order=order)
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index 1f36437822..8e6260f8a0 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -254,6 +254,7 @@ def get_order_lines_data(
"variant__product__collections",
"variant__product__product_type",
).filter(variant__product__charge_taxes=True)
+ system_tax_included = Site.objects.get_current().settings.include_taxes_in_prices
for line in lines:
if not line.variant:
continue
@@ -261,6 +262,15 @@ def get_order_lines_data(
product_type = line.variant.product.product_type
tax_code = retrieve_tax_code_from_meta(product, default=None)
tax_code = tax_code or retrieve_tax_code_from_meta(product_type)
+
+ # Confirm if line doesn't have included taxes in the price. If not then, we
+ # check if the current Saleor config doesn't assume that taxes are included in
+ # prices
+ line_has_included_taxes = (
+ line.unit_price_gross_amount != line.unit_price_net_amount
+ )
+ tax_included = line_has_included_taxes or system_tax_included
+
append_line_to_data(
data=data,
quantity=line.quantity,
@@ -268,9 +278,7 @@ def get_order_lines_data(
tax_code=tax_code,
item_code=line.variant.sku,
name=line.variant.product.name,
- # The orders created from checkout have already assigned taxes,
- # orders from draft doesn't have.
- tax_included=line.unit_price_gross_amount != line.unit_price_net_amount,
+ tax_included=tax_included,
)
if order.discount_amount:
append_line_to_data(
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 88ff49a404..a50581236a 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -655,9 +655,12 @@ def test_api_post_request_handles_json_errors(product, monkeypatch):
assert response == {}
-def test_get_order_request_data_checks_if_taxes_are_included_to_price(
+def test_get_order_request_data_checks_when_taxes_are_included_to_price(
order_with_lines, shipping_zone, site_settings, address_usa
):
+ site_settings.include_taxes_in_prices = True
+ site_settings.company_address = address_usa
+ site_settings.save()
method = shipping_zone.shipping_methods.get()
line = order_with_lines.lines.first()
line.unit_price_gross_amount = line.unit_price_net_amount
@@ -668,18 +671,41 @@ def test_get_order_request_data_checks_if_taxes_are_included_to_price(
order_with_lines.shipping_method = method
order_with_lines.save()
+ config = AvataxConfiguration(
+ username_or_account="", password_or_license="", use_sandbox=False,
+ )
+ request_data = get_order_request_data(order_with_lines, config)
+ lines_data = request_data["createTransactionModel"]["lines"]
+
+ assert all([line for line in lines_data])
+
+
+def test_get_order_request_data_checks_when_taxes_are_not_included_to_price(
+ order_with_lines, shipping_zone, site_settings, address_usa
+):
site_settings.company_address = address_usa
+ site_settings.include_taxes_in_prices = False
site_settings.save()
+ method = shipping_zone.shipping_methods.get()
+ line = order_with_lines.lines.first()
+ line.unit_price_gross_amount = line.unit_price_net_amount
+ line.save()
+
+ order_with_lines.shipping_address = order_with_lines.billing_address.get_copy()
+ order_with_lines.shipping_method_name = method.name
+ order_with_lines.shipping_method = method
+ order_with_lines.save()
+
config = AvataxConfiguration(
username_or_account="", password_or_license="", use_sandbox=False,
)
+
request_data = get_order_request_data(order_with_lines, config)
lines_data = request_data["createTransactionModel"]["lines"]
line_without_taxes = [line for line in lines_data if line["taxIncluded"] is False]
+ # if order line has different .net and .gross we already added tax to it
lines_with_taxes = [line for line in lines_data if line["taxIncluded"] is True]
- assert len(line_without_taxes) == 1
- assert len(lines_with_taxes) == 2
-
- assert line_without_taxes[0]["itemCode"] == line.product_sku
+ assert len(line_without_taxes) == 2
+ assert len(lines_with_taxes) == 1