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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/unicoding.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 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,Balance
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,37 @@ 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.M_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']
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
}]
# 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
# }]

85 changes: 83 additions & 2 deletions accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.dispatch import receiver
from django.db.models.signals import post_save
from accounting.exceptions import AccountingEquationError

from decimal import Decimal
'''

Account
Expand Down Expand Up @@ -48,8 +48,72 @@ class CurrencyChoices(models.TextChoices):
IQD = 'IQD', 'IQD'


class Balance:
def __init__(self, balances):
try:
balance1 = balances[0]
except IndexError:
balance1 = {'currency': 'USD', 'sum': 0}
try:
balance2 = balances[1]
except IndexError:
if balance1['currency'] == 'USD':
balance2 = {'currency': 'IQD', 'sum': Decimal(0)}
else:
balance2 = {'currency': 'USD', 'sum': Decimal(0)}

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
}]
def __radd__(self, other):
if other == 0: #
return self
else:
return self.__add__(other)

def __gt__(self, other):
return self.balanceUSD > other.balanceUSD, self.balanceIQD > other.balanceIQD

def __lt__(self, other):
return self.balanceUSD < other.balanceUSD, self.balanceIQD < other.balanceIQD

def is_zero(self):
if self.balanceUSD == 0 and self.balanceIQD == 0:
return True
else:
return False













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 +126,23 @@ def __str__(self):
def balance(self):
return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by()

def M_Balance(self):
M_child = self.children.all()
list_balances = []

if len(M_child) == 0:
return self.balance()

elif len(M_child) > 0:
if len(M_child) == 1:
return self.balance()
else:
for r in list(M_child):
list_child = r.balance()
obj = Balance(list_child)
list_balances.append(obj)
return sum(list_balances)

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