diff --git a/accounting/Task4.py b/accounting/Task4.py new file mode 100644 index 0000000..906b01a --- /dev/null +++ b/accounting/Task4.py @@ -0,0 +1,111 @@ + +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 + }] + + def __gt__(self, other): + + if self.balanceUSD > other.balanceUSD : + StaUSD = True + else: + StaUSD = False + if self.balanceIQD > other.balanceIQD : + StaIQD = True + else: + StaIQD = False + return {'USD':StaUSD},{'IQD':StaIQD} + + + def __lt__(self,other): + + if self.balanceUSD < other.balanceUSD : + StaUSD = True + else: + StaUSD = False + if self.balanceIQD < other.balanceIQD : + StaIQD = True + else: + StaIQD = False + return {'USD':StaUSD},{'IQD':StaIQD} + + def is_zero(self): + if self.balanceIQD == 0 and self.balanceUSD == 0 : + return True + else: + return False + + def is_zero(self): + if self.balanceIQD == 0 and self.balanceUSD == 0 : + return True + else: + return False + #Here Is The Test <: + +list1=[{ + 'currency': 'USD', + 'sum': 500 + }, { + 'currency': 'IQD', + 'sum': 0 + }] +list2=[{ + 'currency': 'USD', + 'sum': 400 + },{ + 'currency': 'IQD', + 'sum': 2000020 + }] +Obj1 = Balance(list1) +Obj2 = Balance(list2) + +print('Obj1 > Obj2 =>',Obj1 > Obj2) #OUTPUT:Obj1 > Obj2 => ({'USD': True}, {'IQD': False}) +print('Obj2 > Obj1 =>',Obj2 > Obj1) #OUTPUT:Obj2 > Obj1 => ({'USD': False}, {'IQD': True}) + +print('Obj1 < Obj2 =>',Obj1 < Obj2) #OUTPUT:Obj1 < Obj2 => ({'USD': False}, {'IQD': True}) +print('Obj2 < Obj1 =>',Obj2 < Obj1) #OUTPUT:Obj2 < Obj1 => ({'USD': True}, {'IQD': False}) + +print('Obj1 ==0 =>',Obj1.is_zero()) #OUTPUT:Obj1 ==0 => False +print('Obj2 ==0 =>',Obj2.is_zero()) #OUTPUT:Obj2 ==0 => False + +'''''''''' + #Another Sol For Task4,From Internet + def __gt__(self, other): + if (isinstance(other,Balance)): + StaUSD = self.balanceUSD > other.balanceIQD + StaIQD = self.balanceIQD > other.balanceUSD + return {'USD':StaUSD},{'IQD':StaIQD} + + + + def __lt__(self, other): + if (isinstance(other,Balance)): + StaUSD = self.balanceUSD < other.balanceIQD + StaIQD = self.balanceIQD < other.balanceUSD + return {'USD':StaUSD},{'IQD':StaIQD} + + def is_zero(self): + return self.balanceIQD == 0 and self.balanceUSD == 0 + + ''''''''' diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..a56db1a 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -1,13 +1,10 @@ from ninja import Router -from ninja.security import django_auth -from django.shortcuts import get_object_or_404 +from accounting import services from accounting.models import Account, AccountTypeChoices from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut from typing import List -from django.db.models import Sum, Avg from rest_framework import status -from restauth.authorization import AuthBearer account_router = Router(tags=['account']) @@ -36,52 +33,20 @@ def get_account_types(request): @account_router.get('/account-balance/{account_id}', response=GeneralLedgerOut) def get_account_balance(request, account_id: int): - account = get_object_or_404(Account, id=account_id) - - balance = account.balance() - - journal_entries = account.journal_entries.all() - - return 200, {'account': account.name, 'balance': list(balance), 'jes': list(journal_entries)} + account=Account.objects.get(id=account_id) + final=services.account_balance(account) + return status.HTTP_200_OK,{'account':account.name ,'balance':final} @account_router.get('/account-balances/', response=List[GeneralLedgerOut]) def get_account_balances(request): accounts = Account.objects.all() - result = [] + result=[] for a in accounts: + final=services.account_balance(a) result.append({ - 'account': a.name, 'balance': list(a.balance()) + 'account':a.name ,'balance':final }) + return status.HTTP_200_OK,result - return status.HTTP_200_OK, result - - - - -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..ed1292e 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -49,7 +49,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='children_account') 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) @@ -99,6 +99,8 @@ def validate_accounting_equation(self): if transaction_sum != 0: raise AccountingEquationError + def __str__(self): + return f'{self.type} - {self.description}' class JournalEntry(models.Model): class Meta: diff --git a/accounting/services.py b/accounting/services.py index 7ecff81..5fc0cf7 100644 --- a/accounting/services.py +++ b/accounting/services.py @@ -1,6 +1,5 @@ from django.db import transaction as db_transaction from rest_framework import status -from accounting.exceptions import AtomicAccountTransferException, ZeroAmountError, AccountingEquationError from accounting.models import Transaction, JournalEntry @@ -32,4 +31,46 @@ def account_transfer(data): # t.delete() # return status.HTTP_400_BAD_REQUEST, {'detail': 'transaction is not valid'} return t + +def account_balance(account): + child=account.children_account.all() + balances=[] + child_balance=[] + account_balance=account.balance() + for a in child: + balance=a.balance() + child_balance.append(list(balance)) + + for j in child_balance: + for n in j: + balances.append(n) + + for f in list(account_balance): + balances.append(f) + + if child==[]: + balances.append(account_balance) + final=GiveFinalBalance(balances) + else: + final= GiveFinalBalance(balances) + + print(balances) + return final + +def GiveFinalBalance(balancs): + balanceUSD=0 + balanceIQD=0 + + for balance in balancs: + if balance["currency"]=="USD": + balanceUSD += balance["sum"] + else: + balanceIQD += balance["sum"] + + final=[{ + 'currency': 'USD', + 'sum': balanceUSD}, { + 'currency': 'IQD', + 'sum': balanceIQD}] + return final diff --git a/db.sqlite3 b/db.sqlite3 index 3cb73d5..bf20b40 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