Merge added and assigned into moneyUsed

This commit is contained in:
Jan Bader 2021-12-11 13:03:26 +00:00
parent e2413290b4
commit 466775817f

View File

@ -91,7 +91,7 @@ func (h *Handler) budgeting(c *gin.Context) {
}
// skip everything in the future
categoriesWithBalance, added, assigned, err := h.calculateBalances(c, budgetUUID, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
categoriesWithBalance, moneyUsed, err := h.calculateBalances(c, budgetUUID, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
if err != nil {
return
}
@ -103,7 +103,7 @@ func (h *Handler) budgeting(c *gin.Context) {
if cat.ID != data.Budget.IncomeCategoryID {
continue
}
availableBalance = -assigned - added
availableBalance = moneyUsed
for _, bal := range cumultativeBalances {
if bal.CategoryID != cat.ID {
@ -123,11 +123,10 @@ func (h *Handler) budgeting(c *gin.Context) {
c.HTML(http.StatusOK, "budgeting.html", d)
}
func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, float64, float64, error) {
func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow, cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, float64, error) {
categoriesWithBalance := []CategoryWithBalance{}
var added float64 = 0
var assigned float64 = 0
var moneyUsed float64 = 0
for i := range categories {
cat := &categories[i]
categoryWithBalance := CategoryWithBalance{
@ -142,11 +141,11 @@ func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstO
continue
}
assigned += bal.Assignments.GetFloat64()
moneyUsed -= bal.Assignments.GetFloat64()
categoryWithBalance.Available += bal.Assignments.GetFloat64()
categoryWithBalance.Available += bal.Transactions.GetFloat64()
if categoryWithBalance.Available < 0 && bal.Date.Before(firstOfMonth) {
added -= categoryWithBalance.Available
moneyUsed += categoryWithBalance.Available
categoryWithBalance.Available = 0
}
@ -161,5 +160,5 @@ func (h *Handler) calculateBalances(c *gin.Context, budgetUUID uuid.UUID, firstO
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
}
return categoriesWithBalance, added, assigned, nil
return categoriesWithBalance, moneyUsed, nil
}