diff --git a/.idea/misc.xml b/.idea/misc.xml index 6ba69809..d56657ad 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/unicoding.iml b/.idea/unicoding.iml index 3e4215da..b3643bb4 100644 --- a/.idea/unicoding.iml +++ b/.idea/unicoding.iml @@ -14,7 +14,7 @@ - + diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e616..e0cdeac5 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -1,13 +1,11 @@ -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.schemas import AccountOut, FourOFourOut, GeneralLedgerOut from typing import List -from django.db.models import Sum, Avg + +from django.shortcuts import get_object_or_404 +from ninja import Router from rest_framework import status -from restauth.authorization import AuthBearer +from accounting.models import Account, AccountTypeChoices, Balance +from accounting.schemas import AccountOut, FourOFourOut, GeneralLedgerOut account_router = Router(tags=['account']) @@ -51,7 +49,7 @@ def get_account_balances(request): result = [] for a in accounts: result.append({ - 'account': a.name, 'balance': list(a.balance()) + 'account': a.name, 'balance': list(a.main_balance()) }) return status.HTTP_200_OK, result @@ -59,29 +57,5 @@ def get_account_balances(request): -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 - }] diff --git a/accounting/models.py b/accounting/models.py index a7d49b7f..18962d2b 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -3,6 +3,7 @@ from django.dispatch import receiver from django.db.models.signals import post_save from accounting.exceptions import AccountingEquationError +from decimal import Decimal ''' @@ -27,6 +28,71 @@ * 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): + obj_balance1 = self.balanceUSD > other.balanceIQD + obj_balance2 = self.balanceIQD > other.balanceUSD + return obj_balance1, obj_balance2 + + def __lt__(self, other): + obj_balance1 = self.balanceUSD < other.balanceUSD + obj_balance2 = self.balanceIQD < other.balanceIQD + return obj_balance1, obj_balance2 + + def e_t_z(self): + if self.balanceUSD == 0 and self.balanceIQD == 0: + return True + else: + return False + + + + class AccountTypeChoices(models.TextChoices): @@ -48,19 +114,38 @@ class CurrencyChoices(models.TextChoices): IQD = 'IQD', 'IQD' -class Account(models.Model): - parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) - 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) - full_code = models.CharField(max_length=25, null=True, blank=True) - extra = models.JSONField(default=dict, null=True, blank=True) - def __str__(self): - return f'{self.full_code} - {self.name}' - def balance(self): - return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() +class Account(models.Model): + 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) + full_code = models.CharField(max_length=25, null=True, blank=True) + extra = models.JSONField(default=dict, null=True, blank=True) + + def __str__(self): + return f'{self.full_code} - {self.name}' + + def balance(self): + return self.journal_entries.values('currency').annotate(sum=Sum('amount')).order_by() + + def main_balance(self): + child = self.children.all() + list_of_balances = [] + + if len(child) == 0: + return self.balance() + + elif len(child) > 0: + if len(child) == 1: + return self.balance() + else: + for k in list(child): + list_of_child = k.balance() + obj = Balance(list_of_child) + list_of_balances.append(obj) + return sum(list_of_balances) # def save( # self, force_insert=False, force_update=False, using=None, update_fields=None