diff --git a/gestion/migrations/0008_auto_20181130_1904.py b/gestion/migrations/0008_auto_20181130_1904.py new file mode 100644 index 0000000..da35ba8 --- /dev/null +++ b/gestion/migrations/0008_auto_20181130_1904.py @@ -0,0 +1,21 @@ +# Generated by Django 2.1 on 2018-11-30 18:04 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('gestion', '0007_auto_20181127_0902'), + ] + + operations = [ + migrations.RemoveField( + model_name='consumptionhistory', + name='menu', + ), + migrations.RemoveField( + model_name='historicalconsumptionhistory', + name='menu', + ), + ] diff --git a/gestion/models.py b/gestion/models.py index 8a5a21a..a553bde 100644 --- a/gestion/models.py +++ b/gestion/models.py @@ -180,7 +180,6 @@ class ConsumptionHistory(models.Model): paymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.PROTECT) date = models.DateTimeField(auto_now_add=True) product = models.ForeignKey(Product, on_delete=models.PROTECT) - menu = models.ForeignKey(MenuHistory, on_delete=models.CASCADE, null=True, blank=True) amount = models.DecimalField(max_digits=7, decimal_places=2, default=0) coopeman = models.ForeignKey(User, on_delete=models.PROTECT, related_name="consumption_selled") history = HistoricalRecords() diff --git a/gestion/urls.py b/gestion/urls.py index 41b0cda..e4828cd 100644 --- a/gestion/urls.py +++ b/gestion/urls.py @@ -31,6 +31,8 @@ urlpatterns = [ path('ranking', views.ranking, name="ranking"), path('annualRanking', views.annualRanking, name="annualRanking"), path('searchProduct', views.searchProduct, name="searchProduct"), + path('cancelConsumption/', views.cancel_consumption, name="cancelConsumption"), + path('cancelMenu/', views.cancel_menu, name="cancelMenu"), path('productProfile/', views.productProfile, name="productProfile"), path('products-autocomplete', views.ProductsAutocomplete.as_view(), name="products-autocomplete"), path('kegs-positive-autocomplete', views.KegPositiveAutocomplete.as_view(), name="kegs-positive-autocomplete"), diff --git a/gestion/views.py b/gestion/views.py index ac59eeb..0964297 100644 --- a/gestion/views.py +++ b/gestion/views.py @@ -115,8 +115,9 @@ def order(request): consumption, _ = Consumption.objects.get_or_create(customer=user, product=article) consumption.quantity += quantity consumption.save() - ch = ConsumptionHistory(customer=user, quantity=quantity, paymentMethod=paymentMethod, product=article, amount=int(quantity*article.amount), coopeman=request.user, menu=mh) - ch.save() + if(article.stockHold > 0): + article.stockHold -= 1 + article.save() return HttpResponse("La commande a bien été effectuée") @login_required @@ -156,6 +157,28 @@ def refund(request): messages.error(request, "Le remboursement a échoué") return redirect(reverse('gestion:manage')) +@login_required +@permission_required('gestion.delete_consumptionhistory') +def cancel_consumption(request, pk): + consumption = get_object_or_404(ConsumptionHistory, pk=pk) + user = consumption.customer + user.profile.debit -= consumption.amount + user.save() + consumption.delete() + messages.success(request, "La consommation a bien été annulée") + return redirect(reverse('users:profile', kwargs={'pk': user.pk})) + +@login_required +@permission_required('gestion.delete_menuhistory') +def cancel_menu(request, pk): + menu_history = get_object_or_404(MenuHistory, pk=pk) + user = menu_history.customer + user.profile.debit -= menu_history.amount + user.save() + menu_history.delete() + messages.success(request, "La consommation du menu a bien été annulée") + return redirect(reverse('users:profile', kwargs={'pk': user.pk})) + ########## Products ########## @login_required @@ -452,4 +475,4 @@ def ranking(request): @login_required def annualRanking(request): - return render(request, "gestion/annual_ranking.html") \ No newline at end of file + return render(request, "gestion/annual_ranking.html") diff --git a/users/templates/users/all_consumptions.html b/users/templates/users/all_consumptions.html new file mode 100644 index 0000000..6cae83d --- /dev/null +++ b/users/templates/users/all_consumptions.html @@ -0,0 +1,61 @@ +{% extends "base.html" %} +{% load static %} +{% block entete %}

Consommations

{%endblock%} + +{% block navbar %} + +{% endblock %} +{% block content %} +
+
+

Consommations ({{user}})

+
+
+
+ + + + + + + + + + + + + {% for c in consumptions %} + + + + + + + + + {%endfor%} + +
ProduitQuantitéMontantType de PaiementDateAnnuler
{{c.product}}{{c.quantity}}{{c.amount}}{{c.paymentMethod}}{{c.date}}Annuler
+
+ +
+
+{%endblock%} diff --git a/users/templates/users/all_menus.html b/users/templates/users/all_menus.html new file mode 100644 index 0000000..9ad411a --- /dev/null +++ b/users/templates/users/all_menus.html @@ -0,0 +1,61 @@ +{% extends "base.html" %} +{% load static %} +{% block entete %}

Menus

{%endblock%} + +{% block navbar %} + +{% endblock %} +{% block content %} +
+
+

Consommations de menus ({{user}})

