From ff9cf0631a7f81a149a9d43a954125493871d768 Mon Sep 17 00:00:00 2001 From: Mohammed Date: Mon, 1 Aug 2022 18:04:10 +0300 Subject: [PATCH 1/7] task3 after sol --- accounting/api/account.py | 52 +++++++++---------- .../migrations/0007_alter_account_parent.py | 19 +++++++ accounting/models.py | 2 +- 3 files changed, 46 insertions(+), 27 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..2e9e409 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -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 @@ -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={ @@ -34,33 +34,38 @@ 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() + account = Account.objects.get(id=account_id) + children_accounts = account.children.all() + account_balance = account.balance() + children_balances=[] + balances=[] - journal_entries = account.journal_entries.all() + for a in children_accounts: + balance = a.balance() + children_balances.append(list(balance)) - return 200, {'account': account.name, 'balance': list(balance), 'jes': list(journal_entries)} + for a in children_balances: + for i in a: + balances.append(i) + for a in list(account_balance): + balances.append(a) -@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()) - }) + if account.parent == None: + final_balance= Balance(balances) + else: + final_balance=list(children_balances)+list(account_balance) - return status.HTTP_200_OK, result + return HTTPStatus.OK, {'account': account.name, 'balance':final_balance} +def Balance(balances): -class Balance: - def __init__(self, balances): balance1 = balances[0] balance2 = balances[1] @@ -71,17 +76,12 @@ def __init__(self, balances): 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 + 'sum': balanceUSD }, { 'currency': 'IQD', - 'sum': self.balanceIQD + 'sum': 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..be32e88 --- /dev/null +++ b/accounting/migrations/0007_alter_account_parent.py @@ -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'), + ), + ] diff --git a/accounting/models.py b/accounting/models.py index a7d49b7..f6359e7 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) From f861b4b38c916a391fdcd0b98bf137c8043ed540 Mon Sep 17 00:00:00 2001 From: Mohammed Date: Mon, 1 Aug 2022 20:02:39 +0300 Subject: [PATCH 2/7] task3 after sol --- accounting/api/transaction.py | 4 ++-- db.sqlite3 | Bin 159744 -> 159744 bytes 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/accounting/api/transaction.py b/accounting/api/transaction.py index 4c5efbe..06e50af 100644 --- a/accounting/api/transaction.py +++ b/accounting/api/transaction.py @@ -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 @@ -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() } diff --git a/db.sqlite3 b/db.sqlite3 index 3cb73d59be6da4a2a0517a8aae2d66cff6d9bafd..d36304b7079c3dd007089381ec8aa987354b58f2 100644 GIT binary patch delta 128 zcmZp8z}fJCbAmLZ-9#B@Ryzhg`diIIVkk*36L8BJ&_zk1w8;RTQ#v{pbrBX2``fe5HqtOAoU2dFBzj20U3kS OFNf1F0k_jH0xdxCc@dWY From 53bc186011b02808236879ec952502aa3e2a11de Mon Sep 17 00:00:00 2001 From: Mohammed Date: Wed, 3 Aug 2022 00:40:51 +0300 Subject: [PATCH 3/7] task3 --- accounting/api/account.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index 2e9e409..b25b135 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -34,6 +34,26 @@ def get_account_types(request): return {t[0]: t[1] for t in AccountTypeChoices.choices} +def Balance(balances): + + balance1 = balances[0] + balance2 = balances[1] + + if balance1['currency'] == 'USD': + balanceUSD = balance1['sum'] + balanceIQD = balance2['sum'] + else: + balanceIQD = balance1['sum'] + balanceUSD = balance2['sum'] + + return [{ + 'currency': 'USD', + 'sum': balanceUSD + }, { + 'currency': 'IQD', + 'sum': balanceIQD + }] + @account_router.get('/account-balance/{account_id}', response=GeneralLedgerOut) @@ -64,24 +84,5 @@ def get_account_balance(request, account_id: int): -def Balance(balances): - - balance1 = balances[0] - balance2 = balances[1] - - if balance1['currency'] == 'USD': - balanceUSD = balance1['sum'] - balanceIQD = balance2['sum'] - else: - balanceIQD = balance1['sum'] - balanceUSD = balance2['sum'] - - return [{ - 'currency': 'USD', - 'sum': balanceUSD - }, { - 'currency': 'IQD', - 'sum': balanceIQD - }] From 4339b29fc940c8d9d62dee28d60c63212ba30228 Mon Sep 17 00:00:00 2001 From: Mohammed Date: Fri, 5 Aug 2022 23:52:04 +0300 Subject: [PATCH 4/7] task4 after sol --- accounting/api/account.py | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index b25b135..f951acc 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -34,26 +34,6 @@ def get_account_types(request): return {t[0]: t[1] for t in AccountTypeChoices.choices} -def Balance(balances): - - balance1 = balances[0] - balance2 = balances[1] - - if balance1['currency'] == 'USD': - balanceUSD = balance1['sum'] - balanceIQD = balance2['sum'] - else: - balanceIQD = balance1['sum'] - balanceUSD = balance2['sum'] - - return [{ - 'currency': 'USD', - 'sum': balanceUSD - }, { - 'currency': 'IQD', - 'sum': balanceIQD - }] - @account_router.get('/account-balance/{account_id}', response=GeneralLedgerOut) @@ -76,7 +56,7 @@ def get_account_balance(request, account_id: int): balances.append(a) if account.parent == None: - final_balance= Balance(balances) + final_balance= get_balance(balances) else: final_balance=list(children_balances)+list(account_balance) @@ -84,5 +64,15 @@ def get_account_balance(request, account_id: int): +def get_balance(balances): + IQD_balance=0 + USD_balance=0 + for a in balances: + if a['currency'] == 'IQD': + IQD_balance += a['sum'] + else: + USD_balance += a['sum'] - + final_balance = [{'currency':'USD', 'sum':USD_balance},{'currency':'IQD', 'sum':IQD_balance}] + return final_balance + \ No newline at end of file From c8626073af715514aa7dcc88fd72ac13bbeca560 Mon Sep 17 00:00:00 2001 From: Mohammed Date: Sat, 6 Aug 2022 00:16:45 +0300 Subject: [PATCH 5/7] task3 after sol --- accounting/api/account.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/accounting/api/account.py b/accounting/api/account.py index f951acc..ce3ac0d 100644 --- a/accounting/api/account.py +++ b/accounting/api/account.py @@ -62,17 +62,14 @@ def get_account_balance(request, account_id: int): return HTTPStatus.OK, {'account': account.name, 'balance':final_balance} - - def get_balance(balances): - IQD_balance=0 - USD_balance=0 - for a in balances: - if a['currency'] == 'IQD': - IQD_balance += a['sum'] + IQD_B=0 + USD_B=0 + for i in balances: + if i['currency'] == 'IQD': + IQD_B += i['sum'] else: - USD_balance += a['sum'] + USD_B += i['sum'] - final_balance = [{'currency':'USD', 'sum':USD_balance},{'currency':'IQD', 'sum':IQD_balance}] - return final_balance - \ No newline at end of file + final_balance = [{'currency':'USD', 'sum':USD_B},{'currency':'IQD', 'sum':IQD_B}] + return final_balance \ No newline at end of file From 3ac65eb330ddef4f52a32683cd096c34e811162c Mon Sep 17 00:00:00 2001 From: Mohammed Date: Sat, 6 Aug 2022 00:18:30 +0300 Subject: [PATCH 6/7] task4 after sol --- accounting/api/Currency.py | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 accounting/api/Currency.py diff --git a/accounting/api/Currency.py b/accounting/api/Currency.py new file mode 100644 index 0000000..66a52f0 --- /dev/null +++ b/accounting/api/Currency.py @@ -0,0 +1,45 @@ + +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 + }] +#task 4 + def currency(self,other): + IQD=True + USD=True + if self.balanceIQD ==0 and self.balanceUSD == 0: + IQD=False + USD=False + else: + if self.balanceIQD>other.balanceIQD : + IQD=True + elif self.balanceIQDother.balanceUSD: + USD=True + elif self.balanceUSD Date: Sat, 6 Aug 2022 00:41:44 +0300 Subject: [PATCH 7/7] task4 after sol --- accounting/api/Currency.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/accounting/api/Currency.py b/accounting/api/Currency.py index 66a52f0..2f75a97 100644 --- a/accounting/api/Currency.py +++ b/accounting/api/Currency.py @@ -1,4 +1,4 @@ - +#task 4 class Balance: def __init__(self, balances): @@ -26,20 +26,27 @@ def __add__(self, other): 'currency': 'IQD', 'sum': self.balanceIQD }] -#task 4 - def currency(self,other): + def __zero__(self,other): IQD=True USD=True if self.balanceIQD ==0 and self.balanceUSD == 0: IQD=False USD=False - else: - if self.balanceIQD>other.balanceIQD : - IQD=True - elif self.balanceIQDother.balanceUSD: - USD=True - elif self.balanceUSDother.balanceIQD : + IQD=True + if self.balanceUSD>other.balanceUSD: + USD=True + return{'IQD':IQD,'USD':USD} + def __smaller__(): + IQD=False + USD=False + if self.balanceIQD