Skip to content

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

Add default tax calculation strategy (#11375)

Jakub88ac529fec49
diff --git a/saleor/checkout/calculations.py b/saleor/checkout/calculations.py
index fef7b1e66c..42fa4a8133 100644
--- a/saleor/checkout/calculations.py
+++ b/saleor/checkout/calculations.py
@@ -348,7 +348,7 @@ def _calculate_and_add_tax(
# Get the taxes calculated with apps and apply to checkout.
tax_data = manager.get_taxes_for_checkout(checkout_info, lines)
_apply_tax_data(checkout, lines, tax_data)
- elif tax_calculation_strategy == TaxCalculationStrategy.FLAT_RATES:
+ else:
# Get taxes calculated with flat rates and apply to checkout.
update_checkout_prices_with_flat_rates(
checkout, checkout_info, lines, prices_entered_with_tax, address, discounts
diff --git a/saleor/checkout/tests/test_calculations.py b/saleor/checkout/tests/test_calculations.py
index 14a4a737d2..42f729c464 100644
--- a/saleor/checkout/tests/test_calculations.py
+++ b/saleor/checkout/tests/test_calculations.py
@@ -221,6 +221,44 @@ def test_fetch_checkout_prices_if_expired_flat_rates(
assert checkout.shipping_tax_rate == Decimal("0.2300")
+@patch(
+ "saleor.checkout.calculations.update_checkout_prices_with_flat_rates",
+ wraps=update_checkout_prices_with_flat_rates,
+)
+def test_fetch_checkout_prices_if_expired_flat_rates_and_no_tax_calc_strategy(
+ mocked_update_checkout_prices_with_flat_rates,
+ checkout_with_items_and_shipping,
+ fetch_kwargs,
+):
+ # given
+ checkout = checkout_with_items_and_shipping
+ tc = checkout.channel.tax_configuration
+ tc.country_exceptions.all().delete()
+ tc.prices_entered_with_tax = True
+ tc.tax_calculation_strategy = None
+ tc.save(update_fields=["prices_entered_with_tax", "tax_calculation_strategy"])
+
+ country_code = checkout.shipping_address.country.code
+ for line in checkout.lines.all():
+ line.variant.product.tax_class.country_rates.update_or_create(
+ country=country_code, rate=23
+ )
+
+ checkout.shipping_method.tax_class.country_rates.update_or_create(
+ country=country_code, rate=23
+ )
+
+ # when
+ fetch_checkout_prices_if_expired(**fetch_kwargs)
+ checkout.refresh_from_db()
+ line = checkout.lines.first()
+
+ # then
+ mocked_update_checkout_prices_with_flat_rates.assert_called_once()
+ assert line.tax_rate == Decimal("0.2300")
+ assert checkout.shipping_tax_rate == Decimal("0.2300")
+
+
@freeze_time("2020-12-12 12:00:00")
def test_fetch_checkout_prices_if_expired_webhooks_success(
plugins_manager,
diff --git a/saleor/graphql/tax/tests/queries/test_tax_configurations.py b/saleor/graphql/tax/tests/queries/test_tax_configurations.py
index 6ae1810863..20047c72eb 100644
--- a/saleor/graphql/tax/tests/queries/test_tax_configurations.py
+++ b/saleor/graphql/tax/tests/queries/test_tax_configurations.py
@@ -1,5 +1,6 @@
import graphene
+from .....tax import TaxCalculationStrategy
from .....tax.models import TaxConfiguration
from ....tests.utils import assert_no_permission, get_graphql_content
from ..fragments import TAX_CONFIGURATION_FRAGMENT
@@ -74,3 +75,21 @@ def test_tax_configurations_filter(channel_USD, staff_api_client):
edges = content["data"]["taxConfigurations"]["edges"]
assert len(edges) == 1
assert edges[0]["node"]["id"] == id
+
+
+def test_tax_configurations_query_staff_user_empty_calculation_strategy(
+ channel_USD, staff_api_client
+):
+ # given
+ tc = TaxConfiguration.objects.first()
+ tc.tax_calculation_strategy = None
+ tc.save(update_fields=["tax_calculation_strategy"])
+
+ # when
+ response = staff_api_client.post_graphql(QUERY, {})
+
+ # then
+ content = get_graphql_content(response)
+
+ node = content["data"]["taxConfigurations"]["edges"][0]["node"]
+ assert node["taxCalculationStrategy"] == TaxCalculationStrategy.FLAT_RATES
diff --git a/saleor/graphql/tax/types.py b/saleor/graphql/tax/types.py
index a30d7d9c96..a8e7e2f6b7 100644
--- a/saleor/graphql/tax/types.py
+++ b/saleor/graphql/tax/types.py
@@ -68,6 +68,10 @@ class TaxConfiguration(ModelObjectType):
root.pk
)
+ @staticmethod
+ def resolve_tax_calculation_strategy(root: models.TaxConfiguration, _info):
+ return root.tax_calculation_strategy or TaxCalculationStrategy.FLAT_RATES
+
class TaxConfigurationCountableConnection(CountableConnection):
class Meta:
diff --git a/saleor/tax/migrations/0006_alter_taxconfiguration_tax_calculation_strategy.py b/saleor/tax/migrations/0006_alter_taxconfiguration_tax_calculation_strategy.py
new file mode 100644
index 0000000000..7fb67c9212
--- /dev/null
+++ b/saleor/tax/migrations/0006_alter_taxconfiguration_tax_calculation_strategy.py
@@ -0,0 +1,24 @@
+# Generated by Django 3.2.16 on 2022-12-01 15:45
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("tax", "0005_migrate_vatlayer"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="taxconfiguration",
+ name="tax_calculation_strategy",
+ field=models.CharField(
+ blank=True,
+ choices=[("FLAT_RATES", "Flat rates"), ("TAX_APP", "Tax app")],
+ default="FLAT_RATES",
+ max_length=20,
+ null=True,
+ ),
+ ),
+ ]
diff --git a/saleor/tax/models.py b/saleor/tax/models.py
index be8b0afd8b..d695dc8339 100644
--- a/saleor/tax/models.py
+++ b/saleor/tax/models.py
@@ -54,7 +54,11 @@ class TaxConfiguration(ModelWithMetadata):
)
charge_taxes = models.BooleanField(default=True)
tax_calculation_strategy = models.CharField(
- max_length=20, choices=TaxCalculationStrategy.CHOICES, blank=True, null=True
+ max_length=20,
+ choices=TaxCalculationStrategy.CHOICES,
+ blank=True,
+ null=True,
+ default=TaxCalculationStrategy.FLAT_RATES,
)
display_gross_prices = models.BooleanField(default=True)
prices_entered_with_tax = models.BooleanField(default=True)