diff --git a/app/apps/yearly_overview/urls.py b/app/apps/yearly_overview/urls.py index a48b06e..bc02f86 100644 --- a/app/apps/yearly_overview/urls.py +++ b/app/apps/yearly_overview/urls.py @@ -17,7 +17,12 @@ urlpatterns = [ ), path( "yearly/account//", - views.yearly_overview_by_account, + views.index_yearly_overview_by_account, name="yearly_overview_account", ), + path( + "yearly-overview//account/data/", + views.yearly_overview_by_account, + name="yearly_overview_account_data", + ), ] diff --git a/app/apps/yearly_overview/views.py b/app/apps/yearly_overview/views.py index de89196..1571de1 100644 --- a/app/apps/yearly_overview/views.py +++ b/app/apps/yearly_overview/views.py @@ -9,6 +9,7 @@ from django.http import Http404 from django.shortcuts import render, redirect from django.utils import timezone +from apps.accounts.models import Account from apps.currencies.models import Currency from apps.currencies.utils.convert import convert from apps.transactions.models import Transaction @@ -209,15 +210,30 @@ def yearly_overview_by_currency(request, year: int): @login_required def yearly_overview_by_account(request, year: int): - next_year = year + 1 - previous_year = year - 1 + month = request.GET.get("month") + account = request.GET.get("account") - transactions = Transaction.objects.filter( - reference_date__year=year, account__is_archived=False - ) + # Base query filter + filter_params = {"reference_date__year": year, "account__is_archived": False} + + # Add month filter if provided + if month: + month = int(month) + if not 1 <= month <= 12: + raise Http404("Invalid month") + filter_params["reference_date__month"] = month + + # Add account filter if provided + if account: + filter_params["account_id"] = int(account) + + transactions = Transaction.objects.filter(**filter_params) + + # Use TruncYear if no month specified, otherwise use TruncMonth + date_trunc = TruncMonth("reference_date") if month else TruncYear("reference_date") monthly_data = ( - transactions.annotate(month=TruncMonth("reference_date")) + transactions.annotate(month=date_trunc) .select_related( "account__currency", "account__exchange_currency", @@ -308,11 +324,20 @@ def yearly_overview_by_account(request, year: int): .order_by("month", "account__name") ) - all_months = [date(year, month, 1) for month in range(1, 13)] + # Determine which dates to include + if month: + all_months = [date(year, month, 1)] + else: + all_months = [date(year, 1, 1)] # Just one entry for the whole year + + # Get all accounts with their currencies (filtered by account if specified) + accounts_filter = {} + if account: + accounts_filter["account__id"] = int(account) - # Get all accounts with their currencies accounts = ( - transactions.values( + transactions.filter(**accounts_filter) + .values( "account__id", "account__name", "account__group__name", @@ -404,11 +429,6 @@ def yearly_overview_by_account(request, year: int): return render( request, - "yearly_overview/pages/overview_by_account.html", - context={ - "year": year, - "next_year": next_year, - "previous_year": previous_year, - "totals": result, - }, + "yearly_overview/fragments/account_data.html", + context={"year": year, "totals": result, "single": True if account else False}, ) diff --git a/app/templates/yearly_overview/fragments/account_data.html b/app/templates/yearly_overview/fragments/account_data.html new file mode 100644 index 0000000..0dfe368 --- /dev/null +++ b/app/templates/yearly_overview/fragments/account_data.html @@ -0,0 +1,181 @@ +{% load i18n %} +
+ {% for date, x in totals.items %} +
+ {% for id, account in x.items %} + {% if not single %} +
+ {% if account.group %} + {{ account.group }}{% endif %}{{ account.name }} +
+ {% endif %} +
+
+
{% translate 'projected income' %}
+
+
+
+ +
+
+ {% if account.exchange_currency and account.exchange_income_unpaid %} +
+ +
+ {% endif %} +
+
+
{% translate 'projected expenses' %}
+
+
+
+
+ +
+
+
+ {% if account.exchange_currency and account.exchange_expense_unpaid %} +
+ +
+ {% endif %} +
+
+
{% translate 'projected total' %}
+
+
+
+ +
+
+ {% if account.exchange_currency and account.exchange_balance_unpaid %} +
+ +
+ {% endif %} +
+
+
+
{% translate 'current income' %}
+
+
+
+ +
+
+ {% if account.exchange_currency and account.exchange_income_paid %} +
+ +
+ {% endif %} +
+
+
{% translate 'current expenses' %}
+
+
+
+ +
+
+ {% if account.exchange_currency and account.exchange_expense_paid %} +
+ +
+ {% endif %} +
+
+
{% translate 'current total' %}
+
+
+
+ +
+
+ {% if account.exchange_currency and account.exchange_balance_paid %} +
+ +
+ {% endif %} +
+
+
+
+
{% translate 'final total' %}
+
+
+
+ +
+
+ {% if account.exchange_currency and account.exchange_balance_total %} +
+ +
+ {% endif %} +
+ {% empty %} + + {% endfor %} +
+ {% endfor %} +
diff --git a/app/templates/yearly_overview/pages/overview_by_account.html b/app/templates/yearly_overview/pages/overview_by_account.html index 5b6f571..c63633a 100644 --- a/app/templates/yearly_overview/pages/overview_by_account.html +++ b/app/templates/yearly_overview/pages/overview_by_account.html @@ -1,6 +1,4 @@ {% extends "layouts/base.html" %} -{% load currency_display %} -{% load crispy_forms_tags %} {% load i18n %} {% load month_name %} {% load static %} @@ -90,201 +88,77 @@ -
- {% for date, x in totals.items %} -
-
-
- {{ forloop.counter }} -
-
-
{{ date.month|month_name }}
-
- {% for id, account in x.items %} -
-

- -

-
-
-
-
-
-
-
{% translate 'projected income' %}
-
-
-
- -
-
- {% if account.exchange_currency and account.exchange_income_unpaid %} -
- -
- {% endif %} -
-
-
{% translate 'projected expenses' %}
-
-
-
-
- -
-
-
- {% if account.exchange_currency and account.exchange_expense_unpaid %} -
- -
- {% endif %} -
-
-
{% translate 'projected total' %}
-
-
-
- -
-
- {% if account.exchange_currency and account.exchange_balance_unpaid %} -
- -
- {% endif %} -
-
-
-
-
-
{% translate 'current income' %}
-
-
-
- -
-
- {% if account.exchange_currency and account.exchange_income_paid %} -
- -
- {% endif %} -
-
-
{% translate 'current expenses' %}
-
-
-
- -
-
- {% if account.exchange_currency and account.exchange_expense_paid %} -
- -
- {% endif %} -
-
-
{% translate 'current total' %}
-
-
-
- -
-
- {% if account.exchange_currency and account.exchange_balance_paid %} -
- -
- {% endif %} -
-
-
-
-
-
{% translate 'final total' %}
-
-
-
- -
-
- {% if account.exchange_currency and account.exchange_balance_total %} -
- -
- {% endif %} -
-
-
-
-
+
+
+
-
- {% endfor %} + +
+
+ +
+ +
+
+
+
{% endblock %}