-
Notifications
You must be signed in to change notification settings - Fork 4
/
payment.go
61 lines (54 loc) · 1.75 KB
/
payment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package bill
import (
"context"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/pay"
"github.com/invopop/gobl/tax"
"github.com/invopop/validation"
)
// Payment contains details as to how the invoice should be paid.
type Payment struct {
// The party responsible for receiving payment of the invoice, if not the supplier.
Payee *org.Party `json:"payee,omitempty" jsonschema:"title=Payee"`
// Payment terms or conditions.
Terms *pay.Terms `json:"terms,omitempty" jsonschema:"title=Terms"`
// Any amounts that have been paid in advance and should be deducted from the amount due.
Advances []*pay.Advance `json:"advances,omitempty" jsonschema:"title=Advances"`
// Details on how payment should be made.
Instructions *pay.Instructions `json:"instructions,omitempty" jsonschema:"title=Instructions"`
}
// ValidateWithContext checks to make sure the payment data looks good
func (p *Payment) ValidateWithContext(ctx context.Context) error {
return tax.ValidateStructWithRegime(ctx, p,
validation.Field(&p.Payee),
validation.Field(&p.Terms),
validation.Field(&p.Advances),
validation.Field(&p.Instructions),
)
}
// ResetAdvances clears the advances list.
func (p *Payment) ResetAdvances() {
if p == nil {
return
}
p.Advances = make([]*pay.Advance, 0)
}
func (p *Payment) calculateAdvances(zero num.Amount, totalWithTax num.Amount) {
for _, a := range p.Advances {
a.CalculateFrom(totalWithTax)
a.Amount = a.Amount.MatchPrecision(zero)
}
}
func (p *Payment) totalAdvance(zero num.Amount) *num.Amount {
if p == nil || len(p.Advances) == 0 {
return nil
}
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
}