fix: wrong calculations

This commit is contained in:
Herculino Trotta
2024-11-18 10:15:52 -03:00
parent 1544c6cca2
commit b3896df466
3 changed files with 65 additions and 49 deletions

View File

@@ -15,17 +15,23 @@ from apps.transactions.utils.calculations import (
def net_worth_main(request):
transactions_queryset = Transaction.objects.filter(
transactions_currency_queryset = Transaction.objects.filter(
is_paid=True, account__is_archived=False
).order_by(
"account__currency__name",
)
transactions_account_queryset = Transaction.objects.filter(
is_paid=True, account__is_archived=False
).order_by(
"account__group__name",
"account__name",
)
currency_net_worth = calculate_currency_totals(
transactions_queryset=transactions_queryset
transactions_queryset=transactions_currency_queryset
)
account_net_worth = calculate_account_totals(
transactions_queryset=transactions_queryset
transactions_queryset=transactions_account_queryset
)
historical_currency_net_worth = calculate_historical_currency_net_worth()

View File

@@ -11,55 +11,65 @@ from apps.currencies.models import Currency
def calculate_currency_totals(transactions_queryset, ignore_empty=False):
# Prepare the aggregation expressions
currency_totals = transactions_queryset.values(
"account__currency",
"account__currency__code",
"account__currency__name",
"account__currency__decimal_places",
"account__currency__prefix",
"account__currency__suffix",
"account__currency__exchange_currency",
).annotate(
expense_current=Coalesce(
Sum(
Case(
When(type=Transaction.Type.EXPENSE, is_paid=True, then="amount"),
default=Value(0),
output_field=models.DecimalField(),
)
currency_totals = (
transactions_queryset.values(
"account__currency",
"account__currency__code",
"account__currency__name",
"account__currency__decimal_places",
"account__currency__prefix",
"account__currency__suffix",
"account__currency__exchange_currency",
)
.annotate(
expense_current=Coalesce(
Sum(
Case(
When(
type=Transaction.Type.EXPENSE, is_paid=True, then="amount"
),
default=Value(0),
output_field=models.DecimalField(),
)
),
Decimal("0"),
),
Decimal("0"),
),
expense_projected=Coalesce(
Sum(
Case(
When(type=Transaction.Type.EXPENSE, is_paid=False, then="amount"),
default=Value(0),
output_field=models.DecimalField(),
)
expense_projected=Coalesce(
Sum(
Case(
When(
type=Transaction.Type.EXPENSE, is_paid=False, then="amount"
),
default=Value(0),
output_field=models.DecimalField(),
)
),
Decimal("0"),
),
Decimal("0"),
),
income_current=Coalesce(
Sum(
Case(
When(type=Transaction.Type.INCOME, is_paid=True, then="amount"),
default=Value(0),
output_field=models.DecimalField(),
)
income_current=Coalesce(
Sum(
Case(
When(type=Transaction.Type.INCOME, is_paid=True, then="amount"),
default=Value(0),
output_field=models.DecimalField(),
)
),
Decimal("0"),
),
Decimal("0"),
),
income_projected=Coalesce(
Sum(
Case(
When(type=Transaction.Type.INCOME, is_paid=False, then="amount"),
default=Value(0),
output_field=models.DecimalField(),
)
income_projected=Coalesce(
Sum(
Case(
When(
type=Transaction.Type.INCOME, is_paid=False, then="amount"
),
default=Value(0),
output_field=models.DecimalField(),
)
),
Decimal("0"),
),
Decimal("0"),
),
)
.order_by()
)
# Process the results and calculate additional totals

View File

@@ -75,7 +75,7 @@ def yearly_overview_by_currency(request, year: int):
transactions = (
Transaction.objects.filter(**filter_params)
.exclude(Q(category__mute=True) & ~Q(category=None))
.order_by("account__group__name", "account__name")
.order_by("account__currency__name")
)
data = calculate_currency_totals(transactions)