From 8ecfd6b87a71251412b080050a23bfa34a9d5e12 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 4 Mar 2022 20:55:38 +0000 Subject: [PATCH] Use in-place calculations --- server/budgeting.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/budgeting.go b/server/budgeting.go index a421dbc..3e6b249 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -171,12 +171,13 @@ func (h *Handler) calculateBalances(budget postgres.Budget, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, numeric.Numeric) { categoriesWithBalance := []CategoryWithBalance{} - moneyUsed := numeric.Zero() + moneyUsed2 := numeric.Zero() + moneyUsed := &moneyUsed2 for i := range categories { cat := &categories[i] // do not show hidden categories categoryWithBalance := h.CalculateCategoryBalances(cat, cumultativeBalances, - firstOfNextMonth, &moneyUsed, firstOfMonth, budget) + firstOfNextMonth, moneyUsed, firstOfMonth, budget) if cat.ID == budget.IncomeCategoryID { continue @@ -185,7 +186,7 @@ func (h *Handler) calculateBalances(budget postgres.Budget, categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance) } - return categoriesWithBalance, moneyUsed + return categoriesWithBalance, *moneyUsed } func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow, @@ -202,11 +203,11 @@ func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow, continue } - *moneyUsed = moneyUsed.Sub(bal.Assignments) + moneyUsed.SubI(bal.Assignments) categoryWithBalance.Available = categoryWithBalance.Available.Add(bal.Assignments) categoryWithBalance.Available = categoryWithBalance.Available.Add(bal.Transactions) if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) { - *moneyUsed = moneyUsed.Add(categoryWithBalance.Available) + moneyUsed.Add(categoryWithBalance.Available) categoryWithBalance.Available = numeric.Zero() }