+
+
+
+ + + + + + + + + + + + + {% for m in menus %} + + + + + + + + + {%endfor%} + +
MenuQuantitéMontantType de PaiementDateAnnuler
{{m.menu}}{{m.quantity}}{{m.amount}}{{m.paymentMethod}}{{m.date}}Annuler
+
+ +
+
+{%endblock%} diff --git a/users/templates/users/profile.html b/users/templates/users/profile.html index eb8aa83..6f6308e 100644 --- a/users/templates/users/profile.html +++ b/users/templates/users/profile.html @@ -127,7 +127,7 @@

{{self | yesno:"Mes dernières,Dernières"}} consommations

-

(Affichage des 10 dernières entrées)

+

(Affichage des 10 dernières entrées : Voir toutes les entrées)

@@ -139,7 +139,7 @@ Montant Type de Paiement Date - + Annuler @@ -150,7 +150,41 @@ {{c.amount}} {{c.paymentMethod}} {{c.date}} - + Annuler + + {%endfor%} + + +
+
+
+
+
+

{{self | yesno:"Mes derniers,Derniers"}} menus

+

(Affichage des 5 dernières entrées : Voir toutes les entrées)

+
+
+
+ + + + + + + + + + + + + {% for m in lastMenus %} + + + + + + + {%endfor%} @@ -161,7 +195,7 @@

{{self | yesno:"Mes derniers,Derniers"}} rechargements

-

(Affichage des 5 dernières entrées)

+

(Affichage des 5 dernières entrées : Voir toutes les entrées)

diff --git a/users/urls.py b/users/urls.py index 8637409..2a6d318 100644 --- a/users/urls.py +++ b/users/urls.py @@ -41,4 +41,6 @@ urlpatterns = [ path('editSchool/', views.editSchool, name="editSchool"), path('deleteSchool/', views.deleteSchool, name="deleteSchool"), path('allReloads//', views.allReloads, name="allReloads"), + path('allConsumptions//', views.all_consumptions, name="allConsumptions"), + path('allMenus//', views.all_menus, name="allMenus"), ] diff --git a/users/views.py b/users/views.py index bf4c7b5..08a85f6 100644 --- a/users/views.py +++ b/users/views.py @@ -15,7 +15,7 @@ from dal import autocomplete from coopeV3.acl import admin_required, superuser_required, self_or_has_perm, active_required from .models import CotisationHistory, WhiteListHistory, School from .forms import CreateUserForm, LoginForm, CreateGroupForm, EditGroupForm, SelectUserForm, GroupsEditForm, EditPasswordForm, addCotisationHistoryForm, addCotisationHistoryForm, addWhiteListHistoryForm, SelectNonAdminUserForm, SelectNonSuperUserForm, SchoolForm -from gestion.models import Reload, Consumption, ConsumptionHistory +from gestion.models import Reload, Consumption, ConsumptionHistory, MenuHistory @active_required def loginView(request): @@ -123,6 +123,7 @@ def profile(request, pk): products.append(ch.product) quantities.append(ch.quantity) lastConsumptions = ConsumptionHistory.objects.filter(customer=user).order_by('-date')[:10] + lastMenus = MenuHistory.objects.filter(customer=user).order_by('-date')[:10] return render(request, "users/profile.html", { "user":user, @@ -132,7 +133,8 @@ def profile(request, pk): "reloads": reloads, "products": products, "quantities": quantities, - "lastConsumptions": lastConsumptions + "lastConsumptions": lastConsumptions, + "lastMenus": lastMenus, }) @active_required @@ -380,10 +382,68 @@ def allReloads(request, pk, page): """ user = get_object_or_404(User, pk=pk) allReloads = Reload.objects.filter(customer=user).order_by('-date') - paginator = Paginator(allReloads, 2) + paginator = Paginator(allReloads, 10) reloads = paginator.get_page(page) return render(request, "users/allReloads.html", {"reloads": reloads, "user":user}) +@active_required +@login_required +@self_or_has_perm('pk', 'auth.view_user') +def all_consumptions(request, pk, page): + """ + Display all the consumptions of the requested user. + + ``pk`` + The pk of the user. + ``page`` + The page number. + + **Context** + + ``reloads`` + The reloads of the page. + ``user`` + The requested user + + **Template** + + :template:`users/all_consumptions.html` + """ + user = get_object_or_404(User, pk=pk) + all_consumptions = ConsumptionHistory.objects.filter(customer=user).order_by('-date') + paginator = Paginator(all_consumptions, 10) + consumptions = paginator.get_page(page) + return render(request, "users/all_consumptions.html", {"consumptions": consumptions, "user":user}) + +@active_required +@login_required +@self_or_has_perm('pk', 'auth.view_user') +def all_menus(request, pk, page): + """ + Display all the menus of the requested user. + + ``pk`` + The pk of the user. + ``page`` + The page number. + + **Context** + + ``reloads`` + The reloads of the page. + ``user`` + The requested user + + **Template** + + :template:`users/all_menus.html` + """ + user = get_object_or_404(User, pk=pk) + all_menus = MenuHistory.objects.filter(customer=user).order_by('-date') + paginator = Paginator(all_menus, 10) + menus = paginator.get_page(page) + return render(request, "users/all_menus.html", {"menus": menus, "user":user}) + ########## Groups ########## @active_required
MenuQuantitéMontantType de PaiementDateAnnuler
{{m.menu}}{{m.quantity}}{{m.amount}}{{m.paymentMethod}}{{m.date}}Annuler