diff --git a/server/budgeting.go b/server/budgeting.go index 8d3ef05..5d2e97e 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "time" "git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres/numeric" @@ -12,17 +11,6 @@ import ( "github.com/google/uuid" ) -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 numeric.Numeric @@ -53,13 +41,13 @@ func (h *Handler) budgetingForMonth(c *gin.Context) { return } - firstOfMonth, err := getDate(c) + month, err := getDate(c) if err != nil { c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budget.ID.String()) return } - data, err := h.prepareBudgeting(c.Request.Context(), budget, firstOfMonth) + data, err := h.getBudgetingViewForMonth(c.Request.Context(), budget, month) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return @@ -67,8 +55,7 @@ func (h *Handler) budgetingForMonth(c *gin.Context) { c.JSON(http.StatusOK, data) } -func (h *Handler) prepareBudgeting(ctx context.Context, budget postgres.Budget, firstOfMonth time.Time) (BudgetingForMonthResponse, error) { - firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0) +func (h *Handler) getBudgetingViewForMonth(ctx context.Context, budget postgres.Budget, month Month) (BudgetingForMonthResponse, error) { categories, err := h.Service.GetCategories(ctx, budget.ID) if err != nil { return BudgetingForMonthResponse{}, fmt.Errorf("error loading categories: %w", err) @@ -79,8 +66,8 @@ func (h *Handler) prepareBudgeting(ctx context.Context, budget postgres.Budget, return BudgetingForMonthResponse{}, fmt.Errorf("error loading balances: %w", err) } - categoriesWithBalance, moneyUsed := h.calculateBalances(budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) - availableBalance := h.getAvailableBalance(budget, moneyUsed, cumultativeBalances, categoriesWithBalance, firstOfNextMonth) + categoriesWithBalance, moneyUsed := h.calculateBalances(budget, month, categories, cumultativeBalances) + availableBalance := h.getAvailableBalance(budget, month, moneyUsed, cumultativeBalances) data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance} return data, nil @@ -91,9 +78,8 @@ type BudgetingForMonthResponse struct { AvailableBalance numeric.Numeric } -func (*Handler) getAvailableBalance(budget postgres.Budget, +func (*Handler) getAvailableBalance(budget postgres.Budget, month Month, moneyUsed numeric.Numeric, cumultativeBalances []postgres.GetCumultativeBalancesRow, - categoriesWithBalance []CategoryWithBalance, firstOfNextMonth time.Time, ) numeric.Numeric { availableBalance := moneyUsed @@ -102,7 +88,7 @@ func (*Handler) getAvailableBalance(budget postgres.Budget, continue } - if !bal.Date.Before(firstOfNextMonth) { + if !month.nextMonth().isAfter(bal.Date) { continue } @@ -147,7 +133,7 @@ func (h *Handler) getBudget(c *gin.Context, budgetUUID uuid.UUID) { c.JSON(http.StatusOK, data) } -func (h *Handler) calculateBalances(budget postgres.Budget, firstOfNextMonth time.Time, firstOfMonth time.Time, +func (h *Handler) calculateBalances(budget postgres.Budget, month Month, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow, ) ([]CategoryWithBalance, numeric.Numeric) { categoriesWithBalance := []CategoryWithBalance{} @@ -166,19 +152,19 @@ func (h *Handler) calculateBalances(budget postgres.Budget, firstOfNextMonth tim } // skip everything in the future - if !bal.Date.Before(firstOfNextMonth) { + if month.nextMonth().isAfter(bal.Date) { continue } moneyUsed.SubI(bal.Assignments) categoryWithBalance.Available.AddI(bal.Assignments) categoryWithBalance.Available.AddI(bal.Transactions) - if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) { + if !categoryWithBalance.Available.IsPositive() && month.isAfter(bal.Date) { moneyUsed.AddI(categoryWithBalance.Available) categoryWithBalance.Available = numeric.Zero() } - if bal.Date.Year() == firstOfMonth.Year() && bal.Date.Month() == firstOfMonth.Month() { + if month.contains(bal.Date) { categoryWithBalance.Activity = bal.Transactions categoryWithBalance.Assigned = bal.Assignments } diff --git a/server/category.go b/server/category.go index ab4676c..1881f2a 100644 --- a/server/category.go +++ b/server/category.go @@ -44,7 +44,7 @@ func (h *Handler) setCategoryAssignment(c *gin.Context) { updateArgs := postgres.UpdateAssignmentParams{ CategoryID: categoryUUID, - Date: date, + Date: date.FirstOfMonth(), Amount: amount, } err = h.Service.UpdateAssignment(c.Request.Context(), updateArgs) diff --git a/server/main_test.go b/server/main_test.go index 674402f..99fa6c6 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -181,9 +181,8 @@ func AssertCategoriesAndAvailableEqual(ctx context.Context, t *testing.T, loc *t } year, _ := strconv.Atoi(parts[0]) month, _ := strconv.Atoi(parts[1]) - first := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc) testCaseFile := filepath.Join(resultDir, file.Name()) - handler.CheckAvailableBalance(ctx, t, testCaseFile, budget, first) + handler.CheckAvailableBalance(ctx, t, testCaseFile, budget, Month{year, month}) } }) } @@ -199,12 +198,12 @@ type CategoryTestData struct { Assigned float64 } -func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, testCaseFile string, budget *postgres.Budget, first time.Time) { +func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, testCaseFile string, budget *postgres.Budget, month Month) { t.Helper() - t.Run(first.Format("2006-01"), func(t *testing.T) { + t.Run(month.String(), func(t *testing.T) { t.Parallel() - data, err := h.prepareBudgeting(ctx, *budget, first) + data, err := h.getBudgetingViewForMonth(ctx, *budget, month) if err != nil { t.Errorf("prepare budgeting: %s", err) return diff --git a/server/util.go b/server/util.go index ffb63a4..531a88e 100644 --- a/server/util.go +++ b/server/util.go @@ -8,7 +8,75 @@ import ( "github.com/gin-gonic/gin" ) -func getDate(c *gin.Context) (time.Time, error) { +type Month struct { + Year int + Month int +} + +func NewFromTime(date time.Time) Month { + return Month{date.Year(), int(date.Month())} +} + +func (m Month) String() string { + return fmt.Sprintf("%d-%d", m.Year, m.Month) +} + +func (m Month) FirstOfMonth() time.Time { + return time.Date(m.Year, time.Month(m.Month), 1, 0, 0, 0, 0, time.Now().Location()) +} + +func (m Month) nextMonth() Month { + if m.Month == 12 { + return Month{m.Year + 1, 1} + } + return Month{m.Year, m.Month} +} + +func (m Month) InFuture(date time.Time) bool { + if m.Year < date.Year() { + return true + } + + if m.Year > date.Year() { + return false + } + + return time.Month(m.Month) < date.Month() +} + +func (m Month) isBeforeOrContains(date time.Time) bool { + if m.Year < date.Year() { + return true + } + + if m.Year > date.Year() { + return false + } + + return time.Month(m.Month) <= date.Month() +} + +func (m Month) InPast(date time.Time) bool { + if m.Year > date.Year() { + return false + } + + if m.Year < date.Year() { + return true + } + + return time.Month(m.Month) > date.Month() +} + +func (m Month) InPresent(date time.Time) bool { + if date.Year() != m.Year { + return false + } + + return date.Month() == time.Month(m.Month) +} + +func getDate(c *gin.Context) (Month, error) { var year, month int yearString := c.Param("year") monthString := c.Param("month") @@ -18,13 +86,20 @@ func getDate(c *gin.Context) (time.Time, error) { year, err := strconv.Atoi(yearString) if err != nil { - return time.Time{}, fmt.Errorf("parse year: %w", err) + return Month{}, fmt.Errorf("parse year: %w", err) } month, err = strconv.Atoi(monthString) if err != nil { - return time.Time{}, fmt.Errorf("parse month: %w", err) + return Month{}, fmt.Errorf("parse month: %w", err) } - return getFirstOfMonth(year, month, time.Now().Location()), nil + return Month{year, month}, nil +} + +func getFirstOfMonthTime(date time.Time) Month { + var monthM time.Month + year, monthM, _ := date.Date() + month := int(monthM) + return Month{year, month} } diff --git a/web/src/pages/Budgeting.vue b/web/src/pages/Budgeting.vue index d800a75..8b4dd8a 100644 --- a/web/src/pages/Budgeting.vue +++ b/web/src/pages/Budgeting.vue @@ -86,12 +86,12 @@ function assignedChanged(e : Event, category : Category){ :value="accountStore.GetBudgeted(selected.Year, selected.Month).assigned" />
- Budgeted this month: + Deassigned this month:
- Budgeted this month: + Spent this month: