Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ther Ali #85

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
103 changes: 88 additions & 15 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from asyncio.windows_events import NULL
from ninja import Router
from ninja.security import django_auth
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -39,7 +40,24 @@ def get_account_balance(request, account_id: int):
account = get_object_or_404(Account, id=account_id)

balance = account.balance()

if bool(account.children.all()) is True:
balance_parant = Balance(balances=list(balance))
childern_balances = []
for i in account.children.all():
childern_balances.append(list(i.balance()))

childern_balance_objs = []
for i in childern_balances:
childern_balance_objs.append(Balance(balances=list(i)))

for i in childern_balance_objs:
balance_parant.__add__(i)

balance = [{'currency': 'USD', 'sum': balance_parant.balanceUSD},{'currency': 'IQD', 'sum': balance_parant.balanceIQD}]




journal_entries = account.journal_entries.all()

return 200, {'account': account.name, 'balance': list(balance), 'jes': list(journal_entries)}
Expand All @@ -48,31 +66,71 @@ def get_account_balance(request, account_id: int):
@account_router.get('/account-balances/', response=List[GeneralLedgerOut])
def get_account_balances(request):
accounts = Account.objects.all()

result = []
for a in accounts:
balance = a.balance()
if bool(a.children.all()) is True:
balance_parant = Balance(balances=list(balance))
childern_balances = []
for i in a.children.all():
childern_balances.append(list(i.balance()))

childern_balance_objs = []
for i in childern_balances:
childern_balance_objs.append(Balance(balances=list(i)))

for i in childern_balance_objs:
balance_parant.__add__(i)

balance = [{'currency': 'USD', 'sum': balance_parant.balanceUSD},{'currency': 'IQD', 'sum': balance_parant.balanceIQD}]

result.append({
'account': a.name, 'balance': list(a.balance())
'account': a.name, 'balance': list(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']
if len(balances) == 2:
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

elif len(balances) == 1:
balance = (balances[0])
if balance['currency'] == 'USD':
self.balanceUSD = balance['sum']
self.balanceIQD = 0

else:
self.balanceUSD = 0
self.balanceIQD = balance['sum']

else:
balanceIQD = balance1['sum']
balanceUSD = balance2['sum']

self.balanceUSD = balanceUSD
self.balanceIQD = balanceIQD
self.balanceUSD = 0
self.balanceIQD = 0

def __init__(self, amount1, currency1, amount2, currency2):
if currency1 == 'USD':
self.balanceUSD = amount1
self.balanceIQD = amount2
elif currency1 == 'IQD':
self.balanceUSD = amount2
self.balanceIQD = amount1


def __add__(self, other):
self.balanceIQD += other.balanceIQD
Expand All @@ -84,4 +142,19 @@ def __add__(self, other):
'currency': 'IQD',
'sum': self.balanceIQD
}]


def __lt__(self, other):
iqd = self.balanceIQD < other.balanceIQD
usd = self.balanceUSD < other.balanceUSD
return(f'({usd},{iqd})')

def __gt__(self, other):
iqd = self.balanceIQD > other.balanceIQD
usd = self.balanceUSD > other.balanceUSD
return(f'({usd},{iqd})')

def isZero(self):
if self.balanceIQD == 0 and self.balanceUSD == 0:
return True
else:
return False
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-08-01 20:25

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'),
),
]
4 changes: 3 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 @@ -60,8 +60,10 @@ 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()


# def save(
# self, force_insert=False, force_update=False, using=None, update_fields=None
# ):
Expand Down
5 changes: 3 additions & 2 deletions accounting/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from decimal import Decimal
from re import T
from typing import List

from ninja import Schema
Expand Down Expand Up @@ -62,7 +63,7 @@ class TransactionIn(Schema):
type: str
description: str
je: JournalEntryInTransaction


class CurrencyBalance(Schema):
currency: str
Expand All @@ -72,4 +73,4 @@ class CurrencyBalance(Schema):
class GeneralLedgerOut(Schema):
account: str
balance: List[CurrencyBalance]
# jes: List[JournalEntryOut]
#jes: list[JournalEntryOut]
Binary file modified db.sqlite3
Binary file not shown.