Group hidden categories

This commit is contained in:
Jan Bader 2021-12-11 15:10:36 +00:00
parent 466775817f
commit d5ebf5a5cf

View File

@ -8,7 +8,6 @@ import (
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
) )
type BudgetingData struct { type BudgetingData struct {
@ -61,22 +60,19 @@ func getDate(c *gin.Context) (time.Time, error) {
} }
func (h *Handler) budgeting(c *gin.Context) { func (h *Handler) budgeting(c *gin.Context) {
budgetID := c.Param("budgetid") alwaysNeededData := c.MustGet("data").(AlwaysNeededData)
budgetUUID, err := uuid.Parse(budgetID) budgetUUID := alwaysNeededData.Budget.ID
if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/login")
return
}
firstOfMonth, err := getDate(c) firstOfMonth, err := getDate(c)
if err != nil { if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String()) c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String())
return return
} }
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0)
d := BudgetingData{ d := BudgetingData{
AlwaysNeededData: c.MustGet("data").(AlwaysNeededData), AlwaysNeededData: alwaysNeededData,
Date: firstOfMonth, Date: firstOfMonth,
Next: firstOfNextMonth, Next: firstOfNextMonth,
Previous: firstOfPreviousMonth, Previous: firstOfPreviousMonth,
@ -91,7 +87,7 @@ func (h *Handler) budgeting(c *gin.Context) {
} }
// skip everything in the future // 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 { if err != nil {
return return
} }
@ -123,8 +119,14 @@ func (h *Handler) budgeting(c *gin.Context) {
c.HTML(http.StatusOK, "budgeting.html", d) 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{} categoriesWithBalance := []CategoryWithBalance{}
hiddenCategory := CategoryWithBalance{
GetCategoriesRow: &postgres.GetCategoriesRow{
Name: "",
Group: "Hidden Categories",
},
}
var moneyUsed float64 = 0 var moneyUsed float64 = 0
for i := range categories { 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, categoryWithBalance)
} }
categoriesWithBalance = append(categoriesWithBalance, hiddenCategory)
return categoriesWithBalance, moneyUsed, nil return categoriesWithBalance, moneyUsed, nil
} }