From 6bdecd33f5c9b589aacfe4c2aa9cf70864e8fe7b Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 12 Feb 2024 14:48:28 +0100 Subject: [PATCH] compute report part 1 --- internal/server/handlers.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 9b2777e..f7aa683 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -106,7 +106,6 @@ func (s Server) handleBookingPage() echo.HandlerFunc { component := views.BookingById(b, constants.Items, constants.Platforms, constants.PaymentMethods) return s.renderTempl(c, http.StatusOK, component) - } } @@ -166,7 +165,7 @@ func (s Server) handleReportsPage() echo.HandlerFunc { func (s Server) handleComputeReport() echo.HandlerFunc { return func(c echo.Context) error { - period := c.QueryParam("period") + period := c.FormValue("period") if !u.Contains([]string{"month", "year"}, period) { return &echo.HTTPError{ Code: http.StatusBadRequest, @@ -174,7 +173,7 @@ func (s Server) handleComputeReport() echo.HandlerFunc { } } - monthStr := c.QueryParam("month") + monthStr := c.FormValue("month") month, err := strconv.Atoi(monthStr) if err != nil || month < 1 || month > 12 { return &echo.HTTPError{ @@ -183,7 +182,7 @@ func (s Server) handleComputeReport() echo.HandlerFunc { } } - yearStr := c.QueryParam("year") + yearStr := c.FormValue("year") year, err := strconv.Atoi(yearStr) if err != nil { return &echo.HTTPError{ @@ -191,13 +190,24 @@ func (s Server) handleComputeReport() echo.HandlerFunc { Message: fmt.Sprintf("%q is not a valid year", year), } } + // TODO: compute report view model + // fetch bookings with From started this month, or year + bs := make([]*booking.Booking, 0) + var startDate time.Time + var endDate time.Time + if period == "month" { + startDate = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) + endDate = time.Date(year, time.Month(month), 31, 0, 0, 0, 0, time.UTC) + } + _ = s.db.Where("\"from\" BETWEEN ? AND ?", startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).Find(&bs) + return s.renderTempl(c, http.StatusOK, views.ReportSection()) } } func (s Server) handleCreateInvoicePdf() echo.HandlerFunc { - return func(c echo.Context) error { + return func(_ echo.Context) error { data := struct { Context map[string]any `json:"context"` Path string `json:"path"`