mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-04 01:12:08 +01:00 
			
		
		
		
	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:
		@@ -3,12 +3,12 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser
 | 
					from .notes import Alias, Note, NoteClub, NoteSpecial, NoteUser
 | 
				
			||||||
from .transactions import MembershipTransaction, Transaction, \
 | 
					from .transactions import MembershipTransaction, Transaction, \
 | 
				
			||||||
    TemplateCategory, TransactionTemplate, RecurrentTransaction
 | 
					    TemplateCategory, TransactionTemplate, RecurrentTransaction, SpecialTransaction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__all__ = [
 | 
					__all__ = [
 | 
				
			||||||
    # Notes
 | 
					    # Notes
 | 
				
			||||||
    'Alias', 'Note', 'NoteClub', 'NoteSpecial', 'NoteUser',
 | 
					    'Alias', 'Note', 'NoteClub', 'NoteSpecial', 'NoteUser',
 | 
				
			||||||
    # Transactions
 | 
					    # Transactions
 | 
				
			||||||
    'MembershipTransaction', 'Transaction', 'TemplateCategory', 'TransactionTemplate',
 | 
					    'MembershipTransaction', 'Transaction', 'TemplateCategory', 'TransactionTemplate',
 | 
				
			||||||
    'RecurrentTransaction',
 | 
					    'RecurrentTransaction', 'SpecialTransaction',
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,6 @@ from django.urls import reverse
 | 
				
			|||||||
from django.utils import timezone
 | 
					from django.utils import timezone
 | 
				
			||||||
from django.utils.translation import gettext_lazy as _
 | 
					from django.utils.translation import gettext_lazy as _
 | 
				
			||||||
from polymorphic.models import PolymorphicModel
 | 
					from polymorphic.models import PolymorphicModel
 | 
				
			||||||
from treasury.models import Remittance
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .notes import Note, NoteClub, NoteSpecial
 | 
					from .notes import Note, NoteClub, NoteSpecial
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -210,13 +209,6 @@ class SpecialTransaction(Transaction):
 | 
				
			|||||||
        blank=True,
 | 
					        blank=True,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    remittance = models.ForeignKey(
 | 
					 | 
				
			||||||
        Remittance,
 | 
					 | 
				
			||||||
        on_delete=models.PROTECT,
 | 
					 | 
				
			||||||
        null=True,
 | 
					 | 
				
			||||||
        verbose_name=_("Remittance"),
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def type(self):
 | 
					    def type(self):
 | 
				
			||||||
        return _('Credit') if isinstance(self.source, NoteSpecial) else _("Debit")
 | 
					        return _('Credit') if isinstance(self.source, NoteSpecial) else _("Debit")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError
 | 
				
			|||||||
from django.db import models
 | 
					from django.db import models
 | 
				
			||||||
from django.db.models import Q
 | 
					from django.db.models import Q
 | 
				
			||||||
from django.utils.translation import gettext_lazy as _
 | 
					from django.utils.translation import gettext_lazy as _
 | 
				
			||||||
from note.models import NoteSpecial
 | 
					from note.models import NoteSpecial, SpecialTransaction
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Invoice(models.Model):
 | 
					class Invoice(models.Model):
 | 
				
			||||||
@@ -110,18 +110,36 @@ class Remittance(models.Model):
 | 
				
			|||||||
        verbose_name=_("Closed"),
 | 
					        verbose_name=_("Closed"),
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @property
 | 
				
			||||||
 | 
					    def transactions(self):
 | 
				
			||||||
 | 
					        return SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def size(self):
 | 
					    def size(self):
 | 
				
			||||||
        return self.specialtransaction_set.count()
 | 
					        return self.transactions.count()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def amount(self):
 | 
					    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):
 | 
					    def full_clean(self, exclude=None, validate_unique=True):
 | 
				
			||||||
        ret = super().full_clean(exclude, validate_unique)
 | 
					        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")
 | 
					            raise ValidationError("All transactions in a remittance must have the same type")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return ret
 | 
					        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"),
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user