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.

6 changes: 4 additions & 2 deletions .idea/unicoding.iml

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

35 changes: 2 additions & 33 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
from django.db.models import Sum, Avg
from rest_framework import status

from accounting.utilities import Balance
from restauth.authorization import AuthBearer

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


@account_router.get("/get_all", response=List[AccountOut])
def get_all(request):
return status.HTTP_200_OK, Account.objects.order_by('full_code')


@account_router.get('/get_one/{account_id}/', response={
200: AccountOut,
404: FourOFourOut,
Expand Down Expand Up @@ -51,37 +50,7 @@ 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.total_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
}]

14 changes: 13 additions & 1 deletion accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.db.models import Sum
from django.dispatch import receiver
from django.db.models.signals import post_save

from accounting.utilities import Balance
from accounting.exceptions import AccountingEquationError

'''
Expand Down Expand Up @@ -49,7 +51,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 @@ -62,6 +64,16 @@ def __str__(self):
def balance(self):
return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by()

def total_balance(self):
children = self.children.all()
if len(children) <= 1: return self.balance()

total = []
for child in list(children):
total.append(Balance(child.balance()))

return sum(total)

# def save(
# self, force_insert=False, force_update=False, using=None, update_fields=None
# ):
Expand Down
39 changes: 39 additions & 0 deletions accounting/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from functools import total_ordering

@total_ordering
class Balance:
def __init__(self, balances):
balanceUSD, balanceIQD = 0, 0

for bal in balances:
if bal['currency'] == 'USD':
balanceUSD = bal['sum']
else:
balanceIQD = bal['sum']

self.balanceUSD, self.balanceIQD = balanceUSD, balanceIQD

def __eq_zero__(self):
return self.balanceUSD == 0 and self.balanceIQD == 0

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

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

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):
return self if other == 0 else self.__add__(other)
Binary file modified db.sqlite3
Binary file not shown.
Loading