diff --git a/README.md b/README.md index 9aaa720..939f293 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Manage your holiday rental - [x] Build the pdf invoice - [ ] Refactor the env variable calls to a Config struct with proper defaults - [x] Refactor handlers to call their dependencies instead of taking them from the Server struct +- [ ] Embed mandatory assets, css in the executable ## Built With diff --git a/config/host.go b/config/host.go new file mode 100644 index 0000000..014bac4 --- /dev/null +++ b/config/host.go @@ -0,0 +1,7 @@ +package config + +type Host struct{} + +func NewHost() *Host { + return &Host{} +} diff --git a/internal/booking/models.go b/internal/booking/models.go index 93a0de5..dd9e4a1 100644 --- a/internal/booking/models.go +++ b/internal/booking/models.go @@ -7,26 +7,26 @@ import ( ) type Booking struct { + From time.Time + To time.Time gorm.Model - Id int Name string `gorm:"column:customer_name"` PhoneNumber string - CustomerNumber int `gorm:"column:customers"` Email string - From time.Time - To time.Time Platform string - PlatformFees float64 `gorm:"type:decimal(10,2)"` Items []Item + Id int + CustomerNumber int `gorm:"column:customers"` + PlatformFees float64 `gorm:"type:decimal(10,2)"` } type Item struct { gorm.Model - Id int - BookingId int Item string - Quantity int - Price float64 `gorm:"type:decimal(10,2)"` PaymentMethod string PaymentStatus string `gorm:"default:Pending"` + Id int + BookingId int + Quantity int + Price float64 `gorm:"type:decimal(10,2)"` } diff --git a/internal/pdf/service.go b/internal/pdf/service.go index 4b8fdc6..1121012 100644 --- a/internal/pdf/service.go +++ b/internal/pdf/service.go @@ -4,11 +4,16 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "io" "net/http" "os" + "strconv" "github.com/labstack/gommon/log" + + "github.com/rjNemo/rentease/internal/booking" + u "github.com/rjNemo/underscore" ) type PdfService struct { @@ -27,13 +32,25 @@ func NewPdfService() *PdfService { } } -func (ps PdfService) BuildInvoice() error { +func (ps PdfService) BuildInvoice(b *booking.Booking) error { data := struct { Context map[string]any `json:"context"` Path string `json:"path"` ProjectId string `json:"projectId"` }{ - Context: map[string]any{}, + Context: map[string]any{ + "id": fmt.Sprintf("VFNI%04s", strconv.Itoa(b.Id)), + "name": b.Name, + "phone_number": b.PhoneNumber, + "custumers_number": b.CustomerNumber, + "platform": b.Platform, + "from": b.From.Format("Monday 02 January 2006"), + "to": b.To.Format("Monday 02 January 2006"), + "lines": b.Items, + "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, } diff --git a/internal/server/handle_pdf.go b/internal/server/handle_pdf.go index 2ba4381..fae3948 100644 --- a/internal/server/handle_pdf.go +++ b/internal/server/handle_pdf.go @@ -1,13 +1,25 @@ package server import ( + "strconv" + "github.com/labstack/echo/v4" + "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/pdf" ) -func handleCreateInvoicePdf(ps *pdf.PdfService) echo.HandlerFunc { +func handleCreateInvoicePdf(bs *booking.Service, ps *pdf.PdfService) echo.HandlerFunc { return func(c echo.Context) error { - err := ps.BuildInvoice() + idStr := c.Param("id") + c.Logger().Info(idStr) + id, err := strconv.Atoi(idStr) + if err != nil { + return err + } + + b := bs.One(id) + + err = ps.BuildInvoice(b) if err != nil { return err } diff --git a/internal/server/routes.go b/internal/server/routes.go index 59709e0..35dadc2 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -1,14 +1,13 @@ package server func (s Server) MountHandlers() { - // landing page s.Router.GET("/", handleHomePage()) s.Router.GET("/bookings", handleListBookingPage(s.bs)) s.Router.GET("/bookings/new", handleNewBookingPage()) s.Router.POST("/bookings/new", handleCreateBooking(s.bs)) s.Router.GET("/bookings/:id", handleBookingPage(s.bs)) - s.Router.POST("bookings/:id/items", handleCreateItem(s.bs)) + s.Router.POST("/bookings/:id/items", handleCreateItem(s.bs)) + s.Router.GET("/bookings/pdf/:id", handleCreateInvoicePdf(s.bs, s.ps)) s.Router.GET("/reports", handleReportsPage()) s.Router.GET("/reports/do", handleComputeReport(s.bs)) - s.Router.GET("/pdf/:id", handleCreateInvoicePdf(s.bs, s.ps)) } diff --git a/internal/server/server.go b/internal/server/server.go index 58d5db7..e76c8af 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -13,21 +13,20 @@ import ( "github.com/a-h/templ" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - "gorm.io/gorm" + "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/pdf" ) type Server struct { Router *echo.Echo - db *gorm.DB bs *booking.Service ps *pdf.PdfService addr string } -func New(bs *booking.Service, ps *pdf.PdfService) *Server { +func New(bs *booking.Service, ps *pdf.PdfService, hc *config.Host) *Server { s := &Server{ Router: NewRouter(), bs: bs, @@ -91,7 +90,6 @@ func customHTTPErrorHandler(e *echo.Echo) echo.HTTPErrorHandler { if errors.As(err, &he) { code = he.Code } - e.Logger.Error(err) errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code) if err := c.File(errorPage); err != nil { diff --git a/internal/views/booking_by_id.templ b/internal/views/booking_by_id.templ index 2c23e09..313515e 100644 --- a/internal/views/booking_by_id.templ +++ b/internal/views/booking_by_id.templ @@ -96,7 +96,7 @@ templ BookingById(booking *BookingViewModel) {

