Add Daily sales

This commit is contained in:
aditya.siregar
2024-08-02 02:27:57 +07:00
parent d50cefbc7f
commit 4e4d9d3a90
7 changed files with 90 additions and 0 deletions
+29
View File
@@ -199,3 +199,32 @@ func (r *OrderRepository) SumAmount(ctx mycontext.Context, req entity.OrderSearc
return amount, nil
}
func (r *OrderRepository) GetDailySalesMetrics(ctx context.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
var sales []entity.ProductDailySales
// 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").
Where("o.status = ?", "PAID").
Where("o.created_at >= ?", time.Now().AddDate(0, 0, -30)). // Last 30 days
Group("day, oi.item_id").
Order("day")
// 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
if err := query.Find(&sales).Error; err != nil {
return nil, err
}
return sales, nil
}