Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Fixed expense contributions being negative.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcourant committed May 28, 2021
1 parent a09b848 commit e2f2f79
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pkg/models/spending.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/getsentry/sentry-go"
"github.com/monetrapp/rest-api/pkg/util"
"github.com/pkg/errors"
"math"
"strconv"
"time"
)
Expand Down Expand Up @@ -92,18 +93,20 @@ func (e *Spending) CalculateNextContribution(
nextDueDate = util.MidnightInLocal(e.NextRecurrence, timezone)
}

needed := int64(math.Max(float64(e.TargetAmount-progressAmount), 0))

// If the next time we would contribute to this expense is after the next time the expense is due, then the expense
// has fallen behind. Mark it as behind and set the contribution to be the difference.
if nextContributionDate.After(nextDueDate) {
e.NextContributionAmount = e.TargetAmount - progressAmount
e.NextContributionAmount = needed
e.IsBehind = progressAmount < e.TargetAmount
return nil
} else if nextContributionDate.Equal(nextDueDate) {
// If the next time we would contribute is the same day it's due, this is okay. The user could change the due
// date if they want a bit of a buffer and we would plan it differently. But we don't want to consider this
// "behind".
e.IsBehind = false
e.NextContributionAmount = e.TargetAmount - progressAmount
e.NextContributionAmount = needed
return nil
} else if progressAmount >= e.TargetAmount {
e.IsBehind = false
Expand All @@ -114,7 +117,7 @@ func (e *Spending) CalculateNextContribution(
nextContributionRule.DTStart(nextContributionDate)
numberOfContributions := len(nextContributionRule.Between(nowInTimezone, nextDueDate, false))

totalNeeded := e.TargetAmount - progressAmount
totalNeeded := needed
perContribution := totalNeeded / int64(numberOfContributions)

e.NextContributionAmount = perContribution
Expand Down

0 comments on commit e2f2f79

Please sign in to comment.