-
Notifications
You must be signed in to change notification settings - Fork 1
/
calc.go
46 lines (38 loc) · 929 Bytes
/
calc.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
package taxes
import (
"sort"
)
// Calc is a tax calculator.
type Calc struct {
rates CurrencyRates
}
// NewCalc constructs new quarterly tax calculator.
func NewCalc(rates CurrencyRates) *Calc {
return &Calc{
rates: rates,
}
}
// Calc calculates company taxes.
func (c *Calc) Calc(company Company, payments []Payment) ([]Tax, error) {
byQuarter := map[Quarter]Tax{}
for _, p := range payments {
bcp, err := CalcBaseCurrencyPayment(c.rates, company.BaseCurrency, p)
if err != nil {
return nil, err
}
q := QuarterOf(bcp.Payment.Date)
t := byQuarter[q]
t.Quarter = q
t.Payments = append(t.Payments, *bcp)
t.Income += bcp.Amount
t.Tax = t.Income * company.TaxRate
byQuarter[q] = t
}
taxes := make([]Tax, 0, len(byQuarter))
for _, t := range byQuarter {
sort.Sort(baseCurrencyPaymentsByDate(t.Payments))
taxes = append(taxes, t)
}
sort.Sort(taxesByQuarter(taxes))
return taxes, nil
}