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
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.

38 changes: 6 additions & 32 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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.schemas import AccountOut, FourOFourOut, GeneralLedgerOut
from typing import List
from django.db.models import Sum, Avg

from django.shortcuts import get_object_or_404
from ninja import Router
from rest_framework import status

from restauth.authorization import AuthBearer
from accounting.models import Account, AccountTypeChoices, Balance
from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut

account_router = Router(tags=['account'])

Expand Down Expand Up @@ -51,37 +49,13 @@ 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.main_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
}]

107 changes: 96 additions & 11 deletions 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 decimal import Decimal

'''

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

'''
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):
print(self.balanceIQD)
self.balanceIQD += other.balanceIQD
print(self.balanceIQD)
print(self.balanceUSD)
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):
obj_balance1 = self.balanceUSD > other.balanceIQD
obj_balance2 = self.balanceIQD > other.balanceUSD
return obj_balance1, obj_balance2

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

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






class AccountTypeChoices(models.TextChoices):
Expand All @@ -48,19 +114,38 @@ class CurrencyChoices(models.TextChoices):
IQD = 'IQD', 'IQD'


class Account(models.Model):
parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL)
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)
full_code = models.CharField(max_length=25, null=True, blank=True)
extra = models.JSONField(default=dict, null=True, blank=True)

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()
class Account(models.Model):
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)
full_code = models.CharField(max_length=25, null=True, blank=True)
extra = models.JSONField(default=dict, null=True, blank=True)

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 main_balance(self):
child = self.children.all()
list_of_balances = []

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

elif len(child) > 0:
if len(child) == 1:
return self.balance()
else:
for k in list(child):
list_of_child = k.balance()
obj = Balance(list_of_child)
list_of_balances.append(obj)
return sum(list_of_balances)

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