2024-08-26 20:49:10 +00:00
|
|
|
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"`
|
2024-08-27 11:52:36 +00:00
|
|
|
CreatedAt time.Time `json:"created_at"`
|
2024-08-26 20:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) CreateAndSendInvoice(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// receive json
|
|
|
|
var order Order
|
|
|
|
|
2024-08-27 11:28:11 +00:00
|
|
|
err := app.readJSON(w, r, &order)
|
|
|
|
if err != nil {
|
|
|
|
app.badRequest(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2024-08-27 11:52:36 +00:00
|
|
|
app.infoLog.Println(order)
|
2024-08-26 21:06:29 +00:00
|
|
|
|
|
|
|
// generate a pdf invoice
|
2024-08-27 11:28:11 +00:00
|
|
|
err = app.createInvoicePDF(order)
|
2024-08-26 20:49:10 +00:00
|
|
|
if err != nil {
|
|
|
|
app.badRequest(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:28:11 +00:00
|
|
|
// create mail attachment
|
|
|
|
attachments := []string{
|
|
|
|
fmt.Sprintf("./invoices/%d.pdf", order.ID),
|
|
|
|
}
|
2024-08-26 20:49:10 +00:00
|
|
|
|
|
|
|
// send mail with attachment
|
2024-08-27 11:28:11 +00:00
|
|
|
err = app.SendMail("info@widgets.com", order.Email, "Your invoice", "invoice", attachments, nil)
|
|
|
|
if err != nil {
|
|
|
|
app.badRequest(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2024-08-26 20:49:10 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2024-08-26 21:06:29 +00:00
|
|
|
t := importer.ImportPage(pdf, "./pdf-templates/invoice.pdf", 1, "/MediaBox")
|
2024-08-26 20:49:10 +00:00
|
|
|
|
|
|
|
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,
|
2024-08-26 21:06:29 +00:00
|
|
|
order.CreatedAt.Format("2006-01-02"),
|
2024-08-26 20:49:10 +00:00
|
|
|
"",
|
|
|
|
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(
|
2024-08-26 21:06:29 +00:00
|
|
|
20,
|
2024-08-26 20:49:10 +00:00
|
|
|
8,
|
2024-08-26 21:06:29 +00:00
|
|
|
fmt.Sprintf("$%.2f", float32(order.Amount)/100.0),
|
2024-08-26 20:49:10 +00:00
|
|
|
"",
|
|
|
|
0,
|
|
|
|
"R",
|
|
|
|
false,
|
|
|
|
0,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
|
|
|
|
invoicePath := fmt.Sprintf("./invoices/%d.pdf", order.ID)
|
|
|
|
err := pdf.OutputFileAndClose(invoicePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|