views.py 9.92 KB
Newer Older
chirac's avatar
chirac committed
1 2 3 4 5 6 7
# App de gestion des users pour re2o
# Goulven Kermarec, Gabriel Détraz
# Gplv2
from django.shortcuts import render, redirect
from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf
from django.template import Context, RequestContext, loader
8
from django.contrib.auth.decorators import login_required, permission_required
chirac's avatar
chirac committed
9
from django.contrib import messages
10
from django.db.models import Max, ProtectedError
chirac's avatar
chirac committed
11

12
from .models import Facture, Article, Cotisation, Paiement, Banque
Dalahro's avatar
Dalahro committed
13
from .forms import NewFactureForm, EditFactureForm, ArticleForm, DelArticleForm, PaiementForm, DelPaiementForm, BanqueForm, DelBanqueForm, NewFactureFormPdf
14
from users.models import User
15
from .tex import render_tex
Dalahro's avatar
Dalahro committed
16
from re2o.settings import ASSO_NAME, ASSO_ADDRESS_LINE1, ASSO_ADDRESS_LINE2, ASSO_SIRET, ASSO_EMAIL, ASSO_PHONE
chirac's avatar
chirac committed
17

18
from dateutil.relativedelta import relativedelta
chirac's avatar
chirac committed
19
from django.utils import timezone
20

chirac's avatar
chirac committed
21 22 23 24 25
def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
    return render_to_response(template, c, context_instance=RequestContext(request))

26
def create_cotis(facture, user, duration):
27 28
    """ Update et crée l'objet cotisation associé à une facture, prend en argument l'user, la facture pour la quantitéi, et l'article pour la durée"""
    cotisation=Cotisation(facture=facture)
29
    date_max = user.end_adhesion() or timezone.now()
chirac's avatar
chirac committed
30 31 32
    if date_max < timezone.now():
        datemax = timezone.now()
    cotisation.date_start=date_max
33
    cotisation.date_end = cotisation.date_start + relativedelta(months=duration) 
34 35 36
    cotisation.save()
    return

chirac's avatar
chirac committed
37
@login_required
chirac's avatar
chirac committed
38
@permission_required('cableur')
chirac's avatar
chirac committed
39
def new_facture(request, userid):
40 41 42 43 44 45
    try:
        user = User.objects.get(pk=userid)
    except User.DoesNotExist:
        messages.error(request, u"Utilisateur inexistant" )
        return redirect("/cotisations/")
    facture = Facture(user=user)
chirac's avatar
chirac committed
46 47
    facture_form = NewFactureForm(request.POST or None, instance=facture)
    if facture_form.is_valid():
48 49
        new_facture = facture_form.save(commit=False)
        article = facture_form.cleaned_data['article']
50 51
        new_facture.prix = sum(art.prix for art in article)
        new_facture.name = ' - '.join(art.name for art in article)
52
        new_facture.save()
53 54 55
        if any(art.cotisation for art in article):
            duration = sum(art.duration*facture.number for art in article if art.cotisation)
            create_cotis(new_facture, user, duration)
56 57 58
            messages.success(request, "La cotisation a été prolongée pour l'adhérent %s " % user.name )
        else:
            messages.success(request, "La facture a été crée")
chirac's avatar
chirac committed
59
        return redirect("/users/profil/" + userid)
