Use a proxy for special transactions in treasury app for modularity (not a clean way, but without any other solution...)

This commit is contained in:
Yohann D'ANELLO 2020-03-23 21:15:59 +01:00
parent 5fd472d408
commit 884a7d0f08
3 changed files with 24 additions and 14 deletions

View File

@ -3,12 +3,12 @@
from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser
from .transactions import MembershipTransaction, Transaction, \
TemplateCategory, TransactionTemplate, RecurrentTransaction
TemplateCategory, TransactionTemplate, RecurrentTransaction, SpecialTransaction
__all__ = [
# Notes
'Alias', 'Note', 'NoteClub', 'NoteSpecial', 'NoteUser',
# Transactions
'MembershipTransaction', 'Transaction', 'TemplateCategory', 'TransactionTemplate',
'RecurrentTransaction',
'RecurrentTransaction', 'SpecialTransaction',
]

View File

@ -6,7 +6,6 @@ from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from polymorphic.models import PolymorphicModel
from treasury.models import Remittance
from .notes import Note, NoteClub, NoteSpecial
@ -210,13 +209,6 @@ class SpecialTransaction(Transaction):
blank=True,
)
remittance = models.ForeignKey(
Remittance,
on_delete=models.PROTECT,
null=True,
verbose_name=_("Remittance"),
)
@property
def type(self):
return _('Credit') if isinstance(self.source, NoteSpecial) else _("Debit")

View File

@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
from note.models import NoteSpecial
from note.models import NoteSpecial, SpecialTransaction
class Invoice(models.Model):
@ -110,18 +110,36 @@ class Remittance(models.Model):
verbose_name=_("Closed"),
)
@property
def transactions(self):
return SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self)
@property
def size(self):
return self.specialtransaction_set.count()
return self.transactions.count()
@property
def amount(self):
return sum(transaction.total for transaction in self.specialtransaction_set.all())
return sum(transaction.total for transaction in self.transactions.all())
def full_clean(self, exclude=None, validate_unique=True):
ret = super().full_clean(exclude, validate_unique)
if self.specialtransaction_set.filter(~Q(source=self.type)).exists():
if self.transactions.filter(~Q(source=self.type)).exists():
raise ValidationError("All transactions in a remittance must have the same type")
return ret
class SpecialTransactionProxy(models.Model):
transaction = models.OneToOneField(
SpecialTransaction,
on_delete=models.CASCADE,
)
remittance = models.ForeignKey(
Remittance,
on_delete=models.PROTECT,
null=True,
verbose_name=_("Remittance"),
)