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
|
2019-09-29 14:02:28 +00:00
|
|
|
# Copyright © 2017 Lara Kermarec
|
2017-01-15 23:01:18 +00:00
|
|
|
# Copyright © 2017 Augustin Lemesle
|
2018-07-05 13:48:07 +00:00
|
|
|
# Copyright © 2018 Hugo Levy-Falk
|
2017-01-15 23:01:18 +00:00
|
|
|
#
|
|
|
|
# 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
|
2019-09-29 14:02:28 +00:00
|
|
|
# Lara Kermarec, Gabriel Détraz
|
2016-07-02 13:58:50 +00:00
|
|
|
# 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
|
2018-07-13 15:52:49 +00:00
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
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
|
2019-11-04 16:55:03 +00:00
|
|
|
from re2o.base import SortTable, re2o_paginator
|
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
|
|
|
)
|
2019-09-23 20:49:59 +00:00
|
|
|
from preferences.models import AssoOption, GeneralOption, Mandate
|
2018-07-21 22:16:05 +00:00
|
|
|
from .models import (
|
|
|
|
Facture,
|
|
|
|
Article,
|
|
|
|
Vente,
|
|
|
|
Paiement,
|
|
|
|
Banque,
|
|
|
|
CustomInvoice,
|
2018-12-31 22:58:37 +00:00
|
|
|
BaseInvoice,
|
2019-01-05 18:45:21 +00:00
|
|
|
CostEstimate,
|
2018-07-21 22:16:05 +00:00
|
|
|
)
|
2017-10-28 04:02:53 +00:00
|
|
|
from .forms import (
|
2018-07-10 16:38:05 +00:00
|
|
|
FactureForm,
|
2017-10-28 04:02:53 +00:00
|
|
|
ArticleForm,
|
|
|
|
DelArticleForm,
|
|
|
|
PaiementForm,
|
|
|
|
DelPaiementForm,
|
|
|
|
BanqueForm,
|
|
|
|
DelBanqueForm,
|
2018-08-26 15:13:29 +00:00
|
|
|
SelectArticleForm,
|
2018-07-21 22:16:05 +00:00
|
|
|
RechargeForm,
|
2018-12-29 13:01:22 +00:00
|
|
|
CustomInvoiceForm,
|
2018-12-31 22:58:37 +00:00
|
|
|
DiscountForm,
|
|
|
|
CostEstimateForm,
|
2017-10-28 04:02:53 +00:00
|
|
|
)
|
2019-01-10 23:39:16 +00:00
|
|
|
from .tex import render_invoice, render_voucher, escape_chars
|
2018-07-02 19:13:13 +00:00
|
|
|
from .payment_methods.forms import payment_method_factory
|
2018-08-31 13:53:45 +00:00
|
|
|
from .utils import find_payment_method
|
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
|
2018-07-10 16:38:05 +00:00
|
|
|
a formset of a new invoice (based on the `:forms:FactureForm()` form.
|
2018-04-09 17:40:46 +00:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
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(
|
2019-12-27 16:00:20 +00:00
|
|
|
Q(type_user="All") | Q(type_user=user.class_type)
|
2017-10-28 04:02:53 +00:00
|
|
|
)
|
2018-07-03 16:53:44 +00:00
|
|
|
# Building the invoice form and the article formset
|
2018-07-10 16:38:05 +00:00
|
|
|
invoice_form = FactureForm(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, instance=invoice, user=request.user, creation=True
|
2018-07-03 16:53:44 +00:00
|
|
|
)
|
2018-07-02 12:09:35 +00:00
|
|
|
|
2018-08-26 15:13:29 +00:00
|
|
|
article_formset = formset_factory(SelectArticleForm)(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, form_kwargs={"user": request.user, "target_user": user}
|
2018-08-26 15:13:29 +00:00
|
|
|
)
|
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
|
2019-09-30 09:33:01 +00:00
|
|
|
# Check if at least 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
|
|
|
# Building a purchase for each article sold
|
2018-07-13 18:19:57 +00:00
|
|
|
purchases = []
|
2018-07-13 20:03:22 +00:00
|
|
|
total_price = 0
|
2016-07-14 11:55:46 +00:00
|
|
|
for art_item in articles:
|
|
|
|
if art_item.cleaned_data:
|
2019-11-04 16:55:03 +00:00
|
|
|
article = art_item.cleaned_data["article"]
|
|
|
|
quantity = art_item.cleaned_data["quantity"]
|
|
|
|
total_price += article.prix * quantity
|
2018-07-13 20:03:22 +00:00
|
|
|
new_purchase = Vente(
|
2018-04-09 17:40:46 +00:00
|
|
|
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,
|
2019-10-02 21:42:54 +00:00
|
|
|
duration_days=article.duration_days,
|
2019-11-04 16:55:03 +00:00
|
|
|
number=quantity,
|
2017-10-13 20:47:32 +00:00
|
|
|
)
|
2018-07-13 18:19:57 +00:00
|
|
|
purchases.append(new_purchase)
|
|
|
|
p = find_payment_method(new_invoice_instance.paiement)
|
2019-11-04 16:55:03 +00:00
|
|
|
if hasattr(p, "check_price"):
|
2018-07-13 20:03:22 +00:00
|
|
|
price_ok, msg = p.check_price(total_price, user)
|
|
|
|
invoice_form.add_error(None, msg)
|
2018-07-13 18:19:57 +00:00
|
|
|
else:
|
2018-07-13 20:03:22 +00:00
|
|
|
price_ok = True
|
|
|
|
if price_ok:
|
|
|
|
new_invoice_instance.save()
|
2019-12-15 15:23:53 +00:00
|
|
|
# Saving purchases so the invoice can find them. Invoice
|
|
|
|
# will modify them after being validated to put the right dates.
|
2018-07-13 20:03:22 +00:00
|
|
|
for p in purchases:
|
|
|
|
p.facture = new_invoice_instance
|
|
|
|
p.save()
|
|
|
|
|
2018-07-13 18:19:57 +00:00
|
|
|
return new_invoice_instance.paiement.end_payment(
|
2019-11-04 16:55:03 +00:00
|
|
|
new_invoice_instance, request
|
2018-07-13 18:19:57 +00:00
|
|
|
)
|
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.error(request, _("You need to choose at least one article."))
|
2018-07-10 16:06:58 +00:00
|
|
|
p = Paiement.objects.filter(is_balance=True)
|
|
|
|
if len(p) and p[0].can_use_payment(request.user):
|
|
|
|
balance = user.solde
|
|
|
|
else:
|
|
|
|
balance = None
|
2018-07-18 00:08:03 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
return form(
|
|
|
|
{
|
2019-11-04 16:55:03 +00:00
|
|
|
"factureform": invoice_form,
|
|
|
|
"articlesformset": article_formset,
|
|
|
|
"articlelist": article_list,
|
|
|
|
"balance": balance,
|
|
|
|
"action_name": _("Confirm"),
|
2019-11-16 14:02:32 +00:00
|
|
|
"title": _("New invoice"),
|
2018-03-31 13:43:46 +00:00
|
|
|
},
|
2019-11-04 16:55:03 +00:00
|
|
|
"cotisations/facture.html",
|
|
|
|
request,
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2018-12-31 22:58:37 +00:00
|
|
|
@login_required
|
|
|
|
@can_create(CostEstimate)
|
|
|
|
def new_cost_estimate(request):
|
|
|
|
"""
|
|
|
|
View used to generate a custom invoice. It's mainly used to
|
|
|
|
get invoices that are not taken into account, for the administrative
|
|
|
|
point of view.
|
|
|
|
"""
|
|
|
|
# The template needs the list of articles (for the JS part)
|
2019-12-27 16:00:20 +00:00
|
|
|
articles = Article.objects.all()
|
2020-05-30 09:00:39 +00:00
|
|
|
# Building the invoice form and the article formset
|
2018-12-31 22:58:37 +00:00
|
|
|
cost_estimate_form = CostEstimateForm(request.POST or None)
|
|
|
|
|
|
|
|
articles_formset = formset_factory(SelectArticleForm)(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, form_kwargs={"user": request.user}
|
2018-12-31 22:58:37 +00:00
|
|
|
)
|
|
|
|
discount_form = DiscountForm(request.POST or None)
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
if (
|
|
|
|
cost_estimate_form.is_valid()
|
|
|
|
and articles_formset.is_valid()
|
|
|
|
and discount_form.is_valid()
|
|
|
|
):
|
2018-12-31 22:58:37 +00:00
|
|
|
cost_estimate_instance = cost_estimate_form.save()
|
|
|
|
for art_item in articles_formset:
|
|
|
|
if art_item.cleaned_data:
|
2019-11-04 16:55:03 +00:00
|
|
|
article = art_item.cleaned_data["article"]
|
|
|
|
quantity = art_item.cleaned_data["quantity"]
|
2018-12-31 22:58:37 +00:00
|
|
|
Vente.objects.create(
|
|
|
|
facture=cost_estimate_instance,
|
|
|
|
name=article.name,
|
|
|
|
prix=article.prix,
|
|
|
|
type_cotisation=article.type_cotisation,
|
|
|
|
duration=article.duration,
|
2019-11-04 16:55:03 +00:00
|
|
|
number=quantity,
|
2018-12-31 22:58:37 +00:00
|
|
|
)
|
|
|
|
discount_form.apply_to_invoice(cost_estimate_instance)
|
2019-01-10 23:39:16 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The cost estimate was created."))
|
|
|
|
return redirect(reverse("cotisations:index-cost-estimate"))
|
2018-12-31 22:58:37 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": cost_estimate_form,
|
|
|
|
"action_name": _("Confirm"),
|
|
|
|
"articlesformset": articles_formset,
|
|
|
|
"articlelist": articles,
|
|
|
|
"discount_form": discount_form,
|
2019-11-16 14:02:32 +00:00
|
|
|
"title": _("New cost estimate"),
|
2019-11-04 16:55:03 +00:00
|
|
|
},
|
|
|
|
"cotisations/facture.html",
|
|
|
|
request,
|
|
|
|
)
|
2018-12-31 22:58:37 +00:00
|
|
|
|
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2018-07-21 22:16:05 +00:00
|
|
|
@can_create(CustomInvoice)
|
|
|
|
def new_custom_invoice(request):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-07-21 22:16:05 +00:00
|
|
|
View used to generate a custom 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)
|
2019-12-27 16:00:20 +00:00
|
|
|
articles = Article.objects.all()
|
2020-05-30 09:00:39 +00:00
|
|
|
# Building the invoice form and the article formset
|
2018-07-21 22:16:05 +00:00
|
|
|
invoice_form = CustomInvoiceForm(request.POST or None)
|
2018-08-26 15:13:29 +00:00
|
|
|
|
2018-09-01 10:26:15 +00:00
|
|
|
articles_formset = formset_factory(SelectArticleForm)(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, form_kwargs={"user": request.user}
|
2018-08-26 15:13:29 +00:00
|
|
|
)
|
2018-12-29 13:01:22 +00:00
|
|
|
discount_form = DiscountForm(request.POST or None)
|
2018-08-26 15:13:29 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
if (
|
|
|
|
invoice_form.is_valid()
|
|
|
|
and articles_formset.is_valid()
|
|
|
|
and discount_form.is_valid()
|
|
|
|
):
|
2018-07-21 22:16:05 +00:00
|
|
|
new_invoice_instance = invoice_form.save()
|
|
|
|
for art_item in articles_formset:
|
|
|
|
if art_item.cleaned_data:
|
2019-11-04 16:55:03 +00:00
|
|
|
article = art_item.cleaned_data["article"]
|
|
|
|
quantity = art_item.cleaned_data["quantity"]
|
2018-07-21 22:16:05 +00:00
|
|
|
Vente.objects.create(
|
|
|
|
facture=new_invoice_instance,
|
|
|
|
name=article.name,
|
|
|
|
prix=article.prix,
|
|
|
|
type_cotisation=article.type_cotisation,
|
|
|
|
duration=article.duration,
|
2019-11-04 16:55:03 +00:00
|
|
|
number=quantity,
|
2018-07-21 22:16:05 +00:00
|
|
|
)
|
2018-12-29 13:01:22 +00:00
|
|
|
discount_form.apply_to_invoice(new_invoice_instance)
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The custom invoice was created."))
|
|
|
|
return redirect(reverse("cotisations:index-custom-invoice"))
|
2018-07-21 22:16:05 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": invoice_form,
|
|
|
|
"action_name": _("Confirm"),
|
|
|
|
"articlesformset": articles_formset,
|
|
|
|
"articlelist": articles,
|
|
|
|
"discount_form": discount_form,
|
2019-11-16 14:02:32 +00:00
|
|
|
"title": _("New custom invoice"),
|
2019-11-04 16:55:03 +00:00
|
|
|
},
|
|
|
|
"cotisations/facture.html",
|
|
|
|
request,
|
|
|
|
)
|
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 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:
|
2019-11-04 16:55:03 +00:00
|
|
|
purchases_info.append(
|
|
|
|
{
|
|
|
|
"name": purchase.name,
|
|
|
|
"price": purchase.prix,
|
|
|
|
"quantity": purchase.number,
|
|
|
|
"total_price": purchase.prix_total,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return render_invoice(
|
|
|
|
request,
|
|
|
|
{
|
|
|
|
"paid": True,
|
|
|
|
"fid": facture.id,
|
|
|
|
"DATE": facture.date,
|
|
|
|
"recipient_name": "{} {}".format(facture.user.name, facture.user.surname),
|
|
|
|
"address": facture.user.room,
|
|
|
|
"article": purchases_info,
|
|
|
|
"total": facture.prix_total(),
|
|
|
|
"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"),
|
|
|
|
"tpl_path": os.path.join(settings.BASE_DIR, LOGO_PATH),
|
|
|
|
"payment_method": facture.paiement.moyen,
|
|
|
|
},
|
|
|
|
)
|
2017-10-13 03:08:30 +00:00
|
|
|
|
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.
|
2018-07-21 22:16:05 +00:00
|
|
|
Articles can be added or removed to the invoice and quantity
|
2018-04-09 17:40:46 +00:00
|
|
|
can be set as desired. This is also the view used to invalidate
|
|
|
|
an invoice.
|
|
|
|
"""
|
2018-07-10 16:38:05 +00:00
|
|
|
invoice_form = FactureForm(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, instance=facture, user=request.user
|
2018-04-13 18:58:29 +00:00
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
purchases_objects = Vente.objects.filter(facture=facture)
|
|
|
|
purchase_form_set = modelformset_factory(
|
2019-11-04 16:55:03 +00:00
|
|
|
Vente, fields=("name", "number"), extra=0, max_num=len(purchases_objects)
|
2018-04-13 18:58:29 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +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()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The invoice was edited."))
|
|
|
|
return redirect(reverse("cotisations:index"))
|
|
|
|
return form(
|
|
|
|
{"factureform": invoice_form, "venteform": purchase_form},
|
|
|
|
"cotisations/edit_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-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
|
|
|
"""
|
2020-05-30 09:00:39 +00:00
|
|
|
View used to delete an existing invoice.
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2016-07-18 16:37:52 +00:00
|
|
|
if request.method == "POST":
|
2018-04-09 17:40:46 +00:00
|
|
|
facture.delete()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The invoice was deleted."))
|
|
|
|
return redirect(reverse("cotisations:index"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{"objet": facture, "objet_name": _("invoice")},
|
2019-11-04 16:55:03 +00:00
|
|
|
"cotisations/delete.html",
|
|
|
|
request,
|
|
|
|
)
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-14 20:29:30 +00:00
|
|
|
|
2018-12-31 22:58:37 +00:00
|
|
|
@login_required
|
|
|
|
@can_edit(CostEstimate)
|
|
|
|
def edit_cost_estimate(request, invoice, **kwargs):
|
2020-05-30 09:00:39 +00:00
|
|
|
# Building the invoice form and the article formset
|
2019-11-04 16:55:03 +00:00
|
|
|
invoice_form = CostEstimateForm(request.POST or None, instance=invoice)
|
2018-12-31 22:58:37 +00:00
|
|
|
purchases_objects = Vente.objects.filter(facture=invoice)
|
|
|
|
purchase_form_set = modelformset_factory(
|
2019-11-04 16:55:03 +00:00
|
|
|
Vente, fields=("name", "number"), extra=0, max_num=len(purchases_objects)
|
2018-12-31 22:58:37 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
purchase_form = purchase_form_set(request.POST or None, queryset=purchases_objects)
|
2018-12-31 22:58:37 +00:00
|
|
|
if invoice_form.is_valid() and purchase_form.is_valid():
|
|
|
|
if invoice_form.changed_data:
|
|
|
|
invoice_form.save()
|
|
|
|
purchase_form.save()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The cost estimate was edited."))
|
|
|
|
return redirect(reverse("cotisations:index-cost-estimate"))
|
2018-12-31 22:58:37 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": invoice_form,
|
|
|
|
"venteform": purchase_form,
|
|
|
|
"title": _("Edit cost estimate"),
|
|
|
|
},
|
|
|
|
"cotisations/edit_facture.html",
|
|
|
|
request,
|
|
|
|
)
|
2018-12-31 22:58:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@can_edit(CostEstimate)
|
|
|
|
@can_create(CustomInvoice)
|
|
|
|
def cost_estimate_to_invoice(request, cost_estimate, **_kwargs):
|
2020-05-30 09:00:39 +00:00
|
|
|
"""Create a custom invoice from a cost estimate."""
|
2018-12-31 22:58:37 +00:00
|
|
|
cost_estimate.create_invoice()
|
|
|
|
messages.success(
|
2019-11-04 16:55:03 +00:00
|
|
|
request, _("An invoice was successfully created from your cost estimate.")
|
2018-12-31 22:58:37 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
return redirect(reverse("cotisations:index-custom-invoice"))
|
2018-12-31 22:58:37 +00:00
|
|
|
|
|
|
|
|
2018-07-21 22:16:05 +00:00
|
|
|
@login_required
|
|
|
|
@can_edit(CustomInvoice)
|
|
|
|
def edit_custom_invoice(request, invoice, **kwargs):
|
2020-05-30 09:00:39 +00:00
|
|
|
# Building the invoice form and the article formset
|
2019-11-04 16:55:03 +00:00
|
|
|
invoice_form = CustomInvoiceForm(request.POST or None, instance=invoice)
|
2018-07-21 22:16:05 +00:00
|
|
|
purchases_objects = Vente.objects.filter(facture=invoice)
|
|
|
|
purchase_form_set = modelformset_factory(
|
2019-11-04 16:55:03 +00:00
|
|
|
Vente, fields=("name", "number"), extra=0, max_num=len(purchases_objects)
|
2018-07-21 22:16:05 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
purchase_form = purchase_form_set(request.POST or None, queryset=purchases_objects)
|
2018-07-21 22:16:05 +00:00
|
|
|
if invoice_form.is_valid() and purchase_form.is_valid():
|
|
|
|
if invoice_form.changed_data:
|
|
|
|
invoice_form.save()
|
|
|
|
purchase_form.save()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The invoice was edited."))
|
|
|
|
return redirect(reverse("cotisations:index-custom-invoice"))
|
2018-07-21 22:16:05 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{
|
|
|
|
"factureform": invoice_form,
|
|
|
|
"venteform": purchase_form,
|
|
|
|
"title": _("Edit custom invoice"),
|
|
|
|
},
|
2019-11-04 16:55:03 +00:00
|
|
|
"cotisations/edit_facture.html",
|
|
|
|
request,
|
|
|
|
)
|
2018-07-21 22:16:05 +00:00
|
|
|
|
|
|
|
|
2018-12-31 22:58:37 +00:00
|
|
|
@login_required
|
|
|
|
@can_view(CostEstimate)
|
|
|
|
def cost_estimate_pdf(request, invoice, **_kwargs):
|
|
|
|
"""
|
|
|
|
View used to generate a PDF file from an existing cost estimate 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.
|
|
|
|
"""
|
|
|
|
purchases_objects = Vente.objects.all().filter(facture=invoice)
|
|
|
|
# Get the article list and build an list out of it
|
|
|
|
# contiaining (article_name, article_price, quantity, total_price)
|
|
|
|
purchases_info = []
|
|
|
|
for purchase in purchases_objects:
|
2019-11-04 16:55:03 +00:00
|
|
|
purchases_info.append(
|
|
|
|
{
|
|
|
|
"name": escape_chars(purchase.name),
|
|
|
|
"price": purchase.prix,
|
|
|
|
"quantity": purchase.number,
|
|
|
|
"total_price": purchase.prix_total,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return render_invoice(
|
|
|
|
request,
|
|
|
|
{
|
|
|
|
"paid": invoice.paid,
|
|
|
|
"fid": invoice.id,
|
|
|
|
"DATE": invoice.date,
|
|
|
|
"recipient_name": invoice.recipient,
|
|
|
|
"address": invoice.address,
|
|
|
|
"article": purchases_info,
|
|
|
|
"total": invoice.prix_total(),
|
|
|
|
"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"),
|
|
|
|
"tpl_path": os.path.join(settings.BASE_DIR, LOGO_PATH),
|
|
|
|
"payment_method": invoice.payment,
|
|
|
|
"remark": invoice.remark,
|
|
|
|
"end_validity": invoice.date + invoice.validity,
|
|
|
|
"is_estimate": True,
|
|
|
|
},
|
|
|
|
)
|
2018-12-31 22:58:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@can_delete(CostEstimate)
|
|
|
|
def del_cost_estimate(request, estimate, **_kwargs):
|
|
|
|
"""
|
2020-05-30 09:00:39 +00:00
|
|
|
View used to delete an existing invoice.
|
2018-12-31 22:58:37 +00:00
|
|
|
"""
|
|
|
|
if request.method == "POST":
|
|
|
|
estimate.delete()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The cost estimate was deleted."))
|
|
|
|
return redirect(reverse("cotisations:index-cost-estimate"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{"objet": estimate, "objet_name": _("cost estimate")},
|
2019-11-04 16:55:03 +00:00
|
|
|
"cotisations/delete.html",
|
|
|
|
request,
|
|
|
|
)
|
2018-12-31 22:58:37 +00:00
|
|
|
|
|
|
|
|
2018-07-21 22:16:05 +00:00
|
|
|
@login_required
|
|
|
|
@can_view(CustomInvoice)
|
|
|
|
def custom_invoice_pdf(request, invoice, **_kwargs):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
# TODO : change vente to purchase
|
|
|
|
purchases_objects = Vente.objects.all().filter(facture=invoice)
|
|
|
|
# Get the article list and build an list out of it
|
|
|
|
# contiaining (article_name, article_price, quantity, total_price)
|
|
|
|
purchases_info = []
|
|
|
|
for purchase in purchases_objects:
|
2019-11-04 16:55:03 +00:00
|
|
|
purchases_info.append(
|
|
|
|
{
|
|
|
|
"name": escape_chars(purchase.name),
|
|
|
|
"price": purchase.prix,
|
|
|
|
"quantity": purchase.number,
|
|
|
|
"total_price": purchase.prix_total,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return render_invoice(
|
|
|
|
request,
|
|
|
|
{
|
|
|
|
"paid": invoice.paid,
|
|
|
|
"fid": invoice.id,
|
|
|
|
"DATE": invoice.date,
|
|
|
|
"recipient_name": invoice.recipient,
|
|
|
|
"address": invoice.address,
|
|
|
|
"article": purchases_info,
|
|
|
|
"total": invoice.prix_total(),
|
|
|
|
"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"),
|
|
|
|
"tpl_path": os.path.join(settings.BASE_DIR, LOGO_PATH),
|
|
|
|
"payment_method": invoice.payment,
|
|
|
|
"remark": invoice.remark,
|
|
|
|
},
|
|
|
|
)
|
2018-07-21 22:16:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@can_delete(CustomInvoice)
|
|
|
|
def del_custom_invoice(request, invoice, **_kwargs):
|
|
|
|
"""
|
2020-05-30 09:00:39 +00:00
|
|
|
View used to delete an existing invoice.
|
2018-07-21 22:16:05 +00:00
|
|
|
"""
|
|
|
|
if request.method == "POST":
|
|
|
|
invoice.delete()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The invoice was deleted."))
|
|
|
|
return redirect(reverse("cotisations:index-custom-invoice"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{"objet": invoice, "objet_name": _("invoice")},
|
2019-11-04 16:55:03 +00:00
|
|
|
"cotisations/delete.html",
|
|
|
|
request,
|
|
|
|
)
|
2018-07-21 22:16:05 +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()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The article was created."))
|
|
|
|
return redirect(reverse("cotisations:index-article"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{
|
|
|
|
"factureform": article,
|
|
|
|
"action_name": _("Add"),
|
|
|
|
"title": _("New article"),
|
|
|
|
},
|
2019-11-04 16:55:03 +00:00
|
|
|
"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()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The article was edited."))
|
|
|
|
return redirect(reverse("cotisations:index-article"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{
|
|
|
|
"factureform": article,
|
|
|
|
"action_name": _("Edit"),
|
|
|
|
"title": _("Edit article"),
|
|
|
|
},
|
2019-11-04 16:55:03 +00:00
|
|
|
"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():
|
2019-11-04 16:55:03 +00:00
|
|
|
article_del = article.cleaned_data["articles"]
|
2018-03-31 15:18:39 +00:00
|
|
|
article_del.delete()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The articles were deleted."))
|
|
|
|
return redirect(reverse("cotisations:index-article"))
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": article,
|
|
|
|
"action_name": _("Delete"),
|
|
|
|
"title": _("Delete article"),
|
|
|
|
},
|
|
|
|
"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.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
payment = PaiementForm(request.POST or None, prefix="payment")
|
2018-07-02 19:13:13 +00:00
|
|
|
payment_method = payment_method_factory(
|
2019-11-04 16:55:03 +00:00
|
|
|
payment.instance, request.POST or None, prefix="payment_method"
|
2018-07-02 19:13:13 +00:00
|
|
|
)
|
|
|
|
if payment.is_valid() and payment_method.is_valid():
|
|
|
|
payment = payment.save()
|
2018-07-05 13:21:51 +00:00
|
|
|
payment_method.save(payment)
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The payment method was created."))
|
|
|
|
return redirect(reverse("cotisations:index-paiement"))
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": payment,
|
|
|
|
"payment_method": payment_method,
|
|
|
|
"action_name": _("Add"),
|
|
|
|
"title": _("New payment method"),
|
|
|
|
},
|
|
|
|
"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.
|
|
|
|
"""
|
2018-07-02 19:13:13 +00:00
|
|
|
payment = PaiementForm(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, instance=paiement_instance, prefix="payment"
|
2018-07-02 19:13:13 +00:00
|
|
|
)
|
|
|
|
payment_method = payment_method_factory(
|
2019-11-04 16:55:03 +00:00
|
|
|
paiement_instance, request.POST or None, prefix="payment_method", creation=False
|
2018-07-02 19:13:13 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
if payment.is_valid() and (payment_method is None or payment_method.is_valid()):
|
2018-07-02 19:13:13 +00:00
|
|
|
payment.save()
|
2018-07-16 19:46:13 +00:00
|
|
|
if payment_method is not None:
|
|
|
|
payment_method.save()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The payment method was edited."))
|
|
|
|
return redirect(reverse("cotisations:index-paiement"))
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": payment,
|
|
|
|
"payment_method": payment_method,
|
|
|
|
"action_name": _("Edit"),
|
|
|
|
"title": _("Edit payment method"),
|
|
|
|
},
|
|
|
|
"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():
|
2019-11-04 16:55:03 +00:00
|
|
|
payment_dels = payment.cleaned_data["paiements"]
|
2018-04-09 17:40:46 +00:00
|
|
|
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,
|
2019-11-04 16:55:03 +00:00
|
|
|
_("The payment method %(method_name)s was deleted.")
|
|
|
|
% {"method_name": payment_del},
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2016-07-06 19:43:39 +00:00
|
|
|
except ProtectedError:
|
2017-10-13 03:08:30 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
2019-11-04 16:55:03 +00:00
|
|
|
_(
|
2019-11-16 14:02:32 +00:00
|
|
|
"The payment method %(method_name)s can't be deleted"
|
|
|
|
" because there are invoices using it."
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
|
|
|
% {"method_name": payment_del},
|
2017-10-13 03:08:30 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
return redirect(reverse("cotisations:index-paiement"))
|
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": payment,
|
|
|
|
"action_name": _("Delete"),
|
|
|
|
"title": _("Delete payment method"),
|
|
|
|
},
|
|
|
|
"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()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The bank was created."))
|
|
|
|
return redirect(reverse("cotisations:index-banque"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{
|
|
|
|
"factureform": bank,
|
|
|
|
"action_name": _("Add"),
|
|
|
|
"title": _("New bank"),
|
|
|
|
},
|
2019-11-04 16:55:03 +00:00
|
|
|
"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()
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.success(request, _("The bank was edited."))
|
|
|
|
return redirect(reverse("cotisations:index-banque"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{
|
|
|
|
"factureform": bank,
|
|
|
|
"action_name": _("Edit"),
|
|
|
|
"title": _("Edit bank"),
|
|
|
|
},
|
2019-11-04 16:55:03 +00:00
|
|
|
"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():
|
2019-11-04 16:55:03 +00:00
|
|
|
bank_dels = bank.cleaned_data["banques"]
|
2018-04-09 17:40:46 +00:00
|
|
|
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,
|
2019-11-04 16:55:03 +00:00
|
|
|
_("The bank %(bank_name)s was deleted.") % {"bank_name": bank_del},
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2016-07-06 20:20:49 +00:00
|
|
|
except ProtectedError:
|
2018-03-31 13:43:46 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
2019-11-04 16:55:03 +00:00
|
|
|
_(
|
|
|
|
"The bank %(bank_name)s can't be deleted because there"
|
|
|
|
" are invoices using it."
|
|
|
|
)
|
|
|
|
% {"bank_name": bank_del},
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
return redirect(reverse("cotisations:index-banque"))
|
|
|
|
return form(
|
2019-11-16 14:02:32 +00:00
|
|
|
{
|
|
|
|
"factureform": bank,
|
|
|
|
"action_name": _("Delete"),
|
|
|
|
"title": _("Delete bank"),
|
|
|
|
},
|
2019-11-04 16:55:03 +00:00
|
|
|
"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)
|
2019-11-04 16:55:03 +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.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value("pagination_number")
|
|
|
|
invoice_list = Facture.objects.select_related("user").select_related("paiement")
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_list = SortTable.sort(
|
|
|
|
invoice_list,
|
2019-11-04 16:55:03 +00:00
|
|
|
request.GET.get("col"),
|
|
|
|
request.GET.get("order"),
|
|
|
|
SortTable.COTISATIONS_CONTROL,
|
2017-10-22 00:33:44 +00:00
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
control_invoices_formset = modelformset_factory(
|
2019-11-04 16:55:03 +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(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, queryset=invoice_list.object_list
|
2018-04-09 17:40:46 +00:00
|
|
|
)
|
|
|
|
if control_invoices_form.is_valid():
|
|
|
|
control_invoices_form.save()
|
2019-11-16 14:02:32 +00:00
|
|
|
reversion.set_comment("Control")
|
2018-05-10 20:28:27 +00:00
|
|
|
messages.success(
|
2019-11-04 16:55:03 +00:00
|
|
|
request, _("Your changes have been properly taken into account.")
|
2018-05-10 20:28:27 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
return redirect(reverse("cotisations:control"))
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"cotisations/control.html",
|
|
|
|
{"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
|
2019-11-04 16:55:03 +00:00
|
|
|
article_list = Article.objects.order_by("name")
|
|
|
|
return render(
|
|
|
|
request, "cotisations/index_article.html", {"article_list": article_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 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.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
payment_list = Paiement.objects.order_by("moyen")
|
|
|
|
return render(
|
|
|
|
request, "cotisations/index_paiement.html", {"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.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
bank_list = Banque.objects.order_by("name")
|
|
|
|
return render(request, "cotisations/index_banque.html", {"banque_list": bank_list})
|
2017-10-13 03:08:30 +00:00
|
|
|
|
2016-07-08 01:27:02 +00:00
|
|
|
|
2018-12-31 22:58:37 +00:00
|
|
|
@login_required
|
|
|
|
@can_view_all(CustomInvoice)
|
|
|
|
def index_cost_estimate(request):
|
|
|
|
"""View used to display every custom invoice."""
|
2019-11-04 16:55:03 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value("pagination_number")
|
|
|
|
cost_estimate_list = CostEstimate.objects.prefetch_related("vente_set")
|
2018-12-31 22:58:37 +00:00
|
|
|
cost_estimate_list = SortTable.sort(
|
|
|
|
cost_estimate_list,
|
2019-11-04 16:55:03 +00:00
|
|
|
request.GET.get("col"),
|
|
|
|
request.GET.get("order"),
|
|
|
|
SortTable.COTISATIONS_CUSTOM,
|
2018-12-31 22:58:37 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
cost_estimate_list = re2o_paginator(request, cost_estimate_list, pagination_number)
|
|
|
|
return render(
|
2018-12-31 22:58:37 +00:00
|
|
|
request,
|
2019-11-04 16:55:03 +00:00
|
|
|
"cotisations/index_cost_estimate.html",
|
|
|
|
{"cost_estimate_list": cost_estimate_list},
|
2018-12-31 22:58:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-07-21 22:16:05 +00:00
|
|
|
@login_required
|
|
|
|
@can_view_all(CustomInvoice)
|
|
|
|
def index_custom_invoice(request):
|
|
|
|
"""View used to display every custom invoice."""
|
2019-11-04 16:55:03 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value("pagination_number")
|
|
|
|
cost_estimate_ids = [i for i, in CostEstimate.objects.values_list("id")]
|
|
|
|
custom_invoice_list = CustomInvoice.objects.prefetch_related("vente_set").exclude(
|
|
|
|
id__in=cost_estimate_ids
|
|
|
|
)
|
2018-07-21 22:16:05 +00:00
|
|
|
custom_invoice_list = SortTable.sort(
|
|
|
|
custom_invoice_list,
|
2019-11-04 16:55:03 +00:00
|
|
|
request.GET.get("col"),
|
|
|
|
request.GET.get("order"),
|
|
|
|
SortTable.COTISATIONS_CUSTOM,
|
2018-07-21 22:16:05 +00:00
|
|
|
)
|
|
|
|
custom_invoice_list = re2o_paginator(
|
2019-11-04 16:55:03 +00:00
|
|
|
request, custom_invoice_list, pagination_number
|
|
|
|
)
|
|
|
|
return render(
|
2018-07-21 22:16:05 +00:00
|
|
|
request,
|
2019-11-04 16:55:03 +00:00
|
|
|
"cotisations/index_custom_invoice.html",
|
|
|
|
{"custom_invoice_list": custom_invoice_list},
|
2018-07-21 22:16:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-07-08 10:35:53 +00:00
|
|
|
@login_required
|
2017-12-27 20:09:00 +00:00
|
|
|
@can_view_all(Facture)
|
2018-07-21 22:16:05 +00:00
|
|
|
@can_view_all(CustomInvoice)
|
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.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value("pagination_number")
|
|
|
|
invoice_list = (
|
|
|
|
Facture.objects.select_related("user")
|
|
|
|
.select_related("paiement")
|
|
|
|
.prefetch_related("vente_set")
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_list = SortTable.sort(
|
|
|
|
invoice_list,
|
2019-11-04 16:55:03 +00:00
|
|
|
request.GET.get("col"),
|
|
|
|
request.GET.get("order"),
|
|
|
|
SortTable.COTISATIONS_INDEX,
|
2017-10-22 00:33:44 +00:00
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
invoice_list = re2o_paginator(request, invoice_list, pagination_number)
|
2019-11-04 16:55:03 +00:00
|
|
|
return render(request, "cotisations/index.html", {"facture_list": invoice_list})
|
2018-01-11 18:25:41 +00:00
|
|
|
|
|
|
|
|
2018-07-03 19:38:10 +00:00
|
|
|
# TODO : change solde to balance
|
2018-01-12 00:07:25 +00:00
|
|
|
@login_required
|
2018-07-03 19:38:10 +00:00
|
|
|
@can_edit(User)
|
|
|
|
def credit_solde(request, user, **_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-07-03 19:38:10 +00:00
|
|
|
View used to edit the balance of a user.
|
|
|
|
Can be use either to increase or decrease a user's balance.
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-07-16 18:14:26 +00:00
|
|
|
try:
|
|
|
|
balance = find_payment_method(Paiement.objects.get(is_balance=True))
|
|
|
|
except Paiement.DoesNotExist:
|
|
|
|
credit_allowed = False
|
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
credit_allowed = balance is not None and balance.can_credit_balance(
|
|
|
|
request.user
|
2018-07-16 18:14:26 +00:00
|
|
|
)
|
|
|
|
if not credit_allowed:
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.error(request, _("You are not allowed to credit your balance."))
|
|
|
|
return redirect(reverse("users:profil", kwargs={"userid": user.id}))
|
2018-07-16 18:14:26 +00:00
|
|
|
|
2019-01-03 18:52:06 +00:00
|
|
|
refill_form = RechargeForm(
|
2019-11-04 16:55:03 +00:00
|
|
|
request.POST or None, user=user, user_source=request.user
|
|
|
|
)
|
2018-04-09 17:40:46 +00:00
|
|
|
if refill_form.is_valid():
|
2019-11-04 16:55:03 +00:00
|
|
|
price = refill_form.cleaned_data["value"]
|
2018-07-10 14:46:49 +00:00
|
|
|
invoice = Facture(user=user)
|
2019-11-04 16:55:03 +00:00
|
|
|
invoice.paiement = refill_form.cleaned_data["payment"]
|
2018-07-13 20:03:22 +00:00
|
|
|
p = find_payment_method(invoice.paiement)
|
2019-11-04 16:55:03 +00:00
|
|
|
if hasattr(p, "check_price"):
|
2018-07-13 20:03:22 +00:00
|
|
|
price_ok, msg = p.check_price(price, user)
|
|
|
|
refill_form.add_error(None, msg)
|
|
|
|
else:
|
|
|
|
price_ok = True
|
|
|
|
if price_ok:
|
2020-04-19 19:19:06 +00:00
|
|
|
invoice.save(request=request)
|
2018-07-13 20:03:22 +00:00
|
|
|
Vente.objects.create(
|
|
|
|
facture=invoice,
|
2019-11-04 16:55:03 +00:00
|
|
|
name="solde",
|
|
|
|
prix=refill_form.cleaned_data["value"],
|
|
|
|
number=1,
|
2018-07-13 20:03:22 +00:00
|
|
|
)
|
2018-07-22 22:13:25 +00:00
|
|
|
|
2018-07-13 20:03:22 +00:00
|
|
|
return invoice.paiement.end_payment(invoice, request)
|
2018-07-13 15:52:49 +00:00
|
|
|
p = get_object_or_404(Paiement, is_balance=True)
|
2019-11-04 16:55:03 +00:00
|
|
|
return form(
|
|
|
|
{
|
|
|
|
"factureform": refill_form,
|
|
|
|
"balance": user.solde,
|
|
|
|
"title": _("Refill your balance"),
|
|
|
|
"action_name": _("Pay"),
|
|
|
|
"max_balance": find_payment_method(p).maximum_balance,
|
|
|
|
},
|
|
|
|
"cotisations/facture.html",
|
|
|
|
request,
|
|
|
|
)
|
2018-06-23 17:54:20 +00:00
|
|
|
|
2019-01-03 18:52:06 +00:00
|
|
|
|
2019-01-10 23:39:16 +00:00
|
|
|
@login_required
|
|
|
|
@can_view(Facture)
|
|
|
|
def voucher_pdf(request, invoice, **_kwargs):
|
|
|
|
"""
|
|
|
|
View used to generate a PDF file from a controlled invoice
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if not invoice.control:
|
2019-11-04 16:55:03 +00:00
|
|
|
messages.error(request, _("Could not find a voucher for that invoice."))
|
|
|
|
return redirect(reverse("cotisations:index"))
|
2019-09-28 11:01:22 +00:00
|
|
|
president = Mandate.get_mandate(invoice.date).president
|
2019-11-04 16:55:03 +00:00
|
|
|
return render_voucher(
|
|
|
|
request,
|
|
|
|
{
|
|
|
|
"asso_name": AssoOption.get_cached_value("name"),
|
|
|
|
"pres_name": " ".join([president.name, president.surname]),
|
|
|
|
"firstname": invoice.user.name,
|
|
|
|
"lastname": invoice.user.surname,
|
|
|
|
"email": invoice.user.email,
|
|
|
|
"phone": invoice.user.telephone,
|
|
|
|
"date_end": invoice.get_subscription().latest("date_end").date_end,
|
|
|
|
"date_begin": invoice.date,
|
|
|
|
},
|
|
|
|
)
|