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