package http import ( "fmt" "net/http" "strconv" "time" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" "github.com/google/uuid" ) type BudgetingData struct { AlwaysNeededData Categories []CategoryWithBalance AvailableBalance postgres.Numeric 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 (h *Handler) budgeting(c *gin.Context) { budgetID := c.Param("budgetid") budgetUUID, err := uuid.Parse(budgetID) if err != nil { c.Redirect(http.StatusTemporaryRedirect, "/login") return } var firstOfMonth time.Time var year, month int yearString := c.Param("year") monthString := c.Param("month") if yearString != "" && monthString != "" { year, err = strconv.Atoi(yearString) if err != nil { c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String()) return } month, err = strconv.Atoi(monthString) if err != nil { c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String()) return } firstOfMonth = getFirstOfMonth(year, month, time.Now().Location()) } else { firstOfMonth = getFirstOfMonthTime(time.Now()) } firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) firstOfPreviousMonth := firstOfMonth.AddDate(0, -1, 0) 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 } categoriesWithBalance := []CategoryWithBalance{} var added float64 = 0 var assigned float64 = 0 for i := range categories { cat := &categories[i] categoryWithBalance := CategoryWithBalance{ GetCategoriesRow: cat, } for _, bal := range cumultativeBalances { if bal.CategoryID != cat.ID { continue } // skip everything in the future if !bal.Date.Before(firstOfNextMonth) { continue } assigned += bal.Assignments.GetFloat64() categoryWithBalance.Available += bal.Assignments.GetFloat64() categoryWithBalance.Available += bal.Transactions.GetFloat64() if categoryWithBalance.Available < 0 && bal.Date.Before(firstOfMonth) { added -= 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() } } 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) }