diff --git a/internal/pdf/service.go b/internal/pdf/service.go index e0152bd..ef6fb42 100644 --- a/internal/pdf/service.go +++ b/internal/pdf/service.go @@ -17,24 +17,27 @@ import ( ) type PdfService struct { - invoicePath string - projectId string - url string - apiKey string + path string + invoiceId string + reportId string + url string + apiKey string } func NewPdfService() (*PdfService, error) { pid := os.Getenv("HTMLDOCS_PROJECT_ID") + rid := os.Getenv("HTMLDOCS_REPORT_PROJECT_ID") url := os.Getenv("HTMLDOCS_URL") key := os.Getenv("HTMLDOCS_KEY") - if pid == "" || url == "" || key == "" { + if pid == "" || rid == "" || url == "" || key == "" { return nil, errors.New("error building Pdf service. Verify your env variables") } return &PdfService{ - invoicePath: "index.html", - projectId: pid, - url: url, - apiKey: key, + path: "index.html", + invoiceId: pid, + reportId: rid, + url: url, + apiKey: key, }, nil } @@ -72,8 +75,8 @@ func (ps PdfService) BuildInvoice(b *booking.Booking, hc *config.Host) error { return sum + i.Price*float64(i.Quantity) }, 0.0), 'f', 2, 64), }, - Path: ps.invoicePath, - ProjectId: ps.projectId, + Path: ps.path, + ProjectId: ps.invoiceId, } payload, err := json.Marshal(data) @@ -85,44 +88,46 @@ func (ps PdfService) BuildInvoice(b *booking.Booking, hc *config.Host) error { return ps.sendData(payload) } -func (ps PdfService) BuildReport(r *booking.Report) error { - // data := struct { - // Context map[string]any `w` - // Path string `json:"path"` - // ProjectId string `json:"projectId"` - // }{ - // Context: map[string]any{ - // "id": r.InvoiceNumber(hc), - // "name": r.Name, - // "phone_number": r.PhoneNumber, - // "customers_number": r.CustomerNumber, - // "platform": r.Platform, - // "from": r.From.Format("02/01/2006"), - // "to": r.To.Format("02/01/2006"), - // "lines": u.Map(b.Items, func(i booking.Item) map[string]any { - // return map[string]any{ - // "name": i.Item, - // "quantity": i.Quantity, - // "price": i.Price, - // "total": i.Price * float64(i.Quantity), - // } - // }), - // "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, - // } +func (ps PdfService) BuildReport(r *booking.Report, period string, month, year int) error { + data := struct { + Context map[string]any `json:"context"` + Path string `json:"path"` + ProjectId string `json:"projectId"` + }{ + Context: map[string]any{ + "month": month, + "year": year, + "total": r.Total, + "platform_fees": r.PlatformFees, + "fee": r.Fee, + "profit": r.Profit, + "card_total": r.CardTotal, + "booking_fees": r.BookingFees, + "lines": u.Map(r.Lines, func(l *booking.Line) map[string]any { + return map[string]any{ + "id": l.Id, + "name": l.CustomerName, + "from": l.From.Format("2006-01-02"), + "to": l.To.Format("2006-01-02"), + "total": l.Total, + "platform": l.Platform, + "platform_fees": l.PlatformFees, + "fee": l.Fee(), + "profit": l.Profit(), + } + }), + }, + Path: ps.path, + ProjectId: ps.reportId, + } - // payload, err := json.Marshal(data) - // if err != nil { - // log.Warnf("Error marshalling JSON:", err) - // return err - // } - // - // return ps.sendData(payload) - return nil + payload, err := json.Marshal(data) + if err != nil { + log.Warnf("Error marshalling JSON:", err) + return err + } + + return ps.sendData(payload) } func (ps PdfService) sendData(payload []byte) error { diff --git a/internal/server/handle_pdf.go b/internal/server/handle_pdf.go index 9af0917..6148c6d 100644 --- a/internal/server/handle_pdf.go +++ b/internal/server/handle_pdf.go @@ -59,7 +59,7 @@ func handleCreateReportPdf(bs *booking.Service, ps *pdf.PdfService) echo.Handler } report := bs.BuildReport(period, month, year) - err = ps.BuildReport(report, month, year) + err = ps.BuildReport(report, period, month, year) if err != nil { return err }