Calculate overspent for last month
This commit is contained in:
parent
b812b19b34
commit
a48509e041
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user