Created graphs in the dashboard with the evolution by account
This commit is contained in:
parent
3ba1dbe4df
commit
4e94aa3713
@ -1,8 +1,10 @@
|
|||||||
|
from datetime import date
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
from django.db.models.fields import related
|
from django.db.models.fields import related
|
||||||
|
from django.db.models.functions import ExtractMonth
|
||||||
|
|
||||||
class Category(models.Model):
|
class Category(models.Model):
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
@ -58,6 +60,41 @@ class Account(models.Model):
|
|||||||
)
|
)
|
||||||
return self.initial_balance + income_total - expenses_total
|
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):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|||||||
@ -225,7 +225,7 @@
|
|||||||
<!-- Chart -->
|
<!-- Chart -->
|
||||||
<!-- ========================= -->
|
<!-- ========================= -->
|
||||||
|
|
||||||
<h3>Distribución mensual (año completo)</h3>
|
<h2>Distribución mensual (año completo)</h2>
|
||||||
|
|
||||||
<canvas id="expensesChart"></canvas>
|
<canvas id="expensesChart"></canvas>
|
||||||
|
|
||||||
@ -249,5 +249,44 @@
|
|||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
|
<h2>Evolución anual por cuenta ({{ selected_year }})</h2>
|
||||||
|
|
||||||
|
<div class="dashboard-grid">
|
||||||
|
{% for acc in accounts_charts %}
|
||||||
|
<div class="card card-chart">
|
||||||
|
<h3>{{ acc.name }}</h3>
|
||||||
|
<p><strong>Saldo actual:</strong> {{ acc.current_balance|floatformat:2 }} €</p>
|
||||||
|
|
||||||
|
<canvas id="accountChart{{ acc.id }}"></canvas>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const monthLabels = {{ chart_labels|safe }};
|
||||||
|
|
||||||
|
{% for acc in accounts_charts %}
|
||||||
|
new Chart(
|
||||||
|
document.getElementById('accountChart{{ acc.id }}'),
|
||||||
|
{
|
||||||
|
type: 'line',
|
||||||
|
data: {
|
||||||
|
labels: monthLabels,
|
||||||
|
datasets: [{
|
||||||
|
label: 'Saldo',
|
||||||
|
data: {{ acc.data|safe }},
|
||||||
|
fill: true,
|
||||||
|
tension: 0.3,
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
responsive: true,
|
||||||
|
maintainAspectRatio: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
{% endfor %}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@ -482,6 +482,21 @@ def dashboard(request):
|
|||||||
'difference_abs': abs(difference),
|
'difference_abs': abs(difference),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# ------------------
|
||||||
|
# Previous expenses by category
|
||||||
|
# ------------------
|
||||||
|
accounts_charts = []
|
||||||
|
for account in accounts:
|
||||||
|
monthly_data = account.monthly_balance(selected_year)
|
||||||
|
m_balance = [row['balance'] for row in monthly_data]
|
||||||
|
|
||||||
|
accounts_charts.append({
|
||||||
|
'id': account.id,
|
||||||
|
'name': account.name,
|
||||||
|
'data': m_balance,
|
||||||
|
'current_balance': account.current_balance(),
|
||||||
|
})
|
||||||
|
|
||||||
# Send the data to the dashboard
|
# Send the data to the dashboard
|
||||||
return render(request, 'expenses/dashboard.html', {
|
return render(request, 'expenses/dashboard.html', {
|
||||||
'by_category': by_category,
|
'by_category': by_category,
|
||||||
@ -506,6 +521,7 @@ def dashboard(request):
|
|||||||
'selected_account': account_id,
|
'selected_account': account_id,
|
||||||
'selected_account_obj': selected_account_obj,
|
'selected_account_obj': selected_account_obj,
|
||||||
'kpi_balance': kpi_balance,
|
'kpi_balance': kpi_balance,
|
||||||
|
'accounts_charts': accounts_charts,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user