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
29 changes: 2 additions & 27 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 , JournalEntry
from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut
from typing import List
from django.db.models import Sum, Avg
Expand Down Expand Up @@ -51,37 +51,12 @@ def get_account_balances(request):
result = []
for a in accounts:
result.append({
'account': a.name, 'balance': list(a.balance())
'account': a.name, 'balance': list(a.sumbalance())
})

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']
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
}]

87 changes: 86 additions & 1 deletion accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.dispatch import receiver
from django.db.models.signals import post_save
from accounting.exceptions import AccountingEquationError
from accounting.api import Balance

'''

Expand All @@ -27,7 +28,70 @@
* Each Transaction should consist of two or more even numbered Journal Entries

'''
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)

#TASK4 function1
def __greatThan__ (self, other):
if self.balanceIQD > other.balanceIQD:
IQD = True
else:
IQD = False
if self.balanceUSD > other.balanceUSD:
USD = True
else:
USD = False
return {'IQD': IQD, 'USD': USD}

#function2
def __lessThan__ (self, other):
if self.balanceIQD < other.balanceIQD:
IQD = True
else:
IQD = False
if self.balanceUSD < other.balanceUSD:
USD = True
else:
USD = False
return {'IQD': IQD, 'USD': USD}


#function3
def __iszero__(self):

if self.balanceIQD == 0 and self.balanceUSD==0:
return True
else:
return False


class AccountTypeChoices(models.TextChoices):
ASSETS = 'ASSETS', 'Assets'
Expand All @@ -49,7 +113,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='account_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 +125,27 @@ def __str__(self):

def balance(self):
return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by()




def sumbalance(self):

if self.parent is not None:
return self.balance()
else:
children = self.account_children.all()
if len(children) == 1:
return self.balance()
elif len(children) == 0:
return self.balance()
else:
sum_balance = []
for childrens in list(children):
child_Balance = childrens.sumbalance()
sum_balance.append(Balance(child_Balance))

return sum(sum_balance)

# def save(
# self, force_insert=False, force_update=False, using=None, update_fields=None
Expand Down