8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-10-31 23:22:49 +00:00
re2o/cotisations/forms.py

304 lines
9.9 KiB
Python
Raw Normal View History

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
# 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
: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
"""
from __future__ import unicode_literals
from django import forms
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
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as _l
from django.shortcuts import get_object_or_404
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
from .payment_methods import balance
2018-04-13 18:58:29 +00:00
2018-07-10 16:38:05 +00:00
class FactureForm(FieldPermissionFormMixin, FormRevMixin, ModelForm):
"""
2018-07-10 16:38:05 +00:00
Form used to manage and create an invoice and its fields.
"""
2018-07-03 16:53:44 +00:00
2018-07-10 16:38:05 +00:00
def __init__(self, *args, creation=False, **kwargs):
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)
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:
self.fields['user'].label = _("Member")
self.fields['user'].empty_label = \
_("Select the proprietary member")
self.fields['valid'].label = _("Validated invoice")
else:
self.fields = {'paiement': self.fields['paiement']}
class Meta:
model = Facture
2018-07-10 16:38:05 +00:00
fields = '__all__'
def clean(self):
2018-07-10 16:38:05 +00:00
cleaned_data = super(FactureForm, self).clean()
paiement = cleaned_data.get('paiement')
if not paiement:
raise forms.ValidationError(
_("A payment method must be specified.")
)
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-13 18:58:29 +00:00
Form used to select an article during the creation of an invoice for a
member.
"""
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')
),
label=_l("Article"),
required=True
)
quantity = forms.IntegerField(
label=_l("Quantity"),
validators=[MinValueValidator(1)],
required=True
)
def __init__(self, *args, **kwargs):
2018-07-03 16:53:44 +00:00
user = kwargs.pop('user')
super(SelectUserArticleForm, self).__init__(*args, **kwargs)
self.fields['article'].queryset = Article.find_allowed_articles(user)
class SelectClubArticleForm(Form):
"""
2018-04-13 18:58:29 +00:00
Form used to select an article during the creation of an invoice for a
club.
"""
article = forms.ModelChoiceField(
2018-04-13 18:58:29 +00:00
queryset=Article.objects.filter(
Q(type_user='All') | Q(type_user='Club')
),
label=_l("Article"),
2017-10-13 03:24:57 +00:00
required=True
)
quantity = forms.IntegerField(
label=_l("Quantity"),
2017-10-13 03:24:57 +00:00
validators=[MinValueValidator(1)],
required=True
)
def __init__(self, user, *args, **kwargs):
super(SelectClubArticleForm, self).__init__(*args, **kwargs)
self.fields['article'].queryset = Article.find_allowed_articles(user)
2018-04-13 18:58:29 +00:00
# TODO : change Facture to Invoice
2016-07-09 21:26:17 +00:00
class NewFactureFormPdf(Form):
"""
Form used to create a custom PDF invoice.
"""
paid = forms.BooleanField(label=_l("Paid"), required=False)
# 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")
)
# 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
class ArticleForm(FormRevMixin, ModelForm):
"""
Form used to create an article.
"""
class Meta:
model = Article
fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(ArticleForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = _("Article name")
2017-10-13 03:24:57 +00:00
class DelArticleForm(FormRevMixin, Form):
"""
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(
queryset=Article.objects.none(),
label=_l("Existing articles"),
2017-10-13 03:24:57 +00:00
widget=forms.CheckboxSelectMultiple
)
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()
# TODO : change Paiement to Payment
class PaiementForm(FormRevMixin, ModelForm):
"""
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.
"""
class Meta:
model = Paiement
# TODO : change moyen to method and type_paiement to payment_type
2018-07-03 16:53:44 +00:00
fields = ['moyen', 'available_for_everyone']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(PaiementForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['moyen'].label = _("Payment method name")
2017-10-13 03:24:57 +00:00
# TODO : change paiement to payment
class DelPaiementForm(FormRevMixin, Form):
"""
Form used to delete one or more payment methods.
The user must choose the one to delete by checking the boxes.
"""
# TODO : change paiement to payment
2017-10-13 03:24:57 +00:00
paiements = forms.ModelMultipleChoiceField(
queryset=Paiement.objects.none(),
label=_l("Existing payment method"),
2017-10-13 03:24:57 +00:00
widget=forms.CheckboxSelectMultiple
)
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()
# TODO : change banque to bank
class BanqueForm(FormRevMixin, ModelForm):
"""
Form used to create a bank.
"""
2016-07-06 20:20:49 +00:00
class Meta:
# TODO : change banque to bank
2016-07-06 20:20:49 +00:00
model = Banque
fields = ['name']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(BanqueForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = _("Bank name")
2016-07-06 20:20:49 +00:00
2017-10-13 03:24:57 +00:00
# TODO : change banque to bank
class DelBanqueForm(FormRevMixin, Form):
"""
Form used to delete one or more banks.
The use must choose the one to delete by checking the boxes.
"""
# TODO : change banque to bank
2017-10-13 03:24:57 +00:00
banques = forms.ModelMultipleChoiceField(
queryset=Banque.objects.none(),
label=_l("Existing banks"),
2017-10-13 03:24:57 +00:00
widget=forms.CheckboxSelectMultiple
)
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()
# TODO : Better name and docstring
class RechargeForm(FormRevMixin, Form):
"""
Form used to refill a user's balance
"""
2018-01-12 00:07:25 +00:00
value = forms.FloatField(
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
)
payment = forms.ModelChoiceField(
queryset=Paiement.objects.none(),
label=_l("Payment method")
)
def __init__(self, *args, user=None, **kwargs):
self.user = user
super(RechargeForm, self).__init__(*args, **kwargs)
self.fields['payment'].empty_label = \
_("Select a payment method")
self.fields['payment'].queryset = Paiement.find_allowed_payments(user)
def clean(self):
2018-04-14 13:39:51 +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
"""
value = self.cleaned_data['value']
2018-07-16 21:17:09 +00:00
balance_method = get_object_or_404(balance.PaymentMethod)
if balance_method.maximum_balance is not None and \
value + self.user.solde > balance_method.maximum_balance:
raise forms.ValidationError(
_("Requested amount is too high. Your balance can't exceed \
%(max_online_balance)s .") % {
'max_online_balance': balance_method.maximum_balance
}
)
return self.cleaned_data