diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..628ac00 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -1,5 +1,4 @@ from ninja import Router -from ninja.security import django_auth from django.shortcuts import get_object_or_404 from accounting.models import Account, AccountTypeChoices from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut diff --git a/accounting/api/task4.py b/accounting/api/task4.py new file mode 100644 index 0000000..0e3601f --- /dev/null +++ b/accounting/api/task4.py @@ -0,0 +1,60 @@ + +class Balance: + + def __init__(self, balances): + balance1 = balances[0] + balance2 = balances[1] + + if balance1['currency'] == 'USD': + balanceUSD = balance1['sum'] + balanceIQD = balance2['sum'] + else: + balanceUSD = balance2['sum'] + balanceIQD = balance1['sum'] + + self.balanceUSD = balanceUSD + self.balanceIQD = balanceIQD + + def __add__(self, other): + + self.balanceIQD += other.balanceIQD + self.balanceUSD += other.balanceUSD + result=[{ + 'currency': 'USD', + 'sum': self.balanceUSD + }, { + 'currency': 'IQD', + 'sum': self.balanceIQD + }] + + return result +#Old + def __Othan__ (self, other): + if self.balanceIQD > other.balanceIQD: + theIQD = True + else: + theIQD = False + if self.balanceUSD > other.balanceUSD: + theUSD = True + else: + theUSD = False + return {'BalanceIQD': theIQD, 'BalanceUSD': theUSD} + + #Younger + def __Ythan__ (self, other): + if self.balanceIQD < other.balanceIQD: + theIQD = True + else: + theIQD = False + if self.balanceUSD < other.balanceUSD: + theUSD = True + else: + theUSD = False + return {'BalanceIQD': theIQD, 'BalanceUSD': theUSD} + + # zero + def __isZero__(self): + if self.balanceIQD == 0 and self.balanceUSD==0: + return True + else: + return False diff --git a/accounting/models.py b/accounting/models.py index a7d49b7..e081e39 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -1,7 +1,6 @@ from django.db import models from django.db.models import Sum -from django.dispatch import receiver -from django.db.models.signals import post_save +from mptt.models import MPTTModel, TreeForeignKey from accounting.exceptions import AccountingEquationError ''' @@ -48,19 +47,26 @@ class CurrencyChoices(models.TextChoices): IQD = 'IQD', 'IQD' -class Account(models.Model): - parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) +class Account(MPTTModel): + parent = TreeForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) type = models.CharField(max_length=255, choices=AccountTypeChoices.choices) name = models.CharField(max_length=255) code = models.CharField(max_length=20, null=True, blank=True) full_code = models.CharField(max_length=25, null=True, blank=True) extra = models.JSONField(default=dict, null=True, blank=True) + class MPTTMeta: + order_insertion_by = ['full_code'] + def __str__(self): return f'{self.full_code} - {self.name}' def balance(self): - return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + result = [ + account.ournal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + for account in self.get_descendants(include_self=True) + ] + return result # def save( # self, force_insert=False, force_update=False, using=None, update_fields=None @@ -89,7 +95,7 @@ def balance(self): # instance.full_code = f'{instance.id}' -class Transaction(models.Model): +class Transaction(MPTTModel): type = models.CharField(max_length=255, choices=TransactionTypeChoices.choices) description = models.CharField(max_length=255) @@ -100,11 +106,14 @@ def validate_accounting_equation(self): raise AccountingEquationError -class JournalEntry(models.Model): +class JournalEntry(MPTTModel): class Meta: verbose_name_plural = 'Journal Entries' - account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='journal_entries') + class MPTTMeta: + order_insertion_by = ['full_code'] + + account = TreeForeignKey(Account, on_delete=models.CASCADE, related_name='journal_entries') transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='journal_entries') amount = models.DecimalField(max_digits=19, decimal_places=2) currency = models.CharField(max_length=3, choices=CurrencyChoices.choices) diff --git a/config/settings.py b/config/settings.py index ba49e84..e9ed4e2 100644 --- a/config/settings.py +++ b/config/settings.py @@ -31,6 +31,7 @@ INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', + 'mptt', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages',