-
Notifications
You must be signed in to change notification settings - Fork 0
/
price.go
192 lines (160 loc) · 5.11 KB
/
price.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package mongo
import (
"encoding/json"
"fmt"
"golang.org/x/exp/constraints"
)
// Price is a structure that holds a price and gives information about the
// amount of tax applied to that price.
type Price struct {
gross Money // The gross.
taxes taxes // The amount of tax subtracted from the gross to produce the net.
}
// PriceFromSubunits constructs a new price object from an integer.
// currIsoCode is an ISO 4217 currency code.
// gross is monetary value in subunits.
// roundFunc is a function to be used for division operations.
func PriceFromSubunits[T constraints.Integer](currIsoCode string, gross T, f roundFunc) (Price, error) {
var price Price
var err error
price.gross, err = MoneyFromSubunits(currIsoCode, gross, f)
if err != nil {
return Price{}, err
}
price.taxes = taxes{
total: price.gross.Clone(0),
detail: make(map[string]Money, 0),
}
return price, nil
}
// PriceFromFloat constructs a new price object from a floating point number.
// currIsoCode is an ISO 4217 currency code.
// gross is monetary value expressed as a float.
// roundFunc is a function to be used for division operations.
func PriceFromFloat[T constraints.Float](currIsoCode string, gross T, f roundFunc) (Price, error) {
var price Price
var err error
price.gross, err = MoneyFromFloat(currIsoCode, gross, f)
if err != nil {
return Price{}, err
}
price.taxes = taxes{
total: price.gross.Clone(0),
detail: make(map[string]Money, 0),
}
return price, nil
}
// PriceFromString constructs a new price object from a string. Everything not
// contained within a number is stripped out before parsing.
// currIsoCode is an ISO 4217 currency code.
// gross is monetary value expressed as a string.
// roundFunc is a function to be used for division operations.
func PriceFromString(currIsoCode string, gross string, f roundFunc) (Price, error) {
var price Price
var err error
price.gross, err = MoneyFromString(currIsoCode, gross, f)
if err != nil {
return Price{}, err
}
price.taxes = taxes{
total: price.gross.Clone(0),
detail: make(map[string]Money, 0),
}
return price, nil
}
// PriceGBP is a helper function.
// gross is the gross monetary value in subunits.
// vat is a tax percentage that's included in the gross value.
func PriceGBP[T constraints.Integer](gross T, vat float64) (Price, error) {
price, err := PriceFromSubunits("GBP", gross, nil)
if err != nil {
return Price{}, err
}
price.IncludeTaxPercent(vat, "VAT")
return price, err
}
// AddTax adds a tax to the price using a money value.
// This will literally add the money amount to the gross price.
func (p *Price) AddTax(m Money, desc string) {
assertSameMoneyCurrency(p.gross, m)
t := p.gross.Clone(m.value)
p.taxes = p.taxes.add(desc, t)
p.gross = p.gross.Add(t)
}
// AddTaxPercentage adds a tax to the price using a percentage.
// This will literally add a percentage to the gross price.
func (p *Price) AddTaxPercent(percent float64, desc string) {
v := (float64(p.gross.value) / 100) * percent
t := p.gross.Clone(p.gross.round(v))
p.taxes = p.taxes.add(desc, t)
p.gross = p.gross.Add(t)
}
// IncludeTax adds a tax to the price using a money value.
// This implies this tax is already included in the gross price.
func (p *Price) IncludeTax(m Money, desc string) {
assertSameMoneyCurrency(p.gross, m)
t := p.gross.Clone(m.value)
p.taxes = p.taxes.add(desc, t)
}
// IncludeTaxPercent adds a tax to the price using a percentage.
// This implies this tax is already included in the gross price.
func (p *Price) IncludeTaxPercent(percent float64, desc string) {
t := p.Net().Sub(p.Net().Div(1 + (percent / 100)))
p.taxes = p.taxes.add(desc, t)
}
// IsoCode returns the ISO 4217 currency code.
func (p Price) IsoCode() string {
return p.gross.format.code
}
// Gross returns the gross monetary value of the price.
func (p Price) Gross() Money {
return p.gross
}
// Net returns the net monetary value of the price which is equal to the gross
// minus tax.
func (p Price) Net() Money {
return p.gross.Sub(p.taxes.total)
}
// Tax returns the total amount of tax subtracted from the gross to produce the net.
func (p Price) Tax() Money {
return p.taxes.total
}
// Add is an arithmetic operator.
func (p Price) Add(v Price) Price {
p.gross = p.gross.Add(v.gross)
for k, v := range v.taxes.detail {
p.taxes = p.taxes.add(k, v)
}
return p
}
// Mul is an arithmetic operator.
func (p Price) Mul(n int64) Price {
p.gross = p.gross.Mul(n)
p.taxes = p.taxes.mul(n)
return p
}
// MarshalJSON is an implementation of json.Marshaller.
func (p Price) MarshalJSON() ([]byte, error) {
tax, err := json.Marshal(p.taxes)
if err != nil {
return []byte{}, err
}
json := fmt.Sprintf(
`{"currency": "%s", "gross": "%s", "net": "%s", "tax": %s}`,
p.IsoCode(),
p.Gross(),
p.Net(),
tax,
)
return []byte(json), nil
}
// String is an implementation of fmt.Stringer and returns the string
// formatted representation of the price value.
func (p Price) String() string {
return p.gross.String()
}
// StringNoSymbol returns the string formatted representation of the price
// value without a currency symbol.
func (p Price) StringNoSymbol() string {
return p.gross.StringNoSymbol()
}