New calcutlations for account

This commit is contained in:
JKuijperM 2026-02-10 15:20:32 +01:00
parent 3154cfac22
commit 840bf75bb9
2 changed files with 50 additions and 13 deletions

View File

@ -86,8 +86,8 @@ class Account(models.Model):
data = [] data = []
for month in range(1, 13): for month in range(1, 13):
balance += income_map.get(month, 0) balance += income_map.get(month, Decimal('0'))
balance -= expenses_map.get(month, 0) balance -= expenses_map.get(month, Decimal('0'))
data.append({ data.append({
'month': month, 'month': month,
@ -95,6 +95,44 @@ class Account(models.Model):
}) })
return data return data
def balance_until(self, date):
incomes_total = (
self.incomes
.filter(date__lte=date)
.aggregate(total=Sum('amount'))['total'] or Decimal('0')
)
expenses_total = (
self.expenses
.filter(date__lte=date)
.aggregate(total=Sum('amount'))['total'] or Decimal('0')
)
return self.initial_balance + incomes_total - expenses_total
def monthly_net(self, year=None):
year = year or date.today().year
data = []
for month in range(1, 13):
income = (
self.incomes
.filter(date__year=year, date__month=month)
.aggregate(total=Sum('amount'))['total'] or Decimal('0')
)
expense = (
self.expenses
.filter(date__year=year, date__month=month)
.aggregate(total=Sum('amount'))['total'] or Decimal('0')
)
data.append({
'month': month,
'net': float(income - expense)
})
return data
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -297,16 +297,13 @@ def dashboard(request):
selected_account_obj = None selected_account_obj = None
kpi_balance = None kpi_balance = None
if account_id: if account_id:
expenses = expenses.filter(account_id=account_id) expenses = expenses.filter(account_id=account_id)
selected_account_obj = accounts.filter(id=account_id).first() selected_account_obj = accounts.filter(id=account_id).first()
kpi_balance = selected_account_obj.current_balance() kpi_balance = selected_account_obj.current_balance() if selected_account_obj else 0
else: else:
kpi_balance = ( kpi_balance = sum(
Account.objects.filter(owner=request.user, active=True) account.current_balance() for account in accounts
.aggregate(total=Sum('initial_balance'))['total'] or 0
) - (
expenses.aggregate(total=Sum('amount'))['total'] or 0
) )
today = date.today() today = date.today()
@ -336,10 +333,10 @@ def dashboard(request):
total=Sum('amount') total=Sum('amount')
)['total'] or 0 )['total'] or 0
expense_count = expenses.count() expense_count = expenses_filtered.count()
category_count = ( category_count = (
expenses expenses_filtered
.values('category') .values('category')
.distinct() .distinct()
.count() .count()
@ -412,6 +409,8 @@ def dashboard(request):
if compare_enabled: if compare_enabled:
previous_expenses = Expense.objects.filter(owner=request.user) previous_expenses = Expense.objects.filter(owner=request.user)
if account_id:
previous_expenses.filter(account_id=account_id)
if selected_month: if selected_month:
# Monthly compare # Monthly compare
@ -502,7 +501,7 @@ def dashboard(request):
'name': account.name, 'name': account.name,
'data': m_balance, 'data': m_balance,
'current_balance': account.current_balance(), 'current_balance': account.current_balance(),
}) })
# Send the data to the dashboard # Send the data to the dashboard
return render(request, 'expenses/dashboard.html', { return render(request, 'expenses/dashboard.html', {