forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.go
378 lines (306 loc) · 9.64 KB
/
money.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package money
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math"
)
// Injection points for backward compatibility.
// If you need to keep your JSON marshal/unmarshal way, overwrite them like below.
//
// money.UnmarshalJSON = func (m *Money, b []byte) error { ... }
// money.MarshalJSON = func (m Money) ([]byte, error) { ... }
var (
// UnmarshalJSON is injection point of json.Unmarshaller for money.Money
UnmarshalJSON = defaultUnmarshalJSON
// MarshalJSON is injection point of json.Marshaller for money.Money
MarshalJSON = defaultMarshalJSON
// ErrCurrencyMismatch happens when two compared Money don't have the same currency.
ErrCurrencyMismatch = errors.New("currencies don't match")
// ErrInvalidJSONUnmarshal happens when the default money.UnmarshalJSON fails to unmarshal Money because of invalid data.
ErrInvalidJSONUnmarshal = errors.New("invalid json unmarshal")
)
func defaultUnmarshalJSON(m *Money, b []byte) error {
data := make(map[string]interface{})
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
var amount float64
if amountRaw, ok := data["amount"]; ok {
amount, ok = amountRaw.(float64)
if !ok {
return ErrInvalidJSONUnmarshal
}
}
var currency string
if currencyRaw, ok := data["currency"]; ok {
currency, ok = currencyRaw.(string)
if !ok {
return ErrInvalidJSONUnmarshal
}
}
var ref *Money
if amount == 0 && currency == "" {
ref = &Money{}
} else {
ref = New(int64(amount), currency)
}
*m = *ref
return nil
}
func defaultMarshalJSON(m Money) ([]byte, error) {
if m == (Money{}) {
m = *New(0, "")
}
buff := bytes.NewBufferString(fmt.Sprintf(`{"amount": %d, "currency": "%s"}`, m.Amount(), m.Currency().Code))
return buff.Bytes(), nil
}
// Amount is a data structure that stores the amount being used for calculations.
type Amount = int64
// Money represents monetary value information, stores
// currency and amount value.
type Money struct {
amount Amount `db:"amount"`
currency *Currency `db:"currency"`
}
// New creates and returns new instance of Money.
func New(amount int64, code string) *Money {
return &Money{
amount: amount,
currency: newCurrency(code).get(),
}
}
// NewFromFloat creates and returns new instance of Money from a float64.
// Always rounding trailing decimals down.
func NewFromFloat(amount float64, code string) *Money {
currencyDecimals := math.Pow10(newCurrency(code).get().Fraction)
return New(int64(amount*currencyDecimals), code)
}
// Currency returns the currency used by Money.
func (m *Money) Currency() *Currency {
return m.currency
}
// Amount returns a copy of the internal monetary value as an int64.
func (m *Money) Amount() int64 {
return m.amount
}
// SameCurrency check if given Money is equals by currency.
func (m *Money) SameCurrency(om *Money) bool {
return m.currency.equals(om.currency)
}
func (m *Money) assertSameCurrency(om *Money) error {
if !m.SameCurrency(om) {
return ErrCurrencyMismatch
}
return nil
}
func (m *Money) compare(om *Money) int {
switch {
case m.amount > om.amount:
return 1
case m.amount < om.amount:
return -1
}
return 0
}
// Equals checks equality between two Money types.
func (m *Money) Equals(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) == 0, nil
}
// GreaterThan checks whether the value of Money is greater than the other.
func (m *Money) GreaterThan(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) == 1, nil
}
// GreaterThanOrEqual checks whether the value of Money is greater or equal than the other.
func (m *Money) GreaterThanOrEqual(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) >= 0, nil
}
// LessThan checks whether the value of Money is less than the other.
func (m *Money) LessThan(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) == -1, nil
}
// LessThanOrEqual checks whether the value of Money is less or equal than the other.
func (m *Money) LessThanOrEqual(om *Money) (bool, error) {
if err := m.assertSameCurrency(om); err != nil {
return false, err
}
return m.compare(om) <= 0, nil
}
// IsZero returns boolean of whether the value of Money is equals to zero.
func (m *Money) IsZero() bool {
return m.amount == 0
}
// IsPositive returns boolean of whether the value of Money is positive.
func (m *Money) IsPositive() bool {
return m.amount > 0
}
// IsNegative returns boolean of whether the value of Money is negative.
func (m *Money) IsNegative() bool {
return m.amount < 0
}
// Absolute returns new Money struct from given Money using absolute monetary value.
func (m *Money) Absolute() *Money {
return &Money{amount: mutate.calc.absolute(m.amount), currency: m.currency}
}
// Negative returns new Money struct from given Money using negative monetary value.
func (m *Money) Negative() *Money {
return &Money{amount: mutate.calc.negative(m.amount), currency: m.currency}
}
// Add returns new Money struct with value representing sum of Self and Other Money.
func (m *Money) Add(ms ...*Money) (*Money, error) {
if len(ms) == 0 {
return m, nil
}
k := New(0, m.currency.Code)
for _, m2 := range ms {
if err := m.assertSameCurrency(m2); err != nil {
return nil, err
}
k.amount = mutate.calc.add(k.amount, m2.amount)
}
return &Money{amount: mutate.calc.add(m.amount, k.amount), currency: m.currency}, nil
}
// Subtract returns new Money struct with value representing difference of Self and Other Money.
func (m *Money) Subtract(ms ...*Money) (*Money, error) {
if len(ms) == 0 {
return m, nil
}
k := New(0, m.currency.Code)
for _, m2 := range ms {
if err := m.assertSameCurrency(m2); err != nil {
return nil, err
}
k.amount = mutate.calc.add(k.amount, m2.amount)
}
return &Money{amount: mutate.calc.subtract(m.amount, k.amount), currency: m.currency}, nil
}
// Multiply returns new Money struct with value representing Self multiplied value by multiplier.
func (m *Money) Multiply(muls ...int64) *Money {
if len(muls) == 0 {
panic("At least one multiplier is required to multiply")
}
k := New(1, m.currency.Code)
for _, m2 := range muls {
k.amount = mutate.calc.multiply(k.amount, m2)
}
return &Money{amount: mutate.calc.multiply(m.amount, k.amount), currency: m.currency}
}
// Round returns new Money struct with value rounded to nearest zero.
func (m *Money) Round() *Money {
return &Money{amount: mutate.calc.round(m.amount, m.currency.Fraction), currency: m.currency}
}
// Split returns slice of Money structs with split Self value in given number.
// After division leftover pennies will be distributed round-robin amongst the parties.
// This means that parties listed first will likely receive more pennies than ones that are listed later.
func (m *Money) Split(n int) ([]*Money, error) {
if n <= 0 {
return nil, errors.New("split must be higher than zero")
}
a := mutate.calc.divide(m.amount, int64(n))
ms := make([]*Money, n)
for i := 0; i < n; i++ {
ms[i] = &Money{amount: a, currency: m.currency}
}
r := mutate.calc.modulus(m.amount, int64(n))
l := mutate.calc.absolute(r)
// Add leftovers to the first parties.
v := int64(1)
if m.amount < 0 {
v = -1
}
for p := 0; l != 0; p++ {
ms[p].amount = mutate.calc.add(ms[p].amount, v)
l--
}
return ms, nil
}
// Allocate returns slice of Money structs with split Self value in given ratios.
// It lets split money by given ratios without losing pennies and as Split operations distributes
// leftover pennies amongst the parties with round-robin principle.
func (m *Money) Allocate(rs ...int) ([]*Money, error) {
if len(rs) == 0 {
return nil, errors.New("no ratios specified")
}
// Calculate sum of ratios.
var sum int64
for _, r := range rs {
if r < 0 {
return nil, errors.New("negative ratios not allowed")
}
if int64(r) > (math.MaxInt64 - sum) {
return nil, errors.New("sum of given ratios exceeds max int")
}
sum += int64(r)
}
var total int64
ms := make([]*Money, 0, len(rs))
for _, r := range rs {
party := &Money{
amount: mutate.calc.allocate(m.amount, int64(r), sum),
currency: m.currency,
}
ms = append(ms, party)
total += party.amount
}
// if the sum of all ratios is zero, then we just returns zeros and don't do anything
// with the leftover
if sum == 0 {
return ms, nil
}
// Calculate leftover value and divide to first parties.
lo := m.amount - total
sub := int64(1)
if lo < 0 {
sub = -sub
}
for p := 0; lo != 0; p++ {
ms[p].amount = mutate.calc.add(ms[p].amount, sub)
lo -= sub
}
return ms, nil
}
// Display lets represent Money struct as string in given Currency value.
func (m *Money) Display() string {
c := m.currency.get()
return c.Formatter().Format(m.amount)
}
// AsMajorUnits lets represent Money struct as subunits (float64) in given Currency value
func (m *Money) AsMajorUnits() float64 {
c := m.currency.get()
return c.Formatter().ToMajorUnits(m.amount)
}
// UnmarshalJSON is implementation of json.Unmarshaller
func (m *Money) UnmarshalJSON(b []byte) error {
return UnmarshalJSON(m, b)
}
// MarshalJSON is implementation of json.Marshaller
func (m Money) MarshalJSON() ([]byte, error) {
return MarshalJSON(m)
}
// Compare function compares two money of the same type
//
// if m.amount > om.amount returns (1, nil)
// if m.amount == om.amount returns (0, nil
// if m.amount < om.amount returns (-1, nil)
//
// If compare moneys from distinct currency, return (m.amount, ErrCurrencyMismatch)
func (m *Money) Compare(om *Money) (int, error) {
if err := m.assertSameCurrency(om); err != nil {
return int(m.amount), err
}
return m.compare(om), nil
}