Fix product performance grouped JSON build
This commit is contained in:
@@ -4676,21 +4676,21 @@ FROM (
|
||||
CASE WHEN SUM(sales_qty_90d) > 0 THEN SUM(margin_index_90d * sales_qty_90d) / NULLIF(SUM(sales_qty_90d),0) ELSE 0 END AS margin_index_90d,
|
||||
CASE
|
||||
WHEN SUM(CASE
|
||||
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
|
||||
WHEN COALESCE(sales_qty_90d,0) > 0 THEN sales_qty_90d
|
||||
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
|
||||
WHEN COALESCE(stock_qty,0) > 0 AND COALESCE(cost_price_usd,0) > 0 THEN stock_qty * cost_price_usd
|
||||
WHEN COALESCE(stock_qty,0) > 0 THEN stock_qty
|
||||
ELSE 1
|
||||
END) > 0
|
||||
THEN SUM(performance_score * CASE
|
||||
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
|
||||
WHEN COALESCE(sales_qty_90d,0) > 0 THEN sales_qty_90d
|
||||
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
|
||||
WHEN COALESCE(stock_qty,0) > 0 AND COALESCE(cost_price_usd,0) > 0 THEN stock_qty * cost_price_usd
|
||||
WHEN COALESCE(stock_qty,0) > 0 THEN stock_qty
|
||||
ELSE 1
|
||||
END) / NULLIF(SUM(CASE
|
||||
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
|
||||
WHEN COALESCE(sales_qty_90d,0) > 0 THEN sales_qty_90d
|
||||
WHEN COALESCE(sales_usd_90d,0) > 0 THEN sales_usd_90d
|
||||
WHEN COALESCE(stock_qty,0) > 0 AND COALESCE(cost_price_usd,0) > 0 THEN stock_qty * cost_price_usd
|
||||
WHEN COALESCE(stock_qty,0) > 0 THEN stock_qty
|
||||
ELSE 1
|
||||
@@ -6051,10 +6051,19 @@ func productPerformanceMapVariantKey(row map[string]any) string {
|
||||
func deriveProductPerformanceGroupMetrics(out map[string]any, groupField string) {
|
||||
normalizeProductPerformanceCostFields(out)
|
||||
preservedCustomerScores := map[string]float64{}
|
||||
preservedProductScores := map[string]float64{}
|
||||
for _, suffix := range productPerformancePeriodSuffixes() {
|
||||
if score, ok := productPerformanceOptionalFloat(out, "customer_score_"+suffix); ok {
|
||||
preservedCustomerScores[suffix] = score
|
||||
}
|
||||
if score, ok := productPerformanceOptionalFloat(out, "performance_score_"+suffix); ok {
|
||||
preservedProductScores[suffix] = score
|
||||
}
|
||||
}
|
||||
if score, ok := productPerformanceOptionalFloat(out, "performance_score"); ok {
|
||||
if _, exists := preservedProductScores["90d"]; !exists {
|
||||
preservedProductScores["90d"] = score
|
||||
}
|
||||
}
|
||||
for _, suffix := range productPerformancePeriodSuffixes() {
|
||||
sales := floatFromMap(out, "sales_usd_"+suffix)
|
||||
@@ -6135,7 +6144,11 @@ func deriveProductPerformanceGroupMetrics(out map[string]any, groupField string)
|
||||
} else {
|
||||
out["customer_score_"+suffix] = productPerformanceCustomerSalesPeriodScore(rowPeriodMetricMap(out, suffix))
|
||||
}
|
||||
out["performance_score_"+suffix] = productPerformanceSalesPeriodScore(out, suffix)
|
||||
if score, ok := preservedProductScores[suffix]; ok {
|
||||
out["performance_score_"+suffix] = score
|
||||
} else {
|
||||
out["performance_score_"+suffix] = productPerformanceSalesPeriodScore(out, suffix)
|
||||
}
|
||||
}
|
||||
out["performance_score"] = out["performance_score_90d"]
|
||||
if floatFromMap(out, "order_qty") > 0 || floatFromMap(out, "order_usd") > 0 {
|
||||
@@ -6737,10 +6750,10 @@ func productPerformanceScoreSuffix(field string) string {
|
||||
}
|
||||
|
||||
func productPerformanceScoreWeight(row map[string]any, suffix string) float64 {
|
||||
if weight := floatFromMap(row, "sales_usd_"+suffix); weight > 0 {
|
||||
if weight := floatFromMap(row, "sales_qty_"+suffix); weight > 0 {
|
||||
return weight
|
||||
}
|
||||
if weight := floatFromMap(row, "sales_qty_"+suffix); weight > 0 {
|
||||
if weight := floatFromMap(row, "sales_usd_"+suffix); weight > 0 {
|
||||
return weight
|
||||
}
|
||||
stockQty := floatFromMap(row, "stock_qty")
|
||||
|
||||
Reference in New Issue
Block a user