diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index aebc7ff..e39e3e2 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -11,10 +11,10 @@ RETURNING id; -- name: UpdateTransaction :exec UPDATE transactions SET date = $1, - memo = $2, - amount = $3, - payee_id = $4, - category_id = $5 + memo = $2, + amount = $3, + payee_id = $4, + category_id = $5 WHERE id = $6; -- name: SetTransactionReconciled :exec diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index 60fcf1d..f110a70 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -294,10 +294,10 @@ func (q *Queries) SetTransactionReconciled(ctx context.Context, id uuid.UUID) er const updateTransaction = `-- name: UpdateTransaction :exec UPDATE transactions SET date = $1, - memo = $2, - amount = $3, - payee_id = $4, - category_id = $5 + memo = $2, + amount = $3, + payee_id = $4, + category_id = $5 WHERE id = $6 ` diff --git a/server/budgeting.go b/server/budgeting.go index 8ab0cae..0527ce0 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -66,16 +66,17 @@ func (h *Handler) getBudgetingViewForMonth(ctx context.Context, budget postgres. return BudgetingForMonthResponse{}, fmt.Errorf("error loading balances: %w", err) } - categoriesWithBalance, moneyUsed := h.calculateBalances(budget, month, categories, cumultativeBalances) + categoriesWithBalance, moneyUsed, overspentLastMonth := h.calculateBalances(budget, month, categories, cumultativeBalances) availableBalance := h.getAvailableBalance(budget, month, moneyUsed, cumultativeBalances) - data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance} + data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance, overspentLastMonth} return data, nil } type BudgetingForMonthResponse struct { - Categories []CategoryWithBalance - AvailableBalance numeric.Numeric + Categories []CategoryWithBalance + AvailableBalance numeric.Numeric + OverspentLastMonth numeric.Numeric } func (*Handler) getAvailableBalance(budget postgres.Budget, month Month, @@ -135,10 +136,11 @@ func (h *Handler) getBudget(c *gin.Context, budgetUUID uuid.UUID) { func (h *Handler) calculateBalances(budget postgres.Budget, month Month, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow, -) ([]CategoryWithBalance, numeric.Numeric) { +) ([]CategoryWithBalance, numeric.Numeric, numeric.Numeric) { categoriesWithBalance := []CategoryWithBalance{} moneyUsed := numeric.Zero() + overspentLastMonth := numeric.Zero() categories = append(categories, postgres.GetCategoriesRow{ Group: "Income", Name: "No Category", @@ -172,6 +174,9 @@ func (h *Handler) calculateBalances(budget postgres.Budget, month Month, categoryWithBalance.AvailableLastMonth.AddI(bal.Transactions) if !categoryWithBalance.AvailableLastMonth.IsPositive() { moneyUsed.AddI(categoryWithBalance.AvailableLastMonth) + if month.Previous().InPresent(bal.Date) { + overspentLastMonth.AddI(categoryWithBalance.AvailableLastMonth) + } categoryWithBalance.AvailableLastMonth = numeric.Zero() } } @@ -179,5 +184,5 @@ func (h *Handler) calculateBalances(budget postgres.Budget, month Month, categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance) } - return categoriesWithBalance, moneyUsed + return categoriesWithBalance, moneyUsed, overspentLastMonth } diff --git a/server/month.go b/server/month.go index 4d46f3e..c3fdf2f 100644 --- a/server/month.go +++ b/server/month.go @@ -22,6 +22,22 @@ 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) Previous() Month { + if m.Month == int(time.January) { + return Month{Year: m.Year - 1, Month: int(time.December)} + } + + return Month{Year: m.Year, Month: m.Month - 1} +} + +func (m Month) Next() Month { + if m.Month == int(time.December) { + return Month{Year: m.Year + 1, Month: int(time.January)} + } + + return Month{Year: m.Year, Month: m.Month + 1} +} + func (m Month) InFuture(date time.Time) bool { if m.Year < date.Year() { return true diff --git a/web/src/pages/Budgeting.vue b/web/src/pages/Budgeting.vue index 09fc432..34fbd8e 100644 --- a/web/src/pages/Budgeting.vue +++ b/web/src/pages/Budgeting.vue @@ -78,29 +78,54 @@ const budgeted = computed(() => accountStore.GetBudgeted(selected.value.Year, se