From fb95d781026a89663665f654b2bc224b0562e828 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 29 Mar 2020 23:51:42 +0200 Subject: [PATCH] Update KegHistory when canceling operation --- gestion/views.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gestion/views.py b/gestion/views.py index d9a415f..7c56c46 100644 --- a/gestion/views.py +++ b/gestion/views.py @@ -338,6 +338,27 @@ def cancel_consumption(request, pk): if product.use_stocks: product.stock += consumption.quantity product.save() + if(product.draft_category == Product.DRAFT_PINTE): + keg = get_object_or_404(Keg, pinte=product) + kegHistory = KegHistory.objects.filter(keg=keg, isCurrentKegHistory=True) + if kegHistory: + kegHistory[0].quantitySold -= Decimal(quantity * 0.5) + kegHistory[0].amountSold -= Decimal(quantity * product.amount) + kegHistory[0].save() + elif(product.draft_category == Product.DRAFT_DEMI): + keg = get_object_or_404(Keg, demi=product) + kegHistory = KegHistory.objects.filter(keg=keg, isCurrentKegHistory=True) + if kegHistory: + kegHistory[0].quantitySold -= Decimal(quantity * 0.25) + kegHistory[0].amountSold -= Decimal(quantity * product.amount) + kegHistory[0].save() + elif(product.draft_category == Product.DRAFT_GALOPIN): + keg = get_object_or_404(Keg, galopin=product) + kegHistory = KegHistory.objects.filter(keg=keg, isCurrentKegHistory=True) + if kegHistory: + kegHistory[0].quantitySold += Decimal(quantity * 0.125) + kegHistory[0].amountSold += Decimal(quantity * product.amount) + kegHistory[0].save() consumption.delete() messages.success(request, "La consommation a bien été annulée") return redirect(reverse('users:profile', kwargs={'pk': user.pk}))