mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
126 lines
2.7 KiB
Go
126 lines
2.7 KiB
Go
package pdf
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/labstack/gommon/log"
|
|
|
|
"github.com/rjNemo/rentease/internal/booking"
|
|
u "github.com/rjNemo/underscore"
|
|
)
|
|
|
|
type PdfService struct {
|
|
invoicePath string
|
|
projectId string
|
|
url string
|
|
apiKey string
|
|
}
|
|
|
|
func NewPdfService() *PdfService {
|
|
return &PdfService{
|
|
invoicePath: "index.html",
|
|
projectId: os.Getenv("HTMLDOCS_PROJECT_ID"),
|
|
url: os.Getenv("HTMLDOCS_URL"),
|
|
apiKey: os.Getenv("HTMLDOCS_KEY"),
|
|
}
|
|
}
|
|
|
|
func (ps PdfService) BuildInvoice(b *booking.Booking) error {
|
|
data := struct {
|
|
Context map[string]any `json:"context"`
|
|
Path string `json:"path"`
|
|
ProjectId string `json:"projectId"`
|
|
}{
|
|
Context: map[string]any{
|
|
"id": fmt.Sprintf("VFNI%04s", strconv.Itoa(b.Id)),
|
|
"name": b.Name,
|
|
"phone_number": b.PhoneNumber,
|
|
"custumers_number": b.CustomerNumber,
|
|
"platform": b.Platform,
|
|
"from": b.From.Format("Monday 02 January 2006"),
|
|
"to": b.To.Format("Monday 02 January 2006"),
|
|
"lines": b.Items,
|
|
"total": strconv.FormatFloat(u.Reduce(b.Items, func(i booking.Item, sum float64) float64 {
|
|
return sum + i.Price*float64(i.Quantity)
|
|
}, 0.0), 'f', 2, 64),
|
|
},
|
|
Path: ps.invoicePath,
|
|
ProjectId: ps.projectId,
|
|
}
|
|
|
|
payload, err := json.Marshal(data)
|
|
if err != nil {
|
|
log.Warnf("Error marshalling JSON:", err)
|
|
return err
|
|
}
|
|
|
|
// build request to third-party API
|
|
req, err := http.NewRequest("POST", ps.url, bytes.NewBuffer(payload))
|
|
if err != nil {
|
|
log.Warnf("Error creating request:", err)
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+ps.apiKey)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Warnf("Error sending request:", err)
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
res := new(struct {
|
|
Url string `json:"url"`
|
|
Error string `json:"error"`
|
|
})
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = json.Unmarshal(body, res)
|
|
if err != nil {
|
|
log.Warnf("error decoding response: %s", err)
|
|
return err
|
|
}
|
|
|
|
if res.Error != "" {
|
|
log.Warnf("error building pdf file %s", err)
|
|
return errors.New(res.Error)
|
|
}
|
|
|
|
resp, err = http.Get(res.Url)
|
|
if err != nil {
|
|
log.Warnf("Error retrieving file")
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
file, err := os.Create("tmp.pdf")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
body, err = io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = file.Write(body)
|
|
if err != nil {
|
|
log.Error("Error copying file content")
|
|
return err
|
|
}
|
|
return nil
|
|
}
|