-
Notifications
You must be signed in to change notification settings - Fork 5
/
invoice.go
146 lines (120 loc) · 3.03 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
package main
import (
"crypto/tls"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
"github.com/Shopify/gomail"
)
func sendInvoice(id int) error {
dog, err := getDog(id)
if err != nil {
return err
}
_, err = generatePdf(dog)
if err != nil {
return err
}
err = sendEmail(dog)
if err != nil {
return err
}
return nil
}
func sendInvoices() (status string, err error) {
emails, err := getEmailQueue()
if err != nil {
return "", err
}
err = markEmailsInProcess(emails)
if err != nil {
return "", err
}
for _, email := range emails {
if err != nil {
return "", err
}
err = sendInvoice(email.DogID)
if err != nil {
return "", err
}
err = markEmailSent(email.ID)
if err != nil {
return "", err
}
}
return "Processed " + strconv.Itoa(len(emails)) + " emails", nil
}
func generatePdf(dog Dog) (string, error) {
pdfGenerator, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
return "", err
}
page := wkhtmltopdf.NewPage(os.Getenv("BASE_URL") + "/invoice/" + strconv.Itoa(dog.ID))
pdfGenerator.AddPage(page)
err = pdfGenerator.Create()
if err != nil {
return "", err
}
invoiceFile := fmt.Sprintf("./public/%s.pdf", getInvoiceNumber(dog))
err = pdfGenerator.WriteFile(invoiceFile)
if err != nil {
return "", err
}
return invoiceFile, nil
}
func sendEmail(dog Dog) error {
invoiceFile := fmt.Sprintf("./public/%s.pdf", getInvoiceNumber(dog))
smtpPort, err := strconv.Atoi(os.Getenv("SMTP_PORT"))
if err != nil {
return fmt.Errorf("error converting SMTP_PORT to int: %s", err)
}
d := gomail.NewDialer(
os.Getenv("SMTP_HOST"),
smtpPort,
os.Getenv("SMTP_USER"),
os.Getenv("SMTP_PASS"),
)
d.Timeout = 30 * time.Second
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
ownerFirstName := strings.Split(dog.OwnerName, " ")[0]
fromFirstName := strings.Split(os.Getenv("FROM_NAME"), " ")[0]
walkOrWalks := "walk"
if dog.Quantity > 1 {
walkOrWalks = "walks"
}
m := gomail.NewMessage()
m.SetHeader("From", fmt.Sprintf("Canine Club<%s>", os.Getenv("SMTP_USER")))
m.SetHeader("To", fmt.Sprintf("%s <%s>", dog.OwnerName, dog.Email))
m.SetHeader("Subject", "Canine Club - Invoice for "+dog.Name)
m.SetBody(
"text/html",
"Hi "+ownerFirstName+",<br><br>Please find attached the invoice for "+dog.Name+"'s "+walkOrWalks+" this week.<p style='font-weight:lighter;'>Please use '<b>"+dog.Name+"</b>' as the reference when making payment. Also note that payment is due by "+nextMonday(
time.Now(),
).Format("Monday, 2 January 2006")+
".</p><br>Any questions let me know,<br>Thank you!<br><br>"+fromFirstName+"<br>Canine Club",
)
m.Attach(invoiceFile)
err = d.DialAndSend(m)
if err != nil {
return err
} else {
return nil
}
}
func getInvoiceNumber(dog Dog) string {
prefix := strings.ToUpper(dog.Name[0:3])
return prefix + time.Now().Format("20060102")
}
func nextMonday(t time.Time) time.Time {
if t.Weekday() == time.Monday {
return t.AddDate(0, 0, 7)
}
for t.Weekday() != time.Monday {
t = t.AddDate(0, 0, 1)
}
return t
}