Skip to content

Commit 128053d

Browse files
committed
fix!: spent amounts are now negative
With this change, all spent amounts are negative as they should have been from the beginning. This will make the calculation allocation + spend = balance correct. BREAKING CHANGE: Sign of spent amounts is flipped to be negative. If you use the official frontend, upgrade the frontend to 1.9.0 at the same time as upgrading the backend to 2.0.0.
1 parent 3bfdbbb commit 128053d

File tree

7 files changed

+23
-23
lines changed

7 files changed

+23
-23
lines changed

pkg/controllers/budget_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (suite *TestSuiteStandard) TestBudgetMonth() {
287287
{
288288
Name: "Utilities",
289289
Month: types.NewMonth(2022, 1),
290-
Spent: decimal.NewFromFloat(10),
290+
Spent: decimal.NewFromFloat(-10),
291291
Balance: decimal.NewFromFloat(10.99),
292292
Allocation: decimal.NewFromFloat(20.99),
293293
},
@@ -306,7 +306,7 @@ func (suite *TestSuiteStandard) TestBudgetMonth() {
306306
Name: "Utilities",
307307
Month: types.NewMonth(2022, 2),
308308
Balance: decimal.NewFromFloat(53.11),
309-
Spent: decimal.NewFromFloat(5),
309+
Spent: decimal.NewFromFloat(-5),
310310
Allocation: decimal.NewFromFloat(47.12),
311311
},
312312
},
@@ -324,7 +324,7 @@ func (suite *TestSuiteStandard) TestBudgetMonth() {
324324
Name: "Utilities",
325325
Month: types.NewMonth(2022, 3),
326326
Balance: decimal.NewFromFloat(69.28),
327-
Spent: decimal.NewFromFloat(15),
327+
Spent: decimal.NewFromFloat(-15),
328328
Allocation: decimal.NewFromFloat(31.17),
329329
},
330330
},

pkg/controllers/envelope_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
290290
models.EnvelopeMonth{
291291
Name: "Utilities",
292292
Month: types.NewMonth(2022, 1),
293-
Spent: decimal.NewFromFloat(10),
293+
Spent: decimal.NewFromFloat(-10),
294294
Balance: decimal.NewFromFloat(10.99),
295295
Allocation: decimal.NewFromFloat(20.99),
296296
},
@@ -301,7 +301,7 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
301301
Name: "Utilities",
302302
Month: types.NewMonth(2022, 2),
303303
Balance: decimal.NewFromFloat(53.11),
304-
Spent: decimal.NewFromFloat(5),
304+
Spent: decimal.NewFromFloat(-5),
305305
Allocation: decimal.NewFromFloat(47.12),
306306
},
307307
},
@@ -311,7 +311,7 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
311311
Name: "Utilities",
312312
Month: types.NewMonth(2022, 3),
313313
Balance: decimal.NewFromFloat(69.28),
314-
Spent: decimal.NewFromFloat(15),
314+
Spent: decimal.NewFromFloat(-15),
315315
Allocation: decimal.NewFromFloat(31.17),
316316
},
317317
},
@@ -321,7 +321,7 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
321321
models.EnvelopeMonth{
322322
Name: "Utilities",
323323
Month: types.NewMonth(1998, 10),
324-
Spent: decimal.NewFromFloat(0),
324+
Spent: decimal.NewFromFloat(-0),
325325
Balance: decimal.NewFromFloat(0),
326326
Allocation: decimal.NewFromFloat(0),
327327
},

