forked from angelodlfrtr/go-invoice-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
393 lines (311 loc) · 9.17 KB
/
document.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package generator
import (
"fmt"
"time"
"github.com/jung-kurt/gofpdf"
"github.com/leekchan/accounting"
"github.com/shopspring/decimal"
)
type Document struct {
Options *Options
Header *HeaderFooter
Footer *HeaderFooter
Type string `validate:"required,oneof=INVOICE DELIVERY_NOTE QUOTATION"`
Ref string `validate:"required,min=1,max=32"`
Version string `validate:"max=32"`
ClientRef string `validate:"max=64"`
Description string `validate:"max=1024"`
Notes string
Company *Contact `validate:"required"`
Customer *Contact `validate:"required"`
Items []*Item
Date string
ValidityDate string
PaymentTerm string
Bank *Bank
}
func (d *Document) Build() (*gofpdf.Fpdf, error) {
// Validate document data
err := d.Validate()
if err != nil {
return nil, err
}
// Build base doc
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
pdf.SetXY(10, 10)
pdf.SetTextColor(BaseTextColor[0], BaseTextColor[1], BaseTextColor[2])
// Set header
if d.Header != nil {
err = d.Header.applyHeader(d, pdf)
if err != nil {
return nil, err
}
}
// Set footer
if d.Footer != nil {
err = d.Footer.applyFooter(d, pdf)
if err != nil {
return nil, err
}
}
// Add first page
pdf.AddPage()
// Load font
pdf.SetFont("Helvetica", "", 12)
// Appenf document title
d.appendTitle(pdf)
// Appenf document metas (ref & version)
d.appendMetas(pdf)
// Append company contact to doc
companyBottom := d.Company.appendCompanyContactToDoc(pdf)
// Append customer contact to doc
customerBottom := d.Customer.appendCustomerContactToDoc(pdf)
if customerBottom > companyBottom {
pdf.SetXY(10, customerBottom)
} else {
pdf.SetXY(10, companyBottom)
}
// Append description
d.appendDescription(pdf)
// Append items
d.appendItems(pdf)
// Append notes
d.appendNotes(pdf)
// Append total
d.appendTotal(pdf)
// Append payment term
d.appendPaymentTerm(pdf)
d.appendBank(pdf)
// Append js to autoprint if AutoPrint == true
if d.Options.AutoPrint {
pdf.SetJavascript("print(true);")
}
return pdf, nil
}
func (d *Document) SetBank(b *Bank) *Document {
d.Bank = b
return d
}
func (d *Document) SetType(docType string) *Document {
d.Type = docType
return d
}
func (d *Document) SetHeader(header *HeaderFooter) *Document {
d.Header = header
return d
}
func (d *Document) SetFooter(footer *HeaderFooter) *Document {
d.Footer = footer
return d
}
func (d *Document) SetRef(ref string) *Document {
d.Ref = ref
return d
}
func (d *Document) SetVersion(version string) *Document {
d.Version = version
return d
}
func (d *Document) SetDescription(desc string) *Document {
d.Description = desc
return d
}
func (d *Document) SetNotes(notes string) *Document {
d.Notes = notes
return d
}
func (d *Document) SetCompany(company *Contact) *Document {
d.Company = company
return d
}
func (d *Document) SetCustomer(customer *Contact) *Document {
d.Customer = customer
return d
}
func (d *Document) AppendItem(item *Item) *Document {
d.Items = append(d.Items, item)
return d
}
func (d *Document) SetDate(date string) *Document {
d.Date = date
return d
}
func (d *Document) SetPaymentTerm(term string) *Document {
d.PaymentTerm = term
return d
}
// ===========================
// PRIVATE ===================
// ===========================
func (d *Document) appendTitle(pdf *gofpdf.Fpdf) {
title := d.typeAsString()
// Set x y
pdf.SetXY(120, BaseMarginTop)
// Draw rect
pdf.SetFillColor(DarkBgColor[0], DarkBgColor[1], DarkBgColor[2])
pdf.Rect(120, BaseMarginTop, 80, 10, "F")
// Draw text
pdf.SetFont("Helvetica", "", 14)
pdf.CellFormat(80, 10, title, "0", 0, "C", false, 0, "")
}
func (d *Document) appendMetas(pdf *gofpdf.Fpdf) {
// Append ref
refString := fmt.Sprintf("%s: %s", d.Options.TextRefTitle, d.Ref)
pdf.SetXY(120, BaseMarginTop+11)
pdf.SetFont("Helvetica", "", 8)
pdf.CellFormat(80, 4, refString, "0", 0, "R", false, 0, "")
// Append version
if len(d.Version) > 0 {
versionString := fmt.Sprintf("%s: %s", d.Options.TextVersionTitle, d.Version)
pdf.SetXY(120, BaseMarginTop+15)
pdf.SetFont("Helvetica", "", 8)
pdf.CellFormat(80, 4, encodeString(versionString), "0", 0, "R", false, 0, "")
}
// Append date
date := time.Now().Format("02/01/2006")
if len(d.Date) > 0 {
date = d.Date
}
dateString := fmt.Sprintf("%s: %s", d.Options.TextDateTitle, date)
pdf.SetXY(120, BaseMarginTop+19)
pdf.SetFont("Helvetica", "", 8)
pdf.CellFormat(80, 4, encodeString(dateString), "0", 0, "R", false, 0, "")
}
func (d *Document) appendDescription(pdf *gofpdf.Fpdf) {
if len(d.Description) > 0 {
pdf.SetY(pdf.GetY() + 10)
pdf.SetFont("Helvetica", "", 10)
pdf.MultiCell(190, 5, encodeString(d.Description), "B", "L", false)
}
}
func (d *Document) drawsTableTitles(pdf *gofpdf.Fpdf) {
// Draw table titles
pdf.SetX(10)
pdf.SetY(pdf.GetY() + 5)
pdf.SetFont("Helvetica", "B", 8)
// Draw rec
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(10, pdf.GetY(), 190, 6, "F")
// Description
pdf.CellFormat(80, 6, encodeString(d.Options.TextItemsDescriptionTitle), "0", 0, "", false, 0, "")
// Unit price
pdf.SetX(90)
pdf.CellFormat(30, 6, encodeString(d.Options.TextItemsUnitCostTitle), "0", 0, "", false, 0, "")
// Quantity
pdf.SetX(120)
pdf.CellFormat(15, 6, encodeString(d.Options.TextItemsQuantityTitle), "0", 0, "", false, 0, "")
// Total HT
pdf.SetX(135)
pdf.CellFormat(20, 6, encodeString(d.Options.TextItemsTotalHTTitle), "0", 0, "", false, 0, "")
// Tax
pdf.SetX(155)
pdf.CellFormat(20, 6, encodeString(d.Options.TextItemsTaxTitle), "0", 0, "", false, 0, "")
// TOTAL TTC
pdf.SetX(175)
pdf.CellFormat(25, 6, encodeString(d.Options.TextItemsTotalTTCTitle), "0", 0, "", false, 0, "")
}
func (d *Document) appendItems(pdf *gofpdf.Fpdf) {
d.drawsTableTitles(pdf)
pdf.SetX(10)
pdf.SetY(pdf.GetY() + 6)
pdf.SetFont("Helvetica", "", 8)
for i := 0; i < len(d.Items); i++ {
item := d.Items[i]
item.appendColTo(d.Options, pdf)
if pdf.GetY() > MaxPageHeight {
// Add page
pdf.AddPage()
d.drawsTableTitles(pdf)
pdf.SetFont("Helvetica", "", 8)
}
pdf.SetX(10)
pdf.SetY(pdf.GetY() + 6)
}
}
func (d *Document) appendNotes(pdf *gofpdf.Fpdf) {
if len(d.Notes) == 0 {
return
}
currentY := pdf.GetY()
if currentY+30 > MaxPageHeight {
pdf.AddPage()
currentY = pdf.GetY()
}
pdf.SetFont("Helvetica", "", 9)
pdf.SetX(BaseMargin)
pdf.SetRightMargin(100)
pdf.SetY(currentY + 10)
_, lineHt := pdf.GetFontSize()
html := pdf.HTMLBasicNew()
html.Write(lineHt, d.Notes)
pdf.SetRightMargin(BaseMargin)
pdf.SetY(currentY)
}
func (d *Document) appendTotal(pdf *gofpdf.Fpdf) {
ac := accounting.Accounting{
Symbol: encodeString(d.Options.CurrencySymbol),
Precision: d.Options.CurrencyPrecision,
Thousand: d.Options.CurrencyThousand,
Decimal: d.Options.CurrencyDecimal,
}
// Get total HT & totalTTC
totalHT, _ := decimal.NewFromString("0")
totalTTC, _ := decimal.NewFromString("0")
for i := 0; i < len(d.Items); i++ {
item := d.Items[i]
totalHT = totalHT.Add(item.totalHT())
totalTTC = totalTTC.Add(item.totalTTC())
}
totalTax := totalTTC.Sub(totalHT)
// Check page height (total bloc height = 30)
if pdf.GetY()+30 > MaxPageHeight {
pdf.AddPage()
}
pdf.SetY(pdf.GetY() + 10)
pdf.SetFont("Helvetica", "", 10)
// Draw TOTAL HT title
pdf.SetX(120)
pdf.SetFillColor(DarkBgColor[0], DarkBgColor[1], DarkBgColor[2])
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextTotalTotal), "0", 0, "R", false, 0, "")
// Draw TOTAL HT amount
pdf.SetX(162)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, ac.FormatMoneyDecimal(totalHT), "0", 0, "L", false, 0, "")
// Draw TAX title
pdf.SetY(pdf.GetY() + 10)
pdf.SetX(120)
pdf.SetFillColor(DarkBgColor[0], DarkBgColor[1], DarkBgColor[2])
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextTotalTax), "0", 0, "R", false, 0, "")
// Draw TAX amount
pdf.SetX(162)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, ac.FormatMoneyDecimal(totalTax), "0", 0, "L", false, 0, "")
// Draw TOTAL TTC title
pdf.SetY(pdf.GetY() + 10)
pdf.SetX(120)
pdf.SetFillColor(DarkBgColor[0], DarkBgColor[1], DarkBgColor[2])
pdf.Rect(120, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(38, 10, encodeString(d.Options.TextTotalWithTax), "0", 0, "R", false, 0, "")
// Draw TOTAL TTC amount
pdf.SetX(162)
pdf.SetFillColor(GreyBgColor[0], GreyBgColor[1], GreyBgColor[2])
pdf.Rect(160, pdf.GetY(), 40, 10, "F")
pdf.CellFormat(40, 10, ac.FormatMoneyDecimal(totalTTC), "0", 0, "L", false, 0, "")
}
func (d *Document) appendPaymentTerm(pdf *gofpdf.Fpdf) {
if len(d.PaymentTerm) > 0 {
paymentTermString := fmt.Sprintf("%s: %s", encodeString(d.Options.TextPaymentTermTitle), encodeString(d.PaymentTerm))
pdf.SetY(pdf.GetY() + 15)
pdf.SetX(120)
pdf.SetFont("Helvetica", "B", 10)
pdf.CellFormat(80, 4, paymentTermString, "0", 0, "R", false, 0, "")
}
}
func (d *Document) appendBank(pdf *gofpdf.Fpdf) {
d.Bank.appendBankTODoc(120, pdf.GetY()+15, true, pdf)
}