Line Items

- +
diff --git a/internal/views/booking_by_id_templ.go b/internal/views/booking_by_id_templ.go index 4d48af0..c6e980a 100644 --- a/internal/views/booking_by_id_templ.go +++ b/internal/views/booking_by_id_templ.go @@ -187,7 +187,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">

Line Items

Item
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">

Line Items

ItemQuantityPrice (€)Payment MethodPayment StatusSub-total (€)
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/views/bookings_list.templ b/internal/views/bookings_list.templ index 99804b2..045406f 100644 --- a/internal/views/bookings_list.templ +++ b/internal/views/bookings_list.templ @@ -16,7 +16,7 @@ templ ListBookings(bookings []*ListBookingsViewModel) {

Overview of the activity

-
ItemQuantityPrice (€)Payment MethodPayment StatusSub-total (€)
+
diff --git a/internal/views/bookings_list_templ.go b/internal/views/bookings_list_templ.go index 465675a..0c9e6eb 100644 --- a/internal/views/bookings_list_templ.go +++ b/internal/views/bookings_list_templ.go @@ -38,7 +38,7 @@ func ListBookings(bookings []*ListBookingsViewModel) templ.Component { templ_7745c5c3_Buffer = templ.GetBuffer() defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Bookings

Overview of the activity

ID
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Bookings

Overview of the activity

IDNameTotal (€)FromToPlatform
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/views/report_section.templ b/internal/views/report_section.templ index 6ec5816..0612a0f 100644 --- a/internal/views/report_section.templ +++ b/internal/views/report_section.templ @@ -19,7 +19,7 @@ templ ReportSection(report []*ReportViewModel) {
-
IDNameTotal (€)FromToPlatform
+
diff --git a/internal/views/report_section_templ.go b/internal/views/report_section_templ.go index d5335ba..d7b6a1f 100644 --- a/internal/views/report_section_templ.go +++ b/internal/views/report_section_templ.go @@ -34,7 +34,7 @@ func ReportSection(report []*ReportViewModel) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Your report

ID
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Your report

IDNameFromToTotal (€)PlatformPlatform Fees
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/main.go b/main.go index 743aca0..e977b8f 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "gorm.io/driver/postgres" "gorm.io/gorm" + "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/pdf" "github.com/rjNemo/rentease/internal/server" @@ -33,5 +34,5 @@ func main() { log.Fatalf("error migrating the database %s\n", err) } - server.New(booking.NewService(db), pdf.NewPdfService()).Start() + server.New(booking.NewService(db), pdf.NewPdfService(), config.NewHost()).Start() }
IDNameFromToTotal (€)PlatformPlatform Fees