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

Aya Jafar #89

Open
wants to merge 7 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
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.
63 changes: 63 additions & 0 deletions accounting/Task4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Balance:
def __init__(self, balances):
balanceUSD = 0
balanceIQD = 0
if balances:
balance1 = balances[0]
#-----------One currency-----------
if len(balances) == 1:
if balance1['currency'] == 'USD':
balanceUSD = balance1['sum']
else:
balanceIQD = balance1['sum']

#-----------Two currencies---------
elif len(balances) == 2:
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
}]

def __gt__(self, other):
result = [False] * 2
if self.balanceUSD > other.balanceUSD : result[0] = True
if self.balanceIQD > other.balanceIQD : result[1] = True
return tuple(result)

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

balance1 = Balance([
{'currency':'USD','sum':100},
{'currency':'IQD','sum':200}
])

balance2 = Balance([
{'currency':'USD','sum':500},
{'currency':'IQD','sum':100}
])


# print("{:25}{}".format("balance1 > balance2 => ", balance1 > balance2))
# print("{:25}{}".format("balance1 < balance2 => ", balance1 < balance2))
# print("{:20}{:5}{}".format("balance1 == 0","=>", balance1.is_zero()))
# print("{:25}{}".format("balance1 + balance2 => ", balance1 + balance2))

7 changes: 3 additions & 4 deletions accounting/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

# Register your models here.
from accounting.models import Account, Transaction, JournalEntry
from mptt.admin import MPTTModelAdmin


@admin.register(Account)
class AccountAdmin(admin.ModelAdmin):
class AccountAdmin(MPTTModelAdmin):
list_display = ['name', 'parent', 'type', 'code', 'full_code']
search_fields = ['name', 'code', 'full_code']
list_filter = ['type']
ordering = ['full_code']


admin.site.register(Account,AccountAdmin)
admin.site.register(Transaction)
admin.site.register(JournalEntry)
61 changes: 25 additions & 36 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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 accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut, GeneralLedgerOutBalances
from typing import List
from django.db.models import Sum, Avg
from rest_framework import status
Expand Down Expand Up @@ -38,50 +38,39 @@ def get_account_types(request):
def get_account_balance(request, account_id: int):
account = get_object_or_404(Account, id=account_id)

balance = account.balance()
parent_list = Account.objects.filter(parent_id__isnull=False).values_list('parent', flat=True)

if account_id in parent_list:
balance = account.parent_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])
@account_router.get('/account-balances/', response=List[GeneralLedgerOutBalances])
def get_account_balances(request):
accounts = Account.objects.all()
result = []

parent_list = Account.objects.filter(parent_id__isnull=False).values_list('parent',flat=True)

for a in accounts:
if a.id in parent_list:
result.append({
'account': a.name,
'balance': list( a.parent_balance() ),
'parent_id':a.parent_id ,
'id':a.id
})

result.append({
'account': a.name, 'balance': list(a.balance())
})

'account': a.name,
'balance': list( a.balance() ),
'parent_id':a.parent_id ,
'id':a.id
})

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
}]

43 changes: 0 additions & 43 deletions accounting/migrations/0001_initial.py

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions accounting/migrations/0003_account_extra_alter_account_type.py

This file was deleted.

18 changes: 0 additions & 18 deletions accounting/migrations/0004_alter_account_extra.py

This file was deleted.

This file was deleted.

This file was deleted.

Empty file removed accounting/migrations/__init__.py
Empty file.
Loading