mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +00:00
refactor report to booking service
This commit is contained in:
parent
bd09500dfc
commit
096e7fbf71
3 changed files with 47 additions and 38 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
package booking
|
package booking
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -11,3 +13,42 @@ type Service struct {
|
||||||
func NewService(db *gorm.DB) *Service {
|
func NewService(db *gorm.DB) *Service {
|
||||||
return &Service{db: db}
|
return &Service{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Line struct {
|
||||||
|
From time.Time
|
||||||
|
To time.Time
|
||||||
|
Id string
|
||||||
|
CustomerName string
|
||||||
|
Platform string
|
||||||
|
Total float64
|
||||||
|
PlatformFees float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bs Service) BuildReport(period string, month, year int) []*Line {
|
||||||
|
res := make([]*Line, 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
bs.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)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/a-h/templ"
|
"github.com/a-h/templ"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/rjNemo/rentease/constants"
|
"github.com/rjNemo/rentease/constants"
|
||||||
|
"github.com/rjNemo/rentease/internal/domains/booking"
|
||||||
"github.com/rjNemo/rentease/internal/views"
|
"github.com/rjNemo/rentease/internal/views"
|
||||||
u "github.com/rjNemo/underscore"
|
u "github.com/rjNemo/underscore"
|
||||||
)
|
)
|
||||||
|
|
@ -35,7 +36,7 @@ func handleReportsPage() echo.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) handleComputeReport() echo.HandlerFunc {
|
func handleComputeReport(bs *booking.Service) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
period := c.FormValue("period")
|
period := c.FormValue("period")
|
||||||
if !u.Contains([]string{"month", "year"}, period) {
|
if !u.Contains([]string{"month", "year"}, period) {
|
||||||
|
|
@ -63,42 +64,9 @@ func (s Server) handleComputeReport() echo.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Result struct {
|
res := bs.BuildReport(period, month, year)
|
||||||
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" {
|
reportVm := u.Map(res, func(r *booking.Line) *views.ReportViewModel {
|
||||||
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{
|
return &views.ReportViewModel{
|
||||||
Id: r.Id,
|
Id: r.Id,
|
||||||
Url: templ.SafeURL(fmt.Sprintf("%s/%s", constants.RouteBooking, r.Id)),
|
Url: templ.SafeURL(fmt.Sprintf("%s/%s", constants.RouteBooking, r.Id)),
|
||||||
|
|
@ -110,6 +78,6 @@ func (s Server) handleComputeReport() echo.HandlerFunc {
|
||||||
PlatformFees: strconv.FormatFloat(r.PlatformFees, 'f', 2, 64),
|
PlatformFees: strconv.FormatFloat(r.PlatformFees, 'f', 2, 64),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return s.renderTempl(c, http.StatusOK, views.ReportSection(reportVm))
|
return renderTempl(c, http.StatusOK, views.ReportSection(reportVm))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,6 @@ func (s Server) MountHandlers() {
|
||||||
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("/reports", handleReportsPage())
|
s.Router.GET("/reports", handleReportsPage())
|
||||||
s.Router.GET("/reports/do", s.handleComputeReport())
|
s.Router.GET("/reports/do", handleComputeReport(s.bs))
|
||||||
s.Router.GET("/pdf", handleCreateInvoicePdf(s.ps))
|
s.Router.GET("/pdf", handleCreateInvoicePdf(s.ps))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue