From 1e495a5e47902422b9570ad842eda7bf867d7acb Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 4 Mar 2022 21:13:50 +0000 Subject: [PATCH] Remove unneeded loop --- server/budgeting.go | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/server/budgeting.go b/server/budgeting.go index 8a77950..01f15d9 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -97,8 +97,7 @@ func (h *Handler) budgetingForMonth(c *gin.Context) { categoriesWithBalance, moneyUsed := h.calculateBalances( budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) - - availableBalance := h.getAvailableBalance(categories, budget, moneyUsed, cumultativeBalances, firstOfNextMonth) + availableBalance := h.getAvailableBalance(budget, moneyUsed, cumultativeBalances, firstOfNextMonth) data := struct { Categories []CategoryWithBalance @@ -107,27 +106,23 @@ func (h *Handler) budgetingForMonth(c *gin.Context) { c.JSON(http.StatusOK, data) } -func (*Handler) getAvailableBalance(categories []postgres.GetCategoriesRow, budget postgres.Budget, +func (*Handler) getAvailableBalance(budget postgres.Budget, moneyUsed numeric.Numeric, cumultativeBalances []postgres.GetCumultativeBalancesRow, firstOfNextMonth time.Time) numeric.Numeric { - availableBalance := numeric.Zero() - for _, cat := range categories { - if cat.ID != budget.IncomeCategoryID { + availableBalance := moneyUsed + fmt.Printf("%s: %f\n", "INC", moneyUsed.GetFloat64()) + + for _, bal := range cumultativeBalances { + if bal.CategoryID != budget.IncomeCategoryID { continue } - availableBalance = moneyUsed - for _, bal := range cumultativeBalances { - if bal.CategoryID != cat.ID { - continue - } - - if !bal.Date.Before(firstOfNextMonth) { - continue - } - - availableBalance = availableBalance.Add(bal.Transactions) + if !bal.Date.Before(firstOfNextMonth) { + continue } + + availableBalance.AddI(bal.Transactions) + availableBalance.AddI(bal.Assignments) } return availableBalance }