Extract Month class
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user