From 06e61ed1bfa605df39544c54ec2b7c859d35b95c Mon Sep 17 00:00:00 2001 From: JKuijperM Date: Fri, 24 Jul 2026 08:06:22 +0200 Subject: [PATCH] Minor changes --- expenses_manager/expenses/models.py | 34 +++++++++++++------ .../expenses/templates/expenses/home.html | 11 +++--- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/expenses_manager/expenses/models.py b/expenses_manager/expenses/models.py index 3f8c995..95339b9 100644 --- a/expenses_manager/expenses/models.py +++ b/expenses_manager/expenses/models.py @@ -151,19 +151,33 @@ class Account(models.Model): def monthly_net(self, year=None): year = year or date.today().year - data = [] + incomes = ( + self.incomes.filter(date__year=year) + .annotate(month=ExtractMonth("date")) + .values("month") + .annotate(total=Sum("amount")) + ) - for month in range(1, 13): - income = self.incomes.filter(date__year=year, date__month=month).aggregate( - total=Sum("amount") - )["total"] or Decimal("0") - expense = self.expenses.filter( - date__year=year, date__month=month - ).aggregate(total=Sum("amount"))["total"] or Decimal("0") + expenses = ( + self.expenses.filter(date__year=year) + .annotate(month=ExtractMonth("date")) + .values("month") + .annotate(total=Sum("amount")) + ) - data.append({"month": month, "net": float(income - expense)}) + income_map = {i["month"]: Decimal(str(i["total"] or 0)) for i in incomes} + expenses_map = {e["month"]: Decimal(str(e["total"] or 0)) for e in expenses} - return data + return [ + { + "month": month, + "net": float( + income_map.get(month, Decimal("0")) + - expenses_map.get(month, Decimal("0")) + ), + } + for month in range(1, 13) + ] def __str__(self): return self.name diff --git a/expenses_manager/expenses/templates/expenses/home.html b/expenses_manager/expenses/templates/expenses/home.html index 579718b..90a45da 100644 --- a/expenses_manager/expenses/templates/expenses/home.html +++ b/expenses_manager/expenses/templates/expenses/home.html @@ -68,14 +68,13 @@ {% for goal in goals %}
{{ goal.name }} + {% if goal.is_exceeded %} + Excedido + {% endif %}
-
+