mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-04 01:12:08 +01:00 
			
		
		
		
	Automatically link SpecialTransactions and their proxies
This commit is contained in:
		@@ -2,9 +2,24 @@
 | 
				
			|||||||
# SPDX-License-Identifier: GPL-3.0-or-later
 | 
					# SPDX-License-Identifier: GPL-3.0-or-later
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.apps import AppConfig
 | 
					from django.apps import AppConfig
 | 
				
			||||||
 | 
					from django.db.models.signals import post_save
 | 
				
			||||||
from django.utils.translation import gettext_lazy as _
 | 
					from django.utils.translation import gettext_lazy as _
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TreasuryConfig(AppConfig):
 | 
					class TreasuryConfig(AppConfig):
 | 
				
			||||||
    name = 'treasury'
 | 
					    name = 'treasury'
 | 
				
			||||||
    verbose_name = _('Treasury')
 | 
					    verbose_name = _('Treasury')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def ready(self):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Define app internal signals to interact with other apps
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        from . import signals
 | 
				
			||||||
 | 
					        from note.models import SpecialTransaction
 | 
				
			||||||
 | 
					        from treasury.models import SpecialTransactionProxy
 | 
				
			||||||
 | 
					        post_save.connect(signals.save_special_transaction, sender=SpecialTransaction)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # If the treasury app was disabled, we ensure that each special transaction is linked to a proxy
 | 
				
			||||||
 | 
					        for transaction in SpecialTransaction.objects.filter(specialtransactionproxy=None):
 | 
				
			||||||
 | 
					            SpecialTransactionProxy.objects.create(transaction=transaction, remittance=None)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,10 @@ from note.models import NoteSpecial, SpecialTransaction
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Invoice(models.Model):
 | 
					class Invoice(models.Model):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    An invoice model that can generate a true invoice
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    id = models.PositiveIntegerField(
 | 
					    id = models.PositiveIntegerField(
 | 
				
			||||||
        primary_key=True,
 | 
					        primary_key=True,
 | 
				
			||||||
        verbose_name=_("Invoice identifier"),
 | 
					        verbose_name=_("Invoice identifier"),
 | 
				
			||||||
@@ -57,6 +61,10 @@ class Invoice(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Product(models.Model):
 | 
					class Product(models.Model):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Product that appear on an invoice.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    invoice = models.ForeignKey(
 | 
					    invoice = models.ForeignKey(
 | 
				
			||||||
        Invoice,
 | 
					        Invoice,
 | 
				
			||||||
        on_delete=models.PROTECT,
 | 
					        on_delete=models.PROTECT,
 | 
				
			||||||
@@ -89,6 +97,10 @@ class Product(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Remittance(models.Model):
 | 
					class Remittance(models.Model):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Treasurers want to regroup checks or bank transfers in bank remittances.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    date = models.DateTimeField(
 | 
					    date = models.DateTimeField(
 | 
				
			||||||
        auto_now_add=True,
 | 
					        auto_now_add=True,
 | 
				
			||||||
        verbose_name=_("Date"),
 | 
					        verbose_name=_("Date"),
 | 
				
			||||||
@@ -132,6 +144,12 @@ class Remittance(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SpecialTransactionProxy(models.Model):
 | 
					class SpecialTransactionProxy(models.Model):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    In order to keep modularity, we don't that the Note app depends on the treasury app.
 | 
				
			||||||
 | 
					    That's why we create a proxy in this app, to link special transactions and remittances.
 | 
				
			||||||
 | 
					    If it isn't very clean, that makes what we want.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    transaction = models.OneToOneField(
 | 
					    transaction = models.OneToOneField(
 | 
				
			||||||
        SpecialTransaction,
 | 
					        SpecialTransaction,
 | 
				
			||||||
        on_delete=models.CASCADE,
 | 
					        on_delete=models.CASCADE,
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								apps/treasury/signals.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								apps/treasury/signals.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
 | 
				
			||||||
 | 
					# SPDX-License-Identifier: GPL-3.0-or-later
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from treasury.models import SpecialTransactionProxy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def save_special_transaction(instance, created, **kwargs):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    When a special transaction is created, we create its linked proxy
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    if created:
 | 
				
			||||||
 | 
					        SpecialTransactionProxy.objects.create(transaction=instance, remittance=None).save()
 | 
				
			||||||
		Reference in New Issue
	
	Block a user