Skip to content

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

Fix for incorrect calculation of tax rate (#7860)

Maciej Korycinski2e78fb5bcf8b
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index 4ffeefa5f1..7bf22b650c 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -582,12 +582,9 @@ class AvataxPlugin(BasePlugin):
return base_rate
# when tax is equal to 0 tax rate for product is still provided
# in the response
- tax = Decimal(details[0].get("tax", 0.0))
- return (
- Decimal(details[0].get("rate", 0.0))
- if tax != Decimal(0.0)
- else base_rate
- )
+ tax = Decimal(sum([detail.get("tax", 0.0) for detail in details]))
+ rate = Decimal(sum([detail.get("rate", 0.0) for detail in details]))
+ return rate if tax != Decimal(0.0) else base_rate
return base_rate
@staticmethod
@@ -602,8 +599,10 @@ class AvataxPlugin(BasePlugin):
if line["itemCode"] == "Shipping":
line_details = line.get("details")
if not line_details:
- return
- return Decimal(line_details[0].get("rate", 0.0))
+ return base_rate
+ return sum(
+ [Decimal(detail.get("rate", 0.0)) for detail in line_details]
+ )
return base_rate
def assign_tax_code_to_object_meta(
diff --git a/saleor/plugins/avatax/tests/conftest.py b/saleor/plugins/avatax/tests/conftest.py
index bc97b0507b..a05500652b 100644
--- a/saleor/plugins/avatax/tests/conftest.py
+++ b/saleor/plugins/avatax/tests/conftest.py
@@ -196,7 +196,40 @@ def avalara_response_for_checkout_with_items_and_shipping():
"reportingTax": 0.93,
"reportingTaxCalculated": 0.93,
"liabilityType": "Seller",
- }
+ },
+ {
+ "id": 0,
+ "transactionLineId": 0,
+ "transactionId": 0,
+ "country": "PL",
+ "region": "PL",
+ "exemptAmount": 0.0,
+ "jurisCode": "EU",
+ "jurisName": "EUROPE",
+ "stateAssignedNo": "",
+ "jurisType": "CNT",
+ "jurisdictionType": "Country",
+ "nonTaxableAmount": 0.0,
+ "rate": 0.13,
+ "tax": 0.93,
+ "taxableAmount": 4.07,
+ "taxType": "Output",
+ "taxSubTypeId": "O",
+ "taxName": "Standard Rate",
+ "taxAuthorityTypeId": 45,
+ "taxCalculated": 0.93,
+ "rateType": "Standard",
+ "rateTypeCode": "S",
+ "unitOfBasis": "PerCurrencyUnit",
+ "isNonPassThru": False,
+ "isFee": False,
+ "reportingTaxableUnits": 4.07,
+ "reportingNonTaxableUnits": 0.0,
+ "reportingExemptUnits": 0.0,
+ "reportingTax": 0.93,
+ "reportingTaxCalculated": 0.93,
+ "liabilityType": "Seller",
+ },
],
"nonPassthroughDetails": [],
"hsCode": "",
@@ -261,7 +294,40 @@ def avalara_response_for_checkout_with_items_and_shipping():
"reportingTax": 1.87,
"reportingTaxCalculated": 1.87,
"liabilityType": "Seller",
- }
+ },
+ {
+ "id": 0,
+ "transactionLineId": 0,
+ "transactionId": 0,
+ "country": "PL",
+ "region": "PL",
+ "exemptAmount": 0.0,
+ "jurisCode": "EU",
+ "jurisName": "EUROPE",
+ "stateAssignedNo": "",
+ "jurisType": "CNT",
+ "jurisdictionType": "Country",
+ "nonTaxableAmount": 0.0,
+ "rate": 0.23,
+ "tax": 1.87,
+ "taxableAmount": 8.13,
+ "taxType": "Output",
+ "taxSubTypeId": "O",
+ "taxName": "Standard Rate",
+ "taxAuthorityTypeId": 45,
+ "taxCalculated": 1.87,
+ "rateType": "Standard",
+ "rateTypeCode": "S",
+ "unitOfBasis": "PerCurrencyUnit",
+ "isNonPassThru": False,
+ "isFee": False,
+ "reportingTaxableUnits": 8.13,
+ "reportingNonTaxableUnits": 0.0,
+ "reportingExemptUnits": 0.0,
+ "reportingTax": 1.87,
+ "reportingTaxCalculated": 1.87,
+ "liabilityType": "Seller",
+ },
],
"nonPassthroughDetails": [],
"hsCode": "",
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index f8f04b883b..70e9934333 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -1201,6 +1201,32 @@ def test_get_checkout_shipping_tax_rate(
assert tax_rate == Decimal("0.23")
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test__get_shipping_tax_rate_handles_multiple_tax_districts(
+ avalara_response_for_checkout_with_items_and_shipping, channel_USD
+):
+ manager = get_plugins_manager()
+ plugin = manager.get_plugin(AvataxPlugin.PLUGIN_ID, channel_USD.slug)
+
+ # 0.46 == sum of two tax districts
+ assert Decimal("0.46") == plugin._get_shipping_tax_rate(
+ avalara_response_for_checkout_with_items_and_shipping, Decimal(0.0)
+ ).quantize(Decimal(".01"))
+
+
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test__get_unit_tax_rate_handles_multiple_tax_districts(
+ avalara_response_for_checkout_with_items_and_shipping, channel_USD
+):
+ manager = get_plugins_manager()
+ plugin = manager.get_plugin(AvataxPlugin.PLUGIN_ID, channel_USD.slug)
+
+ # 0.36 == sum of two tax districts
+ assert Decimal("0.36") == plugin._get_unit_tax_rate(
+ avalara_response_for_checkout_with_items_and_shipping, "123", Decimal(0.0)
+ ).quantize(Decimal(".01"))
+
+
@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
def test_get_checkout_shipping_tax_rate_checkout_not_valid_default_value_returned(
monkeypatch, checkout_with_item, address, plugin_configuration
diff --git a/saleor/plugins/avatax/tests/test_avatax_caching.py b/saleor/plugins/avatax/tests/test_avatax_caching.py
index b11b74b9e6..e7f67ba774 100644
--- a/saleor/plugins/avatax/tests/test_avatax_caching.py
+++ b/saleor/plugins/avatax/tests/test_avatax_caching.py
@@ -527,7 +527,7 @@ def test_get_checkout_line_tax_rate_use_cache(
)
# when
- assert result == Decimal("0.23")
+ assert result == Decimal("0.36")
avalara_cache_key = CACHE_KEY + str(checkout.token)
mocked_cache.assert_called_with(avalara_cache_key)
@@ -581,7 +581,7 @@ def test_get_checkout_line_tax_rate_save_avatax_response_in_cache(
# Second Avatax call to make sure that we use cached response
# when
- assert result == Decimal("0.23")
+ assert result == Decimal("0.36")
avalara_request_data = generate_request_data_from_checkout(
checkout_info, lines, plugin.config, []
@@ -629,7 +629,7 @@ def test_get_checkout_shipping_tax_rate_use_cache(
)
# when
- assert result == Decimal("0.23")
+ assert result == Decimal("0.46")
avalara_cache_key = CACHE_KEY + str(checkout.token)
mocked_cache.assert_called_with(avalara_cache_key)
@@ -672,7 +672,7 @@ def test_get_checkout_shipping_tax_rate_save_avatax_response_in_cache(
# Second Avatax call to make sure that we use cached response
# when
- assert result == Decimal("0.23")
+ assert result == Decimal("0.46")
avalara_request_data = generate_request_data_from_checkout(
checkout_info, lines, plugin.config, []