From f2edef5f65e4c5fd7aedd652e4d30e728a6d533e Mon Sep 17 00:00:00 2001 From: KwikKill Date: Sat, 28 Dec 2024 00:15:16 +0100 Subject: [PATCH] Fix multi product/discount handling --- insalan/payment/views.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/insalan/payment/views.py b/insalan/payment/views.py index 7d90f86e..056e49e6 100644 --- a/insalan/payment/views.py +++ b/insalan/payment/views.py @@ -424,17 +424,19 @@ def create(self, request): {"err": _("Préconditions de paiement non remplies")}, status=status.HTTP_400_BAD_REQUEST, ) - + amount = transaction_obj.amount - + # If the user has a discount for some products, apply them for product in transaction_obj.products.all(): discounts = Discount.objects.filter(user=payer, product=product, used=False) - if discounts.exists(): - discount = discounts.first() - amount = transaction_obj.amount - discount.discount - # Add the discount to the transaction object - transaction_obj.discounts.add(discount) + for discount in discounts: + # Check if the discount is applicable + if amount >= discount.discount: + amount -= discount.discount + + # Add the discount to the transaction object + transaction_obj.discounts.add(discount) # helloasso intent helloasso_amount = int(