New calcutlations for account
This commit is contained in:
parent
3154cfac22
commit
840bf75bb9
@ -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
|
||||||
|
|
||||||
|
|||||||
@ -300,13 +300,10 @@ def dashboard(request):
|
|||||||
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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user