Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Do not call Avatax when using flat rate (#15038)
Maciej Korycinski83f1e58d07e6
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.pyindex 4dcd5e6410..9641475aa7 100644--- a/saleor/plugins/avatax/plugin.py+++ b/saleor/plugins/avatax/plugin.py@@ -18,7 +18,13 @@ from ...checkout.fetch import fetch_checkout_linesfrom ...core.taxes import TaxError, TaxType, zero_taxed_moneyfrom ...order.interface import OrderTaxedPricesDatafrom ...product.models import ProductType-from ...tax.utils import get_charge_taxes_for_checkout, get_charge_taxes_for_order+from ...tax import TaxCalculationStrategy+from ...tax.utils import (+ get_charge_taxes_for_checkout,+ get_charge_taxes_for_order,+ get_tax_calculation_strategy_for_checkout,+ get_tax_calculation_strategy_for_order,+)from ..base_plugin import BasePlugin, ConfigurationTypeFieldfrom ..error_codes import PluginErrorCodefrom . import (@@ -311,6 +317,10 @@ class AvataxPlugin(BasePlugin):if self._skip_plugin(previous_value):return previous_value+ tax_strategy = get_tax_calculation_strategy_for_checkout(checkout_info, lines)+ if tax_strategy == TaxCalculationStrategy.FLAT_RATES:+ return previous_value+data = generate_request_data_from_checkout(checkout_info,lines,@@ -347,6 +357,10 @@ class AvataxPlugin(BasePlugin):def order_confirmed(self, order: "Order", previous_value: Any) -> Any:if not self.active:return previous_value+ tax_strategy = get_tax_calculation_strategy_for_order(order)+ if tax_strategy == TaxCalculationStrategy.FLAT_RATES:+ return previous_value+request_data = get_order_request_data(order, self.config)if not request_data:return previous_valuediff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.pyindex f7c24c654b..a054dd7f27 100644--- a/saleor/plugins/avatax/tests/test_avatax.py+++ b/saleor/plugins/avatax/tests/test_avatax.py@@ -32,6 +32,7 @@ from ....product import ProductTypeKindfrom ....product.models import Product, ProductTypefrom ....product.utils.variant_prices import update_discounted_prices_for_promotionfrom ....shipping.utils import convert_to_shipping_method_data+from ....tax import TaxCalculationStrategyfrom ....tax.models import TaxClassfrom ...manager import get_plugins_managerfrom ...models import PluginConfiguration@@ -4157,6 +4158,160 @@ def test_order_confirmed()+@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])+def test_preprocess_order_creation_with_enabled_flat_rates(+ api_post_request_task_mock,+ checkout_with_item,+ address,+ ship_to_pl_address,+ site_settings,+ shipping_zone,+ plugin_configuration,+):+ # given+ channel = checkout_with_item.channel+ tax_configuration = channel.tax_configuration+ tax_configuration.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES+ tax_configuration.save()+ tax_configuration.country_exceptions.all().delete()++ plugin_configuration(+ from_street_address="Tęczowa 7",+ from_city="WROCŁAW",+ from_postal_code="53-603",+ from_country="PL",+ shipping_tax_code="FR00001",+ )++ manager = get_plugins_manager(allow_replica=True)+ site_settings.company_address = address+ site_settings.save()++ checkout_with_item.shipping_address = ship_to_pl_address+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()+ checkout_with_item.save()+ lines, _ = fetch_checkout_lines(checkout_with_item)+ checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)++ # when+ manager.preprocess_order_creation(checkout_info, lines)++ # then+ assert not api_post_request_task_mock.called+++@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])+def test_preprocess_order_creation_with_country_exception_for_flat_rates(+ api_post_request_task_mock,+ checkout_with_item,+ address,+ ship_to_pl_address,+ site_settings,+ shipping_zone,+ plugin_configuration,+):+ # given+ channel = checkout_with_item.channel+ tax_configuration = channel.tax_configuration+ tax_configuration.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES+ tax_configuration.save()+ tax_configuration.country_exceptions.all().delete()++ tax_configuration.country_exceptions.create(+ country="PL",+ tax_calculation_strategy=TaxCalculationStrategy.FLAT_RATES,+ )++ plugin_configuration(+ from_street_address="Tęczowa 7",+ from_city="WROCŁAW",+ from_postal_code="53-603",+ from_country="PL",+ shipping_tax_code="FR00001",+ )++ manager = get_plugins_manager(allow_replica=True)+ site_settings.company_address = address+ site_settings.save()++ checkout_with_item.shipping_address = ship_to_pl_address+ checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()+ checkout_with_item.save()+ lines, _ = fetch_checkout_lines(checkout_with_item)+ checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)++ # when+ manager.preprocess_order_creation(checkout_info, lines)++ # then+ assert not api_post_request_task_mock.called+++@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])+def test_order_confirmed_skip_when_flat_rate_usage(+ api_post_request_task_mock, order, plugin_configuration+):+ # given+ channel = order.channel+ tax_configuration = channel.tax_configuration+ tax_configuration.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES+ tax_configuration.save()+ tax_configuration.country_exceptions.all().delete()++ plugin_configuration(+ from_street_address="Tęczowa 7",+ from_city="WROCŁAW",+ from_postal_code="53-603",+ from_country="PL",+ shipping_tax_code="FR00001",+ )++ manager = get_plugins_manager(allow_replica=True)++ # when+ manager.order_confirmed(order)++ # then+ assert not api_post_request_task_mock.called+++@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])+def test_order_confirmed_skip_with_country_exception_for_flat_rate(+ api_post_request_task_mock, order, order_line, plugin_configuration+):+ # given+ channel = order.channel+ tax_configuration = channel.tax_configuration+ tax_configuration.tax_calculation_strategy = TaxCalculationStrategy.TAX_APP+ tax_configuration.save()+ tax_configuration.country_exceptions.all().delete()++ tax_configuration.country_exceptions.create(+ country="PL",+ tax_calculation_strategy=TaxCalculationStrategy.FLAT_RATES,+ )++ plugin_configuration(+ from_street_address="Tęczowa 7",+ from_city="WROCŁAW",+ from_postal_code="53-603",+ from_country="PL",+ shipping_tax_code="FR00001",+ )++ manager = get_plugins_manager(allow_replica=True)++ # when+ manager.order_confirmed(order)++ # then+ assert not api_post_request_task_mock.called++@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])def test_order_created_no_lines(