update product analytic if exiest order

This commit is contained in:
efrilm 2025-11-25 13:23:59 +07:00
parent cd784624c9
commit 0c331dce6a

View File

@ -110,56 +110,36 @@ func (r *AnalyticsRepositoryImpl) GetSalesAnalytics(ctx context.Context, organiz
func (r *AnalyticsRepositoryImpl) GetProductAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time, limit int) ([]*entities.ProductAnalytics, error) { func (r *AnalyticsRepositoryImpl) GetProductAnalytics(ctx context.Context, organizationID uuid.UUID, outletID *uuid.UUID, dateFrom, dateTo time.Time, limit int) ([]*entities.ProductAnalytics, error) {
var results []*entities.ProductAnalytics var results []*entities.ProductAnalytics
// Subquery untuk menghitung analytics dari orders query := r.db.WithContext(ctx).
salesSubquery := r.db.Table("order_items oi"). Table("order_items oi").
Select(` Select(`
p.id as product_id, p.id as product_id,
COALESCE(SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.quantity - COALESCE(oi.refund_quantity, 0) ELSE 0 END), 0) as quantity_sold, p.name as product_name,
COALESCE(SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_price - COALESCE(oi.refund_amount, 0) ELSE 0 END), 0) as revenue, c.id as category_id,
c.name as category_name,
c.order as category_order,
COALESCE(SUM(oi.quantity), 0) as quantity_sold,
COALESCE(SUM(oi.total_price), 0) as revenue,
CASE CASE
WHEN SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.quantity - COALESCE(oi.refund_quantity, 0) ELSE 0 END) > 0 WHEN SUM(oi.quantity) > 0 THEN COALESCE(SUM(oi.total_price), 0) / SUM(oi.quantity)
THEN COALESCE(SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_price - COALESCE(oi.refund_amount, 0) ELSE 0 END), 0) / SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.quantity - COALESCE(oi.refund_quantity, 0) ELSE 0 END)
ELSE 0 ELSE 0
END as average_price, END as average_price,
COUNT(DISTINCT oi.order_id) as order_count COUNT(DISTINCT oi.order_id) as order_count
`). `).
Joins("JOIN products p ON oi.product_id = p.id"). Joins("JOIN products p ON oi.product_id = p.id").
Joins("JOIN categories c ON p.category_id = c.id").
Joins("JOIN orders o ON oi.order_id = o.id"). Joins("JOIN orders o ON oi.order_id = o.id").
Where("o.organization_id = ?", organizationID). Where("o.organization_id = ?", organizationID).
Where("o.is_void = ?", false). Where("o.is_void = ?", false).
Where("o.is_refund = ?", false).
Where("o.payment_status = ?", entities.PaymentStatusCompleted).
Where("oi.status != ?", entities.OrderItemStatusCancelled).
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo) Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
if outletID != nil { if outletID != nil {
salesSubquery = salesSubquery.Where("o.outlet_id = ?", *outletID) query = query.Where("o.outlet_id = ?", *outletID)
} }
salesSubquery = salesSubquery.Group("p.id")
// Main query: ambil semua products dan LEFT JOIN dengan sales subquery
query := r.db.WithContext(ctx).
Table("products p").
Select(`
p.id as product_id,
p.name as product_name,
p.sku as product_sku,
c.id as category_id,
c.name as category_name,
c.order as category_order,
COALESCE(sales.quantity_sold, 0) as quantity_sold,
COALESCE(sales.revenue, 0) as revenue,
COALESCE(sales.average_price, 0) as average_price,
COALESCE(sales.order_count, 0) as order_count
`).
Joins("JOIN categories c ON p.category_id = c.id").
Joins("LEFT JOIN (?) as sales ON sales.product_id = p.id", salesSubquery).
Where("p.organization_id = ?", organizationID)
err := query. err := query.
Group("p.id, p.name, p.sku, c.id, c.name, c.order, sales.quantity_sold, sales.revenue, sales.average_price, sales.order_count"). Group("p.id, p.name, c.id, c.name, c.order").
Order("COALESCE(sales.revenue, 0) DESC, p.name ASC"). Order("revenue DESC").
Limit(limit). Limit(limit).
Scan(&results).Error Scan(&results).Error