Fix product performance grouped JSON build

This commit is contained in:
M_Kececi
2026-07-07 12:59:19 +03:00
parent 005d2eafac
commit c6a3d1552e
4 changed files with 256 additions and 66 deletions
@@ -3631,8 +3631,10 @@ function salesGroupPerformanceScore (row) {
function productSalesPeriodScore (row) {
const suffix = row?.suffix || '90d'
const periodDays = productPerformancePeriodDays(row, suffix)
return productScore100({
suffix,
periodDays,
salesUSD: Number(row?.sales_usd_90d || 0),
salesIndex: Number(row?.sales_index_90d || 0),
margin: Number(row?.gross_margin_cost_90d ?? row?.gross_margin_90d ?? 0),
@@ -3698,15 +3700,15 @@ function periodMetricSource (row, suffix) {
}
}
function productScore100 ({ suffix = '90d', salesUSD = 0, margin = 0, stockTurnover = 0, marketCount = 0, customerCount = 0 } = {}) {
function productScore100 ({ suffix = '90d', periodDays = 0, salesUSD = 0, margin = 0, stockTurnover = 0, marketCount = 0, customerCount = 0 } = {}) {
if (Number(salesUSD || 0) <= 0) return 1
const revenue = ratioScore(salesUSD, productRevenueTarget(suffix))
const revenue = ratioScore(salesUSD, productRevenueTarget(suffix, periodDays))
return clampScore(
0.30 * marginScore(margin) +
0.20 * ratioScore(stockTurnover, stockTurnoverTarget(suffix)) +
0.20 * revenue +
0.05 * ratioScore(marketCount, marketSpreadTarget(suffix)) +
0.25 * ratioScore(customerCount, customerSpreadTarget(suffix))
0.25 * ratioScore(customerCount, customerSpreadTarget(suffix, periodDays))
)
}
@@ -3728,7 +3730,7 @@ function ratioScore (value, target) {
}
function marginScore (margin) {
return ratioScore(Math.max(0, Number(margin || 0)), 0.40)
return ratioScore(Math.max(0, Number(margin || 0)), 0.45)
}
function clampScore (score) {
@@ -3737,11 +3739,11 @@ function clampScore (score) {
return Math.round(Math.min(100, n) * 10000) / 10000
}
function productRevenueTarget (suffix) {
if (suffix === '180d') return 20000
if (suffix === '365d') return 40000
if (suffix === 'total') return 120000
return 10000
function productRevenueTarget (suffix, periodDays = 0) {
if (suffix === '180d') return 50000
if (suffix === '365d') return 100000
if (suffix === 'total') return 50000 * totalPeriodMultiplier(periodDays)
return 25000
}
function customerRevenueTarget (suffix) {
@@ -3780,11 +3782,19 @@ function marketSpreadTarget (suffix) {
return 6
}
function customerSpreadTarget (suffix) {
function customerSpreadTarget (suffix, periodDays = 0) {
if (suffix === '90d') return 8
if (suffix === '180d') return 20
if (suffix === '365d') return 50
return 50
return 20 * totalPeriodMultiplier(periodDays)
}
function totalPeriodMultiplier (periodDays = 0) {
let days = Number(periodDays || 0)
if (!Number.isFinite(days) || days <= 0) {
days = productPerformancePeriodDays({ kpi_date: new Date().toISOString().slice(0, 10) }, 'total')
}
return Math.max(1, days / 180)
}
function periodCount (row, prefix, suffix) {