pkg/controllers/month.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (co Controller) SetAllocations(c *gin.Context) {
194194
// If the mode is the spend of last month, calculate and set it
195195
amount := allocation.Amount
196196
if data.Mode == AllocateLastMonthSpend {
197-
amount = models.Envelope{DefaultModel: models.DefaultModel{ID: allocation.EnvelopeID}}.Spent(co.DB, pastMonth)
197+
amount = models.Envelope{DefaultModel: models.DefaultModel{ID: allocation.EnvelopeID}}.Spent(co.DB, pastMonth).Neg()
198198
}
199199

200200
if !queryWithRetry(c, co.DB.Create(&models.Allocation{

pkg/importer/parser/ynab4/parse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ func TestParse(t *testing.T) {
133133
budgeted float32
134134
income float32
135135
}{
136-
{types.NewMonth(2022, 10), 46.17, -110, 185, 75, 0},
137-
{types.NewMonth(2022, 11), 906.17, -140, 170, 140, 1000},
138-
{types.NewMonth(2022, 12), 866.17, -115, 110, 115, 95},
136+
{types.NewMonth(2022, 10), 46.17, -110, -185, 75, 0},
137+
{types.NewMonth(2022, 11), 906.17, -140, -170, 140, 1000},
138+
{types.NewMonth(2022, 12), 866.17, -115, -110, 115, 95},
139139
{types.NewMonth(2023, 1), 556.17, -5, 0, 0, 0},
140140
{types.NewMonth(2023, 2), 436.17, 115, 0, 0, 0},
141141
}

pkg/models/budget_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -389,21 +389,21 @@ func (suite *TestSuiteStandard) TestMonth() {
389389
Month: types.NewMonth(2022, 1),
390390
Income: decimal.NewFromFloat(0),
391391
Balance: decimal.NewFromFloat(10.99),
392-
Spent: decimal.NewFromFloat(10),
392+
Spent: decimal.NewFromFloat(-10),
393393
Allocation: decimal.NewFromFloat(20.99),
394394
Available: decimal.NewFromFloat(-20.99),
395395
Categories: []models.CategoryEnvelopes{
396396
{
397397
Name: category.Name,
398398
ID: category.ID,
399399
Balance: decimal.NewFromFloat(10.99),
400-
Spent: decimal.NewFromFloat(10),
400+
Spent: decimal.NewFromFloat(-10),
401401
Allocation: decimal.NewFromFloat(20.99),
402402
Envelopes: []models.EnvelopeMonth{
403403
{
404404
Name: "Utilities",
405405
Month: types.NewMonth(2022, 1),
406-
Spent: decimal.NewFromFloat(10),
406+
Spent: decimal.NewFromFloat(-10),
407407
Balance: decimal.NewFromFloat(10.99),
408408
Allocation: decimal.NewFromFloat(20.99),
409409
Links: models.EnvelopeMonthLinks{
@@ -421,22 +421,22 @@ func (suite *TestSuiteStandard) TestMonth() {
421421
Month: types.NewMonth(2022, 2),
422422
Income: decimal.NewFromFloat(0),
423423
Balance: decimal.NewFromFloat(53.11),
424-
Spent: decimal.NewFromFloat(5),
424+
Spent: decimal.NewFromFloat(-5),
425425
Allocation: decimal.NewFromFloat(47.12),
426426
Available: decimal.NewFromFloat(-68.11),
427427
Categories: []models.CategoryEnvelopes{
428428
{
429429
Name: category.Name,
430430
ID: category.ID,
431431
Balance: decimal.NewFromFloat(53.11),
432-
Spent: decimal.NewFromFloat(5),
432+
Spent: decimal.NewFromFloat(-5),
433433
Allocation: decimal.NewFromFloat(47.12),
434434
Envelopes: []models.EnvelopeMonth{
435435
{
436436
Name: "Utilities",
437437
Month: types.NewMonth(2022, 2),
438438
Balance: decimal.NewFromFloat(53.11),
439-
Spent: decimal.NewFromFloat(5),
439+
Spent: decimal.NewFromFloat(-5),
440440
Allocation: decimal.NewFromFloat(47.12),
441441
Links: models.EnvelopeMonthLinks{
442442
Allocation: fmt.Sprintf("http://example.com/v1/allocations/%s", allocationFebruary.ID),
@@ -453,22 +453,22 @@ func (suite *TestSuiteStandard) TestMonth() {
453453
Month: types.NewMonth(2022, 3),
454454
Income: decimal.NewFromFloat(1500),
455455
Balance: decimal.NewFromFloat(69.28),
456-
Spent: decimal.NewFromFloat(15),
456+
Spent: decimal.NewFromFloat(-15),
457457
Allocation: decimal.NewFromFloat(31.17),
458458
Available: decimal.NewFromFloat(1400.72),
459459
Categories: []models.CategoryEnvelopes{
460460
{
461461
Name: category.Name,
462462
ID: category.ID,
463463
Balance: decimal.NewFromFloat(69.28),
464-
Spent: decimal.NewFromFloat(15),
464+
Spent: decimal.NewFromFloat(-15),
465465
Allocation: decimal.NewFromFloat(31.17),
466466
Envelopes: []models.EnvelopeMonth{
467467
{
468468
Name: "Utilities",
469469
Month: types.NewMonth(2022, 3),
470470
Balance: decimal.NewFromFloat(69.28),
471-
Spent: decimal.NewFromFloat(15),
471+
Spent: decimal.NewFromFloat(-15),
472472
Allocation: decimal.NewFromFloat(31.17),
473473
Links: models.EnvelopeMonthLinks{
474474
Allocation: fmt.Sprintf("http://example.com/v1/allocations/%s", allocationMarch.ID),

pkg/models/envelope.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (e Envelope) Spent(db *gorm.DB, month types.Month) decimal.Decimal {
6969
}
7070
}
7171

72-
return outgoingSum.Sub(incomingSum)
72+
return outgoingSum.Neg().Add(incomingSum)
7373
}
7474

7575
type AggregatedTransaction struct {

pkg/models/envelope_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (suite *TestSuiteStandard) TestEnvelopeMonthSum() {
8383
TransactionCreate: models.TransactionCreate{
8484
BudgetID: budget.ID,
8585
EnvelopeID: &envelope.ID,
86-
Amount: spent.Neg(),
86+
Amount: spent,
8787
SourceAccountID: externalAccount.ID,
8888
DestinationAccountID: internalAccount.ID,
8989
Date: time.Time(january.AddDate(0, 1)),
@@ -96,7 +96,7 @@ func (suite *TestSuiteStandard) TestEnvelopeMonthSum() {
9696

9797
envelopeMonth, _, err := envelope.Month(suite.db, january)
9898
assert.Nil(suite.T(), err)
99-
assert.True(suite.T(), envelopeMonth.Spent.Equal(spent), "Month calculation for 2022-01 is wrong: should be %v, but is %v", spent, envelopeMonth.Spent)
99+
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)
100100

101101
envelopeMonth, _, err = envelope.Month(suite.db, january.AddDate(0, 1))
102102
assert.Nil(suite.T(), err)

0 commit comments

Comments
 (0)