8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-09 11:26:27 +00:00
re2o/cotisations/forms.py

319 lines
10 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
2018-06-23 17:54:20 +00:00
from django.utils.translation import ugettext_lazy as _
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-12-31 22:58:37 +00:00
from .models import (
Article, Paiement, Facture, Banque,
CustomInvoice, Vente, CostEstimate,
2018-12-31 22:58:37 +00:00
)
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
class SelectArticleForm(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(
queryset=Article.objects.none(),
2018-06-23 17:54:20 +00:00
label=_("Article"),
required=True
)
quantity = forms.IntegerField(
2018-06-23 17:54:20 +00:00
label=_("Quantity"),
validators=[MinValueValidator(1)],
required=True
)
def __init__(self, *args, **kwargs):
2018-07-03 16:53:44 +00:00
user = kwargs.pop('user')
target_user = kwargs.pop('target_user', None)
super(SelectArticleForm, self).__init__(*args, **kwargs)
2018-12-29 13:01:22 +00:00
self.fields['article'].queryset = Article.find_allowed_articles(
user, target_user)
class DiscountForm(Form):
"""
Form used in oder to create a discount on an invoice.
"""
is_relative = forms.BooleanField(
2019-01-08 23:38:53 +00:00
label=_("Discount is on percentage."),
2018-12-29 13:01:22 +00:00
required=False,
)
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()
discount = self.cleaned_data['discount']
is_relative = self.cleaned_data['is_relative']
if is_relative:
amount = discount/100 * invoice_price
else:
amount = discount
2018-12-29 22:55:18 +00:00
if amount:
2018-12-29 13:01:22 +00:00
name = _("{}% discount") if is_relative else _("{}€ discount")
name = name.format(discount)
Vente.objects.create(
facture=invoice,
name=name,
prix=-amount,
number=1
)
2018-04-13 18:58:29 +00:00
2018-07-21 22:16:05 +00:00
class CustomInvoiceForm(FormRevMixin, ModelForm):
"""
2018-07-21 22:16:05 +00:00
Form used to create a custom invoice.
"""
2018-07-21 22:16:05 +00:00
class Meta:
model = CustomInvoice
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.
"""
class Meta:
model = CostEstimate
exclude = ['paid', 'final_invoice']
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(),
2018-06-23 17:54:20 +00:00
label=_("Available 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(),
2018-06-23 17:54:20 +00:00
label=_("Available payment methods"),
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(),
2018-06-23 17:54:20 +00:00
label=_("Available 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
"""
value = forms.DecimalField(
2018-06-23 17:54:20 +00:00
label=_("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(),
2018-06-23 17:54:20 +00:00
label=_("Payment method")
)
2018-08-27 17:04:27 +00:00
def __init__(self, *args, user=None, user_source=None, **kwargs):
self.user = user
super(RechargeForm, self).__init__(*args, **kwargs)
self.fields['payment'].empty_label = \
_("Select a payment method")
2018-12-29 13:01:22 +00:00
self.fields['payment'].queryset = Paiement.find_allowed_payments(
user_source).exclude(is_balance=True)
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(
2019-01-08 23:38:53 +00:00
_("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