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-06-23 17:54:20 +00:00
|
|
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-07-16 19:46:13 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
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-12-31 22:58:37 +00:00
|
|
|
from .models import (
|
|
|
|
Article, Paiement, Facture, Banque,
|
|
|
|
CustomInvoice, Vente, CostEstimate
|
|
|
|
)
|
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):
|
|
|
|
user = kwargs['user']
|
2017-10-08 23:34:49 +00:00
|
|
|
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
|
2018-07-10 16:38:05 +00:00
|
|
|
super(FactureForm, 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-13 14:14:08 +00:00
|
|
|
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']}
|
2018-06-17 15:33:49 +00:00
|
|
|
|
2016-07-06 19:23:05 +00:00
|
|
|
class Meta:
|
|
|
|
model = Facture
|
2018-07-10 16:38:05 +00:00
|
|
|
fields = '__all__'
|
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()
|
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-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
|
|
|
"""
|
2017-10-13 03:24:57 +00:00
|
|
|
article = forms.ModelChoiceField(
|
2018-08-26 15:13:29 +00:00
|
|
|
queryset=Article.objects.none(),
|
2018-06-23 17:54:20 +00:00
|
|
|
label=_("Article"),
|
2017-10-28 04:02:53 +00:00
|
|
|
required=True
|
|
|
|
)
|
|
|
|
quantity = forms.IntegerField(
|
2018-06-23 17:54:20 +00:00
|
|
|
label=_("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-09-01 10:26:15 +00:00
|
|
|
target_user = kwargs.pop('target_user', None)
|
2018-08-26 15:13:29 +00:00
|
|
|
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(
|
|
|
|
label=_("Discount is on percentage"),
|
|
|
|
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-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
|
|
|
"""
|
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']
|
|
|
|
|
|
|
|
|
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-06-23 17:54:20 +00:00
|
|
|
label=_("Available 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-06-23 17:54:20 +00:00
|
|
|
label=_("Available payment methods"),
|
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-06-23 17:54:20 +00:00
|
|
|
label=_("Available 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-10-02 00:22:04 +00:00
|
|
|
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
|
|
|
)
|
2018-07-03 17:30:31 +00:00
|
|
|
payment = forms.ModelChoiceField(
|
|
|
|
queryset=Paiement.objects.none(),
|
2018-06-23 17:54:20 +00:00
|
|
|
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)
|
2018-07-03 17:30:31 +00:00
|
|
|
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)
|
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
|
|
|
|
"""
|
2018-01-14 19:15:21 +00:00
|
|
|
value = self.cleaned_data['value']
|
2018-07-16 21:17:09 +00:00
|
|
|
balance_method = get_object_or_404(balance.PaymentMethod)
|
2018-07-13 15:52:49 +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(
|
|
|
|
_("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-07-13 15:52:49 +00:00
|
|
|
return self.cleaned_data
|