From d59a170c1095b50752eeda3da5c14b33daf60c6b Mon Sep 17 00:00:00 2001 From: Sura Atta <96949252+SuraAtta@users.noreply.github.com> Date: Mon, 1 Aug 2022 01:36:17 +0300 Subject: [PATCH 1/4] task3 --- accounting/api/account.py | 43 ++++------------ .../migrations/0007_alter_account_parent.py | 19 ++++++++ accounting/models.py | 46 +++++++++++++++++- db.sqlite3 | Bin 159744 -> 159744 bytes 4 files changed, 73 insertions(+), 35 deletions(-) create mode 100644 accounting/migrations/0007_alter_account_parent.py diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..5109c4c 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -49,39 +49,14 @@ def get_account_balance(request, account_id: int): 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 - - - - -class Balance: - def __init__(self, balances): - balance1 = balances[0] - balance2 = balances[1] - - if balance1['currency'] == 'USD': - balanceUSD = balance1['sum'] - balanceIQD = balance2['sum'] + for a in accounts.all(): + if a.parent == None: + total = a.TBalance() + result.append({ + 'account': a.name, 'balance': list(total) + }) + 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 - }] + total = a.balance() + return status.HTTP_200_OK, result diff --git a/accounting/migrations/0007_alter_account_parent.py b/accounting/migrations/0007_alter_account_parent.py new file mode 100644 index 0000000..2873aff --- /dev/null +++ b/accounting/migrations/0007_alter_account_parent.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0.6 on 2022-07-31 22:31 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounting', '0006_alter_account_code_alter_account_full_code_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='account', + name='parent', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='accounting.account'), + ), + ] diff --git a/accounting/models.py b/accounting/models.py index a7d49b7..fe4be5b 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) @@ -61,6 +61,50 @@ def __str__(self): def balance(self): return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + + def TBalance(self): + Accbalance = Balance(self.balance()) + children = self.children.all() + total = [] + for b in children: + ch_balance=Balance(b.balance()) + total = Accbalance.__add__(ch_balance) + if children.count() == 0: + return self.balance() + return total + +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': 'USD', + 'sum': self.balanceUSD + }, { + 'currency': 'IQD', + 'sum': self.balanceIQD + }] + def __radd__(self, other): + if other == 0: + return self + else: + return self.__add__(other) # def save( # self, force_insert=False, force_update=False, using=None, update_fields=None diff --git a/db.sqlite3 b/db.sqlite3 index 3cb73d59be6da4a2a0517a8aae2d66cff6d9bafd..b72cf7a2cc9404e4260ad44b5b154ef547fdb04c 100644 GIT binary patch delta 1320 zcmai!ZA@EL7{|}Om%a_?18kiGd?{mw5886y+Iw%8xfKeu7uo{r3Tz=w-=HnDxAcWV zabY^M=qGed#@89Aep!4;vk=XUCi-cT48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X7DAe=!2h_~sTu`7({6cP|*~0Dave#V}v4OCO z2H_Vr(Zv_E&~PLETT~j*2AOJY@Bb6)SmRbLb}qsS=$0_Ii~9B_9yVo;@ES8XCb#6g z%<*kbOD~two{F7Xs77*?3sI8sYRIc)w2^2~gGW5J$1^@d#mj5w7+N6SWq~0NfqnW( OQK=o*_EbJQ+Wx;^7^NZr From 09e3052d69b331d48516f4368503d4514ab9ceda Mon Sep 17 00:00:00 2001 From: Sura Atta <96949252+SuraAtta@users.noreply.github.com> Date: Mon, 1 Aug 2022 01:39:17 +0300 Subject: [PATCH 2/4] task3 --- accounting/api/account.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index 5109c4c..7657649 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -55,8 +55,6 @@ def get_account_balances(request): result.append({ 'account': a.name, 'balance': list(total) }) - else: total = a.balance() - return status.HTTP_200_OK, result From 4e6f93748c9bc52d69e65f5790238e1dfa399b6f Mon Sep 17 00:00:00 2001 From: Sura Atta <96949252+SuraAtta@users.noreply.github.com> Date: Mon, 1 Aug 2022 01:40:03 +0300 Subject: [PATCH 3/4] task3 --- accounting/api/account.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index 7657649..0d57bf6 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -44,7 +44,6 @@ 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() @@ -57,4 +56,4 @@ def get_account_balances(request): }) else: total = a.balance() - return status.HTTP_200_OK, result + return status.HTTP_200_OK, result \ No newline at end of file From f4136d66194bac44c86ec3883969e535ca932620 Mon Sep 17 00:00:00 2001 From: Sura Atta <96949252+SuraAtta@users.noreply.github.com> Date: Sat, 6 Aug 2022 18:27:56 +0300 Subject: [PATCH 4/4] task4 --- accounting/models.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/accounting/models.py b/accounting/models.py index fe4be5b..0d07a81 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -105,6 +105,24 @@ def __radd__(self, other): return self else: return self.__add__(other) + + def _greater_(self,other): + if( self.balanceIQD > other.balanceIQD): + return True + else: + return False + + def _les_(self , other): + if( self.balanceIQD > other.balanceIQD): + return True + else: + return False + + def _zero_(self , other): + if( self.balanceIQD == 0 and other.balanceIQD == 0 ): + return True + else: + return False # def save( # self, force_insert=False, force_update=False, using=None, update_fields=None