2020-11-23 16:06:37 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
|
2017-01-15 23:01:18 +00:00
|
|
|
# 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.
|
2017-10-13 21:15:07 +00:00
|
|
|
"""
|
2018-04-13 18:58:29 +00:00
|
|
|
Forms for the 'cotisation' app of re2o. It highly depends on
|
2018-04-09 17:40:46 +00:00
|
|
|
:cotisations:models and is mainly used by :cotisations:views.
|
|
|
|
|
|
|
|
The following forms are mainly used to create, edit or delete
|
|
|
|
anything related to 'cotisations' :
|
|
|
|
* Payments Methods
|
|
|
|
* Banks
|
|
|
|
* Invoices
|
|
|
|
* Articles
|
|
|
|
|
|
|
|
See the details for each of these operations in the documentation
|
|
|
|
of each of the method.
|
2017-10-13 21:15:07 +00:00
|
|
|
"""
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
from django import forms
|
2018-04-14 13:39:51 +00:00
|
|
|
from django.core.validators import MinValueValidator
|
2021-02-10 10:06:09 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.forms import Form, ModelForm
|
2018-07-16 19:46:13 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2021-02-10 10:06:09 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-03-31 13:43:46 +00:00
|
|
|
|
2017-12-28 01:08:02 +00:00
|
|
|
from re2o.field_permissions import FieldPermissionFormMixin
|
2021-01-02 22:19:49 +00:00
|
|
|
from re2o.mixins import FormRevMixin
|
|
|
|
from re2o.widgets import AutocompleteModelWidget
|
2021-02-10 10:06:09 +00:00
|
|
|
|
|
|
|
from .models import (Article, Banque, CostEstimate, CustomInvoice, Facture,
|
|
|
|
Paiement, Vente)
|
2018-07-03 17:30:31 +00:00
|
|
|
from .payment_methods import balance
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2017-12-28 01:08:02 +00:00
|
|
|
|
2018-07-10 16:38:05 +00:00
|
|
|
class FactureForm(FieldPermissionFormMixin, FormRevMixin, ModelForm):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-07-10 16:38:05 +00:00
|
|
|
Form used to manage and create an invoice and its fields.
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-07-03 16:53:44 +00:00
|
|
|
|
2018-07-10 16:38:05 +00:00
|
|
|
def __init__(self, *args, creation=False, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
user = kwargs["user"]
|
|
|
|
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
|
2018-07-10 16:38:05 +00:00
|
|
|
super(FactureForm, self).__init__(*args, prefix=prefix, **kwargs)
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["paiement"].empty_label = _("Select a payment method")
|
|
|
|
self.fields["paiement"].queryset = Paiement.find_allowed_payments(user)
|
2018-07-10 16:38:05 +00:00
|
|
|
if not creation:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["user"].label = _("Member")
|
|
|
|
self.fields["user"].empty_label = _("Select the proprietary member")
|
|
|
|
self.fields["valid"].label = _("Validated invoice")
|
2018-07-10 16:38:05 +00:00
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields = {"paiement": self.fields["paiement"]}
|
2018-06-17 15:33:49 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
class Meta:
|
|
|
|
model = Facture
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = "__all__"
|
2020-12-28 17:57:08 +00:00
|
|
|
widgets = {
|
2021-01-02 22:19:49 +00:00
|
|
|
"user": AutocompleteModelWidget(url="/users/user-autocomplete"),
|
|
|
|
"banque": AutocompleteModelWidget(url="/cotisations/banque-autocomplete"),
|
2020-12-28 17:57:08 +00:00
|
|
|
}
|
2016-07-06 19:23:05 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2018-07-10 16:38:05 +00:00
|
|
|
cleaned_data = super(FactureForm, self).clean()
|
2019-11-04 16:55:03 +00:00
|
|
|
paiement = cleaned_data.get("paiement")
|
2016-07-14 11:55:46 +00:00
|
|
|
if not paiement:
|
2019-11-04 16:55:03 +00:00
|
|
|
raise forms.ValidationError(_("A payment method must be specified."))
|
2016-07-06 19:23:05 +00:00
|
|
|
return cleaned_data
|
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
|
2018-08-26 15:13:29 +00:00
|
|
|
class SelectArticleForm(FormRevMixin, Form):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-04-13 18:58:29 +00:00
|
|
|
Form used to select an article during the creation of an invoice for a
|
|
|
|
member.
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
article = forms.ModelChoiceField(
|
2019-11-04 16:55:03 +00:00
|
|
|
queryset=Article.objects.none(), label=_("Article"), required=True
|
2017-10-28 04:02:53 +00:00
|
|
|
)
|
|
|
|
quantity = forms.IntegerField(
|
2019-11-04 16:55:03 +00:00
|
|
|
label=_("Quantity"), validators=[MinValueValidator(1)], required=True
|
2017-10-28 04:02:53 +00:00
|
|
|
)
|
|
|
|
|
2018-06-17 19:49:59 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
user = kwargs.pop("user")
|
|
|
|
target_user = kwargs.pop("target_user", None)
|
2018-08-26 15:13:29 +00:00
|
|
|
super(SelectArticleForm, self).__init__(*args, **kwargs)
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["article"].queryset = Article.find_allowed_articles(
|
|
|
|
user, target_user
|
|
|
|
)
|
2018-12-29 13:01:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DiscountForm(Form):
|
|
|
|
"""
|
|
|
|
Form used in oder to create a discount on an invoice.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-12-29 13:01:22 +00:00
|
|
|
is_relative = forms.BooleanField(
|
2019-11-16 14:02:32 +00:00
|
|
|
label=_("Discount is in percentage."), required=False
|
2018-12-29 13:01:22 +00:00
|
|
|
)
|
|
|
|
discount = forms.DecimalField(
|
|
|
|
label=_("Discount"),
|
|
|
|
max_value=100,
|
|
|
|
min_value=0,
|
|
|
|
max_digits=5,
|
|
|
|
decimal_places=2,
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def apply_to_invoice(self, invoice):
|
|
|
|
invoice_price = invoice.prix_total()
|
2019-11-04 16:55:03 +00:00
|
|
|
discount = self.cleaned_data["discount"]
|
|
|
|
is_relative = self.cleaned_data["is_relative"]
|
2018-12-29 13:01:22 +00:00
|
|
|
if is_relative:
|
2019-11-04 16:55:03 +00:00
|
|
|
amount = discount / 100 * invoice_price
|
2018-12-29 13:01:22 +00:00
|
|
|
else:
|
|
|
|
amount = discount
|
2018-12-29 22:55:18 +00:00
|
|
|
if amount:
|
2019-11-16 14:02:32 +00:00
|
|
|
name = _("{}% discount") if is_relative else _("{} € discount")
|
2018-12-29 13:01:22 +00:00
|
|
|
name = name.format(discount)
|
2019-11-04 16:55:03 +00:00
|
|
|
Vente.objects.create(facture=invoice, name=name, prix=-amount, number=1)
|
2018-06-17 19:49:59 +00:00
|
|
|
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2018-07-21 22:16:05 +00:00
|
|
|
class CustomInvoiceForm(FormRevMixin, ModelForm):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-07-21 22:16:05 +00:00
|
|
|
Form used to create a custom invoice.
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-07-21 22:16:05 +00:00
|
|
|
class Meta:
|
|
|
|
model = CustomInvoice
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = "__all__"
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
|
2018-12-31 22:58:37 +00:00
|
|
|
class CostEstimateForm(FormRevMixin, ModelForm):
|
|
|
|
"""
|
|
|
|
Form used to create a cost estimate.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-12-31 22:58:37 +00:00
|
|
|
class Meta:
|
|
|
|
model = CostEstimate
|
2019-11-04 16:55:03 +00:00
|
|
|
exclude = ["paid", "final_invoice"]
|
2018-12-31 22:58:37 +00:00
|
|
|
|
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class ArticleForm(FormRevMixin, ModelForm):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to create an article.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
class Meta:
|
|
|
|
model = Article
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = "__all__"
|
2016-07-06 19:23:05 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
|
2017-10-08 20:22:04 +00:00
|
|
|
super(ArticleForm, self).__init__(*args, prefix=prefix, **kwargs)
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["name"].label = _("Article name")
|
2016-07-06 19:23:05 +00:00
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class DelArticleForm(FormRevMixin, Form):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to delete one or more of the currently available articles.
|
|
|
|
The user must choose the one to delete by checking the boxes.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
articles = forms.ModelMultipleChoiceField(
|
2017-12-13 17:56:16 +00:00
|
|
|
queryset=Article.objects.none(),
|
2019-11-16 14:02:32 +00:00
|
|
|
label=_("Current articles"),
|
2019-11-04 16:55:03 +00:00
|
|
|
widget=forms.CheckboxSelectMultiple,
|
2017-10-13 03:24:57 +00:00
|
|
|
)
|
|
|
|
|
2017-12-13 17:56:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
instances = kwargs.pop("instances", None)
|
2017-12-13 17:56:16 +00:00
|
|
|
super(DelArticleForm, self).__init__(*args, **kwargs)
|
|
|
|
if instances:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["articles"].queryset = instances
|
2017-12-13 17:56:16 +00:00
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["articles"].queryset = Article.objects.all()
|
2017-12-13 17:56:16 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Paiement to Payment
|
2018-03-31 15:18:39 +00:00
|
|
|
class PaiementForm(FormRevMixin, ModelForm):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to create a new payment method.
|
|
|
|
The 'cheque' type is used to associate a specific behaviour requiring
|
|
|
|
a cheque number and a bank.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
class Meta:
|
|
|
|
model = Paiement
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change moyen to method and type_paiement to payment_type
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ["moyen", "available_for_everyone"]
|
2016-07-06 19:23:05 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
|
2017-10-08 20:22:04 +00:00
|
|
|
super(PaiementForm, self).__init__(*args, prefix=prefix, **kwargs)
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["moyen"].label = _("Payment method name")
|
2016-07-06 19:23:05 +00:00
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change paiement to payment
|
2018-03-31 15:18:39 +00:00
|
|
|
class DelPaiementForm(FormRevMixin, Form):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to delete one or more payment methods.
|
|
|
|
The user must choose the one to delete by checking the boxes.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change paiement to payment
|
2017-10-13 03:24:57 +00:00
|
|
|
paiements = forms.ModelMultipleChoiceField(
|
2017-12-13 17:56:16 +00:00
|
|
|
queryset=Paiement.objects.none(),
|
2019-11-16 14:02:32 +00:00
|
|
|
label=_("Current payment methods"),
|
2019-11-04 16:55:03 +00:00
|
|
|
widget=forms.CheckboxSelectMultiple,
|
2017-10-13 03:24:57 +00:00
|
|
|
)
|
|
|
|
|
2017-12-13 17:56:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
instances = kwargs.pop("instances", None)
|
2017-12-13 17:56:16 +00:00
|
|
|
super(DelPaiementForm, self).__init__(*args, **kwargs)
|
|
|
|
if instances:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["paiements"].queryset = instances
|
2017-12-13 17:56:16 +00:00
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["paiements"].queryset = Paiement.objects.all()
|
2017-12-13 17:56:16 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
2018-03-31 15:18:39 +00:00
|
|
|
class BanqueForm(FormRevMixin, ModelForm):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to create a bank.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2016-07-06 20:20:49 +00:00
|
|
|
class Meta:
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
2016-07-06 20:20:49 +00:00
|
|
|
model = Banque
|
2019-11-04 16:55:03 +00:00
|
|
|
fields = ["name"]
|
2016-07-06 20:20:49 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
|
2017-10-08 20:22:04 +00:00
|
|
|
super(BanqueForm, self).__init__(*args, prefix=prefix, **kwargs)
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["name"].label = _("Bank name")
|
2016-07-06 20:20:49 +00:00
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
2018-03-31 15:18:39 +00:00
|
|
|
class DelBanqueForm(FormRevMixin, Form):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to delete one or more banks.
|
|
|
|
The use must choose the one to delete by checking the boxes.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
2017-10-13 03:24:57 +00:00
|
|
|
banques = forms.ModelMultipleChoiceField(
|
2017-12-13 17:56:16 +00:00
|
|
|
queryset=Banque.objects.none(),
|
2019-11-16 14:02:32 +00:00
|
|
|
label=_("Current banks"),
|
2019-11-04 16:55:03 +00:00
|
|
|
widget=forms.CheckboxSelectMultiple,
|
2017-10-13 03:24:57 +00:00
|
|
|
)
|
2017-12-13 17:56:16 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
instances = kwargs.pop("instances", None)
|
2017-12-13 17:56:16 +00:00
|
|
|
super(DelBanqueForm, self).__init__(*args, **kwargs)
|
|
|
|
if instances:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["banques"].queryset = instances
|
2017-12-13 17:56:16 +00:00
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["banques"].queryset = Banque.objects.all()
|
2018-01-11 18:25:41 +00:00
|
|
|
|
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : Better name and docstring
|
2018-03-31 15:18:39 +00:00
|
|
|
class RechargeForm(FormRevMixin, Form):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to refill a user's balance
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
value = forms.DecimalField(label=_("Amount"), decimal_places=2)
|
2018-07-03 17:30:31 +00:00
|
|
|
payment = forms.ModelChoiceField(
|
2019-11-04 16:55:03 +00:00
|
|
|
queryset=Paiement.objects.none(), label=_("Payment method")
|
2018-07-03 17:30:31 +00:00
|
|
|
)
|
2018-01-14 19:15:21 +00:00
|
|
|
|
2018-08-27 17:04:27 +00:00
|
|
|
def __init__(self, *args, user=None, user_source=None, **kwargs):
|
2018-07-13 14:14:08 +00:00
|
|
|
self.user = user
|
2018-01-14 19:15:21 +00:00
|
|
|
super(RechargeForm, self).__init__(*args, **kwargs)
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["payment"].empty_label = _("Select a payment method")
|
|
|
|
self.fields["payment"].queryset = Paiement.find_allowed_payments(
|
|
|
|
user_source
|
|
|
|
).exclude(is_balance=True)
|
2018-01-14 19:15:21 +00:00
|
|
|
|
2018-07-13 15:52:49 +00:00
|
|
|
def clean(self):
|
2018-04-14 13:39:51 +00:00
|
|
|
"""
|
2018-07-03 17:30:31 +00:00
|
|
|
Returns a cleaned value from the received form by validating
|
2018-04-14 13:39:51 +00:00
|
|
|
the value is well inside the possible limits
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
value = self.cleaned_data["value"]
|
2018-07-16 21:17:09 +00:00
|
|
|
balance_method = get_object_or_404(balance.PaymentMethod)
|
2019-11-04 16:55:03 +00:00
|
|
|
if (
|
|
|
|
balance_method.maximum_balance is not None
|
|
|
|
and value + self.user.solde > balance_method.maximum_balance
|
|
|
|
):
|
2018-03-31 13:43:46 +00:00
|
|
|
raise forms.ValidationError(
|
2019-11-04 16:55:03 +00:00
|
|
|
_(
|
|
|
|
"Requested amount is too high. Your balance can't exceed"
|
|
|
|
" %(max_online_balance)s €."
|
|
|
|
)
|
|
|
|
% {"max_online_balance": balance_method.maximum_balance}
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2018-07-13 15:52:49 +00:00
|
|
|
return self.cleaned_data
|