166 lines
4.4 KiB
Go
166 lines
4.4 KiB
Go
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 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) {
|
|
budgetID := c.Param("budgetid")
|
|
budgetUUID, err := uuid.Parse(budgetID)
|
|
if err != nil {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
|
return
|
|
}
|
|
|
|
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),
|
|
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, 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
|
|
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
|
|
}
|
|
|
|
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)
|
|
}
|
|
return categoriesWithBalance, added, assigned, nil
|
|
}
|