Fix amount for available balance #50

Merged
jacob1123 merged 28 commits from available-balance into master 2022-04-08 22:43:22 +02:00
5 changed files with 97 additions and 37 deletions
Showing only changes of commit 54e591bc5e - Show all commits

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"time"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
"git.javil.eu/jacob1123/budgeteer/postgres/numeric" "git.javil.eu/jacob1123/budgeteer/postgres/numeric"
@ -12,17 +11,6 @@ import (
"github.com/google/uuid" "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 { type CategoryWithBalance struct {
*postgres.GetCategoriesRow *postgres.GetCategoriesRow
Available numeric.Numeric Available numeric.Numeric
@ -53,13 +41,13 @@ func (h *Handler) budgetingForMonth(c *gin.Context) {
return return
} }
firstOfMonth, err := getDate(c) month, err := getDate(c)
if err != nil { if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budget.ID.String()) c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budget.ID.String())
return return
} }
data, err := h.prepareBudgeting(c.Request.Context(), budget, firstOfMonth) data, err := h.getBudgetingViewForMonth(c.Request.Context(), budget, month)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
@ -67,8 +55,7 @@ func (h *Handler) budgetingForMonth(c *gin.Context) {
c.JSON(http.StatusOK, data) c.JSON(http.StatusOK, data)
} }
func (h *Handler) prepareBudgeting(ctx context.Context, budget postgres.Budget, firstOfMonth time.Time) (BudgetingForMonthResponse, error) { func (h *Handler) getBudgetingViewForMonth(ctx context.Context, budget postgres.Budget, month Month) (BudgetingForMonthResponse, error) {
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
categories, err := h.Service.GetCategories(ctx, budget.ID) categories, err := h.Service.GetCategories(ctx, budget.ID)
if err != nil { if err != nil {
return BudgetingForMonthResponse{}, fmt.Errorf("error loading categories: %w", err) 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) return BudgetingForMonthResponse{}, fmt.Errorf("error loading balances: %w", err)
} }
categoriesWithBalance, moneyUsed := h.calculateBalances(budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances) categoriesWithBalance, moneyUsed := h.calculateBalances(budget, month, categories, cumultativeBalances)
availableBalance := h.getAvailableBalance(budget, moneyUsed, cumultativeBalances, categoriesWithBalance, firstOfNextMonth) availableBalance := h.getAvailableBalance(budget, month, moneyUsed, cumultativeBalances)
data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance} data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance}
return data, nil return data, nil
@ -91,9 +78,8 @@ type BudgetingForMonthResponse struct {
AvailableBalance numeric.Numeric AvailableBalance numeric.Numeric
} }
func (*Handler) getAvailableBalance(budget postgres.Budget, func (*Handler) getAvailableBalance(budget postgres.Budget, month Month,
moneyUsed numeric.Numeric, cumultativeBalances []postgres.GetCumultativeBalancesRow, moneyUsed numeric.Numeric, cumultativeBalances []postgres.GetCumultativeBalancesRow,
categoriesWithBalance []CategoryWithBalance, firstOfNextMonth time.Time,
) numeric.Numeric { ) numeric.Numeric {
availableBalance := moneyUsed availableBalance := moneyUsed
@ -102,7 +88,7 @@ func (*Handler) getAvailableBalance(budget postgres.Budget,
continue continue
} }
if !bal.Date.Before(firstOfNextMonth) { if !month.nextMonth().isAfter(bal.Date) {
continue continue
} }
@ -147,7 +133,7 @@ func (h *Handler) getBudget(c *gin.Context, budgetUUID uuid.UUID) {
c.JSON(http.StatusOK, data) 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, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow,
) ([]CategoryWithBalance, numeric.Numeric) { ) ([]CategoryWithBalance, numeric.Numeric) {
categoriesWithBalance := []CategoryWithBalance{} categoriesWithBalance := []CategoryWithBalance{}
@ -166,19 +152,19 @@ func (h *Handler) calculateBalances(budget postgres.Budget, firstOfNextMonth tim
} }
// skip everything in the future // skip everything in the future
if !bal.Date.Before(firstOfNextMonth) { if month.nextMonth().isAfter(bal.Date) {
continue continue
} }
moneyUsed.SubI(bal.Assignments) moneyUsed.SubI(bal.Assignments)
categoryWithBalance.Available.AddI(bal.Assignments) categoryWithBalance.Available.AddI(bal.Assignments)
categoryWithBalance.Available.AddI(bal.Transactions) 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) moneyUsed.AddI(categoryWithBalance.Available)
categoryWithBalance.Available = numeric.Zero() 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.Activity = bal.Transactions
categoryWithBalance.Assigned = bal.Assignments categoryWithBalance.Assigned = bal.Assignments
} }

View File

@ -44,7 +44,7 @@ func (h *Handler) setCategoryAssignment(c *gin.Context) {
updateArgs := postgres.UpdateAssignmentParams{ updateArgs := postgres.UpdateAssignmentParams{
CategoryID: categoryUUID, CategoryID: categoryUUID,
Date: date, Date: date.FirstOfMonth(),
Amount: amount, Amount: amount,
} }
err = h.Service.UpdateAssignment(c.Request.Context(), updateArgs) err = h.Service.UpdateAssignment(c.Request.Context(), updateArgs)

View File

@ -181,9 +181,8 @@ func AssertCategoriesAndAvailableEqual(ctx context.Context, t *testing.T, loc *t
} }
year, _ := strconv.Atoi(parts[0]) year, _ := strconv.Atoi(parts[0])
month, _ := strconv.Atoi(parts[1]) month, _ := strconv.Atoi(parts[1])
first := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc)
testCaseFile := filepath.Join(resultDir, file.Name()) 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 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.Helper()
t.Run(first.Format("2006-01"), func(t *testing.T) { t.Run(month.String(), func(t *testing.T) {
t.Parallel() t.Parallel()
data, err := h.prepareBudgeting(ctx, *budget, first) data, err := h.getBudgetingViewForMonth(ctx, *budget, month)
if err != nil { if err != nil {
t.Errorf("prepare budgeting: %s", err) t.Errorf("prepare budgeting: %s", err)
return return

View File

@ -8,7 +8,75 @@ import (
"github.com/gin-gonic/gin" "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 var year, month int
yearString := c.Param("year") yearString := c.Param("year")
monthString := c.Param("month") monthString := c.Param("month")
@ -18,13 +86,20 @@ func getDate(c *gin.Context) (time.Time, error) {
year, err := strconv.Atoi(yearString) year, err := strconv.Atoi(yearString)
if err != nil { 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) month, err = strconv.Atoi(monthString)
if err != nil { 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}
} }

View File

@ -86,12 +86,12 @@ function assignedChanged(e : Event, category : Category){
:value="accountStore.GetBudgeted(selected.Year, selected.Month).assigned" :value="accountStore.GetBudgeted(selected.Year, selected.Month).assigned"
/> />
</span><br /> </span><br />
<span>Budgeted this month: <span>Deassigned this month:
<Currency <Currency
:value="accountStore.GetBudgeted(selected.Year, selected.Month).deassigned" :value="accountStore.GetBudgeted(selected.Year, selected.Month).deassigned"
/> />
</span><br /> </span><br />
<span>Budgeted this month: <span>Spent this month:
<Currency <Currency
:value="accountStore.GetBudgeted(selected.Year, selected.Month).activity" :value="accountStore.GetBudgeted(selected.Year, selected.Month).activity"
/> />