Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix invalid draft order prices when using staff discount (#12267)
Iga Karbowiakf02712698f59
diff --git a/saleor/order/base_calculations.py b/saleor/order/base_calculations.pyindex ec7f53ca69..8752e06472 100644--- a/saleor/order/base_calculations.py+++ b/saleor/order/base_calculations.py@@ -17,7 +17,7 @@ def base_order_shipping(order: "Order") -> Money:return order.base_shipping_price-def _base_order_subtotal(order: "Order", lines: Iterable["OrderLine"]) -> Money:+def base_order_subtotal(order: "Order", lines: Iterable["OrderLine"]) -> Money:currency = order.currencysubtotal = zero_money(currency)for line in lines:@@ -39,7 +39,7 @@ def base_order_total(order: "Order", lines: Iterable["OrderLine"]) -> Money:(OrderDiscounts with type `order_discount.type == OrderDiscountType.MANUAL`)."""currency = order.currency- subtotal = _base_order_subtotal(order, lines)+ subtotal = base_order_subtotal(order, lines)shipping_price = order.base_shipping_priceorder_discounts = order.discounts.all()order_discounts_to_update = []diff --git a/saleor/order/calculations.py b/saleor/order/calculations.pyindex 5f2729d6e5..36a9ba1c5c 100644--- a/saleor/order/calculations.py+++ b/saleor/order/calculations.py@@ -19,6 +19,7 @@ from ..tax.utils import (normalize_tax_rate_for_db,)from . import ORDER_EDITABLE_STATUS+from .base_calculations import base_order_subtotal, base_order_totalfrom .interface import OrderTaxedPricesDatafrom .models import Order, OrderLine@@ -31,6 +32,9 @@ def _recalculate_order_prices(Does not throw TaxError."""undiscounted_subtotal = zero_taxed_money(order.currency)++ _update_order_discounts_and_base_undiscounted_total(order, lines)+for line in lines:variant = line.variantif variant:@@ -63,10 +67,27 @@ def _recalculate_order_prices()except TaxError:pass- order.undiscounted_total = undiscounted_subtotal + order.shipping_price+ order.undiscounted_total = undiscounted_subtotal + TaxedMoney(+ net=order.base_shipping_price, gross=order.base_shipping_price+ )order.total = manager.calculate_order_total(order, lines)+def _update_order_discounts_and_base_undiscounted_total(+ order: Order, lines: Iterable[OrderLine]+):+ """Update order discounts and order undiscounted_total price.++ Entire order vouchers and staff order discounts are recalculated and updated.+ """+ base_order_total(order, lines)+ subtotal = base_order_subtotal(order, lines)+ undiscounted_total = subtotal + order.base_shipping_price+ order.undiscounted_total = TaxedMoney(+ net=undiscounted_total, gross=undiscounted_total+ )++def _get_order_base_prices(order, lines):currency = order.currencyundiscounted_subtotal = zero_taxed_money(currency)diff --git a/saleor/order/tests/test_calculations.py b/saleor/order/tests/test_calculations.pyindex a6ee0bc7b1..b5e6fee6ed 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 ...discount import DiscountValueTypefrom ...tax import TaxCalculationStrategyfrom ...tax.calculations.order import update_order_prices_with_flat_ratesfrom .. import OrderStatus, calculations@@ -68,16 +69,6 @@ def test_recalculate_order_prices(order_with_lines, order_lines, tax_data):order = order_with_linescurrency = order.currencylines = list(order_lines)- lines.append(- Mock(- variant=None,- total_price=create_taxed_money(- net=Decimal("33.33"),- gross=Decimal("44.44"),- currency=order.currency,- ),- )- )total_prices = [get_order_priced_taxes_data(line, "total", currency) for line in tax_data.lines@@ -96,7 +87,7 @@ def test_recalculate_order_prices(order_with_lines, order_lines, tax_data):subtotal = sum((get_taxed_money(line, "total", currency) for line in tax_data.lines),zero_taxed_money(order.currency),- ) + create_taxed_money(Decimal("33.33"), Decimal("44.44"), order.currency)+ )total = shipping + subtotalmanager = Mock(@@ -171,16 +162,6 @@ def test_recalculate_order_prices_tax_error_line_prices(order = order_with_linescurrency = order.currencylines = list(order_lines)- lines.append(- Mock(- variant=None,- total_price=create_taxed_money(- net=Decimal("33.33"),- gross=Decimal("44.44"),- currency=currency,- ),- )- )error_line = order_lines[0]old_line_unit_price = error_line.unit_priceold_line_undiscounted_unit_price = error_line.undiscounted_unit_price@@ -203,13 +184,9 @@ def test_recalculate_order_prices_tax_error_line_prices(shipping_tax_rate = tax_data.shipping_tax_rateshipping = get_taxed_money(tax_data, "shipping_price", currency)- subtotal = (- error_line.total_price- + sum(- [get_taxed_money(line, "total", currency) for line in tax_data.lines[1:]],- zero_taxed_money(currency),- )- + create_taxed_money(Decimal("33.33"), Decimal("44.44"), currency)+ subtotal = error_line.total_price + sum(+ [get_taxed_money(line, "total", currency) for line in tax_data.lines[1:]],+ zero_taxed_money(currency),)total = shipping + subtotal@@ -253,16 +230,7 @@ def test_recalculate_order_prices_tax_error_shipping_price(order = order_with_linescurrency = order.currencylines = list(order_lines)- lines.append(- Mock(- variant=None,- total_price=create_taxed_money(- net=Decimal("33.33"),- gross=Decimal("44.44"),- currency=currency,- ),- )- )+old_shipping_price = order.shipping_priceold_shipping_tax_rate = order.shipping_tax_rate@@ -283,7 +251,7 @@ def test_recalculate_order_prices_tax_error_shipping_price(subtotal = sum([get_taxed_money(line, "total", currency) for line in tax_data.lines],zero_taxed_money(currency),- ) + create_taxed_money(Decimal("33.33"), Decimal("44.44"), currency)+ )manager = Mock(calculate_order_line_unit=Mock(side_effect=unit_prices),@@ -312,6 +280,166 @@ def test_recalculate_order_prices_tax_error_shipping_price(assert tax_rate == line.tax_rate+def test_recalculate_order_prices_order_discounts_and_total_undiscounted_price_changed(+ draft_order, order_lines, shipping_method_weight_based, tax_data+):+ # given+ order = draft_order+ currency = order.currency+ lines = list(order_lines)++ old_shipping_price = order.shipping_price++ old_undiscounted_amount = draft_order.undiscounted_total+ discount_amount = old_undiscounted_amount.net+ order_discount = draft_order.discounts.create(+ value_type=DiscountValueType.PERCENTAGE,+ value=100,+ reason="Discount reason",+ amount=discount_amount,+ )++ new_shipping_price = Money(20, currency)+ shipping_listing = shipping_method_weight_based.channel_listings.get(+ channel=order.channel+ )+ shipping_listing.price = new_shipping_price+ shipping_listing.save(update_fields=["price_amount"])++ order.base_shipping_price = new_shipping_price+ order.shipping_method = shipping_method_weight_based+ order.shipping_method_name = shipping_method_weight_based.name+ order.save(+ update_fields=[+ "base_shipping_price_amount",+ "shipping_method",+ "shipping_method_name",+ ]+ )++ total_prices = [+ get_order_priced_taxes_data(line, "total", currency) for line in tax_data.lines+ ]+ unit_prices = []+ for line, total_price in zip(lines, total_prices):+ unit_prices.append(+ OrderTaxedPricesData(+ undiscounted_price=total_price.undiscounted_price / line.quantity,+ price_with_discounts=total_price.price_with_discounts / line.quantity,+ )+ )+ tax_rates = [line.tax_rate for line in tax_data.lines]+ shipping_tax_rate = 0++ manager = Mock(+ calculate_order_line_unit=Mock(side_effect=unit_prices),+ calculate_order_line_total=Mock(side_effect=total_prices),+ get_order_shipping_tax_rate=Mock(return_value=shipping_tax_rate),+ get_order_line_tax_rate=Mock(side_effect=tax_rates),+ calculate_order_shipping=Mock(return_value=zero_taxed_money(currency)),+ calculate_order_total=Mock(return_value=zero_taxed_money(currency)),+ )++ # when+ calculations._recalculate_order_prices(manager, order, lines)++ # then+ order_discount.refresh_from_db()+ assert order.undiscounted_total.net == old_undiscounted_amount.net + (+ new_shipping_price - old_shipping_price.net+ )+ assert order_discount.amount == order.undiscounted_total.net+ assert order.total == zero_taxed_money(currency)+ assert order.shipping_price == zero_taxed_money(currency)+ assert order.shipping_tax_rate == shipping_tax_rate+++def test_update_order_discounts_and_base_undiscounted_total_shipping_price_changed(+ draft_order, order_lines, shipping_method_weight_based+):+ """Ensure that the order discounts and order base undiscounted price is properly+ updated after changing the shipping price.+ """+ # given+ order = draft_order+ currency = order.currency+ old_shipping_price = order.shipping_price++ old_undiscounted_amount = draft_order.undiscounted_total+ discount_amount = old_undiscounted_amount.net+ # add order manual discount+ order_discount = draft_order.discounts.create(+ value_type=DiscountValueType.PERCENTAGE,+ value=100,+ reason="Discount reason",+ amount=discount_amount,+ )++ # change the shipping, increase the shipping price+ new_shipping_price = Money(20, currency)+ shipping_listing = shipping_method_weight_based.channel_listings.get(+ channel=order.channel+ )+ shipping_listing.price = new_shipping_price+ shipping_listing.save(update_fields=["price_amount"])++ order.base_shipping_price = new_shipping_price+ order.shipping_method = shipping_method_weight_based+ order.shipping_method_name = shipping_method_weight_based.name+ order.save(+ update_fields=[+ "base_shipping_price_amount",+ "shipping_method",+ "shipping_method_name",+ ]+ )++ # when+ calculations._update_order_discounts_and_base_undiscounted_total(order, order_lines)++ # then+ order_discount.refresh_from_db()+ assert order.undiscounted_total.net == old_undiscounted_amount.net + (+ new_shipping_price - old_shipping_price.net+ )+ assert order_discount.amount == order.undiscounted_total.net+++def test_update_order_discounts_and_base_undiscounted_total_line_quantity_changed(+ draft_order, order_lines, shipping_method_weight_based+):+ """Ensure that the order discounts and order base undiscounted price is properly+ updated after changing the line quantity+ """+ # given+ order = draft_order++ old_undiscounted_amount = draft_order.undiscounted_total+ discount_amount = old_undiscounted_amount.net+ # add order manual discount+ order_discount = draft_order.discounts.create(+ value_type=DiscountValueType.PERCENTAGE,+ value=100,+ reason="Discount reason",+ amount=discount_amount,+ )++ line = order_lines.first()+ line.quantity += 1+ line.save(update_fields=["quantity"])++ # when+ calculations._update_order_discounts_and_base_undiscounted_total(order, order_lines)++ # then+ order_discount.refresh_from_db()+ assert (+ order.undiscounted_total.net+ == old_undiscounted_amount.net + line.unit_price.net+ )+ assert order_discount.amount == order.undiscounted_total.net++def test_apply_tax_data(order_with_lines, order_lines, tax_data):# givenorder = order_with_linesdiff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 170f6f4546..b41e2c0079 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -4113,6 +4113,8 @@ def order_with_lines(order.shipping_price = TaxedMoney(net=net, gross=gross)order.base_shipping_price = netorder.shipping_tax_rate = calculate_tax_rate(order.shipping_price)+ order.total += order.shipping_price+ order.undiscounted_total += order.shipping_priceorder.save()recalculate_order(order)@@ -4625,11 +4627,10 @@ def fulfillment_awaiting_approval(fulfilled_order):@pytest.fixture-def draft_order(order_with_lines, shipping_method):+def draft_order(order_with_lines):Allocation.objects.filter(order_line__order=order_with_lines).delete()order_with_lines.status = OrderStatus.DRAFTorder_with_lines.origin = OrderOrigin.DRAFT- order_with_lines.shipping_method = shipping_methodorder_with_lines.save(update_fields=["status", "origin"])return order_with_lines