This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
/
webinvoice.go
83 lines (71 loc) · 2.24 KB
/
webinvoice.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
package nlgids
import (
"fmt"
"log"
"net/http"
"path"
"strconv"
"time"
"github.com/miekg/nlgids/email"
ntour "github.com/miekg/nlgids/tour"
"github.com/miekg/nlgids/webinvoice"
)
// WebInvoice sends an email to the recipients from a invoice form from the website.
func (n *NLgids) WebInvoice(w http.ResponseWriter, r *http.Request) (int, error) {
tour, personsStr := r.PostFormValue("tour"), r.PostFormValue("persons")
t, duration := r.PostFormValue("time"), r.PostFormValue("duration")
costStr, date := r.PostFormValue("cost"), r.PostFormValue("date")
name, fullname := r.PostFormValue("name"), r.PostFormValue("fullname")
email := r.PostFormValue("email")
where, how := r.PostFormValue("where"), r.PostFormValue("how")
if tour == "" || personsStr == "" || t == "" || date == "" || name == "" ||
fullname == "" || duration == "" {
return http.StatusBadRequest, fmt.Errorf("nlgids: all empty")
}
cost, err := strconv.ParseFloat(costStr, 64)
if err != nil {
return http.StatusBadRequest, err
}
persons, err := strconv.Atoi(personsStr)
if err != nil {
return http.StatusBadRequest, err
}
// Get the real the name of the tour.
tour = ntour.NameOrNonExists(tour, n.Config.Tours)
log.Printf("[INFO] NLgids tour: %s:%s:%s:%s:%s:%f", tour, name, email, personsStr, date, cost)
invoice := &webinvoice.Invoice{
Tour: tour,
Persons: persons,
Time: t,
Duration: duration,
Cost: cost,
Date: date,
Name: name,
FullName: fullname,
Email: email,
Where: where,
How: how,
Kenmerk: webinvoice.Kenmerk(time.Now().UTC()),
}
tmpl := path.Join(n.Config.Template, webinvoice.Template)
pdf, err := invoice.Create(n.Config.Template, tmpl)
if err != nil {
return http.StatusInternalServerError, err
}
if len(pdf) == 0 {
return http.StatusInternalServerError, err
}
return sendInvoice(invoice, pdf, n.Config.Recipients)
}
func sendInvoice(i *webinvoice.Invoice, pdf []byte, rcpts []string) (int, error) {
subject := i.MailSubject()
body, err := i.MailBody()
if err != nil {
return http.StatusInternalServerError, err
}
mail := email.NewInvoice(subject, body, i.FileName, pdf)
if err := mail.Do(rcpts); err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}