mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
translate platform and payment method in invoices (#42)
This commit is contained in:
parent
7d909f34e3
commit
b0198f7f9a
11 changed files with 65 additions and 28 deletions
|
|
@ -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",
|
||||
|
|
|
|||
18
internal/config/payment_method.go
Normal file
18
internal/config/payment_method.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
16
internal/config/platform.go
Normal file
16
internal/config/platform.go
Normal file
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue