From 40edb8c6f0c3036cef1517d8f734d1d26198d3cb Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 22 Feb 2025 12:25:08 +0100 Subject: [PATCH] update report template --- assets/assets/html/report.html | 172 ++++++++++++++++++---------- internal/config/platform.go | 2 +- internal/constant/months.go | 21 +++- internal/driver/pdf/html.go | 22 +++- internal/server/handle_pdf.go | 4 +- internal/server/handle_reports.go | 4 +- internal/service/booking/report.go | 75 ++++++++---- internal/service/booking/service.go | 2 +- 8 files changed, 210 insertions(+), 92 deletions(-) diff --git a/assets/assets/html/report.html b/assets/assets/html/report.html index a5cb858..4370204 100644 --- a/assets/assets/html/report.html +++ b/assets/assets/html/report.html @@ -1,68 +1,118 @@ - + - + - -

Bilan réservations

-

{{month}} {{ year }}

-
- - - - - - - - - - - - - - - - {% for row in lines %} - - - - - - - - - - - - {% endfor %} - - - - - - - - - - - - - - - - - - - - - - - - - -
#NomDuAuRevenue (€)PlatformeCommission (€)NemoImmo (€)Profit (€)
{{ row.Id }} {{ row.CustomerName }}{{ row.From }}{{ row.To }}{{ row.Total }}{{ row.Platform }}{{ row.PlatformFees }}{{ row.Fee }}{{ row.Profit }}
Totaux{{ total }}{{ platformFees }}{{ fee }}{{ profit }}
dont CB :{{ cardTotal }}Frais Booking :{{ bookingFees }}
-
+ +
+ Rapport Mensuel - {{ .Month }} {{ .Year }} +
+ +
+
Revenu total : {{ .Total }} €
+
dont paiements par Carte Bancaire : {{ .CardTotal }} €
+
Frais Booking.com : {{ .BookingFees }} €
+
Frais NemoImmo : {{ .Fee }} €
+
Bénéfice : {{ .Profit }} €
+
+ + + + + + + + + + + + + + + + + {{ range .Lines }} + + + + + + + + + + + + {{ end }} + + + + + + + + + + + +
N° FactureNom ClientDuAuRevenu (€)PlateformeFrais Plateforme (€)Frais NemoImmo (€)Bénéfice (€)
{{ .InvoiceNumber }}{{ .CustomerName }}{{ .From }}{{ .To }}{{ .Total }}{{ .Platform }}{{ .PlatformFees }}{{ .Fee }}{{ .Profit }}
Totaux{{ .Total }}{{ .PlatformFees }}{{ .Fee }}{{ .Profit }}
+ diff --git a/internal/config/platform.go b/internal/config/platform.go index 38bc64a..cc5bfa4 100644 --- a/internal/config/platform.go +++ b/internal/config/platform.go @@ -13,4 +13,4 @@ func (p Platform) ToFrench() string { type Translatable interface { ToFrench() string -} \ No newline at end of file +} diff --git a/internal/constant/months.go b/internal/constant/months.go index 2f0b854..f3020c0 100644 --- a/internal/constant/months.go +++ b/internal/constant/months.go @@ -1,6 +1,8 @@ package constant -var Months = []string{ +type Month string + +var Months = []Month{ "January", "February", "March", @@ -14,3 +16,20 @@ var Months = []string{ "November", "December", } + +func (m Month) ToFrench() string { + return map[Month]string{ + "January": "Janvier", + "February": "Février", + "March": "Mars", + "April": "Avril", + "May": "Mai", + "June": "Juin", + "July": "Juillet", + "August": "Août", + "September": "Septembre", + "October": "Octobre", + "November": "Novembre", + "December": "Décembre", + }[m] +} diff --git a/internal/driver/pdf/html.go b/internal/driver/pdf/html.go index 1992313..893b872 100644 --- a/internal/driver/pdf/html.go +++ b/internal/driver/pdf/html.go @@ -3,6 +3,7 @@ package pdf import ( "bytes" "fmt" + "log" "os" "text/template" @@ -36,6 +37,23 @@ func (pc *HtmlPdfClient) BuildInvoice(data booking.Invoice) (string, error) { return outputPath, nil } -func (pc *HtmlPdfClient) BuildReport(context map[string]any, period string, month int, year int) error { - panic("unimplemented") +func (pc *HtmlPdfClient) BuildReport(report booking.ReportData, period string, month int, year int) (string, error) { + tmpl, err := template.ParseFS(assets.Static, "assets/html/report.html") + if err != nil { + return "", fmt.Errorf("error parsing template: %v", err) + } + + var buf bytes.Buffer + if err := tmpl.Execute(&buf, report); err != nil { + log.Printf("err: %+v\n", err) + return "", fmt.Errorf("error executing template: %v", err) + } + + outputPath := fmt.Sprintf("report-%s-%d-%d.html", period, month, year) + if err := os.WriteFile(outputPath, buf.Bytes(), 0644); err != nil { + log.Printf("err: %+v\n", err) + return "", fmt.Errorf("error writing HTML file: %v", err) + } + + return outputPath, nil } diff --git a/internal/server/handle_pdf.go b/internal/server/handle_pdf.go index e1f5e6e..dce1d0a 100644 --- a/internal/server/handle_pdf.go +++ b/internal/server/handle_pdf.go @@ -62,11 +62,11 @@ func handlePdfCreateReport(bs *booking.Service) echo.HandlerFunc { } report := bs.Report(period, month, year) - err = bs.BuildReport(report, period, month, year) + filePath,err := bs.BuildReport(report, period, month, year) if err != nil { return err } - return c.Attachment("tmp.pdf", fmt.Sprintf("VF-%02d-report.pdf", month)) + return c.File(filePath) } } diff --git a/internal/server/handle_reports.go b/internal/server/handle_reports.go index 48e7615..385b152 100644 --- a/internal/server/handle_reports.go +++ b/internal/server/handle_reports.go @@ -29,7 +29,9 @@ func handleReportsPage() echo.HandlerFunc { if err != nil { yearStr = time.Now().Format("2006") } - return renderTempl(c, http.StatusOK, view.Reports(constant.Months, month, yearStr)) + return renderTempl(c, http.StatusOK, view.Reports(u.Map(constant.Months, func(m constant.Month) string { + return string(m) + }), month, yearStr)) } } diff --git a/internal/service/booking/report.go b/internal/service/booking/report.go index df09ca6..632c5f2 100644 --- a/internal/service/booking/report.go +++ b/internal/service/booking/report.go @@ -6,8 +6,13 @@ import ( "time" u "github.com/rjNemo/underscore" + + "github.com/rjNemo/rentease/internal/config" + "github.com/rjNemo/rentease/internal/constant" ) +var hc = config.NewHost() + type Report struct { Lines []*Line Total float64 @@ -18,27 +23,51 @@ type Report struct { BookingFees float64 } -func (r Report) Serialize(month, year int) map[string]any { - return map[string]any{ - "month": month, - "year": year, - "total": strconv.FormatFloat(r.Total, 'f', 2, 64), - "platform_fees": strconv.FormatFloat(r.PlatformFees, 'f', 2, 64), - "fee": strconv.FormatFloat(r.Fee, 'f', 2, 64), - "profit": strconv.FormatFloat(r.Profit, 'f', 2, 64), - "card_total": strconv.FormatFloat(r.CardTotal, 'f', 2, 64), - "booking_fees": strconv.FormatFloat(r.BookingFees, 'f', 2, 64), - "lines": u.Map(r.Lines, func(l *Line) map[string]any { - return map[string]any{ - "id": l.Id, - "name": l.CustomerName, - "from": l.From.Format("02/01/2006"), - "to": l.To.Format("02/01/2006"), - "total": strconv.FormatFloat(l.Total, 'f', 2, 64), - "platform": l.Platform, - "platform_fees": strconv.FormatFloat(l.PlatformFees, 'f', 2, 64), - "fee": strconv.FormatFloat(l.Fee(), 'f', 2, 64), - "profit": strconv.FormatFloat(l.Profit(), 'f', 2, 64), +type ReportData struct { + Month string + Year int + Total string + PlatformFees string + Fee string + Profit string + CardTotal string + BookingFees string + Lines []ReportLine +} + +type ReportLine struct { + InvoiceNumber string + CustomerName string + From string + To string + Total string + Platform string + PlatformFees string + Fee string + Profit string +} + +func (r Report) Data(month, year int) ReportData { + return ReportData{ + Month: constant.Months[month-1].ToFrench(), + Year: year, + Total: strconv.FormatFloat(r.Total, 'f', 2, 64), + PlatformFees: strconv.FormatFloat(r.PlatformFees, 'f', 2, 64), + Fee: strconv.FormatFloat(r.Fee, 'f', 2, 64), + Profit: strconv.FormatFloat(r.Profit, 'f', 2, 64), + CardTotal: strconv.FormatFloat(r.CardTotal, 'f', 2, 64), + BookingFees: strconv.FormatFloat(r.BookingFees, 'f', 2, 64), + Lines: u.Map(r.Lines, func(l *Line) ReportLine { + return ReportLine{ + InvoiceNumber: l.InvoiceNumber(hc), + CustomerName: l.CustomerName, + From: l.From.Format("02/01/2006"), + To: l.To.Format("02/01/2006"), + Total: strconv.FormatFloat(l.Total, 'f', 2, 64), + Platform: l.Platform, + PlatformFees: strconv.FormatFloat(l.PlatformFees, 'f', 2, 64), + Fee: strconv.FormatFloat(l.Fee(), 'f', 2, 64), + Profit: strconv.FormatFloat(l.Profit(), 'f', 2, 64), } }), } @@ -82,6 +111,6 @@ func (bs Service) Report(period string, month, year int) *Report { } } -func (bs Service) BuildReport(report *Report, period string, month, year int) error { - return bs.pdf.BuildReport(report.Serialize(month, year), period, month, year) +func (bs Service) BuildReport(report *Report, period string, month, year int) (string, error) { + return bs.pdf.BuildReport(report.Data(month, year), period, month, year) } diff --git a/internal/service/booking/service.go b/internal/service/booking/service.go index 3f22020..a2fe21f 100644 --- a/internal/service/booking/service.go +++ b/internal/service/booking/service.go @@ -31,7 +31,7 @@ type Store interface { type PdfClient interface { BuildInvoice(invoice Invoice) (string, error) - BuildReport(context map[string]any, period string, month, year int) error + BuildReport(report ReportData, period string, month, year int) (string, error) } type CalendarClient interface {