mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/rjNemo/rentease/config"
|
|
"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, hc *config.Host) 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),
|
|
}
|
|
}
|
|
|
|
r := bs.BuildReport(period, month, year)
|
|
|
|
reportVm := &views.ReportViewModel{
|
|
Total: strconv.FormatFloat(r.Total, 'f', 2, 64),
|
|
PlatformFees: strconv.FormatFloat(r.PlatformFees, 'f', 2, 64),
|
|
Fee: strconv.FormatFloat(r.Fee, 'f', 2, 64),
|
|
Profit: strconv.FormatFloat(r.Profit, 'f', 2, 64),
|
|
CardTotal: strconv.FormatFloat(r.CardTotal, 'f', 2, 64),
|
|
BookingFees: strconv.FormatFloat(r.BookingFees, 'f', 2, 64),
|
|
Lines: u.Map(r.Lines, func(l *booking.Line) *views.ReportLine {
|
|
return &views.ReportLine{
|
|
Id: l.InvoiceNumber(hc),
|
|
Url: templ.SafeURL(fmt.Sprintf("%s/%d", constants.RouteBooking, l.Id)),
|
|
Total: strconv.FormatFloat(l.Total, 'f', 2, 64),
|
|
CustomerName: l.CustomerName,
|
|
From: l.From.Format("2006-01-02"),
|
|
To: l.To.Format("2006-01-02"),
|
|
Platform: l.Platform,
|
|
PlatformFees: strconv.FormatFloat(l.PlatformFees, 'f', 2, 64),
|
|
Fee: strconv.FormatFloat(l.Fee(), 'f', 2, 64),
|
|
Profit: strconv.FormatFloat(l.Profit(), 'f', 2, 64),
|
|
}
|
|
}),
|
|
}
|
|
|
|
return renderTempl(c, http.StatusOK, views.ReportSection(reportVm))
|
|
}
|
|
}
|