diff --git a/expenses_manager/expenses/models.py b/expenses_manager/expenses/models.py index 7316932..9c96007 100644 --- a/expenses_manager/expenses/models.py +++ b/expenses_manager/expenses/models.py @@ -1,8 +1,10 @@ +from datetime import date 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 +from django.db.models.functions import ExtractMonth class Category(models.Model): name = models.CharField(max_length=100) @@ -58,6 +60,41 @@ class Account(models.Model): ) return self.initial_balance + income_total - expenses_total + def monthly_balance(self, year=None): + year = year or date.today().year + + incomes = ( + self.incomes + .filter(date__year=year) + .annotate(month=ExtractMonth('date')) + .values('month') + .annotate(total=Sum('amount')) + ) + + expenses = ( + self.expenses + .filter(date__year=year) + .annotate(month=ExtractMonth('date')) + .values('month') + .annotate(total=Sum('amount')) + ) + + income_map = {i['month']: i['total'] for i in incomes} + expenses_map = {e['month']: e['total'] for e in expenses} + + balance = self.initial_balance + data = [] + + for month in range(1, 13): + balance += income_map.get(month, 0) + balance -= expenses_map.get(month, 0) + + data.append({ + 'month': month, + 'balance': float(balance), + }) + return data + def __str__(self): return self.name diff --git a/expenses_manager/expenses/templates/expenses/dashboard.html b/expenses_manager/expenses/templates/expenses/dashboard.html index 78df8c6..1df92a9 100644 --- a/expenses_manager/expenses/templates/expenses/dashboard.html +++ b/expenses_manager/expenses/templates/expenses/dashboard.html @@ -225,7 +225,7 @@ -
Saldo actual: {{ acc.current_balance|floatformat:2 }} €
+ + +