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
+9 -3
View File
@@ -15,17 +15,23 @@ from apps.transactions.utils.calculations import (
def net_worth_main(request): 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 is_paid=True, account__is_archived=False
).order_by( ).order_by(
"account__group__name", "account__group__name",
"account__name", "account__name",
) )
currency_net_worth = calculate_currency_totals( currency_net_worth = calculate_currency_totals(
transactions_queryset=transactions_queryset transactions_queryset=transactions_currency_queryset
) )
account_net_worth = calculate_account_totals( account_net_worth = calculate_account_totals(
transactions_queryset=transactions_queryset transactions_queryset=transactions_account_queryset
) )
historical_currency_net_worth = calculate_historical_currency_net_worth() historical_currency_net_worth = calculate_historical_currency_net_worth()
+55 -45
View File
@@ -11,55 +11,65 @@ from apps.currencies.models import Currency
def calculate_currency_totals(transactions_queryset, ignore_empty=False): def calculate_currency_totals(transactions_queryset, ignore_empty=False):
# Prepare the aggregation expressions # Prepare the aggregation expressions
currency_totals = transactions_queryset.values( currency_totals = (
"account__currency", transactions_queryset.values(
"account__currency__code", "account__currency",
"account__currency__name", "account__currency__code",
"account__currency__decimal_places", "account__currency__name",
"account__currency__prefix", "account__currency__decimal_places",
"account__currency__suffix", "account__currency__prefix",
"account__currency__exchange_currency", "account__currency__suffix",
).annotate( "account__currency__exchange_currency",
expense_current=Coalesce( )
Sum( .annotate(
Case( expense_current=Coalesce(
When(type=Transaction.Type.EXPENSE, is_paid=True, then="amount"), Sum(
default=Value(0), Case(
output_field=models.DecimalField(), 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(
expense_projected=Coalesce( Case(
Sum( When(
Case( type=Transaction.Type.EXPENSE, is_paid=False, then="amount"
When(type=Transaction.Type.EXPENSE, is_paid=False, then="amount"), ),
default=Value(0), default=Value(0),
output_field=models.DecimalField(), output_field=models.DecimalField(),
) )
),
Decimal("0"),
), ),
Decimal("0"), income_current=Coalesce(
), Sum(
income_current=Coalesce( Case(
Sum( When(type=Transaction.Type.INCOME, is_paid=True, then="amount"),
Case( default=Value(0),
When(type=Transaction.Type.INCOME, is_paid=True, then="amount"), output_field=models.DecimalField(),
default=Value(0), )
output_field=models.DecimalField(), ),
) Decimal("0"),
), ),
Decimal("0"), income_projected=Coalesce(
), Sum(
income_projected=Coalesce( Case(
Sum( When(
Case( type=Transaction.Type.INCOME, is_paid=False, then="amount"
When(type=Transaction.Type.INCOME, is_paid=False, then="amount"), ),
default=Value(0), default=Value(0),
output_field=models.DecimalField(), output_field=models.DecimalField(),
) )
),
Decimal("0"),
), ),
Decimal("0"), )
), .order_by()
) )
# Process the results and calculate additional totals # Process the results and calculate additional totals
+1 -1
View File
@@ -75,7 +75,7 @@ def yearly_overview_by_currency(request, year: int):
transactions = ( transactions = (
Transaction.objects.filter(**filter_params) Transaction.objects.filter(**filter_params)
.exclude(Q(category__mute=True) & ~Q(category=None)) .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) data = calculate_currency_totals(transactions)