-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf.go
More file actions
117 lines (98 loc) · 2.44 KB
/
pdf.go
File metadata and controls
117 lines (98 loc) · 2.44 KB
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
package main
import (
"bytes"
"log"
"text/template"
wkhtmltopdf "github.com/SebastiaanKlippert/go-wkhtmltopdf"
"github.com/code-cell/tracking/data"
"github.com/gobuffalo/packr"
)
//go:generate packr
type templateRow struct {
Description string
Hours float32
Rate string
Total string
}
type templateData struct {
SubmitDate string
CompanyName string
From string
For string
InvoiceNumber string
Due string
VatRate float32
Vat string
Total string
Legal string
Rows []*templateRow
}
func generateInvoice(output string, from *data.From, invoice *data.Invoice, client *data.Client, hours []*data.Hour) {
sums := make(map[string]float32, 0)
var sumTotal float32
for _, hour := range hours {
if _, found := sums[hour.Project]; !found {
sums[hour.Project] = 0
}
sums[hour.Project] += hour.Hours
sumTotal += hour.Hours
}
rows := make([]*templateRow, 0)
for projectKey, sum := range sums {
project := client.FindProject(projectKey)
if project == nil {
log.Fatalf("Client %v doesn't have project %v", client.Key, projectKey)
}
rows = append(rows, &templateRow{
Description: project.Name,
Hours: sum,
Rate: formatCurrency(invoice.Rate),
Total: formatCurrency(sum * invoice.Rate),
})
}
vat := float32(0.0)
data := &templateData{
SubmitDate: formatTime(invoice.To),
CompanyName: from.Name,
From: linesWithBR(from.BillingInfo),
For: linesWithBR(client.BillingInfo),
InvoiceNumber: invoice.Number,
Due: formatTime(invoice.To),
VatRate: 0,
Vat: formatCurrency(vat),
Total: formatCurrency((sumTotal * invoice.Rate) + vat),
Legal: from.Legal,
Rows: rows,
}
box := packr.NewBox("./templates")
t := template.New("invoice")
t, err := t.Parse(box.String("simple.html"))
if err != nil {
log.Fatal(err)
}
var buf bytes.Buffer
err = t.Execute(&buf, data)
if err != nil {
log.Fatal(err)
}
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
log.Fatal(err)
}
pdfg.Dpi.Set(600)
pdfg.NoCollate.Set(false)
pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4)
pdfg.MarginTop.Set(40)
pdfg.MarginLeft.Set(40)
pdfg.MarginRight.Set(40)
page := wkhtmltopdf.NewPageReader(&buf)
pdfg.AddPage(page)
err = pdfg.Create()
if err != nil {
log.Fatal(err)
}
err = pdfg.WriteFile(output)
if err != nil {
log.Fatal(err)
}
}