Skip to content

Commit

Permalink
fix: budget income calculation (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
morremeyer authored Apr 27, 2024
1 parent 888154f commit a33b80e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/models/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (b Budget) Income(db *gorm.DB, month types.Month) (income decimal.Decimal,
Joins("JOIN accounts source_account ON transactions.source_account_id = source_account.id AND source_account.deleted_at IS NULL").
Joins("JOIN accounts destination_account ON transactions.destination_account_id = destination_account.id AND destination_account.deleted_at IS NULL").
Joins("JOIN budgets ON source_account.budget_id = budgets.id").
Where("source_account.external = 1").
Where("source_account.on_budget = false AND destination_account.on_budget = true").
Where("destination_account.external = 0").
Where("transactions.envelope_id IS NULL").
Where("transactions.available_from >= date(?) AND transactions.available_from < date(?)", month, month.AddDate(0, 1)).
Expand Down
22 changes: 19 additions & 3 deletions pkg/models/budget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
Name: "TestBudgetCalculations Cash Account",
})

// Regression test for https://github.com/envelope-zero/backend/issues/1007
offBudgetAccount := suite.createTestAccount(models.Account{
BudgetID: budget.ID,
OnBudget: false,
External: false,
Name: "TestBudgetCalculations Off Budget AccountAccount",
})

employerAccount := suite.createTestAccount(models.Account{
BudgetID: budget.ID,
External: true,
Expand Down Expand Up @@ -146,15 +154,23 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
Amount: decimal.NewFromFloat(20),
})

shouldBalance := decimal.NewFromFloat(7269.38)
// Regression test for https://github.com/envelope-zero/backend/issues/1007
_ = suite.createTestTransaction(models.Transaction{
Date: time.Time(marchTwentyTwentyTwo),
SourceAccountID: offBudgetAccount.ID,
DestinationAccountID: cashAccount.ID,
Amount: decimal.NewFromFloat(20),
})

shouldBalance := decimal.NewFromFloat(7289.38)
isBalance, err := budget.Balance(models.DB)
if err != nil {
assert.FailNow(suite.T(), "Balance for budget could not be calculated")
}
assert.True(suite.T(), isBalance.Equal(shouldBalance), "Balance for budget is not correct. Should be %s, is %s", shouldBalance, budget.Balance)
assert.True(suite.T(), isBalance.Equal(shouldBalance), "Balance for budget is not correct. Should be %s, is %s", shouldBalance, isBalance)

// Verify income for used budget in March
shouldIncome := decimal.NewFromFloat(4600)
shouldIncome := decimal.NewFromFloat(4620) // Income transaction from employer + income from off budget account
income, err := budget.Income(models.DB, marchTwentyTwentyTwo)
assert.Nil(suite.T(), err)
assert.True(suite.T(), income.Equal(shouldIncome), "Income is %s, should be %s", income, shouldIncome)
Expand Down

0 comments on commit a33b80e

Please sign in to comment.