Skip to content

Commit 03cb7fe

Browse files
committed
fix: spend is positive when more was spent then taken in
1 parent 3a02562 commit 03cb7fe

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

pkg/controllers/budget.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func SetAllocationsMonth(c *gin.Context) {
575575
// If the mode is the spend of last month, calculate and set it
576576
amount := allocation.Amount
577577
if data.Mode == AllocateLastMonthSpend {
578-
amount = models.Envelope{Model: models.Model{ID: allocation.EnvelopeID}}.Spent(pastMonth).Neg()
578+
amount = models.Envelope{Model: models.Model{ID: allocation.EnvelopeID}}.Spent(pastMonth)
579579
}
580580

581581
err = database.DB.Create(&models.Allocation{

pkg/controllers/budget_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (suite *TestSuiteEnv) TestBudgetMonth() {
288288
{
289289
Name: "Utilities",
290290
Month: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
291-
Spent: decimal.NewFromFloat(-10),
291+
Spent: decimal.NewFromFloat(10),
292292
Balance: decimal.NewFromFloat(10.99),
293293
Allocation: decimal.NewFromFloat(20.99),
294294
},
@@ -306,8 +306,8 @@ func (suite *TestSuiteEnv) TestBudgetMonth() {
306306
{
307307
Name: "Utilities",
308308
Month: time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC),
309-
Spent: decimal.NewFromFloat(-5),
310309
Balance: decimal.NewFromFloat(53.11),
310+
Spent: decimal.NewFromFloat(5),
311311
Allocation: decimal.NewFromFloat(47.12),
312312
},
313313
},
@@ -324,8 +324,8 @@ func (suite *TestSuiteEnv) TestBudgetMonth() {
324324
{
325325
Name: "Utilities",
326326
Month: time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC),
327-
Spent: decimal.NewFromFloat(-15),
328327
Balance: decimal.NewFromFloat(69.28),
328+
Spent: decimal.NewFromFloat(15),
329329
Allocation: decimal.NewFromFloat(31.17),
330330
},
331331
},

pkg/controllers/envelope_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (suite *TestSuiteEnv) TestEnvelopeMonth() {
274274
models.EnvelopeMonth{
275275
Name: "Utilities",
276276
Month: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
277-
Spent: decimal.NewFromFloat(-10),
277+
Spent: decimal.NewFromFloat(10),
278278
Balance: decimal.NewFromFloat(10.99),
279279
Allocation: decimal.NewFromFloat(20.99),
280280
},
@@ -284,8 +284,8 @@ func (suite *TestSuiteEnv) TestEnvelopeMonth() {
284284
models.EnvelopeMonth{
285285
Name: "Utilities",
286286
Month: time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC),
287-
Spent: decimal.NewFromFloat(-5),
288287
Balance: decimal.NewFromFloat(53.11),
288+
Spent: decimal.NewFromFloat(5),
289289
Allocation: decimal.NewFromFloat(47.12),
290290
},
291291
},
@@ -294,8 +294,8 @@ func (suite *TestSuiteEnv) TestEnvelopeMonth() {
294294
models.EnvelopeMonth{
295295
Name: "Utilities",
296296
Month: time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC),
297-
Spent: decimal.NewFromFloat(-15),
298297
Balance: decimal.NewFromFloat(69.28),
298+
Spent: decimal.NewFromFloat(15),
299299
Allocation: decimal.NewFromFloat(31.17),
300300
},
301301
},

pkg/models/envelope.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (e Envelope) Spent(t time.Time) decimal.Decimal {
6262
}
6363
}
6464

65-
return incomingSum.Sub(outgoingSum)
65+
return outgoingSum.Sub(incomingSum)
6666
}
6767

6868
// Balance calculates the balance of an Envelope in a specific month

pkg/models/envelope_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ func (suite *TestSuiteEnv) TestEnvelopeMonthSum() {
9595

9696
envelopeMonth, err := envelope.Month(january)
9797
assert.Nil(suite.T(), err)
98-
assert.True(suite.T(), envelopeMonth.Spent.Equal(spent.Neg()), "Month calculation for 2022-01 is wrong: should be %v, but is %v", spent.Neg(), envelopeMonth.Spent)
98+
assert.True(suite.T(), envelopeMonth.Spent.Equal(spent), "Month calculation for 2022-01 is wrong: should be %v, but is %v", spent, envelopeMonth.Spent)
9999

100100
envelopeMonth, err = envelope.Month(january.AddDate(0, 1, 0))
101101
assert.Nil(suite.T(), err)
102-
assert.True(suite.T(), envelopeMonth.Spent.Equal(spent.Neg()), "Month calculation for 2022-02 is wrong: should be %v, but is %v", spent, envelopeMonth.Spent)
102+
assert.True(suite.T(), envelopeMonth.Spent.Equal(spent), "Month calculation for 2022-02 is wrong: should be %v, but is %v", spent, envelopeMonth.Spent)
103103

104104
err = database.DB.Delete(&transaction).Error
105105
if err != nil {
@@ -216,6 +216,18 @@ func (suite *TestSuiteEnv) TestEnvelopeMonthBalance() {
216216
suite.Assert().Fail("Resource could not be saved", err)
217217
}
218218

219+
// Used to test the Envelope.Balance method without any transactions
220+
envelope2 := &models.Envelope{
221+
EnvelopeCreate: models.EnvelopeCreate{
222+
Name: "Testing envelope without any transactions",
223+
CategoryID: category.ID,
224+
},
225+
}
226+
err = database.DB.Create(&envelope2).Error
227+
if err != nil {
228+
suite.Assert().Fail("Resource could not be saved", err)
229+
}
230+
219231
january := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
220232

221233
allocationJan := &models.Allocation{
@@ -281,4 +293,9 @@ func (suite *TestSuiteEnv) TestEnvelopeMonthBalance() {
281293
envelopeMonth, err = envelope.Month(january.AddDate(0, 1, 0))
282294
assert.Nil(suite.T(), err)
283295
assert.True(suite.T(), envelopeMonth.Balance.Equal(shouldBalance), "Balance calculation for 2022-02 is wrong: should be %v, but is %v", shouldBalance, envelopeMonth.Balance)
296+
297+
shouldBalance = decimal.NewFromFloat(0)
298+
envelopeMonth, err = envelope2.Month(january.AddDate(0, 1, 0))
299+
assert.Nil(suite.T(), err)
300+
assert.True(suite.T(), envelopeMonth.Balance.Equal(shouldBalance), "Balance calculation for 2022-02 is wrong: should be %v, but is %v", shouldBalance, envelopeMonth.Balance)
284301
}

0 commit comments

Comments
 (0)