diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..0d57bf6 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -44,44 +44,16 @@ 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 - - - - -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 \ No newline at end of file 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..0d07a81 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,68 @@ 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 _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 diff --git a/db.sqlite3 b/db.sqlite3 index 3cb73d5..b72cf7a 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