2017-09-10 14:53:02 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
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-09 17:40:46 +00:00
|
|
|
The database models for the 'cotisation' app of re2o.
|
|
|
|
The goal is to keep the main actions here, i.e. the 'clean' and 'save'
|
|
|
|
function are higly reposnsible for the changes, checking the coherence of the
|
|
|
|
data and the good behaviour in general for not breaking the database.
|
2017-10-13 21:15:07 +00:00
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
For further details on each of those models, see the documentation details for
|
|
|
|
each.
|
2017-10-13 21:15:07 +00:00
|
|
|
"""
|
2017-01-15 23:01:18 +00:00
|
|
|
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-10-13 21:15:07 +00:00
|
|
|
from dateutil.relativedelta import relativedelta
|
2017-09-10 23:29:24 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
from django.db import models
|
2018-04-14 13:39:51 +00:00
|
|
|
from django.db.models import Q, Max
|
2016-07-25 22:13:38 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
|
|
|
from django.dispatch import receiver
|
2017-06-26 17:23:01 +00:00
|
|
|
from django.forms import ValidationError
|
2016-07-17 18:08:56 +00:00
|
|
|
from django.core.validators import MinValueValidator
|
2017-07-24 00:33:38 +00:00
|
|
|
from django.utils import timezone
|
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-06-21 18:03:46 +00:00
|
|
|
from django.urls import reverse
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.contrib import messages
|
2017-09-01 01:15:54 +00:00
|
|
|
|
2018-04-14 13:39:51 +00:00
|
|
|
from machines.models import regen
|
2017-12-28 01:08:02 +00:00
|
|
|
from re2o.field_permissions import FieldPermissionModelMixin
|
2018-03-31 15:18:39 +00:00
|
|
|
from re2o.mixins import AclMixin, RevMixin
|
2017-12-28 01:08:02 +00:00
|
|
|
|
2018-07-02 20:14:51 +00:00
|
|
|
from cotisations.utils import find_payment_method
|
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
2018-03-31 15:18:39 +00:00
|
|
|
class Facture(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The model for an invoice. It reprensents the fact that a user paid for
|
|
|
|
something (it can be multiple article paid at once).
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
An invoice is linked to :
|
|
|
|
* one or more purchases (one for each article sold that time)
|
|
|
|
* a user (the one who bought those articles)
|
|
|
|
* a payment method (the one used by the user)
|
|
|
|
* (if applicable) a bank
|
|
|
|
* (if applicable) a cheque number.
|
|
|
|
Every invoice is dated throught the 'date' value.
|
|
|
|
An invoice has a 'controlled' value (default : False) which means that
|
|
|
|
someone with high enough rights has controlled that invoice and taken it
|
|
|
|
into account. It also has a 'valid' value (default : True) which means
|
|
|
|
that someone with high enough rights has decided that this invoice was not
|
|
|
|
valid (thus it's like the user never paid for his articles). It may be
|
|
|
|
necessary in case of non-payment.
|
|
|
|
"""
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change paiement to payment
|
2016-07-02 13:58:50 +00:00
|
|
|
paiement = models.ForeignKey('Paiement', on_delete=models.PROTECT)
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change banque to bank
|
2017-10-13 02:07:56 +00:00
|
|
|
banque = models.ForeignKey(
|
2017-10-13 21:15:07 +00:00
|
|
|
'Banque',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
2018-03-31 13:43:46 +00:00
|
|
|
null=True
|
|
|
|
)
|
|
|
|
# TODO : maybe change to cheque nummber because not evident
|
|
|
|
cheque = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
blank=True,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Cheque number")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
date = models.DateTimeField(
|
|
|
|
auto_now_add=True,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Date")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
# TODO : change name to validity for clarity
|
|
|
|
valid = models.BooleanField(
|
|
|
|
default=True,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Validated")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
# TODO : changed name to controlled for clarity
|
|
|
|
control = models.BooleanField(
|
|
|
|
default=False,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Controlled")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2016-07-11 20:52:55 +00:00
|
|
|
|
2017-12-28 01:08:02 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = False
|
2017-12-30 23:23:59 +00:00
|
|
|
permissions = (
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
2018-04-13 18:58:29 +00:00
|
|
|
('change_facture_control',
|
2018-04-14 13:39:51 +00:00
|
|
|
_l("Can change the \"controlled\" state")),
|
2018-04-13 18:58:29 +00:00
|
|
|
# TODO : seems more likely to be call create_facture_pdf
|
|
|
|
# or create_invoice_pdf
|
|
|
|
('change_facture_pdf',
|
2018-04-14 13:39:51 +00:00
|
|
|
_l("Can create a custom PDF invoice")),
|
2018-04-13 18:58:29 +00:00
|
|
|
('view_facture',
|
2018-04-14 13:39:51 +00:00
|
|
|
_l("Can see an invoice's details")),
|
2018-04-13 18:58:29 +00:00
|
|
|
('change_all_facture',
|
2018-04-14 13:39:51 +00:00
|
|
|
_l("Can edit all the previous invoices")),
|
2017-12-30 23:23:59 +00:00
|
|
|
)
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name = _l("Invoice")
|
|
|
|
verbose_name_plural = _l("Invoices")
|
2017-12-28 01:08:02 +00:00
|
|
|
|
2018-04-02 01:52:15 +00:00
|
|
|
def linked_objects(self):
|
|
|
|
"""Return linked objects : machine and domain.
|
|
|
|
Usefull in history display"""
|
|
|
|
return self.vente_set.all()
|
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change prix to price
|
2016-07-11 20:52:55 +00:00
|
|
|
def prix(self):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Returns: the raw price without the quantities.
|
|
|
|
Deprecated, use :total_price instead.
|
|
|
|
"""
|
|
|
|
price = Vente.objects.filter(
|
2017-10-13 21:15:07 +00:00
|
|
|
facture=self
|
2018-07-02 12:41:02 +00:00
|
|
|
).aggregate(models.Sum('prix'))['prix__sum']
|
2018-04-09 17:40:46 +00:00
|
|
|
return price
|
2016-07-11 20:52:55 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change prix to price
|
2016-07-11 22:05:07 +00:00
|
|
|
def prix_total(self):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Returns: the total price for an invoice. Sum all the articles' prices
|
|
|
|
and take the quantities into account.
|
|
|
|
"""
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Vente to somethingelse
|
2017-10-13 02:07:56 +00:00
|
|
|
return Vente.objects.filter(
|
2017-10-13 21:15:07 +00:00
|
|
|
facture=self
|
2018-07-02 12:41:02 +00:00
|
|
|
).aggregate(
|
|
|
|
total=models.Sum(
|
|
|
|
models.F('prix')*models.F('number'),
|
|
|
|
output_field=models.FloatField()
|
|
|
|
)
|
|
|
|
)['total']
|
2016-07-11 22:05:07 +00:00
|
|
|
|
2016-07-11 20:52:55 +00:00
|
|
|
def name(self):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Returns : a string with the name of all the articles in the invoice.
|
|
|
|
Used for reprensenting the invoice with a string.
|
|
|
|
"""
|
2017-10-13 02:07:56 +00:00
|
|
|
name = ' - '.join(Vente.objects.filter(
|
|
|
|
facture=self
|
2018-07-02 12:41:02 +00:00
|
|
|
).values_list('name', flat=True))
|
2016-07-11 20:52:55 +00:00
|
|
|
return name
|
|
|
|
|
2017-12-08 22:59:20 +00:00
|
|
|
def can_edit(self, user_request, *args, **kwargs):
|
2017-12-31 19:13:41 +00:00
|
|
|
if not user_request.has_perm('cotisations.change_facture'):
|
2018-03-31 13:43:46 +00:00
|
|
|
return False, _("You don't have the right to edit an invoice.")
|
2018-04-13 18:58:29 +00:00
|
|
|
elif not user_request.has_perm('cotisations.change_all_facture') and \
|
|
|
|
not self.user.can_edit(user_request, *args, **kwargs)[0]:
|
|
|
|
return False, _("You don't have the right to edit this user's "
|
|
|
|
"invoices.")
|
|
|
|
elif not user_request.has_perm('cotisations.change_all_facture') and \
|
|
|
|
(self.control or not self.valid):
|
|
|
|
return False, _("You don't have the right to edit an invoice "
|
|
|
|
"already controlled or invalidated.")
|
2017-12-08 22:59:20 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
|
|
|
|
|
|
|
def can_delete(self, user_request, *args, **kwargs):
|
2017-12-31 19:13:41 +00:00
|
|
|
if not user_request.has_perm('cotisations.delete_facture'):
|
2018-03-31 13:43:46 +00:00
|
|
|
return False, _("You don't have the right to delete an invoice.")
|
2018-01-08 01:14:42 +00:00
|
|
|
if not self.user.can_edit(user_request, *args, **kwargs)[0]:
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to delete this user's "
|
|
|
|
"invoices.")
|
2017-12-08 22:59:20 +00:00
|
|
|
if self.control or not self.valid:
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to delete an invoice "
|
|
|
|
"already controlled or invalidated.")
|
2017-12-08 22:59:20 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
|
|
|
|
2018-04-14 13:39:51 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2018-04-13 18:58:29 +00:00
|
|
|
if not user_request.has_perm('cotisations.view_facture') and \
|
|
|
|
self.user != user_request:
|
|
|
|
return False, _("You don't have the right to see someone else's "
|
|
|
|
"invoices history.")
|
2017-12-27 20:09:00 +00:00
|
|
|
elif not self.valid:
|
2018-03-31 13:43:46 +00:00
|
|
|
return False, _("The invoice has been invalidated.")
|
2017-12-08 22:59:20 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
|
|
|
|
2017-12-29 18:43:44 +00:00
|
|
|
@staticmethod
|
2018-04-14 13:39:51 +00:00
|
|
|
def can_change_control(user_request, *_args, **_kwargs):
|
|
|
|
""" Returns True if the user can change the 'controlled' status of
|
|
|
|
this invoice """
|
2018-04-13 18:58:29 +00:00
|
|
|
return (
|
|
|
|
user_request.has_perm('cotisations.change_facture_control'),
|
|
|
|
_("You don't have the right to edit the \"controlled\" state.")
|
|
|
|
)
|
2017-12-27 20:09:00 +00:00
|
|
|
|
2017-12-29 18:43:44 +00:00
|
|
|
@staticmethod
|
2018-04-14 13:39:51 +00:00
|
|
|
def can_change_pdf(user_request, *_args, **_kwargs):
|
|
|
|
""" Returns True if the user can change this invoice """
|
2018-04-13 18:58:29 +00:00
|
|
|
return (
|
|
|
|
user_request.has_perm('cotisations.change_facture_pdf'),
|
|
|
|
_("You don't have the right to edit an invoice.")
|
|
|
|
)
|
2017-12-27 20:09:00 +00:00
|
|
|
|
2018-06-17 15:33:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_create(user_request, *_args, **_kwargs):
|
2018-07-03 11:58:10 +00:00
|
|
|
"""Check if a user can create an invoice.
|
2018-06-17 15:33:49 +00:00
|
|
|
|
|
|
|
:param user_request: The user who wants to create an invoice.
|
|
|
|
:return: a message and a boolean which is True if the user can create
|
|
|
|
an invoice or if the `options.allow_self_subscription` is set.
|
|
|
|
"""
|
2018-07-03 16:53:44 +00:00
|
|
|
nb_payments = len(Paiement.find_allowed_payments(user_request))
|
|
|
|
nb_articles = len(Article.find_allowed_articles(user_request))
|
2018-06-17 15:33:49 +00:00
|
|
|
return (
|
2018-07-03 16:53:44 +00:00
|
|
|
user_request.has_perm('cotisations.add_facture')
|
|
|
|
or (nb_payments*nb_articles),
|
2018-06-17 15:33:49 +00:00
|
|
|
_("You don't have the right to create an invoice.")
|
|
|
|
)
|
|
|
|
|
2018-01-08 22:57:19 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Facture, self).__init__(*args, **kwargs)
|
|
|
|
self.field_permissions = {
|
2018-04-13 18:58:29 +00:00
|
|
|
'control': self.can_change_control,
|
2018-01-08 22:57:19 +00:00
|
|
|
}
|
2017-12-28 01:08:02 +00:00
|
|
|
|
2016-07-11 20:52:55 +00:00
|
|
|
def __str__(self):
|
2017-01-08 12:41:01 +00:00
|
|
|
return str(self.user) + ' ' + str(self.date)
|
2016-07-11 20:52:55 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2016-07-25 22:13:38 +00:00
|
|
|
@receiver(post_save, sender=Facture)
|
2018-04-15 01:00:05 +00:00
|
|
|
def facture_post_save(**kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Synchronise the LDAP user after an invoice has been saved.
|
|
|
|
"""
|
2016-07-25 22:13:38 +00:00
|
|
|
facture = kwargs['instance']
|
|
|
|
user = facture.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
2016-07-25 22:13:38 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2016-07-25 22:13:38 +00:00
|
|
|
@receiver(post_delete, sender=Facture)
|
2018-04-15 01:00:05 +00:00
|
|
|
def facture_post_delete(**kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Synchronise the LDAP user after an invoice has been deleted.
|
|
|
|
"""
|
2016-07-25 22:13:38 +00:00
|
|
|
user = kwargs['instance'].user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
2016-07-25 22:13:38 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Vente to Purchase
|
2018-03-31 15:18:39 +00:00
|
|
|
class Vente(RevMixin, AclMixin, models.Model):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The model defining a purchase. It consist of one type of article being
|
|
|
|
sold. In particular there may be multiple purchases in a single invoice.
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
It's reprensentated by:
|
|
|
|
* an amount (the number of items sold)
|
|
|
|
* an invoice (whose the purchase is part of)
|
|
|
|
* an article
|
|
|
|
* (if applicable) a cotisation (which holds some informations about
|
|
|
|
the effect of the purchase on the time agreed for this user)
|
|
|
|
"""
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change this to English
|
2017-10-28 02:59:40 +00:00
|
|
|
COTISATION_TYPE = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('Connexion', _l("Connexion")),
|
|
|
|
('Adhesion', _l("Membership")),
|
|
|
|
('All', _l("Both of them")),
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change facture to invoice
|
|
|
|
facture = models.ForeignKey(
|
|
|
|
'Facture',
|
|
|
|
on_delete=models.CASCADE,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Invoice")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
# TODO : change number to amount for clarity
|
|
|
|
number = models.IntegerField(
|
|
|
|
validators=[MinValueValidator(1)],
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Amount")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
# TODO : change this field for a ForeinKey to Article
|
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Article")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
# TODO : change prix to price
|
|
|
|
# TODO : this field is not needed if you use Article ForeignKey
|
|
|
|
prix = models.DecimalField(
|
|
|
|
max_digits=5,
|
|
|
|
decimal_places=2,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Price"))
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : this field is not needed if you use Article ForeignKey
|
2017-10-15 18:37:21 +00:00
|
|
|
duration = models.PositiveIntegerField(
|
2017-10-13 21:15:07 +00:00
|
|
|
blank=True,
|
2018-03-31 13:43:46 +00:00
|
|
|
null=True,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Duration (in whole month)")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
# TODO : this field is not needed if you use Article ForeignKey
|
2017-10-28 02:59:40 +00:00
|
|
|
type_cotisation = models.CharField(
|
|
|
|
choices=COTISATION_TYPE,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2018-03-31 13:43:46 +00:00
|
|
|
max_length=255,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Type of cotisation")
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2017-12-30 23:23:59 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('view_vente', _l("Can see a purchase's details")),
|
|
|
|
('change_all_vente', _l("Can edit all the previous purchases")),
|
2017-12-30 23:23:59 +00:00
|
|
|
)
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name = _l("Purchase")
|
|
|
|
verbose_name_plural = _l("Purchases")
|
2017-12-30 23:23:59 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change prix_total to total_price
|
2016-07-14 11:55:46 +00:00
|
|
|
def prix_total(self):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Returns: the total of price for this amount of items.
|
|
|
|
"""
|
2016-07-14 11:55:46 +00:00
|
|
|
return self.prix*self.number
|
|
|
|
|
2017-07-24 00:33:38 +00:00
|
|
|
def update_cotisation(self):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Update the related object 'cotisation' if there is one. Based on the
|
|
|
|
duration of the purchase.
|
|
|
|
"""
|
2016-07-17 18:08:56 +00:00
|
|
|
if hasattr(self, 'cotisation'):
|
|
|
|
cotisation = self.cotisation
|
2017-10-13 02:07:56 +00:00
|
|
|
cotisation.date_end = cotisation.date_start + relativedelta(
|
2017-10-13 21:15:07 +00:00
|
|
|
months=self.duration*self.number)
|
2017-07-24 00:33:38 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def create_cotis(self, date_start=False):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Update and create a 'cotisation' related object if there is a
|
|
|
|
cotisation_type defined (which means the article sold represents
|
|
|
|
a cotisation)
|
|
|
|
"""
|
2017-10-28 02:59:40 +00:00
|
|
|
if not hasattr(self, 'cotisation') and self.type_cotisation:
|
2017-10-13 02:07:56 +00:00
|
|
|
cotisation = Cotisation(vente=self)
|
2017-10-28 02:59:40 +00:00
|
|
|
cotisation.type_cotisation = self.type_cotisation
|
2017-07-24 00:33:38 +00:00
|
|
|
if date_start:
|
2017-10-29 17:48:30 +00:00
|
|
|
end_cotisation = Cotisation.objects.filter(
|
2017-10-13 21:15:07 +00:00
|
|
|
vente__in=Vente.objects.filter(
|
|
|
|
facture__in=Facture.objects.filter(
|
|
|
|
user=self.facture.user
|
|
|
|
).exclude(valid=False))
|
2018-07-02 12:41:02 +00:00
|
|
|
).filter(
|
|
|
|
Q(type_cotisation='All') |
|
|
|
|
Q(type_cotisation=self.type_cotisation)
|
|
|
|
).filter(
|
|
|
|
date_start__lt=date_start
|
|
|
|
).aggregate(Max('date_end'))['date_end__max']
|
2018-04-13 18:58:29 +00:00
|
|
|
elif self.type_cotisation == "Adhesion":
|
2017-10-28 02:59:40 +00:00
|
|
|
end_cotisation = self.facture.user.end_adhesion()
|
2017-07-24 00:33:38 +00:00
|
|
|
else:
|
2017-10-28 02:59:40 +00:00
|
|
|
end_cotisation = self.facture.user.end_connexion()
|
2017-07-24 00:33:38 +00:00
|
|
|
date_start = date_start or timezone.now()
|
2017-10-28 02:59:40 +00:00
|
|
|
end_cotisation = end_cotisation or date_start
|
|
|
|
date_max = max(end_cotisation, date_start)
|
2017-07-24 00:33:38 +00:00
|
|
|
cotisation.date_start = date_max
|
2017-10-13 02:07:56 +00:00
|
|
|
cotisation.date_end = cotisation.date_start + relativedelta(
|
2017-10-13 21:15:07 +00:00
|
|
|
months=self.duration*self.number
|
2018-07-02 12:41:02 +00:00
|
|
|
)
|
2017-07-24 00:33:38 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Save a purchase object and check if all the fields are coherents
|
|
|
|
It also update the associated cotisation in the changes have some
|
|
|
|
effect on the user's cotisation
|
|
|
|
"""
|
|
|
|
# Checking that if a cotisation is specified, there is also a duration
|
2017-10-28 02:59:40 +00:00
|
|
|
if self.type_cotisation and not self.duration:
|
2018-03-31 13:43:46 +00:00
|
|
|
raise ValidationError(
|
|
|
|
_("A cotisation should always have a duration.")
|
|
|
|
)
|
2017-07-24 00:33:38 +00:00
|
|
|
self.update_cotisation()
|
|
|
|
super(Vente, self).save(*args, **kwargs)
|
2016-07-17 18:08:56 +00:00
|
|
|
|
2017-12-08 22:59:20 +00:00
|
|
|
def can_edit(self, user_request, *args, **kwargs):
|
2017-12-31 19:13:41 +00:00
|
|
|
if not user_request.has_perm('cotisations.change_vente'):
|
2018-03-31 15:23:34 +00:00
|
|
|
return False, _("You don't have the right to edit the purchases.")
|
2018-04-14 13:39:51 +00:00
|
|
|
elif (not user_request.has_perm('cotisations.change_all_facture') and
|
|
|
|
not self.facture.user.can_edit(
|
|
|
|
user_request, *args, **kwargs
|
2018-07-02 12:41:02 +00:00
|
|
|
)[0]):
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to edit this user's "
|
|
|
|
"purchases.")
|
2018-04-14 13:39:51 +00:00
|
|
|
elif (not user_request.has_perm('cotisations.change_all_vente') and
|
|
|
|
(self.facture.control or not self.facture.valid)):
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to edit a purchase "
|
|
|
|
"already controlled or invalidated.")
|
2017-12-27 20:09:00 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
2017-12-11 21:02:32 +00:00
|
|
|
|
2017-12-08 22:59:20 +00:00
|
|
|
def can_delete(self, user_request, *args, **kwargs):
|
2017-12-31 19:13:41 +00:00
|
|
|
if not user_request.has_perm('cotisations.delete_vente'):
|
2018-03-31 13:43:46 +00:00
|
|
|
return False, _("You don't have the right to delete a purchase.")
|
2018-01-08 01:14:42 +00:00
|
|
|
if not self.facture.user.can_edit(user_request, *args, **kwargs)[0]:
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to delete this user's "
|
|
|
|
"purchases.")
|
2017-12-27 20:09:00 +00:00
|
|
|
if self.facture.control or not self.facture.valid:
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to delete a purchase "
|
|
|
|
"already controlled or invalidated.")
|
2017-12-27 20:09:00 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
2017-12-08 22:59:20 +00:00
|
|
|
|
2018-04-14 13:39:51 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
|
|
|
if (not user_request.has_perm('cotisations.view_vente') and
|
|
|
|
self.facture.user != user_request):
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to see someone "
|
|
|
|
"else's purchase history.")
|
2017-12-27 20:09:00 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
2017-12-08 22:59:20 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
def __str__(self):
|
2016-07-11 20:52:55 +00:00
|
|
|
return str(self.name) + ' ' + str(self.facture)
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change vente to purchase
|
2016-07-25 22:13:38 +00:00
|
|
|
@receiver(post_save, sender=Vente)
|
2018-04-15 01:00:05 +00:00
|
|
|
def vente_post_save(**kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Creates a 'cotisation' related object if needed and synchronise the
|
|
|
|
LDAP user when a purchase has been saved.
|
|
|
|
"""
|
|
|
|
purchase = kwargs['instance']
|
2018-04-11 09:30:46 +00:00
|
|
|
if hasattr(purchase, 'cotisation'):
|
2018-04-09 17:40:46 +00:00
|
|
|
purchase.cotisation.vente = purchase
|
|
|
|
purchase.cotisation.save()
|
|
|
|
if purchase.type_cotisation:
|
|
|
|
purchase.create_cotis()
|
|
|
|
purchase.cotisation.save()
|
|
|
|
user = purchase.facture.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
2016-07-25 22:13:38 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change vente to purchase
|
2016-07-25 22:13:38 +00:00
|
|
|
@receiver(post_delete, sender=Vente)
|
2018-04-15 01:00:05 +00:00
|
|
|
def vente_post_delete(**kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Synchronise the LDAP user after a purchase has been deleted.
|
|
|
|
"""
|
|
|
|
purchase = kwargs['instance']
|
|
|
|
if purchase.type_cotisation:
|
|
|
|
user = purchase.facture.user
|
2016-11-20 15:53:59 +00:00
|
|
|
user.ldap_sync(base=False, access_refresh=True, mac_refresh=False)
|
2016-07-25 22:13:38 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Article(RevMixin, AclMixin, models.Model):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
2018-04-13 18:58:29 +00:00
|
|
|
The definition of an article model. It represents a type of object
|
|
|
|
that can be sold to the user.
|
|
|
|
|
2018-04-09 17:40:46 +00:00
|
|
|
It's represented by:
|
|
|
|
* a name
|
|
|
|
* a price
|
2018-04-13 18:58:29 +00:00
|
|
|
* a cotisation type (indicating if this article reprensents a
|
|
|
|
cotisation or not)
|
2018-04-09 17:40:46 +00:00
|
|
|
* a duration (if it is a cotisation)
|
|
|
|
* a type of user (indicating what kind of user can buy this article)
|
|
|
|
"""
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : Either use TYPE or TYPES in both choices but not both
|
2017-10-28 02:59:40 +00:00
|
|
|
USER_TYPES = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('Adherent', _l("Member")),
|
|
|
|
('Club', _l("Club")),
|
|
|
|
('All', _l("Both of them")),
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
COTISATION_TYPE = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('Connexion', _l("Connexion")),
|
|
|
|
('Adhesion', _l("Membership")),
|
|
|
|
('All', _l("Both of them")),
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Designation")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
# TODO : change prix to price
|
|
|
|
prix = models.DecimalField(
|
|
|
|
max_digits=5,
|
|
|
|
decimal_places=2,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Unitary price")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2017-10-15 18:37:21 +00:00
|
|
|
duration = models.PositiveIntegerField(
|
2017-07-18 21:57:57 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2018-03-31 13:43:46 +00:00
|
|
|
validators=[MinValueValidator(0)],
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Duration (in whole month)")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2017-10-28 02:59:40 +00:00
|
|
|
type_user = models.CharField(
|
|
|
|
choices=USER_TYPES,
|
|
|
|
default='All',
|
2018-03-31 13:43:46 +00:00
|
|
|
max_length=255,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Type of users concerned")
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
|
|
|
type_cotisation = models.CharField(
|
|
|
|
choices=COTISATION_TYPE,
|
|
|
|
default=None,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2018-03-31 13:43:46 +00:00
|
|
|
max_length=255,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Type of cotisation")
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
2018-07-03 16:53:44 +00:00
|
|
|
available_for_everyone = models.BooleanField(
|
2018-06-17 19:49:59 +00:00
|
|
|
default=False,
|
2018-07-03 16:53:44 +00:00
|
|
|
verbose_name=_l("Is available for every user")
|
2018-06-17 19:49:59 +00:00
|
|
|
)
|
2017-06-26 17:23:01 +00:00
|
|
|
|
2017-10-29 10:57:18 +00:00
|
|
|
unique_together = ('name', 'type_user')
|
|
|
|
|
2017-12-30 23:23:59 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('view_article', _l("Can see an article's details")),
|
2018-07-03 16:53:44 +00:00
|
|
|
('buy_every_article', _l("Can buy every_article"))
|
2017-12-30 23:23:59 +00:00
|
|
|
)
|
2018-03-31 13:43:46 +00:00
|
|
|
verbose_name = "Article"
|
|
|
|
verbose_name_plural = "Articles"
|
2017-12-30 23:23:59 +00:00
|
|
|
|
2017-06-26 17:23:01 +00:00
|
|
|
def clean(self):
|
2018-03-31 13:43:46 +00:00
|
|
|
if self.name.lower() == 'solde':
|
|
|
|
raise ValidationError(
|
|
|
|
_("Solde is a reserved article name")
|
|
|
|
)
|
2017-10-28 02:59:40 +00:00
|
|
|
if self.type_cotisation and not self.duration:
|
|
|
|
raise ValidationError(
|
2018-03-31 13:43:46 +00:00
|
|
|
_("Duration must be specified for a cotisation")
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
2017-06-26 17:23:01 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-07-03 16:53:44 +00:00
|
|
|
def can_buy_article(self, user, *_args, **_kwargs):
|
|
|
|
"""Check if a user can buy this article.
|
|
|
|
|
|
|
|
:param self: The article
|
|
|
|
:param user: The user requesting buying
|
|
|
|
:returns: A boolean stating if usage is granted and an explanation
|
|
|
|
message if the boolean is `False`.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
self.available_for_everyone
|
|
|
|
or user.has_perm('cotisations.buy_every_article'),
|
|
|
|
_("You cannot use this Payment.")
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def find_allowed_articles(cls, user):
|
|
|
|
return [p for p in cls.objects.all() if p.can_buy_article(user)[0]]
|
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Banque(RevMixin, AclMixin, models.Model):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The model defining a bank. It represents a user's bank. It's mainly used
|
|
|
|
for statistics by regrouping the user under their bank's name and avoid
|
|
|
|
the use of a simple name which leads (by experience) to duplicates that
|
|
|
|
only differs by a capital letter, a space, a misspelling, ... That's why
|
|
|
|
it's easier to use simple object for the banks.
|
|
|
|
"""
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=255,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Name")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2017-12-30 23:23:59 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('view_banque', _l("Can see a bank's details")),
|
2017-12-30 23:23:59 +00:00
|
|
|
)
|
2018-04-13 18:58:29 +00:00
|
|
|
verbose_name = _l("Bank")
|
|
|
|
verbose_name_plural = _l("Banks")
|
2017-12-30 23:23:59 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2018-07-05 13:21:51 +00:00
|
|
|
def check_no_balance():
|
|
|
|
"""This functions checks that no Paiement with is_balance=True exists
|
|
|
|
|
|
|
|
:raises ValidationError: if such a Paiement exists.
|
|
|
|
"""
|
|
|
|
p = Paiement.objects.filter(is_balance=True)
|
|
|
|
if len(p)>0:
|
|
|
|
raise ValidationError(
|
|
|
|
_("There are already payment method(s) for user balance")
|
|
|
|
)
|
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change Paiement to Payment
|
2018-03-31 15:18:39 +00:00
|
|
|
class Paiement(RevMixin, AclMixin, models.Model):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The model defining a payment method. It is how the user is paying for the
|
|
|
|
invoice. It's easier to know this information when doing the accouts.
|
|
|
|
It is represented by:
|
|
|
|
* a name
|
|
|
|
* a type (used for the type 'cheque' which implies the use of a bank
|
|
|
|
and an account number in related models)
|
|
|
|
"""
|
2018-04-13 18:58:29 +00:00
|
|
|
|
2017-07-18 21:57:57 +00:00
|
|
|
PAYMENT_TYPES = (
|
2018-03-31 15:32:26 +00:00
|
|
|
(0, _l("Standard")),
|
|
|
|
(1, _l("Cheque")),
|
2017-07-18 21:57:57 +00:00
|
|
|
)
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change moyen to method
|
|
|
|
moyen = models.CharField(
|
|
|
|
max_length=255,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Method")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
type_paiement = models.IntegerField(
|
|
|
|
choices=PAYMENT_TYPES,
|
|
|
|
default=0,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Payment type")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2018-07-03 16:53:44 +00:00
|
|
|
available_for_everyone = models.BooleanField(
|
2018-06-17 15:33:49 +00:00
|
|
|
default=False,
|
2018-07-03 16:53:44 +00:00
|
|
|
verbose_name=_l("Is available for every user")
|
2018-06-17 15:33:49 +00:00
|
|
|
)
|
2018-07-05 13:21:51 +00:00
|
|
|
is_balance = models.BooleanField(
|
|
|
|
default=False,
|
|
|
|
editable=False,
|
|
|
|
verbose_name=_l("Is user balance"),
|
|
|
|
help_text=_l("There should be only one balance payment method."),
|
|
|
|
validators=[check_no_balance]
|
|
|
|
)
|
2016-07-02 13:58:50 +00:00
|
|
|
|
2017-12-30 23:23:59 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('view_paiement', _l("Can see a payement's details")),
|
2018-07-03 16:53:44 +00:00
|
|
|
('use_every_payment', _l("Can use every payement")),
|
2017-12-30 23:23:59 +00:00
|
|
|
)
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name = _l("Payment method")
|
|
|
|
verbose_name_plural = _l("Payment methods")
|
2017-12-30 23:23:59 +00:00
|
|
|
|
2016-07-02 13:58:50 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.moyen
|
|
|
|
|
2017-06-26 17:23:01 +00:00
|
|
|
def clean(self):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Override of the herited clean function to get a correct name
|
|
|
|
"""
|
2017-06-26 17:23:01 +00:00
|
|
|
self.moyen = self.moyen.title()
|
|
|
|
|
2017-09-04 13:31:48 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Override of the herited save function to be sure only one payment
|
|
|
|
method of type 'cheque' exists.
|
|
|
|
"""
|
2017-09-04 13:31:48 +00:00
|
|
|
if Paiement.objects.filter(type_paiement=1).count() > 1:
|
2018-03-31 13:43:46 +00:00
|
|
|
raise ValidationError(
|
|
|
|
_("You cannot have multiple payment method of type cheque")
|
|
|
|
)
|
2017-09-04 13:31:48 +00:00
|
|
|
super(Paiement, self).save(*args, **kwargs)
|
|
|
|
|
2018-07-02 12:41:02 +00:00
|
|
|
def end_payment(self, invoice, request, use_payment_method=True):
|
2018-06-21 18:03:46 +00:00
|
|
|
"""
|
2018-07-02 12:41:02 +00:00
|
|
|
The general way of ending a payment.
|
|
|
|
|
|
|
|
:param invoice: The invoice being created.
|
|
|
|
:param request: Request sended by the user.
|
|
|
|
:param use_payment_method: If `self` has an attribute `payment_method`,
|
|
|
|
returns the result of
|
|
|
|
`self.payment_method.end_payment(invoice, request)`
|
|
|
|
|
|
|
|
:returns: An `HttpResponse`-like object.
|
2018-06-21 18:03:46 +00:00
|
|
|
"""
|
2018-07-02 20:14:51 +00:00
|
|
|
payment_method = find_payment_method(self)
|
|
|
|
if payment_method is not None and use_payment_method:
|
|
|
|
return payment_method.end_payment(invoice, request)
|
2018-06-21 18:03:46 +00:00
|
|
|
|
|
|
|
# In case a cotisation was bought, inform the user, the
|
|
|
|
# cotisation time has been extended too
|
|
|
|
if any(sell.type_cotisation for sell in invoice.vente_set.all()):
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The cotisation of %(member_name)s has been \
|
|
|
|
extended to %(end_date)s.") % {
|
|
|
|
'member_name': request.user.pseudo,
|
|
|
|
'end_date': request.user.end_adhesion()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
# Else, only tell the invoice was created
|
|
|
|
else:
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("The invoice has been created.")
|
|
|
|
)
|
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
2018-07-03 14:22:38 +00:00
|
|
|
kwargs={'userid': invoice.user.pk}
|
2018-06-21 18:03:46 +00:00
|
|
|
))
|
|
|
|
|
2018-07-03 16:53:44 +00:00
|
|
|
def can_use_payment(self, user, *_args, **_kwargs):
|
|
|
|
"""Check if a user can use this payment.
|
|
|
|
|
|
|
|
:param self: The payment
|
|
|
|
:param user: The user requesting usage
|
|
|
|
:returns: A boolean stating if usage is granted and an explanation
|
|
|
|
message if the boolean is `False`.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
self.available_for_everyone
|
|
|
|
or user.has_perm('cotisations.use_every_payment'),
|
|
|
|
_("You cannot use this Payment.")
|
|
|
|
)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def find_allowed_payments(cls, user):
|
|
|
|
return [p for p in cls.objects.all() if p.can_use_payment(user)[0]]
|
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Cotisation(RevMixin, AclMixin, models.Model):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
The model defining a cotisation. It holds information about the time a user
|
|
|
|
is allowed when he has paid something.
|
|
|
|
It characterised by :
|
|
|
|
* a date_start (the date when the cotisaiton begins/began
|
|
|
|
* a date_end (the date when the cotisation ends/ended
|
|
|
|
* a type of cotisation (which indicates the implication of such
|
|
|
|
cotisation)
|
|
|
|
* a purchase (the related objects this cotisation is linked to)
|
|
|
|
"""
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2017-10-28 02:59:40 +00:00
|
|
|
COTISATION_TYPE = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('Connexion', _l("Connexion")),
|
|
|
|
('Adhesion', _l("Membership")),
|
|
|
|
('All', _l("Both of them")),
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
|
|
|
|
2018-03-31 13:43:46 +00:00
|
|
|
# TODO : change vente to purchase
|
|
|
|
vente = models.OneToOneField(
|
|
|
|
'Vente',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Purchase")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
2017-10-28 02:59:40 +00:00
|
|
|
type_cotisation = models.CharField(
|
|
|
|
choices=COTISATION_TYPE,
|
|
|
|
max_length=255,
|
2018-03-02 23:17:42 +00:00
|
|
|
default='All',
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Type of cotisation")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
date_start = models.DateTimeField(
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Starting date")
|
2018-03-31 13:43:46 +00:00
|
|
|
)
|
|
|
|
date_end = models.DateTimeField(
|
2018-03-31 15:32:26 +00:00
|
|
|
verbose_name=_l("Ending date")
|
2017-10-28 02:59:40 +00:00
|
|
|
)
|
2016-07-02 16:30:59 +00:00
|
|
|
|
2017-12-30 23:23:59 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-03-31 15:32:26 +00:00
|
|
|
('view_cotisation', _l("Can see a cotisation's details")),
|
|
|
|
('change_all_cotisation', _l("Can edit the previous cotisations")),
|
2017-12-30 23:23:59 +00:00
|
|
|
)
|
|
|
|
|
2018-04-14 13:39:51 +00:00
|
|
|
def can_edit(self, user_request, *_args, **_kwargs):
|
2017-12-31 19:13:41 +00:00
|
|
|
if not user_request.has_perm('cotisations.change_cotisation'):
|
2018-03-31 13:43:46 +00:00
|
|
|
return False, _("You don't have the right to edit a cotisation.")
|
2018-04-13 18:58:29 +00:00
|
|
|
elif not user_request.has_perm('cotisations.change_all_cotisation') \
|
|
|
|
and (self.vente.facture.control or
|
|
|
|
not self.vente.facture.valid):
|
|
|
|
return False, _("You don't have the right to edit a cotisation "
|
|
|
|
"already controlled or invalidated.")
|
2017-12-27 20:09:00 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
2017-12-11 21:02:32 +00:00
|
|
|
|
2018-04-14 13:39:51 +00:00
|
|
|
def can_delete(self, user_request, *_args, **_kwargs):
|
2017-12-31 19:13:41 +00:00
|
|
|
if not user_request.has_perm('cotisations.delete_cotisation'):
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to delete a "
|
|
|
|
"cotisation.")
|
2017-12-27 20:09:00 +00:00
|
|
|
if self.vente.facture.control or not self.vente.facture.valid:
|
2018-04-13 18:58:29 +00:00
|
|
|
return False, _("You don't have the right to delete a cotisation "
|
|
|
|
"already controlled or invalidated.")
|
2017-12-27 20:09:00 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
2017-12-08 22:59:20 +00:00
|
|
|
|
2018-04-14 13:39:51 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2017-12-31 19:13:41 +00:00
|
|
|
if not user_request.has_perm('cotisations.view_cotisation') and\
|
2018-04-13 18:58:29 +00:00
|
|
|
self.vente.facture.user != user_request:
|
|
|
|
return False, _("You don't have the right to see someone else's "
|
|
|
|
"cotisation history.")
|
2017-12-27 20:09:00 +00:00
|
|
|
else:
|
|
|
|
return True, None
|
2017-12-08 22:59:20 +00:00
|
|
|
|
2016-07-02 16:30:59 +00:00
|
|
|
def __str__(self):
|
2016-07-17 18:08:56 +00:00
|
|
|
return str(self.vente)
|
2016-07-02 16:30:59 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2017-09-01 01:15:54 +00:00
|
|
|
@receiver(post_save, sender=Cotisation)
|
2018-04-15 01:00:05 +00:00
|
|
|
def cotisation_post_save(**_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Mark some services as needing a regeneration after the edition of a
|
|
|
|
cotisation. Indeed the membership status may have changed.
|
|
|
|
"""
|
2017-09-01 01:15:54 +00:00
|
|
|
regen('dns')
|
|
|
|
regen('dhcp')
|
|
|
|
regen('mac_ip_list')
|
2017-09-14 18:03:28 +00:00
|
|
|
regen('mailing')
|
2017-09-01 01:15:54 +00:00
|
|
|
|
2017-10-13 02:07:56 +00:00
|
|
|
|
2017-09-01 01:15:54 +00:00
|
|
|
@receiver(post_delete, sender=Cotisation)
|
2018-04-15 01:00:05 +00:00
|
|
|
def cotisation_post_delete(**_kwargs):
|
2018-04-09 17:40:46 +00:00
|
|
|
"""
|
|
|
|
Mark some services as needing a regeneration after the deletion of a
|
|
|
|
cotisation. Indeed the membership status may have changed.
|
|
|
|
"""
|
2017-09-01 20:21:51 +00:00
|
|
|
regen('mac_ip_list')
|
2017-09-14 18:03:28 +00:00
|
|
|
regen('mailing')
|