Fix bugs in calculateBalances and handle not found categories
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
cf39db52fb
commit
684efffbdf
@ -162,48 +162,36 @@ func (h *Handler) calculateBalances(budget postgres.Budget,
|
|||||||
) ([]CategoryWithBalance, numeric.Numeric) {
|
) ([]CategoryWithBalance, numeric.Numeric) {
|
||||||
categoriesWithBalance := []CategoryWithBalance{}
|
categoriesWithBalance := []CategoryWithBalance{}
|
||||||
|
|
||||||
moneyUsed2 := numeric.Zero()
|
moneyUsed := numeric.Zero()
|
||||||
moneyUsed := &moneyUsed2
|
|
||||||
for i := range categories {
|
for i := range categories {
|
||||||
cat := &categories[i]
|
cat := &categories[i]
|
||||||
// do not show hidden categories
|
categoryWithBalance := NewCategoryWithBalance(cat)
|
||||||
categoryWithBalance := h.CalculateCategoryBalances(cat, cumultativeBalances,
|
for _, bal := range cumultativeBalances {
|
||||||
firstOfNextMonth, moneyUsed, firstOfMonth, budget)
|
if bal.CategoryID != cat.ID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip everything in the future
|
||||||
|
if !bal.Date.Before(firstOfNextMonth) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
moneyUsed.SubI(bal.Assignments)
|
||||||
|
categoryWithBalance.Available.AddI(bal.Assignments)
|
||||||
|
categoryWithBalance.Available.AddI(bal.Transactions)
|
||||||
|
if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) {
|
||||||
|
moneyUsed.AddI(categoryWithBalance.Available)
|
||||||
|
categoryWithBalance.Available = numeric.Zero()
|
||||||
|
}
|
||||||
|
|
||||||
|
if bal.Date.Year() == firstOfMonth.Year() && bal.Date.Month() == firstOfMonth.Month() {
|
||||||
|
categoryWithBalance.Activity = bal.Transactions
|
||||||
|
categoryWithBalance.Assigned = bal.Assignments
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
|
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
|
||||||
}
|
}
|
||||||
|
|
||||||
return categoriesWithBalance, *moneyUsed
|
return categoriesWithBalance, moneyUsed
|
||||||
}
|
|
||||||
|
|
||||||
func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow,
|
|
||||||
cumultativeBalances []postgres.GetCumultativeBalancesRow, firstOfNextMonth time.Time,
|
|
||||||
moneyUsed *numeric.Numeric, firstOfMonth time.Time, budget postgres.Budget,
|
|
||||||
) CategoryWithBalance {
|
|
||||||
categoryWithBalance := NewCategoryWithBalance(cat)
|
|
||||||
for _, bal := range cumultativeBalances {
|
|
||||||
if bal.CategoryID != cat.ID {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// skip everything in the future
|
|
||||||
if !bal.Date.Before(firstOfNextMonth) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
moneyUsed.SubI(bal.Assignments)
|
|
||||||
categoryWithBalance.Available.AddI(bal.Assignments)
|
|
||||||
categoryWithBalance.Available.AddI(bal.Transactions)
|
|
||||||
if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) {
|
|
||||||
moneyUsed.AddI(categoryWithBalance.Available)
|
|
||||||
categoryWithBalance.Available = numeric.Zero()
|
|
||||||
}
|
|
||||||
|
|
||||||
if bal.Date.Before(firstOfNextMonth) {
|
|
||||||
categoryWithBalance.Activity = bal.Transactions
|
|
||||||
categoryWithBalance.Assigned = bal.Assignments
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return categoryWithBalance
|
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,8 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal(t, -115170.56, data.AvailableBalance.GetFloat64(), "available balance")
|
// 2022-01 assert_equal(t, -115170.56, data.AvailableBalance.GetFloat64(), "available balance")
|
||||||
|
assert_equal(t, -110181.600, data.AvailableBalance.GetFloat64(), "available balance")
|
||||||
|
|
||||||
categoryTestDataFile, err := os.Open("../testdata/production-export/results/categories-2021-12.json")
|
categoryTestDataFile, err := os.Open("../testdata/production-export/results/categories-2021-12.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -112,6 +113,7 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget
|
|||||||
}
|
}
|
||||||
|
|
||||||
for categoryName, categoryTestData := range categoryTestData {
|
for categoryName, categoryTestData := range categoryTestData {
|
||||||
|
found := false
|
||||||
for _, category := range data.Categories {
|
for _, category := range data.Categories {
|
||||||
name := category.Group + " : " + category.Name
|
name := category.Group + " : " + category.Name
|
||||||
|
|
||||||
@ -119,8 +121,12 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget
|
|||||||
assert_equal(t, categoryTestData.Available, category.Available.GetFloat64(), "available for "+categoryName)
|
assert_equal(t, categoryTestData.Available, category.Available.GetFloat64(), "available for "+categoryName)
|
||||||
assert_equal(t, categoryTestData.Activity, category.Activity.GetFloat64(), "activity for "+categoryName)
|
assert_equal(t, categoryTestData.Activity, category.Activity.GetFloat64(), "activity for "+categoryName)
|
||||||
assert_equal(t, categoryTestData.Assigned, category.Assigned.GetFloat64(), "assigned for "+categoryName)
|
assert_equal(t, categoryTestData.Assigned, category.Assigned.GetFloat64(), "assigned for "+categoryName)
|
||||||
|
found = true
|
||||||
}
|
}
|
||||||
fmt.Printf("%s: %f %f\n", category.Name, category.Activity.GetFloat64(), category.Available.GetFloat64())
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Errorf("category " + categoryName + " was not found in result")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user