-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
149 lines (128 loc) · 3.29 KB
/
app.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
package main
import (
"context"
"flag"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"time"
_ "embed"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"go.uber.org/zap"
)
const (
dateFormat = "Jan 2, 2006"
receiptDateFormat = "January 2006"
stdDateFormat = "02-01-2006"
)
var (
//go:embed receipt.html
receiptTemplate string
)
type Receipt struct {
ID int
Tenant Person
Landlord Person
Amount uint64
startDate time.Time
endDate time.Time
issueDate time.Time
IsProvisional bool
}
func (r *Receipt) StartDate() string {
return r.startDate.Format(dateFormat)
}
func (r *Receipt) IssueDate() string {
return r.issueDate.Format(dateFormat)
}
func (r *Receipt) EndDate() string {
return r.endDate.Format(dateFormat)
}
func (r *Receipt) ReceiptDate() string {
return r.startDate.Format(receiptDateFormat)
}
func main() {
var cfgFile string
flag.StringVar(&cfgFile, "config", "", "specify the config file to be used")
flag.Parse()
if cfgFile == "" {
flag.Usage()
os.Exit(1)
}
config, err := ParseConfig(cfgFile)
logger, _ := zap.NewDevelopment()
zap.ReplaceGlobals(logger)
if err != nil {
zap.L().Fatal("failed parse config yaml", zap.Error(err))
}
zap.L().Info("loaded config file", zap.Any("config", config))
fyStart := time.Date(int(config.FinancialYear), 4, 1, 0, 0, 0, 0, time.Local)
var receipts []*Receipt
for i := 0; i < 12; i++ {
receipt := &Receipt{
Tenant: config.Tenant,
Landlord: config.Landlord,
Amount: config.Rent,
ID: i + 1,
startDate: fyStart.AddDate(0, i, 0),
endDate: fyStart.AddDate(0, i+1, -1),
issueDate: fyStart.AddDate(0, i+1, 0),
}
if receipt.issueDate.Month() == time.April {
receipt.issueDate = receipt.issueDate.AddDate(0, -1, 0)
}
receipts = append(receipts, receipt)
}
generatePDF(receipts, config)
}
func writeHTML(receipts []*Receipt) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
tpl, err := template.New("receipt").Parse(receiptTemplate)
if err != nil {
zap.L().Fatal("failed to parse template", zap.Error(err))
}
if err := tpl.Execute(w, receipts); err != nil {
zap.L().Fatal("failed to generate receipts", zap.Error(err))
}
w.WriteHeader(http.StatusOK)
})
}
// generatePDF creates a PDF with the given data
func generatePDF(receipts []*Receipt, config *Config) (string, error) {
file := fmt.Sprintf("./e_rent_receipt_%d.pdf", config.FinancialYear)
// Convert objects and save the output PDF document.
outFile, err := os.Create(file)
if err != nil {
return "", err
}
defer outFile.Close()
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
ts := httptest.NewServer(writeHTML(receipts))
defer ts.Close()
var buf []byte
if err := chromedp.Run(ctx,
chromedp.Navigate(ts.URL),
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
buf, _, err = page.PrintToPDF().
WithDisplayHeaderFooter(false).
WithPaperHeight(11.7).
WithPaperWidth(8.27).
WithLandscape(false).
Do(ctx)
return err
}),
); err != nil {
return "", err
}
if err := ioutil.WriteFile(file, buf, 0644); err != nil {
return "", err
}
return file, nil
}