diff --git a/http/budgeting.go b/http/budgeting.go index b4579c8..1a2cee6 100644 --- a/http/budgeting.go +++ b/http/budgeting.go @@ -91,7 +91,7 @@ func (h *Handler) budgeting(c *gin.Context) { } // skip everything in the future - categoriesWithBalance, added, assigned, err := h.calculateBalances(c, budgetUUID, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) + categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, budgetUUID, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) if err != nil { return } @@ -103,7 +103,7 @@ func (h *Handler) budgeting(c *gin.Context) { if cat.ID != data.Budget.IncomeCategoryID { continue } - availableBalance = -assigned - added + availableBalance = moneyUsed for _, bal := range cumultativeBalances { if bal.CategoryID != cat.ID { @@ -123,11 +123,10 @@ func (h *Handler) budgeting(c *gin.Context) { c.HTML(http.StatusOK, "budgeting.html", d) } -func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, float64, float64, error) { +func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, float64, error) { categoriesWithBalance := []CategoryWithBalance{} - var added float64 = 0 - var assigned float64 = 0 + var moneyUsed float64 = 0 for i := range categories { cat := &categories[i] categoryWithBalance := CategoryWithBalance{ @@ -142,11 +141,11 @@ func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstO continue } - assigned += bal.Assignments.GetFloat64() + moneyUsed -= bal.Assignments.GetFloat64() categoryWithBalance.Available += bal.Assignments.GetFloat64() categoryWithBalance.Available += bal.Transactions.GetFloat64() if categoryWithBalance.Available < 0 && bal.Date.Before(firstOfMonth) { - added -= categoryWithBalance.Available + moneyUsed += categoryWithBalance.Available categoryWithBalance.Available = 0 } @@ -161,5 +160,5 @@ func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstO categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance) } - return categoriesWithBalance, added, assigned, nil + return categoriesWithBalance, moneyUsed, nil }