Merge pull request #175

feat(insights:category-explorer): separate current and projected totals
This commit is contained in:
Herculino Trotta
2025-02-18 20:46:28 -03:00
committed by GitHub
4 changed files with 262 additions and 170 deletions
+82 -14
View File
@@ -3,7 +3,7 @@ from django.db.models.functions import Coalesce
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
def get_category_sums_by_account(queryset, category): def get_category_sums_by_account(queryset, category=None):
""" """
Returns income/expense sums per account for a specific category. Returns income/expense sums per account for a specific category.
""" """
@@ -11,10 +11,11 @@ def get_category_sums_by_account(queryset, category):
queryset.filter(category=category) queryset.filter(category=category)
.values("account__name") .values("account__name")
.annotate( .annotate(
income=Coalesce( current_income=Coalesce(
Sum( Sum(
Case( Case(
When(type="IN", then="amount"), When(type="IN", then="amount"),
When(is_paid=True, then="amount"),
default=Value(0), default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30), output_field=DecimalField(max_digits=42, decimal_places=30),
) )
@@ -22,10 +23,35 @@ def get_category_sums_by_account(queryset, category):
Value(0), Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30), output_field=DecimalField(max_digits=42, decimal_places=30),
), ),
expense=Coalesce( current_expense=Coalesce(
Sum( Sum(
Case( Case(
When(type="EX", then=-F("amount")), When(type="EX", then=-F("amount")),
When(is_paid=True, then="amount"),
default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
)
),
Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
),
projected_income=Coalesce(
Sum(
Case(
When(type="IN", then="amount"),
When(is_paid=False, then="amount"),
default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
)
),
Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
),
projected_expense=Coalesce(
Sum(
Case(
When(type="EX", then=-F("amount")),
When(is_paid=False, then="amount"),
default=Value(0), default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30), output_field=DecimalField(max_digits=42, decimal_places=30),
) )
@@ -41,18 +67,26 @@ def get_category_sums_by_account(queryset, category):
"labels": [item["account__name"] for item in sums], "labels": [item["account__name"] for item in sums],
"datasets": [ "datasets": [
{ {
"label": _("Income"), "label": _("Current Income"),
"data": [float(item["income"]) for item in sums], "data": [float(item["current_income"]) for item in sums],
}, },
{ {
"label": _("Expenses"), "label": _("Current Expenses"),
"data": [float(item["expense"]) for item in sums], "data": [float(item["current_expense"]) for item in sums],
},
{
"label": _("Projected Income"),
"data": [float(item["projected_income"]) for item in sums],
},
{
"label": _("Projected Expenses"),
"data": [float(item["projected_expense"]) for item in sums],
}, },
], ],
} }
def get_category_sums_by_currency(queryset, category): def get_category_sums_by_currency(queryset, category=None):
""" """
Returns income/expense sums per currency for a specific category. Returns income/expense sums per currency for a specific category.
""" """
@@ -60,10 +94,11 @@ def get_category_sums_by_currency(queryset, category):
queryset.filter(category=category) queryset.filter(category=category)
.values("account__currency__name") .values("account__currency__name")
.annotate( .annotate(
income=Coalesce( current_income=Coalesce(
Sum( Sum(
Case( Case(
When(type="IN", then="amount"), When(type="IN", then="amount"),
When(is_paid=True, then="amount"),
default=Value(0), default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30), output_field=DecimalField(max_digits=42, decimal_places=30),
) )
@@ -71,10 +106,35 @@ def get_category_sums_by_currency(queryset, category):
Value(0), Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30), output_field=DecimalField(max_digits=42, decimal_places=30),
), ),
expense=Coalesce( current_expense=Coalesce(
Sum( Sum(
Case( Case(
When(type="EX", then=-F("amount")), When(type="EX", then=-F("amount")),
When(is_paid=True, then="amount"),
default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
)
),
Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
),
projected_income=Coalesce(
Sum(
Case(
When(type="IN", then="amount"),
When(is_paid=False, then="amount"),
default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
)
),
Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30),
),
projected_expense=Coalesce(
Sum(
Case(
When(type="EX", then=-F("amount")),
When(is_paid=False, then="amount"),
default=Value(0), default=Value(0),
output_field=DecimalField(max_digits=42, decimal_places=30), output_field=DecimalField(max_digits=42, decimal_places=30),
) )
@@ -90,12 +150,20 @@ def get_category_sums_by_currency(queryset, category):
"labels": [item["account__currency__name"] for item in sums], "labels": [item["account__currency__name"] for item in sums],
"datasets": [ "datasets": [
{ {
"label": _("Income"), "label": _("Current Income"),
"data": [float(item["income"]) for item in sums], "data": [float(item["current_income"]) for item in sums],
}, },
{ {
"label": _("Expenses"), "label": _("Current Expenses"),
"data": [float(item["expense"]) for item in sums], "data": [float(item["current_expense"]) for item in sums],
},
{
"label": _("Projected Income"),
"data": [float(item["projected_income"]) for item in sums],
},
{
"label": _("Projected Expenses"),
"data": [float(item["projected_expense"]) for item in sums],
}, },
], ],
} }
@@ -1,6 +1,7 @@
{% load i18n %} {% load i18n %}
{% if account_data.labels %} {% if account_data.labels %}
<div class="chart-container" style="position: relative; height:400px; width:100%" _="init call setupAccountChart() end"> <div class="chart-container" style="position: relative; height:400px; width:100%"
_="init call setupAccountChart() end">
<canvas id="accountChart"></canvas> <canvas id="accountChart"></canvas>
</div> </div>
@@ -35,7 +36,11 @@
callbacks: { callbacks: {
label: function (context) { label: function (context) {
if (context.parsed.x !== null) { if (context.parsed.x !== null) {
return new Intl.NumberFormat(undefined).format(Math.abs(context.parsed.x)); // Using abs for display return `${context.dataset.label}: ${new Intl.NumberFormat(undefined, {
minimumFractionDigits: 0,
maximumFractionDigits: 30,
roundingMode: 'trunc'
}).format(Math.abs(context.parsed.x))}`;
} }
return ""; return "";
}, },
@@ -52,16 +57,28 @@
labels: accountData.labels, labels: accountData.labels,
datasets: [ datasets: [
{ {
label: "{% trans 'Income' %}", label: "{% trans 'Current Income' %}",
data: accountData.datasets[0].data, data: accountData.datasets[0].data,
backgroundColor: '#4dde80', backgroundColor: '#4dde80',
stack: 'stack0' stack: 'stack0'
}, },
{ {
label: "{% trans 'Expenses' %}", label: "{% trans 'Current Expenses' %}",
data: accountData.datasets[1].data, data: accountData.datasets[1].data,
backgroundColor: '#f87171', backgroundColor: '#f87171',
stack: 'stack0' stack: 'stack0'
},
{
label: "{% trans 'Projected Income' %}",
data: accountData.datasets[2].data,
backgroundColor: '#4dde8080', // Added transparency
stack: 'stack0'
},
{
label: "{% trans 'Projected Expenses' %}",
data: accountData.datasets[3].data,
backgroundColor: '#f8717180', // Added transparency
stack: 'stack0'
} }
] ]
}, },
@@ -11,18 +11,18 @@
function setupCurrencyChart() { function setupCurrencyChart() {
var chartOptions = { var chartOptions = {
indexAxis: 'y', // This makes the chart horizontal indexAxis: 'y',
responsive: true, responsive: true,
maintainAspectRatio: false, maintainAspectRatio: false,
scales: { scales: {
x: { x: {
stacked: true, // Enable stacking on the x-axis stacked: true,
title: { title: {
display: false, display: false,
}, },
}, },
y: { y: {
stacked: true, // Enable stacking on the y-axis stacked: true,
title: { title: {
display: false, display: false,
}, },
@@ -36,7 +36,11 @@
callbacks: { callbacks: {
label: function (context) { label: function (context) {
if (context.parsed.x !== null) { if (context.parsed.x !== null) {
return new Intl.NumberFormat(undefined).format(Math.abs(context.parsed.x)); // Using abs for display return `${context.dataset.label}: ${new Intl.NumberFormat(undefined, {
minimumFractionDigits: 0,
maximumFractionDigits: 30,
roundingMode: 'trunc'
}).format(Math.abs(context.parsed.x))}`;
} }
return ""; return "";
}, },
@@ -53,28 +57,32 @@
labels: currencyData.labels, labels: currencyData.labels,
datasets: [ datasets: [
{ {
label: "{% trans 'Income' %}", label: "{% trans 'Current Income' %}",
data: currencyData.datasets[0].data, data: currencyData.datasets[0].data,
backgroundColor: '#4dde80', backgroundColor: '#4dde80',
stack: 'stack0' stack: 'stack0'
}, },
{ {
label: "{% trans 'Expenses' %}", label: "{% trans 'Current Expenses' %}",
data: currencyData.datasets[1].data, data: currencyData.datasets[1].data,
backgroundColor: '#f87171', backgroundColor: '#f87171',
stack: 'stack0' stack: 'stack0'
},
{
label: "{% trans 'Projected Income' %}",
data: currencyData.datasets[2].data,
backgroundColor: '#4dde8080', // Added transparency
stack: 'stack0'
},
{
label: "{% trans 'Projected Expenses' %}",
data: currencyData.datasets[3].data,
backgroundColor: '#f8717180', // Added transparency
stack: 'stack0'
} }
] ]
}, },
options: { options: chartOptions
...chartOptions,
plugins: {
...chartOptions.plugins,
title: {
display: false,
}
}
}
} }
); );
} }
@@ -2,7 +2,8 @@
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
<form _="install init_tom_select <form _="install init_tom_select
on change trigger updated" id="category-form"> on change trigger updated
init trigger updated" id="category-form">
{% crispy category_form %} {% crispy category_form %}
</form> </form>
@@ -15,7 +16,6 @@
<div class="card-body"> <div class="card-body">
<div id="account-card" class="show-loading" hx-get="{% url 'category_sum_by_account' %}" <div id="account-card" class="show-loading" hx-get="{% url 'category_sum_by_account' %}"
hx-trigger="updated from:window" hx-include="#category-form, #picker-form, #picker-type"> hx-trigger="updated from:window" hx-include="#category-form, #picker-form, #picker-type">
<c-msg.empty title="{% translate "No information to display" %}"></c-msg.empty>
</div> </div>
</div> </div>
</div> </div>
@@ -28,7 +28,6 @@
<div class="card-body"> <div class="card-body">
<div id="currency-card" class="show-loading" hx-get="{% url 'category_sum_by_currency' %}" <div id="currency-card" class="show-loading" hx-get="{% url 'category_sum_by_currency' %}"
hx-trigger="updated from:window" hx-include="#category-form, #picker-form, #picker-type"> hx-trigger="updated from:window" hx-include="#category-form, #picker-form, #picker-type">
<c-msg.empty title="{% translate "No information to display" %}"></c-msg.empty>
</div> </div>
</div> </div>
</div> </div>