From 14e0ee7a762128dd7f4ae058cae0447f15b0c092 Mon Sep 17 00:00:00 2001 From: JKuijperM Date: Thu, 11 Jun 2026 18:06:43 +0200 Subject: [PATCH] Added the comparative table --- .../templates/expenses/dashboard.html | 39 ++++++++++++++++++- expenses_manager/expenses/views.py | 17 +++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/expenses_manager/expenses/templates/expenses/dashboard.html b/expenses_manager/expenses/templates/expenses/dashboard.html index d31dad1..d49a356 100644 --- a/expenses_manager/expenses/templates/expenses/dashboard.html +++ b/expenses_manager/expenses/templates/expenses/dashboard.html @@ -142,11 +142,24 @@ Categorías {{ kpi_categories }} + +
+ Media diaria + {{ daily_average|floatformat:2 }}€ / día +
+ + {% if selected_month == today.month and selected_year == today.year %} +
+ Proyección fin de mes + {{ projected_end_of_month|floatformat:2 }}€ + Basado en tu ritmo de gasto actual +
+ {% endif %} {% if compare_enabled %}
-

📊 Resumen comparativo

+

Resumen comparativo

Gastos periodo actual: {{ kpi_total|floatformat:2 }} €
Gastos periodo anterior: {{ kpi_previous_total|floatformat:2 }} €
@@ -158,6 +171,30 @@ {% if kpi_trend == "up" %}⚠️ Has gastado más que el periodo pasado. {% else %} ¡Bien! Has reducido tus gastos. {% endif %}

+ +

Desglose de cambios por categoría

+ + + + + + + + + + + {% for cat in category_comparison %} + + + + + + + {% endfor %} + +
CategoríaPeriodo AnteriorPeriodo ActualVariación
{{ cat.category }}{{ cat.previous|floatformat:2 }}€{{ cat.current|floatformat:2 }}€ + {% if cat.difference > 0 %}+{% endif %}{{ cat.difference|floatformat:2 }}€ +
{% endif %} diff --git a/expenses_manager/expenses/views.py b/expenses_manager/expenses/views.py index 9194952..0f86762 100644 --- a/expenses_manager/expenses/views.py +++ b/expenses_manager/expenses/views.py @@ -354,6 +354,8 @@ def dashboard(request): }) #Graphic + daily_average = 0 + projected_end_of_month = 0 if selected_month: by_day_qs = ( expenses_filtered.annotate(day=ExtractDay("date")) @@ -364,6 +366,14 @@ def dashboard(request): import calendar num_days = calendar.monthrange(selected_year, selected_month)[1] + if selected_year == today.year and selected_month == today.month: + days_passed = today.day + else: + days_passed = calendar.monthrange(selected_year, selected_month)[1] + + daily_average = total_amount / days_passed if days_passed > 0 else 0 + total_days_in_month = calendar.monthrange(selected_year, selected_month)[1] + projected_end_of_month = daily_average * total_days_in_month chart_labels = list(range(1, num_days + 1)) chart_totals = [day_totals.get(d, 0) for d in chart_labels] @@ -378,7 +388,7 @@ def dashboard(request): chart_labels = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"] chart_totals = [month_totals.get(m, 0) for m in range(1, 13)] chart_type = "month" - + year_list = ( Expense.objects.filter(owner=request.user) .annotate(year=ExtractYear("date")) @@ -462,13 +472,14 @@ def dashboard(request): # Recent expenses recent_expenses = Expense.objects.filter(owner=request.user).order_by('-date', '-id')[:7] - + # Send the data to the dashboard return render( request, "expenses/dashboard.html", { "active_menu": "dashboard", + "today": today, "by_category": by_category, "by_category_chart": by_category_chart, "chart_labels": chart_labels, @@ -495,6 +506,8 @@ def dashboard(request): "period": period, "goals": goals, "recent_expenses": recent_expenses, + "daily_average": daily_average, + "projected_end_of_month": projected_end_of_month, }, )