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)
|
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)
|
availableBalance := h.getAvailableBalance(budget, month, moneyUsed, cumultativeBalances)
|
||||||
|
|
||||||
data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance}
|
data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance, overspentLastMonth}
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type BudgetingForMonthResponse struct {
|
type BudgetingForMonthResponse struct {
|
||||||
Categories []CategoryWithBalance
|
Categories []CategoryWithBalance
|
||||||
AvailableBalance numeric.Numeric
|
AvailableBalance numeric.Numeric
|
||||||
|
OverspentLastMonth numeric.Numeric
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*Handler) getAvailableBalance(budget postgres.Budget, month Month,
|
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,
|
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, numeric.Numeric) {
|
||||||
categoriesWithBalance := []CategoryWithBalance{}
|
categoriesWithBalance := []CategoryWithBalance{}
|
||||||
|
|
||||||
moneyUsed := numeric.Zero()
|
moneyUsed := numeric.Zero()
|
||||||
|
overspentLastMonth := numeric.Zero()
|
||||||
categories = append(categories, postgres.GetCategoriesRow{
|
categories = append(categories, postgres.GetCategoriesRow{
|
||||||
Group: "Income",
|
Group: "Income",
|
||||||
Name: "No Category",
|
Name: "No Category",
|
||||||
@ -172,6 +174,9 @@ func (h *Handler) calculateBalances(budget postgres.Budget, month Month,
|
|||||||
categoryWithBalance.AvailableLastMonth.AddI(bal.Transactions)
|
categoryWithBalance.AvailableLastMonth.AddI(bal.Transactions)
|
||||||
if !categoryWithBalance.AvailableLastMonth.IsPositive() {
|
if !categoryWithBalance.AvailableLastMonth.IsPositive() {
|
||||||
moneyUsed.AddI(categoryWithBalance.AvailableLastMonth)
|
moneyUsed.AddI(categoryWithBalance.AvailableLastMonth)
|
||||||
|
if month.Previous().InPresent(bal.Date) {
|
||||||
|
overspentLastMonth.AddI(categoryWithBalance.AvailableLastMonth)
|
||||||
|
}
|
||||||
categoryWithBalance.AvailableLastMonth = numeric.Zero()
|
categoryWithBalance.AvailableLastMonth = numeric.Zero()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -179,5 +184,5 @@ func (h *Handler) calculateBalances(budget postgres.Budget, month Month,
|
|||||||
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
|
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
|
||||||
}
|
}
|
||||||
|
|
||||||
return categoriesWithBalance, moneyUsed
|
return categoriesWithBalance, moneyUsed, overspentLastMonth
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user