This commit is contained in:
Herculino Trotta
2024-10-09 00:31:21 -03:00
parent e78e4cc5e1
commit 3dde44b1cd
139 changed files with 4965 additions and 1004 deletions
View File
+3
View File
@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class NetWorthConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.net_worth"
+3
View File
@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+7
View File
@@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path("net-worth/", views.net_worth_main, name="net_worth"),
]
@@ -0,0 +1,77 @@
from django.db.models import Sum
from decimal import Decimal
from django.db.models.functions import TruncMonth
from apps.transactions.models import Transaction
from apps.accounts.models import Account
from apps.currencies.models import Currency
def calculate_net_worth():
accounts = Account.objects.all()
net_worth = {}
for account in accounts:
currency = account.currency
if currency.code not in net_worth:
net_worth[currency.code] = Decimal("0")
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
net_worth[currency.code] += account_balance
return net_worth
def calculate_historical_net_worth(start_date, end_date):
asset_accounts = Account.objects.all()
currencies = Currency.objects.all()
# Initialize the result dictionary
historical_net_worth = {}
# Get all months between start_date and end_date
months = (
Transaction.objects.filter(account__in=asset_accounts)
.annotate(month=TruncMonth("date"))
.values("month")
.distinct()
.order_by("month")
)
for month_data in months:
month = month_data["month"]
month_str = month.strftime("%Y-%m")
historical_net_worth[month_str] = {
currency.code: Decimal("0.00") for currency in currencies
}
for account in asset_accounts:
currency = account.currency
income = Transaction.objects.filter(
account=account,
type=Transaction.Type.INCOME,
is_paid=True,
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,
).aggregate(total=Sum("amount"))["total"] or Decimal("0.00")
account_balance = income - expenses
historical_net_worth[month_str][currency.code] += account_balance
return historical_net_worth
+39
View File
@@ -0,0 +1,39 @@
from datetime import datetime
from django.http import JsonResponse
from django.shortcuts import render
from apps.net_worth.utils.calculate_net_worth import (
calculate_net_worth,
calculate_historical_net_worth,
)
from apps.currencies.models import Currency
# Create your views here.
def net_worth_main(request):
net_worth = calculate_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)
# Format the net worth with currency details
formatted_net_worth = []
for currency_code, amount in net_worth.items():
currency = Currency.objects.get(code=currency_code)
formatted_net_worth.append(
{
"amount": amount,
"code": currency.code,
"name": currency.name,
"prefix": currency.prefix,
"suffix": currency.suffix,
"decimal_places": currency.decimal_places,
}
)
return render(
request, "net_worth/net_worth.html", {"currency_net_worth": formatted_net_worth}
)