mirror of
https://github.com/nanoy42/coope
synced 2024-09-12 01:53:08 +00:00
Improve transactions
This commit is contained in:
parent
d7e21b6600
commit
192696d026
2 changed files with 16 additions and 19 deletions
|
@ -72,13 +72,15 @@ def order(request):
|
||||||
"""
|
"""
|
||||||
Processes the given order. The order is passed through POST.
|
Processes the given order. The order is passed through POST.
|
||||||
"""
|
"""
|
||||||
error_message = "Impossible d'effectuer la transaction. Toute opération abandonnée. Veuillez contacter le président ou le trésorier"
|
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
if("user" not in request.POST or "paymentMethod" not in request.POST or "amount" not in request.POST or "order" not in request.POST):
|
if("user" not in request.POST or "paymentMethod" not in request.POST or "amount" not in request.POST or "order" not in request.POST):
|
||||||
raise Exception("Erreur du post")
|
raise Exception("Erreur du post.")
|
||||||
else:
|
else:
|
||||||
user = get_object_or_404(User, pk=request.POST['user'])
|
try:
|
||||||
|
user = User.objects.get(pk=request.POST['user'])
|
||||||
|
except:
|
||||||
|
raise Exception("Impossible de récupérer l'utilisateur")
|
||||||
paymentMethod = get_object_or_404(PaymentMethod, pk=request.POST['paymentMethod'])
|
paymentMethod = get_object_or_404(PaymentMethod, pk=request.POST['paymentMethod'])
|
||||||
amount = Decimal(request.POST['amount'])
|
amount = Decimal(request.POST['amount'])
|
||||||
order = json.loads(request.POST["order"])
|
order = json.loads(request.POST["order"])
|
||||||
|
@ -87,8 +89,7 @@ def order(request):
|
||||||
cotisations = json.loads(request.POST['cotisations'])
|
cotisations = json.loads(request.POST['cotisations'])
|
||||||
gp,_ = GeneralPreferences.objects.get_or_create(pk=1)
|
gp,_ = GeneralPreferences.objects.get_or_create(pk=1)
|
||||||
if (not order) and (not menus) and (not cotisations):
|
if (not order) and (not menus) and (not cotisations):
|
||||||
error_message = "Pas de commande"
|
raise Exception("Pas de commande.")
|
||||||
raise Exception(error_message)
|
|
||||||
if(cotisations):
|
if(cotisations):
|
||||||
for co in cotisations:
|
for co in cotisations:
|
||||||
cotisation = Cotisation.objects.get(pk=co['pk'])
|
cotisation = Cotisation.objects.get(pk=co['pk'])
|
||||||
|
@ -98,8 +99,7 @@ def order(request):
|
||||||
if(user.profile.balance >= cotisation_history.cotisation.amount):
|
if(user.profile.balance >= cotisation_history.cotisation.amount):
|
||||||
user.profile.debit += cotisation_history.cotisation.amount
|
user.profile.debit += cotisation_history.cotisation.amount
|
||||||
else:
|
else:
|
||||||
error_message = "Solde insuffisant"
|
raise Exception("Solde insuffisant")
|
||||||
raise Exception(error_message)
|
|
||||||
else:
|
else:
|
||||||
user.profile.direct_debit += cotisation_history.cotisation.amount
|
user.profile.direct_debit += cotisation_history.cotisation.amount
|
||||||
cotisation_history.user = user
|
cotisation_history.user = user
|
||||||
|
@ -122,15 +122,13 @@ def order(request):
|
||||||
menu = get_object_or_404(Menu, pk=m["pk"])
|
menu = get_object_or_404(Menu, pk=m["pk"])
|
||||||
adherentRequired = adherentRequired or menu.adherent_required
|
adherentRequired = adherentRequired or menu.adherent_required
|
||||||
if(adherentRequired and not user.profile.is_adherent):
|
if(adherentRequired and not user.profile.is_adherent):
|
||||||
error_message = "N'est pas adhérent et devrait l'être."
|
raise Exception("N'est pas adhérent et devrait l'être.")
|
||||||
raise Exception(error_message)
|
|
||||||
# Partie un peu complexe : je libère toutes les pintes de la commande, puis je test
|
# Partie un peu complexe : je libère toutes les pintes de la commande, puis je test
|
||||||
# s'il a trop de pintes non rendues, puis je réalloue les pintes
|
# s'il a trop de pintes non rendues, puis je réalloue les pintes
|
||||||
for pinte in listPintes:
|
for pinte in listPintes:
|
||||||
allocate(pinte, None)
|
allocate(pinte, None)
|
||||||
if(gp.use_pinte_monitoring and gp.lost_pintes_allowed and user.profile.nb_pintes >= gp.lost_pintes_allowed):
|
if(gp.use_pinte_monitoring and gp.lost_pintes_allowed and user.profile.nb_pintes >= gp.lost_pintes_allowed):
|
||||||
error_message = "Impossible de réaliser la commande : l'utilisateur a perdu trop de pintes."
|
raise Exception("Impossible de réaliser la commande : l'utilisateur a perdu trop de pintes.")
|
||||||
raise Exception(error_message)
|
|
||||||
for pinte in listPintes:
|
for pinte in listPintes:
|
||||||
allocate(pinte, user)
|
allocate(pinte, user)
|
||||||
for o in order:
|
for o in order:
|
||||||
|
@ -161,7 +159,7 @@ def order(request):
|
||||||
kegHistory.amountSold += Decimal(quantity * product.amount)
|
kegHistory.amountSold += Decimal(quantity * product.amount)
|
||||||
kegHistory.save()
|
kegHistory.save()
|
||||||
if product.use_stocks:
|
if product.use_stocks:
|
||||||
if(product.stock > quantity):
|
if(product.stock >= quantity):
|
||||||
product.stock -= quantity
|
product.stock -= quantity
|
||||||
product.save()
|
product.save()
|
||||||
else:
|
else:
|
||||||
|
@ -176,8 +174,7 @@ def order(request):
|
||||||
if(user.profile.balance >= Decimal(product.amount*quantity)):
|
if(user.profile.balance >= Decimal(product.amount*quantity)):
|
||||||
user.profile.debit += Decimal(product.amount*quantity)
|
user.profile.debit += Decimal(product.amount*quantity)
|
||||||
else:
|
else:
|
||||||
error_message = "Solde insuffisant"
|
raise Exception("Solde insuffisant")
|
||||||
raise Exception(error_message)
|
|
||||||
else:
|
else:
|
||||||
user.profile.direct_debit += Decimal(product.amount*quantity)
|
user.profile.direct_debit += Decimal(product.amount*quantity)
|
||||||
for m in menus:
|
for m in menus:
|
||||||
|
@ -189,8 +186,7 @@ def order(request):
|
||||||
if(user.profile.balance >= Decimal(product.amount*quantity)):
|
if(user.profile.balance >= Decimal(product.amount*quantity)):
|
||||||
user.profile.debit += Decimal(product.amount*quantity)
|
user.profile.debit += Decimal(product.amount*quantity)
|
||||||
else:
|
else:
|
||||||
error_message = "Solde insuffisant"
|
raise Exception("Solde insuffisant")
|
||||||
raise Exception(error_message)
|
|
||||||
else:
|
else:
|
||||||
user.profile.direct_debit += Decimal(product.amount*quantity)
|
user.profile.direct_debit += Decimal(product.amount*quantity)
|
||||||
for article in menu.articles.all():
|
for article in menu.articles.all():
|
||||||
|
@ -222,7 +218,7 @@ def order(request):
|
||||||
kegHistory.amountSold += Decimal(quantity * product.amount)
|
kegHistory.amountSold += Decimal(quantity * product.amount)
|
||||||
kegHistory.save()
|
kegHistory.save()
|
||||||
if article.use_stocks:
|
if article.use_stocks:
|
||||||
if(article.stock > quantity):
|
if(article.stock >= quantity):
|
||||||
article.stock -= quantity
|
article.stock -= quantity
|
||||||
article.save()
|
article.save()
|
||||||
else:
|
else:
|
||||||
|
@ -231,7 +227,7 @@ def order(request):
|
||||||
user.save()
|
user.save()
|
||||||
return HttpResponse("La commande a bien été effectuée")
|
return HttpResponse("La commande a bien été effectuée")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return HttpResponse(error_message)
|
return HttpResponse("Impossible d'effectuer la transaction : " + e.args[0])
|
||||||
|
|
||||||
@active_required
|
@active_required
|
||||||
@login_required
|
@login_required
|
||||||
|
|
|
@ -211,6 +211,7 @@ $(document).ready(function(){
|
||||||
location.reload();
|
location.reload();
|
||||||
}).fail(function(data){
|
}).fail(function(data){
|
||||||
alert("Impossible d'effectuer la transaction. Veuillez contacter le trésorier ou le président");
|
alert("Impossible d'effectuer la transaction. Veuillez contacter le trésorier ou le président");
|
||||||
|
console.log(data);
|
||||||
location.reload();
|
location.reload();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue