From d29de86c5d97ee253f29d390995a929d894515cb Mon Sep 17 00:00:00 2001 From: teb <85996160+Teebak@users.noreply.github.com> Date: Fri, 5 Aug 2022 02:51:59 +0300 Subject: [PATCH 1/2] task --- accounting/api/account.py | 29 ++------------------- accounting/models.py | 55 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 29 deletions(-) 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..e19a25a 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,36 @@ * 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) class AccountTypeChoices(models.TextChoices): ASSETS = 'ASSETS', 'Assets' @@ -49,7 +79,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 +91,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 From baae7f4a44d3af50443d9fd8c16bb36a0fc94e74 Mon Sep 17 00:00:00 2001 From: teb <85996160+Teebak@users.noreply.github.com> Date: Sun, 7 Aug 2022 23:04:38 +0300 Subject: [PATCH 2/2] task4 --- accounting/models.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/accounting/models.py b/accounting/models.py index e19a25a..00a1173 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -58,6 +58,40 @@ def __radd__(self,other): 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'