Skip to content

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

Use different tax code when amount is zero -3.1 (#8615)

Maciej Korycinskica8ae1b37c31
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7818a4c86d..f1697c6a1a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -168,6 +168,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add `customerId` to `checkoutCustomerAttach` mutation
- Add new permision `IMPERSONATE_USER`
- Handle `SameSite` cookie attribute in jwt refresh token middleware - #8209 by @jakubkuc
+- Add workaround for failing Avatax when line has price 0 - #8610 by @korycins
### Breaking
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index 4aeb5379e5..6125773338 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -254,15 +254,21 @@ def get_checkout_lines_data(
item_code = line_info.variant.sku or line_info.variant.get_global_id()
tax_code = retrieve_tax_code_from_meta(product, default=None)
tax_code = tax_code or retrieve_tax_code_from_meta(product_type)
+ amount = base_calculations.base_checkout_line_total(
+ line_info,
+ channel,
+ discounts,
+ ).gross.amount
append_line_to_data(
data=data,
quantity=line_info.line.quantity,
- amount=base_calculations.base_checkout_line_total(
- line_info,
- channel,
- discounts,
- ).gross.amount,
- tax_code=tax_code,
+ amount=amount,
+ # This is a workaround for Avatax and sending a lines with amount 0. Like
+ # order lines which are fully discounted for some reason. If we use a
+ # standard tax_code, Avatax will raise an exception: "When shipping
+ # cross-border into CIF countries, Tax Included is not supported with mixed
+ # positive and negative line amounts."
+ tax_code=tax_code if amount else DEFAULT_TAX_CODE,
item_code=item_code,
name=name,
)
@@ -299,11 +305,17 @@ def get_order_lines_data(
line.unit_price_gross_amount != line.unit_price_net_amount
)
tax_included = line_has_included_taxes or system_tax_included
+ amount = line.unit_price_gross_amount * line.quantity
append_line_to_data(
data=data,
quantity=line.quantity,
- amount=line.unit_price_gross_amount * line.quantity,
- tax_code=tax_code,
+ amount=amount,
+ # This is a workaround for Avatax and sending a lines with amount 0. Like
+ # order lines which are fully discounted for some reason. If we use a
+ # standard tax_code, Avatax will raise an exception: "When shipping
+ # cross-border into CIF countries, Tax Included is not supported with mixed
+ # positive and negative line amounts."
+ tax_code=tax_code if amount else DEFAULT_TAX_CODE,
item_code=line.variant.sku or line.variant.get_global_id(),
name=line.variant.product.name,
tax_included=tax_included,
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 0cc9a19145..7d816edc28 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -25,6 +25,7 @@ from ....shipping.utils import convert_to_shipping_method_data
from ...manager import get_plugins_manager
from ...models import PluginConfiguration
from .. import (
+ DEFAULT_TAX_CODE,
META_CODE_KEY,
META_DESCRIPTION_KEY,
AvataxConfiguration,
@@ -34,6 +35,8 @@ from .. import (
api_post_request,
generate_request_data_from_checkout,
get_cached_tax_codes_or_fetch,
+ get_checkout_lines_data,
+ get_order_lines_data,
get_order_request_data,
get_order_tax_data,
taxes_need_new_fetch,
@@ -2038,3 +2041,117 @@ def test_validate_adddress_details(
shipping_address, is_shipping_required, address, shipping_method
)
assert is_valid is expected_is_valid
+
+
+def test_get_checkout_lines_data_sets_different_tax_code_for_zero_amount(
+ settings, channel_USD, plugin_configuration, checkout_with_item
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.avatax.plugin.AvataxPlugin"]
+ plugin_configuration(channel=channel_USD)
+
+ line = checkout_with_item.lines.first()
+ variant = line.variant
+ variant.channel_listings.all().update(price_amount=Decimal("0"))
+ variant.product.store_value_in_metadata(
+ {META_CODE_KEY: "taxcode", META_DESCRIPTION_KEY: "tax_description"}
+ )
+ variant.product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, lines, [], get_plugins_manager()
+ )
+
+ # when
+ lines_data = get_checkout_lines_data(
+ checkout_info,
+ lines,
+ )
+
+ # then
+ assert lines_data[0]["amount"] == "0.00"
+ assert lines_data[0]["taxCode"] == DEFAULT_TAX_CODE
+
+
+def test_get_checkout_lines_data_sets_different_tax_code_only_for_zero_amount(
+ settings, channel_USD, plugin_configuration, checkout_with_item
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.avatax.plugin.AvataxPlugin"]
+ plugin_configuration(channel=channel_USD)
+
+ line = checkout_with_item.lines.first()
+ line.quantity = 1
+ line.save()
+
+ variant = line.variant
+ variant.channel_listings.all().update(price_amount=Decimal("11"))
+ variant.product.store_value_in_metadata(
+ {META_CODE_KEY: "taxcode", META_DESCRIPTION_KEY: "tax_description"}
+ )
+ variant.product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, lines, [], get_plugins_manager()
+ )
+
+ # when
+ lines_data = get_checkout_lines_data(
+ checkout_info,
+ lines,
+ )
+
+ # then
+ assert lines_data[0]["amount"] == "11.00"
+ assert lines_data[0]["taxCode"] == "taxcode"
+
+
+def test_get_order_lines_data_sets_different_tax_code_for_zero_amount(
+ settings, channel_USD, plugin_configuration, order_with_lines
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.avatax.plugin.AvataxPlugin"]
+ plugin_configuration(channel=channel_USD)
+
+ line = order_with_lines.lines.first()
+ line.unit_price_gross_amount = Decimal("0")
+ line.save()
+ variant = line.variant
+ variant.product.store_value_in_metadata(
+ {META_CODE_KEY: "taxcode", META_DESCRIPTION_KEY: "tax_description"}
+ )
+ variant.product.save()
+
+ # when
+ lines_data = get_order_lines_data(order_with_lines)
+
+ # then
+ assert lines_data[0]["amount"] == "0.000"
+ assert lines_data[0]["taxCode"] == DEFAULT_TAX_CODE
+
+
+def test_get_order_lines_data_sets_different_tax_code_only_for_zero_amount(
+ settings, channel_USD, plugin_configuration, order_with_lines
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.avatax.plugin.AvataxPlugin"]
+ plugin_configuration(channel=channel_USD)
+
+ line = order_with_lines.lines.first()
+ line.quantity = 1
+ line.unit_price_gross_amount = Decimal("10.0")
+ line.save()
+ variant = line.variant
+ variant.product.store_value_in_metadata(
+ {META_CODE_KEY: "taxcode", META_DESCRIPTION_KEY: "tax_description"}
+ )
+ variant.product.save()
+
+ # when
+ lines_data = get_order_lines_data(order_with_lines)
+
+ # then
+ assert lines_data[0]["amount"] == "10.000"
+ assert lines_data[0]["taxCode"] == "taxcode"