/
invoice.go
50 lines (41 loc) 路 936 Bytes
/
invoice.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
package manager
import (
"fmt"
"time"
)
type Invoice struct {
ID uint64
Client string
Emitted, Delivered time.Time
Services []Service
Comment string
Currency rune
PaymentDays uint8
Quote bool
}
func (i Invoice) Total() float64 {
var t float64
for _, s := range i.Services {
t += s.Amount()
}
return t
}
type Service struct {
Description string
UnitCost float64
Unit string
Quantity float64
}
func (s Service) format(currency rune) (desc, qty, uc, amount string) {
desc = s.Description
qty = fmt.Sprintf("%g %s", s.Quantity, s.Unit)
uc = formatMoney(s.UnitCost, currency)
amount = formatMoney(s.UnitCost*s.Quantity, currency)
return
}
func (s Service) Amount() float64 {
return s.UnitCost * s.Quantity
}
func formatMoney(value float64, currency rune) string {
return fmt.Sprintf("%.2f %c", value, currency)
}