From 293213b6d829ed10416ae1e9e1ac617d379c15d6 Mon Sep 17 00:00:00 2001 From: zhraa Date: Wed, 3 Aug 2022 13:56:38 +0300 Subject: [PATCH 1/3] made new changes --- accounting/api/account.py | 53 ++++++--------------------------------- accounting/models.py | 4 ++- accounting/services.py | 43 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..2b7fb81 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,18 @@ 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 - - - - -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 - }] - + return status.HTTP_200_OK,result \ No newline at end of file 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..640ee2b 100644 --- a/accounting/services.py +++ b/accounting/services.py @@ -32,4 +32,47 @@ 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 From 0b38c6b9ca7f6a81011789344b68f9a3675acc52 Mon Sep 17 00:00:00 2001 From: zhraa Date: Wed, 3 Aug 2022 14:07:43 +0300 Subject: [PATCH 2/3] made new changes --- accounting/services.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/accounting/services.py b/accounting/services.py index 640ee2b..86af426 100644 --- a/accounting/services.py +++ b/accounting/services.py @@ -38,17 +38,17 @@ def account_balance(account): balances=[] child_balance=[] account_balance=account.balance() - for a in child: - balance=a.balance() + for i in child: + balance=i.balance() child_balance.append(list(balance)) - for j in child_balance: - for n in j: - balances.append(n) + for f in child_balance: + for j in f: + balances.append(j) - for f in list(account_balance): - balances.append(f) + for n in list(account_balance): + balances.append(n) if child==[]: balances.append(account_balance) From 969a468712440f7844b89d9f315d4f040eb32ea7 Mon Sep 17 00:00:00 2001 From: zhraa Date: Mon, 8 Aug 2022 05:41:18 +0300 Subject: [PATCH 3/3] made new changes --- .vscode/launch.json | 13 ++++++++++++ accounting/api/account.py | 44 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6355088 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Dart & Flutter", + "request": "launch", + "type": "dart" + } + ] +} \ No newline at end of file diff --git a/accounting/api/account.py b/accounting/api/account.py index 2b7fb81..251a8ec 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -24,6 +24,9 @@ def get_one(request, account_id: int): return account except Account.DoesNotExist: return 404, {'detail': f'Account with id {account_id} does not exist'} + + + @account_router.get('/get_account_types/') @@ -47,4 +50,43 @@ def get_account_balances(request): result.append({ 'account':a.name ,'balance':final }) - return status.HTTP_200_OK,result \ No newline at end of file + 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 + }] + +def greater(self, other): + print(self.balanceUSD > other.balanceUSD,self.balanceIQD > other.balanceIQD) + +def lesser(self, other): + print(self.balanceUSD < other.balanceUSD,self.balanceIQD < other.balanceIQD) + + +def is_zero(self): + if self.balanceIQD == 0 and self.balanceUSD == 0: + return True + else: + return False \ No newline at end of file