From aaedfc6d5b6c6e5f9e21cb1bbfa668fda866b1e5 Mon Sep 17 00:00:00 2001 From: sajad mohammed Date: Thu, 4 Aug 2022 19:01:24 +0300 Subject: [PATCH 1/2] task3 --- .idea/misc.xml | 3 ++ accounting/api/account.py | 24 ++++++---- ...level_account_lft_account_rght_and_more.py | 44 ++++++++++++++++++ accounting/models.py | 17 +++++-- db.sqlite3 | Bin 159744 -> 167936 bytes 5 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 accounting/migrations/0007_account_level_account_lft_account_rght_and_more.py diff --git a/.idea/misc.xml b/.idea/misc.xml index 6ba69809..bcbff53b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e616..f9bc154d 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -6,6 +6,7 @@ from typing import List from django.db.models import Sum, Avg from rest_framework import status +import mptt from restauth.authorization import AuthBearer @@ -60,17 +61,15 @@ def get_account_balances(request): class Balance: - def __init__(self, balances): - balance1 = balances[0] - balance2 = balances[1] - - if balance1['currency'] == 'USD': - balanceUSD = balance1['sum'] - balanceIQD = balance2['sum'] - else: - balanceIQD = balance1['sum'] - balanceUSD = balance2['sum'] + 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 @@ -85,3 +84,8 @@ def __add__(self, other): 'sum': self.balanceIQD }] + def __radd__(self,other): + if other == 0: + return self + else: + return self.__add__(other) diff --git a/accounting/migrations/0007_account_level_account_lft_account_rght_and_more.py b/accounting/migrations/0007_account_level_account_lft_account_rght_and_more.py new file mode 100644 index 00000000..9b190705 --- /dev/null +++ b/accounting/migrations/0007_account_level_account_lft_account_rght_and_more.py @@ -0,0 +1,44 @@ +# Generated by Django 4.0.6 on 2022-08-03 14:02 + +from django.db import migrations, models +import django.db.models.deletion +import mptt.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounting', '0006_alter_account_code_alter_account_full_code_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='account', + name='level', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='account', + name='lft', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='account', + name='rght', + field=models.PositiveIntegerField(default=0, editable=False), + preserve_default=False, + ), + migrations.AddField( + model_name='account', + name='tree_id', + field=models.PositiveIntegerField(db_index=True, default=0, editable=False), + preserve_default=False, + ), + migrations.AlterField( + model_name='account', + name='parent', + field=mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounting.account'), + ), + ] diff --git a/accounting/models.py b/accounting/models.py index a7d49b7f..65686340 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -3,6 +3,11 @@ from django.dispatch import receiver from django.db.models.signals import post_save from accounting.exceptions import AccountingEquationError +from mptt.models import TreeForeignKey,MPTTModel +from mptt.fields import TreeForeignKey +from django.shortcuts import get_object_or_404 + + ''' @@ -48,8 +53,8 @@ class CurrencyChoices(models.TextChoices): IQD = 'IQD', 'IQD' -class Account(models.Model): - parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) +class Account(MPTTModel): + parent = TreeForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) 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) @@ -60,7 +65,13 @@ def __str__(self): return f'{self.full_code} - {self.name}' def balance(self): - return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + result = [ + account.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + for account in self.get_descendants(include_self=False) + + ] + return result + #return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() # def save( # self, force_insert=False, force_update=False, using=None, update_fields=None diff --git a/db.sqlite3 b/db.sqlite3 index 3cb73d59be6da4a2a0517a8aae2d66cff6d9bafd..88301b72ceb71009794e8f5482f922817566a3ee 100644 GIT binary patch delta 1482 zcmdT^U1$?o6ux)vOlKxdavQZxlaMkUb#1V1CX=eIVry}OY!wxRnDX@uc%zG26aKw z>}?=3?HM>uGEteu&q0V>jQXPu^(Y4CE3PKhLllMSLEX&cwV`ar((P>CECk8(XuFTz z1c%=NJdOXypYcchuIMY3FezQ;8aWmT(An6shRPd*kWifIfzk?>llgW&^KG$NIe>kB zRsc<$8-xJ}`vfXvBENDd#5zd5gTTbEdlS2piDXK*3L~RjBXWZP0JBc9HLpdh6CsJnWh}zYPX>*7X?PDBj^$A=do`{5JKh4<`O= za! z-KUN<_VNB?432eW&9rfRX(MWLCus4FhN`AIRr1Nz(X$wor9HbPd1~N z#QS40I36Is^AQ*!|L_;Sql=q*xr32gR!|C6gr^FN%N***?l4zdEdv9CIuOG^$V44uR$B(WXw!`;3;3BOIrS&A3m9)!RQSrVSzq!V z8&4;{Ie!3u75^)KS^mw62B-O0rP!D)87C|9iB0~mm(I9wa=N|}s~88fA>-tWdcv&y zml>ovL9*hajLiCsDOrhm>G|=wndwD|C7Jno#gdaR>+1>fzhz+Mf5gE5hyMltcm7ZO zk2VWlxWGU8LO-hn3kx#?BP-B-tUwkgBQuC#VrFL8{NJ8YL5P=s2~cM~15n>3{yqHp z{2f3?#q!sSvv4rTGKdN?FuFzrxcUXV1}7&L6<61?O0j|@WCa-*U=pbn1*v((sX$4f zB@$pwa5;!35oQhsNswWjj={mMA;C$BdD+#qB21{F$%(}o)wN=bAWddb?OhIxlI)uovq!LQ|Lw!b`B{vU{VoIFKHgnCd$=}mv2qr1lyK^@-`(se zV9&13nksH?&e-l>l9-f}nwMH09}f-i#FX63y!f2_bg Date: Thu, 11 Aug 2022 15:26:52 +0300 Subject: [PATCH 2/2] 'task4' --- accounting/models.py | 3 +++ task4.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 task4.py diff --git a/accounting/models.py b/accounting/models.py index 65686340..6bd3f006 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -71,6 +71,9 @@ def balance(self): ] return result + + + #return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() # def save( diff --git a/task4.py b/task4.py new file mode 100644 index 00000000..05dbbb44 --- /dev/null +++ b/task4.py @@ -0,0 +1,28 @@ +iqd = 50 +usd = 40 +def __gt__(self, other): + IQD = bool(self.balanceiqd > other.balanceIQD) + USD = bool(self.balanceUSD > other.balanceUSD) + return IQD, USD + + +def __lt__(self, other): + IQD = bool(self.balanceiqd < other.balanceIQD) + USD = bool(self.balanceUSD < other.balanceUSD) + return IQD, USD + + +def other_than_gt_and_ls(self, other): + if self.balanceIQD > other.balanceIQD and self.balanceUSD < other.balanceUSD: + return True, False + else: + return False, True + + +def is_zero(self): + if self.balanceIQD == 0 and self.balanceUSD == 0: + return True + else: + return False + +print(iqd.__lt__(50)) \ No newline at end of file