Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix base order total calculation with voucher (#10604)
Filip Owczarek1748c77271a4
diff --git a/saleor/graphql/order/tests/test_pagination.py b/saleor/graphql/order/tests/test_pagination.pyindex 5f4a4181ec..69a232878d 100644--- a/saleor/graphql/order/tests/test_pagination.py+++ b/saleor/graphql/order/tests/test_pagination.py@@ -509,16 +509,19 @@ def test_draft_orders_query_pagination_with_filter_search(user_email="[email protected]",status=OrderStatus.DRAFT,channel=channel_USD,+ should_refresh_prices=False,),Order(user_email="[email protected]",status=OrderStatus.DRAFT,channel=channel_USD,+ should_refresh_prices=False,),Order(user_email="[email protected]",status=OrderStatus.DRAFT,channel=channel_USD,+ should_refresh_prices=False,),])diff --git a/saleor/order/base_calculations.py b/saleor/order/base_calculations.pyindex 4e985b68e4..3f3d85ed3d 100644--- a/saleor/order/base_calculations.py+++ b/saleor/order/base_calculations.py@@ -3,7 +3,8 @@ from typing import TYPE_CHECKING, Iterablefrom prices import Money, TaxedMoneyfrom ..core.taxes import zero_money-from ..discount import OrderDiscountType+from ..discount import DiscountValueType, OrderDiscountType+from ..discount.models import OrderDiscountfrom ..discount.utils import apply_discount_to_valuefrom .interface import OrderTaxedPricesData@@ -22,31 +23,81 @@ def base_order_shipping(order: "Order") -> Money:return channel_listing.price-def base_order_total(order: "Order", lines: Iterable["OrderLine"]) -> Money:- currency = order.currency- total = base_order_total_without_order_discount(order, lines)- order_discount = order.discounts.filter(type=OrderDiscountType.MANUAL).first()- if order_discount:- total = apply_discount_to_value(- value=order_discount.value,- value_type=order_discount.value_type,- currency=currency,- price_to_discount=total,- )- return max(total, zero_money(currency))---def base_order_total_without_order_discount(- 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:quantity = line.quantityprice_with_discounts = line.base_unit_price * quantitysubtotal += price_with_discounts- base_shipping_price = base_order_shipping(order)- return subtotal + base_shipping_price+ return subtotal+++def base_order_total(order: "Order", lines: Iterable["OrderLine"]) -> Money:+ """Return order total, recalculate, and update order discounts.++ This function returns the order total. All discounts are included in this price.+ Shipping vouchers are included in the shipping price.+ Specific product vouchers are included in line base prices.+ Entire order vouchers are recalculated and updated in this function+ (OrderDiscounts with type `order_discount.type == OrderDiscountType.VOUCHER`).+ Staff order discounts are recalculated and updated in this function+ (OrderDiscounts with type `order_discount.type == OrderDiscountType.MANUAL`).+ """+ currency = order.currency+ subtotal = _base_order_subtotal(order, lines)+ shipping_price = base_order_shipping(order)+ order_discounts = order.discounts.all()+ order_discounts_to_update = []+ for order_discount in order_discounts:+ subtotal_before_discount = subtotal+ shipping_price_before_discount = shipping_price+ if order_discount.type == OrderDiscountType.VOUCHER:+ subtotal = apply_discount_to_value(+ value=order_discount.value,+ value_type=order_discount.value_type,+ currency=currency,+ price_to_discount=subtotal,+ )+ elif order_discount.value_type == DiscountValueType.PERCENTAGE:+ subtotal = apply_discount_to_value(+ value=order_discount.value,+ value_type=order_discount.value_type,+ currency=currency,+ price_to_discount=subtotal,+ )+ shipping_price = apply_discount_to_value(+ value=order_discount.value,+ value_type=order_discount.value_type,+ currency=currency,+ price_to_discount=shipping_price,+ )+ else:+ temporary_undiscounted_total = subtotal + shipping_price+ if temporary_undiscounted_total.amount > 0:+ temporary_total = apply_discount_to_value(+ value=order_discount.value,+ value_type=order_discount.value_type,+ currency=currency,+ price_to_discount=temporary_undiscounted_total,+ )+ total_discount = temporary_undiscounted_total - temporary_total+ subtotal_discount = (+ subtotal / temporary_undiscounted_total+ ) * total_discount+ shipping_discount = total_discount - subtotal_discount++ subtotal -= subtotal_discount+ shipping_price -= shipping_discount+ shipping_discount_amount = shipping_price_before_discount - shipping_price+ subtotal_discount_amount = subtotal_before_discount - subtotal+ total_discount_amount = shipping_discount_amount + subtotal_discount_amount+ if order_discount.amount != total_discount_amount:+ order_discount.amount = total_discount_amount+ order_discounts_to_update.append(order_discount)+ if order_discounts_to_update:+ OrderDiscount.objects.bulk_update(order_discounts_to_update, ["amount_value"])+ return max(subtotal + shipping_price, zero_money(currency))def base_order_line_total(order_line: "OrderLine") -> OrderTaxedPricesData:diff --git a/saleor/order/calculations.py b/saleor/order/calculations.pyindex 1c43f09a7d..5d99756504 100644--- a/saleor/order/calculations.py+++ b/saleor/order/calculations.py@@ -7,9 +7,8 @@ from prices import Money, TaxedMoneyfrom ..core.prices import quantize_pricefrom ..core.taxes import TaxData, TaxError, zero_taxed_money-from ..discount import OrderDiscountTypefrom ..plugins.manager import PluginsManager-from . import ORDER_EDITABLE_STATUS, utils+from . import ORDER_EDITABLE_STATUSfrom .interface import OrderTaxedPricesDatafrom .models import Order, OrderLine@@ -118,14 +117,6 @@ def fetch_order_prices_if_expired(order.should_refresh_prices = False- order_discount = order.discounts.filter(type=OrderDiscountType.MANUAL).first()- if order_discount:- utils.update_order_discount_for_order(- order,- lines,- order_discount,- )-_recalculate_order_prices(manager, order, lines)tax_data = manager.get_taxes_for_order(order)diff --git a/saleor/order/tests/test_base_calculations/__init__.py b/saleor/order/tests/test_base_calculations/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/order/tests/test_base_calculations/test_base_order_line_total.py b/saleor/order/tests/test_base_calculations/test_base_order_line_total.pynew file mode 100644index 0000000000..0b5dd2588a--- /dev/null+++ b/saleor/order/tests/test_base_calculations/test_base_order_line_total.py@@ -0,0 +1,28 @@+from prices import TaxedMoney++from ... import base_calculations+from ...interface import OrderTaxedPricesData+++def test_base_order_line_total(order_with_lines):+ # given+ line = order_with_lines.lines.all().first()++ # when+ order_total = base_calculations.base_order_line_total(line)++ # then+ base_line_unit_price = line.base_unit_price+ quantity = line.quantity+ expected_price_with_discount = (+ TaxedMoney(base_line_unit_price, base_line_unit_price) * quantity+ )+ base_line_undiscounted_unit_price = line.undiscounted_base_unit_price+ expected_undiscounted_price = (+ TaxedMoney(base_line_undiscounted_unit_price, base_line_undiscounted_unit_price)+ * quantity+ )+ assert order_total == OrderTaxedPricesData(+ price_with_discounts=expected_price_with_discount,+ undiscounted_price=expected_undiscounted_price,+ )diff --git a/saleor/order/tests/test_base_calculations/test_base_order_shipping.py b/saleor/order/tests/test_base_calculations/test_base_order_shipping.pynew file mode 100644index 0000000000..f6f3326f96--- /dev/null+++ b/saleor/order/tests/test_base_calculations/test_base_order_shipping.py@@ -0,0 +1,38 @@+from ....core.taxes import zero_money+from ... import base_calculations+++def test_base_order_shipping(order_with_lines):+ # given+ order = order_with_lines+ channel_listing = order.shipping_method.channel_listings.filter(+ channel_id=order.channel_id+ ).first()++ # when+ order_total = base_calculations.base_order_shipping(order)++ # then+ assert order_total == channel_listing.price+++def test_base_order_shipping_without_shipping(order):+ # given++ # when+ order_total = base_calculations.base_order_shipping(order)++ # then+ assert order_total == zero_money(order.currency)+++def test_base_order_shipping_shipping_without_channel_listing(order_with_lines):+ # given+ order = order_with_lines+ order.shipping_method.channel_listings.filter(channel_id=order.channel_id).delete()++ # when+ order_total = base_calculations.base_order_shipping(order)++ # then+ assert order_total == zero_money(order.currency)diff --git a/saleor/order/tests/test_base_calculations/test_base_order_total.py b/saleor/order/tests/test_base_calculations/test_base_order_total.pynew file mode 100644index 0000000000..2d8326014e--- /dev/null+++ b/saleor/order/tests/test_base_calculations/test_base_order_total.py@@ -0,0 +1,611 @@+from decimal import Decimal++from prices import Money++from ....core.taxes import zero_money+from ....discount import DiscountValueType, OrderDiscountType+from ... import base_calculations+++def test_base_order_total(order_with_lines):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total+++def test_base_order_total_with_fixed_voucher(order_with_lines):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ discount_amount = 10+ order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=discount_amount,+ name="Voucher",+ translated_name="VoucherPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(discount_amount, order.currency)+ order_discount.refresh_from_db()+ assert order_discount.amount_value == discount_amount+++def test_base_order_total_with_fixed_voucher_more_then_total(order_with_lines):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity++ order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=100,+ name="Voucher",+ translated_name="VoucherPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ # Voucher isn't applied on shipping price+ assert order_total == shipping_price+ order_discount.refresh_from_db()+ assert order_discount.amount == subtotal+++def test_base_order_total_with_percentage_voucher(order_with_lines):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ discount_amount = subtotal.amount * Decimal(0.5)+ order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="Voucher",+ translated_name="VoucherPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(discount_amount, order.currency)+ order_discount.refresh_from_db()+ assert order_discount.amount_value == discount_amount+++def test_base_order_total_with_fixed_manual_discount(order_with_lines):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ discount_amount = 10+ order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(discount_amount, order.currency)+ order_discount.refresh_from_db()+ assert order_discount.amount_value == discount_amount+++def test_base_order_total_with_fixed_manual_discount_and_zero_order_total(order):+ # given+ lines = order.lines.all()++ order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=0,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == zero_money(order.currency)+++def test_base_order_total_with_fixed_manual_discount_more_then_total(order_with_lines):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=100,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == Money(Decimal("0"), order.currency)+ order_discount.refresh_from_db()+ assert order_discount.amount == undiscounted_total+++def test_base_order_total_with_percentage_manual_discount(order_with_lines):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ discount_amount = undiscounted_total.amount * Decimal(0.5)+ order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(discount_amount, order.currency)+ order_discount.refresh_from_db()+ assert order_discount.amount_value == discount_amount+++def test_base_order_total_with_fixed_voucher_and_fixed_manual_discount(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ voucher_discount_amount = 10+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=voucher_discount_amount,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )+ manual_discount_amount = 10+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=manual_discount_amount,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(+ voucher_discount_amount, order.currency+ ) - Money(manual_discount_amount, order.currency)+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amount+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+++def test_base_order_total_with_percentage_voucher_and_fixed_manual_discount(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ voucher_discount_amount = subtotal.amount * Decimal(0.5)+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )+ manual_discount_amount = 10+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(+ voucher_discount_amount, order.currency+ ) - Money(manual_discount_amount, order.currency)+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amount+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+++def test_base_order_total_with_fixed_voucher_and_percentage_manual_discount(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ voucher_discount_amount = 10+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )+ temporary_total = undiscounted_total - Money(+ voucher_discount_amount, order.currency+ )+ manual_discount_amount = temporary_total.amount * Decimal(0.5)+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(+ voucher_discount_amount, order.currency+ ) - Money(manual_discount_amount, order.currency)+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amount+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+++def test_base_order_total_with_percentage_voucher_and_percentage_manual_discount(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ voucher_discount_amount = subtotal.amount * Decimal(0.5)+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ temporary_total = undiscounted_total - Money(+ voucher_discount_amount, order.currency+ )+ manual_discount_amount = temporary_total.amount * Decimal(0.5)+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(+ voucher_discount_amount, order.currency+ ) - Money(manual_discount_amount, order.currency)+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amount+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+++def test_base_order_total_with_fixed_manual_discount_and_fixed_voucher(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ manual_discount_amount = 10+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=manual_discount_amount,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ voucher_discount_amount = 10+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=voucher_discount_amount,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(+ voucher_discount_amount, order.currency+ ) - Money(manual_discount_amount, order.currency)+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amount+++def test_base_order_total_with_fixed_manual_discount_and_percentage_voucher(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ manual_discount_amount = 10+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=manual_discount_amount,+ )++ subtotal_discount_from_order_discount = (+ subtotal / undiscounted_total * manual_discount_amount+ )+ temporary_subtotal_amount = subtotal.amount - subtotal_discount_from_order_discount+ voucher_discount_amount = round(temporary_subtotal_amount * Decimal(0.5), 2)+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=voucher_discount_amount,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert voucher_discount_amount == Decimal("30.62")+ expected_total = (+ undiscounted_total+ - Money(voucher_discount_amount, order.currency)+ - Money(manual_discount_amount, order.currency)+ )+ assert order_total == expected_total+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amount+++def test_base_order_total_with_percentage_manual_discount_and_fixed_voucher(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ manual_discount_amount = undiscounted_total.amount * Decimal(0.5)+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ voucher_discount_amount = 10+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.FIXED,+ value=10,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(+ voucher_discount_amount, order.currency+ ) - Money(manual_discount_amount, order.currency)+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amount+++def test_base_order_total_with_percentage_manual_discount_and_percentage_voucher(+ order_with_lines,+):+ # given+ order = order_with_lines+ lines = order.lines.all()+ shipping_price = order.shipping_price.net+ subtotal = zero_money(order.currency)+ for line in lines:+ subtotal += line.base_unit_price * line.quantity+ undiscounted_total = subtotal + shipping_price++ manual_discount_amount = undiscounted_total.amount * Decimal(0.5)+ manual_order_discount = order.discounts.create(+ type=OrderDiscountType.MANUAL,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ temporary_subtotal_amount = subtotal.amount * Decimal(0.5)+ voucher_discount_amount = temporary_subtotal_amount * Decimal(0.5)+ voucher_order_discount = order.discounts.create(+ type=OrderDiscountType.VOUCHER,+ value_type=DiscountValueType.PERCENTAGE,+ value=50,+ name="StaffDiscount",+ translated_name="StaffDiscountPL",+ currency=order.currency,+ amount_value=0,+ )++ # when+ order_total = base_calculations.base_order_total(order, lines)++ # then+ assert order_total == undiscounted_total - Money(+ voucher_discount_amount, order.currency+ ) - Money(manual_discount_amount, order.currency)+ manual_order_discount.refresh_from_db()+ assert manual_order_discount.amount_value == manual_discount_amount+ voucher_order_discount.refresh_from_db()+ assert voucher_order_discount.amount_value == voucher_discount_amountdiff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 9b279a4cbc..a56fa7de92 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -43,7 +43,6 @@ from . import (OrderAuthorizeStatus,OrderChargeStatus,OrderStatus,- base_calculations,events,)from .fetch import OrderLineInfo@@ -753,40 +752,6 @@ def create_order_discount_for_order(return order_discount-def update_order_discount_for_order(- order: Order,- lines: Iterable[OrderLine],- order_discount_to_update: OrderDiscount,- reason: Optional[str] = None,- value_type: Optional[str] = None,- value: Optional[Decimal] = None,-):- """Update the order_discount for an order."""- current_value = order_discount_to_update.value- value = value if value is not None else current_value- value_type = value_type or order_discount_to_update.value_type- fields_to_update = []- if reason is not None:- order_discount_to_update.reason = reason- fields_to_update.append("reason")-- current_total = base_calculations.base_order_total_without_order_discount(- order, lines- )-- discounted_total = apply_discount_to_value(- value, value_type, order.currency, current_total- )- new_amount = quantize_price(current_total - discounted_total, order.currency)-- order_discount_to_update.amount = new_amount- order_discount_to_update.value = value- order_discount_to_update.value_type = value_type- fields_to_update.extend(["value_type", "value", "amount_value"])-- order_discount_to_update.save(update_fields=fields_to_update)--def remove_order_discount_from_order(order: Order, order_discount: OrderDiscount):"""Remove the order discount from order and update the prices."""