Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
41 changes: 23 additions & 18 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
@@ -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'])

Expand All @@ -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/')
Expand All @@ -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):
Expand All @@ -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
4 changes: 3 additions & 1 deletion accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions accounting/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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