Adding account to dashboard

This commit is contained in:
JKuijperM 2026-02-01 19:18:53 +01:00
parent 5a75fc37bc
commit 83fd40085f
3 changed files with 57 additions and 1 deletions

View File

@ -1,5 +1,7 @@
from decimal import Decimal
from django.db import models
from django.conf import settings
from django.db.models import Sum
from django.db.models.fields import related
class Category(models.Model):
@ -45,6 +47,13 @@ class Account(models.Model):
class Meta:
ordering = ['name']
def current_balance(self):
expenses_total = (
self.expenses.aggregate(total=Sum('amount'))['total']
or Decimal('0')
)
return self.initial_balance - expenses_total
def __str__(self):
return self.name

View File

@ -9,7 +9,12 @@
<body>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<h1>Dashboard</h1>
<h1>
Dashboard
{% if selected_account_obj %}
— {{ selected_account_obj.name }}
{% endif %}
</h1>
<!-- ========================= -->
<!-- Filters -->
@ -57,6 +62,18 @@
Comparar con periodo anterior
</label>
<label for="account">Cuenta:</label>
<select name="account" id="account">
<option value="">Todas</option>
{% for acc in accounts %}
<option value="{{ acc.id }}"
{% if selected_account == acc.id %}selected{% endif %}
>
{{ acc.name }}
</option>
{% endfor %}
</select>
<button type="submit">Aplicar</button>
</form>
@ -76,6 +93,11 @@
<div class="kpis">
<div class="kpi">
<h3>Saldo actual</h3>
{{ kpi_balance|floatformat:2 }}€
</div>
<div class="kpi">
<h3>Total</h3>
<p>{{ kpi_total }}€</p>
@ -90,6 +112,7 @@
<h3>Categorías</h3>
<p>{{ kpi_categories }}</p>
</div>
</div>
{% if compare_enabled %}

View File

@ -272,6 +272,12 @@ def dashboard(request):
current_year = date.today().year
account_id = _get_int(request.GET.get('account'))
accounts = Account.objects.filter(
owner=request.user,
active=True,
)
selected_year = year or current_year
selected_month = month
@ -282,6 +288,20 @@ def dashboard(request):
# -----------------
expenses = Expense.objects.filter(owner=request.user)
selected_account_obj = None
kpi_balance = None
if account_id:
expenses = expenses.filter(account_id=account_id)
selected_account_obj = accounts.filter(id=account_id).first()
kpi_balance = selected_account_obj.current_balance()
else:
kpi_balance = (
Accounts.objects.filter(owner.request.user, active=True)
.aggregate(total=Sum('initial_balance'))['total'] or 0
) - (
expenses.aggregate(total=Sum('amount'))['total'] or 0
)
today = date.today()
if period == 'this_month':
@ -482,6 +502,10 @@ def dashboard(request):
'kpi_percentage': percentage,
'category_comparison': category_comparison,
'kpi_trend': kpi_trend,
'accounts':accounts,
'selected_account': account_id,
'selected_account_obj': selected_account_obj,
'kpi_balance': kpi_balance,
})