mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-04 01:12:08 +01:00 
			
		
		
		
	Transaction types are now properties
This commit is contained in:
		
				
					committed by
					
						
						Bombar Maxime
					
				
			
			
				
	
			
			
			
						parent
						
							7313e5a337
						
					
				
				
					commit
					4909a7f09c
				
			@@ -7,7 +7,7 @@ from django.utils import timezone
 | 
			
		||||
from django.utils.translation import gettext_lazy as _
 | 
			
		||||
from polymorphic.models import PolymorphicModel
 | 
			
		||||
 | 
			
		||||
from .notes import Note, NoteClub
 | 
			
		||||
from .notes import Note, NoteClub, NoteSpecial
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
Defines transactions
 | 
			
		||||
@@ -111,20 +111,6 @@ class Transaction(PolymorphicModel):
 | 
			
		||||
        verbose_name=_('amount'),
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    type = models.CharField(
 | 
			
		||||
        verbose_name=_('type'),
 | 
			
		||||
        choices=(
 | 
			
		||||
            ('gift', _('Gift')),
 | 
			
		||||
            ('transfer', _('Transfer')),
 | 
			
		||||
            ('template', _('Template')),
 | 
			
		||||
            ('credit', _('Credit')),
 | 
			
		||||
            ('debit', _('Debit')),
 | 
			
		||||
            ('membership', _('membership transaction')),
 | 
			
		||||
        ),
 | 
			
		||||
        default='transfer',
 | 
			
		||||
        max_length=10,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    reason = models.CharField(
 | 
			
		||||
        verbose_name=_('reason'),
 | 
			
		||||
        max_length=255,
 | 
			
		||||
@@ -175,6 +161,10 @@ class Transaction(PolymorphicModel):
 | 
			
		||||
    def total(self):
 | 
			
		||||
        return self.amount * self.quantity
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def type(self):
 | 
			
		||||
        return _('Transfer')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TemplateTransaction(Transaction):
 | 
			
		||||
    """
 | 
			
		||||
@@ -191,6 +181,10 @@ class TemplateTransaction(Transaction):
 | 
			
		||||
        on_delete=models.PROTECT,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def type(self):
 | 
			
		||||
        return _('Template')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SpecialTransaction(Transaction):
 | 
			
		||||
    """
 | 
			
		||||
@@ -213,6 +207,10 @@ class SpecialTransaction(Transaction):
 | 
			
		||||
        blank=True,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def type(self):
 | 
			
		||||
        return _('Credit') if isinstance(self.source, NoteSpecial) else _("Debit")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class MembershipTransaction(Transaction):
 | 
			
		||||
    """
 | 
			
		||||
@@ -229,3 +227,7 @@ class MembershipTransaction(Transaction):
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _("membership transaction")
 | 
			
		||||
        verbose_name_plural = _("membership transactions")
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def type(self):
 | 
			
		||||
        return _('membership transaction')
 | 
			
		||||
 
 | 
			
		||||
@@ -193,7 +193,6 @@ function consume(source, dest, quantity, amount, reason, type, category, templat
 | 
			
		||||
            "valid": true,
 | 
			
		||||
            "polymorphic_ctype": type,
 | 
			
		||||
            "resourcetype": "TemplateTransaction",
 | 
			
		||||
            "type": "template",
 | 
			
		||||
            "source": source,
 | 
			
		||||
            "destination": dest,
 | 
			
		||||
            "category": category,
 | 
			
		||||
 
 | 
			
		||||
@@ -201,8 +201,8 @@ SPDX-License-Identifier: GPL-2.0-or-later
 | 
			
		||||
            $("#type_transfer").removeAttr('checked');
 | 
			
		||||
            $("#type_credit").removeAttr('checked');
 | 
			
		||||
            $("#type_debit").removeAttr('checked');
 | 
			
		||||
            $("label[for='type_gift']").attr('class', 'btn btn-sm btn-outline-primary');
 | 
			
		||||
            $("label[for='type_transfer']").attr('class', 'btn btn-sm btn-outline-primary');
 | 
			
		||||
            $("label[for='type_credit']").attr('class', 'btn btn-sm btn-outline-primary');
 | 
			
		||||
            $("label[for='type_debit']").attr('class', 'btn btn-sm btn-outline-primary');
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
@@ -246,7 +246,6 @@ SPDX-License-Identifier: GPL-2.0-or-later
 | 
			
		||||
                            "valid": true,
 | 
			
		||||
                            "polymorphic_ctype": {{ polymorphic_ctype }},
 | 
			
		||||
                            "resourcetype": "Transaction",
 | 
			
		||||
                            "type": "gift",
 | 
			
		||||
                            "source": {{ user.note.id }},
 | 
			
		||||
                            "destination": dest.id
 | 
			
		||||
                        }, function () {
 | 
			
		||||
@@ -276,7 +275,6 @@ SPDX-License-Identifier: GPL-2.0-or-later
 | 
			
		||||
                                "valid": true,
 | 
			
		||||
                                "polymorphic_ctype": {{ polymorphic_ctype }},
 | 
			
		||||
                                "resourcetype": "Transaction",
 | 
			
		||||
                                "type": "transfer",
 | 
			
		||||
                                "source": source.id,
 | 
			
		||||
                                "destination": dest.id
 | 
			
		||||
                            }, function () {
 | 
			
		||||
@@ -298,11 +296,10 @@ SPDX-License-Identifier: GPL-2.0-or-later
 | 
			
		||||
                let special_note = $("#credit_type").val();
 | 
			
		||||
                let user_note = dests_notes_display[0].id;
 | 
			
		||||
                let given_reason = $("#reason").val();
 | 
			
		||||
                let source, dest, reason, type;
 | 
			
		||||
                let source, dest, reason;
 | 
			
		||||
                if ($("#type_credit").is(':checked')) {
 | 
			
		||||
                    source = special_note;
 | 
			
		||||
                    dest = user_note;
 | 
			
		||||
                    type = "credit";
 | 
			
		||||
                    reason = "Crédit " + $("#credit_type option:selected").text().toLowerCase();
 | 
			
		||||
                    if (given_reason.length > 0)
 | 
			
		||||
                        reason += " (" + given_reason + ")";
 | 
			
		||||
@@ -324,7 +321,6 @@ SPDX-License-Identifier: GPL-2.0-or-later
 | 
			
		||||
                        "valid": true,
 | 
			
		||||
                        "polymorphic_ctype": {{ special_polymorphic_ctype }},
 | 
			
		||||
                        "resourcetype": "SpecialTransaction",
 | 
			
		||||
                        "type": type,
 | 
			
		||||
                        "source": source,
 | 
			
		||||
                        "destination": dest,
 | 
			
		||||
                        "last_name": $("#last_name").val(),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user