/
invoice.go
201 lines (190 loc) · 3.55 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
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
package types
import (
"fmt"
"strings"
)
type Invoices []*Invoice
func (arr Invoices) ById(id string) *Invoice {
for _, a := range arr {
if a == nil {
continue
}
if a.Id == id {
return a
}
}
return nil
}
func (arr Invoices) ByCode(code string) *Invoice {
for _, a := range arr {
if a == nil {
continue
}
if a.Code == code {
return a
}
}
return nil
}
func (arr Invoices) ByYear(year int) *Invoice {
for _, a := range arr {
if a == nil {
continue
}
if a.Year() == year {
return a
}
}
return nil
}
func (it *Invoice_Item) AdjustedRate() float32 {
switch strings.ToLower(it.Unit) {
case "percent":
fallthrough
case "%":
return it.Rate / 100
}
return it.Rate
}
func (it *Invoice_Item) Price(denom string) *Price {
pr := &Price{
Amount: it.Amount * it.AdjustedRate(),
Denom: denom,
}
return pr
}
func (inv *Invoice) MakeId() string {
return fmt.Sprintf("%d-%s", inv.Year(), strings.ReplaceAll(inv.Code, " ", "_"))
}
func (inv *Invoice) Year() int {
t, err := ParseTime(inv.IssueDate)
if err != nil {
return 0
}
return t.Year()
}
func (inv *Invoice) MakeFileName() string {
format := "{Full Name}_{Code}_{Issue Date}"
prj := inv.Project
cl := inv.Client
acc := inv.Account
switch {
case cl != nil && cl.InvoiceFileNameFormat != "":
format = cl.InvoiceFileNameFormat
case acc != nil && acc.InvoiceFileNameFormat != "":
format = acc.InvoiceFileNameFormat
}
tokens := map[string]func() string{
"{Full Name}": func() string {
if acc == nil {
return "-"
}
if val, ok := acc.Contact[ContactFullName]; ok {
return val
}
return "-"
},
"{Name}": func() string {
if acc == nil {
return "-"
}
if val, ok := acc.Contact[ContactName]; ok {
return val
}
return "-"
},
"{Account Name}": func() string {
if acc == nil {
return "-"
}
return acc.Name
},
"{Client Name}": func() string {
if cl == nil {
return "-"
}
return cl.Name
},
"{Project Name}": func() string {
if prj == nil {
return "-"
}
return prj.Name
},
"{Personal Code}": func() string {
if acc == nil {
return "-"
}
if val, ok := acc.Contact[ContactPersonalCode]; ok {
return val
}
return "-"
},
"{Reg. #}": func() string {
if acc == nil {
return "-"
}
if val, ok := acc.Contact[ContactRegistrationNumber]; ok {
return val
}
return "-"
},
"{Company Reg. #}": func() string {
if acc == nil {
return "-"
}
if val, ok := acc.Contact[ContactCompanyRegistrationNumber]; ok {
return val
}
return "-"
},
"{Client Reg. #}": func() string {
if cl == nil {
return "-"
}
if val, ok := cl.Contact[ContactRegistrationNumber]; ok {
return val
}
return "-"
},
"{Client Company Reg. #}": func() string {
if cl == nil {
return "-"
}
if val, ok := cl.Contact[ContactCompanyRegistrationNumber]; ok {
return val
}
return "-"
},
"{Code}": func() string {
return inv.Code
},
"{Issue Date}": func() string {
t, err := ParseTime(inv.IssueDate)
if err != nil {
return "-"
}
return t.Format("20061011")
},
"{Due Date}": func() string {
t, err := ParseTime(inv.DueDate)
if err != nil {
return "-"
}
return t.Format("20061011")
},
}
for k, val := range tokens {
format = strings.ReplaceAll(format, k, val())
}
return SanitizePath(format)
}
func (ic *InvoiceTemplateContext) SelectedAccount(inv *Invoice) *Account {
if ic == nil || ic.Account == nil {
if inv == nil {
return nil
}
return inv.Account
}
return ic.Account
}