From 9816635f02a6a1b93753edc9758e5318d54f5a7e Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Mon, 9 Oct 2023 16:26:15 +0000 Subject: [PATCH 1/2] Fixing invoice advance amount scaling --- bill/payment.go | 2 +- bill/payment_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/bill/payment.go b/bill/payment.go index abbbd771..dec3d473 100644 --- a/bill/payment.go +++ b/bill/payment.go @@ -53,8 +53,8 @@ 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()) } return &sum } diff --git a/bill/payment_test.go b/bill/payment_test.go index 61d340cc..a9b27543 100644 --- a/bill/payment_test.go +++ b/bill/payment_test.go @@ -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.845", a.String()) + + }) } From 6be384b4039631b6bf9187ad3ad4ece750d60d9f Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Mon, 9 Oct 2023 16:31:18 +0000 Subject: [PATCH 2/2] Making sure advance amounts are scaled down for zero --- bill/payment.go | 1 + bill/payment_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bill/payment.go b/bill/payment.go index dec3d473..e643b91d 100644 --- a/bill/payment.go +++ b/bill/payment.go @@ -55,6 +55,7 @@ func (p *Payment) totalAdvance(zero num.Amount) *num.Amount { for _, a := range p.Advances { sum = sum.MatchPrecision(a.Amount) sum = sum.Add(a.Amount) + a.Amount = a.Amount.Rescale(zero.Exp()) } return &sum } diff --git a/bill/payment_test.go b/bill/payment_test.go index a9b27543..7e170e93 100644 --- a/bill/payment_test.go +++ b/bill/payment_test.go @@ -66,7 +66,7 @@ func TestPaymentCalculations(t *testing.T) { 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()) - }) }