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
|
2018-04-14 13:39:51 +00:00
|
|
|
"""cotisations.views
|
|
|
|
The different views used in the Cotisations module
|
|
|
|
"""
|
|
|
|
|
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
|
2018-04-14 13:39:51 +00:00
|
|
|
from django.contrib.auth.decorators import login_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
|
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-03-31 13:43:46 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-04-14 13:39:51 +00:00
|
|
|
|
2017-10-13 20:47:32 +00:00
|
|
|
# Import des models, forms et fonctions re2o
|
2018-04-03 01:47:03 +00:00
|
|
|
from reversion import revisions as reversion
|
2017-10-13 20:47:32 +00:00
|
|
|
from users.models import User
|
|
|
|
from re2o.settings import LOGO_PATH
|
|
|
|
from re2o import settings
|
|
|
|
from re2o.views import form
|
2018-05-29 18:35:29 +00:00
|
|
|
from re2o.utils import SortTable, re2o_paginator, messages_protected_error
|
2017-12-29 00:51:24 +00:00
|
|
|
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,
|
|
|
|
RechargeForm
|
2017-10-28 04:02:53 +00:00
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
from . import payment as online_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
|
|
|
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View called to create a new invoice.
|
|
|
|
Currently, Send the list of available articles for the user along with
|
|
|
|
a formset of a new invoice (based on the `:forms:NewFactureForm()` form.
|
|
|
|
A bit of JS is used in the template to add articles in a fancier way.
|
|
|
|
If everything is correct, save each one of the articles, save the
|
|
|
|
purchase object associated and finally the newly created invoice.
|
|
|
|
|
|
|
|
TODO : The whole verification process should be moved to the model. This
|
|
|
|
function should only act as a dumb interface between the model and the
|
|
|
|
user.
|
|
|
|
"""
|
|
|
|
invoice = Facture(user=user)
|
|
|
|
# The template needs the list of articles (for the JS part)
|
2017-10-28 04:02:53 +00:00
|
|
|
article_list = Article.objects.filter(
|
|
|
|
Q(type_user='All') | Q(type_user=request.user.class_name)
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
# Building the invocie form and the article formset
|
|
|
|
invoice_form = NewFactureForm(request.POST or None, instance=invoice)
|
2017-10-28 04:02:53 +00:00
|
|
|
if request.user.is_class_club:
|
2018-04-13 18:58:29 +00:00
|
|
|
article_formset = formset_factory(SelectClubArticleForm)(
|
|
|
|
request.POST or None
|
|
|
|
)
|
2017-10-28 04:02:53 +00:00
|
|
|
else:
|
2018-04-13 18:58:29 +00:00
|
|
|
article_formset = formset_factory(SelectUserArticleForm)(
|
|
|
|
request.POST or None
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
|
|
|
|
if invoice_form.is_valid() and article_formset.is_valid():
|
|
|
|
new_invoice_instance = invoice_form.save(commit=False)
|
2016-07-14 11:55:46 +00:00
|
|
|
articles = article_formset
|
2018-04-09 17:40:46 +00:00
|
|
|
# Check if at leat one article has been selected
|
2016-07-14 11:55:46 +00:00
|
|
|
if any(art.cleaned_data for art in articles):
|
2018-04-09 17:40:46 +00:00
|
|
|
user_balance = OptionalUser.get_cached_value('user_solde')
|
|
|
|
negative_balance = OptionalUser.get_cached_value('solde_negatif')
|
|
|
|
# If the paiement using balance has been activated,
|
|
|
|
# checking that the total price won't get the user under
|
|
|
|
# the authorized minimum (negative_balance)
|
|
|
|
if user_balance:
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Paiement to Payment
|
2018-04-13 18:58:29 +00:00
|
|
|
if new_invoice_instance.paiement == (
|
2018-04-14 13:39:51 +00:00
|
|
|
Paiement.objects.get_or_create(moyen='solde')[0]
|
2018-04-13 18:58:29 +00:00
|
|
|
):
|
2018-04-09 17:40:46 +00:00
|
|
|
total_price = 0
|
2017-06-26 17:23:01 +00:00
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
2018-04-13 18:58:29 +00:00
|
|
|
total_price += (
|
|
|
|
art_item.cleaned_data['article'].prix *
|
|
|
|
art_item.cleaned_data['quantity']
|
|
|
|
)
|
|
|
|
if (float(user.solde) - float(total_price)
|
|
|
|
< negative_balance):
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_("Your balance is too low for this operation.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
2018-03-31 13:43:46 +00:00
|
|
|
))
|
2018-04-09 17:40:46 +00:00
|
|
|
# Saving the invoice
|
|
|
|
new_invoice_instance.save()
|
|
|
|
|
|
|
|
# Building a purchase for each article sold
|
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']
|
2018-04-09 17:40:46 +00:00
|
|
|
new_purchase = Vente.objects.create(
|
|
|
|
facture=new_invoice_instance,
|
2017-10-13 20:47:32 +00:00
|
|
|
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
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
new_purchase.save()
|
|
|
|
|
|
|
|
# In case a cotisation was bought, inform the user, the
|
|
|
|
# cotisation time has been extended too
|
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,
|
2018-03-31 13:43:46 +00:00
|
|
|
_("The cotisation of %(member_name)s has been \
|
|
|
|
extended to %(end_date)s.") % {
|
2018-04-13 11:40:02 +00:00
|
|
|
'member_name': user.pseudo,
|
|
|
|
'end_date': user.end_adhesion()
|
2018-03-31 13:43:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
# Else, only tell the invoice was created
|
2016-07-14 11:55:46 +00:00
|
|
|
else:
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The invoice has been created.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
2018-03-31 13:43:46 +00:00
|
|
|
))
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.error(
|
2017-10-13 20:47:32 +00:00
|
|
|
request,
|
2018-03-31 13:43:46 +00:00
|
|
|
_("You need to choose at least one article.")
|
|
|
|
)
|
|
|
|
return form(
|
|
|
|
{
|
2018-04-09 17:40:46 +00:00
|
|
|
'factureform': invoice_form,
|
2018-03-31 13:43:46 +00:00
|
|
|
'venteform': article_formset,
|
|
|
|
'articlelist': article_list
|
|
|
|
},
|
|
|
|
'cotisations/new_facture.html', request
|
|
|
|
)
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-04-13 18:58:29 +00:00
|
|
|
View used to generate a custom PDF invoice. It's mainly used to
|
2018-04-09 17:40:46 +00:00
|
|
|
get invoices that are not taken into account, for the administrative
|
|
|
|
point of view.
|
|
|
|
"""
|
2018-04-11 19:53:54 +00:00
|
|
|
# The template needs the list of articles (for the JS part)
|
|
|
|
articles = Article.objects.filter(
|
|
|
|
Q(type_user='All') | Q(type_user=request.user.class_name)
|
|
|
|
)
|
|
|
|
# Building the invocie form and the article formset
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_form = NewFactureFormPdf(request.POST or None)
|
2018-04-11 19:53:54 +00:00
|
|
|
if request.user.is_class_club:
|
2018-04-13 18:58:29 +00:00
|
|
|
articles_formset = formset_factory(SelectClubArticleForm)(
|
|
|
|
request.POST or None
|
|
|
|
)
|
2018-04-11 19:53:54 +00:00
|
|
|
else:
|
2018-04-13 18:58:29 +00:00
|
|
|
articles_formset = formset_factory(SelectUserArticleForm)(
|
|
|
|
request.POST or None
|
|
|
|
)
|
2018-04-11 19:53:54 +00:00
|
|
|
if invoice_form.is_valid() and articles_formset.is_valid():
|
|
|
|
# Get the article list and build an list out of it
|
|
|
|
# contiaining (article_name, article_price, quantity, total_price)
|
|
|
|
articles_info = []
|
|
|
|
for articles_form in articles_formset:
|
|
|
|
if articles_form.cleaned_data:
|
|
|
|
article = articles_form.cleaned_data['article']
|
|
|
|
quantity = articles_form.cleaned_data['quantity']
|
|
|
|
articles_info.append({
|
|
|
|
'name': article.name,
|
|
|
|
'price': article.prix,
|
|
|
|
'quantity': quantity,
|
|
|
|
'total_price': article.prix * quantity
|
|
|
|
})
|
2018-04-11 17:20:40 +00:00
|
|
|
paid = invoice_form.cleaned_data['paid']
|
|
|
|
recipient = invoice_form.cleaned_data['dest']
|
|
|
|
address = invoice_form.cleaned_data['chambre']
|
2018-04-11 19:53:54 +00:00
|
|
|
total_price = sum(a['total_price'] for a in articles_info)
|
|
|
|
|
2017-11-14 19:00:24 +00:00
|
|
|
return render_invoice(request, {
|
2017-10-13 03:08:30 +00:00
|
|
|
'DATE': timezone.now(),
|
2018-04-11 17:20:40 +00:00
|
|
|
'recipient_name': recipient,
|
|
|
|
'address': address,
|
2018-04-11 19:53:54 +00:00
|
|
|
'article': articles_info,
|
2018-04-09 17:40:46 +00:00
|
|
|
'total': total_price,
|
2017-10-13 03:08:30 +00:00
|
|
|
'paid': paid,
|
2018-01-31 02:27:59 +00:00
|
|
|
'asso_name': AssoOption.get_cached_value('name'),
|
|
|
|
'line1': AssoOption.get_cached_value('adresse1'),
|
|
|
|
'line2': AssoOption.get_cached_value('adresse2'),
|
|
|
|
'siret': AssoOption.get_cached_value('siret'),
|
|
|
|
'email': AssoOption.get_cached_value('contact'),
|
|
|
|
'phone': AssoOption.get_cached_value('telephone'),
|
2017-10-13 03:08:30 +00:00
|
|
|
'tpl_path': os.path.join(settings.BASE_DIR, LOGO_PATH)
|
|
|
|
})
|
|
|
|
return form({
|
2018-04-09 17:40:46 +00:00
|
|
|
'factureform': invoice_form,
|
2018-04-11 19:53:54 +00:00
|
|
|
'action_name': _("Create"),
|
|
|
|
'articlesformset': articles_formset,
|
|
|
|
'articles': articles
|
2017-10-13 03:08:30 +00:00
|
|
|
}, 'cotisations/facture.html', request)
|
|
|
|
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
2016-07-11 22:05:07 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view(Facture)
|
2018-04-15 01:00:05 +00:00
|
|
|
def facture_pdf(request, facture, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to generate a PDF file from an existing invoice in database
|
|
|
|
Creates a line for each Purchase (thus article sold) and generate the
|
|
|
|
invoice with the total price, the payment method, the address and the
|
|
|
|
legal information for the user.
|
|
|
|
"""
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change vente to purchase
|
2018-04-09 17:40:46 +00:00
|
|
|
purchases_objects = Vente.objects.all().filter(facture=facture)
|
2018-04-11 19:53:54 +00:00
|
|
|
# Get the article list and build an list out of it
|
|
|
|
# contiaining (article_name, article_price, quantity, total_price)
|
|
|
|
purchases_info = []
|
2018-04-09 17:40:46 +00:00
|
|
|
for purchase in purchases_objects:
|
2018-04-11 19:53:54 +00:00
|
|
|
purchases_info.append({
|
|
|
|
'name': purchase.name,
|
|
|
|
'price': purchase.prix,
|
|
|
|
'quantity': purchase.number,
|
|
|
|
'total_price': purchase.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,
|
2018-04-11 17:20:40 +00:00
|
|
|
'recipient_name': "{} {}".format(
|
|
|
|
facture.user.name,
|
|
|
|
facture.user.surname
|
|
|
|
),
|
2018-04-11 18:40:59 +00:00
|
|
|
'address': facture.user.room,
|
2018-04-11 19:53:54 +00:00
|
|
|
'article': purchases_info,
|
2017-10-13 03:08:30 +00:00
|
|
|
'total': facture.prix_total(),
|
2018-01-31 02:27:59 +00:00
|
|
|
'asso_name': AssoOption.get_cached_value('name'),
|
|
|
|
'line1': AssoOption.get_cached_value('adresse1'),
|
|
|
|
'line2': AssoOption.get_cached_value('adresse2'),
|
|
|
|
'siret': AssoOption.get_cached_value('siret'),
|
|
|
|
'email': AssoOption.get_cached_value('contact'),
|
|
|
|
'phone': AssoOption.get_cached_value('telephone'),
|
2017-10-13 03:08:30 +00:00
|
|
|
'tpl_path': os.path.join(settings.BASE_DIR, LOGO_PATH)
|
|
|
|
})
|
|
|
|
|
2016-07-11 22:05:07 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
2016-07-13 23:54:06 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_edit(Facture)
|
2018-04-15 01:00:05 +00:00
|
|
|
def edit_facture(request, facture, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to edit an existing invoice.
|
|
|
|
Articles can be added or remove to the invoice and quantity
|
|
|
|
can be set as desired. This is also the view used to invalidate
|
|
|
|
an invoice.
|
|
|
|
"""
|
2018-04-13 18:58:29 +00:00
|
|
|
invoice_form = EditFactureForm(
|
|
|
|
request.POST or None,
|
|
|
|
instance=facture,
|
|
|
|
user=request.user
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
purchases_objects = Vente.objects.filter(facture=facture)
|
|
|
|
purchase_form_set = modelformset_factory(
|
2017-10-13 20:47:32 +00:00
|
|
|
Vente,
|
|
|
|
fields=('name', 'number'),
|
|
|
|
extra=0,
|
2018-04-09 17:40:46 +00:00
|
|
|
max_num=len(purchases_objects)
|
2017-10-13 20:47:32 +00:00
|
|
|
)
|
2018-04-13 18:58:29 +00:00
|
|
|
purchase_form = purchase_form_set(
|
|
|
|
request.POST or None,
|
|
|
|
queryset=purchases_objects
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
if invoice_form.is_valid() and purchase_form.is_valid():
|
|
|
|
if invoice_form.changed_data:
|
|
|
|
invoice_form.save()
|
|
|
|
purchase_form.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The invoice has been successfully edited.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index'))
|
2017-10-13 03:08:30 +00:00
|
|
|
return form({
|
2018-04-09 17:40:46 +00:00
|
|
|
'factureform': invoice_form,
|
|
|
|
'venteform': purchase_form
|
2017-10-13 03:08:30 +00:00
|
|
|
}, 'cotisations/edit_facture.html', request)
|
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
2016-07-14 20:29:30 +00:00
|
|
|
@login_required
|
2017-12-09 01:15:11 +00:00
|
|
|
@can_delete(Facture)
|
2018-04-15 01:00:05 +00:00
|
|
|
def del_facture(request, facture, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to delete an existing invocie.
|
|
|
|
"""
|
2016-07-18 16:37:52 +00:00
|
|
|
if request.method == "POST":
|
2018-04-09 17:40:46 +00:00
|
|
|
facture.delete()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
2018-03-31 15:23:34 +00:00
|
|
|
_("The invoice has been successfully deleted.")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
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,
|
2018-04-09 17:40:46 +00:00
|
|
|
'objet_name': _("Invoice")
|
2017-10-13 03:08:30 +00:00
|
|
|
}, 'cotisations/delete.html', request)
|
|
|
|
|
2016-07-14 20:29:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change solde to balance
|
2017-06-26 17:23:01 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_create(Facture)
|
|
|
|
@can_edit(User)
|
2018-04-15 01:00:05 +00:00
|
|
|
def credit_solde(request, user, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to edit the balance of a user.
|
|
|
|
Can be use either to increase or decrease a user's balance.
|
|
|
|
"""
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice = CreditSoldeForm(request.POST or None)
|
|
|
|
if invoice.is_valid():
|
|
|
|
invoice_instance = invoice.save(commit=False)
|
|
|
|
invoice_instance.user = user
|
|
|
|
invoice_instance.save()
|
|
|
|
new_purchase = Vente.objects.create(
|
|
|
|
facture=invoice_instance,
|
2017-10-13 20:47:32 +00:00
|
|
|
name="solde",
|
2018-04-09 17:40:46 +00:00
|
|
|
prix=invoice.cleaned_data['montant'],
|
2017-10-13 20:47:32 +00:00
|
|
|
number=1
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
new_purchase.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
2018-03-31 15:23:34 +00:00
|
|
|
_("Balance successfully updated.")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
2018-04-14 01:43:00 +00:00
|
|
|
'factureform': invoice,
|
2018-04-09 17:40:46 +00:00
|
|
|
'action_name': _("Edit")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2017-06-26 17:23:01 +00:00
|
|
|
|
|
|
|
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to add an article.
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
.. note:: If a purchase has already been sold, the price are calculated
|
|
|
|
once and for all. That means even if the price of an article is edited
|
|
|
|
later, it won't change the invoice. That is really important to keep
|
|
|
|
this behaviour in order not to modify all the past and already
|
|
|
|
accepted invoices.
|
|
|
|
"""
|
2016-07-06 18:57:31 +00:00
|
|
|
article = ArticleForm(request.POST or None)
|
|
|
|
if article.is_valid():
|
2018-03-31 15:18:39 +00:00
|
|
|
article.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The article has been successfully created.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-article'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': article,
|
|
|
|
'action_name': _("Add")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-08 01:27:02 +00:00
|
|
|
|
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)
|
2018-04-15 01:00:05 +00:00
|
|
|
def edit_article(request, article_instance, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to edit an article.
|
|
|
|
"""
|
2016-07-08 01:27:02 +00:00
|
|
|
article = ArticleForm(request.POST or None, instance=article_instance)
|
|
|
|
if article.is_valid():
|
2018-03-31 22:06:44 +00:00
|
|
|
if article.changed_data:
|
|
|
|
article.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The article has been successfully edited.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-article'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': article,
|
|
|
|
'action_name': _('Edit')
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-06 18:57:31 +00:00
|
|
|
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to delete one of the articles.
|
|
|
|
"""
|
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']
|
2018-03-31 15:18:39 +00:00
|
|
|
article_del.delete()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The article(s) have been successfully deleted.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-article'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': article,
|
|
|
|
'action_name': _("Delete")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-06 18:57:31 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change paiement to payment
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to add a payment method.
|
|
|
|
"""
|
|
|
|
payment = PaiementForm(request.POST or None)
|
|
|
|
if payment.is_valid():
|
|
|
|
payment.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The payment method has been successfully created.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-paiement'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': payment,
|
|
|
|
'action_name': _("Add")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : chnage paiement to Payment
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_edit(Paiement)
|
2018-04-15 13:34:51 +00:00
|
|
|
def edit_paiement(request, paiement_instance, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to edit a payment method.
|
|
|
|
"""
|
|
|
|
payment = PaiementForm(request.POST or None, instance=paiement_instance)
|
|
|
|
if payment.is_valid():
|
|
|
|
if payment.changed_data:
|
|
|
|
payment.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The payement method has been successfully edited.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-paiement'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': payment,
|
|
|
|
'action_name': _("Edit")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-06 19:43:39 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change paiement to payment
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to delete a set of payment methods.
|
|
|
|
"""
|
|
|
|
payment = DelPaiementForm(request.POST or None, instances=instances)
|
|
|
|
if payment.is_valid():
|
|
|
|
payment_dels = payment.cleaned_data['paiements']
|
|
|
|
for payment_del in payment_dels:
|
2016-07-06 19:43:39 +00:00
|
|
|
try:
|
2018-04-09 17:40:46 +00:00
|
|
|
payment_del.delete()
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.success(
|
2017-10-13 20:47:32 +00:00
|
|
|
request,
|
2018-03-31 13:43:46 +00:00
|
|
|
_("The payment method %(method_name)s has been \
|
2018-03-31 15:23:34 +00:00
|
|
|
successfully deleted.") % {
|
2018-04-13 11:40:02 +00:00
|
|
|
'method_name': payment_del
|
2018-03-31 13:43:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-05-29 18:35:29 +00:00
|
|
|
except ProtectedError as err:
|
|
|
|
messages_protected_error(messages, request, payment_del, err)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-paiement'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': payment,
|
|
|
|
'action_name': _("Delete")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-06 19:43:39 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to add a bank.
|
|
|
|
"""
|
|
|
|
bank = BanqueForm(request.POST or None)
|
|
|
|
if bank.is_valid():
|
|
|
|
bank.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The bank has been successfully created.")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-banque'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': bank,
|
|
|
|
'action_name': _("Add")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-08 22:59:20 +00:00
|
|
|
@can_edit(Banque)
|
2018-04-15 01:00:05 +00:00
|
|
|
def edit_banque(request, banque_instance, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to edit a bank.
|
|
|
|
"""
|
|
|
|
bank = BanqueForm(request.POST or None, instance=banque_instance)
|
|
|
|
if bank.is_valid():
|
|
|
|
if bank.changed_data:
|
|
|
|
bank.save()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The bank has been successfully edited")
|
|
|
|
)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-banque'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': bank,
|
|
|
|
'action_name': _("Edit")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-06 20:20:49 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : chnage banque to bank
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to delete a set of banks.
|
|
|
|
"""
|
|
|
|
bank = DelBanqueForm(request.POST or None, instances=instances)
|
|
|
|
if bank.is_valid():
|
|
|
|
bank_dels = bank.cleaned_data['banques']
|
|
|
|
for bank_del in bank_dels:
|
2016-07-06 20:20:49 +00:00
|
|
|
try:
|
2018-04-09 17:40:46 +00:00
|
|
|
bank_del.delete()
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The bank %(bank_name)s has been successfully \
|
|
|
|
deleted.") % {
|
2018-04-13 11:40:02 +00:00
|
|
|
'bank_name': bank_del
|
2018-03-31 13:43:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-05-29 18:35:29 +00:00
|
|
|
except ProtectedError as err:
|
|
|
|
messages_protected_error(messages, request, bank_del, err)
|
2017-10-26 22:11:18 +00:00
|
|
|
return redirect(reverse('cotisations:index-banque'))
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'factureform': bank,
|
|
|
|
'action_name': _("Delete")
|
|
|
|
}, 'cotisations/facture.html', request)
|
2016-07-06 20:20:49 +00:00
|
|
|
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to control the invoices all at once.
|
|
|
|
"""
|
2018-01-30 22:07:43 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value('pagination_number')
|
2018-04-13 18:58:29 +00:00
|
|
|
invoice_list = (Facture.objects.select_related('user').
|
|
|
|
select_related('paiement'))
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_list = SortTable.sort(
|
|
|
|
invoice_list,
|
2017-10-22 00:33:44 +00:00
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.COTISATIONS_CONTROL
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
control_invoices_formset = modelformset_factory(
|
2017-10-13 20:47:32 +00:00
|
|
|
Facture,
|
|
|
|
fields=('control', 'valid'),
|
|
|
|
extra=0
|
2018-04-09 17:40:46 +00:00
|
|
|
)
|
|
|
|
invoice_list = re2o_paginator(request, invoice_list, pagination_number)
|
|
|
|
control_invoices_form = control_invoices_formset(
|
|
|
|
request.POST or None,
|
|
|
|
queryset=invoice_list.object_list
|
|
|
|
)
|
|
|
|
if control_invoices_form.is_valid():
|
|
|
|
control_invoices_form.save()
|
2018-04-03 01:47:03 +00:00
|
|
|
reversion.set_comment("Controle")
|
2018-05-10 20:28:27 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("Your changes have been properly taken into account.")
|
|
|
|
)
|
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', {
|
2018-04-09 17:40:46 +00:00
|
|
|
'facture_list': invoice_list,
|
|
|
|
'controlform': control_invoices_form
|
2017-10-13 03:08:30 +00:00
|
|
|
})
|
|
|
|
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to display the list of all available articles.
|
|
|
|
"""
|
|
|
|
# TODO : Offer other means of sorting
|
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
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change paiement to payment
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to display the list of all available payment methods.
|
|
|
|
"""
|
|
|
|
payment_list = Paiement.objects.order_by('moyen')
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/index_paiement.html', {
|
2018-04-09 17:40:46 +00:00
|
|
|
'paiement_list': payment_list
|
2017-10-13 03:08:30 +00:00
|
|
|
})
|
|
|
|
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to display the list of all available banks.
|
|
|
|
"""
|
|
|
|
bank_list = Banque.objects.order_by('name')
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/index_banque.html', {
|
2018-04-09 17:40:46 +00:00
|
|
|
'banque_list': bank_list
|
2017-10-13 03:08:30 +00:00
|
|
|
})
|
|
|
|
|
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):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to display the list of all exisitng invoices.
|
|
|
|
"""
|
2018-01-30 22:07:43 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value('pagination_number')
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_list = Facture.objects.select_related('user')\
|
2017-10-22 00:33:44 +00:00
|
|
|
.select_related('paiement').prefetch_related('vente_set')
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_list = SortTable.sort(
|
|
|
|
invoice_list,
|
2017-10-22 00:33:44 +00:00
|
|
|
request.GET.get('col'),
|
|
|
|
request.GET.get('order'),
|
|
|
|
SortTable.COTISATIONS_INDEX
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_list = re2o_paginator(request, invoice_list, pagination_number)
|
2017-10-13 03:08:30 +00:00
|
|
|
return render(request, 'cotisations/index.html', {
|
2018-04-09 17:40:46 +00:00
|
|
|
'facture_list': invoice_list
|
2017-10-13 03:08:30 +00:00
|
|
|
})
|
2018-01-11 18:25:41 +00:00
|
|
|
|
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
# TODO : merge this function with new_facture() which is nearly the same
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
2018-01-11 18:25:41 +00:00
|
|
|
@login_required
|
|
|
|
def new_facture_solde(request, userid):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View called to create a new invoice when using the balance to pay.
|
|
|
|
Currently, send the list of available articles for the user along with
|
|
|
|
a formset of a new invoice (based on the `:forms:NewFactureForm()` form.
|
|
|
|
A bit of JS is used in the template to add articles in a fancier way.
|
|
|
|
If everything is correct, save each one of the articles, save the
|
|
|
|
purchase object associated and finally the newly created invoice.
|
|
|
|
|
|
|
|
TODO : The whole verification process should be moved to the model. This
|
|
|
|
function should only act as a dumb interface between the model and the
|
|
|
|
user.
|
|
|
|
"""
|
2018-01-11 18:25:41 +00:00
|
|
|
user = request.user
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice = Facture(user=user)
|
|
|
|
payment, _created = Paiement.objects.get_or_create(moyen='Solde')
|
2018-04-13 12:32:39 +00:00
|
|
|
invoice.paiement = payment
|
2018-04-09 17:40:46 +00:00
|
|
|
# The template needs the list of articles (for the JS part)
|
2018-01-11 18:25:41 +00:00
|
|
|
article_list = Article.objects.filter(
|
|
|
|
Q(type_user='All') | Q(type_user=request.user.class_name)
|
|
|
|
)
|
|
|
|
if request.user.is_class_club:
|
2018-04-13 18:58:29 +00:00
|
|
|
article_formset = formset_factory(SelectClubArticleForm)(
|
|
|
|
request.POST or None
|
|
|
|
)
|
2018-01-11 18:25:41 +00:00
|
|
|
else:
|
2018-04-13 18:58:29 +00:00
|
|
|
article_formset = formset_factory(SelectUserArticleForm)(
|
|
|
|
request.POST or None
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
|
2018-01-11 18:25:41 +00:00
|
|
|
if article_formset.is_valid():
|
|
|
|
articles = article_formset
|
2018-04-09 17:40:46 +00:00
|
|
|
# Check if at leat one article has been selected
|
2018-01-11 18:25:41 +00:00
|
|
|
if any(art.cleaned_data for art in articles):
|
2018-04-09 17:40:46 +00:00
|
|
|
user_balance = OptionalUser.get_cached_value('user_solde')
|
|
|
|
negative_balance = OptionalUser.get_cached_value('solde_negatif')
|
|
|
|
# If the paiement using balance has been activated,
|
|
|
|
# checking that the total price won't get the user under
|
|
|
|
# the authorized minimum (negative_balance)
|
|
|
|
if user_balance:
|
|
|
|
total_price = 0
|
2018-01-11 18:25:41 +00:00
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
2018-04-09 17:40:46 +00:00
|
|
|
total_price += art_item.cleaned_data['article']\
|
2018-01-11 18:25:41 +00:00
|
|
|
.prix*art_item.cleaned_data['quantity']
|
2018-04-09 17:40:46 +00:00
|
|
|
if float(user.solde) - float(total_price) < negative_balance:
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_("The balance is too low for this operation.")
|
|
|
|
)
|
2018-01-11 18:25:41 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
2018-03-31 13:43:46 +00:00
|
|
|
))
|
2018-04-09 17:40:46 +00:00
|
|
|
# Saving the invoice
|
|
|
|
invoice.save()
|
|
|
|
|
|
|
|
# Building a purchase for each article sold
|
2018-01-11 18:25:41 +00:00
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
|
|
|
article = art_item.cleaned_data['article']
|
|
|
|
quantity = art_item.cleaned_data['quantity']
|
2018-04-09 17:40:46 +00:00
|
|
|
new_purchase = Vente.objects.create(
|
|
|
|
facture=invoice,
|
2018-01-11 18:25:41 +00:00
|
|
|
name=article.name,
|
|
|
|
prix=article.prix,
|
|
|
|
type_cotisation=article.type_cotisation,
|
|
|
|
duration=article.duration,
|
|
|
|
number=quantity
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
new_purchase.save()
|
|
|
|
|
|
|
|
# In case a cotisation was bought, inform the user, the
|
|
|
|
# cotisation time has been extended too
|
2018-01-11 18:25:41 +00:00
|
|
|
if any(art_item.cleaned_data['article'].type_cotisation
|
|
|
|
for art_item in articles if art_item.cleaned_data):
|
|
|
|
messages.success(
|
|
|
|
request,
|
2018-03-31 15:23:34 +00:00
|
|
|
_("The cotisation of %(member_name)s has been successfully \
|
|
|
|
extended to %(end_date)s.") % {
|
2018-04-13 11:40:02 +00:00
|
|
|
'member_name': user.pseudo,
|
|
|
|
'end_date': user.end_adhesion()
|
2018-03-31 13:43:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
# Else, only tell the invoice was created
|
2018-01-11 18:25:41 +00:00
|
|
|
else:
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The invoice has been successuflly created.")
|
|
|
|
)
|
2018-01-11 18:25:41 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
2018-03-31 13:43:46 +00:00
|
|
|
))
|
2018-01-11 18:25:41 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
2018-03-31 13:43:46 +00:00
|
|
|
_("You need to choose at least one article.")
|
|
|
|
)
|
2018-01-11 18:25:41 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': userid}
|
|
|
|
))
|
|
|
|
|
|
|
|
return form({
|
|
|
|
'venteform': article_formset,
|
|
|
|
'articlelist': article_list
|
|
|
|
}, 'cotisations/new_facture_solde.html', request)
|
|
|
|
|
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
# TODO : change recharge to refill
|
2018-01-12 00:07:25 +00:00
|
|
|
@login_required
|
|
|
|
def recharge(request):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
View used to refill the balance by using online payment.
|
|
|
|
"""
|
2018-01-31 02:27:59 +00:00
|
|
|
if AssoOption.get_cached_value('payment') == 'NONE':
|
2018-01-27 12:52:36 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
2018-03-31 13:43:46 +00:00
|
|
|
_("Online payment is disabled.")
|
2018-01-27 12:52:36 +00:00
|
|
|
)
|
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': request.user.id}
|
|
|
|
))
|
2018-04-09 17:40:46 +00:00
|
|
|
refill_form = RechargeForm(request.POST or None, user=request.user)
|
|
|
|
if refill_form.is_valid():
|
|
|
|
invoice = Facture(user=request.user)
|
2018-04-13 18:58:29 +00:00
|
|
|
payment, _created = Paiement.objects.get_or_create(
|
|
|
|
moyen='Rechargement en ligne'
|
|
|
|
)
|
2018-04-13 12:32:39 +00:00
|
|
|
invoice.paiement = payment
|
|
|
|
invoice.valid = False
|
|
|
|
invoice.save()
|
2018-04-09 17:40:46 +00:00
|
|
|
purchase = Vente.objects.create(
|
|
|
|
facture=invoice,
|
2018-01-12 00:07:25 +00:00
|
|
|
name='solde',
|
2018-04-09 17:40:46 +00:00
|
|
|
prix=refill_form.cleaned_data['value'],
|
|
|
|
number=1
|
2018-01-12 00:07:25 +00:00
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
purchase.save()
|
2018-04-13 18:58:29 +00:00
|
|
|
content = online_payment.PAYMENT_SYSTEM[
|
|
|
|
AssoOption.get_cached_value('payment')
|
|
|
|
](invoice, request)
|
2018-01-12 00:07:25 +00:00
|
|
|
return render(request, 'cotisations/payment.html', content)
|
2018-04-09 17:40:46 +00:00
|
|
|
return form({
|
|
|
|
'rechargeform': refill_form
|
|
|
|
}, 'cotisations/recharge.html', request)
|