Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix voucher min spent (#7408)
Damian Wysocki09c5b89405cd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 71a9e22e94..2b4b53edf3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -102,6 +102,7 @@ All notable, unreleased changes to this project will be documented in this file.- Trigger `checkout_updated` hook for checkout meta mutations - #7392 by @maarcingebala- Optimize `inputType` resolver on `AttributeValue` type - 7396 by @tomaszszymanski129- Allow filtering pages by ids - #7393 by @IKarbowiak+- Fix validate `min_spent` on vouchers to use net or gross value depends on `settings.display_gross_prices` - #7408 by @d-wysocki- Fix invoice generation - #7376 by tomaszszymanski129- Unify channel ID params #7378- targetChannel from ChannelDeleteInput changed to channelIddiff --git a/saleor/checkout/tests/test_checkout.py b/saleor/checkout/tests/test_checkout.pyindex 28a49b2860..722bd57072 100644--- a/saleor/checkout/tests/test_checkout.py+++ b/saleor/checkout/tests/test_checkout.py@@ -181,7 +181,7 @@ def test_get_voucher_discount_for_checkout_voucher_validation(subtotal = manager.calculate_checkout_subtotal(checkout_info, lines, address, [])customer_email = checkout_with_voucher.get_customer_email()mock_validate_voucher.assert_called_once_with(- voucher, subtotal.gross, quantity, customer_email, checkout_with_voucher.channel+ voucher, subtotal, quantity, customer_email, checkout_with_voucher.channel)diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 25af832313..288b96ca6d 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -9,10 +9,11 @@ from django.utils import timezonefrom django_countries.fields import CountryFieldfrom django_prices.models import MoneyFieldfrom django_prices.templatetags.prices import amount-from prices import Money, fixed_discount, percentage_discount+from prices import Money, TaxedMoney, fixed_discount, percentage_discountfrom ..channel.models import Channelfrom ..core.permissions import DiscountPermissions+from ..core.taxes import display_gross_pricesfrom ..core.utils.translations import TranslationProxyfrom . import DiscountValueType, OrderDiscountType, VoucherType@@ -115,7 +116,8 @@ class Voucher(models.Model):return pricereturn price - after_discount- def validate_min_spent(self, value: Money, channel: Channel):+ def validate_min_spent(self, value: TaxedMoney, channel: Channel):+ value = value.gross if display_gross_prices() else value.netvoucher_channel_listing = self.channel_listings.filter(channel=channel).first()if not voucher_channel_listing:raise NotApplicable("This voucher is not assigned to this channel")diff --git a/saleor/discount/tests/test_discounts.py b/saleor/discount/tests/test_discounts.pyindex 6fc8cbaac1..018a005744 100644--- a/saleor/discount/tests/test_discounts.py+++ b/saleor/discount/tests/test_discounts.py@@ -3,7 +3,7 @@ from decimal import Decimalimport pytestfrom django.utils import timezone-from prices import Money+from prices import Money, TaxedMoneyfrom ...checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom ...checkout.utils import get_voucher_discount_for_checkout@@ -29,11 +29,12 @@ from ..utils import ()- "min_spent_amount, value",- [(Money(5, "USD"), Money(10, "USD")), (Money(10, "USD"), Money(10, "USD"))],-)-def test_valid_voucher_min_spent_amount(min_spent_amount, value, channel_USD):+def test_valid_voucher_min_spent_amount_with_display_gross_prices(+ channel_USD, site_settings+):+ site_settings.display_gross_prices = True+ site_settings.save()+voucher = Voucher.objects.create(code="unique",type=VoucherType.SHIPPING,@@ -43,12 +44,36 @@ def test_valid_voucher_min_spent_amount(min_spent_amount, value, channel_USD):voucher=voucher,channel=channel_USD,discount=Money(10, "USD"),- min_spent=min_spent_amount,+ min_spent=Money(7, "USD"),)+ value = TaxedMoney(net=Money(5, "USD"), gross=Money(7, "USD"))voucher.validate_min_spent(value, channel_USD)+def test_valid_voucher_min_spent_amount_without_display_gross_prices(+ channel_USD, site_settings+):+ site_settings.display_gross_prices = False+ site_settings.save()++ voucher = Voucher.objects.create(+ code="unique",+ type=VoucherType.SHIPPING,+ discount_value_type=DiscountValueType.FIXED,+ )+ VoucherChannelListing.objects.create(+ voucher=voucher,+ channel=channel_USD,+ discount=Money(10, "USD"),+ min_spent=Money(7, "USD"),+ )+ value = TaxedMoney(net=Money(5, "USD"), gross=Money(7, "USD"))++ with pytest.raises(NotApplicable):+ voucher.validate_min_spent(value, channel_USD)++def test_valid_voucher_min_spent_amount_voucher_not_assigned_to_channel(channel_USD, channel_PLN):@@ -63,8 +88,10 @@ def test_valid_voucher_min_spent_amount_voucher_not_assigned_to_channel(discount=Money(10, channel_USD.currency_code),min_spent=(Money(5, channel_USD.currency_code)),)+ price = Money(10, channel_PLN.currency_code)+ total_price = TaxedMoney(net=price, gross=price)with pytest.raises(NotApplicable):- voucher.validate_min_spent(Money(10, channel_PLN.currency_code), channel_PLN)+ voucher.validate_min_spent(total_price, channel_PLN)def test_valid_voucher_min_checkout_items_quantity(voucher):@@ -387,9 +414,8 @@ def test_validate_voucher(min_spent_amount=min_spent_amount,)total_price = Money(total, "USD")- validate_voucher(- voucher, total_price, total_quantity, "[email protected]", channel_USD- )+ price = TaxedMoney(gross=total_price, net=total_price)+ validate_voucher(voucher, price, total_quantity, "[email protected]", channel_USD)@pytest.mark.parametrize(@@ -423,9 +449,11 @@ def test_validate_voucher_not_applicable(min_spent_amount=min_spent_amount,)total_price = Money(total, "USD")+ price = TaxedMoney(net=total_price, gross=total_price)+with pytest.raises(NotApplicable):validate_voucher(- voucher, total_price, total_quantity, "[email protected]", channel_USD+ voucher, price, total_quantity, "[email protected]", channel_USD)@@ -435,8 +463,10 @@ def test_validate_voucher_not_applicable_once_per_customer(voucher.apply_once_per_customer = Truevoucher.save()VoucherCustomer.objects.create(voucher=voucher, customer_email=customer_user.email)+ price = Money(0, "USD")+ total_price = TaxedMoney(net=price, gross=price)with pytest.raises(NotApplicable):- validate_voucher(voucher, 0, 0, customer_user.email, channel_USD)+ validate_voucher(voucher, total_price, 0, customer_user.email, channel_USD)date_time_now = timezone.now()diff --git a/saleor/discount/utils.py b/saleor/discount/utils.pyindex e23f788a99..de7b768635 100644--- a/saleor/discount/utils.py+++ b/saleor/discount/utils.py@@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Setfrom django.db.models import Ffrom django.utils import timezone-from prices import Money+from prices import Money, TaxedMoneyfrom ..channel.models import Channelfrom ..checkout import calculations@@ -131,7 +131,7 @@ def validate_voucher_for_checkout(customer_email = checkout_info.get_customer_email()validate_voucher(voucher,- subtotal.gross,+ subtotal,quantity,customer_email,checkout_info.channel,@@ -144,14 +144,12 @@ def validate_voucher_in_order(order: "Order"):customer_email = order.get_customer_email()if not order.voucher:return- validate_voucher(- order.voucher, subtotal.gross, quantity, customer_email, order.channel- )+ validate_voucher(order.voucher, subtotal, quantity, customer_email, order.channel)def validate_voucher(voucher: "Voucher",- total_price: Money,+ total_price: TaxedMoney,quantity: int,customer_email: str,channel: Channel,diff --git a/saleor/graphql/checkout/tests/test_checkout_promo_codes.py b/saleor/graphql/checkout/tests/test_checkout_promo_codes.pyindex 3f4b5958dc..cb61103315 100644--- a/saleor/graphql/checkout/tests/test_checkout_promo_codes.py+++ b/saleor/graphql/checkout/tests/test_checkout_promo_codes.py@@ -5,8 +5,10 @@ import graphenefrom prices import Moneyfrom ....checkout import calculations+from ....checkout.error_codes import CheckoutErrorCodefrom ....checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom ....checkout.utils import add_voucher_to_checkout+from ....core.taxes import TaxedMoneyfrom ....discount import DiscountInfo, VoucherTypefrom ....plugins.manager import get_plugins_managerfrom ...tests.utils import get_graphql_content@@ -229,6 +231,7 @@ MUTATION_CHECKOUT_ADD_PROMO_CODE = """errors {fieldmessage+ code}checkout {id,@@ -264,6 +267,59 @@ def test_checkout_add_voucher_code(api_client, checkout_with_item, voucher):assert data["checkout"]["voucherCode"] == voucher.code+def test_checkout_add_voucher_code_with_display_gross_prices(+ api_client, checkout_with_item, voucher, site_settings, monkeypatch+):+ site_settings.display_gross_prices = True+ site_settings.save()++ voucher = voucher+ voucher_channel_listing = voucher.channel_listings.first()+ voucher_channel_listing.min_spent_amount = 100+ voucher_channel_listing.save()++ monkeypatch.setattr(+ "saleor.discount.utils.calculations.checkout_subtotal",+ lambda manager, checkout_info, lines, address, discounts: TaxedMoney(+ Money(95, "USD"), Money(100, "USD")+ ),+ )++ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)+ variables = {"checkoutId": checkout_id, "promoCode": voucher.code}+ data = _mutate_checkout_add_promo_code(api_client, variables)++ assert not data["errors"]+ assert data["checkout"]["id"] == checkout_id+ assert data["checkout"]["voucherCode"] == voucher.code+++def test_checkout_add_voucher_code_without_display_gross_prices(+ api_client, checkout_with_item, voucher, site_settings, monkeypatch+):+ site_settings.display_gross_prices = False+ site_settings.save()++ voucher = voucher+ voucher_channel_listing = voucher.channel_listings.first()+ voucher_channel_listing.min_spent_amount = 100+ voucher_channel_listing.save()++ monkeypatch.setattr(+ "saleor.discount.utils.calculations.checkout_subtotal",+ lambda manager, checkout_info, lines, address, discounts: TaxedMoney(+ Money(95, "USD"), Money(100, "USD")+ ),+ )++ checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)+ variables = {"checkoutId": checkout_id, "promoCode": voucher.code}+ data = _mutate_checkout_add_promo_code(api_client, variables)++ assert data["errors"][0]["code"] == CheckoutErrorCode.VOUCHER_NOT_APPLICABLE.name+ assert data["errors"][0]["field"] == "promoCode"++def test_checkout_add_voucher_code_checkout_with_sale(api_client, checkout_with_item, voucher_percentage, discount_info):diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex 7ff317f723..ea295a49c1 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -678,7 +678,7 @@ def test_get_voucher_discount_for_order_voucher_validation(validate_voucher_in_order(order_with_lines)mock_validate_voucher.assert_called_once_with(- voucher, subtotal.gross, quantity, customer_email, order_with_lines.channel+ voucher, subtotal, quantity, customer_email, order_with_lines.channel)