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..6bd3f006 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,16 @@ 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 3cb73d59..88301b72 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ 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