Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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'),
),
]
46 changes: 45 additions & 1 deletion accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
# ):
Expand Down Expand Up @@ -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
}]