mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +00:00
public handler
This commit is contained in:
parent
1f86cbf3ed
commit
9bbd11588b
4 changed files with 132 additions and 107 deletions
115
internal/server/handle_reports.go
Normal file
115
internal/server/handle_reports.go
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
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/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, yearStr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Server) handleComputeReport() 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Result struct {
|
||||||
|
From time.Time
|
||||||
|
To time.Time
|
||||||
|
Id string
|
||||||
|
CustomerName string
|
||||||
|
Platform string
|
||||||
|
Total float64
|
||||||
|
PlatformFees float64
|
||||||
|
}
|
||||||
|
res := make([]*Result, 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)
|
||||||
|
} else {
|
||||||
|
startDate = time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
endDate = time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.db.
|
||||||
|
Raw(`
|
||||||
|
select id, customer_name, "from", "to", platform, total, platform_fees
|
||||||
|
from (select sum(price) as total, booking_id
|
||||||
|
from bookings
|
||||||
|
join items on bookings.id = items.booking_id
|
||||||
|
where "from" between ? and ?
|
||||||
|
group by booking_id) as sbi,
|
||||||
|
(select * from bookings) as b
|
||||||
|
where sbi.booking_id = b.id;
|
||||||
|
`,
|
||||||
|
startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).
|
||||||
|
Scan(&res)
|
||||||
|
|
||||||
|
reportVm := u.Map(res, func(r *Result) *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 s.renderTempl(c, http.StatusOK, views.ReportSection(reportVm))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,10 +18,10 @@ import (
|
||||||
myTime "github.com/rjNemo/rentease/pkg/time"
|
myTime "github.com/rjNemo/rentease/pkg/time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s Server) handleHomePage() echo.HandlerFunc {
|
func handleHomePage() echo.HandlerFunc {
|
||||||
return func(ctx echo.Context) error {
|
return func(ctx echo.Context) error {
|
||||||
component := views.Index()
|
component := views.Index()
|
||||||
return s.renderTempl(ctx, http.StatusOK, component)
|
return renderTempl(ctx, http.StatusOK, component)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,9 +145,9 @@ func (s Server) handleCreateItem() echo.HandlerFunc {
|
||||||
|
|
||||||
type NewItem struct {
|
type NewItem struct {
|
||||||
Item string `form:"item"`
|
Item string `form:"item"`
|
||||||
|
PaymentMethod string `form:"method"`
|
||||||
Quantity int `form:"quantity"`
|
Quantity int `form:"quantity"`
|
||||||
Price float64 `form:"price"`
|
Price float64 `form:"price"`
|
||||||
PaymentMethod string `form:"method"`
|
|
||||||
}
|
}
|
||||||
ni := new(NewItem)
|
ni := new(NewItem)
|
||||||
if err := c.Bind(ni); err != nil {
|
if err := c.Bind(ni); err != nil {
|
||||||
|
|
@ -167,107 +167,6 @@ func (s Server) handleCreateItem() echo.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) 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 s.renderTempl(c, http.StatusOK, views.Reports(constants.Months, yearStr))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Server) handleComputeReport() 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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Result struct {
|
|
||||||
From time.Time
|
|
||||||
To time.Time
|
|
||||||
Id string
|
|
||||||
CustomerName string
|
|
||||||
Platform string
|
|
||||||
Total float64
|
|
||||||
PlatformFees float64
|
|
||||||
}
|
|
||||||
res := make([]*Result, 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)
|
|
||||||
} else {
|
|
||||||
startDate = time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
|
|
||||||
endDate = time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.db.
|
|
||||||
Raw(`
|
|
||||||
select id, customer_name, "from", "to", platform, total, platform_fees
|
|
||||||
from (select sum(price) as total, booking_id
|
|
||||||
from bookings
|
|
||||||
join items on bookings.id = items.booking_id
|
|
||||||
where "from" between ? and ?
|
|
||||||
group by booking_id) as sbi,
|
|
||||||
(select * from bookings) as b
|
|
||||||
where sbi.booking_id = b.id;
|
|
||||||
`,
|
|
||||||
startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).
|
|
||||||
Scan(&res)
|
|
||||||
|
|
||||||
reportVm := u.Map(res, func(r *Result) *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 s.renderTempl(c, http.StatusOK, views.ReportSection(reportVm))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleCreateInvoicePdf(ps *pdf.PdfService) echo.HandlerFunc {
|
func handleCreateInvoicePdf(ps *pdf.PdfService) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
err := ps.BuildInvoice()
|
err := ps.BuildInvoice()
|
||||||
|
|
|
||||||
|
|
@ -24,13 +24,13 @@ func (s Server) MountHandlers() {
|
||||||
// static assets
|
// static assets
|
||||||
s.Router.Static("/static", "assets")
|
s.Router.Static("/static", "assets")
|
||||||
// landing page
|
// landing page
|
||||||
s.Router.GET("/", s.handleHomePage())
|
s.Router.GET("/", handleHomePage())
|
||||||
s.Router.GET(constants.RouteBooking, s.handleListBookingPage())
|
s.Router.GET(constants.RouteBooking, s.handleListBookingPage())
|
||||||
s.Router.GET(constants.RouteNewBooking, s.handleNewBookingPage())
|
s.Router.GET(constants.RouteNewBooking, s.handleNewBookingPage())
|
||||||
s.Router.POST(constants.RouteNewBooking, s.handleCreateBooking())
|
s.Router.POST(constants.RouteNewBooking, s.handleCreateBooking())
|
||||||
s.Router.GET(fmt.Sprintf("%s/:id", constants.RouteBooking), s.handleBookingPage())
|
s.Router.GET(fmt.Sprintf("%s/:id", constants.RouteBooking), s.handleBookingPage())
|
||||||
s.Router.POST(fmt.Sprintf("%s/:id/items", constants.RouteBooking), s.handleCreateItem())
|
s.Router.POST(fmt.Sprintf("%s/:id/items", constants.RouteBooking), s.handleCreateItem())
|
||||||
s.Router.GET(constants.RouteReports, s.handleReportsPage())
|
s.Router.GET("/reports", handleReportsPage())
|
||||||
s.Router.GET(fmt.Sprintf("%s/do", constants.RouteReports), s.handleComputeReport())
|
s.Router.GET("/reports/do", s.handleComputeReport())
|
||||||
s.Router.GET("/pdf", handleCreateInvoicePdf(s.ps))
|
s.Router.GET("/pdf", handleCreateInvoicePdf(s.ps))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,17 @@ func (s Server) Start() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func renderTempl(c echo.Context, status int, t templ.Component) error {
|
||||||
|
c.Response().Writer.WriteHeader(status)
|
||||||
|
|
||||||
|
err := t.Render(context.Background(), c.Response().Writer)
|
||||||
|
if err != nil {
|
||||||
|
return c.String(http.StatusInternalServerError, "failed to render response template")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s Server) renderTempl(c echo.Context, status int, t templ.Component) error {
|
func (s Server) renderTempl(c echo.Context, status int, t templ.Component) error {
|
||||||
c.Response().Writer.WriteHeader(status)
|
c.Response().Writer.WriteHeader(status)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue