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
51 changes: 25 additions & 26 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut
from typing import List
from django.db.models import Sum, Avg
from rest_framework import status
from http import HTTPStatus

from restauth.authorization import AuthBearer

Expand All @@ -14,7 +14,7 @@

@account_router.get("/get_all", response=List[AccountOut])
def get_all(request):
return status.HTTP_200_OK, Account.objects.order_by('full_code')
return HTTPStatus.OK, Account.objects.order_by('full_code')


@account_router.get('/get_one/{account_id}/', response={
Expand All @@ -34,33 +34,38 @@ def get_account_types(request):
return {t[0]: t[1] for t in AccountTypeChoices.choices}




@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)
account = Account.objects.get(id=account_id)
children_accounts = account.children.all()
account_balance = account.balance()
children_balances=[]
balances=[]

balance = account.balance()
for a in children_accounts:
balance = a.balance()
children_balances.append(list(balance))

journal_entries = account.journal_entries.all()
for a in children_balances:
for i in a:
balances.append(i)

return 200, {'account': account.name, 'balance': list(balance), 'jes': list(journal_entries)}
for a in list(account_balance):
balances.append(a)

if account.parent == None:
final_balance= Balance(balances)
else:
final_balance=list(children_balances)+list(account_balance)

@account_router.get('/account-balances/', response=List[GeneralLedgerOut])
def get_account_balances(request):
accounts = Account.objects.all()
result = []
for a in accounts:
result.append({
'account': a.name, 'balance': list(a.balance())
})
return HTTPStatus.OK, {'account': account.name, 'balance':final_balance}

return status.HTTP_200_OK, result


def Balance(balances):


class Balance:
def __init__(self, balances):
balance1 = balances[0]
balance2 = balances[1]

Expand All @@ -71,17 +76,11 @@ def __init__(self, balances):
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
'sum': balanceUSD
}, {
'currency': 'IQD',
'sum': self.balanceIQD
'sum': balanceIQD
}]

59 changes: 59 additions & 0 deletions accounting/api/task4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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
}]


#Task-4
def check1(self , other):
if self.balanceUSD > other.balanceUSD:
result_OF_USD = True
else:
result_OF_USD = False

if self.balanceIQD > other.balanceIQD:
result_OF_IQD = True
else:
result_OF_IQD = False

return {result_OF_USD , result_OF_IQD}

def check2(self , other):
if self.balanceUSD < other.balanceUSD:
result_OF_USD = True
else:
result_OF_USD = False

if self.balanceIQD < other.balanceIQD:
result_OF_IQD = True
else:
result_OF_IQD = False

return {result_OF_USD , result_OF_IQD}

def is_the_sklolo_zero(self):
if self.balanceIQD == 0 and self.balanceUSD == 0:
return True
else:
return False
2 changes: 1 addition & 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')
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