From 3ead995da522167e5da692b753e95b8a20892851 Mon Sep 17 00:00:00 2001 From: ther Date: Mon, 1 Aug 2022 23:28:46 +0300 Subject: [PATCH 1/2] task 3 1.0v --- accounting/api/account.py | 77 ++++++++++++++---- .../migrations/0007_alter_account_parent.py | 19 +++++ accounting/models.py | 4 +- db.sqlite3 | Bin 159744 -> 159744 bytes 4 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 accounting/migrations/0007_alter_account_parent.py diff --git a/accounting/api/account.py b/accounting/api/account.py index e843e61..317293f 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -1,3 +1,4 @@ +from asyncio.windows_events import NULL from ninja import Router from ninja.security import django_auth from django.shortcuts import get_object_or_404 @@ -39,7 +40,24 @@ def get_account_balance(request, account_id: int): account = get_object_or_404(Account, id=account_id) balance = account.balance() - + if bool(account.children.all()) is True: + balance_parant = Balance(balances=list(balance)) + childern_balances = [] + for i in account.children.all(): + childern_balances.append(list(i.balance())) + + childern_balance_objs = [] + for i in childern_balances: + childern_balance_objs.append(Balance(balances=list(i))) + + for i in childern_balance_objs: + balance_parant.__add__(i) + + balance = [{'currency': 'USD', 'sum': balance_parant.balanceUSD},{'currency': 'IQD', 'sum': balance_parant.balanceIQD}] + + + + journal_entries = account.journal_entries.all() return 200, {'account': account.name, 'balance': list(balance), 'jes': list(journal_entries)} @@ -48,10 +66,27 @@ 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 = [] for a in accounts: + balance = a.balance() + if bool(a.children.all()) is True: + balance_parant = Balance(balances=list(balance)) + childern_balances = [] + for i in a.children.all(): + childern_balances.append(list(i.balance())) + + childern_balance_objs = [] + for i in childern_balances: + childern_balance_objs.append(Balance(balances=list(i))) + + for i in childern_balance_objs: + balance_parant.__add__(i) + + balance = [{'currency': 'USD', 'sum': balance_parant.balanceUSD},{'currency': 'IQD', 'sum': balance_parant.balanceIQD}] + result.append({ - 'account': a.name, 'balance': list(a.balance()) + 'account': a.name, 'balance': list(balance) }) return status.HTTP_200_OK, result @@ -61,18 +96,33 @@ 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'] + if len(balances) == 2: + 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 + + elif len(balances) == 1: + balance = (balances[0]) + if balance['currency'] == 'USD': + self.balanceUSD = balance['sum'] + self.balanceIQD = 0 + + else: + self.balanceUSD = 0 + self.balanceIQD = balance['sum'] + else: - balanceIQD = balance1['sum'] - balanceUSD = balance2['sum'] - - self.balanceUSD = balanceUSD - self.balanceIQD = balanceIQD + self.balanceUSD = 0 + self.balanceIQD = 0 def __add__(self, other): self.balanceIQD += other.balanceIQD @@ -84,4 +134,3 @@ def __add__(self, other): 'currency': 'IQD', 'sum': self.balanceIQD }] - diff --git a/accounting/migrations/0007_alter_account_parent.py b/accounting/migrations/0007_alter_account_parent.py new file mode 100644 index 0000000..a4559be --- /dev/null +++ b/accounting/migrations/0007_alter_account_parent.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0.6 on 2022-08-01 20:25 + +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'), + ), + ] diff --git a/accounting/models.py b/accounting/models.py index a7d49b7..4f59f6f 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -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) @@ -60,8 +60,10 @@ 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 save( # self, force_insert=False, force_update=False, using=None, update_fields=None # ): diff --git a/db.sqlite3 b/db.sqlite3 index 3cb73d59be6da4a2a0517a8aae2d66cff6d9bafd..4607a246b411fa6b96c85c19f46d3d5c962740e6 100644 GIT binary patch delta 128 zcmZp8z}fJCbAmLZ-9#B@Ryzj0aIcLi3;3C(I9^X?7cgexc)i(CU_FP6lN2k1qP=5c za&mrYUP)$Nx`Ba#d3<6{Nor9%R4~3Eu_!gK#K^$NNY}tZ*T7K0$iT|T)XLCY&(Pe! c%)orJzT{#tMyck@`t6tX8Mj~7XVP^50QY$(8vp36L8BJ&_zk1w8;RTQ#v{pbrBX2``fe5HqtOAoU2dFBzj20U3kS OFNf1F0k_jH0xdxCc@dWY From bd0594f6ab3cd1513eb17299575e407467321e89 Mon Sep 17 00:00:00 2001 From: ther Date: Sat, 6 Aug 2022 14:17:26 +0300 Subject: [PATCH 2/2] task4 1.0v --- accounting/api/account.py | 26 +++++++++++++++++++++++++- accounting/schemas.py | 5 +++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index 317293f..50d030f 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -93,7 +93,6 @@ def get_account_balances(request): - class Balance: def __init__(self, balances): if len(balances) == 2: @@ -123,6 +122,15 @@ def __init__(self, balances): else: self.balanceUSD = 0 self.balanceIQD = 0 + + def __init__(self, amount1, currency1, amount2, currency2): + if currency1 == 'USD': + self.balanceUSD = amount1 + self.balanceIQD = amount2 + elif currency1 == 'IQD': + self.balanceUSD = amount2 + self.balanceIQD = amount1 + def __add__(self, other): self.balanceIQD += other.balanceIQD @@ -134,3 +142,19 @@ def __add__(self, other): 'currency': 'IQD', 'sum': self.balanceIQD }] + + def __lt__(self, other): + iqd = self.balanceIQD < other.balanceIQD + usd = self.balanceUSD < other.balanceUSD + return(f'({usd},{iqd})') + + def __gt__(self, other): + iqd = self.balanceIQD > other.balanceIQD + usd = self.balanceUSD > other.balanceUSD + return(f'({usd},{iqd})') + + def isZero(self): + if self.balanceIQD == 0 and self.balanceUSD == 0: + return True + else: + return False \ No newline at end of file diff --git a/accounting/schemas.py b/accounting/schemas.py index 7f67a7e..7a34b76 100644 --- a/accounting/schemas.py +++ b/accounting/schemas.py @@ -1,4 +1,5 @@ from decimal import Decimal +from re import T from typing import List from ninja import Schema @@ -62,7 +63,7 @@ class TransactionIn(Schema): type: str description: str je: JournalEntryInTransaction - + class CurrencyBalance(Schema): currency: str @@ -72,4 +73,4 @@ class CurrencyBalance(Schema): class GeneralLedgerOut(Schema): account: str balance: List[CurrencyBalance] - # jes: List[JournalEntryOut] + #jes: list[JournalEntryOut]