chirac's avatar
chirac committed
60 61
    return form({'factureform': facture_form}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
62
@login_required
63
def new_facture_pdf(request):
Dalahro's avatar
Dalahro committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    facture_form = NewFactureFormPdf(request.POST or None)
    if facture_form.is_valid():
        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']
        objet = facture_form.cleaned_data['obj']
        detail = facture_form.cleaned_data['detail']
        for a in article:
            tbl.append([a, quantite, a.prix * quantite])
        prix_total = sum(a[2] for a in tbl)
        return render_tex(request, 'cotisations/factures.tex', {'DATE' : timezone.now(),'dest':destinataire, 'obj':objet, 'detail':detail, 'article':tbl, 'total':prix_total, 'paid':paid, 'asso_name':ASSO_NAME, 'line1':ASSO_ADDRESS_LINE1, 'line2':ASSO_ADDRESS_LINE2, 'siret':ASSO_SIRET, 'email':ASSO_EMAIL, 'phone':ASSO_PHONE})
    return form({'factureform': facture_form}, 'cotisations/facture.html', request) 
78

chirac's avatar
chirac committed
79
@login_required
chirac's avatar
chirac committed
80
@permission_required('cableur')
chirac's avatar
chirac committed
81 82 83 84 85 86 87 88 89 90 91
def edit_facture(request, factureid):
    try:
        facture = Facture.objects.get(pk=factureid)
    except Facture.DoesNotExist:
        messages.error(request, u"Facture inexistante" )
        return redirect("/cotisations/")
    facture_form = EditFactureForm(request.POST or None, instance=facture)
    if facture_form.is_valid():
        facture_form.save()
        messages.success(request, "La facture a bien été modifiée")
        return redirect("/cotisations/")
92
    return form({'factureform': facture_form}, 'cotisations/facture.html', request)
chirac's avatar
chirac committed
93

chirac's avatar
chirac committed
94
@login_required
95
@permission_required('trésorier')
96 97 98 99 100
def add_article(request):
    article = ArticleForm(request.POST or None)
    if article.is_valid():
        article.save()
        messages.success(request, "L'article a été ajouté")
101 102 103
        return redirect("/cotisations/index_article/")
    return form({'factureform': article}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
104
@login_required
105
@permission_required('trésorier')
106 107 108 109 110 111 112 113 114 115 116
def edit_article(request, articleid):
    try:
        article_instance = Article.objects.get(pk=articleid)
    except Article.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/cotisations/index_article/")
    article = ArticleForm(request.POST or None, instance=article_instance)
    if article.is_valid():
        article.save()
        messages.success(request, "Type d'article modifié")
        return redirect("/cotisations/index_article/")
117 118
    return form({'factureform': article}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
119
@login_required
120
@permission_required('trésorier')
121 122 123 124 125 126
def del_article(request):
    article = DelArticleForm(request.POST or None)
    if article.is_valid():
        article_del = article.cleaned_data['articles']
        article_del.delete()
        messages.success(request, "Le/les articles ont été supprimé")
127
        return redirect("/cotisations/index_article")
128 129
    return form({'factureform': article}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
130
@login_required
131
@permission_required('trésorier')
132 133 134 135 136
def add_paiement(request):
    paiement = PaiementForm(request.POST or None)
    if paiement.is_valid():
        paiement.save()
        messages.success(request, "Le moyen de paiement a été ajouté")
137 138 139
        return redirect("/cotisations/index_paiement/")
    return form({'factureform': paiement}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
140
@login_required
141
@permission_required('trésorier')
142 143 144 145 146 147 148 149 150 151 152
def edit_paiement(request, paiementid):
    try:
        paiement_instance = Paiement.objects.get(pk=paiementid)
    except Paiement.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/cotisations/index_paiement/")
    paiement = PaiementForm(request.POST or None, instance=paiement_instance)
    if paiement.is_valid():
        paiement.save()
        messages.success(request, "Type de paiement modifié")
        return redirect("/cotisations/index_paiement/")
153 154
    return form({'factureform': paiement}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
155
@login_required
156
@permission_required('trésorier')
157 158 159 160 161 162 163 164 165 166
def del_paiement(request):
    paiement = DelPaiementForm(request.POST or None)
    if paiement.is_valid():
        paiement_dels = paiement.cleaned_data['paiements']
        for paiement_del in paiement_dels:
            try:
                paiement_del.delete()
                messages.success(request, "Le moyen de paiement a été supprimé")
            except ProtectedError:
                messages.error(request, "Le moyen de paiement %s est affecté à au moins une facture, vous ne pouvez pas le supprimer" % paiement_del)
167
        return redirect("/cotisations/index_paiement/")
168 169
    return form({'factureform': paiement}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
170
@login_required
chirac's avatar
chirac committed
171
@permission_required('cableur')
chirac's avatar
chirac committed
172 173 174 175 176
def add_banque(request):
    banque = BanqueForm(request.POST or None)
    if banque.is_valid():
        banque.save()
        messages.success(request, "La banque a été ajoutée")
177 178 179
        return redirect("/cotisations/index_banque/")
    return form({'factureform': banque}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
180
@login_required
181
@permission_required('trésorier')
182 183 184 185 186 187 188 189 190 191 192
def edit_banque(request, banqueid):
    try:
        banque_instance = Article.objects.get(pk=banqueid)
    except Banque.DoesNotExist:
        messages.error(request, u"Entrée inexistante" )
        return redirect("/cotisations/index_banque/")
    banque = BanqueForm(request.POST or None, instance=banque_instance)
    if banque.is_valid():
        banque.save()
        messages.success(request, "Banque modifiée")
        return redirect("/cotisations/index_banque/")
chirac's avatar
chirac committed
193 194
    return form({'factureform': banque}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
195
@login_required
196
@permission_required('trésorier')
chirac's avatar
chirac committed
197 198 199 200 201 202 203 204 205 206
def del_banque(request):
    banque = DelBanqueForm(request.POST or None)
    if banque.is_valid():
        banque_dels = banque.cleaned_data['banques']
        for banque_del in banque_dels:
            try:
                banque_del.delete()
                messages.success(request, "La banque a été supprimée")
            except ProtectedError:
                messages.error(request, "La banque %s est affectée à au moins une facture, vous ne pouvez pas la supprimer" % banque_del)
207
        return redirect("/cotisations/index_banque/")
chirac's avatar
chirac committed
208 209
    return form({'factureform': banque}, 'cotisations/facture.html', request)

chirac's avatar
chirac committed
210
@login_required
chirac's avatar
chirac committed
211
@permission_required('cableur')
212 213
def index_article(request):
    article_list = Article.objects.order_by('name')
214
    return render(request, 'cotisations/index_article.html', {'article_list':article_list})
215

chirac's avatar
chirac committed
216
@login_required
chirac's avatar
chirac committed
217
@permission_required('cableur')
218 219
def index_paiement(request):
    paiement_list = Paiement.objects.order_by('moyen')
220
    return render(request, 'cotisations/index_paiement.html', {'paiement_list':paiement_list})
221

chirac's avatar
chirac committed
222
@login_required
chirac's avatar
chirac committed
223
@permission_required('cableur')
224 225
def index_banque(request):
    banque_list = Banque.objects.order_by('name')
226
    return render(request, 'cotisations/index_banque.html', {'banque_list':banque_list})
227

chirac's avatar
chirac committed
228
@login_required
chirac's avatar
chirac committed
229
@permission_required('cableur')
chirac's avatar
chirac committed
230
def index(request):
231
    facture_list = Facture.objects.order_by('date').reverse()
232
    return render(request, 'cotisations/index.html', {'facture_list': facture_list})