Skip to content

Commit 0208596

Browse files
committed
fix: total income calculated up to the specified month
1 parent 69436f2 commit 0208596

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

pkg/models/budget.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ func (b Budget) Income(t time.Time) (decimal.Decimal, error) {
8888
}
8989

9090
// TotalIncome calculates the total income over all time.
91-
func (b Budget) TotalIncome() (decimal.Decimal, error) {
91+
func (b Budget) TotalIncome(month time.Time) (decimal.Decimal, error) {
92+
// Only use the year and month values, everything else is reset to the start
93+
// Add a month to also factor in all allocations in the requested month
94+
month = time.Date(month.Year(), month.AddDate(0, 1, 0).Month(), 1, 0, 0, 0, 0, time.UTC)
95+
9296
var income decimal.NullDecimal
9397
err := database.DB.
9498
Select("SUM(amount)").
@@ -97,6 +101,7 @@ func (b Budget) TotalIncome() (decimal.Decimal, error) {
97101
Where("source_account.external = 1").
98102
Where("destination_account.external = 0").
99103
Where("transactions.envelope_id IS NULL").
104+
Where("transactions.date < date(?) ", month).
100105
Where(&Transaction{
101106
TransactionCreate: TransactionCreate{
102107
BudgetID: b.ID,

pkg/models/budget_test.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,23 @@ func (suite *TestSuiteEnv) TestBudgetCalculations() {
138138
suite.Assert().Fail("Resource could not be saved", err)
139139
}
140140

141-
salaryTransaction := models.Transaction{
141+
salaryTransactionFebruary := models.Transaction{
142+
TransactionCreate: models.TransactionCreate{
143+
Date: marchFifteenthTwentyTwentyTwo,
144+
BudgetID: budget.ID,
145+
EnvelopeID: nil,
146+
SourceAccountID: employerAccount.ID,
147+
DestinationAccountID: bankAccount.ID,
148+
Reconciled: true,
149+
Amount: decimal.NewFromFloat(1800),
150+
},
151+
}
152+
err = database.DB.Save(&salaryTransactionFebruary).Error
153+
if err != nil {
154+
suite.Assert().Fail("Resource could not be saved", err)
155+
}
156+
157+
salaryTransactionMarch := models.Transaction{
142158
TransactionCreate: models.TransactionCreate{
143159
Date: marchFifteenthTwentyTwentyTwo,
144160
BudgetID: budget.ID,
@@ -149,7 +165,7 @@ func (suite *TestSuiteEnv) TestBudgetCalculations() {
149165
Amount: decimal.NewFromFloat(2800),
150166
},
151167
}
152-
err = database.DB.Save(&salaryTransaction).Error
168+
err = database.DB.Save(&salaryTransactionMarch).Error
153169
if err != nil {
154170
suite.Assert().Fail("Resource could not be saved", err)
155171
}
@@ -216,18 +232,18 @@ func (suite *TestSuiteEnv) TestBudgetCalculations() {
216232

217233
budget = budget.WithCalculations()
218234

219-
shouldBalance := decimal.NewFromFloat(5469.38)
235+
shouldBalance := decimal.NewFromFloat(7269.38)
220236
assert.True(suite.T(), budget.Balance.Equal(shouldBalance), "Balance for budget is not correct. Should be %s, is %s", shouldBalance, budget.Balance)
221237

222238
// Verify income for used budget in March
223-
shouldIncome := decimal.NewFromFloat(2800)
239+
shouldIncome := decimal.NewFromFloat(4600)
224240
income, err := budget.Income(marchFifteenthTwentyTwentyTwo)
225241
assert.Nil(suite.T(), err)
226242
assert.True(suite.T(), income.Equal(shouldIncome), "Income is %s, should be %s", income, shouldIncome)
227243

228244
// Verify total income for used budget
229-
shouldIncomeTotal := decimal.NewFromFloat(5600)
230-
income, err = budget.TotalIncome()
245+
shouldIncomeTotal := decimal.NewFromFloat(4600)
246+
income, err = budget.TotalIncome(marchFifteenthTwentyTwentyTwo)
231247
assert.Nil(suite.T(), err)
232248
assert.True(suite.T(), income.Equal(shouldIncomeTotal), "Income is %s, should be %s", income, shouldIncomeTotal)
233249

@@ -237,7 +253,7 @@ func (suite *TestSuiteEnv) TestBudgetCalculations() {
237253
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
238254

239255
// Verify total income for empty budget
240-
income, err = emptyBudget.TotalIncome()
256+
income, err = emptyBudget.TotalIncome(marchFifteenthTwentyTwentyTwo)
241257
assert.Nil(suite.T(), err)
242258
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
243259

@@ -281,7 +297,7 @@ func (suite *TestSuiteEnv) TestTotalIncomeNoTransactions() {
281297
suite.Assert().Fail("Resource could not be saved", err)
282298
}
283299

284-
income, err := budget.TotalIncome()
300+
income, err := budget.TotalIncome(time.Date(2031, 3, 17, 0, 0, 0, 0, time.UTC))
285301
assert.Nil(suite.T(), err)
286302
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
287303
}

0 commit comments

Comments
 (0)