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
52 changes: 52 additions & 0 deletions accounting/api/Currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#task 4
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
result=[{
'currency': 'USD',
'sum': self.balanceUSD
}, {
'currency': 'IQD',
'sum': self.balanceIQD
}]
def __zero__(self,other):
IQD=True
USD=True
if self.balanceIQD ==0 and self.balanceUSD == 0:
IQD=False
USD=False
return{'IQD':IQD,'USD':USD}
def __grater__():
IQD=False
USD=False
if self.balanceIQD>other.balanceIQD :
IQD=True
if self.balanceUSD>other.balanceUSD:
USD=True
return{'IQD':IQD,'USD':USD}
def __smaller__():
IQD=False
USD=False
if self.balanceIQD<other.balanceIQD :
IQD=True
if self.balanceUSD<other.balanceUSD:
USD=True
return{'IQD':IQD,'USD':USD}

86 changes: 37 additions & 49 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut
from typing import List
from django.db.models import Sum, Avg
from rest_framework import status
from http import HTTPStatus

from restauth.authorization import AuthBearer

Expand All @@ -14,7 +14,7 @@

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


@account_router.get('/get_one/{account_id}/', response={
Expand All @@ -34,54 +34,42 @@ def get_account_types(request):
return {t[0]: t[1] for t in AccountTypeChoices.choices}


@account_router.get('/account-balance/{account_id}', response=GeneralLedgerOut)
def get_account_balance(request, account_id: int):
account = get_object_or_404(Account, id=account_id)

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())
})

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']
@account_router.get('/account-balance/{account_id}', response=GeneralLedgerOut)
def get_account_balance(request, account_id: int):
account = Account.objects.get(id=account_id)
children_accounts = account.children.all()
account_balance = account.balance()
children_balances=[]
balances=[]

for a in children_accounts:
balance = a.balance()
children_balances.append(list(balance))

for a in children_balances:
for i in a:
balances.append(i)

for a in list(account_balance):
balances.append(a)

if account.parent == None:
final_balance= get_balance(balances)
else:
final_balance=list(children_balances)+list(account_balance)

return HTTPStatus.OK, {'account': account.name, 'balance':final_balance}

def get_balance(balances):
IQD_B=0
USD_B=0
for i in balances:
if i['currency'] == 'IQD':
IQD_B += i['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
}]
USD_B += i['sum']

final_balance = [{'currency':'USD', 'sum':USD_B},{'currency':'IQD', 'sum':IQD_B}]
return final_balance
4 changes: 2 additions & 2 deletions accounting/api/transaction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ninja import Router
from rest_framework import status
from http import HTTPStatus
from accounting import services
from accounting.exceptions import AtomicAccountTransferException, ZeroAmountError, AccountingEquationError
from accounting.schemas import TransactionIn, TransactionOut, TransactionOutSchema
Expand All @@ -16,7 +16,7 @@
@transaction_router.post('/add-transaction', response=TransactionOutSchema)
def add_transaction(request, transaction_in: TransactionIn):
t = services.account_transfer(transaction_in)
return status.HTTP_200_OK, {
return HTTPStatus.OK, {
'transaction': t,
# 'jes': t.journal_entries.all()
}
19 changes: 19 additions & 0 deletions accounting/migrations/0007_alter_account_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0.6 on 2022-08-01 15:03

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('accounting', '0006_alter_account_code_alter_account_full_code_and_more'),
]

operations = [
migrations.AlterField(
model_name='account',
name='parent',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='accounting.account'),
),
]
2 changes: 1 addition & 1 deletion accounting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 Down
Binary file modified db.sqlite3
Binary file not shown.