new rport builder

This commit is contained in:
Ruidy 2024-03-03 14:25:36 +01:00
parent 575dc5ad7b
commit 96710d033a
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 54 additions and 49 deletions

View file

@ -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 {

View file

@ -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
}