Skip to content

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

Add fields for calculating taxes from source country and no VAT

Maciej Korycinski10f29fd9c925
diff --git a/saleor/plugins/vatlayer/__init__.py b/saleor/plugins/vatlayer/__init__.py
index 9c896be943..c6181e8f32 100644
--- a/saleor/plugins/vatlayer/__init__.py
+++ b/saleor/plugins/vatlayer/__init__.py
@@ -1,4 +1,5 @@
from dataclasses import dataclass
+from typing import List, Optional
from django_prices_vatlayer.utils import get_tax_for_rate, get_tax_rates_for_country
from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
@@ -68,6 +69,9 @@ DEFAULT_TAX_RATE_NAME = TaxRateType.STANDARD
@dataclass
class VatlayerConfiguration:
access_key: str
+ excluded_countries: Optional[List[str]]
+ countries_from_source: Optional[List[str]]
+ source_country: Optional[str]
def _convert_to_naive_taxed_money(base, taxes, rate_name):
diff --git a/saleor/plugins/vatlayer/plugin.py b/saleor/plugins/vatlayer/plugin.py
index 2119a04b74..08c0825b6b 100644
--- a/saleor/plugins/vatlayer/plugin.py
+++ b/saleor/plugins/vatlayer/plugin.py
@@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Any, Iterable, List, Optional, Union
from django.conf import settings
from django.core.exceptions import ValidationError
+from django_countries import countries
from django_countries.fields import Country
from django_prices_vatlayer.utils import (
fetch_rate_types,
@@ -48,8 +49,39 @@ class VatlayerPlugin(BasePlugin):
PLUGIN_NAME = "Vatlayer"
META_CODE_KEY = "vatlayer.code"
META_DESCRIPTION_KEY = "vatlayer.description"
- DEFAULT_CONFIGURATION = [{"name": "Access key", "value": None}]
+
+ DEFAULT_CONFIGURATION = [
+ {"name": "Access key", "value": None},
+ {"name": "source_country", "value": None},
+ {"name": "countries_to_calculate_taxes_from_source", "value": None},
+ {"name": "excluded_countries", "value": None},
+ ]
+
CONFIG_STRUCTURE = {
+ "source_country": {
+ "type": ConfigurationTypeField.STRING,
+ "help_test": (
+ "Country code in ISO format, required to calculate taxes for countries "
+ "from `Calculate taxes based on source country`."
+ ),
+ "label": "Source country",
+ },
+ "countries_to_calculate_taxes_from_source": {
+ "type": ConfigurationTypeField.STRING,
+ "help_text": (
+ "List of destination countries (separated by comma), in ISO format which "
+ "will use source country to calculate taxes."
+ ),
+ "label": "Countries for which taxes will be calculated from source country",
+ },
+ "excluded_countries": {
+ "type": ConfigurationTypeField.STRING,
+ "help_text": (
+ "List of countries (separated by comma), in ISO format for which no VAT "
+ "should be added."
+ ),
+ "label": "Countries for which no VAT will be added.",
+ },
"Access key": {
"type": ConfigurationTypeField.PASSWORD,
"help_text": "Required to authenticate to Vatlayer API.",
@@ -61,7 +93,31 @@ class VatlayerPlugin(BasePlugin):
super().__init__(*args, **kwargs)
# Convert to dict to easier take config elements
configuration = {item["name"]: item["value"] for item in self.configuration}
- self.config = VatlayerConfiguration(access_key=configuration["Access key"])
+
+ source_country = configuration["source_country"] or ""
+ source_country = countries.alpha2(source_country.strip())
+
+ countries_from_source = configuration[
+ "countries_to_calculate_taxes_from_source"
+ ]
+ countries_from_source = countries_from_source or ""
+ countries_from_source = [
+ countries.alpha2(c.strip()) for c in countries_from_source.split(",")
+ ]
+ countries_from_source = list(filter(None, countries_from_source))
+
+ excluded_countries = configuration["excluded_countries"] or ""
+ excluded_countries = [
+ countries.alpha2(c.strip()) for c in excluded_countries.split(",")
+ ]
+ excluded_countries = list(filter(None, excluded_countries))
+
+ self.config = VatlayerConfiguration(
+ access_key=configuration["Access key"],
+ source_country=source_country,
+ excluded_countries=excluded_countries,
+ countries_from_source=countries_from_source,
+ )
self._cached_taxes = {}
def _skip_plugin(
@@ -118,10 +174,20 @@ class VatlayerPlugin(BasePlugin):
from cache or db.
"""
if not country:
- country = Country(settings.DEFAULT_COUNTRY)
+ source_country_code = self.config.source_country or settings.DEFAULT_COUNTRY
+ country = Country(source_country_code)
country_code = country.code
+
+ if country_code in self.config.countries_from_source:
+ country_code = self.config.source_country
+
+ if country_code in self.config.excluded_countries:
+ return None
+
if country_code in self._cached_taxes:
return self._cached_taxes[country_code]
+
+ country = Country(country_code)
taxes = get_taxes_for_country(country)
self._cached_taxes[country_code] = taxes
return taxes
@@ -458,3 +524,17 @@ class VatlayerPlugin(BasePlugin):
)
}
)
+ countries_from_source = configuration.get(
+ "countries_to_calculate_taxes_from_source"
+ )
+ source_country = configuration.get("source_country")
+ if countries_from_source and not source_country:
+ raise ValidationError(
+ {
+ "source_country": ValidationError(
+ "Source country required when `Countries for which taxes will "
+ "be calculated from source country` provided.",
+ code=PluginErrorCode.INVALID.value,
+ )
+ }
+ )
diff --git a/saleor/plugins/vatlayer/tests/conftest.py b/saleor/plugins/vatlayer/tests/conftest.py
new file mode 100644
index 0000000000..b3075bc824
--- /dev/null
+++ b/saleor/plugins/vatlayer/tests/conftest.py
@@ -0,0 +1,39 @@
+from unittest.mock import patch
+
+import pytest
+
+from ...manager import get_plugins_manager
+from ..plugin import VatlayerPlugin
+
+
+def vatlayer_plugin(settings, vatlayer):
+ def fun(
+ active=True,
+ access_key="key",
+ source_country=None,
+ countries_to_calculate_taxes_from_source=None,
+ excluded_countries=None,
+ ):
+ settings.PLUGINS = ["saleor.plugins.vatlayer.plugin.VatlayerPlugin"]
+ manager = get_plugins_manager()
+ with patch("saleor.plugins.vatlayer.plugin.fetch_rate_types"):
+ manager.save_plugin_configuration(
+ VatlayerPlugin.PLUGIN_ID,
+ {
+ "active": active,
+ "configuration": [
+ {"name": "Access key", "value": access_key},
+ {"name": "source_country", "value": source_country},
+ {
+ "name": "countries_to_calculate_taxes_from_source",
+ "value": countries_to_calculate_taxes_from_source,
+ },
+ {"name": "excluded_countries", "value": excluded_countries},
+ ],
+ },
+ )
+ manager = get_plugins_manager()
+ return manager.plugins[0]
+
+ return fun
diff --git a/saleor/plugins/vatlayer/tests/test_vatlayer.py b/saleor/plugins/vatlayer/tests/test_vatlayer.py
index 3d274d5a11..97ae244841 100644
--- a/saleor/plugins/vatlayer/tests/test_vatlayer.py
+++ b/saleor/plugins/vatlayer/tests/test_vatlayer.py
@@ -253,6 +253,77 @@ def test_calculate_checkout_total(
)
+def test_calculate_checkout_total_from_source_country(
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+ discount_info,
+):
+ plugin = vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ plugin.assign_tax_code_to_object_meta(product, "standard", None)
+ product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, lines, [discount_info], manager
+ )
+ total = manager.calculate_checkout_total(
+ checkout_info, lines, address, [discount_info]
+ )
+ total = quantize_price(total, total.currency)
+
+ # make sure that address has PL code
+ assert address.country.code == "PL"
+
+ # make sure that we applied DE taxes (19%)
+ assert total == TaxedMoney(net=Money("21.00", "USD"), gross=Money("25.00", "USD"))
+
+
+def test_calculate_checkout_total_with_excluded_country(
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+ discount_info,
+):
+ plugin = vatlayer_plugin(source_country="PL", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ plugin.assign_tax_code_to_object_meta(product, "standard", None)
+ product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, lines, [discount_info], manager
+ )
+ total = manager.calculate_checkout_total(
+ checkout_info, lines, address, [discount_info]
+ )
+ total = quantize_price(total, total.currency)
+
+ # make sure that we not have VAT
+ assert total == TaxedMoney(net=Money("25.00", "USD"), gross=Money("25.00", "USD"))
+
+
@pytest.mark.parametrize(
"with_discount, expected_net, expected_gross, taxes_in_prices",
[
@@ -303,6 +374,79 @@ def test_calculate_checkout_subtotal(
)
+def test_calculate_checkout_subtotal_from_source_country(
+ site_settings,
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+ discount_info,
+):
+ plugin = vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ plugin.assign_tax_code_to_object_meta(product, "standard", None)
+ product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, lines, [discount_info], manager
+ )
+ total = manager.calculate_checkout_subtotal(
+ checkout_info, lines, address, [discount_info]
+ )
+ total = quantize_price(total, total.currency)
+
+ # make sure that address has PL code
+ assert address.country.code == "PL"
+
+ # make sure that we applied DE taxes (19%)
+ assert total == TaxedMoney(net=Money("12.60", "USD"), gross=Money("15.00", "USD"))
+
+
+def test_calculate_checkout_subtotal_with_excluded_country(
+ site_settings,
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+ discount_info,
+):
+ plugin = vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ plugin.assign_tax_code_to_object_meta(product, "standard", None)
+ product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, lines, [discount_info], manager
+ )
+ total = manager.calculate_checkout_subtotal(
+ checkout_info, lines, address, [discount_info]
+ )
+ total = quantize_price(total, total.currency)
+
+ # make sure that we not have VAT
+ assert total == TaxedMoney(net=Money("15.00", "USD"), gross=Money("15.00", "USD"))
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_calculate_order_shipping(vatlayer, order_line, shipping_zone, site_settings):
manager = get_plugins_manager()
@@ -317,6 +461,50 @@ def test_calculate_order_shipping(vatlayer, order_line, shipping_zone, site_sett
assert price == TaxedMoney(net=Money("8.13", "USD"), gross=Money("10.00", "USD"))
+def test_calculate_order_shipping_from_source_country(
+ vatlayer_plugin, order_line, shipping_zone, site_settings
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ order = order_line.order
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = order.billing_address.get_copy()
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ price = manager.calculate_order_shipping(order)
+ price = quantize_price(price, price.currency)
+
+ # make sure that address has PL code
+ assert order.shipping_address.country.code == "PL"
+
+ # make sure that we applied DE taxes (19%)
+ assert price == TaxedMoney(net=Money("8.40", "USD"), gross=Money("10.00", "USD"))
+
+
+def test_calculate_order_shipping_with_excluded_country(
+ vatlayer_plugin, order_line, shipping_zone, site_settings
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ order = order_line.order
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = order.billing_address.get_copy()
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ price = manager.calculate_order_shipping(order)
+ price = quantize_price(price, price.currency)
+
+ assert price == TaxedMoney(net=Money("10.00", "USD"), gross=Money("10.00", "USD"))
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_calculate_order_shipping_for_order_without_shipping(
vatlayer, order_line, shipping_zone, site_settings
@@ -356,6 +544,71 @@ def test_calculate_order_line_unit(vatlayer, order_line, shipping_zone, site_set
)
+def test_calculate_order_line_unit_from_source_country(
+ vatlayer_plugin, order_line, shipping_zone, site_settings
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ order_line.unit_price = TaxedMoney(
+ net=Money("10.00", "USD"), gross=Money("10.00", "USD")
+ )
+ order_line.save()
+
+ order = order_line.order
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = order.billing_address.get_copy()
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ variant = order_line.variant
+ product = variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ line_price = manager.calculate_order_line_unit(order, order_line, variant, product)
+ line_price = quantize_price(line_price, line_price.currency)
+
+ # make sure that we applied DE taxes (19%)
+ assert line_price == TaxedMoney(
+ net=Money("8.40", "USD"), gross=Money("10.00", "USD")
+ )
+
+
+def test_calculate_order_line_unit_with_excluded_country(
+ vatlayer_plugin, order_line, shipping_zone, site_settings
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ order_line.unit_price = TaxedMoney(
+ net=Money("10.00", "USD"), gross=Money("10.00", "USD")
+ )
+ order_line.save()
+
+ order = order_line.order
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = order.billing_address.get_copy()
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ variant = order_line.variant
+ product = variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ line_price = manager.calculate_order_line_unit(order, order_line, variant, product)
+ line_price = quantize_price(line_price, line_price.currency)
+
+ assert line_price == TaxedMoney(
+ net=Money("10.00", "USD"), gross=Money("10.00", "USD")
+ )
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_calculate_checkout_line_total(
vatlayer, checkout_with_item, shipping_zone, address, site_settings
@@ -394,6 +647,85 @@ def test_calculate_checkout_line_total(
)
+def test_calculate_checkout_line_total_from_source_country(
+ vatlayer_plugin, checkout_with_item, shipping_zone, address, site_settings
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ line = checkout_with_item.lines.first()
+ assert line.quantity > 1
+
+ method = shipping_zone.shipping_methods.get()
+ checkout_with_item.shipping_address = address
+ checkout_with_item.shipping_method_name = method.name
+ checkout_with_item.shipping_method = method
+ checkout_with_item.save()
+
+ variant = line.variant
+ product = variant.product
+ manager.assign_tax_code_to_object_meta(variant.product, "standard")
+ product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, [], manager)
+ checkout_line_info = lines[0]
+
+ line_price = manager.calculate_checkout_line_total(
+ checkout_info,
+ lines,
+ checkout_line_info,
+ address,
+ [],
+ )
+
+ # make sure that we applied DE taxes (19%)
+ assert line_price == TaxedMoney(
+ net=Money("8.40", "USD") * line.quantity,
+ gross=Money("10.00", "USD") * line.quantity,
+ )
+
+
+def test_calculate_checkout_line_total_with_excluded_country(
+ vatlayer_plugin, checkout_with_item, shipping_zone, address, site_settings
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ line = checkout_with_item.lines.first()
+ assert line.quantity > 1
+
+ method = shipping_zone.shipping_methods.get()
+ checkout_with_item.shipping_address = address
+ checkout_with_item.shipping_method_name = method.name
+ checkout_with_item.shipping_method = method
+ checkout_with_item.save()
+
+ variant = line.variant
+ product = variant.product
+ manager.assign_tax_code_to_object_meta(variant.product, "standard")
+ product.save()
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, [], manager)
+ checkout_line_info = lines[0]
+
+ line_price = manager.calculate_checkout_line_total(
+ checkout_info,
+ lines,
+ checkout_line_info,
+ address,
+ [],
+ )
+
+ assert line_price == TaxedMoney(
+ net=Money("10.00", "USD") * line.quantity,
+ gross=Money("10.00", "USD") * line.quantity,
+ )
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_calculate_checkout_line_unit_price(
vatlayer, checkout_with_item, shipping_zone, address, site_settings
@@ -443,6 +775,32 @@ def test_get_tax_rate_percentage_value(
assert tax_rate == Decimal("23")
+def test_get_tax_rate_percentage_value_from_source_country(
+ vatlayer_plugin, order_line, shipping_zone, site_settings, product
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ country = Country("PL")
+ tax_rate = manager.get_tax_rate_percentage_value(product, country)
+ # make sure that we return DE tax rate
+ assert tax_rate == Decimal("19")
+
+
+def test_get_tax_rate_percentage_value_with_excluded_country(
+ vatlayer_plugin, order_line, shipping_zone, site_settings, product
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ country = Country("PL")
+ tax_rate = manager.get_tax_rate_percentage_value(product, country)
+
+ assert tax_rate == Decimal("0")
+
+
def test_save_plugin_configuration(vatlayer, settings):
settings.PLUGINS = ["saleor.plugins.vatlayer.plugin.VatlayerPlugin"]
manager = get_plugins_manager()
@@ -508,7 +866,59 @@ def test_apply_taxes_to_product(
),
country,
)
- assert price == TaxedMoney(net=Money("4.07", "USD"), gross=Money("5.00", "USD"))
+ assert price == TaxedMoney(net=Money("4.07", "USD"), gross=Money("5.00", "USD"))
+
+
+def test_apply_taxes_to_product_from_source_country(
+ vatlayer_plugin, settings, variant, discount_info, channel_USD
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ country = Country("PL")
+
+ variant.product.metadata = {
+ "vatlayer.code": "standard",
+ "vatlayer.description": "standard",
+ }
+ variant_channel_listing = variant.channel_listings.get(channel=channel_USD)
+
+ price = manager.apply_taxes_to_product(
+ variant.product,
+ variant.get_price(
+ variant.product, [], channel_USD, variant_channel_listing, [discount_info]
+ ),
+ country,
+ )
+
+ assert price == TaxedMoney(net=Money("4.20", "USD"), gross=Money("5.00", "USD"))
+
+
+def test_apply_taxes_to_product_with_excluded_country(
+ vatlayer_plugin, settings, variant, discount_info, channel_USD
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ country = Country("PL")
+
+ variant.product.metadata = {
+ "vatlayer.code": "standard",
+ "vatlayer.description": "standard",
+ }
+ variant_channel_listing = variant.channel_listings.get(channel=channel_USD)
+
+ price = manager.apply_taxes_to_product(
+ variant.product,
+ variant.get_price(
+ variant.product, [], channel_USD, variant_channel_listing, [discount_info]
+ ),
+ country,
+ )
+
+ assert price == TaxedMoney(net=Money("5.00", "USD"), gross=Money("5.00", "USD"))
def test_apply_taxes_to_product_uses_taxes_from_product_type(
@@ -639,6 +1049,86 @@ def test_get_checkout_line_tax_rate(
assert tax_rate == Decimal("0.230")
+def test_get_checkout_line_tax_rate_from_source_country(
+ site_settings,
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ unit_price = TaxedMoney(Money(12, "USD"), Money(15, "USD"))
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, [], manager)
+ checkout_line_info = lines[0]
+ tax_rate = manager.get_checkout_line_tax_rate(
+ checkout_info,
+ lines,
+ checkout_line_info,
+ checkout_with_item.shipping_address,
+ [],
+ unit_price,
+ )
+ assert tax_rate == Decimal("0.190")
+
+
+def test_get_checkout_line_tax_rate_with_excluded_country(
+ site_settings,
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ unit_price = TaxedMoney(Money(15, "USD"), Money(15, "USD"))
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, [], manager)
+ checkout_line_info = lines[0]
+ tax_rate = manager.get_checkout_line_tax_rate(
+ checkout_info,
+ lines,
+ checkout_line_info,
+ checkout_with_item.shipping_address,
+ [],
+ unit_price,
+ )
+ assert tax_rate == Decimal("0")
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_get_checkout_line_tax_rate_order_not_valid(
site_settings,
@@ -672,6 +1162,76 @@ def test_get_checkout_line_tax_rate_order_not_valid(
assert tax_rate == Decimal("0.25")
+def test_get_order_line_tax_rate_from_source_country(
+ site_settings,
+ vatlayer_plugin,
+ order_line,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ order = order_line.order
+ product = Product.objects.get(name=order_line.product_name)
+ product.save()
+
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = address
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ unit_price = TaxedMoney(Money(12, "USD"), Money(15, "USD"))
+
+ tax_rate = manager.get_order_line_tax_rate(
+ order, product, order_line.variant, address, unit_price
+ )
+ assert tax_rate == Decimal("0.190")
+
+
+def test_get_order_line_tax_rate_with_excluded_country(
+ site_settings,
+ vatlayer_plugin,
+ order_line,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ order = order_line.order
+ product = Product.objects.get(name=order_line.product_name)
+ product.save()
+
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = address
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ unit_price = TaxedMoney(Money(15, "USD"), Money(15, "USD"))
+
+ tax_rate = manager.get_order_line_tax_rate(
+ order, product, order_line.variant, address, unit_price
+ )
+ assert tax_rate == Decimal("0")
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_get_order_line_tax_rate(
site_settings,
@@ -766,6 +1326,84 @@ def test_get_checkout_shipping_tax_rate(
assert tax_rate == Decimal("0.230")
+def test_get_checkout_shipping_tax_rate_from_source_country(
+ site_settings,
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ unit_price = TaxedMoney(Money(12, "USD"), Money(15, "USD"))
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, [], manager)
+
+ tax_rate = manager.get_checkout_shipping_tax_rate(
+ checkout_info,
+ lines,
+ checkout_with_item.shipping_address,
+ [],
+ unit_price,
+ )
+ assert tax_rate == Decimal("0.190")
+
+
+def test_get_checkout_shipping_tax_rate_with_excluded_country(
+ site_settings,
+ vatlayer_plugin,
+ checkout_with_item,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ checkout_with_item.shipping_address = address
+ checkout_with_item.save()
+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.save()
+
+ line = checkout_with_item.lines.first()
+ product = line.variant.product
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ unit_price = TaxedMoney(Money(15, "USD"), Money(15, "USD"))
+
+ lines = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, [], manager)
+
+ tax_rate = manager.get_checkout_shipping_tax_rate(
+ checkout_info,
+ lines,
+ checkout_with_item.shipping_address,
+ [],
+ unit_price,
+ )
+ assert tax_rate == Decimal("0")
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_get_checkout_shipping_tax_rate_no_address(
site_settings,
@@ -866,6 +1504,72 @@ def test_get_order_shipping_tax_rate(
assert tax_rate == Decimal("0.230")
+def test_get_order_shipping_tax_rate_from_source_country(
+ site_settings,
+ vatlayer_plugin,
+ order_line,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(
+ source_country="DE", countries_to_calculate_taxes_from_source="PL,FR"
+ )
+ manager = get_plugins_manager()
+
+ order = order_line.order
+ product = Product.objects.get(name=order_line.product_name)
+ product.save()
+
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = address
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ shipping_price = TaxedMoney(Money(12, "USD"), Money(15, "USD"))
+
+ tax_rate = manager.get_order_shipping_tax_rate(order, shipping_price)
+ assert tax_rate == Decimal("0.190")
+
+
+def test_get_order_shipping_tax_rate_with_excluded_country(
+ site_settings,
+ vatlayer_plugin,
+ order_line,
+ address,
+ shipping_zone,
+):
+ vatlayer_plugin(source_country="DE", excluded_countries="PL,FR")
+ manager = get_plugins_manager()
+
+ order = order_line.order
+ product = Product.objects.get(name=order_line.product_name)
+ product.save()
+
+ method = shipping_zone.shipping_methods.get()
+ order.shipping_address = address
+ order.shipping_method_name = method.name
+ order.shipping_method = method
+ order.save()
+
+ manager.assign_tax_code_to_object_meta(product, "standard")
+ product.save()
+
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ shipping_price = TaxedMoney(Money(15, "USD"), Money(15, "USD"))
+
+ tax_rate = manager.get_order_shipping_tax_rate(order, shipping_price)
+ assert tax_rate == Decimal("0")
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_get_order_shipping_tax_rate_no_address_given(
site_settings,