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.

50 changes: 10 additions & 40 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ def get_all(request):
})
def get_one(request, account_id: int):
try:
account = Account.objects.get(id=account_id)
return account
return Account.objects.get(id=account_id)
except Account.DoesNotExist:
return 404, {'detail': f'Account with id {account_id} does not exist'}
return status.HTTP_404_NOT_FOUND, {'detail': f'Account with id {account_id} does not exist'}


@account_router.get('/get_account_types/')
Expand All @@ -36,52 +35,23 @@ def get_account_types(request):

@account_router.get('/account-balance/{account_id}', response=GeneralLedgerOut)
def get_account_balance(request, account_id: int):
global balance, balanceUSD
account = get_object_or_404(Account, id=account_id)
if account.parent is None:
balance=account.parent_balances

balance = account.balance()

else:
balance = account.balance()
journal_entries = account.journal_entries.all()

return 200, {'account': account.name, 'balance': list(balance), 'jes': list(journal_entries)}


@account_router.get('/account-balances/', response=List[GeneralLedgerOut])
def get_account_balances(request):
accounts = Account.objects.all()
result = []
for a in accounts:
result.append({
'account': a.name, 'balance': list(a.balance())
})

result = [
{'account': a.name, 'balance': list(a.balance())} for a in accounts
]
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
}]

89 changes: 56 additions & 33 deletions accounting/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.db import models
from django.db.models import Sum
from django.dispatch import receiver
from django.db.models.signals import post_save

from accounting.exceptions import AccountingEquationError

'''
Expand All @@ -12,17 +11,16 @@
- name
- code
- full_code

Transaction
- type
- description

JournalEntry
- account
- transaction
- amount
- currency

* Accounts should support multiple currencies
* Each Transaction should consist of two or more even numbered Journal Entries

Expand All @@ -48,8 +46,48 @@ class CurrencyChoices(models.TextChoices):
IQD = 'IQD', 'IQD'


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': 'IQD',
'sum': self.balanceIQD
}, {
'currency': 'USD',
'sum': self.balanceUSD
}]

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

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

def is_zero(self):
bIQD = self.balanceIQD == 0
bUSD = self.balanceUSD == 0
return bIQD, bUSD


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,32 +100,17 @@ def __str__(self):
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
# ):
# creating = not bool(self.id)
#
# if creating:
# self.code = self.id
# try:
# self.full_code = f'{self.parent.full_code}{self.id}'
# except AttributeError:
# self.full_code = self.id
#
# super(Account, self).save()
#
# if creating:
# self.refresh_from_db()


# @receiver(post_save, sender=Account)
# def add_code_and_full_code(sender, instance, **kwargs):
# instance.code = instance.id
# if instance.parent:
# instance.full_code = f'{instance.parent.full_code}{instance.id}'
# else:
# instance.full_code = f'{instance.id}'

@property
def parent_balances(self):
global total_balance
parent_children = self.children.all()
for account in parent_children:
balance_acc = Balance(account.balance())
children = self.children.all()
for child_obj in children:
balance_acc_obj = Balance(child_obj.balance())
total_balance = balance_acc_obj.__add__(balance_acc)
return total_balance

class Transaction(models.Model):
type = models.CharField(max_length=255, choices=TransactionTypeChoices.choices)
Expand All @@ -110,4 +133,4 @@ class Meta:
currency = models.CharField(max_length=3, choices=CurrencyChoices.choices)

def __str__(self):
return f'{self.amount} - {self.currency}'
return f'{self.amount} - {self.currency}'
4 changes: 1 addition & 3 deletions accounting/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class TransactionOut(Schema):

class TransactionOutSchema(Schema):
transaction: TransactionOut
# jes: List[StandAloneJournalEntry]


class JournalEntry(Schema):
Expand Down Expand Up @@ -71,5 +70,4 @@ class CurrencyBalance(Schema):

class GeneralLedgerOut(Schema):
account: str
balance: List[CurrencyBalance]
# jes: List[JournalEntryOut]
balance: List[CurrencyBalance]
2 changes: 1 addition & 1 deletion accounting/services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import transaction as db_transaction
from rest_framework import status
from accounting.exceptions import AtomicAccountTransferException, ZeroAmountError, AccountingEquationError

from accounting.models import Transaction, JournalEntry


Expand Down
5 changes: 2 additions & 3 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""config URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Expand All @@ -23,7 +22,7 @@
api = NinjaAPI(
title='Accounting for All',
version='0.2',
description='This is a model preview of a double entry accouting system.',
description='This is a model preview of a double entry accounting system.',
csrf=True,
)
api.add_router('account/', account_router)
Expand All @@ -34,4 +33,4 @@
urlpatterns = [
path('admin/', admin.site.urls),
path("api/", api.urls),
]
]
Loading