Fixed function monthly_balance
This commit is contained in:
parent
dc6f5197ae
commit
bb9f13a031
@ -68,10 +68,8 @@ class Account(models.Model):
|
||||
|
||||
def monthly_balance(self, year=None):
|
||||
year = year or date.today().year
|
||||
today = date.today()
|
||||
|
||||
# -------------------------
|
||||
# Previous accumulated balance
|
||||
# -------------------------
|
||||
previous_income = (
|
||||
self.incomes.filter(date__year__lt=year)
|
||||
.aggregate(total=Sum('amount'))['total']
|
||||
@ -84,11 +82,7 @@ class Account(models.Model):
|
||||
or Decimal('0')
|
||||
)
|
||||
|
||||
balance = (self.initial_balance + previous_income - previous_income)
|
||||
|
||||
# -------------------------
|
||||
# Current year data
|
||||
# -------------------------
|
||||
balance = self.initial_balance + previous_income - previous_expenses
|
||||
|
||||
incomes = (
|
||||
self.incomes.filter(date__year=year)
|
||||
@ -104,8 +98,8 @@ class Account(models.Model):
|
||||
.annotate(total=Sum("amount"))
|
||||
)
|
||||
|
||||
income_map = {i["month"]: i["total"] for i in incomes}
|
||||
expenses_map = {e["month"]: e["total"] for e in expenses}
|
||||
income_map = {i["month"]: Decimal(str(i["total"] or 0)) for i in incomes}
|
||||
expenses_map = {e["month"]: Decimal(str(e["total"] or 0)) for e in expenses}
|
||||
|
||||
data = []
|
||||
|
||||
@ -116,11 +110,18 @@ class Account(models.Model):
|
||||
data.append(
|
||||
{
|
||||
"month": month,
|
||||
"balance": float(balance),
|
||||
"balance": float(balance)
|
||||
}
|
||||
)
|
||||
|
||||
if year == today.year:
|
||||
current_month_idx = today.month - 1
|
||||
if 0 <= current_month_idx < len(data):
|
||||
data[current_month_idx]["balance"] = float(self.current_balance())
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def balance_until(self, date):
|
||||
incomes_total = self.incomes.filter(date__lte=date).aggregate(
|
||||
total=Sum("amount")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user