Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add missing vatlayaer recalculation in order price recalculation (#10658)
Filip Owczarek9d866b74c351
diff --git a/saleor/order/calculations.py b/saleor/order/calculations.pyindex 5d99756504..8fa309a568 100644--- a/saleor/order/calculations.py+++ b/saleor/order/calculations.py@@ -46,6 +46,10 @@ def _recalculate_order_prices(except TaxError:pass+ # Plugins like Vatlayer don't implement Order line calculation instead of implement+ # `update_taxes_for_order_lines`+ manager.update_taxes_for_order_lines(order, list(lines))+try:order.shipping_price = manager.calculate_order_shipping(order)order.shipping_tax_rate = manager.get_order_shipping_tax_rate(diff --git a/saleor/order/tests/test_calculations.py b/saleor/order/tests/test_calculations.pyindex 52c9e70b42..03e4c28ef2 100644--- a/saleor/order/tests/test_calculations.py+++ b/saleor/order/tests/test_calculations.py@@ -7,6 +7,7 @@ from prices import Money, TaxedMoneyfrom ...core.prices import quantize_pricefrom ...core.taxes import TaxData, TaxError, TaxLineData, zero_taxed_money+from ...plugins.manager import get_plugins_managerfrom .. import OrderStatus, calculationsfrom ..interface import OrderTaxedPricesData@@ -124,6 +125,22 @@ def test_recalculate_order_prices(order_with_lines, order_lines, tax_data):assert tax_rate == line.tax_rate+@patch("saleor.plugins.manager.PluginsManager.update_taxes_for_order_lines")+def test_recalculate_order_prices_calls_update_taxes_for_order_lines(+ mocked_update_taxes_for_order_lines, order_with_lines, order_lines+):+ # given+ order = order_with_lines+ manager = get_plugins_manager()+ lines = list(order_lines)++ # when+ calculations._recalculate_order_prices(manager, order, lines)++ # then+ mocked_update_taxes_for_order_lines.assert_called_once_with(order, lines)++@pytest.mark.parametrize("mocked_method_name",[diff --git a/saleor/plugins/vatlayer/plugin.py b/saleor/plugins/vatlayer/plugin.pyindex ddc759f0e0..f5b8c81631 100644--- a/saleor/plugins/vatlayer/plugin.py+++ b/saleor/plugins/vatlayer/plugin.py@@ -15,10 +15,10 @@ from django_prices_vatlayer.utils import ()from prices import Money, TaxedMoney, TaxedMoneyRange-from ...checkout import base_calculations, calculations+from ...checkout import base_calculationsfrom ...core.prices import quantize_price-from ...core.taxes import TaxType, zero_money-from ...discount import VoucherType+from ...core.taxes import TaxType, zero_money, zero_taxed_money+from ...discount import OrderDiscountType, VoucherTypefrom ...order.interface import OrderTaxedPricesDatafrom ...order.utils import (get_total_order_discount_excluding_shipping,@@ -42,15 +42,9 @@ if TYPE_CHECKING:from ...account.models import Addressfrom ...channel.models import Channelfrom ...checkout.fetch import CheckoutInfo, CheckoutLineInfo- from ...checkout.models import Checkoutfrom ...discount import DiscountInfofrom ...order.models import Order, OrderLine- from ...product.models import (- Collection,- Product,- ProductVariant,- ProductVariantChannelListing,- )+ from ...product.models import Product, ProductVariantfrom ..models import PluginConfiguration@@ -179,6 +173,32 @@ class VatlayerPlugin(BasePlugin):checkout_info, lines, address, discounts)+ def calculate_order_total(+ self,+ order: "Order",+ lines: Iterable["OrderLine"],+ previous_value: TaxedMoney,+ ) -> TaxedMoney:+ if self._skip_plugin(previous_value):+ return previous_value++ currency = order.currency++ total = zero_taxed_money(currency)+ undiscounted_subtotal = zero_taxed_money(currency)+ for line in lines:+ total += line.total_price+ undiscounted_subtotal += line.undiscounted_total_price+ total += order.shipping_price++ # Vatlayer doesn't propagate order discount to shipping we should include+ # remaining amount in total calculation.+ order_discount = order.discounts.filter(type=OrderDiscountType.MANUAL).first()+ if order_discount and order_discount.amount > undiscounted_subtotal.gross:+ remaining_amount = order_discount.amount - undiscounted_subtotal.gross+ total -= remaining_amount+ return max(total, zero_taxed_money(currency))+def _get_taxes_for_country(self, country: Country):"""Try to fetch cached taxes on the plugin level.@@ -347,9 +367,12 @@ class VatlayerPlugin(BasePlugin):)line.total_price = line.unit_price * line.quantityline.undiscounted_total_price = line.undiscounted_unit_price * line.quantity- line.tax_rate = (line.unit_price.tax / line.unit_price.net).quantize(- Decimal(".0001")- )+ if line.unit_price.net == zero_money(line.currency):+ line.tax_rate = Decimal("0")+ else:+ line.tax_rate = (line.unit_price.tax / line.unit_price.net).quantize(+ Decimal(".0001")+ )def calculate_checkout_line_total(self,diff --git a/saleor/plugins/vatlayer/tests/test_vatlayer.py b/saleor/plugins/vatlayer/tests/test_vatlayer.pyindex 6389642d57..a3450ca638 100644--- a/saleor/plugins/vatlayer/tests/test_vatlayer.py+++ b/saleor/plugins/vatlayer/tests/test_vatlayer.py@@ -549,6 +549,198 @@ def test_calculate_checkout_subtotal_with_excluded_country(@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])+def test_calculations_calculate_order_total(vatlayer, order_with_lines):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ lines = order.lines.all()+ order.shipping_price = manager.calculate_order_shipping(order)+ lines = manager.update_taxes_for_order_lines(order, lines)++ # when+ total = manager.calculate_order_total(order, lines)+ total = quantize_price(total, total.currency)++ # then+ assert total == TaxedMoney(net=Money("65.04", "USD"), gross=Money("80.00", "USD"))+++@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])+def test_calculations_calculate_order_total_voucher(vatlayer, order_with_lines):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ lines = list(order.lines.all())+ discount_amount = 10+ order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=discount_amount,+ name="Voucher",+ translated_name="VoucherPL",+ currency=order.currency,+ amount_value=10,+ )+ order.shipping_price = manager.calculate_order_shipping(order)+ manager.update_taxes_for_order_lines(order, lines)++ # when+ total = manager.calculate_order_total(order, lines)+ total = quantize_price(total, total.currency)++ # then+ assert total == TaxedMoney(net=Money("56.92", "USD"), gross=Money("70.01", "USD"))+++@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])+def test_calculations_calculate_order_total_with_manual_discount(+ vatlayer, order_with_lines+):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ lines = list(order.lines.all())+ order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=10,+ )+ order.shipping_price = manager.calculate_order_shipping(order)+ manager.update_taxes_for_order_lines(order, lines)++ # when+ total = manager.calculate_order_total(order, lines)+ total = quantize_price(total, total.currency)++ # then+ assert total == TaxedMoney(net=Money("56.92", "USD"), gross=Money("70.01", "USD"))+++@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])+def test_calculations_calculate_order_total_with_discount_for_order_total(+ vatlayer, order_with_lines+):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ lines = list(order.lines.all())+ order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=80,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=80,+ )+ order.shipping_price = manager.calculate_order_shipping(order)+ manager.update_taxes_for_order_lines(order, lines)++ # when+ total = manager.calculate_order_total(order, lines)+ total = quantize_price(total, total.currency)++ # then+ assert total == TaxedMoney(net=Money("0", "USD"), gross=Money("0", "USD"))+++@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])+def test_calculations_calculate_order_total_with_discount_for_subtotal_and_shipping(+ vatlayer, order_with_lines+):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ lines = list(order.lines.all())+ order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=75,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=75,+ )+ order.shipping_price = manager.calculate_order_shipping(order)+ manager.update_taxes_for_order_lines(order, lines)++ # when+ total = manager.calculate_order_total(order, lines)+ total = quantize_price(total, total.currency)++ # then+ assert total == TaxedMoney(net=Money("3.13", "USD"), gross=Money("5.00", "USD"))+++@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])+def test_calculations_calculate_order_total_with_discount_for_more_than_order_total(+ vatlayer, order_with_lines+):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ lines = list(order.lines.all())+ order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=100,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=100,+ )+ order.shipping_price = manager.calculate_order_shipping(order)+ manager.update_taxes_for_order_lines(order, lines)++ # when+ total = manager.calculate_order_total(order, lines)+ total = quantize_price(total, total.currency)++ # then+ assert total == TaxedMoney(net=Money("0", "USD"), gross=Money("0", "USD"))+++@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])+def test_calculations_calculate_order_total_with_manual_discount_and_voucher(+ vatlayer, order_with_lines+):+ # given+ manager = get_plugins_manager()+ order = order_with_lines+ lines = list(order.lines.all())+ order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=10,+ )+ order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="Voucher",+ translated_name="VoucherPL",+ currency=order.currency,+ amount_value=10,+ )+ order.shipping_price = manager.calculate_order_shipping(order)+ manager.update_taxes_for_order_lines(order, lines)++ # when+ total = manager.calculate_order_total(order, lines)+ total = quantize_price(total, total.currency)++ # then+ assert total == TaxedMoney(net=Money("48.77", "USD"), gross=Money("60.00", "USD"))++def test_calculate_order_shipping(vatlayer, order_line, shipping_zone, site_settings):manager = get_plugins_manager()order = order_line.order@@ -1523,7 +1715,7 @@ def test_calculations_checkout_shipping_price_with_vatlayer()-def test_skip_diabled_plugin(settings, channel_USD):+def test_skip_disabled_plugin(settings, channel_USD):settings.PLUGINS = ["saleor.plugins.vatlayer.plugin.VatlayerPlugin"]manager = get_plugins_manager()plugin: VatlayerPlugin = manager.get_plugin(