Fix product performance grouped JSON build
This commit is contained in:
@@ -3479,8 +3479,8 @@ function aggregateGroupFields (sourceRows, groupField = '') {
|
||||
for (const field of fields) {
|
||||
if (field === 'row_key' || field === 'key') continue
|
||||
if (/^gross_margin(_base|_cost)?(_|$)/i.test(field)) continue
|
||||
if (field === 'stock_qty') {
|
||||
out[field] = distinctVariantStockQty(sourceRows)
|
||||
if (field === 'stock_qty' || /^avg_stock_/i.test(field)) {
|
||||
out[field] = distinctVariantNumber(sourceRows, field)
|
||||
continue
|
||||
}
|
||||
if (field === 'idle_cost_usd') {
|
||||
@@ -3528,6 +3528,19 @@ function weightFieldForMetric (field) {
|
||||
|
||||
function applyDerivedGroupMetrics (out, sourceRows, groupField = '') {
|
||||
Object.assign(out, normalizeProductCostFields(out))
|
||||
const preservedPerformanceScores = {}
|
||||
if (Object.prototype.hasOwnProperty.call(out, 'performance_score')) {
|
||||
preservedPerformanceScores['90d'] = Number(out.performance_score || 0)
|
||||
}
|
||||
const preservedCustomerScores = {}
|
||||
for (const suffix of ['90d', '180d', '365d', 'total']) {
|
||||
if (Object.prototype.hasOwnProperty.call(out, `performance_score_${suffix}`)) {
|
||||
preservedPerformanceScores[suffix] = Number(out[`performance_score_${suffix}`] || 0)
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(out, `customer_score_${suffix}`)) {
|
||||
preservedCustomerScores[suffix] = Number(out[`customer_score_${suffix}`] || 0)
|
||||
}
|
||||
}
|
||||
for (const suffix of ['90d', '180d', '365d', 'total']) {
|
||||
const sales = Number(out[`sales_usd_${suffix}`] || 0)
|
||||
const qty = Number(out[`sales_qty_${suffix}`] || 0)
|
||||
@@ -3579,14 +3592,14 @@ function applyDerivedGroupMetrics (out, sourceRows, groupField = '') {
|
||||
if (sourceRows.some(row => row.performance_bucket)) {
|
||||
out.performance_bucket = dominantValue(sourceRows, 'performance_bucket')
|
||||
}
|
||||
out.customer_score_90d = customerSalesPeriodScore(periodMetricSource(out, '90d'))
|
||||
out.customer_score_180d = customerSalesPeriodScore(periodMetricSource(out, '180d'))
|
||||
out.customer_score_365d = customerSalesPeriodScore(periodMetricSource(out, '365d'))
|
||||
out.customer_score_total = customerSalesPeriodScore(periodMetricSource(out, 'total'))
|
||||
out.performance_score_90d = productSalesPeriodScore(productPeriodMetricSource(out, '90d'))
|
||||
out.performance_score_180d = productSalesPeriodScore(productPeriodMetricSource(out, '180d'))
|
||||
out.performance_score_365d = productSalesPeriodScore(productPeriodMetricSource(out, '365d'))
|
||||
out.performance_score_total = productSalesPeriodScore(productPeriodMetricSource(out, 'total'))
|
||||
for (const suffix of ['90d', '180d', '365d', 'total']) {
|
||||
out[`customer_score_${suffix}`] = Object.prototype.hasOwnProperty.call(preservedCustomerScores, suffix)
|
||||
? preservedCustomerScores[suffix]
|
||||
: customerSalesPeriodScore(periodMetricSource(out, suffix))
|
||||
out[`performance_score_${suffix}`] = Object.prototype.hasOwnProperty.call(preservedPerformanceScores, suffix)
|
||||
? preservedPerformanceScores[suffix]
|
||||
: productSalesPeriodScore(productPeriodMetricSource(out, suffix))
|
||||
}
|
||||
out.performance_score = Number(out.performance_score_90d || 0)
|
||||
}
|
||||
|
||||
@@ -3779,19 +3792,23 @@ function productVariantStockKey (row) {
|
||||
}
|
||||
|
||||
function distinctVariantStockQty (sourceRows) {
|
||||
return distinctVariantNumber(sourceRows, 'stock_qty')
|
||||
}
|
||||
|
||||
function distinctVariantNumber (sourceRows, field) {
|
||||
const seen = new Set()
|
||||
let total = 0
|
||||
let keyed = false
|
||||
for (const row of sourceRows) {
|
||||
const key = productVariantStockKey(row)
|
||||
const stockQty = Number(row?.stock_qty || 0)
|
||||
const value = Number(row?.[field] || 0)
|
||||
if (!key) continue
|
||||
keyed = true
|
||||
if (seen.has(key)) continue
|
||||
seen.add(key)
|
||||
total += stockQty
|
||||
total += value
|
||||
}
|
||||
return keyed ? total : sumRows(sourceRows, 'stock_qty')
|
||||
return keyed ? total : sumRows(sourceRows, field)
|
||||
}
|
||||
|
||||
function distinctVariantStockCost (sourceRows) {
|
||||
|
||||
Reference in New Issue
Block a user