From e461e876881f0924e75a2d475b920147ec1c527f Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Wed, 25 Dec 2024 23:36:28 -0300 Subject: [PATCH] feat(transactions:all): add summary --- app/apps/transactions/urls.py | 5 + app/apps/transactions/views/transactions.py | 47 +- .../transactions/fragments/summary.html | 431 ++++++++++++++++++ .../transactions/pages/transactions.html | 15 +- 4 files changed, 490 insertions(+), 8 deletions(-) create mode 100644 app/templates/transactions/fragments/summary.html diff --git a/app/apps/transactions/urls.py b/app/apps/transactions/urls.py index c89b1a9..f0402ea 100644 --- a/app/apps/transactions/urls.py +++ b/app/apps/transactions/urls.py @@ -6,6 +6,11 @@ urlpatterns = [ path( "transactions/list/", views.transaction_all_list, name="transactions_all_list" ), + path( + "transactions/summary/", + views.transaction_all_summary, + name="transactions_all_summary", + ), path( "transactions/actions/pay", views.bulk_pay_transactions, diff --git a/app/apps/transactions/views/transactions.py b/app/apps/transactions/views/transactions.py index cdd9d67..262c4ca 100644 --- a/app/apps/transactions/views/transactions.py +++ b/app/apps/transactions/views/transactions.py @@ -11,9 +11,15 @@ from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_http_methods from apps.common.decorators.htmx import only_htmx +from apps.common.utils.dicts import remove_falsey_entries +from apps.transactions.filters import TransactionsFilter from apps.transactions.forms import TransactionForm, TransferForm from apps.transactions.models import Transaction -from apps.transactions.filters import TransactionsFilter +from apps.transactions.utils.calculations import ( + calculate_currency_totals, + calculate_account_totals, + calculate_percentage_distribution, +) from apps.transactions.utils.default_ordering import default_order @@ -194,6 +200,39 @@ def transaction_all_list(request): }, ) - # response.headers["HX-Push-Url"] = ( - # f"{reverse('transactions_all_index')}?{request.GET.urlencode()}" - # ) + +@only_htmx +@login_required +@require_http_methods(["GET"]) +def transaction_all_summary(request): + transactions = Transaction.objects.prefetch_related( + "account", + "account__group", + "category", + "tags", + "account__exchange_currency", + "account__currency", + "installment_plan", + ).all() + + f = TransactionsFilter(request.GET, queryset=transactions) + + currency_data = calculate_currency_totals(f.qs.all(), ignore_empty=True) + currency_percentages = calculate_percentage_distribution(currency_data) + account_data = calculate_account_totals(transactions_queryset=f.qs.all()) + account_percentages = calculate_percentage_distribution(account_data) + + context = { + "income_current": remove_falsey_entries(currency_data, "income_current"), + "income_projected": remove_falsey_entries(currency_data, "income_projected"), + "expense_current": remove_falsey_entries(currency_data, "expense_current"), + "expense_projected": remove_falsey_entries(currency_data, "expense_projected"), + "total_current": remove_falsey_entries(currency_data, "total_current"), + "total_final": remove_falsey_entries(currency_data, "total_final"), + "total_projected": remove_falsey_entries(currency_data, "total_projected"), + "currency_percentages": currency_percentages, + "account_data": account_data, + "account_percentages": account_percentages, + } + + return render(request, "transactions/fragments/summary.html", context) diff --git a/app/templates/transactions/fragments/summary.html b/app/templates/transactions/fragments/summary.html new file mode 100644 index 0000000..d7a3369 --- /dev/null +++ b/app/templates/transactions/fragments/summary.html @@ -0,0 +1,431 @@ +{% load tools %} +{% load i18n %} +{% load currency_display %} + +
+
+
+ {# Income#} +
+ +
+
+
{% translate 'current' %}
+
+
+ {% for currency in income_current.values %} +
+ +
+ {% if currency.exchanged %} +
+ +
+ {% endif %} + {% empty %} +
-
+ {% endfor %} +
+
+
+
+
+
{% translate 'projected' %}
+
+
+ {% for currency in income_projected.values %} +
+ +
+ {% if currency.exchanged %} +
+ +
+ {% endif %} + {% empty %} +
-
+ {% endfor %} +
+
+
+
+ {# Expenses#} +
+ +
+
+
{% translate 'current' %}
+
+
+ {% for currency in expense_current.values %} +
+ +
+ {% if currency.exchanged %} +
+ +
+ {% endif %} + {% empty %} +
-
+ {% endfor %} +
+
+
+
+
+
{% translate 'projected' %}
+
+
+ {% for currency in expense_projected.values %} +
+ +
+ {% if currency.exchanged %} +
+ +
+ {% endif %} + {% empty %} +
-
+ {% endfor %} +
+
+
+
+ {# Total#} +
+ +
+
+
{% translate 'current' %}
+
+
+ {% for currency in total_current.values %} +
+ +
+ {% if currency.exchanged %} +
+ +
+ {% endif %} + {% empty %} +
-
+ {% endfor %} +
+
+
+
+
{% translate 'projected' %}
+
+
+ {% for currency in total_projected.values %} +
+ +
+ {% if currency.exchanged %} +
+ +
+ {% endif %} + {% empty %} +
-
+ {% endfor %} +
+
+
+
+
+ {% for currency in total_final.values %} +
+ +
+ {% if currency.exchanged %} +
+ +
+ {% endif %} + {% empty %} +
-
+ {% endfor %} +
+
+
+
+
+ + {% for p in currency_percentages.values %} +

{{ p.currency.name }} ({{ p.currency.code }})

+ + {% endfor %} +
+
+
+
+
+
+
+ {% for account_id, account in account_data.items %} + {% if not single %} +
+ {% if account.account.group %} + {{ account.account.group }}{% endif %}{{ account.account.name }} +
+ {% endif %} +
+
+
{% translate 'projected income' %}
+
+
+
+ +
+
+ {% if account.exchanged and account.exchanged.income_projected %} +
+ +
+ {% endif %} +
+
+
{% translate 'projected expenses' %}
+
+
+
+
+ +
+
+
+ {% if account.exchanged and account.exchanged.expense_projected %} +
+ +
+ {% endif %} +
+
+
{% translate 'projected total' %}
+
+
+
+ +
+
+ {% if account.exchanged.total_projected and account.exchanged.total_projected %} +
+ +
+ {% endif %} +
+
+
+
{% translate 'current income' %}
+
+
+
+ +
+
+ {% if account.exchanged and account.exchanged.income_current %} +
+ +
+ {% endif %} +
+
+
{% translate 'current expenses' %}
+
+
+
+ +
+
+ {% if account.exchanged and account.exchanged.expense_current %} +
+ +
+ {% endif %} +
+
+
{% translate 'current total' %}
+
+
+
+ +
+
+ {% if account.exchanged and account.exchanged.total_current %} +
+ +
+ {% endif %} +
+
+
+
+
{% translate 'final total' %}
+
+
+
+ +
+
+ {% if account.exchanged and account.exchanged.total_final %} +
+ +
+ {% endif %} +
+ {% with p=account_percentages|get_dict_item:account_id %} +
+ +
+ {% endwith %} +
+ {% empty %} + + {% endfor %} +
+
+
+
diff --git a/app/templates/transactions/pages/transactions.html b/app/templates/transactions/pages/transactions.html index 1076854..e70cdd4 100644 --- a/app/templates/transactions/pages/transactions.html +++ b/app/templates/transactions/pages/transactions.html @@ -5,9 +5,9 @@ {% block title %}{% translate 'Transactions' %}{% endblock %} {% block content %} -
+
-
+
{# Filter transactions#}
@@ -27,7 +27,7 @@
-
+
@@ -37,13 +37,20 @@
-
+
+
+
+
+
{% endblock %}