diff --git a/app/apps/net_worth/views.py b/app/apps/net_worth/views.py index 9d53758..308a022 100644 --- a/app/apps/net_worth/views.py +++ b/app/apps/net_worth/views.py @@ -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() diff --git a/app/apps/transactions/utils/calculations.py b/app/apps/transactions/utils/calculations.py index 6710db3..5241388 100644 --- a/app/apps/transactions/utils/calculations.py +++ b/app/apps/transactions/utils/calculations.py @@ -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 diff --git a/app/apps/yearly_overview/views.py b/app/apps/yearly_overview/views.py index 771c6d9..2a60052 100644 --- a/app/apps/yearly_overview/views.py +++ b/app/apps/yearly_overview/views.py @@ -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)