package server import ( "fmt" "net/http" "strconv" "time" "github.com/a-h/templ" "github.com/labstack/echo/v4" "github.com/rjNemo/rentease/constants" "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/views" u "github.com/rjNemo/underscore" ) func handleReportsPage() echo.HandlerFunc { return func(c echo.Context) error { period := c.QueryParam("period") if !u.Contains([]string{"month", "year"}, period) { period = "month" } monthStr := c.QueryParam("month") month, err := strconv.Atoi(monthStr) if err != nil || month < 1 || month > 12 { month = int(time.Now().Month()) } yearStr := c.QueryParam("year") _, err = strconv.Atoi(yearStr) if err != nil { yearStr = time.Now().Format("2006") } return renderTempl(c, http.StatusOK, views.Reports(constants.Months, month, yearStr)) } } func handleComputeReport(bs *booking.Service) echo.HandlerFunc { return func(c echo.Context) error { period := c.FormValue("period") if !u.Contains([]string{"month", "year"}, period) { return &echo.HTTPError{ Code: http.StatusBadRequest, Message: fmt.Sprintf("%q is not a valid period", period), } } monthStr := c.FormValue("month") month, err := strconv.Atoi(monthStr) if err != nil || month < 1 || month > 12 { return &echo.HTTPError{ Code: http.StatusBadRequest, Message: fmt.Sprintf("%q is not a valid month", month), } } yearStr := c.FormValue("year") year, err := strconv.Atoi(yearStr) if err != nil { return &echo.HTTPError{ Code: http.StatusBadRequest, Message: fmt.Sprintf("%q is not a valid year", year), } } res := bs.BuildReport(period, month, year) reportVm := u.Map(res, func(r *booking.Line) *views.ReportViewModel { return &views.ReportViewModel{ Id: r.Id, Url: templ.SafeURL(fmt.Sprintf("%s/%s", constants.RouteBooking, r.Id)), Total: strconv.FormatFloat(r.Total, 'f', 2, 64), CustomerName: r.CustomerName, From: r.From.Format("2006-01-02"), To: r.To.Format("2006-01-02"), Platform: r.Platform, PlatformFees: strconv.FormatFloat(r.PlatformFees, 'f', 2, 64), } }) return renderTempl(c, http.StatusOK, views.ReportSection(reportVm)) } }