From 04d7ceefaf405f102ae32528831510a4e5635e7d Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 8 Aug 2022 00:33:54 +0300 Subject: [PATCH 1/2] made new changes --- accounting/api/account.py | 40 +++++++--------- ...alter_account_code_alter_account_parent.py | 0 accounting/models.py | 46 ++++++++++++++++++- 3 files changed, 62 insertions(+), 24 deletions(-) create mode 100644 accounting/migrations/0007_alter_account_code_alter_account_parent.py diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e616..bfd95b5d 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -1,7 +1,7 @@ from ninja import Router from ninja.security import django_auth from django.shortcuts import get_object_or_404 -from accounting.models import Account, AccountTypeChoices +from accounting.models import Account, AccountTypeChoices,Balance from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut from typing import List from django.db.models import Sum, Avg @@ -56,32 +56,26 @@ def get_account_balances(request): return status.HTTP_200_OK, result +@account_router.get('/total_balance/', response=List[GeneralLedgerOut]) +def get_total_balance(request, account_id: int): + account = Account.objects.get(id=account_id) + children = account.children.all() + balances = [] + if account.children.all() == []: + total_balance = account.balance() + else: + parent_balance = account.balance() -class Balance: - def __init__(self, balances): - balance1 = balances[0] - balance2 = balances[1] + for child in children: + balances = [child.balance()] - if balance1['currency'] == 'USD': - balanceUSD = balance1['sum'] - balanceIQD = balance2['sum'] - else: - balanceIQD = balance1['sum'] - balanceUSD = balance2['sum'] + balance1 = balances[0] + balance2 = balances[1] - 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_balance = Balance(parent_balance) + Balance(balance1) + Balance(balance2) + + return total_balance diff --git a/accounting/migrations/0007_alter_account_code_alter_account_parent.py b/accounting/migrations/0007_alter_account_code_alter_account_parent.py new file mode 100644 index 00000000..e69de29b diff --git a/accounting/models.py b/accounting/models.py index a7d49b7f..36747a64 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) @@ -62,6 +62,21 @@ def __str__(self): def balance(self): return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + + + + + def is_balance_zero(self, account_id: int): + account = Account.objects.get(id=account_id) + if 'currency' == 'USD': + if account.balance() == 0: + return True + + if 'currency' == 'IQD': + if account.balance() == 0: + return True + + return False # def save( # self, force_insert=False, force_update=False, using=None, update_fields=None # ): @@ -111,3 +126,32 @@ class Meta: def __str__(self): return f'{self.amount} - {self.currency}' + + + + +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'] + + 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 + }] \ No newline at end of file From 4157ced8826a145c256d8990eed80b65996c634f Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 8 Aug 2022 00:52:17 +0300 Subject: [PATCH 2/2] made new changes --- ...7_alter_account_code_alter_account_parent.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/accounting/migrations/0007_alter_account_code_alter_account_parent.py b/accounting/migrations/0007_alter_account_code_alter_account_parent.py index e69de29b..b460be8c 100644 --- a/accounting/migrations/0007_alter_account_code_alter_account_parent.py +++ b/accounting/migrations/0007_alter_account_code_alter_account_parent.py @@ -0,0 +1,17 @@ +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'), + ), + ] \ No newline at end of file