refactor report to booking service

This commit is contained in:
Ruidy 2024-02-16 15:28:14 +01:00
parent bd09500dfc
commit 096e7fbf71
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 47 additions and 38 deletions

View file

@ -1,6 +1,8 @@
package booking
import (
"time"
"gorm.io/gorm"
)
@ -11,3 +13,42 @@ type Service struct {
func NewService(db *gorm.DB) *Service {
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
}

View file

@ -9,6 +9,7 @@ import (
"github.com/a-h/templ"
"github.com/labstack/echo/v4"
"github.com/rjNemo/rentease/constants"
"github.com/rjNemo/rentease/internal/domains/booking"
"github.com/rjNemo/rentease/internal/views"
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 {
period := c.FormValue("period")
if !u.Contains([]string{"month", "year"}, period) {
@ -63,42 +64,9 @@ func (s Server) handleComputeReport() echo.HandlerFunc {
}
}
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
res := bs.BuildReport(period, month, year)
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 {
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)),
@ -110,6 +78,6 @@ func (s Server) handleComputeReport() echo.HandlerFunc {
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))
}
}

View file

@ -31,6 +31,6 @@ func (s Server) MountHandlers() {
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.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))
}