-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscounts.go
70 lines (63 loc) · 1.49 KB
/
discounts.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
69
70
package facturae
import (
"github.com/invopop/gobl/bill"
)
// Discounts list discounts applied on a line or document level.
type Discounts struct {
Items []*Discount `xml:"Discount"`
}
// Discount is used in general and line discounts. Unlike in GOBL,
// FacturaE does not differentiate between the two, which could potentially
// lead to confusion for handling taxes.
type Discount struct {
Reason string `xml:"DiscountReason"`
Rate string `xml:"DiscountRate,omitempty"`
Amount string `xml:"DiscountAmount"`
}
func newDiscounts(discounts []*bill.Discount) *Discounts {
if len(discounts) == 0 {
return nil
}
m := &Discounts{
Items: make([]*Discount, len(discounts)),
}
for i, v := range discounts {
m.Items[i] = newDiscount(v)
}
return m
}
func newDiscount(discount *bill.Discount) *Discount {
nd := &Discount{
Reason: discount.Reason,
Amount: discount.Amount.String(),
}
if discount.Percent != nil {
nd.Rate = discount.Percent.StringWithoutSymbol()
}
return nd
}
func newLineDiscounts(lds []*bill.LineDiscount) *Discounts {
if len(lds) == 0 {
return nil
}
nlds := &Discounts{
Items: make([]*Discount, len(lds)),
}
for i, v := range lds {
nlds.Items[i] = newLineDiscount(v)
}
return nlds
}
func newLineDiscount(ld *bill.LineDiscount) *Discount {
if ld.Percent != nil {
return &Discount{
Reason: ld.Reason,
Rate: ld.Percent.StringWithoutSymbol(),
Amount: ld.Amount.String(),
}
}
return &Discount{
Reason: ld.Reason,
Amount: ld.Amount.String(),
}
}