diff --git a/insalan/payment/models.py b/insalan/payment/models.py index f64aa8de..032a920a 100644 --- a/insalan/payment/models.py +++ b/insalan/payment/models.py @@ -340,7 +340,7 @@ def validate_transaction(self): # For each discount, mark it as used for discount in self.discounts.all(): discount.use() - + self.save() logger.info("Transaction %s succeeded", self.id) self.run_success_hooks() @@ -394,6 +394,10 @@ class Meta: ) count = models.IntegerField(default=1, editable=True, verbose_name=_("Quantité")) +# Discount + +class DiscountAlreadyUsedError(Exception): + """Error raised when trying to use an already used discount""" class Discount(models.Model): """ @@ -434,6 +438,8 @@ class Meta: def use(self): """Use the discount""" + if self.used: + raise DiscountAlreadyUsedError("Discount already used") self.used = True self.used_date = timezone.make_aware(datetime.now()) self.save() diff --git a/insalan/payment/views.py b/insalan/payment/views.py index 02c853ae..7d90f86e 100644 --- a/insalan/payment/views.py +++ b/insalan/payment/views.py @@ -429,7 +429,7 @@ def create(self, request): # 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) + discounts = Discount.objects.filter(user=payer, product=product, used=False) if discounts.exists(): discount = discounts.first() amount = transaction_obj.amount - discount.discount