update products
This commit is contained in:
parent
54dc8662d6
commit
3e0d75a4d0
@ -105,6 +105,7 @@ type ProductAnalyticsResponse struct {
|
|||||||
type ProductAnalyticsData struct {
|
type ProductAnalyticsData struct {
|
||||||
ProductID uuid.UUID `json:"product_id"`
|
ProductID uuid.UUID `json:"product_id"`
|
||||||
ProductName string `json:"product_name"`
|
ProductName string `json:"product_name"`
|
||||||
|
ProductSku string `json:"product_sku"`
|
||||||
CategoryID uuid.UUID `json:"category_id"`
|
CategoryID uuid.UUID `json:"category_id"`
|
||||||
CategoryName string `json:"category_name"`
|
CategoryName string `json:"category_name"`
|
||||||
CategoryOrder int `json:"category_order"`
|
CategoryOrder int `json:"category_order"`
|
||||||
|
|||||||
@ -31,6 +31,7 @@ type SalesAnalytics struct {
|
|||||||
type ProductAnalytics struct {
|
type ProductAnalytics struct {
|
||||||
ProductID uuid.UUID `json:"product_id"`
|
ProductID uuid.UUID `json:"product_id"`
|
||||||
ProductName string `json:"product_name"`
|
ProductName string `json:"product_name"`
|
||||||
|
ProductSku string `json:"product_sku"`
|
||||||
CategoryID uuid.UUID `json:"category_id"`
|
CategoryID uuid.UUID `json:"category_id"`
|
||||||
CategoryName string `json:"category_name"`
|
CategoryName string `json:"category_name"`
|
||||||
CategoryOrder int `json:"category_order"`
|
CategoryOrder int `json:"category_order"`
|
||||||
|
|||||||
@ -109,6 +109,7 @@ type ProductAnalyticsResponse struct {
|
|||||||
type ProductAnalyticsData struct {
|
type ProductAnalyticsData struct {
|
||||||
ProductID uuid.UUID `json:"product_id"`
|
ProductID uuid.UUID `json:"product_id"`
|
||||||
ProductName string `json:"product_name"`
|
ProductName string `json:"product_name"`
|
||||||
|
ProductSku string `json:"product_sku"`
|
||||||
CategoryID uuid.UUID `json:"category_id"`
|
CategoryID uuid.UUID `json:"category_id"`
|
||||||
CategoryName string `json:"category_name"`
|
CategoryName string `json:"category_name"`
|
||||||
CategoryOrder int `json:"category_order"`
|
CategoryOrder int `json:"category_order"`
|
||||||
|
|||||||
@ -187,6 +187,7 @@ func (p *AnalyticsProcessorImpl) GetProductAnalytics(ctx context.Context, req *m
|
|||||||
resultData = append(resultData, models.ProductAnalyticsData{
|
resultData = append(resultData, models.ProductAnalyticsData{
|
||||||
ProductID: data.ProductID,
|
ProductID: data.ProductID,
|
||||||
ProductName: data.ProductName,
|
ProductName: data.ProductName,
|
||||||
|
ProductSku: data.ProductSku,
|
||||||
CategoryID: data.CategoryID,
|
CategoryID: data.CategoryID,
|
||||||
CategoryName: data.CategoryName,
|
CategoryName: data.CategoryName,
|
||||||
CategoryOrder: data.CategoryOrder,
|
CategoryOrder: data.CategoryOrder,
|
||||||
|
|||||||
@ -110,14 +110,10 @@ 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
|
||||||
|
|
||||||
query := r.db.WithContext(ctx).
|
// Subquery untuk menghitung analytics dari orders
|
||||||
Table("order_items oi").
|
salesSubquery := r.db.Table("order_items oi").
|
||||||
Select(`
|
Select(`
|
||||||
p.id as product_id,
|
p.id as product_id,
|
||||||
p.name as product_name,
|
|
||||||
c.id as category_id,
|
|
||||||
c.name as category_name,
|
|
||||||
c.order as category_order,
|
|
||||||
COALESCE(SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.quantity - COALESCE(oi.refund_quantity, 0) ELSE 0 END), 0) as quantity_sold,
|
COALESCE(SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.quantity - COALESCE(oi.refund_quantity, 0) ELSE 0 END), 0) as quantity_sold,
|
||||||
COALESCE(SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_price - COALESCE(oi.refund_amount, 0) ELSE 0 END), 0) as revenue,
|
COALESCE(SUM(CASE WHEN oi.is_fully_refunded = false THEN oi.total_price - COALESCE(oi.refund_amount, 0) ELSE 0 END), 0) as revenue,
|
||||||
CASE
|
CASE
|
||||||
@ -128,7 +124,6 @@ func (r *AnalyticsRepositoryImpl) GetProductAnalytics(ctx context.Context, organ
|
|||||||
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).
|
||||||
@ -138,12 +133,33 @@ func (r *AnalyticsRepositoryImpl) GetProductAnalytics(ctx context.Context, organ
|
|||||||
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 {
|
||||||
query = query.Where("o.outlet_id = ?", *outletID)
|
salesSubquery = salesSubquery.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, c.id, c.name").
|
Group("p.id, p.name, p.sku, c.id, c.name, c.order, sales.quantity_sold, sales.revenue, sales.average_price, sales.order_count").
|
||||||
Order("revenue DESC").
|
Order("COALESCE(sales.revenue, 0) DESC, p.name ASC").
|
||||||
Limit(limit).
|
Limit(limit).
|
||||||
Scan(&results).Error
|
Scan(&results).Error
|
||||||
|
|
||||||
|
|||||||
@ -157,6 +157,7 @@ func ProductAnalyticsModelToContract(resp *models.ProductAnalyticsResponse) *con
|
|||||||
data = append(data, contract.ProductAnalyticsData{
|
data = append(data, contract.ProductAnalyticsData{
|
||||||
ProductID: item.ProductID,
|
ProductID: item.ProductID,
|
||||||
ProductName: item.ProductName,
|
ProductName: item.ProductName,
|
||||||
|
ProductSku: item.ProductSku,
|
||||||
CategoryID: item.CategoryID,
|
CategoryID: item.CategoryID,
|
||||||
CategoryName: item.CategoryName,
|
CategoryName: item.CategoryName,
|
||||||
CategoryOrder: item.CategoryOrder,
|
CategoryOrder: item.CategoryOrder,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user