Skip to content

Commit a33b80e

Browse files
authored
fix: budget income calculation (#1023)
1 parent 888154f commit a33b80e

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

pkg/models/budget.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (b Budget) Income(db *gorm.DB, month types.Month) (income decimal.Decimal,
5858
Joins("JOIN accounts source_account ON transactions.source_account_id = source_account.id AND source_account.deleted_at IS NULL").
5959
Joins("JOIN accounts destination_account ON transactions.destination_account_id = destination_account.id AND destination_account.deleted_at IS NULL").
6060
Joins("JOIN budgets ON source_account.budget_id = budgets.id").
61-
Where("source_account.external = 1").
61+
Where("source_account.on_budget = false AND destination_account.on_budget = true").
6262
Where("destination_account.external = 0").
6363
Where("transactions.envelope_id IS NULL").
6464
Where("transactions.available_from >= date(?) AND transactions.available_from < date(?)", month, month.AddDate(0, 1)).

pkg/models/budget_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
5656
Name: "TestBudgetCalculations Cash Account",
5757
})
5858

59+
// Regression test for https://github.com/envelope-zero/backend/issues/1007
60+
offBudgetAccount := suite.createTestAccount(models.Account{
61+
BudgetID: budget.ID,
62+
OnBudget: false,
63+
External: false,
64+
Name: "TestBudgetCalculations Off Budget AccountAccount",
65+
})
66+
5967
employerAccount := suite.createTestAccount(models.Account{
6068
BudgetID: budget.ID,
6169
External: true,
@@ -146,15 +154,23 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
146154
Amount: decimal.NewFromFloat(20),
147155
})
148156

149-
shouldBalance := decimal.NewFromFloat(7269.38)
157+
// Regression test for https://github.com/envelope-zero/backend/issues/1007
158+
_ = suite.createTestTransaction(models.Transaction{
159+
Date: time.Time(marchTwentyTwentyTwo),
160+
SourceAccountID: offBudgetAccount.ID,
161+
DestinationAccountID: cashAccount.ID,
162+
Amount: decimal.NewFromFloat(20),
163+
})
164+
165+
shouldBalance := decimal.NewFromFloat(7289.38)
150166
isBalance, err := budget.Balance(models.DB)
151167
if err != nil {
152168
assert.FailNow(suite.T(), "Balance for budget could not be calculated")
153169
}
154-
assert.True(suite.T(), isBalance.Equal(shouldBalance), "Balance for budget is not correct. Should be %s, is %s", shouldBalance, budget.Balance)
170+
assert.True(suite.T(), isBalance.Equal(shouldBalance), "Balance for budget is not correct. Should be %s, is %s", shouldBalance, isBalance)
155171

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

0 commit comments

Comments
 (0)