Commit 16f92d3a authored by Gabriel Detraz's avatar Gabriel Detraz

Systeme de commande fonctionnel

parent 72a43bdc
......@@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/1.8/ref/settings/
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from .settings_local import SECRET_KEY, DATABASES, DEBUG, ALLOWED_HOSTS, ASSO_NAME, ASSO_ADDRESS_LINE1, ASSO_ADDRESS_LINE2, ASSO_SIRET, ASSO_EMAIL, ASSO_PHONE, LOGO_PATH, services_urls, REQ_EXPIRE_HRS, REQ_EXPIRE_STR, EMAIL_FROM, SITE_NAME
from .settings_local import SECRET_KEY, DATABASES, DEBUG, ALLOWED_HOSTS, ASSO_NAME, ASSO_ADDRESS_LINE1, ASSO_ADDRESS_LINE2, ASSO_SIRET, ASSO_EMAIL, ASSO_PHONE, LOGO_PATH, services_urls, REQ_EXPIRE_HRS, REQ_EXPIRE_STR, EMAIL_FROM, MINIMUM_LEVEL, SITE_NAME
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
......
......@@ -17,7 +17,7 @@
<thead>
<tr>
<th>Date</th>
<th>Cableur</th>
<th>Action effectuee par</th>
<th>Commentaire</th>
</tr>
</thead>
......
......@@ -9,7 +9,7 @@ class PanierAdmin(VersionAdmin):
list_display = ('prix','nom')
class CommandeAdmin(admin.ModelAdmin):
list_display = ('user', 'quantite', 'prix', 'livre', 'date')
list_display = ('user', 'quantite', 'prix_unitaire', 'nom', 'livre', 'date')
class CreditAdmin(VersionAdmin):
list_display = ('user','montant','moyen','validite')
......
......@@ -4,14 +4,36 @@ from django import forms
from django.core.validators import MinValueValidator
from .models import Panier, Paiement, Commande, Credit, Livraison
class NewCommandeForm(ModelForm):
class Meta:
model = Commande
fields = ['quantite','date']
class BaseEditCommandeForm(NewCommandeForm):
class Meta(NewCommandeForm.Meta):
fields = ['quantite','date']
class FullEditCommandeForm(NewCommandeForm):
class Meta(NewCommandeForm.Meta):
fields = '__all__'
class SelectArticleForm(ModelForm):
article = forms.ModelChoiceField(queryset=Panier.objects.all(), label="Articles en vente actuellement")
class Meta:
exclude = ['nom','prix']
model = Panier
class LivraisonForm(ModelForm):
class Meta:
model = Livraison
fields = ['date']
fields = ['date','date_modif']
def __init__(self, *args, **kwargs):
super(LivraisonForm, self).__init__(*args, **kwargs)
self.fields['date'].label = 'Date de livraison'
self.fields['date_modif'].label = 'Date limite de modification'
class DelLivraisonForm(ModelForm):
date = forms.ModelMultipleChoiceField(queryset=Livraison.objects.all(), label="Date de livraison", widget=forms.CheckboxSelectMultiple)
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('panier', '0004_remove_panier_quantite'),
]
operations = [
migrations.RenameField(
model_name='commande',
old_name='prix',
new_name='prix_unitaire',
),
migrations.AddField(
model_name='commande',
name='nom',
field=models.CharField(default='Panier', max_length=255),
preserve_default=False,
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import datetime
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('panier', '0005_auto_20160905_0207'),
]
operations = [
migrations.AddField(
model_name='livraison',
name='date_modif',
field=models.DateTimeField(default=datetime.datetime(2016, 9, 5, 0, 51, 14, 377006, tzinfo=utc), help_text='%d/%m/%y %H:%M:%S'),
preserve_default=False,
),
]
......@@ -14,11 +14,15 @@ class Panier(models.Model):
class Commande(models.Model):
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
nom = models.CharField(max_length=255)
quantite = models.IntegerField()
prix = models.DecimalField(max_digits=5, decimal_places=2)
prix_unitaire = models.DecimalField(max_digits=5, decimal_places=2)
livre = models.BooleanField(default=False)
date = models.ForeignKey('Livraison', on_delete=models.PROTECT)
def prix(self):
return self.prix_unitaire*self.quantite
class Credit(models.Model):
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
montant = models.DecimalField(max_digits=5, decimal_places=2)
......@@ -26,7 +30,7 @@ class Credit(models.Model):
validite = models.BooleanField(default=True)
def __str__(self):
return str(self.montant) + str(self.user)
return str(self.montant) + "€ pour " + str(self.user)
class Paiement(models.Model):
moyen = models.CharField(max_length=255, unique=True)
......@@ -36,6 +40,7 @@ class Paiement(models.Model):
class Livraison(models.Model):
date = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S')
date_modif = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S')
def __str__(self):
return str(self.date)
......@@ -10,8 +10,10 @@
<tr>
<td>{{ article.nom }}</td>
<td>{{ article.prix }}</td>
{% if is_bureau %}
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'panier:edit-article' article.id %}"><i class="glyphicon glyphicon-pushpin"></i> Editer</a>
<a class="btn btn-info btn-sm" role="button" href="{% url 'panier:history' 'article' article.id %}"><i class="glyphicon glyphicon-repeat"></i> Historique</a></td>
{% endif %}
</tr>
{% endfor %}
</table>
......@@ -2,14 +2,18 @@
<thead>
<tr>
<th>Date de livraison</th>
<th>Date limite de modification des commandes</th>
<th></th>
</tr>
</thead>
{% for livraison in livraison_list %}
<tr>
<td>{{ livraison.date }}</td>
<td>{{ livraison.date_modif }}</td>
{% if is_bureau %}
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'panier:edit-livraison' livraison.id %}"><i class="glyphicon glyphicon-pushpin"></i> Editer</a>
<a class="btn btn-info btn-sm" role="button" href="{% url 'panier:history' 'livraison' livraison.id %}"><i class="glyphicon glyphicon-repeat"></i> Historique</a></td>
{% endif %}
</tr>
{% endfor %}
</table>
......@@ -20,20 +20,38 @@
<tr>
<th>Nom</th>
<th>Prenom</th>
<th>Qualité</th>
<th>Quantité</th>
<th>Prix</th>
<th>Livré</th>
<th>Date de livraison</th>
<th></th>
<th></th>
</tr>
</thead>
{% for panier in paniers_list %}
<tr>
<td>{{ panier.user.name }}</td>
<td>{{ panier.user.surname }}</td>
<td>{{ panier.nom }}</td>
<td>{{ panier.quantite }}</td>
<td>{{ panier.prix }}</td>
<td>{{ panier.livre }}</td>
{% if panier.livre %}
<td><font color="green">Oui</font></td>
{% else %}
<td><font color="red">Non</font></td>
{% endif %}
<td>{{ panier.date }}</td>
<td><div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="editioncredit" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Modifier
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="editionfacture">
<li><a href="{% url 'panier:edit-commande' panier.id %}"><i class="glyphicon glyphicon-bitcoin"></i> Editer</a></li>
<li><a href="{% url 'panier:del-commande' panier.id %}"><i class="glyphicon glyphicon-trash"></i> Supprimer</a></li>
</ul></div></td>
<td><a class="btn btn-info btn-sm" role="button" href="{% url 'panier:history' 'commande' panier.id %}"><i class="glyphicon glyphicon-repeat"></i> Historique</a></td>
</tr>
{% endfor %}
</table>
......@@ -73,7 +91,11 @@
<td>{{ credit.user.surname }}</td>
<td>{{ credit.montant }}</td>
<td>{{ credit.moyen }}</td>
<td>{{ credit.validite }}</td>
{% if credit.validite %}
<td><font color="green">Oui</font></td>
{% else %}
<td><font color="red">Non</font></td>
{% endif %}
{% if is_bureau %}
<td><div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="editioncredit" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
......
......@@ -5,8 +5,10 @@
{% block content %}
<h2>Liste des types d'articles</h2>
{% if is_bureau %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'panier:add-article' %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un type d'articles</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'panier:del-article' %}"><i class="glyphicon glyphicon-trash"></i> Supprimer un ou plusieurs types d'articles</a>
{% endif %}
{% include "panier/aff_article.html" with article_list=article_list %}
<br />
<br />
......
......@@ -5,8 +5,10 @@
{% block content %}
<h2>Liste des dates de livraisons</h2>
{% if is_bureau %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'panier:add-livraison' %}"><i class="glyphicon glyphicon-plus"></i> Ajouter une date de livraison</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'panier:del-livraison' %}"><i class="glyphicon glyphicon-trash"></i> Supprimer une ou plusieurs dates de livraisons</a>
{% endif %}
{% include "panier/aff_livraison.html" with livraison_list=livraison_list %}
<br />
<br />
......
......@@ -5,8 +5,10 @@
{% block content %}
<h2>Liste des types de paiements</h2>
{% if is_bureau %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'panier:add-paiement' %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un type de paiement</a>
<a class="btn btn-danger btn-sm" role="button" href="{% url 'panier:del-paiement' %}"><i class="glyphicon glyphicon-trash"></i> Supprimer un ou plusieurs types de paiements</a>
{% endif %}
{% include "panier/aff_paiement.html" with paiement_list=paiement_list %}
<br />
<br />
......
{% extends "panier/sidebar.html" %}
{% load bootstrap3 %}
{% block title %}Creation et modification de commander{% endblock %}
{% block content %}
{% bootstrap_form_errors article_form %}
{% bootstrap_form_errors commande_form %}
<h2>Commander un panier</h2>
<form class="form" method="post">
{% csrf_token %}
{% bootstrap_form article_form %}
{% bootstrap_form commande_form %}
{% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %}
</form>
<br />
<br />
<br />
{% endblock %}
{% extends "base.html" %}
{% block sidebar %}
{% if is_bureau %}
<p><a href="{% url "panier:index-paiement" %}">Liste des moyens de paiement</a></p>
<p><a href="{% url "panier:index-livraison" %}">Liste des dates de livraison</a></p>
<p><a href="{% url "panier:index-article" %}">Liste des paniers en vente</a></p>
{% if is_bureau %}
<p><a href="{% url "panier:index-paiement" %}">Liste des moyens de paiement</a></p>
{% endif %}
{% endblock %}
......@@ -3,6 +3,9 @@ from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^new_commande/(?P<userid>[0-9]+)$', views.new_commande, name='new-commande'),
url(r'^edit_commande/(?P<commandeid>[0-9]+)$', views.edit_commande, name='edit-commande'),
url(r'^del_commande/(?P<commandeid>[0-9]+)$', views.del_commande, name='del-commande'),
url(r'^add_paiement/$', views.add_paiement, name='add-paiement'),
url(r'^edit_paiement/(?P<paiementid>[0-9]+)$', views.edit_paiement, name='edit-paiement'),
url(r'^del_paiement/$', views.del_paiement, name='del-paiement'),
......@@ -22,6 +25,7 @@ urlpatterns = [
url(r'^history/(?P<object>livraison)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^history/(?P<object>credit)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^history/(?P<object>article)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^history/(?P<object>commande)/(?P<id>[0-9]+)$', views.history, name='history'),
url(r'^$', views.index, name='index'),
]
......
......@@ -15,11 +15,11 @@ from django.db import transaction
from reversion import revisions as reversion
from .models import Panier, Paiement, Commande, Credit, Livraison
from .forms import PaiementForm, CreditForm, CreditFullForm, DelPaiementForm, LivraisonForm, DelLivraisonForm
from .forms import PanierForm, DelPanierForm
from .forms import PanierForm, DelPanierForm, SelectArticleForm, BaseEditCommandeForm, NewCommandeForm
from users.views import User
from amap.settings import PAGINATION_NUMBER
from amap.settings import MINIMUM_LEVEL, PAGINATION_NUMBER
def form(ctx, template, request):
c = ctx
......@@ -30,6 +30,77 @@ def form(ctx, template, request):
context_instance=RequestContext(request)
)
@login_required
def new_commande(request, userid):
try:
user = User.objects.get(pk=userid)
except User.DoesNotExist:
messages.error(request, u"Utilisateur inexistant" )
return redirect("/panier/")
if not request.user.has_perms(('bureau',)) and user != request.user:
messages.error(request, "Vous ne pouvez pas commander pour quelqu'un d'autre !")
return redirect("/users/profil/" + str(request.user.id))
commande = Commande(user=user)
commande_form = NewCommandeForm(request.POST or None, instance=commande)
article_form = SelectArticleForm(request.POST or None)
if commande_form.is_valid() and article_form.is_valid():
commande = commande_form.save(commit=False)
prix_unitaire = article_form.cleaned_data['article'].prix
quantite = commande.quantite
if user.solde() - prix_unitaire*quantite < MINIMUM_LEVEL:
messages.error(request, "Solde insuffisant pour effectuer l'operation")
return redirect("/users/profil/" + userid)
with transaction.atomic(), reversion.create_revision():
commande.prix_unitaire = article_form.cleaned_data['article'].prix
commande.nom = article_form.cleaned_data['article'].nom
commande.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "La commande est enregistree")
return redirect("/users/profil/" + userid)
return form({'article_form': article_form, 'commande_form': commande_form}, 'panier/new_commande.html', request)
@login_required
def edit_commande(request, commandeid):
try:
commande_instance = Commande.objects.get(pk=commandeid)
except Commande.DoesNotExist:
messages.error(request, u"Entrée inexistante" )
return redirect("/panier/")
if not request.user.has_perms(('bureau',)) and commande_instance.user != request.user:
messages.error(request, "Vous ne pouvez pas commander pour quelqu'un d'autre !")
return redirect("/users/profil/" + str(request.user.id))
if request.user.has_perms(('bureau',)):
commande = BaseEditCommandeForm(request.POST or None, instance=commande_instance)
else:
commande = FullEditCommandeForm(request.POST or None, instance=commande_instance)
if commande.is_valid():
with transaction.atomic(), reversion.create_revision():
commande.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in commande.changed_data))
messages.success(request, "Commande modifiee")
return redirect("/panier/")
return form({'panierform': commande}, 'panier/panier.html', request)
@login_required
def del_commande(request, commandeid):
try:
commande = Commande.objects.get(pk=commandeid)
except Commande.DoesNotExist:
messages.error(request, u"Credit inexistant" )
return redirect("/panier/")
if not request.user.has_perms(('bureau',)) and commande.user != request.user:
messages.error(request, "Vous ne pouvez pas commander pour quelqu'un d'autre !")
return redirect("/users/profil/" + str(request.user.id))
if request.method == "POST":
with transaction.atomic(), reversion.create_revision():
commande.delete()
reversion.set_user(request.user)
messages.success(request, "La commande a été détruite")
return redirect("/panier/")
return form({'objet': commande, 'objet_name': 'commande'}, 'panier/delete.html', request)
@login_required
@permission_required('bureau')
def add_livraison(request):
......@@ -58,7 +129,7 @@ def edit_livraison(request, livraisonid):
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in livraison.changed_data))
messages.success(request, "Date modifiee")
return redirect("/penier/index_livraison/")
return redirect("/panier/index_livraison/")
return form({'panierform': livraison}, 'panier/panier.html', request)
@login_required
......@@ -239,6 +310,15 @@ def history(request, object, id):
if not request.user.has_perms(('bureau',)) and object_instance.user != request.user:
messages.error(request, "Vous ne pouvez pas afficher l'historique d'une facture d'un autre user que vous sans droit ")
return redirect("/panier/")
elif object == 'commande':
try:
object_instance = Commande.objects.get(pk=id)
except Commande.DoesNotExist:
messages.error(request, "Commande inexistante")
return redirect("/panier/")
if not request.user.has_perms(('bureau',)) and object_instance.user != request.user:
messages.error(request, "Vous ne pouvez pas afficher l'historique d'une commande d'un autre user que vous sans droit ")
return redirect("/panier/")
elif object == 'paiement' and request.user.has_perms(('bureau',)):
try:
object_instance = Paiement.objects.get(pk=id)
......@@ -276,7 +356,7 @@ def history(request, object, id):
@login_required
def index(request):
paniers_list = Panier.objects.order_by('pk')
paniers_list = Commande.objects.order_by('pk')
credit_list = Credit.objects.order_by('pk')
if not request.user.has_perms(('bureau',)):
paniers_list = paniers_list.filter(user=request.user)
......@@ -304,7 +384,6 @@ def index(request):
return render(request, 'panier/index.html', {'paniers_list': paniers_list, 'credit_list': credit_list})
@login_required
@permission_required('bureau')
def index_article(request):
article_list = Panier.objects.order_by('nom')
return render(request, 'panier/index_article.html', {'article_list':article_list})
......@@ -316,7 +395,6 @@ def index_paiement(request):
return render(request, 'panier/index_paiement.html', {'paiement_list':paiement_list})
@login_required
@permission_required('bureau')
def index_livraison(request):
livraison_list = Livraison.objects.order_by('date')
return render(request, 'panier/index_livraison.html', {'livraison_list':livraison_list})
......@@ -13,6 +13,8 @@ import datetime
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from panier.models import Credit, Commande
def get_admin_right():
try:
admin_right = ListRight.objects.get(listright="admin")
......@@ -132,10 +134,6 @@ class User(AbstractBaseUser):
def has_perm(self, perm, obj=None):
return True
def has_access(self):
""" Renvoie si un utilisateur a accès à internet """
return self.state == User.STATE_ACTIVE
def has_module_perms(self, app_label):
# Simplest version again
return True
......@@ -152,6 +150,12 @@ class User(AbstractBaseUser):
return
user_right.delete()
def solde(self):
""" Renvoie le solde dynamiquement"""
credit = sum(credit.montant for credit in Credit.objects.filter(user=self) if credit.validite)
debit = sum(commande.prix_unitaire*commande.quantite for commande in Commande.objects.filter(user=self))
return credit-debit
def __str__(self):
return self.pseudo
......
......@@ -30,12 +30,13 @@
<td>{{ user.name }}</td>
<td>{{ user.surname }}</td>
<td>{{ user.pseudo }}</td>
<td>{% if user.has_access == True %}
<font color="green">Active</font>
{% else %}
<font color="red">Désactivée</font>
{% endif %}
</td>
{% if user.state == 0 %}
<td><font color="green">Actif</font></td>
{% elif user.state == 1 %}
<td><font color="red">Désactivé</font></td>
{% else %}
<td><font color="orange">Archivé</font></td>
{% endif %}
<td>{{ user.telephone }}</td>
<td>{{ user.email }}</td>
<td><a href="{% url "users:profil" user.id%}" class="btn btn-primary btn-sm" role="button"><i class="glyphicon glyphicon-user"></i></a>
......
......@@ -29,23 +29,17 @@
<th>Telephone</th>
<td>{{ user.telephone }}</td>
<th>Solde</th>
<td></td>
<td>{{ user.solde }} €</td>
</tr>
<tr>
<th>Commentaire</th>
<td>{{ user.comment }}</td>
<th>Date d'inscription</th>
<td>{{ user.registered }}</td>
</tr>
<tr>
<th>ENS</th>
{% if user.is_ens %}
<td><font color="green">Oui</font></td>
{% else %}
<td><font color="red">Non</font></td>
{% endif %}
<th>Solde</th>
<td></td>
</tr>
<tr>
<th>Statut</th>
......@@ -64,10 +58,14 @@
{% endif %}
</tr>
<tr>
<th>Commentaire</th>
<td>{{ user.comment }}</td>
</tr>
</table>
<h2>Commandes et crédits</h2>
{% if is_bureau %}<a class="btn btn-primary btn-sm" role="button" href="{% url 'panier:add-credit' user.id %}"><i class="glyphicon glyphicon-piggy-bank"></i> Enregistrer un crédit</a>{% endif %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'panier:new-commande' user.id %}"><i class="glyphicon glyphicon-shopping-cart"></i> Nouvelle commande</a>
<br/>
{% include "panier/aff_paniers.html" %}
......
......@@ -20,7 +20,7 @@ from users.models import DelRightForm, DelListRightForm, NewListRightForm
from users.models import EditInfoForm, BaseInfoForm, StateForm, RightForm, ListRightForm
from users.forms import PassForm, ResetPasswordForm
from panier.models import Panier, Credit
from panier.models import Commande, Credit
from amap.settings import REQ_EXPIRE_STR, EMAIL_FROM, ASSO_NAME, ASSO_EMAIL, SITE_NAME, PAGINATION_NUMBER
......@@ -295,11 +295,8 @@ def profil(request, userid):
messages.error(request, "Vous ne pouvez pas afficher un autre user que vous sans droit bureau")
return redirect("/users/profil/" + str(request.user.id))
list_droits = Right.objects.filter(user=users)
paniers_list = Panier.objects.order_by('pk')
credit_list = Credit.objects.order_by('pk')
if not request.user.has_perms(('bureau',)):
paniers_list = paniers_list.filter(user=request.user)
credit_list = credit_list.filter(user=request.user)
paniers_list = Commande.objects.filter(user=users).order_by('pk')
credit_list = Credit.objects.filter(user=users).order_by('pk')
paginator_paniers = Paginator(paniers_list, PAGINATION_NUMBER)
paginator_credit = Paginator(credit_list, PAGINATION_NUMBER)
page = request.GET.get('page')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment