feat: change how values are colored on yearly overviews and add exchange rates to accounts

This commit is contained in:
Herculino Trotta
2024-11-01 17:23:20 -03:00
parent 4fe1f8adcd
commit 47f4043db6
3 changed files with 137 additions and 20 deletions

View File

@@ -10,6 +10,8 @@ from django.db.models.expressions import Case, When
from django.db.models.functions import Concat
from apps.transactions.models import Transaction
from apps.currencies.utils.convert import convert
from apps.currencies.models import Currency
@login_required
@@ -196,14 +198,24 @@ def yearly_overview_by_account(request, year: int):
monthly_data = (
transactions.annotate(month=TruncMonth("reference_date"))
.select_related(
"account__currency",
"account__exchange_currency",
)
.values(
"month",
"account__id",
"account__name",
"account__currency",
"account__exchange_currency",
"account__currency__code",
"account__currency__prefix",
"account__currency__suffix",
"account__currency__decimal_places",
"account__exchange_currency__code",
"account__exchange_currency__prefix",
"account__exchange_currency__suffix",
"account__exchange_currency__decimal_places",
)
.annotate(
income_paid=Coalesce(
@@ -276,10 +288,9 @@ def yearly_overview_by_account(request, year: int):
.order_by("month", "account__name")
)
# Create a list of all months in the year
all_months = [date(year, month, 1) for month in range(1, 13)]
# Get all accounts that had transactions in this year
# Get all accounts with their currencies
accounts = (
transactions.values(
"account__id",
@@ -289,12 +300,18 @@ def yearly_overview_by_account(request, year: int):
"account__currency__prefix",
"account__currency__suffix",
"account__currency__decimal_places",
"account__exchange_currency__code",
"account__exchange_currency__prefix",
"account__exchange_currency__suffix",
"account__exchange_currency__decimal_places",
)
.distinct()
.order_by("account__name")
)
# Create a dictionary to store the final result
# Get Currency objects for conversion
currencies = {currency.id: currency for currency in Currency.objects.all()}
result = {
month: {
account["account__id"]: {
@@ -306,6 +323,18 @@ def yearly_overview_by_account(request, year: int):
"suffix": account["account__currency__suffix"],
"decimal_places": account["account__currency__decimal_places"],
},
"exchange_currency": (
{
"code": account["account__exchange_currency__code"],
"prefix": account["account__exchange_currency__prefix"],
"suffix": account["account__exchange_currency__suffix"],
"decimal_places": account[
"account__exchange_currency__decimal_places"
],
}
if account["account__exchange_currency__code"]
else None
),
"income_paid": Decimal("0"),
"expense_paid": Decimal("0"),
"income_unpaid": Decimal("0"),
@@ -334,6 +363,24 @@ def yearly_overview_by_account(request, year: int):
"balance_total",
]:
result[month][account_id][field] = entry[field]
if result[month][account_id]["exchange_currency"]:
from_currency = currencies[entry["account__currency"]]
to_currency = currencies[entry["account__exchange_currency"]]
if entry[field] > 0 or entry[field] < 0:
converted_amount, prefix, suffix, decimal_places = convert(
amount=entry[field],
from_currency=from_currency,
to_currency=to_currency,
)
if isinstance(converted_amount, Decimal):
print(converted_amount)
result[month][account_id][
f"exchange_{field}"
] = converted_amount
else:
result[month][account_id][f"exchange_{field}"] = Decimal(0)
return render(
request,
@@ -343,6 +390,5 @@ def yearly_overview_by_account(request, year: int):
"next_year": next_year,
"previous_year": previous_year,
"totals": result,
"accounts": accounts,
},
)

View File

@@ -105,7 +105,7 @@
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#{{ account.name|slugify }}-{{ forloop.parentloop.counter }}" aria-expanded="false" aria-controls="{{ account.name|slugify }}-{{ forloop.counter }}">
<span class="badge text-bg-primary me-2">{{ account.group }}</span>{{ account.name }}
{% if account.group %}<span class="badge text-bg-primary me-2">{{ account.group }}</span>{% endif %}{{ account.name }}
</button>
</h2>
<div id="{{ account.name|slugify }}-{{ forloop.parentloop.counter }}" class="accordion-collapse collapse">
@@ -117,7 +117,7 @@
<div class="tw-text-gray-400">{% translate 'projected income' %}</div>
</div>
<div class="dotted-line flex-grow-1"></div>
<div class="text-end font-monospace tw-text-green-300">
<div class="text-end font-monospace tw-text-green-400">
<c-amount.display
:amount="account.income_unpaid"
:prefix="account.currency.prefix"
@@ -125,25 +125,45 @@
:decimal_places="account.currency.decimal_places"></c-amount.display>
</div>
</div>
{% if account.exchange_currency and account.exchange_income_unpaid %}
<div class="text-end font-monospace tw-text-gray-500">
<c-amount.display
:amount="account.exchange_income_unpaid"
:prefix="account.exchange_currency.prefix"
:suffix="account.exchange_currency.suffix"
:decimal_places="account.exchange_currency.decimal_places"></c-amount.display>
</div>
{% endif %}
<div class="d-flex justify-content-between align-items-baseline mt-2">
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'projected expenses' %}</div>
</div>
<div class="dotted-line flex-grow-1"></div>
<div class="text-end font-monospace tw-text-red-300">
<c-amount.display
:amount="account.expense_unpaid"
:prefix="account.currency.prefix"
:suffix="account.currency.suffix"
:decimal_places="account.currency.decimal_places"></c-amount.display>
<div>
<div class="text-end font-monospace tw-text-red-400">
<c-amount.display
:amount="account.expense_unpaid"
:prefix="account.currency.prefix"
:suffix="account.currency.suffix"
:decimal_places="account.currency.decimal_places"></c-amount.display>
</div>
</div>
</div>
{% if account.exchange_currency and account.exchange_expense_unpaid %}
<div class="text-end font-monospace tw-text-gray-500">
<c-amount.display
:amount="account.exchange_expense_unpaid"
:prefix="account.exchange_currency.prefix"
:suffix="account.exchange_currency.suffix"
:decimal_places="account.exchange_currency.decimal_places"></c-amount.display>
</div>
{% endif %}
<div class="d-flex justify-content-between align-items-baseline mt-2">
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'projected total' %}</div>
</div>
<div class="dotted-line flex-grow-1"></div>
<div class="text-end font-monospace tw-text-yellow-300">
<div class="text-end font-monospace {% if account.balance_unpaid > 0 %}tw-text-green-400{% elif account.balance_unpaid < 0 %}tw-text-red-400{% endif %}">
<c-amount.display
:amount="account.balance_unpaid"
:prefix="account.currency.prefix"
@@ -151,6 +171,15 @@
:decimal_places="account.currency.decimal_places"></c-amount.display>
</div>
</div>
{% if account.exchange_currency and account.exchange_balance_unpaid %}
<div class="text-end font-monospace tw-text-gray-500">
<c-amount.display
:amount="account.exchange_balance_unpaid"
:prefix="account.exchange_currency.prefix"
:suffix="account.exchange_currency.suffix"
:decimal_places="account.exchange_currency.decimal_places"></c-amount.display>
</div>
{% endif %}
</div>
<hr class="my-3 d-block d-lg-none">
<div class="col-12 col-lg-6">
@@ -167,6 +196,15 @@
:decimal_places="account.currency.decimal_places"></c-amount.display>
</div>
</div>
{% if account.exchange_currency and account.exchange_income_paid %}
<div class="text-end font-monospace tw-text-gray-500">
<c-amount.display
:amount="account.exchange_income_paid"
:prefix="account.exchange_currency.prefix"
:suffix="account.exchange_currency.suffix"
:decimal_places="account.exchange_currency.decimal_places"></c-amount.display>
</div>
{% endif %}
<div class="d-flex justify-content-between align-items-baseline mt-2">
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'current expenses' %}</div>
@@ -180,12 +218,21 @@
:decimal_places="account.currency.decimal_places"></c-amount.display>
</div>
</div>
{% if account.exchange_currency and account.exchange_expense_paid %}
<div class="text-end font-monospace tw-text-gray-500">
<c-amount.display
:amount="account.exchange_expense_paid"
:prefix="account.exchange_currency.prefix"
:suffix="account.exchange_currency.suffix"
:decimal_places="account.exchange_currency.decimal_places"></c-amount.display>
</div>
{% endif %}
<div class="d-flex justify-content-between align-items-baseline mt-2">
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'current total' %}</div>
</div>
<div class="dotted-line flex-grow-1"></div>
<div class="text-end font-monospace tw-text-yellow-400">
<div class="text-end font-monospace {% if account.balance_paid > 0 %}tw-text-green-400{% elif account.balance_paid < 0 %}tw-text-red-400{% endif %}">
<c-amount.display
:amount="account.balance_paid"
:prefix="account.currency.prefix"
@@ -193,6 +240,15 @@
:decimal_places="account.currency.decimal_places"></c-amount.display>
</div>
</div>
{% if account.exchange_currency and account.exchange_balance_paid %}
<div class="text-end font-monospace tw-text-gray-500">
<c-amount.display
:amount="account.exchange_balance_paid"
:prefix="account.exchange_currency.prefix"
:suffix="account.exchange_currency.suffix"
:decimal_places="account.exchange_currency.decimal_places"></c-amount.display>
</div>
{% endif %}
</div>
<hr class="my-3">
<div>
@@ -201,7 +257,7 @@
<div class="tw-text-gray-400">{% translate 'final total' %}</div>
</div>
<div class="dotted-line flex-grow-1"></div>
<div class="text-end font-monospace tw-text-green-300">
<div class="text-end font-monospace {% if account.balance_total > 0 %}tw-text-green-400{% elif account.balance_total < 0 %}tw-text-red-400{% endif %}">
<c-amount.display
:amount="account.balance_total"
:prefix="account.currency.prefix"
@@ -209,6 +265,15 @@
:decimal_places="account.currency.decimal_places"></c-amount.display>
</div>
</div>
{% if account.exchange_currency and account.exchange_balance_total %}
<div class="text-end font-monospace tw-text-gray-500">
<c-amount.display
:amount="account.exchange_balance_total"
:prefix="account.exchange_currency.prefix"
:suffix="account.exchange_currency.suffix"
:decimal_places="account.exchange_currency.decimal_places"></c-amount.display>
</div>
{% endif %}
</div>
</div>
</div>

