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 e843e61..251a8ec 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']) @@ -27,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/') @@ -36,28 +36,21 @@ 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): @@ -84,4 +77,16 @@ def __add__(self, other): '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 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..86af426 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 i in child: + balance=i.balance() + child_balance.append(list(balance)) + + + for f in child_balance: + for j in f: + balances.append(j) + + for n in list(account_balance): + balances.append(n) + + 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