Skip to content

Commit a476ab8

Browse files
committed
refactor: delete unused method Overspend
1 parent cfc0d21 commit a476ab8

File tree

2 files changed

+0
-77
lines changed

2 files changed

+0
-77
lines changed

pkg/models/budget.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -147,58 +147,6 @@ func (b Budget) TotalBudgeted(db *gorm.DB, month types.Month) (decimal.Decimal,
147147
return budgeted.Decimal, nil
148148
}
149149

150-
// Overspent calculates overspend for a specific month.
151-
func (b Budget) Overspent(db *gorm.DB, month types.Month) (decimal.Decimal, error) {
152-
var envelopes []Envelope
153-
err := db.
154-
Joins("Category", db.Where(&Category{CategoryCreate: CategoryCreate{BudgetID: b.ID}})).
155-
Find(&envelopes).
156-
Error
157-
if err != nil {
158-
return decimal.Zero, err
159-
}
160-
161-
var overspent decimal.Decimal
162-
for _, envelope := range envelopes {
163-
balance, err := envelope.Balance(db, month)
164-
if err != nil {
165-
return decimal.Zero, err
166-
}
167-
168-
if balance.IsNegative() {
169-
overspent = overspent.Add(balance.Neg())
170-
}
171-
}
172-
173-
var noEnvelopeSum decimal.NullDecimal
174-
err = db.
175-
Select("SUM(amount)").
176-
Joins("JOIN accounts source_account ON transactions.source_account_id = source_account.id AND source_account.deleted_at IS NULL").
177-
Joins("JOIN accounts destination_account ON transactions.destination_account_id = destination_account.id AND destination_account.deleted_at IS NULL").
178-
Where("source_account.external = 0").
179-
Where("destination_account.external = 1").
180-
Where("transactions.envelope_id IS NULL").
181-
// Add a month to also factor in all allocations in the requested month
182-
Where("transactions.date < date(?) ", month.AddDate(0, 1)).
183-
Where(&Transaction{
184-
TransactionCreate: TransactionCreate{
185-
BudgetID: b.ID,
186-
},
187-
}).
188-
Table("transactions").
189-
Find(&noEnvelopeSum).
190-
Error
191-
if err != nil {
192-
return decimal.Zero, err
193-
}
194-
195-
if noEnvelopeSum.Valid {
196-
overspent = overspent.Add(noEnvelopeSum.Decimal)
197-
}
198-
199-
return overspent, nil
200-
}
201-
202150
type CategoryEnvelopes struct {
203151
ID uuid.UUID `json:"id" example:"dafd9a74-6aeb-46b9-9f5a-cfca624fea85"` // ID of the category
204152
Name string `json:"name" example:"Rainy Day Funds" default:""` // Name of the category

pkg/models/budget_test.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,6 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
281281
budgeted, err = emptyBudget.Budgeted(suite.db, marchTwentyTwentyTwo)
282282
assert.Nil(suite.T(), err)
283283
assert.True(suite.T(), budgeted.IsZero(), "Budgeted is %s, should be 0", budgeted)
284-
285-
// Verify overspent calculation for month without spend
286-
overspent, err := budget.Overspent(suite.db, types.NewMonth(2022, 1))
287-
assert.Nil(suite.T(), err)
288-
assert.True(suite.T(), overspent.IsZero(), "Overspent is %s, should be 0", overspent)
289-
290-
// Verify overspent calculation for month with spend
291-
overspent, err = budget.Overspent(suite.db, marchTwentyTwentyTwo)
292-
assert.Nil(suite.T(), err)
293-
assert.True(suite.T(), overspent.Equal(decimal.NewFromFloat(63.62)), "Overspent is %s, should be 63.62", overspent)
294284
}
295285

296286
func (suite *TestSuiteStandard) TestMonthIncomeNoTransactions() {
@@ -362,21 +352,6 @@ func (suite *TestSuiteStandard) TestBudgetTotalBudgetedDBFail() {
362352
suite.Assert().Equal("sql: database is closed", err.Error())
363353
}
364354

365-
func (suite *TestSuiteStandard) TestBudgetOverspentDBFail() {
366-
budget := models.Budget{}
367-
368-
err := suite.db.Save(&budget).Error
369-
if err != nil {
370-
suite.Assert().Fail("Resource could not be saved", err)
371-
}
372-
373-
suite.CloseDB()
374-
375-
_, err = budget.Overspent(suite.db, types.NewMonth(2023, 9))
376-
suite.Assert().NotNil(err)
377-
suite.Assert().Equal("sql: database is closed", err.Error())
378-
}
379-
380355
// TestBudgetMonth verifies that the monthly calculations are correct.
381356
func (suite *TestSuiteStandard) TestMonth() {
382357
budget := suite.createTestBudget(models.BudgetCreate{})

0 commit comments

Comments
 (0)