package http import ( "fmt" "net/http" "strconv" "time" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" ) type BudgetingData struct { AlwaysNeededData Categories []CategoryWithBalance AvailableBalance float64 Date time.Time Next time.Time Previous time.Time } func getFirstOfMonth(year, month int, location *time.Location) time.Time { return time.Date(year, time.Month(month), 1, 0, 0, 0, 0, location) } func getFirstOfMonthTime(date time.Time) time.Time { var monthM time.Month year, monthM, _ := date.Date() month := int(monthM) return getFirstOfMonth(year, month, date.Location()) } type CategoryWithBalance struct { *postgres.GetCategoriesRow Available float64 AvailableLastMonth float64 Activity float64 Assigned float64 } func getDate(c *gin.Context) (time.Time, error) { var year, month int yearString := c.Param("year") monthString := c.Param("month") if yearString == "" && monthString == "" { return getFirstOfMonthTime(time.Now()), nil } year, err := strconv.Atoi(yearString) if err != nil { return time.Time{}, fmt.Errorf("parse year: %w", err) } month, err = strconv.Atoi(monthString) if err != nil { return time.Time{}, fmt.Errorf("parse month: %w", err) } return getFirstOfMonth(year, month, time.Now().Location()), nil } func (h *Handler) budgeting(c *gin.Context) { 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: alwaysNeededData, Date: firstOfMonth, Next: firstOfNextMonth, Previous: firstOfPreviousMonth, } categories, err := h.Service.DB.GetCategories(c.Request.Context(), budgetUUID) cumultativeBalances, err := h.Service.DB.GetCumultativeBalances(c.Request.Context(), budgetUUID) if err != nil { c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("load balances: %w", err)) return } // skip everything in the future categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, alwaysNeededData.Budget, 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 = moneyUsed 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, 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 { cat := &categories[i] categoryWithBalance := CategoryWithBalance{ GetCategoriesRow: cat, } for _, bal := range cumultativeBalances { if bal.CategoryID != cat.ID { continue } if !bal.Date.Before(firstOfNextMonth) { continue } moneyUsed -= bal.Assignments.GetFloat64() categoryWithBalance.Available += bal.Assignments.GetFloat64() categoryWithBalance.Available += bal.Transactions.GetFloat64() if categoryWithBalance.Available < 0 && bal.Date.Before(firstOfMonth) { moneyUsed += categoryWithBalance.Available categoryWithBalance.Available = 0 } if bal.Date.Before(firstOfMonth) { categoryWithBalance.AvailableLastMonth = categoryWithBalance.Available } else if bal.Date.Before(firstOfNextMonth) { categoryWithBalance.Activity = bal.Transactions.GetFloat64() categoryWithBalance.Assigned = bal.Assignments.GetFloat64() } } // 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 }