From b0198f7f9a3821a47e7135bdbeed585337c6e7d2 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 22 Feb 2025 09:24:34 +0100 Subject: [PATCH] translate platform and payment method in invoices (#42) --- internal/config/host.go | 8 ++++---- internal/config/payment_method.go | 18 ++++++++++++++++++ internal/config/platform.go | 16 ++++++++++++++++ internal/driver/pdf/html.go | 3 --- internal/server/handle_bookings.go | 18 ++++++++++-------- internal/server/handle_payments.go | 4 ++-- internal/service/booking/models.go | 10 +++++----- internal/service/booking/payment.go | 4 +++- internal/service/booking/service.go | 1 - internal/service/booking/sync.go | 4 ++-- internal/view/booking_viewmodel.go | 7 +++++-- 11 files changed, 65 insertions(+), 28 deletions(-) create mode 100644 internal/config/payment_method.go create mode 100644 internal/config/platform.go diff --git a/internal/config/host.go b/internal/config/host.go index 0f9915b..51cd6c2 100644 --- a/internal/config/host.go +++ b/internal/config/host.go @@ -11,8 +11,8 @@ type Host struct { PhoneNumber string Email string InvoicePrefix string - PaymentMethods []string - Platforms []string + PaymentMethods []PaymentMethod + Platforms []Platform CustomerSeed int } @@ -38,8 +38,8 @@ func NewHost() *Host { Email: "location.villafleurie@gmail.com", CustomerSeed: 239, InvoicePrefix: "VFNI", - PaymentMethods: []string{"Card", "Cash", "Cheque", "Transfer"}, // TODO: add to DB - Platforms: []string{"Booking", "AirBnb", "TripAdvisor", "Other"}, // TODO: add to DB + PaymentMethods: []PaymentMethod{"Card", "Cash", "Cheque", "Transfer"}, // TODO: add to DB + Platforms: []Platform{"Booking", "AirBnb", "TripAdvisor", "Other"}, // TODO: add to DB Items: map[string]HostItem{ // TODO: move to DB "T2": { Name: "T2", diff --git a/internal/config/payment_method.go b/internal/config/payment_method.go new file mode 100644 index 0000000..1139823 --- /dev/null +++ b/internal/config/payment_method.go @@ -0,0 +1,18 @@ +package config + +type PaymentMethod string + +func (pm PaymentMethod) ToFrench() string { + switch pm { + case "Card": + return "Carte Bancaire" + case "Cash": + return "Espèces" + case "Cheque": + return "Chèque" + case "Transfer": + return "Virement" + default: + return string(pm) + } +} diff --git a/internal/config/platform.go b/internal/config/platform.go new file mode 100644 index 0000000..38bc64a --- /dev/null +++ b/internal/config/platform.go @@ -0,0 +1,16 @@ +package config + +type Platform string + +func (p Platform) ToFrench() string { + switch p { + case "Other": + return "Direct" + default: + return string(p) + } +} + +type Translatable interface { + ToFrench() string +} \ No newline at end of file diff --git a/internal/driver/pdf/html.go b/internal/driver/pdf/html.go index 180f862..1992313 100644 --- a/internal/driver/pdf/html.go +++ b/internal/driver/pdf/html.go @@ -6,7 +6,6 @@ import ( "os" "text/template" - "github.com/labstack/gommon/log" "github.com/rjNemo/rentease/assets" "github.com/rjNemo/rentease/internal/service/booking" ) @@ -18,7 +17,6 @@ func NewPdfClient() (*HtmlPdfClient, error) { } func (pc *HtmlPdfClient) BuildInvoice(data booking.Invoice) (string, error) { - log.Info("building invoice") tmpl, err := template.ParseFS(assets.Static, "assets/html/invoice.html") if err != nil { return "", fmt.Errorf("error parsing template: %v", err) @@ -35,7 +33,6 @@ func (pc *HtmlPdfClient) BuildInvoice(data booking.Invoice) (string, error) { return "", fmt.Errorf("error writing HTML file: %v", err) } - log.Info("building invoice") return outputPath, nil } diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index e62da8f..27a1f07 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -69,7 +69,9 @@ func handleBookingList(bs *booking.Service) echo.HandlerFunc { func handleBookingCreatePage(hc *config.Host) echo.HandlerFunc { return func(c echo.Context) error { - return renderTempl(c, http.StatusOK, view.NewBooking(hc.Platforms)) + return renderTempl(c, http.StatusOK, view.NewBooking(u.Map(hc.Platforms, func(p config.Platform) string { + return string(p) + }))) } } @@ -130,7 +132,7 @@ func handleBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { Email: b.Email, From: b.From.Format(time.DateOnly), To: b.To.Format(time.DateOnly), - Platform: b.Platform, + Platform: string(b.Platform), ExternalId: eid, Canceled: b.Canceled, PlatformFees: strconv.FormatFloat(b.PlatformFees, 'f', 2, 64), @@ -153,7 +155,7 @@ func handleBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { Payments: u.Map(b.Payments, func(p booking.Payment) view.PaymentViewModel { return view.PaymentViewModel{ Amount: strconv.FormatFloat(p.Amount, 'f', 2, 64), - PaymentMethod: p.PaymentMethod, + PaymentMethod: string(p.PaymentMethod), PaymentUrl: fmt.Sprintf("%s/%d", constant.RoutePayment, p.ID), } }), @@ -161,7 +163,7 @@ func handleBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { 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), - Platforms: hc.Platforms, + Platforms: u.Map(hc.Platforms, func(p config.Platform) string { return string(p) }), ItemList: u.OrderBy(func(items map[string]config.HostItem) (out []string) { // TODO: return the full item to prefill the form for _, item := range items { out = append(out, item.Name) @@ -217,9 +219,9 @@ func handleBookingUpdate(bs *booking.Service, hc *config.Host) echo.HandlerFunc From: b.From.Format(time.DateOnly), To: b.To.Format(time.DateOnly), Canceled: b.Canceled, - Platform: b.Platform, + Platform: string(b.Platform), ExternalId: *b.ExternalId, - Platforms: hc.Platforms, + Platforms: u.Map(hc.Platforms, func(p config.Platform) string { return string(p) }), PlatformFees: strconv.FormatFloat(b.PlatformFees, 'f', 2, 64), PaymentMethods: hc.PaymentMethods, Url: templ.EscapeString(fmt.Sprintf("%s/%d", constant.RouteBooking, b.Id)), @@ -279,7 +281,7 @@ func handleCreateItem(bs *booking.Service, hc *config.Host) echo.HandlerFunc { return fmt.Errorf("invalid item name %q", ni.Item) } - newItems := bs.CreateItem(b.Id, itm, ni.Quantity, ni.Price, ni.PaymentMethod, b.CustomerNumber, b.Platform) + newItems := bs.CreateItem(b.Id, itm, ni.Quantity, ni.Price, ni.PaymentMethod, b.CustomerNumber, string(b.Platform)) // TODO: fix the calendar integration // if err = cs.Create( @@ -377,7 +379,7 @@ func handlePaymentUpdate(bs *booking.Service) echo.HandlerFunc { return renderTempl(c, http.StatusOK, view.PaymentLine(&view.PaymentViewModel{ Amount: strconv.FormatFloat(p.Amount, 'f', 2, 64), - PaymentMethod: p.PaymentMethod, + PaymentMethod: string(p.PaymentMethod), PaymentUrl: fmt.Sprintf("%s/%d", constant.RoutePayment, p.ID), })) } diff --git a/internal/server/handle_payments.go b/internal/server/handle_payments.go index 90576a0..6d36245 100644 --- a/internal/server/handle_payments.go +++ b/internal/server/handle_payments.go @@ -44,7 +44,7 @@ func handleCreatePayment(bs *booking.Service) echo.HandlerFunc { u.Map(nb.Payments, func(p booking.Payment) *view.PaymentViewModel { return &view.PaymentViewModel{ Amount: strconv.FormatFloat(p.Amount, 'f', 2, 64), - PaymentMethod: p.PaymentMethod, + PaymentMethod: string(p.PaymentMethod), PaymentUrl: fmt.Sprintf("%s/%d", constant.RoutePayment, p.ID), } }), @@ -63,7 +63,7 @@ func handlePaymentForm(bs *booking.Service) echo.HandlerFunc { p := bs.OnePayment(id) form := view.PaymentForm(&view.PaymentViewModel{ Amount: strconv.FormatFloat(p.Amount, 'f', 2, 64), - PaymentMethod: p.PaymentMethod, + PaymentMethod: string(p.PaymentMethod), PaymentUrl: fmt.Sprintf("%s/%d", constant.RoutePayment, p.ID), }) return renderTempl(c, http.StatusOK, form) diff --git a/internal/service/booking/models.go b/internal/service/booking/models.go index eb1d735..0524afe 100644 --- a/internal/service/booking/models.go +++ b/internal/service/booking/models.go @@ -18,7 +18,7 @@ type Booking struct { Name string `gorm:"column:customer_name"` PhoneNumber string Email string - Platform string + Platform config.Platform ExternalId *string `gorm:"uniqueIndex:booking_external_id"` Items []Item Payments []Payment `gorm:"foreignKey:BookingID"` @@ -39,7 +39,7 @@ func NewBooking(from, to time.Time, name, phoneNumber, email, platform string, PhoneNumber: phoneNumber, CustomerNumber: customerNumber, Email: email, - Platform: platform, + Platform: config.Platform(platform), PlatformFees: platformFees, ExternalId: externalId, } @@ -62,7 +62,7 @@ func (b Booking) ToInvoice(hc *config.Host) Invoice { Name: b.Name, PhoneNumber: b.PhoneNumber, CustomersNumber: b.CustomerNumber, - Platform: b.Platform, + Platform: b.Platform.ToFrench(), ID: b.InvoiceNumber(hc), From: b.From.Format("02/01/2006"), To: b.To.Format("02/01/2006"), @@ -80,7 +80,7 @@ func (b Booking) ToInvoice(hc *config.Host) Invoice { Payments: u.Map(b.Payments, func(p Payment) PaymentLine { return PaymentLine{ Date: p.CreatedAt.Format("02/01/2006"), - Method: p.PaymentMethod, + Method: p.PaymentMethod.ToFrench(), Amount: strconv.FormatFloat(p.Amount, 'f', 2, 64), } }), @@ -129,5 +129,5 @@ type Payment struct { BookingID uint `gorm:"not null;index"` Booking Booking `gorm:"foreignKey:BookingID;constraint:OnDelete:CASCADE"` Amount float64 - PaymentMethod string + PaymentMethod config.PaymentMethod } diff --git a/internal/service/booking/payment.go b/internal/service/booking/payment.go index edbeaae..8cc006b 100644 --- a/internal/service/booking/payment.go +++ b/internal/service/booking/payment.go @@ -2,6 +2,8 @@ package booking import ( "log" + + "github.com/rjNemo/rentease/internal/config" ) func (bs Service) OnePayment(id int) *Payment { @@ -16,7 +18,7 @@ func (bs Service) CreatePayment(bid int, amount float64, paymentMethod string) ( p, err := bs.store.CreatePayment(&Payment{ BookingID: uint(bid), Amount: amount, - PaymentMethod: paymentMethod, + PaymentMethod: config.PaymentMethod(paymentMethod), }) if err != nil { return nil, err diff --git a/internal/service/booking/service.go b/internal/service/booking/service.go index 6f4dbf5..3f22020 100644 --- a/internal/service/booking/service.go +++ b/internal/service/booking/service.go @@ -101,6 +101,5 @@ func (bs Service) Cancel(id int) { } func (bs Service) BuildInvoice(b *Booking, hc *config.Host) (string, error) { - log.Println("Build invoice") return bs.pdf.BuildInvoice(b.ToInvoice(hc)) } diff --git a/internal/service/booking/sync.go b/internal/service/booking/sync.go index 8d0b96c..575982e 100644 --- a/internal/service/booking/sync.go +++ b/internal/service/booking/sync.go @@ -11,10 +11,10 @@ func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { } itm := b.Items[0] - b = bs.Create(b.From, b.To, b.Name, b.PhoneNumber, b.Email, b.Platform, b.CustomerNumber, b.PlatformFees, b.ExternalId) + b = bs.Create(b.From, b.To, b.Name, b.PhoneNumber, b.Email, string(b.Platform), b.CustomerNumber, b.PlatformFees, b.ExternalId) if item, ok := config.NewHost().Items[itm.Item]; ok { - bs.CreateItem(b.Id, item, itm.Quantity, itm.Price, itm.PaymentMethod, b.CustomerNumber, b.Platform) + bs.CreateItem(b.Id, item, itm.Quantity, itm.Price, itm.PaymentMethod, b.CustomerNumber, string(b.Platform)) } return b, nil diff --git a/internal/view/booking_viewmodel.go b/internal/view/booking_viewmodel.go index cd1c148..1c1bf82 100644 --- a/internal/view/booking_viewmodel.go +++ b/internal/view/booking_viewmodel.go @@ -1,6 +1,9 @@ package view -import "github.com/a-h/templ" +import ( + "github.com/a-h/templ" + "github.com/rjNemo/rentease/internal/config" +) type BookingViewModel struct { Id string @@ -17,7 +20,7 @@ type BookingViewModel struct { PlatformFees string Items ItemListViewModel ItemList []string - PaymentMethods []string + PaymentMethods []config.PaymentMethod Url string PdfUrl templ.SafeURL PaymentUrl string