mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-25 10:08:36 +02:00
feat(insights:category-explorer): separate current and projected totals
This commit is contained in:
@@ -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,83 +1,100 @@
|
|||||||
{% 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%"
|
||||||
<canvas id="accountChart"></canvas>
|
_="init call setupAccountChart() end">
|
||||||
</div>
|
<canvas id="accountChart"></canvas>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// Get the data from your Django view (passed as JSON)
|
// Get the data from your Django view (passed as JSON)
|
||||||
var accountData = {{ account_data|safe }};
|
var accountData = {{ account_data|safe }};
|
||||||
|
|
||||||
function setupAccountChart() {
|
function setupAccountChart() {
|
||||||
var chartOptions = {
|
var chartOptions = {
|
||||||
indexAxis: 'y', // This makes the chart horizontal
|
indexAxis: 'y', // This makes the chart horizontal
|
||||||
responsive: true,
|
responsive: true,
|
||||||
maintainAspectRatio: false,
|
maintainAspectRatio: false,
|
||||||
scales: {
|
scales: {
|
||||||
x: {
|
x: {
|
||||||
stacked: true, // Enable stacking on the x-axis
|
stacked: true, // Enable stacking on the x-axis
|
||||||
title: {
|
title: {
|
||||||
display: false,
|
display: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
y: {
|
y: {
|
||||||
stacked: true, // Enable stacking on the y-axis
|
stacked: true, // Enable stacking on the y-axis
|
||||||
title: {
|
title: {
|
||||||
display: false,
|
display: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
plugins: {
|
plugins: {
|
||||||
legend: {
|
legend: {
|
||||||
display: false,
|
display: false,
|
||||||
},
|
},
|
||||||
tooltip: {
|
tooltip: {
|
||||||
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,
|
||||||
return "";
|
maximumFractionDigits: 30,
|
||||||
},
|
roundingMode: 'trunc'
|
||||||
}
|
}).format(Math.abs(context.parsed.x))}`;
|
||||||
}
|
}
|
||||||
}
|
return "";
|
||||||
};
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
new Chart(
|
new Chart(
|
||||||
document.getElementById('accountChart'),
|
document.getElementById('accountChart'),
|
||||||
{
|
{
|
||||||
type: 'bar',
|
type: 'bar',
|
||||||
data: {
|
data: {
|
||||||
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' %}",
|
||||||
options: {
|
data: accountData.datasets[2].data,
|
||||||
...chartOptions,
|
backgroundColor: '#4dde8080', // Added transparency
|
||||||
plugins: {
|
stack: 'stack0'
|
||||||
...chartOptions.plugins,
|
},
|
||||||
title: {
|
{
|
||||||
display: false,
|
label: "{% trans 'Projected Expenses' %}",
|
||||||
}
|
data: accountData.datasets[3].data,
|
||||||
}
|
backgroundColor: '#f8717180', // Added transparency
|
||||||
}
|
stack: 'stack0'
|
||||||
}
|
}
|
||||||
);
|
]
|
||||||
}
|
},
|
||||||
</script>
|
options: {
|
||||||
|
...chartOptions,
|
||||||
|
plugins: {
|
||||||
|
...chartOptions.plugins,
|
||||||
|
title: {
|
||||||
|
display: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
{% else %}
|
{% else %}
|
||||||
<c-msg.empty title="{% translate "No information to display" %}"></c-msg.empty>
|
<c-msg.empty title="{% translate "No information to display" %}"></c-msg.empty>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -1,84 +1,92 @@
|
|||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% if currency_data.labels %}
|
{% if currency_data.labels %}
|
||||||
<div class="chart-container" style="position: relative; height:400px; width:100%"
|
<div class="chart-container" style="position: relative; height:400px; width:100%"
|
||||||
_="init call setupCurrencyChart() end">
|
_="init call setupCurrencyChart() end">
|
||||||
<canvas id="currencyChart"></canvas>
|
<canvas id="currencyChart"></canvas>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// Get the data from your Django view (passed as JSON)
|
// Get the data from your Django view (passed as JSON)
|
||||||
var currencyData = {{ currency_data|safe }};
|
var currencyData = {{ currency_data|safe }};
|
||||||
|
|
||||||
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,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
plugins: {
|
plugins: {
|
||||||
legend: {
|
legend: {
|
||||||
display: false,
|
display: false,
|
||||||
},
|
},
|
||||||
tooltip: {
|
tooltip: {
|
||||||
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,
|
||||||
return "";
|
maximumFractionDigits: 30,
|
||||||
},
|
roundingMode: 'trunc'
|
||||||
}
|
}).format(Math.abs(context.parsed.x))}`;
|
||||||
}
|
}
|
||||||
}
|
return "";
|
||||||
};
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
new Chart(
|
new Chart(
|
||||||
document.getElementById('currencyChart'),
|
document.getElementById('currencyChart'),
|
||||||
{
|
{
|
||||||
type: 'bar',
|
type: 'bar',
|
||||||
data: {
|
data: {
|
||||||
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' %}",
|
||||||
options: {
|
data: currencyData.datasets[2].data,
|
||||||
...chartOptions,
|
backgroundColor: '#4dde8080', // Added transparency
|
||||||
plugins: {
|
stack: 'stack0'
|
||||||
...chartOptions.plugins,
|
},
|
||||||
title: {
|
{
|
||||||
display: false,
|
label: "{% trans 'Projected Expenses' %}",
|
||||||
}
|
data: currencyData.datasets[3].data,
|
||||||
}
|
backgroundColor: '#f8717180', // Added transparency
|
||||||
}
|
stack: 'stack0'
|
||||||
}
|
}
|
||||||
);
|
]
|
||||||
}
|
},
|
||||||
</script>
|
options: chartOptions
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
{% else %}
|
{% else %}
|
||||||
<c-msg.empty title="{% translate "No information to display" %}"></c-msg.empty>
|
<c-msg.empty title="{% translate "No information to display" %}"></c-msg.empty>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user