udemy-go-web-2/cmd/micro/invoice/invoice-handlers.go

146 lines
2.2 KiB
Go

package main
import (
"fmt"
"net/http"
"time"
"github.com/phpdave11/gofpdf"
"github.com/phpdave11/gofpdf/contrib/gofpdi"
)
type Order struct {
ID int `json:"id"`
Quantity int `json:"quantity"`
Amount int `json:"amount"`
Product string `json:"product"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
CreatedAt time.Time `json:"-"`
}
func (app *application) CreateAndSendInvoice(w http.ResponseWriter, r *http.Request) {
// receive json
var order Order
err := app.readJSON(w, r, &order)
if err != nil {
app.badRequest(w, r, err)
return
}
// generate a pdf invoice
// create mail
// send mail with attachment
// send response
var resp JSONResponse
resp.OK = true
resp.Message = fmt.Sprintf("Invoice %d.pdf created and sent to %s", order.ID, order.Email)
app.writeJSON(w, http.StatusCreated, resp)
}
func (app *application) createInvoicePDF(order Order) error {
pdf := gofpdf.New("P", "mm", "Letter", "")
pdf.SetMargins(10, 13, 10)
pdf.SetAutoPageBreak(true, 0)
importer := gofpdi.NewImporter()
t := importer.ImportPage(pdf, "./pdf-templates/invoice.pdf", q, "/MediaBox")
pdf.AddPage()
importer.UseImportedTemplate(pdf, t, 0, 0, 215.9, 0)
pdf.SetY(20)
// write info
pdf.SetY(50)
pdf.SetX(10)
pdf.SetFont("Times", "", 11)
pdf.CellFormat(
97,
8,
fmt.Sprintf("Attention: %s %s", order.FirstName, order.LastName),
"",
0,
"L",
false,
0,
"",
)
pdf.Ln(5)
pdf.CellFormat(
97,
8,
order.Email,
"",
0,
"L",
false,
0,
"",
)
pdf.Ln(5)
pdf.CellFormat(
97,
8,
order.CreatedAt.Format("2005-01-02"),
"",
0,
"L",
false,
0,
"",
)
pdf.SetX(58)
pdf.SetY(93)
pdf.CellFormat(
155,
8,
order.Product,
"",
0,
"L",
false,
0,
"",
)
pdf.SetX(166)
pdf.CellFormat(
20,
8,
fmt.Sprintf("%d", order.Quantity),
"",
0,
"C",
false,
0,
"",
)
pdf.SetX(185)
pdf.CellFormat(
155,
8,
fmt.Sprintf("€%.2f", float32(order.Amount/100.0)),
"",
0,
"R",
false,
0,
"",
)
invoicePath := fmt.Sprintf("./invoices/%d.pdf", order.ID)
err := pdf.OutputFileAndClose(invoicePath)
if err != nil {
return err
}
return nil
}