2017-01-15 23:01:18 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2017 Gabriel Détraz
|
|
|
|
# Copyright © 2017 Goulven Kermarec
|
|
|
|
# Copyright © 2017 Augustin Lemesle
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
# App de gestion des users pour re2o
|
|
|
|
# Goulven Kermarec, Gabriel Détraz
|
|
|
|
# Gplv2
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-10-13 20:47:32 +00:00
|
|
|
import os
|
2017-10-26 22:11:18 +00:00
|
|
|
|
|
|
|
from django.urls import reverse
|
2016-07-02 13:58:50 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2016-07-22 01:25:04 +00:00
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
2018-01-11 18:25:41 +00:00
|
|
|
from django.core.validators import MaxValueValidator
|
2016-07-09 02:12:09 +00:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2016-07-02 13:58:50 +00:00
|
|
|
from django.contrib import messages
|
2017-10-13 20:47:32 +00:00
|
|
|
from django.db.models import ProtectedError
|
2016-07-21 14:58:12 +00:00
|
|
|
from django.db import transaction
|
2017-10-28 04:02:53 +00:00
|
|
|
from django.db.models import Q
|
2016-07-13 23:54:06 +00:00
|
|
|
from django.forms import modelformset_factory, formset_factory
|
2017-10-13 20:47:32 +00:00
|
|
|
from django.utils import timezone
|
2018-01-12 00:07:25 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.decorators.debug import sensitive_variables
|
2016-07-21 14:58:12 +00:00
|
|
|
from reversion import revisions as reversion
|
2016-12-11 16:24:04 +00:00
|
|
|
from reversion.models import Version
|
2017-10-13 20:47:32 +00:00
|
|
|
# Import des models, forms et fonctions re2o
|
|
|
|
from users.models import User
|
|
|
|
from re2o.settings import LOGO_PATH
|
|
|
|
from re2o import settings
|
|
|
|
from re2o.views import form
|
2017-12-29 00:51:24 +00:00
|
|
|
from re2o.utils import SortTable
|
|
|
|
from re2o.acl import (
|
2017-12-13 17:56:16 +00:00
|
|
|
can_create,
|
|
|
|
can_edit,
|
|
|
|
can_delete,
|
|
|
|
can_view,
|
2017-12-27 20:09:00 +00:00
|
|
|
can_view_all,
|
|
|
|
can_delete_set,
|
|
|
|
can_change,
|
2017-12-13 17:56:16 +00:00
|
|
|
)
|
2017-10-13 20:47:32 +00:00
|
|
|
from preferences.models import OptionalUser, AssoOption, GeneralOption
|
|
|
|
from .models import Facture, Article, Vente, Paiement, Banque
|
2017-10-28 04:02:53 +00:00
|
|
|
from .forms import (
|
|
|
|
NewFactureForm,
|
|
|
|
EditFactureForm,
|
|
|
|
ArticleForm,
|
|
|
|
DelArticleForm,
|
|
|
|
PaiementForm,
|
|
|
|
DelPaiementForm,
|
|
|
|
BanqueForm,
|
|
|
|
DelBanqueForm,
|
|
|
|
NewFactureFormPdf,
|
|
|
|
SelectUserArticleForm,
|
|
|
|
SelectClubArticleForm,
|
2018-01-11 18:25:41 +00:00
|
|
|
CreditSoldeForm,
|
|
|
|
NewFactureSoldeForm,
|
|
|
|
RechargeForm
|
2017-10-28 04:02:53 +00:00
|
|
|
)
|
2018-01-12 00:07:25 +00:00
|
|
|
from . import payment
|
2017-11-14 19:00:24 +00:00
|
|
|
from .tex import render_invoice
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2017-12-28 01:08:02 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_create(Facture)
|
|
|
|
@can_edit(User)
|
|
|
|
def new_facture(request, user, userid):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Creation d'une facture pour un user. Renvoie la liste des articles
|
|
|
|
et crée des factures dans un formset. Utilise un peu de js coté template
|
|
|
|
pour ajouter des articles.
|
|
|
|
Parse les article et boucle dans le formset puis save les ventes,
|
|
|
|
enfin sauve la facture parente.
|
|
|
|
TODO : simplifier cette fonction, déplacer l'intelligence coté models
|
|
|
|
Facture et Vente."""
|
2016-07-02 15:58:04 +00:00
|
|
|
facture = Facture(user=user)
|
2016-07-14 18:51:45 +00:00
|
|
|
# Le template a besoin de connaitre les articles pour le js
|
2017-10-28 04:02:53 +00:00
|
|
|
article_list = Article.objects.filter(
|
|
|
|
Q(type_user='All') | Q(type_user=request.user.class_name)
|
|
|
|
)
|
2016-07-14 18:51:45 +00:00
|
|
|
# On envoie la form fature et un formset d'articles
|
2016-07-02 13:58:50 +00:00
|
|
|
facture_form = NewFactureForm(request.POST or None, instance=facture)
|
2017-10-28 04:02:53 +00:00
|
|
|
if request.user.is_class_club:
|
|
|
|
article_formset = formset_factory(SelectClubArticleForm)(request.POST or None)
|
|
|
|
else:
|
|
|
|
article_formset = formset_factory(SelectUserArticleForm)(request.POST or None)
|
2016-07-14 11:55:46 +00:00
|
|
|
if facture_form.is_valid() and article_formset.is_valid():
|
2017-10-13 20:47:32 +00:00
|
|
|
new_facture_instance = facture_form.save(commit=False)
|
2016-07-14 11:55:46 +00:00
|
|
|
articles = article_formset
|
2016-07-14 18:51:45 +00:00
|
|
|
# Si au moins un article est rempli
|
2016-07-14 11:55:46 +00:00
|
|
|
if any(art.cleaned_data for art in articles):
|
2017-10-13 20:47:32 +00:00
|
|
|
options, _created = OptionalUser.objects.get_or_create()
|
2017-06-26 17:23:01 +00:00
|
|
|
user_solde = options.user_solde
|
|
|
|
solde_negatif = options.solde_negatif
|
2017-10-13 03:08:30 +00:00
|
|
|
# Si on paye par solde, que l'option est activée,
|
|
|
|
# on vérifie que le négatif n'est pas atteint
|
2017-06-26 17:23:01 +00:00
|
|
|
if user_solde:
|
2017-10-13 20:47:32 +00:00
|
|
|
if new_facture_instance.paiement == Paiement.objects.get_or_create(
|
|
|
|
moyen='solde'
|
2017-10-13 03:08:30 +00:00
|
|
|
)[0]:
|
2017-06-26 17:23:01 +00:00
|
|
|
prix_total = 0
|
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
2017-10-13 03:08:30 +00:00
|
|
|
prix_total += art_item.cleaned_data['article']\
|
|
|
|
.prix*art_item.cleaned_data['quantity']
|
2017-06-26 17:23:01 +00:00
|
|
|
if float(user.solde) - float(prix_total) < solde_negatif:
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.error(request, "Le solde est insuffisant pour\
|
|
|
|
effectuer l'opération")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
|
|
|
))
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
2017-10-13 20:47:32 +00:00
|
|
|
new_facture_instance.save()
|
2016-07-21 14:58:12 +00:00
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
2016-07-14 11:55:46 +00:00
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
|
|
|
article = art_item.cleaned_data['article']
|
|
|
|
quantity = art_item.cleaned_data['quantity']
|
2017-10-13 03:08:30 +00:00
|
|
|
new_vente = Vente.objects.create(
|
2017-10-13 20:47:32 +00:00
|
|
|
facture=new_facture_instance,
|
|
|
|
name=article.name,
|
|
|
|
prix=article.prix,
|
2017-10-28 02:59:40 +00:00
|
|
|
type_cotisation=article.type_cotisation,
|
2017-10-13 20:47:32 +00:00
|
|
|
duration=article.duration,
|
|
|
|
number=quantity
|
|
|
|
)
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
new_vente.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
2017-10-28 02:59:40 +00:00
|
|
|
if any(art_item.cleaned_data['article'].type_cotisation
|
2017-10-13 20:47:32 +00:00
|
|
|
for art_item in articles if art_item.cleaned_data):
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
"La cotisation a été prolongée\
|
|
|
|
pour l'adhérent %s jusqu'au %s" % (
|
|
|
|
user.pseudo, user.end_adhesion()
|
|
|
|
)
|
|
|
|
)
|
2016-07-14 11:55:46 +00:00
|
|
|
else:
|
|
|
|
messages.success(request, "La facture a été crée")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
|
|
|
))
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.error(
|
2017-10-13 20:47:32 +00:00
|
|
|
request,
|
|
|
|
u"Il faut au moins un article valide pour créer une facture"
|
2017-10-13 03:08:30 +00:00
|
|
|
)
|
|
|
|
return form({
|
|
|
|
'factureform': facture_form,
|
|
|
|
'venteform': article_formset,
|
|
|
|
'articlelist': article_list
|
|
|
|
}, 'cotisations/new_facture.html', request)
|
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-28 13:04:14 +00:00
|
|
|
@can_change(Facture, 'pdf')
|
2016-07-08 01:27:02 +00:00
|
|
|
def new_facture_pdf(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Permet de générer un pdf d'une facture. Réservée
|
|
|
|
au trésorier, permet d'emettre des factures sans objet
|
|
|
|
Vente ou Facture correspondant en bdd"""
|
2016-07-09 21:26:17 +00:00
|
|
|
facture_form = NewFactureFormPdf(request.POST or None)
|
|
|
|
if facture_form.is_valid():
|
2017-10-13 20:47:32 +00:00
|
|
|
options, _created = AssoOption.objects.get_or_create()
|
2016-07-09 21:26:17 +00:00
|
|
|
tbl = []
|
|
|
|
article = facture_form.cleaned_data['article']
|
|
|
|
quantite = facture_form.cleaned_data['number']
|
|
|
|
paid = facture_form.cleaned_data['paid']
|
|
|
|
destinataire = facture_form.cleaned_data['dest']
|
2016-07-13 23:54:06 +00:00
|
|
|
chambre = facture_form.cleaned_data['chambre']
|
|
|
|
fid = facture_form.cleaned_data['fid']
|
2017-10-13 20:47:32 +00:00
|
|
|
for art in article:
|
|
|
|
tbl.append([art, quantite, art.prix * quantite])
|
2016-07-09 21:26:17 +00:00
|
|
|
prix_total = sum(a[2] for a in tbl)
|
2017-10-13 03:08:30 +00:00
|
|
|
user = {'name': destinataire, 'room': chambre}
|
2017-11-14 19:00:24 +00:00
|
|
|
return render_invoice(request, {
|
2017-10-13 03:08:30 +00:00
|
|
|
'DATE': timezone.now(),
|
|
|
|
'dest': user,
|
|
|
|
'fid': fid,
|
|
|
|
'article': tbl,
|
|
|
|
'total': prix_total,
|
|
|
|
'paid': paid,
|
|
|
|
'asso_name': options.name,
|
|
|
|
'line1': options.adresse1,
|
|
|
|
'line2': options.adresse2,
|
|
|
|
'siret': options.siret,
|
|
|
|
'email': options.contact,
|
|
|
|
'phone': options.telephone,
|
|
|
|
'tpl_path': os.path.join(settings.BASE_DIR, LOGO_PATH)
|
|
|
|
})
|
|
|
|
return form({
|
|
|
|
'factureform': facture_form
|
|
|
|
}, 'cotisations/facture.html', request)
|
|
|
|
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2016-07-11 22:05:07 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view(Facture)
|
|
|
|
def facture_pdf(request, facture, factureid):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Affiche en pdf une facture. Cree une ligne par Vente de la facture,
|
|
|
|
et génére une facture avec le total, le moyen de paiement, l'adresse
|
|
|
|
de l'adhérent, etc. Réservée à self pour un user sans droits,
|
|
|
|
les droits cableurs permettent d'afficher toute facture"""
|
2017-12-27 20:09:00 +00:00
|
|
|
|
2017-10-13 20:47:32 +00:00
|
|
|
ventes_objects = Vente.objects.all().filter(facture=facture)
|
2016-07-11 22:05:07 +00:00
|
|
|
ventes = []
|
2017-10-13 20:47:32 +00:00
|
|
|
options, _created = AssoOption.objects.get_or_create()
|
|
|
|
for vente in ventes_objects:
|
|
|
|
ventes.append([vente, vente.number, vente.prix_total])
|
2017-11-14 19:00:24 +00:00
|
|
|
return render_invoice(request, {
|
2017-10-13 03:08:30 +00:00
|
|
|
'paid': True,
|
|
|
|
'fid': facture.id,
|
|
|
|
'DATE': facture.date,
|
|
|
|
'dest': facture.user,
|
|
|
|
'article': ventes,
|
|
|
|
'total': facture.prix_total(),
|
|
|
|
'asso_name': options.name,
|
|
|
|
'line1': options.adresse1,
|
|
|
|
'line2': options.adresse2,
|
|
|
|
'siret': options.siret,
|
|
|
|
'email': options.contact,
|
|
|
|
'phone': options.telephone,
|
|
|
|
'tpl_path': os.path.join(settings.BASE_DIR, LOGO_PATH)
|
|
|
|
})
|
|
|
|
|
2016-07-11 22:05:07 +00:00
|
|
|
|
2016-07-13 23:54:06 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_edit(Facture)
|
|
|
|
def edit_facture(request, facture, factureid):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Permet l'édition d'une facture. On peut y éditer les ventes
|
|
|
|
déjà effectuer, ou rendre une facture invalide (non payées, chèque
|
|
|
|
en bois etc). Mets à jour les durée de cotisation attenantes"""
|
2017-12-28 01:08:02 +00:00
|
|
|
facture_form = EditFactureForm(request.POST or None, instance=facture, user=request.user)
|
2016-07-13 23:54:06 +00:00
|
|
|
ventes_objects = Vente.objects.filter(facture=facture)
|
2017-10-13 03:08:30 +00:00
|
|
|
vente_form_set = modelformset_factory(
|
2017-10-13 20:47:32 +00:00
|
|
|
Vente,
|
|
|
|
fields=('name', 'number'),
|
|
|
|
extra=0,
|
|
|
|
max_num=len(ventes_objects)
|
|
|
|
)
|
2016-07-13 23:54:06 +00:00
|
|
|
vente_form = vente_form_set(request.POST or None, queryset=ventes_objects)
|
|
|
|
if facture_form.is_valid() and vente_form.is_valid():
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
facture_form.save()
|
|
|
|
vente_form.save()
|
|
|
|
reversion.set_user(request.user)
|
2017-10-13 03:08:30 +00:00
|
|
|
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
|
|
|
|
field for form in vente_form for field
|
2017-10-13 20:47:32 +00:00
|
|
|
in facture_form.changed_data + form.changed_data))
|
2016-07-02 13:58:50 +00:00
|
|
|
messages.success(request, "La facture a bien été modifiée")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index'))
|
2017-10-13 03:08:30 +00:00
|
|
|
return form({
|
|
|
|
'factureform': facture_form,
|
|
|
|
'venteform': vente_form
|
|
|
|
}, 'cotisations/edit_facture.html', request)
|
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2016-07-14 20:29:30 +00:00
|
|
|
@login_required
|
2017-12-09 01:15:11 +00:00
|
|
|
@can_delete(Facture)
|
|
|
|
def del_facture(request, facture, factureid):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Suppression d'une facture. Supprime en cascade les ventes
|
|
|
|
et cotisations filles"""
|
2016-07-18 16:37:52 +00:00
|
|
|
if request.method == "POST":
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
facture.delete()
|
|
|
|
reversion.set_user(request.user)
|
2016-07-18 16:37:52 +00:00
|
|
|
messages.success(request, "La facture a été détruite")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index'))
|
2017-10-13 03:08:30 +00:00
|
|
|
return form({
|
|
|
|
'objet': facture,
|
|
|
|
'objet_name': 'facture'
|
|
|
|
}, 'cotisations/delete.html', request)
|
|
|
|
|
2016-07-14 20:29:30 +00:00
|
|
|
|
2017-06-26 17:23:01 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_create(Facture)
|
|
|
|
@can_edit(User)
|
|
|
|
def credit_solde(request, user, userid):
|
2017-06-26 17:23:01 +00:00
|
|
|
""" Credit ou débit de solde """
|
|
|
|
facture = CreditSoldeForm(request.POST or None)
|
|
|
|
if facture.is_valid():
|
|
|
|
facture_instance = facture.save(commit=False)
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
facture_instance.user = user
|
|
|
|
facture_instance.save()
|
|
|
|
reversion.set_user(request.user)
|
2017-10-13 03:08:30 +00:00
|
|
|
reversion.set_comment("Création")
|
|
|
|
new_vente = Vente.objects.create(
|
2017-10-13 20:47:32 +00:00
|
|
|
facture=facture_instance,
|
|
|
|
name="solde",
|
|
|
|
prix=facture.cleaned_data['montant'],
|
|
|
|
number=1
|
|
|
|
)
|
2017-06-26 17:23:01 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
new_vente.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
|
|
|
messages.success(request, "Solde modifié")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index'))
|
2017-06-26 17:23:01 +00:00
|
|
|
return form({'factureform': facture}, 'cotisations/facture.html', request)
|
|
|
|
|
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_create(Article)
|
2016-07-06 18:57:31 +00:00
|
|
|
def add_article(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Ajoute un article. Champs : désignation,
|
|
|
|
prix, est-ce une cotisation et si oui sa durée
|
|
|
|
Réservé au trésorier
|
|
|
|
Nota bene : les ventes déjà effectuées ne sont pas reliées
|
|
|
|
aux articles en vente. La désignation, le prix... sont
|
|
|
|
copiés à la création de la facture. Un changement de prix n'a
|
|
|
|
PAS de conséquence sur les ventes déjà faites"""
|
2016-07-06 18:57:31 +00:00
|
|
|
article = ArticleForm(request.POST or None)
|
|
|
|
if article.is_valid():
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
article.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
2016-07-06 18:57:31 +00:00
|
|
|
messages.success(request, "L'article a été ajouté")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-article'))
|
2016-07-08 01:27:02 +00:00
|
|
|
return form({'factureform': article}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_edit(Article)
|
|
|
|
def edit_article(request, article_instance, articleid):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Edition d'un article (designation, prix, etc)
|
|
|
|
Réservé au trésorier"""
|
2016-07-08 01:27:02 +00:00
|
|
|
article = ArticleForm(request.POST or None, instance=article_instance)
|
|
|
|
if article.is_valid():
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
article.save()
|
|
|
|
reversion.set_user(request.user)
|
2017-10-13 03:08:30 +00:00
|
|
|
reversion.set_comment(
|
|
|
|
"Champs modifié(s) : %s" % ', '.join(
|
|
|
|
field for field in article.changed_data
|
|
|
|
)
|
|
|
|
)
|
2016-07-08 01:27:02 +00:00
|
|
|
messages.success(request, "Type d'article modifié")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-article'))
|
2016-07-06 18:57:31 +00:00
|
|
|
return form({'factureform': article}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-13 17:56:16 +00:00
|
|
|
@can_delete_set(Article)
|
|
|
|
def del_article(request, instances):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Suppression d'un article en vente"""
|
2017-12-13 17:56:16 +00:00
|
|
|
article = DelArticleForm(request.POST or None, instances=instances)
|
2016-07-06 18:57:31 +00:00
|
|
|
if article.is_valid():
|
|
|
|
article_del = article.cleaned_data['articles']
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
article_del.delete()
|
|
|
|
reversion.set_user(request.user)
|
2016-07-06 18:57:31 +00:00
|
|
|
messages.success(request, "Le/les articles ont été supprimé")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-article'))
|
2016-07-06 18:57:31 +00:00
|
|
|
return form({'factureform': article}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_create(Paiement)
|
2016-07-06 19:43:39 +00:00
|
|
|
def add_paiement(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Ajoute un moyen de paiement. Relié aux factures
|
|
|
|
via foreign key"""
|
2016-07-06 19:43:39 +00:00
|
|
|
paiement = PaiementForm(request.POST or None)
|
|
|
|
if paiement.is_valid():
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
paiement.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
2016-07-06 19:43:39 +00:00
|
|
|
messages.success(request, "Le moyen de paiement a été ajouté")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-paiement'))
|
2016-07-08 01:27:02 +00:00
|
|
|
return form({'factureform': paiement}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_edit(Paiement)
|
|
|
|
def edit_paiement(request, paiement_instance, paiementid):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Edition d'un moyen de paiement"""
|
2016-07-08 01:27:02 +00:00
|
|
|
paiement = PaiementForm(request.POST or None, instance=paiement_instance)
|
|
|
|
if paiement.is_valid():
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
paiement.save()
|
|
|
|
reversion.set_user(request.user)
|
2017-10-13 03:08:30 +00:00
|
|
|
reversion.set_comment(
|
|
|
|
"Champs modifié(s) : %s" % ', '.join(
|
2017-10-13 20:47:32 +00:00
|
|
|
field for field in paiement.changed_data
|
2017-10-13 03:08:30 +00:00
|
|
|
)
|
|
|
|
)
|
2016-07-08 01:27:02 +00:00
|
|
|
messages.success(request, "Type de paiement modifié")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-paiement'))
|
2016-07-06 19:43:39 +00:00
|
|
|
return form({'factureform': paiement}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-13 17:56:16 +00:00
|
|
|
@can_delete_set(Paiement)
|
|
|
|
def del_paiement(request, instances):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Suppression d'un moyen de paiement"""
|
2017-12-13 17:56:16 +00:00
|
|
|
paiement = DelPaiementForm(request.POST or None, instances=instances)
|
2016-07-06 19:43:39 +00:00
|
|
|
if paiement.is_valid():
|
|
|
|
paiement_dels = paiement.cleaned_data['paiements']
|
|
|
|
for paiement_del in paiement_dels:
|
|
|
|
try:
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
paiement_del.delete()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Destruction")
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.success(
|
2017-10-13 20:47:32 +00:00
|
|
|
request,
|
|
|
|
"Le moyen de paiement a été supprimé"
|
2017-10-13 03:08:30 +00:00
|
|
|
)
|
2016-07-06 19:43:39 +00:00
|
|
|
except ProtectedError:
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
"Le moyen de paiement %s est affecté à au moins une\
|
|
|
|
facture, vous ne pouvez pas le supprimer" % paiement_del
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-paiement'))
|
2016-07-06 19:43:39 +00:00
|
|
|
return form({'factureform': paiement}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_create(Banque)
|
2016-07-06 20:20:49 +00:00
|
|
|
def add_banque(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Ajoute une banque à la liste des banques"""
|
2016-07-06 20:20:49 +00:00
|
|
|
banque = BanqueForm(request.POST or None)
|
|
|
|
if banque.is_valid():
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
banque.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
2016-07-06 20:20:49 +00:00
|
|
|
messages.success(request, "La banque a été ajoutée")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-banque'))
|
2016-07-08 01:27:02 +00:00
|
|
|
return form({'factureform': banque}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_edit(Banque)
|
|
|
|
def edit_banque(request, banque_instance, banqueid):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Edite le nom d'une banque"""
|
2016-07-08 01:27:02 +00:00
|
|
|
banque = BanqueForm(request.POST or None, instance=banque_instance)
|
|
|
|
if banque.is_valid():
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
banque.save()
|
|
|
|
reversion.set_user(request.user)
|
2017-10-13 03:08:30 +00:00
|
|
|
reversion.set_comment(
|
|
|
|
"Champs modifié(s) : %s" % ', '.join(
|
|
|
|
field for field in banque.changed_data
|
|
|
|
)
|
|
|
|
)
|
2016-07-08 01:27:02 +00:00
|
|
|
messages.success(request, "Banque modifiée")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-banque'))
|
2016-07-06 20:20:49 +00:00
|
|
|
return form({'factureform': banque}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-13 17:56:16 +00:00
|
|
|
@can_delete_set(Banque)
|
|
|
|
def del_banque(request, instances):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Supprime une banque"""
|
2017-12-13 17:56:16 +00:00
|
|
|
banque = DelBanqueForm(request.POST or None, instances=instances)
|
2016-07-06 20:20:49 +00:00
|
|
|
if banque.is_valid():
|
|
|
|
banque_dels = banque.cleaned_data['banques']
|
|
|
|
for banque_del in banque_dels:
|
|
|
|
try:
|
2016-07-21 14:58:12 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
banque_del.delete()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Destruction")
|
2016-07-06 20:20:49 +00:00
|
|
|
messages.success(request, "La banque a été supprimée")
|
|
|
|
except ProtectedError:
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.error(request, "La banque %s est affectée à au moins\
|
|
|
|
une facture, vous ne pouvez pas la supprimer" % banque_del)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-banque'))
|
2016-07-06 20:20:49 +00:00
|
|
|
return form({'factureform': banque}, 'cotisations/facture.html', request)
|
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-17 18:08:56 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view_all(Facture)
|
2017-12-28 13:04:14 +00:00
|
|
|
@can_change(Facture, 'control')
|
2016-07-17 18:08:56 +00:00
|
|
|
def control(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Pour le trésorier, vue pour controler en masse les
|
|
|
|
factures.Case à cocher, pratique"""
|
2017-10-13 20:47:32 +00:00
|
|
|
options, _created = GeneralOption.objects.get_or_create()
|
2017-06-25 23:29:34 +00:00
|
|
|
pagination_number = options.pagination_number
|
2017-10-22 00:33:44 +00:00
|
|
|
facture_list = Facture.objects.select_related('user').select_related('paiement')
|
|
|
|
facture_list = SortTable.sort(
|
|
|
|
facture_list,
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.COTISATIONS_CONTROL
|
|
|
|
)
|
2017-10-13 03:08:30 +00:00
|
|
|
controlform_set = modelformset_factory(
|
2017-10-13 20:47:32 +00:00
|
|
|
Facture,
|
|
|
|
fields=('control', 'valid'),
|
|
|
|
extra=0
|
|
|
|
)
|
2017-06-25 23:29:34 +00:00
|
|
|
paginator = Paginator(facture_list, pagination_number)
|
2016-11-18 10:31:33 +00:00
|
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
|
|
facture_list = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
facture_list = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
facture_list = paginator.page(paginator.num.pages)
|
2017-10-22 00:33:44 +00:00
|
|
|
controlform = controlform_set(request.POST or None, queryset=facture_list.object_list)
|
2016-07-17 18:08:56 +00:00
|
|
|
if controlform.is_valid():
|
2016-12-14 02:08:57 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
controlform.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Controle trésorier")
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:control'))
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/control.html', {
|
|
|
|
'facture_list': facture_list,
|
|
|
|
'controlform': controlform
|
|
|
|
})
|
|
|
|
|
2016-07-17 18:08:56 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view_all(Article)
|
2016-07-08 01:27:02 +00:00
|
|
|
def index_article(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Affiche l'ensemble des articles en vente"""
|
2016-07-08 01:27:02 +00:00
|
|
|
article_list = Article.objects.order_by('name')
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/index_article.html', {
|
|
|
|
'article_list': article_list
|
|
|
|
})
|
|
|
|
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view_all(Paiement)
|
2016-07-08 01:27:02 +00:00
|
|
|
def index_paiement(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Affiche l'ensemble des moyens de paiement en vente"""
|
2016-07-08 01:27:02 +00:00
|
|
|
paiement_list = Paiement.objects.order_by('moyen')
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/index_paiement.html', {
|
|
|
|
'paiement_list': paiement_list
|
|
|
|
})
|
|
|
|
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view_all(Banque)
|
2016-07-08 01:27:02 +00:00
|
|
|
def index_banque(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Affiche l'ensemble des banques"""
|
2016-07-08 01:27:02 +00:00
|
|
|
banque_list = Banque.objects.order_by('name')
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/index_banque.html', {
|
|
|
|
'banque_list': banque_list
|
|
|
|
})
|
|
|
|
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view_all(Facture)
|
2016-07-02 13:58:50 +00:00
|
|
|
def index(request):
|
2017-10-13 03:08:30 +00:00
|
|
|
"""Affiche l'ensemble des factures, pour les cableurs et +"""
|
2017-10-13 20:47:32 +00:00
|
|
|
options, _created = GeneralOption.objects.get_or_create()
|
2017-06-25 23:29:34 +00:00
|
|
|
pagination_number = options.pagination_number
|
2017-10-22 00:33:44 +00:00
|
|
|
facture_list = Facture.objects.select_related('user')\
|
|
|
|
.select_related('paiement').prefetch_related('vente_set')
|
|
|
|
facture_list = SortTable.sort(
|
|
|
|
facture_list,
|
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.COTISATIONS_INDEX
|
|
|
|
)
|
2017-06-25 23:29:34 +00:00
|
|
|
paginator = Paginator(facture_list, pagination_number)
|
2016-07-22 01:25:04 +00:00
|
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
|
|
facture_list = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
# If page is not an integer, deliver first page.
|
|
|
|
facture_list = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
|
facture_list = paginator.page(paginator.num_pages)
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/index.html', {
|
|
|
|
'facture_list': facture_list
|
|
|
|
})
|
2018-01-11 18:25:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def new_facture_solde(request, userid):
|
|
|
|
"""Creation d'une facture pour un user. Renvoie la liste des articles
|
|
|
|
et crée des factures dans un formset. Utilise un peu de js coté template
|
|
|
|
pour ajouter des articles.
|
|
|
|
Parse les article et boucle dans le formset puis save les ventes,
|
|
|
|
enfin sauve la facture parente.
|
|
|
|
TODO : simplifier cette fonction, déplacer l'intelligence coté models
|
|
|
|
Facture et Vente."""
|
|
|
|
user = request.user
|
|
|
|
facture = Facture(user=user)
|
|
|
|
paiement, _created = Paiement.objects.get_or_create(moyen='Solde')
|
|
|
|
facture.paiement = paiement
|
|
|
|
# Le template a besoin de connaitre les articles pour le js
|
|
|
|
article_list = Article.objects.filter(
|
|
|
|
Q(type_user='All') | Q(type_user=request.user.class_name)
|
|
|
|
)
|
|
|
|
if request.user.is_class_club:
|
|
|
|
article_formset = formset_factory(SelectClubArticleForm)(request.POST or None)
|
|
|
|
else:
|
|
|
|
article_formset = formset_factory(SelectUserArticleForm)(request.POST or None)
|
|
|
|
if article_formset.is_valid():
|
|
|
|
articles = article_formset
|
|
|
|
# Si au moins un article est rempli
|
|
|
|
if any(art.cleaned_data for art in articles):
|
|
|
|
options, _created = OptionalUser.objects.get_or_create()
|
|
|
|
user_solde = options.user_solde
|
|
|
|
solde_negatif = options.solde_negatif
|
|
|
|
# Si on paye par solde, que l'option est activée,
|
|
|
|
# on vérifie que le négatif n'est pas atteint
|
|
|
|
if user_solde:
|
|
|
|
prix_total = 0
|
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
|
|
|
prix_total += art_item.cleaned_data['article']\
|
|
|
|
.prix*art_item.cleaned_data['quantity']
|
|
|
|
if float(user.solde) - float(prix_total) < solde_negatif:
|
|
|
|
messages.error(request, "Le solde est insuffisant pour\
|
|
|
|
effectuer l'opération")
|
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
|
|
|
))
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
facture.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
|
|
|
article = art_item.cleaned_data['article']
|
|
|
|
quantity = art_item.cleaned_data['quantity']
|
|
|
|
new_vente = Vente.objects.create(
|
|
|
|
facture=facture,
|
|
|
|
name=article.name,
|
|
|
|
prix=article.prix,
|
|
|
|
type_cotisation=article.type_cotisation,
|
|
|
|
duration=article.duration,
|
|
|
|
number=quantity
|
|
|
|
)
|
|
|
|
with transaction.atomic(), reversion.create_revision():
|
|
|
|
new_vente.save()
|
|
|
|
reversion.set_user(request.user)
|
|
|
|
reversion.set_comment("Création")
|
|
|
|
if any(art_item.cleaned_data['article'].type_cotisation
|
|
|
|
for art_item in articles if art_item.cleaned_data):
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
"La cotisation a été prolongée\
|
|
|
|
pour l'adhérent %s jusqu'au %s" % (
|
|
|
|
user.pseudo, user.end_adhesion()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
messages.success(request, "La facture a été crée")
|
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
|
|
|
))
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
u"Il faut au moins un article valide pour créer une facture"
|
|
|
|
)
|
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
|
|
|
))
|
|
|
|
|
|
|
|
return form({
|
|
|
|
'venteform': article_formset,
|
|
|
|
'articlelist': article_list
|
|
|
|
}, 'cotisations/new_facture_solde.html', request)
|
|
|
|
|
|
|
|
|
2018-01-12 00:07:25 +00:00
|
|
|
@login_required
|
|
|
|
def recharge(request):
|
2018-01-14 19:15:21 +00:00
|
|
|
f = RechargeForm(request.POST or None, user=request.user)
|
2018-01-12 00:07:25 +00:00
|
|
|
if f.is_valid():
|
|
|
|
facture = Facture(user=request.user)
|
|
|
|
paiement, _created = Paiement.objects.get_or_create(moyen='Rechargement en ligne')
|
|
|
|
facture.paiement = paiement
|
|
|
|
facture.valid = False
|
|
|
|
facture.save()
|
|
|
|
v = Vente.objects.create(
|
|
|
|
facture=facture,
|
|
|
|
name='solde',
|
|
|
|
prix=f.cleaned_data['value'],
|
|
|
|
number=1,
|
|
|
|
)
|
|
|
|
v.save()
|
|
|
|
options, _created = AssoOption.objects.get_or_create()
|
2018-01-14 19:15:21 +00:00
|
|
|
content = payment.PAYMENT_SYSTEM[options.payment](facture, request)
|
2018-01-12 00:07:25 +00:00
|
|
|
return render(request, 'cotisations/payment.html', content)
|
|
|
|
return form({'rechargeform':f}, 'cotisations/recharge.html', request)
|