2020-02-18 20:30:26 +00:00
|
|
|
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
|
2019-07-16 10:43:23 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from django.db import models
|
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-02-18 20:14:29 +00:00
|
|
|
from .notes import Note, NoteClub
|
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
|
|
|
|
|
|
|
|
Example: food, softs, ...
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
associated to selling something (a burger, a beer, ...)
|
|
|
|
"""
|
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
|
|
|
)
|
|
|
|
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'),
|
|
|
|
)
|
|
|
|
amount = models.PositiveIntegerField(
|
|
|
|
verbose_name=_('amount'),
|
2019-07-17 09:17:50 +00:00
|
|
|
help_text=_('in centimes'),
|
2019-07-16 10:43:23 +00:00
|
|
|
)
|
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,
|
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-02-23 15:48:35 +00:00
|
|
|
display = models.BooleanField(
|
2020-03-07 21:28:59 +00:00
|
|
|
default=True,
|
2020-02-23 15:48:35 +00:00
|
|
|
)
|
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
|
|
|
|
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'),
|
|
|
|
)
|
|
|
|
destination = models.ForeignKey(
|
|
|
|
Note,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='+',
|
|
|
|
verbose_name=_('destination'),
|
|
|
|
)
|
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-02-18 20:14:29 +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
|
|
|
)
|
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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2019-07-19 13:00:44 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
When saving, also transfer money between two notes
|
|
|
|
"""
|
2020-02-27 16:22:59 +00:00
|
|
|
|
|
|
|
if self.source.pk == self.destination.pk:
|
|
|
|
# When source == destination, no money is transfered
|
2020-03-11 21:51:46 +00:00
|
|
|
super().save(*args, **kwargs)
|
2020-02-27 16:22:59 +00:00
|
|
|
return
|
|
|
|
|
2019-07-19 13:00:44 +00:00
|
|
|
created = self.pk is None
|
|
|
|
to_transfer = self.amount * self.quantity
|
|
|
|
if not created:
|
|
|
|
# Revert old transaction
|
|
|
|
old_transaction = Transaction.objects.get(pk=self.pk)
|
|
|
|
if old_transaction.valid:
|
|
|
|
self.source.balance += to_transfer
|
|
|
|
self.destination.balance -= to_transfer
|
|
|
|
|
|
|
|
if self.valid:
|
|
|
|
self.source.balance -= to_transfer
|
|
|
|
self.destination.balance += to_transfer
|
|
|
|
|
|
|
|
# Save notes
|
|
|
|
self.source.save()
|
|
|
|
self.destination.save()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
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-14 12:16:18 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
2020-03-14 12:25:54 +00:00
|
|
|
return _('transfer')
|
2020-03-14 12:16:18 +00:00
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
2020-02-23 16:27:55 +00:00
|
|
|
class TemplateTransaction(Transaction):
|
|
|
|
"""
|
|
|
|
Special type of :model:`note.Transaction` associated to a :model:`note.TransactionTemplate`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
template = models.ForeignKey(
|
|
|
|
TransactionTemplate,
|
2020-02-23 20:05:51 +00:00
|
|
|
null=True,
|
|
|
|
on_delete=models.SET_NULL,
|
2020-02-23 16:27:55 +00:00
|
|
|
)
|
|
|
|
category = models.ForeignKey(
|
|
|
|
TemplateCategory,
|
2020-02-23 20:05:51 +00:00
|
|
|
on_delete=models.PROTECT,
|
2020-02-23 16:27:55 +00:00
|
|
|
)
|
|
|
|
|
2020-03-14 12:16:18 +00:00
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return _('template')
|
|
|
|
|
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,
|
|
|
|
verbose_name=_("bank")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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-14 12:16:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return _('membership')
|