Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Set flat rates as default tax calculation strategy (#12069)
Marcin Gębalab5e99bb1c822
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e0210e0c15..5ea31b940a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -62,6 +62,8 @@ All notable, unreleased changes to this project will be documented in this file.- `THUMBNAIL_CREATED`- CORS is now handled in the ASGI layer - #11415 by @patrys- Added native support for gzip compression - #11833 by @patrys+- Set flat rates as the default tax calculation strategy - #12069 by @maarcingebala+ - Enables flat rates for channels in which no tax calculation method was set.# 3.11.0diff --git a/saleor/graphql/tax/tests/queries/test_tax_configurations.py b/saleor/graphql/tax/tests/queries/test_tax_configurations.pyindex 20047c72eb..6ae1810863 100644--- a/saleor/graphql/tax/tests/queries/test_tax_configurations.py+++ b/saleor/graphql/tax/tests/queries/test_tax_configurations.py@@ -1,6 +1,5 @@import graphene-from .....tax import TaxCalculationStrategyfrom .....tax.models import TaxConfigurationfrom ....tests.utils import assert_no_permission, get_graphql_contentfrom ..fragments import TAX_CONFIGURATION_FRAGMENT@@ -75,21 +74,3 @@ def test_tax_configurations_filter(channel_USD, staff_api_client):edges = content["data"]["taxConfigurations"]["edges"]assert len(edges) == 1assert 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_RATESdiff --git a/saleor/graphql/tax/types.py b/saleor/graphql/tax/types.pyindex d3132c29de..654922ba26 100644--- a/saleor/graphql/tax/types.py+++ b/saleor/graphql/tax/types.py@@ -69,12 +69,6 @@ class TaxConfiguration(ModelObjectType[models.TaxConfiguration]):root.pk)- @staticmethod- def resolve_tax_calculation_strategy(- root: models.TaxConfiguration, _info: ResolveInfo- ):- return root.tax_calculation_strategy or TaxCalculationStrategy.FLAT_RATES-class TaxConfigurationCountableConnection(CountableConnection):class Meta:diff --git a/saleor/order/calculations.py b/saleor/order/calculations.pyindex 8b23de78b9..5f2729d6e5 100644--- a/saleor/order/calculations.py+++ b/saleor/order/calculations.py@@ -253,7 +253,7 @@ def _calculate_and_add_tax(_recalculate_order_prices(manager, order, lines)tax_data = manager.get_taxes_for_order(order)_apply_tax_data(order, lines, tax_data)- elif tax_calculation_strategy == TaxCalculationStrategy.FLAT_RATES:+ else:update_order_prices_with_flat_rates(order, lines, prices_entered_with_tax)diff --git a/saleor/order/tests/test_calculations.py b/saleor/order/tests/test_calculations.pyindex cec68bb229..a6ee0bc7b1 100644--- a/saleor/order/tests/test_calculations.py+++ b/saleor/order/tests/test_calculations.py@@ -766,6 +766,39 @@ def test_fetch_order_prices_if_expired_use_base_shipping_price()[email protected]("prices_entered_with_tax", [True, False])+def test_fetch_order_prices_if_expired_flat_rates_and_no_tax_calc_strategy(+ order_with_lines,+ fetch_kwargs,+ prices_entered_with_tax,+):+ # given+ order = order_with_lines+ tc = order.channel.tax_configuration+ tc.country_exceptions.all().delete()+ tc.prices_entered_with_tax = prices_entered_with_tax+ tc.tax_calculation_strategy = None+ tc.save(update_fields=["prices_entered_with_tax", "tax_calculation_strategy"])++ country_code = order.shipping_address.country.code+ for line in order.lines.all():+ line.variant.product.tax_class.country_rates.update_or_create(+ country=country_code, rate=23+ )++ order.shipping_method.tax_class.country_rates.update_or_create(+ country=country_code, rate=23+ )++ # when+ calculations.fetch_order_prices_if_expired(**fetch_kwargs)+ order.refresh_from_db()+ line = order.lines.first()++ assert line.tax_rate == Decimal("0.2300")+ assert order.shipping_tax_rate == Decimal("0.2300")++@patch("saleor.order.calculations.fetch_order_prices_if_expired")def test_order_line_unit(mocked_fetch_order_prices_if_expired):# givendiff --git a/saleor/tax/migrations/0007_auto_20230217_0837.py b/saleor/tax/migrations/0007_auto_20230217_0837.pynew file mode 100644index 0000000000..1bb163dd0b--- /dev/null+++ b/saleor/tax/migrations/0007_auto_20230217_0837.py@@ -0,0 +1,24 @@+# Generated by Django 3.2.18 on 2023-02-17 08:37++from django.db import migrations++from .. import TaxCalculationStrategy+++def set_flat_rates_as_default_strategy(apps, schema_editor):+ TaxConfiguration = apps.get_model("tax", "TaxConfiguration")+ TaxConfiguration.objects.filter(tax_calculation_strategy=None).update(+ tax_calculation_strategy=TaxCalculationStrategy.FLAT_RATES+ )+++class Migration(migrations.Migration):+ dependencies = [+ ("tax", "0006_alter_taxconfiguration_tax_calculation_strategy"),+ ]++ operations = [+ migrations.RunPython(+ set_flat_rates_as_default_strategy, migrations.RunPython.noop+ )+ ]diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 99abe24b0b..b81b491e28 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -4034,6 +4034,7 @@ def order_with_lines(slug="test-product-9",product_type=product_type,category=category,+ tax_class=default_tax_class,)ProductChannelListing.objects.create(product=product,