Skip to content

Commit

Permalink
Merge pull request #212 from invopop/advance-amount-fix
Browse files Browse the repository at this point in the history
Advance amount fix
  • Loading branch information
samlown authored Oct 9, 2023
2 parents e194d19 + 6be384b commit a37a65c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions bill/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (p *Payment) totalAdvance(zero num.Amount) *num.Amount {
}
sum := zero
for _, a := range p.Advances {
sum = sum.MatchPrecision(a.Amount)
sum = sum.Add(a.Amount)
a.Amount = a.Amount.Rescale(zero.Exp())
}
Expand Down
18 changes: 18 additions & 0 deletions bill/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ func TestPaymentCalculations(t *testing.T) {
p.calculateAdvances(zero, total)
sum := p.totalAdvance(zero)
assert.Equal(t, "30.00", sum.String())

t.Run("maintains precision", func(t *testing.T) {
zero := num.MakeAmount(0, 2)
total := num.MakeAmount(20845, 3)
p := &Payment{
Advances: []*pay.Advance{
{
Description: "Paid in advance",
Percent: num.NewPercentage(100, 2),
},
},
}
p.calculateAdvances(zero, total)
a := p.totalAdvance(zero)

assert.Equal(t, "20.85", p.Advances[0].Amount.String())
assert.Equal(t, "20.845", a.String())
})
}

0 comments on commit a37a65c

Please sign in to comment.