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

Ibrahim Anmar #109

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
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.
37 changes: 5 additions & 32 deletions accounting/api/account.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from tkinter import Y
from ninja import Router
from ninja.security import django_auth
from django.shortcuts import get_object_or_404
from accounting.models import Account, AccountTypeChoices
from accounting.models import Account ,AccountTypeChoices
from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut
from typing import List
from django.db.models import Sum, Avg
from rest_framework import status

from restauth.authorization import AuthBearer
Expand Down Expand Up @@ -50,38 +50,11 @@ def get_account_balances(request):
accounts = Account.objects.all()
result = []
for a in accounts:
balance = a.main_balance()
print(balance)
result.append({
'account': a.name, 'balance': list(a.balance())
'account': a.name, 'balance': list(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
}]

120 changes: 92 additions & 28 deletions accounting/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pickle import OBJ
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

from decimal import Decimal
'''

Account
Expand All @@ -27,6 +28,80 @@
* Each Transaction should consist of two or more even numbered Journal Entries

'''
class Balance:
def __init__(self, balances):

try:
balance1 = balances[0]
except IndexError:
balance1 = {'currency': 'USD', 'sum': 0}
try:
balance2 = balances[1]
except IndexError:
if balance1['currency'] == 'USD':
balance2 = {'currency': 'IQD', 'sum': Decimal(0)}
else:
balance2 = {'currency': 'USD', 'sum': Decimal(0)}

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):
print(self.balanceIQD)
self.balanceIQD += other.balanceIQD
print(self.balanceIQD)
print(self.balanceUSD)
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 __gt__(self, other):

if self.balanceUSD > other.balanceUSD :
StaUSD = True
else:
StaUSD = False
if self.balanceIQD > other.balanceIQD :
StaIQD = True
else:
StaIQD = False
return {'USD':StaUSD},{'IQD':StaIQD}


def __lt__(self,other):

if self.balanceUSD < other.balanceUSD :
StaUSD = True
else:
StaUSD = False
if self.balanceIQD < other.balanceIQD :
StaIQD = True
else:
StaIQD = False
return {'USD':StaUSD},{'IQD':StaIQD}

def is_zero(self):
if self.balanceIQD == 0 and self.balanceUSD == 0 :
return True
else:
return False


class AccountTypeChoices(models.TextChoices):
Expand All @@ -49,7 +124,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,32 +137,21 @@ 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}'

'''this function to amount the blance from 3 ways'''
def main_balance(self):
child = self.children.all()
list_of_balance = []
if len(child) == 0:
return self.balance()
elif len(child) > 0:
if len(child) == 1:
return self.balance()
else:
for x in child:
child_balance = x.balance()
OBJ = Balance(child_balance)
list_of_balance.append(OBJ)
return sum(list_of_balance)

class Transaction(models.Model):
type = models.CharField(max_length=255, choices=TransactionTypeChoices.choices)
Expand Down
Binary file modified db.sqlite3
Binary file not shown.
Loading