more changes

This commit is contained in:
Herculino Trotta
2024-10-13 12:10:50 -03:00
parent 1717d8a94e
commit d20897a28a
33 changed files with 1552 additions and 153 deletions

View File

@@ -2,10 +2,77 @@ from django.db.models import Sum
from decimal import Decimal
from django.db.models.functions import TruncMonth
from django.utils.translation import gettext_lazy as _
from apps.transactions.models import Transaction
from apps.accounts.models import Account
from apps.currencies.models import Currency
from apps.currencies.utils.convert import convert
def calculate_account_net_worth():
account_net_worth = {}
ungrouped_id = None # Special ID for ungrouped accounts
# Initialize the "Ungrouped" category
account_net_worth[ungrouped_id] = {"name": _("Ungrouped"), "accounts": {}}
# Get all accounts
accounts = Account.objects.all()
for account in accounts:
currency = account.currency
income = Transaction.objects.filter(
account=account, type=Transaction.Type.INCOME, is_paid=True
).aggregate(total=Sum("amount"))["total"] or Decimal("0")
expenses = Transaction.objects.filter(
account=account, type=Transaction.Type.EXPENSE, is_paid=True
).aggregate(total=Sum("amount"))["total"] or Decimal("0")
account_balance = income - expenses
account_data = {
"name": account.name,
"balance": account_balance,
"currency": {
"code": currency.code,
"name": currency.name,
"prefix": currency.prefix,
"suffix": currency.suffix,
"decimal_places": currency.decimal_places,
},
}
if account.exchange_currency:
converted_amount, prefix, suffix, decimal_places = convert(
amount=account_balance,
from_currency=account.currency,
to_currency=account.exchange_currency,
)
if converted_amount:
account_data["exchange"] = {
"amount": converted_amount,
"prefix": prefix,
"suffix": suffix,
"decimal_places": decimal_places,
}
if account.group:
group_id = account.group.id
group_name = account.group.name
if group_id not in account_net_worth:
account_net_worth[group_id] = {"name": group_name, "accounts": {}}
account_net_worth[group_id]["accounts"][account.id] = account_data
else:
account_net_worth[ungrouped_id]["accounts"][account.id] = account_data
# Remove the "Ungrouped" category if it's empty
if not account_net_worth[ungrouped_id]["accounts"]:
del account_net_worth[ungrouped_id]
return account_net_worth
def calculate_net_worth():
@@ -41,7 +108,7 @@ def calculate_historical_net_worth(start_date, end_date):
# Get all months between start_date and end_date
months = (
Transaction.objects.filter(account__in=asset_accounts)
.annotate(month=TruncMonth("date"))
.annotate(month=TruncMonth("reference_date"))
.values("month")
.distinct()
.order_by("month")
@@ -61,14 +128,14 @@ def calculate_historical_net_worth(start_date, end_date):
account=account,
type=Transaction.Type.INCOME,
is_paid=True,
date__lte=month,
reference_date__lte=month,
).aggregate(total=Sum("amount"))["total"] or Decimal("0.00")
expenses = Transaction.objects.filter(
account=account,
type=Transaction.Type.EXPENSE,
is_paid=True,
date__lte=month,
reference_date__lte=month,
).aggregate(total=Sum("amount"))["total"] or Decimal("0.00")
account_balance = income - expenses

View File

@@ -1,11 +1,14 @@
from datetime import datetime
from dateutil.relativedelta import relativedelta
from django.http import JsonResponse
from django.shortcuts import render
from django.utils import timezone
from apps.net_worth.utils.calculate_net_worth import (
calculate_net_worth,
calculate_historical_net_worth,
calculate_account_net_worth,
)
from apps.currencies.models import Currency
@@ -13,12 +16,14 @@ from apps.currencies.models import Currency
# Create your views here.
def net_worth_main(request):
net_worth = calculate_net_worth()
detailed_net_worth = calculate_account_net_worth()
# historical = calculate_historical_net_worth(
# start_date=datetime(day=1, month=1, year=2021).date(),
# end_date=datetime(day=1, month=1, year=2025).date(),
# )
# print(historical)
print(detailed_net_worth)
# Format the net worth with currency details
formatted_net_worth = []
for currency_code, amount in net_worth.items():
@@ -34,6 +39,25 @@ def net_worth_main(request):
}
)
end_date = timezone.now()
start_date = end_date - relativedelta(years=5) # Last year
# Calculate historical net worth
historical_data = calculate_historical_net_worth(start_date, end_date)
# Prepare data for the template
currencies = Currency.objects.all()
print(historical_data)
return render(
request, "net_worth/net_worth.html", {"currency_net_worth": formatted_net_worth}
request,
"net_worth/net_worth.html",
{
"currency_net_worth": formatted_net_worth,
"account_net_worth": detailed_net_worth,
"currencies": currencies,
"historical_data_json": JsonResponse(historical_data).content.decode(
"utf-8"
),
},
)