View File

@@ -104,7 +104,7 @@
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'projected income' %}</div>
</div>
<div class="text-end font-monospace tw-text-green-300">
<div class="text-end font-monospace">
{% for entry in x.income_unpaid %}
<c-amount.display
:amount="entry.amount"
@@ -120,7 +120,7 @@
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'projected expenses' %}</div>
</div>
<div class="text-end font-monospace tw-text-red-300">
<div class="text-end font-monospace">
{% for entry in x.expense_unpaid %}
<c-amount.display
:amount="entry.amount"
@@ -136,13 +136,15 @@
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'projected total' %}</div>
</div>
<div class="text-end font-monospace tw-text-yellow-300">
<div class="text-end font-monospace">
{% for entry in x.balance_unpaid %}
<div class="{% if entry.amount > 0 %}tw-text-green-400{% elif entry.amount < 0 %}tw-text-red-400{% endif %}">
<c-amount.display
:amount="entry.amount"
:prefix="entry.prefix"
:suffix="entry.suffix"
:decimal_places="entry.decimal_places"></c-amount.display>
</div>
{% empty %}
<div>-</div>
{% endfor %}
@@ -153,7 +155,7 @@
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'current income' %}</div>
</div>
<div class="text-end font-monospace tw-text-green-400">
<div class="text-end font-monospace">
{% for entry in x.income_paid %}
<c-amount.display
:amount="entry.amount"
@@ -169,7 +171,7 @@
<div class="text-end font-monospace">
<div class="tw-text-gray-400">{% translate 'current expenses' %}</div>
</div>
<div class="text-end font-monospace tw-text-red-400">
<div class="text-end font-monospace">
{% for entry in x.expense_paid %}
<c-amount.display
:amount="entry.amount"
@@ -187,11 +189,13 @@
</div>
<div class="text-end font-monospace tw-text-yellow-400">
{% for entry in x.balance_paid %}
<div class="{% if entry.amount > 0 %}tw-text-green-400{% elif entry.amount < 0 %}tw-text-red-400{% endif %}">
<c-amount.display
:amount="entry.amount"
:prefix="entry.prefix"
:suffix="entry.suffix"
:decimal_places="entry.decimal_places"></c-amount.display>
</div>
{% empty %}
<div>-</div>
{% endfor %}
@@ -204,11 +208,13 @@
</div>
<div class="text-end font-monospace">
{% for entry in x.balance_total %}
<div class="{% if entry.amount > 0 %}tw-text-green-400{% elif entry.amount < 0 %}tw-text-red-400{% endif %}">
<c-amount.display
:amount="entry.amount"
:prefix="entry.prefix"
:suffix="entry.suffix"
:decimal_places="entry.decimal_places"></c-amount.display>
</div>
{% empty %}
<div>-</div>
{% endfor %}