diff --git a/http/budgeting.go b/http/budgeting.go index b8999d2..b4579c8 100644 --- a/http/budgeting.go +++ b/http/budgeting.go @@ -14,7 +14,7 @@ import ( type BudgetingData struct { AlwaysNeededData Categories []CategoryWithBalance - AvailableBalance postgres.Numeric + AvailableBalance float64 Date time.Time Next time.Time Previous time.Time @@ -75,6 +75,12 @@ func (h *Handler) budgeting(c *gin.Context) { } firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) + d := BudgetingData{ + AlwaysNeededData: c.MustGet("data").(AlwaysNeededData), + Date: firstOfMonth, + Next: firstOfNextMonth, + Previous: firstOfPreviousMonth, + } categories, err := h.Service.DB.GetCategories(c.Request.Context(), budgetUUID) @@ -84,6 +90,40 @@ func (h *Handler) budgeting(c *gin.Context) { return } + // skip everything in the future + categoriesWithBalance, added, assigned, err := h.calculateBalances(c, budgetUUID, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) + if err != nil { + return + } + d.Categories = categoriesWithBalance + + data := c.MustGet("data").(AlwaysNeededData) + var availableBalance float64 = 0 + for _, cat := range categories { + if cat.ID != data.Budget.IncomeCategoryID { + continue + } + availableBalance = -assigned - added + + for _, bal := range cumultativeBalances { + if bal.CategoryID != cat.ID { + continue + } + + if !bal.Date.Before(firstOfNextMonth) { + continue + } + + availableBalance += bal.Transactions.GetFloat64() + } + } + + d.AvailableBalance = availableBalance + + 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) { categoriesWithBalance := []CategoryWithBalance{} var added float64 = 0 @@ -98,7 +138,6 @@ func (h *Handler) budgeting(c *gin.Context) { continue } - // skip everything in the future if !bal.Date.Before(firstOfNextMonth) { continue } @@ -122,39 +161,5 @@ func (h *Handler) budgeting(c *gin.Context) { categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance) } - - data := c.MustGet("data").(AlwaysNeededData) - var availableBalance float64 = 0 - for _, cat := range categories { - if cat.ID != data.Budget.IncomeCategoryID { - continue - } - availableBalance = -assigned - added - - for _, bal := range cumultativeBalances { - if bal.CategoryID != cat.ID { - continue - } - - if !bal.Date.Before(firstOfNextMonth) { - continue - } - - availableBalance += bal.Transactions.GetFloat64() - } - } - - var availableBalanceNum postgres.Numeric - availableBalanceNum.Set(availableBalance) - - d := BudgetingData{ - AlwaysNeededData: c.MustGet("data").(AlwaysNeededData), - Categories: categoriesWithBalance, - AvailableBalance: availableBalanceNum, - Date: firstOfMonth, - Next: firstOfNextMonth, - Previous: firstOfPreviousMonth, - } - - c.HTML(http.StatusOK, "budgeting.html", d) + return categoriesWithBalance, added, assigned, nil } diff --git a/web/budgeting.html b/web/budgeting.html index 60290a1..327ff5e 100644 --- a/web/budgeting.html +++ b/web/budgeting.html @@ -20,7 +20,7 @@ Next Month