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
|
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
|
2017-10-28 04:02:53 +00:00
|
|
|
from django.db.models import Q
|
2016-07-09 21:26:17 +00:00
|
|
|
from django.forms import ModelForm, Form
|
2018-04-14 13:39:51 +00:00
|
|
|
from django.core.validators import MinValueValidator
|
2018-03-31 13:43:46 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-03-31 15:32:26 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _l
|
2018-03-31 13:43:46 +00:00
|
|
|
|
2017-12-28 01:08:02 +00:00
|
|
|
from re2o.field_permissions import FieldPermissionFormMixin
|
2018-04-13 18:58:29 +00:00
|
|
|
from re2o.mixins import FormRevMixin
|
2018-04-14 13:39:51 +00:00
|
|
|
from .models import Article, Paiement, Facture, Banque
|
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-03-31 15:18:39 +00:00
|
|
|
class NewFactureForm(FormRevMixin, ModelForm):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to create a new invoice by using a payment method, a bank and a
|
|
|
|
cheque number.
|
|
|
|
"""
|
2018-07-03 16:53:44 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-03 16:53:44 +00:00
|
|
|
user = kwargs.pop('user')
|
2017-10-08 23:34:49 +00:00
|
|
|
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
|
2017-10-08 20:22:04 +00:00
|
|
|
super(NewFactureForm, self).__init__(*args, prefix=prefix, **kwargs)
|
2018-03-31 13:43:46 +00:00
|
|
|
self.fields['paiement'].empty_label = \
|
|
|
|
_("Select a payment method")
|
2018-07-03 16:53:44 +00:00
|
|
|
self.fields['paiement'].queryset = Paiement.objects.filter(
|
|
|
|
pk__in=map(lambda x: x.pk, Paiement.find_allowed_payments(user))
|
|
|
|
)
|
2018-06-17 15:33:49 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
class Meta:
|
|
|
|
model = Facture
|
2018-07-02 12:24:53 +00:00
|
|
|
fields = ['paiement']
|
2016-07-06 19:23:05 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2017-10-13 03:24:57 +00:00
|
|
|
cleaned_data = super(NewFactureForm, self).clean()
|
2018-03-31 13:43:46 +00:00
|
|
|
paiement = cleaned_data.get('paiement')
|
2016-07-14 11:55:46 +00:00
|
|
|
if not paiement:
|
2018-03-31 13:43:46 +00:00
|
|
|
raise forms.ValidationError(
|
2018-03-31 15:23:34 +00:00
|
|
|
_("A payment method must be specified.")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2016-07-06 19:23:05 +00:00
|
|
|
return cleaned_data
|
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
|
2018-07-03 16:53:44 +00:00
|
|
|
class SelectUserArticleForm(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
|
|
|
"""
|
2017-10-13 03:24:57 +00:00
|
|
|
article = forms.ModelChoiceField(
|
2018-04-13 18:58:29 +00:00
|
|
|
queryset=Article.objects.filter(
|
|
|
|
Q(type_user='All') | Q(type_user='Adherent')
|
|
|
|
),
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Article"),
|
2017-10-28 04:02:53 +00:00
|
|
|
required=True
|
|
|
|
)
|
|
|
|
quantity = forms.IntegerField(
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Quantity"),
|
2017-10-28 04:02:53 +00:00
|
|
|
validators=[MinValueValidator(1)],
|
|
|
|
required=True
|
|
|
|
)
|
|
|
|
|
2018-06-17 19:49:59 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-03 16:53:44 +00:00
|
|
|
user = kwargs.pop('user')
|
2018-06-17 19:49:59 +00:00
|
|
|
super(SelectUserArticleForm, self).__init__(*args, **kwargs)
|
2018-07-03 16:53:44 +00:00
|
|
|
self.fields['article'].queryset = Article.objects.filter(
|
|
|
|
pk__in=map(lambda x: x.pk, Article.find_allowed_articles(user))
|
|
|
|
)
|
2018-06-17 19:49:59 +00:00
|
|
|
|
2017-10-28 04:02:53 +00:00
|
|
|
|
|
|
|
class SelectClubArticleForm(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
|
|
|
|
club.
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2017-10-28 04:02:53 +00:00
|
|
|
article = forms.ModelChoiceField(
|
2018-04-13 18:58:29 +00:00
|
|
|
queryset=Article.objects.filter(
|
|
|
|
Q(type_user='All') | Q(type_user='Club')
|
|
|
|
),
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Article"),
|
2017-10-13 03:24:57 +00:00
|
|
|
required=True
|
|
|
|
)
|
|
|
|
quantity = forms.IntegerField(
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Quantity"),
|
2017-10-13 03:24:57 +00:00
|
|
|
validators=[MinValueValidator(1)],
|
|
|
|
required=True
|
|
|
|
)
|
|
|
|
|
2018-06-17 19:49:59 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-03 16:53:44 +00:00
|
|
|
user = kwargs.pop('user')
|
2018-06-17 19:49:59 +00:00
|
|
|
super(SelectClubArticleForm, self).__init__(*args, **kwargs)
|
2018-07-03 16:53:44 +00:00
|
|
|
self.fields['article'].queryset = Article.objects.filter(
|
|
|
|
pk__in=map(lambda x: x.pk, Article.find_allowed_articles(user))
|
|
|
|
)
|
2018-06-17 19:49:59 +00:00
|
|
|
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Facture to Invoice
|
2016-07-09 21:26:17 +00:00
|
|
|
class NewFactureFormPdf(Form):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to create a custom PDF invoice.
|
|
|
|
"""
|
2018-03-31 15:32:26 +00:00
|
|
|
paid = forms.BooleanField(label=_l("Paid"), required=False)
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change dest field to recipient
|
2018-04-13 18:58:29 +00:00
|
|
|
dest = forms.CharField(
|
|
|
|
required=True,
|
|
|
|
max_length=255,
|
|
|
|
label=_l("Recipient")
|
|
|
|
)
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change chambre field to address
|
2018-04-13 18:58:29 +00:00
|
|
|
chambre = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
max_length=10,
|
|
|
|
label=_l("Address")
|
|
|
|
)
|
|
|
|
|
2017-10-13 03:24:57 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Facture to Invoice
|
2017-12-28 01:08:02 +00:00
|
|
|
class EditFactureForm(FieldPermissionFormMixin, NewFactureForm):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Form used to edit an invoice and its fields : payment method, bank,
|
|
|
|
user associated, ...
|
|
|
|
"""
|
2016-07-06 19:23:05 +00:00
|
|
|
class Meta(NewFactureForm.Meta):
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Facture to Invoice
|
2017-12-28 01:08:02 +00:00
|
|
|
model = Facture
|
|
|
|
fields = '__all__'
|
2016-07-06 19:23:05 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Facture to Invoice
|
2016-07-06 19:23:05 +00:00
|
|
|
super(EditFactureForm, self).__init__(*args, **kwargs)
|
2018-03-31 13:43:46 +00:00
|
|
|
self.fields['user'].label = _("Member")
|
|
|
|
self.fields['user'].empty_label = \
|
|
|
|
_("Select the proprietary member")
|
|
|
|
self.fields['valid'].label = _("Validated invoice")
|
2016-07-14 20:29:30 +00:00
|
|
|
|
2016-07-06 19:23:05 +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.
|
|
|
|
"""
|
2016-07-06 19:23:05 +00:00
|
|
|
class Meta:
|
|
|
|
model = Article
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-10-08 23:34:49 +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)
|
2018-03-31 13:43:46 +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.
|
|
|
|
"""
|
2017-10-13 03:24:57 +00:00
|
|
|
articles = forms.ModelMultipleChoiceField(
|
2017-12-13 17:56:16 +00:00
|
|
|
queryset=Article.objects.none(),
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Existing articles"),
|
2017-10-13 03:24:57 +00:00
|
|
|
widget=forms.CheckboxSelectMultiple
|
|
|
|
)
|
|
|
|
|
2017-12-13 17:56:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
instances = kwargs.pop('instances', None)
|
|
|
|
super(DelArticleForm, self).__init__(*args, **kwargs)
|
|
|
|
if instances:
|
|
|
|
self.fields['articles'].queryset = instances
|
|
|
|
else:
|
|
|
|
self.fields['articles'].queryset = Article.objects.all()
|
|
|
|
|
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.
|
|
|
|
"""
|
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
|
2018-07-03 16:53:44 +00:00
|
|
|
fields = ['moyen', 'available_for_everyone']
|
2016-07-06 19:23:05 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-10-08 23:34:49 +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)
|
2018-03-31 13:43:46 +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.
|
|
|
|
"""
|
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(),
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Existing payment method"),
|
2017-10-13 03:24:57 +00:00
|
|
|
widget=forms.CheckboxSelectMultiple
|
|
|
|
)
|
|
|
|
|
2017-12-13 17:56:16 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
instances = kwargs.pop('instances', None)
|
|
|
|
super(DelPaiementForm, self).__init__(*args, **kwargs)
|
|
|
|
if instances:
|
|
|
|
self.fields['paiements'].queryset = instances
|
|
|
|
else:
|
|
|
|
self.fields['paiements'].queryset = Paiement.objects.all()
|
|
|
|
|
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.
|
|
|
|
"""
|
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
|
|
|
|
fields = ['name']
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-10-08 23:34:49 +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)
|
2018-03-31 13:43:46 +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.
|
|
|
|
"""
|
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(),
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Existing banks"),
|
2017-10-13 03:24:57 +00:00
|
|
|
widget=forms.CheckboxSelectMultiple
|
|
|
|
)
|
2017-12-13 17:56:16 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
instances = kwargs.pop('instances', None)
|
|
|
|
super(DelBanqueForm, self).__init__(*args, **kwargs)
|
|
|
|
if instances:
|
|
|
|
self.fields['banques'].queryset = instances
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
"""
|
2018-01-12 00:07:25 +00:00
|
|
|
value = forms.FloatField(
|
2018-03-31 15:32:26 +00:00
|
|
|
label=_l("Amount"),
|
2018-01-12 00:07:25 +00:00
|
|
|
min_value=0.01,
|
2018-04-13 18:58:29 +00:00
|
|
|
validators=[]
|
2018-01-12 00:07:25 +00:00
|
|
|
)
|
2018-07-03 17:30:31 +00:00
|
|
|
payment = forms.ModelChoiceField(
|
|
|
|
queryset=Paiement.objects.none(),
|
|
|
|
label=_l("Payment method")
|
|
|
|
)
|
2018-01-14 19:15:21 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user')
|
|
|
|
super(RechargeForm, self).__init__(*args, **kwargs)
|
2018-07-03 17:30:31 +00:00
|
|
|
self.fields['payment'].empty_label = \
|
|
|
|
_("Select a payment method")
|
|
|
|
self.fields['payment'].queryset = Paiement.objects.filter(
|
|
|
|
pk__in=map(lambda x: x.pk,
|
|
|
|
Paiement.find_allowed_payments(self.user))
|
|
|
|
)
|
2018-01-14 19:15:21 +00:00
|
|
|
|
|
|
|
def clean_value(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
|
|
|
|
"""
|
2018-01-14 19:15:21 +00:00
|
|
|
value = self.cleaned_data['value']
|
2018-07-03 17:30:31 +00:00
|
|
|
balance_method, _created = balance.PaymentMethod\
|
|
|
|
.objects.get_or_create()
|
|
|
|
if value < balance_method.minimum_balance:
|
2018-03-31 13:43:46 +00:00
|
|
|
raise forms.ValidationError(
|
|
|
|
_("Requested amount is too small. Minimum amount possible : \
|
2018-03-31 15:23:34 +00:00
|
|
|
%(min_online_amount)s €.") % {
|
2018-07-03 17:30:31 +00:00
|
|
|
'min_online_amount': balance_method.minimum_balance
|
2018-03-31 13:43:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-07-03 17:30:31 +00:00
|
|
|
if value + self.user.solde > balance_method.maximum_balance:
|
2018-03-31 13:43:46 +00:00
|
|
|
raise forms.ValidationError(
|
|
|
|
_("Requested amount is too high. Your balance can't exceed \
|
2018-03-31 15:23:34 +00:00
|
|
|
%(max_online_balance)s €.") % {
|
2018-07-03 17:30:31 +00:00
|
|
|
'max_online_balance': balance_method.maximum_balance
|
2018-03-31 13:43:46 +00:00
|
|
|
}
|
|
|
|
)
|
2018-01-14 19:15:21 +00:00
|
|
|
return value
|