Fix product performance grouped JSON build
This commit is contained in:
@@ -3536,10 +3536,20 @@ function weightFieldForMetric (field) {
|
||||
function applyDerivedGroupMetrics (out, sourceRows, groupField = '') {
|
||||
Object.assign(out, normalizeProductCostFields(out))
|
||||
const preservedCustomerScores = {}
|
||||
const preservedProductScores = {}
|
||||
for (const suffix of ['90d', '180d', '365d', 'total']) {
|
||||
if (Object.prototype.hasOwnProperty.call(out, `customer_score_${suffix}`)) {
|
||||
preservedCustomerScores[suffix] = Number(out[`customer_score_${suffix}`] || 0)
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(out, `performance_score_${suffix}`)) {
|
||||
preservedProductScores[suffix] = Number(out[`performance_score_${suffix}`] || 0)
|
||||
}
|
||||
}
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(out, 'performance_score') &&
|
||||
!Object.prototype.hasOwnProperty.call(preservedProductScores, '90d')
|
||||
) {
|
||||
preservedProductScores['90d'] = Number(out.performance_score || 0)
|
||||
}
|
||||
for (const suffix of ['90d', '180d', '365d', 'total']) {
|
||||
const sales = Number(out[`sales_usd_${suffix}`] || 0)
|
||||
@@ -3596,7 +3606,9 @@ function applyDerivedGroupMetrics (out, sourceRows, groupField = '') {
|
||||
out[`customer_score_${suffix}`] = Object.prototype.hasOwnProperty.call(preservedCustomerScores, suffix)
|
||||
? preservedCustomerScores[suffix]
|
||||
: customerSalesPeriodScore(periodMetricSource(out, suffix))
|
||||
out[`performance_score_${suffix}`] = productSalesPeriodScore(productPeriodMetricSource(out, suffix))
|
||||
out[`performance_score_${suffix}`] = Object.prototype.hasOwnProperty.call(preservedProductScores, suffix)
|
||||
? preservedProductScores[suffix]
|
||||
: productSalesPeriodScore(productPeriodMetricSource(out, suffix))
|
||||
}
|
||||
out.performance_score = Number(out.performance_score_90d || 0)
|
||||
}
|
||||
@@ -3997,10 +4009,10 @@ function scoreSuffixForMetric (field) {
|
||||
}
|
||||
|
||||
function scoreWeight (row, suffix) {
|
||||
const salesUsd = Number(row?.[`sales_usd_${suffix}`] || 0)
|
||||
if (salesUsd > 0) return salesUsd
|
||||
const salesQty = Number(row?.[`sales_qty_${suffix}`] || 0)
|
||||
if (salesQty > 0) return salesQty
|
||||
const salesUsd = Number(row?.[`sales_usd_${suffix}`] || 0)
|
||||
if (salesUsd > 0) return salesUsd
|
||||
const stockQty = Number(row?.stock_qty || 0)
|
||||
if (stockQty <= 0) return 1
|
||||
const costPrice = Number(row?.[`cost_price_usd_${suffix}`] || row?.cost_price_usd || 0)
|
||||
|
||||
Reference in New Issue
Block a user