Add Filter Sales Metric

This commit is contained in:
aditya.siregar 2024-08-02 14:41:41 +07:00
parent 4e4d9d3a90
commit eff502b24a
5 changed files with 47 additions and 16 deletions

View File

@ -126,6 +126,7 @@ type OrderSearch struct {
Offset int
StartDate string
EndDate string
Period string
}
type HistoryOrderList []*HistoryOrderDB
@ -180,7 +181,9 @@ func (e *TicketSoldDB) ToTicketSold() *TicketSold {
}
type ProductDailySales struct {
Day time.Time
ProductID int64
TotalQuantity int
Day time.Time
SiteID int64
SiteName string
PaymentType string
Total float64
}

View File

@ -282,9 +282,11 @@ func (h *Handler) toDailySales(resp []entity.ProductDailySales) []response.Produ
var dailySales []response.ProductDailySales
for _, b := range resp {
dailySales = append(dailySales, response.ProductDailySales{
Day: b.Day,
ProductID: b.ProductID,
TotalQuantity: b.TotalQuantity,
Day: b.Day,
SiteID: b.SiteID,
Total: b.Total,
SiteName: b.SiteName,
PaymentType: b.PaymentType,
})
}
return dailySales

View File

@ -19,6 +19,7 @@ type OrderParam struct {
Status string `form:"status" json:"status"`
Limit int `form:"limit" json:"limit" example:"10"`
Offset int `form:"offset" json:"offset" example:"0"`
Period string `form:"period" json:"period" example:"1d,7d,1m"`
}
func (o *OrderParam) ToOrderEntity(ctx mycontext.Context) entity.OrderSearch {
@ -32,6 +33,7 @@ func (o *OrderParam) ToOrderEntity(ctx mycontext.Context) entity.OrderSearch {
StartDate: o.StartDate,
EndDate: o.EndDate,
Status: o.Status,
Period: o.Period,
}
}

View File

@ -113,7 +113,9 @@ type CreateOrderItemResponse struct {
}
type ProductDailySales struct {
Day time.Time
ProductID int64
TotalQuantity int
Day time.Time `json:"day"`
SiteID int64 `json:"site_id"`
SiteName string `json:"site_name"`
PaymentType string `json:"payment_type"`
Total float64 `json:"total"`
}

View File

@ -203,25 +203,47 @@ func (r *OrderRepository) SumAmount(ctx mycontext.Context, req entity.OrderSearc
func (r *OrderRepository) GetDailySalesMetrics(ctx context.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
var sales []entity.ProductDailySales
var dateTrunc, periodFilter string
now := time.Now()
switch req.Period {
case "1d":
dateTrunc = "hour"
periodFilter = now.Add(-24 * time.Hour).Format("2006-01-02 15:04:05")
case "7d":
dateTrunc = "day"
periodFilter = now.Add(-7 * 24 * time.Hour).Format("2006-01-02 15:04:05")
case "1m":
dateTrunc = "day"
periodFilter = now.AddDate(0, -1, 0).Format("2006-01-02 15:04:05")
case "1y":
dateTrunc = "week"
periodFilter = now.AddDate(-1, 0, 0).Format("2006-01-02 15:04:05")
default:
dateTrunc = "day"
periodFilter = now.AddDate(0, -1, 0).Format("2006-01-02 15:04:05") // Default to last month
}
// Build the query with GORM
query := r.db.WithContext(ctx).
Table("orders o").
Select(`DATE_TRUNC('day', o.created_at) AS day, oi.item_id, SUM(oi.qty) AS total_quantity`).
Joins("LEFT JOIN order_items oi ON o.id = oi.order_id").
Select(`DATE_TRUNC(?, o.created_at) AS day, s.id AS site_id, s.name AS site_name, o.payment_type, SUM(oi.qty * oi.price) AS total_quantity`, dateTrunc).
Joins("JOIN order_items oi ON o.id = oi.order_id").
Joins("JOIN sites s ON o.site_id = s.id").
Where("o.status = ?", "PAID").
Where("o.created_at >= ?", time.Now().AddDate(0, 0, -30)). // Last 30 days
Group("day, oi.item_id").
Order("day")
Where("o.created_at >= ?", periodFilter)
// Apply filters based on the presence of PartnerID and SiteID
if req.PartnerID != nil {
query = query.Where("o.partner_id = ?", *req.PartnerID)
}
if req.SiteID != nil {
query = query.Where("o.site_id = ?", *req.SiteID)
}
// Execute the query and scan the results
query = query.Group("day, s.id, s.name, o.payment_type").
Order("day")
if err := query.Find(&sales).Error; err != nil {
return nil, err
}