Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ahmed Amer Abd Al-Azeez(Task 3 + Task 4) #107

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
38 changes: 3 additions & 35 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,8 @@ def get_account_balance(request, account_id: int):
@account_router.get('/account-balances/', response=List[GeneralLedgerOut])
def get_account_balances(request):
accounts = Account.objects.all()
result = []
res = [ ]
for a in accounts:
result.append({
'account': a.name, 'balance': list(a.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
}]
res.append({'account': a.name,'balance': list(a.Balance_Sum())})

return status.HTTP_200_OK, res
121 changes: 72 additions & 49 deletions accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,58 @@
from django.db.models.signals import post_save
from accounting.exceptions import AccountingEquationError

'''

Account
- parent
- type
- 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

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


def __sub__(self, other):
self.balanceIQD -= other.balanceIQD
self.balanceUSD -= other.balanceUSD
return [{
'currency': 'USD',
'sum': self.balanceUSD
}, {
'currency': 'IQD',
'sum': self.balanceIQD
}]

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):
return self.balanceUSD == 0 and self.balanceIQD == 0


class AccountTypeChoices(models.TextChoices):
Expand Down Expand Up @@ -61,33 +90,27 @@ 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}'


def Balance_Sum(self):
children = self.account_children.all()
balances = []

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

elif len(children) > 0:

if len(children) == 1:

return self.balance()
else:

for childrens in list(children):
children_balances = childrens.balance()
chilrenOBJ = Balance(children_balances)
balances.append(chilrenOBJ)
return sum(balances)


class Transaction(models.Model):
type = models.CharField(max_length=255, choices=TransactionTypeChoices.choices)
Expand Down