From 8c0f44aa4cb8c02d1413f313f63a8777f60d9f2f Mon Sep 17 00:00:00 2001 From: Hassan Ali <108836804+hassanali19988@users.noreply.github.com> Date: Sun, 7 Aug 2022 23:45:11 +0300 Subject: [PATCH] Update models.py --- accounting/models.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/accounting/models.py b/accounting/models.py index a7d49b7..5e25fbb 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -47,7 +47,45 @@ class CurrencyChoices(models.TextChoices): USD = 'USD', 'USD' IQD = 'IQD', 'IQD' - +class Balance: + def __init__(self, balances): + balanceIQD = 0 + balanceUSD = 0 + for i in balances: + if i['currency'] == 'USD': + balanceUSD = i['sum'] + if i['currency'] == 'IQD': + balanceIQD = i['sum'] + + self.balanceUSD = balanceUSD + self.balanceIQD = balanceIQD + + def __add__(self, other): + self.balanceIQD += other.balanceIQD + self.balanceUSD += other.balanceUSD + return [{ + 'currency': 'IQD', + 'sum': self.balanceIQD + }, { + 'currency': 'USD', + 'sum': self.balanceUSD + }] + + def __gt__(self, other): + bIQD = self.balanceIQD > other.balanceIQD + bUSD = self.balanceUSD > other.balanceUSD + return bIQD, bUSD + + def __lt__(self, other): + bIQD = self.balanceIQD < other.balanceIQD + bUSD = self.balanceUSD < other.balanceUSD + return bIQD, bUSD + + def is_zero(self): + bIQD = True if self.balanceIQD == 0 else False + bUSD = True if self.balanceUSD == 0 else False + return bIQD, bUSD + class Account(models.Model): parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) type = models.CharField(max_length=255, choices=AccountTypeChoices.choices)