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
44 changes: 8 additions & 36 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions accounting/migrations/0007_alter_account_parent.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
64 changes: 63 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 @@ -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
Expand Down
Binary file modified db.sqlite3
Binary file not shown.