diff --git a/http/budgeting.go b/http/budgeting.go index 1a2cee6..e6bda3d 100644 --- a/http/budgeting.go +++ b/http/budgeting.go @@ -8,7 +8,6 @@ import ( "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" - "github.com/google/uuid" ) type BudgetingData struct { @@ -61,22 +60,19 @@ func getDate(c *gin.Context) (time.Time, error) { } func (h *Handler) budgeting(c *gin.Context) { - budgetID := c.Param("budgetid") - budgetUUID, err := uuid.Parse(budgetID) - if err != nil { - c.Redirect(http.StatusTemporaryRedirect, "/login") - return - } + alwaysNeededData := c.MustGet("data").(AlwaysNeededData) + budgetUUID := alwaysNeededData.Budget.ID firstOfMonth, err := getDate(c) if err != nil { c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String()) return } + firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) d := BudgetingData{ - AlwaysNeededData: c.MustGet("data").(AlwaysNeededData), + AlwaysNeededData: alwaysNeededData, Date: firstOfMonth, Next: firstOfNextMonth, Previous: firstOfPreviousMonth, @@ -91,7 +87,7 @@ func (h *Handler) budgeting(c *gin.Context) { } // skip everything in the future - categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, budgetUUID, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) + categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, alwaysNeededData.Budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) if err != nil { return } @@ -123,8 +119,14 @@ 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, error) { +func (h *Handler) calculateBalances(c *gin.Context, budget postgres.Budget, firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, float64, error) { categoriesWithBalance := []CategoryWithBalance{} + hiddenCategory := CategoryWithBalance{ + GetCategoriesRow: &postgres.GetCategoriesRow{ + Name: "", + Group: "Hidden Categories", + }, + } var moneyUsed float64 = 0 for i := range categories { @@ -158,7 +160,23 @@ func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstO } + // do not show hidden categories + if cat.Group == "Hidden Categories" { + hiddenCategory.Available += categoryWithBalance.Available + hiddenCategory.AvailableLastMonth += categoryWithBalance.AvailableLastMonth + hiddenCategory.Activity += categoryWithBalance.Activity + hiddenCategory.Assigned += categoryWithBalance.Assigned + continue + } + + if cat.ID == budget.IncomeCategoryID { + continue + } + categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance) } + + categoriesWithBalance = append(categoriesWithBalance, hiddenCategory) + return categoriesWithBalance, moneyUsed, nil }