Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mohammed Ali - Task3 + Task4 #90

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
55 changes: 55 additions & 0 deletions Balance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Balance:
def __init__(self, amount1=None,cur1=None,amount2=None,cur2=None):
if amount1==None:
amount1=0
cur1='USD'
if amount2==None and cur1=='USD':
amount2=0
cur2='IQD'
elif amount2==None and cur1=='IQD':
amount2=0
cur2='USD'
self.amount1 = amount1
self.amount2 = amount2
self.cur1=cur1
self.cur2=cur2
def __eq__(self, other):
if self.cur1 != other.cur1:
return f'Error! Defrent Currency type'
if self.cur2 != other.cur2:
return f'Error! Defrent Currency type'
return f'({self.amount1 == other.amount1} , {self.amount2==other.amount2})'
def __lt__(self, other):
if self.cur1 != other.cur1:
return f'Error! Defrent Currency type'
if self.cur2 != other.cur2:
return f'Error! Defrent Currency type'
return f'({self.amount1 < other.amount1} , {self.amount2<other.amount2})'
def __gt__(self, other):
if self.cur1 != other.cur1:
return f'Error! Defrent Currency type'
if self.cur2 != other.cur2:
return f'Error! Defrent Currency type'
return f'({self.amount1 > other.amount1} , {self.amount2>other.amount2})'
def is_zero(self):
if self.amount1==0:
a=True
elif self.amount1!=0:
a=False
if self.amount2==0:
b=True
elif self.amount2!=0:
b=False
if a==True and b==True:
return True
elif a==False and b==False:
return False
return f'({a} , {b})'


a=Balance(200,'USD')
b=Balance(200,'USD',2000,'IQD')
print(a == b)
print(a < b)
print(a > b)
print(a.is_zero())
38 changes: 28 additions & 10 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,36 @@ def get_account_balance(request, account_id: int):
return 200, {'account': account.name, 'balance': list(balance), 'jes': list(journal_entries)}


@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 status.HTTP_200_OK, result


@account_router.get('/account-balances/')
def get_account_balances(request,account_id:int):
account=Account.objects.get(id=account_id)
final=Acc_bal(account)
return status.HTTP_200_OK,{'account':account.name ,'balance':final}

def Acc_bal(account):

children=account.children.all()
child_bal=[]
bal=[]
acc_bal=account.balance()
# if children.count() == 0:
# return account.balance()
for child in children:
child_bal.append(list(child.balance()))
for a in list(acc_bal):
bal.append(a)

for a in child_bal:
for b in a:
bal.append(b)
if account.parent == None:
total= Balance(bal)
else:
total= list(acc_bal)

return total


class Balance:
Expand Down
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
Binary file modified db.sqlite3
Binary file not shown.