Use in-place calculations

This commit is contained in:
Jan Bader 2022-03-04 20:55:38 +00:00
parent d89e702669
commit 8ecfd6b87a

View File

@ -171,12 +171,13 @@ func (h *Handler) calculateBalances(budget postgres.Budget,
cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, numeric.Numeric) { cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, numeric.Numeric) {
categoriesWithBalance := []CategoryWithBalance{} categoriesWithBalance := []CategoryWithBalance{}
moneyUsed := numeric.Zero() moneyUsed2 := numeric.Zero()
moneyUsed := &moneyUsed2
for i := range categories { for i := range categories {
cat := &categories[i] cat := &categories[i]
// do not show hidden categories // do not show hidden categories
categoryWithBalance := h.CalculateCategoryBalances(cat, cumultativeBalances, categoryWithBalance := h.CalculateCategoryBalances(cat, cumultativeBalances,
firstOfNextMonth, &moneyUsed, firstOfMonth, budget) firstOfNextMonth, moneyUsed, firstOfMonth, budget)
if cat.ID == budget.IncomeCategoryID { if cat.ID == budget.IncomeCategoryID {
continue continue
@ -185,7 +186,7 @@ func (h *Handler) calculateBalances(budget postgres.Budget,
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance) categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
} }
return categoriesWithBalance, moneyUsed return categoriesWithBalance, *moneyUsed
} }
func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow, func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow,
@ -202,11 +203,11 @@ func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow,
continue continue
} }
*moneyUsed = moneyUsed.Sub(bal.Assignments) moneyUsed.SubI(bal.Assignments)
categoryWithBalance.Available = categoryWithBalance.Available.Add(bal.Assignments) categoryWithBalance.Available = categoryWithBalance.Available.Add(bal.Assignments)
categoryWithBalance.Available = categoryWithBalance.Available.Add(bal.Transactions) categoryWithBalance.Available = categoryWithBalance.Available.Add(bal.Transactions)
if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) { if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) {
*moneyUsed = moneyUsed.Add(categoryWithBalance.Available) moneyUsed.Add(categoryWithBalance.Available)
categoryWithBalance.Available = numeric.Zero() categoryWithBalance.Available = numeric.Zero()
} }