diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..33bbdd2 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -1,7 +1,7 @@ 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.models import Account, AccountTypeChoices , JournalEntry from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut from typing import List from django.db.models import Sum, Avg @@ -51,7 +51,7 @@ def get_account_balances(request): result = [] for a in accounts: result.append({ - 'account': a.name, 'balance': list(a.balance()) + 'account': a.name, 'balance': list(a.sumbalance()) }) return status.HTTP_200_OK, result @@ -59,29 +59,4 @@ def get_account_balances(request): -class Balance: - def __init__(self, balances): - balance1 = balances[0] - balance2 = balances[1] - - if balance1['currency'] == 'USD': - balanceUSD = balance1['sum'] - balanceIQD = balance2['sum'] - else: - balanceIQD = balance1['sum'] - balanceUSD = balance2['sum'] - - self.balanceUSD = balanceUSD - self.balanceIQD = balanceIQD - - def __add__(self, other): - self.balanceIQD += other.balanceIQD - self.balanceUSD += other.balanceUSD - return [{ - 'currency': 'USD', - 'sum': self.balanceUSD - }, { - 'currency': 'IQD', - 'sum': self.balanceIQD - }] diff --git a/accounting/models.py b/accounting/models.py index a7d49b7..00a1173 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -3,6 +3,7 @@ from django.dispatch import receiver from django.db.models.signals import post_save from accounting.exceptions import AccountingEquationError +from accounting.api import Balance ''' @@ -27,7 +28,70 @@ * Each Transaction should consist of two or more even numbered Journal Entries ''' +class Balance: + def __init__(self, balances): + balanceIQD = 0 + balanceUSD = 0 + for i in balances: + if i['currency'] == 'USD': + balanceUSD = i['sum'] + if i['currency'] == 'IQD': + balanceIQD = i['sum'] + + self.balanceUSD = balanceUSD + self.balanceIQD = balanceIQD + + def __add__(self, other): + self.balanceIQD += other.balanceIQD + self.balanceUSD += other.balanceUSD + return [{ + 'currency': 'USD', + 'sum': self.balanceUSD + }, { + 'currency': 'IQD', + 'sum': self.balanceIQD + }] + + + def __radd__(self,other): + if other == 0: + return self + else: + return self.__add__(other) + + #TASK4 function1 + def __greatThan__ (self, other): + if self.balanceIQD > other.balanceIQD: + IQD = True + else: + IQD = False + if self.balanceUSD > other.balanceUSD: + USD = True + else: + USD = False + return {'IQD': IQD, 'USD': USD} + + #function2 + def __lessThan__ (self, other): + if self.balanceIQD < other.balanceIQD: + IQD = True + else: + IQD = False + if self.balanceUSD < other.balanceUSD: + USD = True + else: + USD = False + return {'IQD': IQD, 'USD': USD} + + + #function3 + def __iszero__(self): + if self.balanceIQD == 0 and self.balanceUSD==0: + return True + else: + return False + class AccountTypeChoices(models.TextChoices): ASSETS = 'ASSETS', 'Assets' @@ -49,7 +113,7 @@ class CurrencyChoices(models.TextChoices): class Account(models.Model): - parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) + parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, related_name='account_children') 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) @@ -61,6 +125,27 @@ def __str__(self): def balance(self): return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + + + + + def sumbalance(self): + + if self.parent is not None: + return self.balance() + else: + children = self.account_children.all() + if len(children) == 1: + return self.balance() + elif len(children) == 0: + return self.balance() + else: + sum_balance = [] + for childrens in list(children): + child_Balance = childrens.sumbalance() + sum_balance.append(Balance(child_Balance)) + + return sum(sum_balance) # def save( # self, force_insert=False, force_update=False, using=None, update_fields=None