/
invoice.go
143 lines (122 loc) · 2.67 KB
/
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
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
package model
import (
"fmt"
"github.com/pjover/sam/internal/domain"
"github.com/pjover/sam/internal/domain/model/payment_type"
"sort"
"strings"
"time"
)
type Invoice struct {
id string
customerId int
date time.Time
yearMonth YearMonth
childrenIds []int
lines []InvoiceLine
paymentType payment_type.PaymentType
note string
emailed bool
printed bool
sentToBank bool
}
func NewInvoice(
id string,
customerId int,
date time.Time,
yearMonth YearMonth,
childrenIds []int,
lines []InvoiceLine,
paymentType payment_type.PaymentType,
note string,
emailed bool,
printed bool,
sentToBank bool,
) Invoice {
return Invoice{
id: id,
customerId: customerId,
date: date,
yearMonth: yearMonth,
childrenIds: childrenIds,
lines: lines,
paymentType: paymentType,
note: note,
emailed: emailed,
printed: printed,
sentToBank: sentToBank,
}
}
func (i Invoice) Id() string {
return i.id
}
func (i Invoice) CustomerId() int {
return i.customerId
}
func (i Invoice) Date() time.Time {
return i.date
}
func (i Invoice) YearMonth() YearMonth {
return i.yearMonth
}
func (i Invoice) ChildrenIds() []int {
return i.childrenIds
}
func (i Invoice) Lines() []InvoiceLine {
return i.lines
}
func (i Invoice) PaymentType() payment_type.PaymentType {
return i.paymentType
}
func (i Invoice) Note() string {
return i.note
}
func (i Invoice) Emailed() bool {
return i.emailed
}
func (i Invoice) Printed() bool {
return i.printed
}
func (i Invoice) SentToBank() bool {
return i.sentToBank
}
func (i Invoice) String() string {
return fmt.Sprintf("%d %s %s %7.2f %s %s", i.customerId, i.id, i.yearMonth.String(), i.Amount(), i.paymentType.Format(), i.LinesFmt(","))
}
func (i Invoice) DateFmt() string {
return i.date.Format(domain.YearMonthDayLayout)
}
func (i Invoice) Amount() float64 {
var amount float64
for _, line := range i.lines {
gross := line.Units() * line.ProductPrice()
lineAmount := gross * (1 + line.TaxPercentage())
amount += lineAmount
}
return amount
}
func (i Invoice) LinesFmt(joinWith string) string {
var lines []string
for _, line := range i.lines {
lines = append(lines, line.Format())
}
sort.Slice(lines, func(i, j int) bool {
return lines[i] < lines[j]
})
return strings.Join(lines, joinWith)
}
// SendToBank sets sentToBank to true
func (i Invoice) SendToBank() Invoice {
i.sentToBank = true
return i
}
type TransientInvoice struct {
IsRectification bool
CustomerId int
Date time.Time
YearMonth YearMonth
ChildrenIds []int
Lines []InvoiceLine
PaymentType payment_type.PaymentType
Note string
}