-
Notifications
You must be signed in to change notification settings - Fork 53
/
build.go
474 lines (406 loc) · 10.9 KB
/
build.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package generator
import (
"bytes"
"fmt"
"time"
"github.com/go-pdf/fpdf"
"github.com/shopspring/decimal"
)
// Build pdf document from data provided
func (doc *Document) Build() (*fpdf.Fpdf, error) {
// Validate document data
if err := doc.Validate(); err != nil {
return nil, err
}
// Build base doc
doc.pdf.SetMargins(BaseMargin, BaseMarginTop, BaseMargin)
doc.pdf.SetXY(10, 10)
doc.pdf.SetTextColor(
doc.Options.BaseTextColor[0],
doc.Options.BaseTextColor[1],
doc.Options.BaseTextColor[2],
)
// Set header
if doc.Header != nil {
if err := doc.Header.applyHeader(doc); err != nil {
return nil, err
}
}
// Set footer
if doc.Footer != nil {
if err := doc.Footer.applyFooter(doc); err != nil {
return nil, err
}
}
// Add first page
doc.pdf.AddPage()
// Load font
doc.pdf.SetFont(doc.Options.Font, "", 12)
// Appenf document title
doc.appendTitle()
// Appenf document metas (ref & version)
doc.appendMetas()
// Append company contact to doc
companyBottom := doc.Company.appendCompanyContactToDoc(doc)
// Append customer contact to doc
customerBottom := doc.Customer.appendCustomerContactToDoc(doc)
if customerBottom > companyBottom {
doc.pdf.SetXY(10, customerBottom)
} else {
doc.pdf.SetXY(10, companyBottom)
}
// Append description
doc.appendDescription()
// Append items
doc.appendItems()
// Check page height (total bloc height = 30, 45 when doc discount)
offset := doc.pdf.GetY() + 30
if doc.Discount != nil {
offset += 15
}
if offset > MaxPageHeight {
doc.pdf.AddPage()
}
// Append notes
doc.appendNotes()
// Append total
doc.appendTotal()
// Append payment term
doc.appendPaymentTerm()
// Append js to autoprint if AutoPrint == true
if doc.Options.AutoPrint {
doc.pdf.SetJavascript("print(true);")
}
return doc.pdf, nil
}
// appendTitle to document
func (doc *Document) appendTitle() {
title := doc.typeAsString()
// Set x y
doc.pdf.SetXY(120, BaseMarginTop)
// Draw rect
doc.pdf.SetFillColor(doc.Options.DarkBgColor[0], doc.Options.DarkBgColor[1], doc.Options.DarkBgColor[2])
doc.pdf.Rect(120, BaseMarginTop, 80, 10, "F")
// Draw text
doc.pdf.SetFont(doc.Options.Font, "", 14)
doc.pdf.CellFormat(80, 10, doc.encodeString(title), "0", 0, "C", false, 0, "")
}
// appendMetas to document
func (doc *Document) appendMetas() {
// Append ref
refString := fmt.Sprintf("%s: %s", doc.Options.TextRefTitle, doc.Ref)
doc.pdf.SetXY(120, BaseMarginTop+11)
doc.pdf.SetFont(doc.Options.Font, "", 8)
doc.pdf.CellFormat(80, 4, doc.encodeString(refString), "0", 0, "R", false, 0, "")
// Append version
if len(doc.Version) > 0 {
versionString := fmt.Sprintf("%s: %s", doc.Options.TextVersionTitle, doc.Version)
doc.pdf.SetXY(120, BaseMarginTop+15)
doc.pdf.SetFont(doc.Options.Font, "", 8)
doc.pdf.CellFormat(80, 4, doc.encodeString(versionString), "0", 0, "R", false, 0, "")
}
// Append date
date := time.Now().Format("02/01/2006")
if len(doc.Date) > 0 {
date = doc.Date
}
dateString := fmt.Sprintf("%s: %s", doc.Options.TextDateTitle, date)
doc.pdf.SetXY(120, BaseMarginTop+19)
doc.pdf.SetFont(doc.Options.Font, "", 8)
doc.pdf.CellFormat(80, 4, doc.encodeString(dateString), "0", 0, "R", false, 0, "")
}
// appendDescription to document
func (doc *Document) appendDescription() {
if len(doc.Description) > 0 {
doc.pdf.SetY(doc.pdf.GetY() + 10)
doc.pdf.SetFont(doc.Options.Font, "", 10)
doc.pdf.MultiCell(190, 5, doc.encodeString(doc.Description), "B", "L", false)
}
}
// drawsTableTitles in document
func (doc *Document) drawsTableTitles() {
// Draw table titles
doc.pdf.SetX(10)
doc.pdf.SetY(doc.pdf.GetY() + 5)
doc.pdf.SetFont(doc.Options.BoldFont, "B", 8)
// Draw rec
doc.pdf.SetFillColor(doc.Options.GreyBgColor[0], doc.Options.GreyBgColor[1], doc.Options.GreyBgColor[2])
doc.pdf.Rect(10, doc.pdf.GetY(), 190, 6, "F")
// Name
doc.pdf.SetX(ItemColNameOffset)
doc.pdf.CellFormat(
ItemColUnitPriceOffset-ItemColNameOffset,
6,
doc.encodeString(doc.Options.TextItemsNameTitle),
"0",
0,
"",
false,
0,
"",
)
// Unit price
doc.pdf.SetX(ItemColUnitPriceOffset)
doc.pdf.CellFormat(
ItemColQuantityOffset-ItemColUnitPriceOffset,
6,
doc.encodeString(doc.Options.TextItemsUnitCostTitle),
"0",
0,
"",
false,
0,
"",
)
// Quantity
doc.pdf.SetX(ItemColQuantityOffset)
doc.pdf.CellFormat(
ItemColTaxOffset-ItemColQuantityOffset,
6,
doc.encodeString(doc.Options.TextItemsQuantityTitle),
"0",
0,
"",
false,
0,
"",
)
// Total HT
doc.pdf.SetX(ItemColTotalHTOffset)
doc.pdf.CellFormat(
ItemColTaxOffset-ItemColTotalHTOffset,
6,
doc.encodeString(doc.Options.TextItemsTotalHTTitle),
"0",
0,
"",
false,
0,
"",
)
// Tax
doc.pdf.SetX(ItemColTaxOffset)
doc.pdf.CellFormat(
ItemColDiscountOffset-ItemColTaxOffset,
6,
doc.encodeString(doc.Options.TextItemsTaxTitle),
"0",
0,
"",
false,
0,
"",
)
// Discount
doc.pdf.SetX(ItemColDiscountOffset)
doc.pdf.CellFormat(
ItemColTotalTTCOffset-ItemColDiscountOffset,
6,
doc.encodeString(doc.Options.TextItemsDiscountTitle),
"0",
0,
"",
false,
0,
"",
)
// TOTAL TTC
doc.pdf.SetX(ItemColTotalTTCOffset)
doc.pdf.CellFormat(
190-ItemColTotalTTCOffset,
6,
doc.encodeString(doc.Options.TextItemsTotalTTCTitle),
"0",
0,
"",
false,
0,
"",
)
}
// appendItems to document
func (doc *Document) appendItems() {
doc.drawsTableTitles()
doc.pdf.SetX(10)
doc.pdf.SetY(doc.pdf.GetY() + 8)
doc.pdf.SetFont(doc.Options.Font, "", 8)
for i := 0; i < len(doc.Items); i++ {
item := doc.Items[i]
// Check item tax
if item.Tax == nil {
item.Tax = doc.DefaultTax
}
// Append to pdf
item.appendColTo(doc.Options, doc)
if doc.pdf.GetY() > MaxPageHeight {
// Add page
doc.pdf.AddPage()
doc.drawsTableTitles()
doc.pdf.SetFont(doc.Options.Font, "", 8)
}
doc.pdf.SetX(10)
doc.pdf.SetY(doc.pdf.GetY() + 6)
}
}
// appendNotes to document
func (doc *Document) appendNotes() {
if len(doc.Notes) == 0 {
return
}
currentY := doc.pdf.GetY()
doc.pdf.SetFont(doc.Options.Font, "", 9)
doc.pdf.SetX(BaseMargin)
doc.pdf.SetRightMargin(100)
doc.pdf.SetY(currentY + 10)
_, lineHt := doc.pdf.GetFontSize()
html := doc.pdf.HTMLBasicNew()
html.Write(lineHt, doc.encodeString(doc.Notes))
doc.pdf.SetRightMargin(BaseMargin)
doc.pdf.SetY(currentY)
}
// appendTotal to document
func (doc *Document) appendTotal() {
doc.pdf.SetY(doc.pdf.GetY() + 10)
doc.pdf.SetFont(doc.Options.Font, "", LargeTextFontSize)
doc.pdf.SetTextColor(
doc.Options.BaseTextColor[0],
doc.Options.BaseTextColor[1],
doc.Options.BaseTextColor[2],
)
// Draw TOTAL HT title
doc.pdf.SetX(120)
doc.pdf.SetFillColor(doc.Options.DarkBgColor[0], doc.Options.DarkBgColor[1], doc.Options.DarkBgColor[2])
doc.pdf.Rect(120, doc.pdf.GetY(), 40, 10, "F")
doc.pdf.CellFormat(38, 10, doc.encodeString(doc.Options.TextTotalTotal), "0", 0, "R", false, 0, "")
// Draw TOTAL HT amount
doc.pdf.SetX(162)
doc.pdf.SetFillColor(doc.Options.GreyBgColor[0], doc.Options.GreyBgColor[1], doc.Options.GreyBgColor[2])
doc.pdf.Rect(160, doc.pdf.GetY(), 40, 10, "F")
doc.pdf.CellFormat(
40,
10,
doc.encodeString(doc.ac.FormatMoneyDecimal(doc.TotalWithoutTaxAndWithoutDocumentDiscount())),
"0",
0,
"L",
false,
0,
"",
)
if doc.Discount != nil {
baseY := doc.pdf.GetY() + 10
// Draw discounted title
doc.pdf.SetXY(120, baseY)
doc.pdf.SetFillColor(doc.Options.DarkBgColor[0], doc.Options.DarkBgColor[1], doc.Options.DarkBgColor[2])
doc.pdf.Rect(120, doc.pdf.GetY(), 40, 15, "F")
// title
doc.pdf.CellFormat(38, 7.5, doc.encodeString(doc.Options.TextTotalDiscounted), "0", 0, "BR", false, 0, "")
// description
doc.pdf.SetXY(120, baseY+7.5)
doc.pdf.SetFont(doc.Options.Font, "", BaseTextFontSize)
doc.pdf.SetTextColor(
doc.Options.GreyTextColor[0],
doc.Options.GreyTextColor[1],
doc.Options.GreyTextColor[2],
)
var descString bytes.Buffer
discountType, discountAmount := doc.Discount.getDiscount()
if discountType == DiscountTypePercent {
descString.WriteString("-")
descString.WriteString(discountAmount.String())
descString.WriteString(" % / -")
descString.WriteString(doc.ac.FormatMoneyDecimal(
doc.TotalWithoutTaxAndWithoutDocumentDiscount().Sub(doc.TotalWithoutTax())),
)
} else {
descString.WriteString("-")
descString.WriteString(doc.ac.FormatMoneyDecimal(discountAmount))
descString.WriteString(" / -")
descString.WriteString(
discountAmount.Mul(decimal.NewFromFloat(100)).Div(doc.TotalWithoutTaxAndWithoutDocumentDiscount()).StringFixed(2),
)
descString.WriteString(" %")
}
doc.pdf.CellFormat(38, 7.5, doc.encodeString(descString.String()), "0", 0, "TR", false, 0, "")
doc.pdf.SetFont(doc.Options.Font, "", LargeTextFontSize)
doc.pdf.SetTextColor(
doc.Options.BaseTextColor[0],
doc.Options.BaseTextColor[1],
doc.Options.BaseTextColor[2],
)
// Draw discount amount
doc.pdf.SetY(baseY)
doc.pdf.SetX(162)
doc.pdf.SetFillColor(doc.Options.GreyBgColor[0], doc.Options.GreyBgColor[1], doc.Options.GreyBgColor[2])
doc.pdf.Rect(160, doc.pdf.GetY(), 40, 15, "F")
doc.pdf.CellFormat(
40,
15,
doc.encodeString(doc.ac.FormatMoneyDecimal(doc.TotalWithoutTax())),
"0",
0,
"L",
false,
0,
"",
)
doc.pdf.SetY(doc.pdf.GetY() + 15)
} else {
doc.pdf.SetY(doc.pdf.GetY() + 10)
}
// Draw tax title
doc.pdf.SetX(120)
doc.pdf.SetFillColor(doc.Options.DarkBgColor[0], doc.Options.DarkBgColor[1], doc.Options.DarkBgColor[2])
doc.pdf.Rect(120, doc.pdf.GetY(), 40, 10, "F")
doc.pdf.CellFormat(38, 10, doc.encodeString(doc.Options.TextTotalTax), "0", 0, "R", false, 0, "")
// Draw tax amount
doc.pdf.SetX(162)
doc.pdf.SetFillColor(doc.Options.GreyBgColor[0], doc.Options.GreyBgColor[1], doc.Options.GreyBgColor[2])
doc.pdf.Rect(160, doc.pdf.GetY(), 40, 10, "F")
doc.pdf.CellFormat(
40,
10,
doc.encodeString(doc.ac.FormatMoneyDecimal(doc.Tax())),
"0",
0,
"L",
false,
0,
"",
)
// Draw total with tax title
doc.pdf.SetY(doc.pdf.GetY() + 10)
doc.pdf.SetX(120)
doc.pdf.SetFillColor(doc.Options.DarkBgColor[0], doc.Options.DarkBgColor[1], doc.Options.DarkBgColor[2])
doc.pdf.Rect(120, doc.pdf.GetY(), 40, 10, "F")
doc.pdf.CellFormat(38, 10, doc.encodeString(doc.Options.TextTotalWithTax), "0", 0, "R", false, 0, "")
// Draw total with tax amount
doc.pdf.SetX(162)
doc.pdf.SetFillColor(doc.Options.GreyBgColor[0], doc.Options.GreyBgColor[1], doc.Options.GreyBgColor[2])
doc.pdf.Rect(160, doc.pdf.GetY(), 40, 10, "F")
doc.pdf.CellFormat(
40,
10,
doc.encodeString(doc.ac.FormatMoneyDecimal(doc.TotalWithTax())),
"0",
0,
"L",
false,
0,
"",
)
}
// appendPaymentTerm to document
func (doc *Document) appendPaymentTerm() {
if len(doc.PaymentTerm) > 0 {
paymentTermString := fmt.Sprintf(
"%s: %s",
doc.encodeString(doc.Options.TextPaymentTermTitle),
doc.encodeString(doc.PaymentTerm),
)
doc.pdf.SetY(doc.pdf.GetY() + 15)
doc.pdf.SetX(120)
doc.pdf.SetFont(doc.Options.BoldFont, "B", 10)
doc.pdf.CellFormat(80, 4, doc.encodeString(paymentTermString), "0", 0, "R", false, 0, "")
}
}