|
|
|
@ -79,7 +79,7 @@ func (h *Handler) prepareBudgeting(ctx context.Context, budget postgres.Budget,
|
|
|
|
|
return BudgetingForMonthResponse{}, fmt.Errorf("error loading balances: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
categoriesWithBalance, moneyUsed := h.calculateBalances(firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
|
|
|
|
|
categoriesWithBalance, moneyUsed := h.calculateBalances(budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
|
|
|
|
|
availableBalance := h.getAvailableBalance(budget, moneyUsed, cumultativeBalances, categoriesWithBalance, firstOfNextMonth)
|
|
|
|
|
|
|
|
|
|
data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance}
|
|
|
|
@ -107,17 +107,9 @@ func (*Handler) getAvailableBalance(budget postgres.Budget,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
availableBalance.AddI(bal.Transactions)
|
|
|
|
|
availableBalance.AddI(bal.Assignments)
|
|
|
|
|
availableBalance.AddI(bal.Assignments) // should be zero, but who knows
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := range categoriesWithBalance {
|
|
|
|
|
cat := &categoriesWithBalance[i]
|
|
|
|
|
if cat.ID != budget.IncomeCategoryID {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cat.Available = availableBalance
|
|
|
|
|
}
|
|
|
|
|
return availableBalance
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -155,7 +147,7 @@ func (h *Handler) getBudget(c *gin.Context, budgetUUID uuid.UUID) {
|
|
|
|
|
c.JSON(http.StatusOK, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) calculateBalances(firstOfNextMonth time.Time, firstOfMonth time.Time,
|
|
|
|
|
func (h *Handler) calculateBalances(budget postgres.Budget, firstOfNextMonth time.Time, firstOfMonth time.Time,
|
|
|
|
|
categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow,
|
|
|
|
|
) ([]CategoryWithBalance, numeric.Numeric) {
|
|
|
|
|
categoriesWithBalance := []CategoryWithBalance{}
|
|
|
|
@ -163,6 +155,10 @@ func (h *Handler) calculateBalances(firstOfNextMonth time.Time, firstOfMonth tim
|
|
|
|
|
moneyUsed := numeric.Zero()
|
|
|
|
|
for i := range categories {
|
|
|
|
|
cat := &categories[i]
|
|
|
|
|
if cat.ID == budget.IncomeCategoryID {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
categoryWithBalance := NewCategoryWithBalance(cat)
|
|
|
|
|
for _, bal := range cumultativeBalances {
|
|
|
|
|
if bal.CategoryID != cat.ID {
|
|
|
|
|