mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
115 lines
3.1 KiB
Go
115 lines
3.1 KiB
Go
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, month, 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))
|
|
}
|
|
}
|