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
111 changes: 111 additions & 0 deletions accounting/Task4.py
Original file line number Diff line number Diff line change
@@ -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

'''''''''
51 changes: 8 additions & 43 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 Down Expand Up @@ -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
}]

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: 42 additions & 1 deletion accounting/services.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Binary file modified db.sqlite3
Binary file not shown.