-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharges.go
68 lines (61 loc) · 1.44 KB
/
charges.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
62
63
64
65
66
67
68
package facturae
import (
"github.com/invopop/gobl/bill"
)
// Charges contains a list of charge instances. This is used both at a line
// and document level as per the FacturaE specification. Unfortunately, this
// implies that taxes will not be applied in the context of a global charge.
// We recommend avoiding using this in Spanish invoices.
type Charges struct {
Items []*Charge `xml:"Charge"`
}
// Charge defines the basic data for a single surcharge.
type Charge struct {
Reason string `xml:"ChargeReason"`
Rate string `xml:"ChargeRate,omitempty"`
Amount string `xml:"ChargeAmount"`
}
func newCharges(charges []*bill.Charge) *Charges {
if len(charges) == 0 {
return nil
}
m := &Charges{
Items: make([]*Charge, len(charges)),
}
for i, v := range charges {
m.Items[i] = newCharge(v)
}
return m
}
func newCharge(charge *bill.Charge) *Charge {
nc := &Charge{
Reason: charge.Reason,
Amount: charge.Amount.String(),
}
if charge.Percent != nil {
nc.Rate = charge.Percent.StringWithoutSymbol()
}
return nc
}
func newLineCharges(lds []*bill.LineCharge) *Charges {
if len(lds) == 0 {
return nil
}
nlds := &Charges{
Items: make([]*Charge, len(lds)),
}
for i, v := range lds {
nlds.Items[i] = newLineCharge(v)
}
return nlds
}
func newLineCharge(lc *bill.LineCharge) *Charge {
nc := &Charge{
Reason: lc.Reason,
Amount: lc.Amount.String(),
}
if lc.Percent != nil {
nc.Rate = lc.Percent.StringWithoutSymbol()
}
return nc
}