2021-06-14 19:45:36 +00:00
|
|
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
2019-07-16 10:43:23 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-08-15 17:10:23 +00:00
|
|
|
|
2020-05-29 19:43:24 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-08-05 17:42:44 +00:00
|
|
|
from django.db import models, transaction
|
2020-03-07 21:28:59 +00:00
|
|
|
from django.urls import reverse
|
2019-07-16 10:43:23 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-02-24 09:36:04 +00:00
|
|
|
from polymorphic.models import PolymorphicModel
|
2019-07-16 10:43:23 +00:00
|
|
|
|
2020-08-06 10:50:24 +00:00
|
|
|
from .notes import Note, NoteClub, NoteSpecial
|
2020-04-22 01:26:45 +00:00
|
|
|
from ..templatetags.pretty_money import pretty_money
|
2019-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Defines transactions
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-02-23 16:27:55 +00:00
|
|
|
class TemplateCategory(models.Model):
|
2020-02-04 00:18:03 +00:00
|
|
|
"""
|
|
|
|
Defined a recurrent transaction category
|
|
|
|
|
2022-08-29 09:17:17 +00:00
|
|
|
Example: food, softs,…
|
2020-02-04 00:18:03 +00:00
|
|
|
"""
|
|
|
|
name = models.CharField(
|
|
|
|
verbose_name=_("name"),
|
|
|
|
max_length=31,
|
|
|
|
unique=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("transaction category")
|
|
|
|
verbose_name_plural = _("transaction categories")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.name)
|
2019-07-16 10:43:23 +00:00
|
|
|
|
2020-02-18 20:14:29 +00:00
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
class TransactionTemplate(models.Model):
|
2020-01-21 21:06:06 +00:00
|
|
|
"""
|
2020-02-04 00:18:03 +00:00
|
|
|
Defined a recurrent transaction
|
2020-01-21 21:06:06 +00:00
|
|
|
|
2022-08-29 09:17:17 +00:00
|
|
|
associated to selling something (a burger, a beer,…)
|
2020-01-21 21:06:06 +00:00
|
|
|
"""
|
2019-07-16 10:43:23 +00:00
|
|
|
name = models.CharField(
|
|
|
|
verbose_name=_('name'),
|
|
|
|
max_length=255,
|
2019-08-13 16:23:15 +00:00
|
|
|
unique=True,
|
2020-03-07 21:28:59 +00:00
|
|
|
error_messages={'unique': _("A template with this name already exist")},
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
2020-04-01 18:56:24 +00:00
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
destination = models.ForeignKey(
|
2019-08-11 17:54:18 +00:00
|
|
|
NoteClub,
|
2019-07-16 10:43:23 +00:00
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='+', # no reverse
|
|
|
|
verbose_name=_('destination'),
|
|
|
|
)
|
2020-04-01 18:56:24 +00:00
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
amount = models.PositiveIntegerField(
|
|
|
|
verbose_name=_('amount'),
|
|
|
|
)
|
2020-02-23 15:48:35 +00:00
|
|
|
category = models.ForeignKey(
|
2020-02-23 16:27:55 +00:00
|
|
|
TemplateCategory,
|
2020-02-04 00:18:03 +00:00
|
|
|
on_delete=models.PROTECT,
|
2020-07-25 15:25:57 +00:00
|
|
|
related_name='templates',
|
2019-07-16 10:43:23 +00:00
|
|
|
verbose_name=_('type'),
|
2020-02-18 20:14:29 +00:00
|
|
|
max_length=31,
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
2020-04-01 18:56:24 +00:00
|
|
|
|
2020-02-23 15:48:35 +00:00
|
|
|
display = models.BooleanField(
|
2020-03-07 21:28:59 +00:00
|
|
|
default=True,
|
2020-04-01 18:56:24 +00:00
|
|
|
verbose_name=_("display"),
|
2020-02-23 15:48:35 +00:00
|
|
|
)
|
2020-04-01 18:56:24 +00:00
|
|
|
|
2020-07-25 15:25:57 +00:00
|
|
|
highlighted = models.BooleanField(
|
|
|
|
default=False,
|
|
|
|
verbose_name=_("highlighted"),
|
|
|
|
)
|
|
|
|
|
2020-02-22 15:31:01 +00:00
|
|
|
description = models.CharField(
|
|
|
|
verbose_name=_('description'),
|
|
|
|
max_length=255,
|
2020-03-14 18:00:20 +00:00
|
|
|
blank=True,
|
2020-02-22 15:31:01 +00:00
|
|
|
)
|
|
|
|
|
2019-07-16 11:50:05 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("transaction template")
|
|
|
|
verbose_name_plural = _("transaction templates")
|
|
|
|
|
2019-08-11 21:24:54 +00:00
|
|
|
def get_absolute_url(self):
|
2020-03-07 21:28:59 +00:00
|
|
|
return reverse('note:template_update', args=(self.pk,))
|
2019-08-11 21:24:54 +00:00
|
|
|
|
2020-09-06 23:06:22 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
2020-02-24 09:36:04 +00:00
|
|
|
class Transaction(PolymorphicModel):
|
2020-01-21 21:06:06 +00:00
|
|
|
"""
|
|
|
|
General transaction between two :model:`note.Note`
|
|
|
|
|
|
|
|
amount is store in centimes of currency, making it a positive integer
|
|
|
|
value. (from someone to someone else)
|
|
|
|
"""
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
source = models.ForeignKey(
|
|
|
|
Note,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='+',
|
|
|
|
verbose_name=_('source'),
|
|
|
|
)
|
2020-03-26 13:45:48 +00:00
|
|
|
|
|
|
|
source_alias = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
default="", # Will be remplaced by the name of the note on save
|
|
|
|
verbose_name=_('used alias'),
|
|
|
|
)
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
destination = models.ForeignKey(
|
|
|
|
Note,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='+',
|
|
|
|
verbose_name=_('destination'),
|
|
|
|
)
|
2020-03-26 13:45:48 +00:00
|
|
|
|
|
|
|
destination_alias = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
default="", # Will be remplaced by the name of the note on save
|
|
|
|
verbose_name=_('used alias'),
|
|
|
|
)
|
|
|
|
|
2019-07-17 10:48:03 +00:00
|
|
|
created_at = models.DateTimeField(
|
|
|
|
verbose_name=_('created at'),
|
2019-07-16 10:43:23 +00:00
|
|
|
default=timezone.now,
|
|
|
|
)
|
2019-07-19 08:12:46 +00:00
|
|
|
quantity = models.PositiveIntegerField(
|
2019-07-16 10:43:23 +00:00
|
|
|
verbose_name=_('quantity'),
|
2019-07-19 08:12:46 +00:00
|
|
|
default=1,
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
2020-03-16 11:11:16 +00:00
|
|
|
amount = models.PositiveIntegerField(
|
|
|
|
verbose_name=_('amount'),
|
|
|
|
)
|
|
|
|
|
2019-07-17 11:53:58 +00:00
|
|
|
reason = models.CharField(
|
|
|
|
verbose_name=_('reason'),
|
|
|
|
max_length=255,
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
2020-03-25 12:13:01 +00:00
|
|
|
|
2019-07-19 08:12:46 +00:00
|
|
|
valid = models.BooleanField(
|
2019-07-16 10:43:23 +00:00
|
|
|
verbose_name=_('valid'),
|
2019-07-19 08:12:46 +00:00
|
|
|
default=True,
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
|
|
|
|
2020-03-25 12:13:01 +00:00
|
|
|
invalidity_reason = models.CharField(
|
|
|
|
verbose_name=_('invalidity reason'),
|
|
|
|
max_length=255,
|
2020-09-06 23:06:22 +00:00
|
|
|
default='',
|
2020-04-01 01:42:19 +00:00
|
|
|
blank=True,
|
2020-03-25 12:13:01 +00:00
|
|
|
)
|
|
|
|
|
2019-07-16 11:50:05 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("transaction")
|
|
|
|
verbose_name_plural = _("transactions")
|
2020-03-11 12:46:47 +00:00
|
|
|
indexes = [
|
|
|
|
models.Index(fields=['created_at']),
|
|
|
|
models.Index(fields=['source']),
|
|
|
|
models.Index(fields=['destination']),
|
|
|
|
]
|
2019-07-16 11:50:05 +00:00
|
|
|
|
2020-08-09 10:31:06 +00:00
|
|
|
def validate(self):
|
2020-08-05 14:23:32 +00:00
|
|
|
previous_source_balance = self.source.balance
|
|
|
|
previous_dest_balance = self.destination.balance
|
|
|
|
|
2020-09-11 20:52:16 +00:00
|
|
|
source_balance = previous_source_balance
|
|
|
|
dest_balance = previous_dest_balance
|
2020-08-13 15:04:10 +00:00
|
|
|
|
2020-08-05 14:23:32 +00:00
|
|
|
created = self.pk is None
|
2020-09-11 20:52:16 +00:00
|
|
|
to_transfer = self.total
|
2020-09-14 07:05:35 +00:00
|
|
|
if not created:
|
2020-08-05 14:23:32 +00:00
|
|
|
# Revert old transaction
|
2020-09-14 07:05:35 +00:00
|
|
|
# We make a select for update to avoid concurrency issues
|
|
|
|
old_transaction = Transaction.objects.select_for_update().get(pk=self.pk)
|
2020-08-13 15:04:10 +00:00
|
|
|
# Check that nothing important changed
|
2020-09-11 20:52:16 +00:00
|
|
|
if not hasattr(self, "_force_save"):
|
|
|
|
for field_name in ["source_id", "destination_id", "quantity", "amount"]:
|
|
|
|
if getattr(self, field_name) != getattr(old_transaction, field_name):
|
|
|
|
raise ValidationError(_("You can't update the {field} on a Transaction. "
|
|
|
|
"Please invalidate it and create one other.").format(field=field_name))
|
2020-08-13 15:04:10 +00:00
|
|
|
|
|
|
|
if old_transaction.valid == self.valid:
|
|
|
|
# Don't change anything
|
|
|
|
return 0, 0
|
2020-08-05 14:23:32 +00:00
|
|
|
if old_transaction.valid:
|
2020-08-13 15:04:10 +00:00
|
|
|
source_balance += to_transfer
|
|
|
|
dest_balance -= to_transfer
|
2020-08-05 14:23:32 +00:00
|
|
|
|
|
|
|
if self.valid:
|
2020-08-13 15:04:10 +00:00
|
|
|
source_balance -= to_transfer
|
|
|
|
dest_balance += to_transfer
|
2020-08-05 14:23:32 +00:00
|
|
|
|
|
|
|
# When a transaction is declared valid, we ensure that the invalidity reason is null, if it was
|
|
|
|
# previously invalid
|
2020-09-06 23:06:22 +00:00
|
|
|
self.invalidity_reason = ""
|
2020-08-05 14:23:32 +00:00
|
|
|
|
2020-08-13 16:04:28 +00:00
|
|
|
if source_balance > 9223372036854775807 or source_balance < -9223372036854775808\
|
|
|
|
or dest_balance > 9223372036854775807 or dest_balance < -9223372036854775808:
|
|
|
|
raise ValidationError(_("The note balances must be between - 92 233 720 368 547 758.08 € "
|
|
|
|
"and 92 233 720 368 547 758.07 €."))
|
2020-08-05 14:23:32 +00:00
|
|
|
|
2020-08-09 10:31:06 +00:00
|
|
|
return source_balance - previous_source_balance, dest_balance - previous_dest_balance
|
|
|
|
|
2020-08-05 17:42:44 +00:00
|
|
|
@transaction.atomic
|
2020-03-19 01:26:06 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2019-07-19 13:00:44 +00:00
|
|
|
"""
|
|
|
|
When saving, also transfer money between two notes
|
|
|
|
"""
|
2020-08-15 16:57:44 +00:00
|
|
|
if self.source.pk == self.destination.pk:
|
|
|
|
# When source == destination, no money is transferred and no transaction is created
|
|
|
|
return
|
|
|
|
|
2020-10-04 18:47:15 +00:00
|
|
|
self.source = Note.objects.select_for_update().get(pk=self.source_id)
|
|
|
|
self.destination = Note.objects.select_for_update().get(pk=self.destination_id)
|
|
|
|
|
2020-08-15 16:57:44 +00:00
|
|
|
# Check that the amounts stay between big integer bounds
|
|
|
|
diff_source, diff_dest = self.validate()
|
|
|
|
|
2021-02-22 10:54:23 +00:00
|
|
|
if not (hasattr(self, '_force_save') and self._force_save) \
|
|
|
|
and (not self.source.is_active or not self.destination.is_active):
|
2020-08-31 18:15:48 +00:00
|
|
|
raise ValidationError(_("The transaction can't be saved since the source note "
|
|
|
|
"or the destination note is not active."))
|
2020-08-15 16:57:44 +00:00
|
|
|
|
|
|
|
# If the aliases are not entered, we assume that the used alias is the name of the note
|
|
|
|
if not self.source_alias:
|
|
|
|
self.source_alias = str(self.source)
|
|
|
|
|
|
|
|
if not self.destination_alias:
|
|
|
|
self.destination_alias = str(self.destination)
|
|
|
|
|
|
|
|
# We save first the transaction, in case of the user has no right to transfer money
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
# Save notes
|
2020-09-14 07:05:35 +00:00
|
|
|
self.source.refresh_from_db()
|
|
|
|
self.source.balance += diff_source
|
|
|
|
self.source._force_save = True
|
|
|
|
self.source.save()
|
|
|
|
self.destination.refresh_from_db()
|
|
|
|
self.destination.balance += diff_dest
|
|
|
|
self.destination._force_save = True
|
|
|
|
self.destination.save()
|
2019-07-19 13:00:44 +00:00
|
|
|
|
2019-08-15 21:08:15 +00:00
|
|
|
@property
|
|
|
|
def total(self):
|
2020-02-18 20:14:29 +00:00
|
|
|
return self.amount * self.quantity
|
2019-08-15 21:08:15 +00:00
|
|
|
|
2020-03-16 12:14:06 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return _('Transfer')
|
|
|
|
|
2020-04-22 01:26:45 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.__class__.__name__ + " from " + str(self.source) + " to " + str(self.destination) + " of "\
|
|
|
|
+ pretty_money(self.quantity * self.amount) + ("" if self.valid else " invalid")
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
2020-03-19 19:37:48 +00:00
|
|
|
class RecurrentTransaction(Transaction):
|
2020-02-23 16:27:55 +00:00
|
|
|
"""
|
|
|
|
Special type of :model:`note.Transaction` associated to a :model:`note.TransactionTemplate`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
template = models.ForeignKey(
|
|
|
|
TransactionTemplate,
|
2020-04-27 01:56:22 +00:00
|
|
|
on_delete=models.PROTECT,
|
2020-02-23 16:27:55 +00:00
|
|
|
)
|
2020-08-03 16:49:15 +00:00
|
|
|
|
2020-09-01 13:51:47 +00:00
|
|
|
def clean(self):
|
2021-02-22 10:54:23 +00:00
|
|
|
if self.template.destination != self.destination and not (hasattr(self, '_force_save') and self._force_save):
|
2020-09-01 13:51:47 +00:00
|
|
|
raise ValidationError(
|
|
|
|
_("The destination of this transaction must equal to the destination of the template."))
|
|
|
|
return super().clean()
|
|
|
|
|
2020-09-11 20:52:16 +00:00
|
|
|
@transaction.atomic
|
2020-09-01 13:51:47 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.clean()
|
|
|
|
return super().save(*args, **kwargs)
|
2020-02-23 16:27:55 +00:00
|
|
|
|
2020-03-16 12:14:06 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return _('Template')
|
|
|
|
|
2020-08-03 16:49:15 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("recurrent transaction")
|
|
|
|
verbose_name_plural = _("recurrent transactions")
|
|
|
|
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2020-03-14 14:13:58 +00:00
|
|
|
class SpecialTransaction(Transaction):
|
|
|
|
"""
|
|
|
|
Special type of :model:`note.Transaction` associated to transactions with special notes
|
|
|
|
"""
|
|
|
|
|
|
|
|
last_name = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
verbose_name=_("name"),
|
|
|
|
)
|
|
|
|
|
|
|
|
first_name = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
verbose_name=_("first_name"),
|
|
|
|
)
|
|
|
|
|
|
|
|
bank = models.CharField(
|
|
|
|
max_length=255,
|
2020-03-16 11:11:16 +00:00
|
|
|
verbose_name=_("bank"),
|
|
|
|
blank=True,
|
2020-03-14 14:13:58 +00:00
|
|
|
)
|
|
|
|
|
2020-03-16 12:14:06 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return _('Credit') if isinstance(self.source, NoteSpecial) else _("Debit")
|
|
|
|
|
2020-04-18 13:59:06 +00:00
|
|
|
def is_credit(self):
|
|
|
|
return isinstance(self.source, NoteSpecial)
|
|
|
|
|
|
|
|
def is_debit(self):
|
|
|
|
return isinstance(self.destination, NoteSpecial)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
# SpecialTransaction are only possible with NoteSpecial object
|
|
|
|
if self.is_credit() == self.is_debit():
|
2020-05-29 19:43:24 +00:00
|
|
|
raise(ValidationError(_("A special transaction is only possible between a"
|
|
|
|
" Note associated to a payment method and a User or a Club")))
|
2020-04-18 13:59:06 +00:00
|
|
|
|
2020-09-11 20:52:16 +00:00
|
|
|
@transaction.atomic
|
2020-09-01 13:51:47 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.clean()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2021-06-14 19:39:29 +00:00
|
|
|
@staticmethod
|
|
|
|
def validate_payment_form(form):
|
|
|
|
"""
|
|
|
|
Ensure that last name and first name are filled for a form that creates a SpecialTransaction,
|
|
|
|
and check that if the user pays with a check, then the bank field is filled.
|
|
|
|
|
|
|
|
Return True iff there is no error.
|
|
|
|
Whenever there is an error, they are inserted in the form errors.
|
|
|
|
"""
|
|
|
|
|
|
|
|
credit_type = form.cleaned_data["credit_type"]
|
|
|
|
last_name = form.cleaned_data["last_name"]
|
|
|
|
first_name = form.cleaned_data["first_name"]
|
|
|
|
bank = form.cleaned_data["bank"]
|
|
|
|
|
2021-06-14 20:15:35 +00:00
|
|
|
error = False
|
2021-06-14 19:39:29 +00:00
|
|
|
|
|
|
|
if not last_name or not first_name or (not bank and credit_type.special_type == "Chèque"):
|
|
|
|
if not last_name:
|
|
|
|
form.add_error('last_name', _("This field is required."))
|
|
|
|
error = True
|
|
|
|
if not first_name:
|
|
|
|
form.add_error('first_name', _("This field is required."))
|
|
|
|
error = True
|
|
|
|
if not bank and credit_type.special_type == "Chèque":
|
|
|
|
form.add_error('bank', _("This field is required."))
|
|
|
|
error = True
|
|
|
|
|
|
|
|
return not error
|
|
|
|
|
2020-08-03 16:49:15 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Special transaction")
|
|
|
|
verbose_name_plural = _("Special transactions")
|
|
|
|
|
2020-03-14 14:13:58 +00:00
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
class MembershipTransaction(Transaction):
|
2020-01-21 21:06:06 +00:00
|
|
|
"""
|
|
|
|
Special type of :model:`note.Transaction` associated to a :model:`member.Membership`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
membership = models.OneToOneField(
|
|
|
|
'member.Membership',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='transaction',
|
|
|
|
)
|
2019-07-16 11:50:05 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("membership transaction")
|
|
|
|
verbose_name_plural = _("membership transactions")
|
2020-03-16 12:14:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
2020-09-02 21:49:10 +00:00
|
|
|
return _('membership').capitalize()
|