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):
|
def monthly_balance(self, year=None):
|
||||||
year = year or date.today().year
|
year = year or date.today().year
|
||||||
|
today = date.today()
|
||||||
# -------------------------
|
|
||||||
# Previous accumulated balance
|
|
||||||
# -------------------------
|
|
||||||
previous_income = (
|
previous_income = (
|
||||||
self.incomes.filter(date__year__lt=year)
|
self.incomes.filter(date__year__lt=year)
|
||||||
.aggregate(total=Sum('amount'))['total']
|
.aggregate(total=Sum('amount'))['total']
|
||||||
@ -84,11 +82,7 @@ class Account(models.Model):
|
|||||||
or Decimal('0')
|
or Decimal('0')
|
||||||
)
|
)
|
||||||
|
|
||||||
balance = (self.initial_balance + previous_income - previous_income)
|
balance = self.initial_balance + previous_income - previous_expenses
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# Current year data
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
incomes = (
|
incomes = (
|
||||||
self.incomes.filter(date__year=year)
|
self.incomes.filter(date__year=year)
|
||||||
@ -96,30 +90,37 @@ class Account(models.Model):
|
|||||||
.values("month")
|
.values("month")
|
||||||
.annotate(total=Sum("amount"))
|
.annotate(total=Sum("amount"))
|
||||||
)
|
)
|
||||||
|
|
||||||
expenses = (
|
expenses = (
|
||||||
self.expenses.filter(date__year=year)
|
self.expenses.filter(date__year=year)
|
||||||
.annotate(month=ExtractMonth("date"))
|
.annotate(month=ExtractMonth("date"))
|
||||||
.values("month")
|
.values("month")
|
||||||
.annotate(total=Sum("amount"))
|
.annotate(total=Sum("amount"))
|
||||||
)
|
)
|
||||||
|
|
||||||
income_map = {i["month"]: i["total"] for i in incomes}
|
income_map = {i["month"]: Decimal(str(i["total"] or 0)) for i in incomes}
|
||||||
expenses_map = {e["month"]: e["total"] for e in expenses}
|
expenses_map = {e["month"]: Decimal(str(e["total"] or 0)) for e in expenses}
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
|
|
||||||
for month in range(1, 13):
|
for month in range(1, 13):
|
||||||
balance += income_map.get(month, Decimal("0"))
|
balance += income_map.get(month, Decimal("0"))
|
||||||
balance -= expenses_map.get(month, Decimal("0"))
|
balance -= expenses_map.get(month, Decimal("0"))
|
||||||
|
|
||||||
data.append(
|
data.append(
|
||||||
{
|
{
|
||||||
"month": month,
|
"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
|
return data
|
||||||
|
|
||||||
|
|
||||||
def balance_until(self, date):
|
def balance_until(self, date):
|
||||||
incomes_total = self.incomes.filter(date__lte=date).aggregate(
|
incomes_total = self.incomes.filter(date__lte=date).aggregate(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user