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()
+15 -5
View File
@@ -11,7 +11,8 @@ 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 = (
transactions_queryset.values(
"account__currency", "account__currency",
"account__currency__code", "account__currency__code",
"account__currency__name", "account__currency__name",
@@ -19,11 +20,14 @@ def calculate_currency_totals(transactions_queryset, ignore_empty=False):
"account__currency__prefix", "account__currency__prefix",
"account__currency__suffix", "account__currency__suffix",
"account__currency__exchange_currency", "account__currency__exchange_currency",
).annotate( )
.annotate(
expense_current=Coalesce( expense_current=Coalesce(
Sum( Sum(
Case( Case(
When(type=Transaction.Type.EXPENSE, is_paid=True, then="amount"), When(
type=Transaction.Type.EXPENSE, is_paid=True, then="amount"
),
default=Value(0), default=Value(0),
output_field=models.DecimalField(), output_field=models.DecimalField(),
) )
@@ -33,7 +37,9 @@ def calculate_currency_totals(transactions_queryset, ignore_empty=False):
expense_projected=Coalesce( expense_projected=Coalesce(
Sum( Sum(
Case( Case(
When(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(),
) )
@@ -53,7 +59,9 @@ def calculate_currency_totals(transactions_queryset, ignore_empty=False):
income_projected=Coalesce( income_projected=Coalesce(
Sum( Sum(
Case( Case(
When(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(),
) )
@@ -61,6 +69,8 @@ def calculate_currency_totals(transactions_queryset, ignore_empty=False):
Decimal("0"), Decimal("0"),
), ),
) )
.order_by()
)
# Process the results and calculate additional totals # Process the results and calculate additional totals
result = {} result = {}
+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)