2019-07-16 10:43:23 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2019-08-11 17:54:18 +00:00
|
|
|
from .notes import Note,NoteClub
|
2019-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Defines transactions
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class TransactionTemplate(models.Model):
|
|
|
|
name = models.CharField(
|
|
|
|
verbose_name=_('name'),
|
|
|
|
max_length=255,
|
|
|
|
)
|
|
|
|
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
|
|
|
)
|
|
|
|
template_type = models.CharField(
|
|
|
|
verbose_name=_('type'),
|
|
|
|
max_length=31
|
|
|
|
)
|
|
|
|
|
2019-07-16 11:50:05 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("transaction template")
|
|
|
|
verbose_name_plural = _("transaction templates")
|
|
|
|
|
2019-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
class Transaction(models.Model):
|
|
|
|
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
|
|
|
)
|
|
|
|
amount = models.PositiveIntegerField(
|
|
|
|
verbose_name=_('amount'),
|
|
|
|
)
|
|
|
|
transaction_type = models.CharField(
|
|
|
|
verbose_name=_('type'),
|
|
|
|
max_length=31,
|
|
|
|
)
|
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")
|
|
|
|
|
2019-07-19 13:00:44 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
When saving, also transfer money between two notes
|
|
|
|
"""
|
|
|
|
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-07-16 10:43:23 +00:00
|
|
|
|
|
|
|
class MembershipTransaction(Transaction):
|
|
|
|
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")
|