Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Return proper order level discounts when generating static order tax payload (#16572)

Piotr Zabieglik939093a9c65c
diff --git a/saleor/discount/utils/shared.py b/saleor/discount/utils/shared.py
index f2741148e4..bbd6504a62 100644
--- a/saleor/discount/utils/shared.py
+++ b/saleor/discount/utils/shared.py
@@ -2,6 +2,7 @@ from collections import defaultdict
from decimal import Decimal
from typing import TYPE_CHECKING, Optional, Union
+from .. import DiscountType
from ..models import (
CheckoutDiscount,
CheckoutLineDiscount,
@@ -82,3 +83,12 @@ def update_line_info_cached_discounts(
]
if discount := line_id_line_discounts_map.get(line_info.line.id):
line_info.discounts.extend(discount)
+
+
+def is_order_level_discount(discount: OrderDiscount) -> bool:
+ from .voucher import is_order_level_voucher
+
+ return discount.type in [
+ DiscountType.MANUAL,
+ DiscountType.ORDER_PROMOTION,
+ ] or is_order_level_voucher(discount.voucher)
diff --git a/saleor/graphql/core/types/taxes.py b/saleor/graphql/core/types/taxes.py
index 84f7dca203..6365f85f12 100644
--- a/saleor/graphql/core/types/taxes.py
+++ b/saleor/graphql/core/types/taxes.py
@@ -7,8 +7,8 @@ from promise import Promise
from ....checkout import base_calculations
from ....checkout.models import Checkout, CheckoutLine
from ....core.prices import quantize_price
-from ....discount import DiscountType
from ....discount.utils.checkout import has_checkout_order_promotion
+from ....discount.utils.shared import is_order_level_discount
from ....discount.utils.voucher import is_order_level_voucher
from ....order.models import Order, OrderLine
from ....order.utils import get_order_country
@@ -392,10 +392,9 @@ class TaxableObject(BaseObjectType):
return [
{"name": discount.name, "amount": discount.amount}
for discount in discounts
- if (
- discount.type in [DiscountType.MANUAL, DiscountType.ORDER_PROMOTION]
- or is_order_level_voucher(discount.voucher)
- )
+ # Only order level discounts, like entire order vouchers,
+ # order promotions and manual discounts should be taken into account
+ if is_order_level_discount(discount)
]
return (
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index 4818ea981c..3d15cda9d4 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -33,6 +33,7 @@ from ..core.utils.anonymization import (
)
from ..core.utils.json_serializer import CustomJsonEncoder
from ..discount import VoucherType
+from ..discount.utils.shared import is_order_level_discount
from ..discount.utils.voucher import is_order_level_voucher
from ..order import FulfillmentStatus, OrderStatus
from ..order.models import Fulfillment, FulfillmentLine, Order, OrderLine
@@ -1434,7 +1435,9 @@ def generate_order_payload_for_tax_calculation(order: "Order"):
discounts = order.discounts.all()
discounts_dict = []
for discount in discounts:
- if is_order_level_voucher(discount.voucher):
+ # Only order level discounts, like entire order vouchers,
+ # order promotions and manual discounts should be taken into account
+ if not is_order_level_discount(discount):
continue
quantize_price_fields(discount, ("amount_value",), order.currency)
discount_amount = quantize_price(discount.amount_value, order.currency)
diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.py
index 1b9664f7b9..bd9eef81c1 100644
--- a/saleor/webhook/tests/test_webhook_payloads.py
+++ b/saleor/webhook/tests/test_webhook_payloads.py
@@ -73,7 +73,7 @@ def parse_django_datetime(date):
@pytest.fixture
-def order_for_payload(fulfilled_order):
+def order_for_payload(fulfilled_order, voucher_percentage):
order = fulfilled_order
new_order = Order.objects.create(
@@ -97,6 +97,7 @@ def order_for_payload(fulfilled_order):
value=Decimal("10"),
amount_value=Decimal("16.5"),
name="Voucher",
+ voucher=voucher_percentage,
)
discount.created_at = datetime.now(pytz.utc) + timedelta(days=1)
@@ -380,7 +381,7 @@ def test_generate_order_payload_for_tax_calculation(
@freeze_time()
@pytest.mark.parametrize("prices_entered_with_tax", [True, False])
@mock.patch("saleor.webhook.payloads._generate_order_lines_payload_for_tax_calculation")
-def test_generate_order_payload_for_tax_calculation_voucher_discounts(
+def test_generate_order_payload_for_tax_calculation_entire_order_voucher(
mocked_order_lines, order_for_payload, prices_entered_with_tax, voucher
):
# given
@@ -397,10 +398,10 @@ def test_generate_order_payload_for_tax_calculation_voucher_discounts(
order = order_for_payload
discount_1, discount_2 = list(order.discounts.all())
voucher.type = VoucherType.ENTIRE_ORDER
- voucher.apply_once_per_order = True
+ voucher.apply_once_per_order = False
voucher.save()
- discount_1.voucher = voucher
- discount_1.save()
+ discount_2.voucher = voucher
+ discount_2.save()
user = order.user
# when
@@ -457,6 +458,44 @@ def test_generate_order_payload_for_tax_calculation_voucher_discounts(
mocked_order_lines.assert_called_once()
+@freeze_time()
[email protected]("prices_entered_with_tax", [True, False])
[email protected]("saleor.webhook.payloads._generate_order_lines_payload_for_tax_calculation")
+def test_generate_order_payload_for_tax_calculation_line_level_voucher_excluded(
+ mocked_order_lines, order_for_payload, prices_entered_with_tax, voucher
+):
+ # given
+ order = order_for_payload
+
+ tax_configuration = order.channel.tax_configuration
+ tax_configuration.prices_entered_with_tax = prices_entered_with_tax
+ tax_configuration.save(update_fields=["prices_entered_with_tax"])
+
+ order_lines = '"order_lines"'
+ mocked_order_lines.return_value = order_lines
+
+ order = order_for_payload
+ discount_1, discount_2 = list(order.discounts.all())
+ voucher.type = VoucherType.ENTIRE_ORDER
+ # line level vouchers should be excluded from discounts
+ voucher.apply_once_per_order = True
+ voucher.save()
+ discount_2.voucher = voucher
+ discount_2.save()
+
+ # when
+ payload = json.loads(generate_order_payload_for_tax_calculation(order))[0]
+
+ # then
+ assert payload["discounts"] == [
+ {
+ "name": discount_1.name,
+ "amount": str(quantize_price(discount_1.amount_value, order.currency)),
+ },
+ ]
+ mocked_order_lines.assert_called_once()
+
+
@freeze_time()
@mock.patch("saleor.webhook.payloads.generate_order_lines_payload")
@mock.patch("saleor.webhook.payloads.generate_fulfillment_lines_payload")