diff --git a/Balance.py b/Balance.py new file mode 100644 index 0000000..f323013 --- /dev/null +++ b/Balance.py @@ -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.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()) \ No newline at end of file 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 3cb73d5..caab5cb 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