From 6a13408508f45cc4032b2098b01a9b7d7a015aaa Mon Sep 17 00:00:00 2001 From: mo7rex Date: Tue, 2 Aug 2022 17:32:33 +0300 Subject: [PATCH 1/3] first commit --- accounting/api/account.py | 38 ++++++++++++++++++++++++++++---------- accounting/models.py | 2 +- db.sqlite3 | Bin 159744 -> 159744 bytes 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..be52250 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -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: diff --git a/accounting/models.py b/accounting/models.py index a7d49b7..a78dc35 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') 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) diff --git a/db.sqlite3 b/db.sqlite3 index 3cb73d59be6da4a2a0517a8aae2d66cff6d9bafd..caab5c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BBMj_ix+(1105sKs)N@LW#f4v7H2XvBG5f7XxJB?5)4f{DJT?0f}$vv zz>Bp@4~0X-_{6@AJ*R0pO&W^(6*LHb@CgU6u+b3sR-E5;>upFA0^5Ce-`o=#Ti_a0 zw@al;KluA!#ILXt!3}mW$G+J$D;bISA~Q=#>6{KqzA9l)A_z%{-tw8%tTa31kzf)$ j!W92qKsVQ=Wl>cKI;a?dx(WA~D@XO}yfmV>HEH}G9Qt+$ From 190e72d3652d11c9d63e08f420c91b5e6a0fdaeb Mon Sep 17 00:00:00 2001 From: mo7rex Date: Sat, 6 Aug 2022 00:53:45 +0300 Subject: [PATCH 2/3] Task 4 sol --- Balance.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Balance.py diff --git a/Balance.py b/Balance.py new file mode 100644 index 0000000..b08e46f --- /dev/null +++ b/Balance.py @@ -0,0 +1,52 @@ +class Balance: + def __init__(self, amount1=None,cur1=None,amount2=None,cur2=None): + if amount1==None: + amount1=0 + cur1='' + if amount2==None: + amount2=0 + cur2='' + 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.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',1000,'IQD') +b=Balance(200,'USD',2000,'IQD') +print(a == b) +print(a < b) +print(a > b) +print(a.is_zero()) \ No newline at end of file From 3299522a650056f23b2c8d64925edde7cc081ab7 Mon Sep 17 00:00:00 2001 From: mo7rex Date: Sat, 6 Aug 2022 12:48:39 +0300 Subject: [PATCH 3/3] add some edits --- Balance.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Balance.py b/Balance.py index b08e46f..f323013 100644 --- a/Balance.py +++ b/Balance.py @@ -2,10 +2,13 @@ class Balance: def __init__(self, amount1=None,cur1=None,amount2=None,cur2=None): if amount1==None: amount1=0 - cur1='' - if amount2==None: + cur1='USD' + if amount2==None and cur1=='USD': amount2=0 - cur2='' + cur2='IQD' + elif amount2==None and cur1=='IQD': + amount2=0 + cur2='USD' self.amount1 = amount1 self.amount2 = amount2 self.cur1=cur1 @@ -44,7 +47,7 @@ def is_zero(self): return f'({a} , {b})' -a=Balance(200,'USD',1000,'IQD') +a=Balance(200,'USD') b=Balance(200,'USD',2000,'IQD') print(a == b) print(a < b)