Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix recalculating tax on country change in draft order (#12887)
Marcin Gębala67f8cb40f298
diff --git a/saleor/tax/calculations/order.py b/saleor/tax/calculations/order.pyindex 000f4343d4..83594ab16f 100644--- a/saleor/tax/calculations/order.py+++ b/saleor/tax/calculations/order.py@@ -51,10 +51,13 @@ def update_order_prices_with_flat_rates(default_tax_rate,country_code,)- elif order.shipping_tax_rate is not None:- # Use order.shipping_tax_rate if it was ever set before (it's non-null now).- # This is a valid case when recalculating shipping price and the tax class is- # null, because it was removed from the system.+ elif (+ order.shipping_tax_class_name is not None+ and order.shipping_tax_rate is not None+ ):+ # Use order.shipping_tax_rate if it was ever set before (it's non-null now and+ # the name is non-null). This is a valid case when recalculating shipping price+ # and the tax class is null, because it was removed from the system.shipping_tax_rate = denormalize_tax_rate_from_db(order.shipping_tax_rate)else:shipping_tax_rate = default_tax_rate@@ -135,10 +138,11 @@ def update_taxes_for_order_lines(default_tax_rate,country_code,)- elif line.tax_rate is not None:- # line.tax_class can be None when the tax class was removed from DB. In- # this case try to use line.tax_rate which stores the denormalized tax rate- # value that was originally used.+ elif line.tax_class_name is not None and line.tax_rate is not None:+ # If tax_class is None but tax_class_name is set, the tax class was set+ # for this line before, but is now removed from the system. In this case+ # try to use line.tax_rate which stores the denormalized tax rate value+ # that was originally provided by the tax class.tax_rate = denormalize_tax_rate_from_db(line.tax_rate)else:tax_rate = default_tax_ratediff --git a/saleor/tax/tests/test_order_calculations.py b/saleor/tax/tests/test_order_calculations.pyindex aa0e0f53d1..fee76c22b5 100644--- a/saleor/tax/tests/test_order_calculations.py+++ b/saleor/tax/tests/test_order_calculations.py@@ -7,7 +7,10 @@ from saleor.tax.models import TaxClassCountryRatefrom ...core.prices import quantize_pricefrom ...core.taxes import zero_money, zero_taxed_moneyfrom ...discount import DiscountType, DiscountValueType+from ...order import OrderStatus+from ...order.calculations import fetch_order_prices_if_expiredfrom ...order.utils import get_order_country+from ...plugins.manager import get_plugins_managerfrom .. import TaxCalculationStrategyfrom ..calculations.order import (update_order_prices_with_flat_rates,@@ -627,3 +630,59 @@ def test_use_original_tax_rate_when_tax_class_is_removed_from_order_line(assert order.total == TaxedMoney(net=Money("65.04", "USD"), gross=Money("80.00", "USD"))+++def test_use_default_country_rate_when_no_tax_class_was_set_before(+ order_with_lines,+):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ country = get_order_country(order)+ TaxClassCountryRate.objects.create(country=country, rate=20)++ prices_entered_with_tax = True+ _enable_flat_rates(order, prices_entered_with_tax)+ lines = order.lines.all()++ # drop tax classes from lines and shipping, so that default country rate is used+ for line in lines:+ line.tax_class = None+ line.tax_rate = Decimal("0")+ line.tax_class_name = None+ line.save(+ update_fields=[+ "tax_rate",+ "tax_class",+ "tax_class_name",+ ]+ )++ order.shipping_method.tax_class.delete()+ order.shipping_tax_class = None+ order.shipping_tax_class_name = None+ order.shipping_tax_rate = Decimal("0")+ order.status = OrderStatus.DRAFT+ order.save(+ update_fields=[+ "status",+ "shipping_tax_class_name",+ "shipping_tax_class",+ "shipping_tax_rate",+ ]+ )+ order.refresh_from_db()++ # when+ fetch_order_prices_if_expired(order, manager, force_update=True)+ order.refresh_from_db()++ # then+ line = order.lines.first()+ assert line.tax_rate == Decimal("0.20")+ assert not line.tax_class+ assert not line.tax_class_name++ assert order.shipping_tax_rate == Decimal("0.20")+ assert not order.shipping_tax_class+ assert not order.shipping_tax_class_name