New calcutlations for account
This commit is contained in:
parent
3154cfac22
commit
840bf75bb9
@ -86,8 +86,8 @@ class Account(models.Model):
|
||||
data = []
|
||||
|
||||
for month in range(1, 13):
|
||||
balance += income_map.get(month, 0)
|
||||
balance -= expenses_map.get(month, 0)
|
||||
balance += income_map.get(month, Decimal('0'))
|
||||
balance -= expenses_map.get(month, Decimal('0'))
|
||||
|
||||
data.append({
|
||||
'month': month,
|
||||
@ -95,6 +95,44 @@ class Account(models.Model):
|
||||
})
|
||||
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):
|
||||
return self.name
|
||||
|
||||
|
||||
@ -297,16 +297,13 @@ def dashboard(request):
|
||||
|
||||
selected_account_obj = None
|
||||
kpi_balance = None
|
||||
if account_id:
|
||||
expenses = expenses.filter(account_id=account_id)
|
||||
if account_id:
|
||||
expenses = expenses.filter(account_id=account_id)
|
||||
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:
|
||||
kpi_balance = (
|
||||
Account.objects.filter(owner=request.user, active=True)
|
||||
.aggregate(total=Sum('initial_balance'))['total'] or 0
|
||||
) - (
|
||||
expenses.aggregate(total=Sum('amount'))['total'] or 0
|
||||
kpi_balance = sum(
|
||||
account.current_balance() for account in accounts
|
||||
)
|
||||
|
||||
today = date.today()
|
||||
@ -336,10 +333,10 @@ def dashboard(request):
|
||||
total=Sum('amount')
|
||||
)['total'] or 0
|
||||
|
||||
expense_count = expenses.count()
|
||||
expense_count = expenses_filtered.count()
|
||||
|
||||
category_count = (
|
||||
expenses
|
||||
expenses_filtered
|
||||
.values('category')
|
||||
.distinct()
|
||||
.count()
|
||||
@ -412,6 +409,8 @@ def dashboard(request):
|
||||
|
||||
if compare_enabled:
|
||||
previous_expenses = Expense.objects.filter(owner=request.user)
|
||||
if account_id:
|
||||
previous_expenses.filter(account_id=account_id)
|
||||
|
||||
if selected_month:
|
||||
# Monthly compare
|
||||
@ -502,7 +501,7 @@ def dashboard(request):
|
||||
'name': account.name,
|
||||
'data': m_balance,
|
||||
'current_balance': account.current_balance(),
|
||||
})
|
||||
})
|
||||
|
||||
# Send the data to the dashboard
|
||||
return render(request, 'expenses/dashboard.html', {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user