来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
258 lines
8.2 KiB
JavaScript
258 lines
8.2 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* 诊断周期毛利率为负的原因(营收 vs 成本构成)
|
|
* 用法(生产):
|
|
* NODE_PATH=./node_modules node --env-file=.env /tmp/prod-diagnose-margin.cjs [cycle=202607]
|
|
*/
|
|
const { Pool } = require('pg');
|
|
|
|
const cycle = String(process.argv[2] || '202607').replace('-', '');
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
|
|
function r2(n) {
|
|
return Math.round((Number(n) || 0) * 100) / 100;
|
|
}
|
|
|
|
(async () => {
|
|
const rev = await pool.query(
|
|
`SELECT COALESCE(SUM(total_amount),0)::numeric AS rev,
|
|
COUNT(*)::int AS cnt,
|
|
COUNT(*) FILTER (WHERE COALESCE(total_amount,0) <= 0)::int AS zero_rev
|
|
FROM orders
|
|
WHERE cycle_code = $1 AND order_status >= 20`,
|
|
[cycle]
|
|
);
|
|
|
|
const cost = await pool.query(
|
|
`SELECT cost_type,
|
|
COUNT(*)::int AS cnt,
|
|
COALESCE(SUM(COALESCE(amount,0)),0)::numeric AS amount_sum,
|
|
COALESCE(SUM(COALESCE(allocated_amount, amount, 0)),0)::numeric AS eff_sum
|
|
FROM cost_details
|
|
WHERE cycle_code = $1
|
|
GROUP BY cost_type
|
|
ORDER BY amount_sum DESC`,
|
|
[cycle]
|
|
);
|
|
|
|
const orphan = await pool.query(
|
|
`SELECT COUNT(*)::int AS cnt,
|
|
COALESCE(SUM(COALESCE(amount,0)),0)::numeric AS total
|
|
FROM cost_details cd
|
|
WHERE cd.cycle_code = $1
|
|
AND cd.order_code IS NOT NULL
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM orders o
|
|
WHERE o.order_code = cd.order_code AND o.cycle_code = cd.cycle_code
|
|
)`,
|
|
[cycle]
|
|
);
|
|
|
|
const periodOnly = await pool.query(
|
|
`SELECT cost_type, COUNT(*)::int AS cnt,
|
|
COALESCE(SUM(COALESCE(amount,0)),0)::numeric AS total
|
|
FROM cost_details
|
|
WHERE cycle_code = $1 AND order_code IS NULL
|
|
GROUP BY cost_type
|
|
ORDER BY total DESC`,
|
|
[cycle]
|
|
);
|
|
|
|
const free = await pool.query(
|
|
`SELECT COUNT(*)::int AS cnt,
|
|
COALESCE(SUM(COALESCE(amount,0)),0)::numeric AS total
|
|
FROM cost_details
|
|
WHERE cost_type = 'free_overhead'
|
|
AND (cycle_code = $1 OR cycle_code LIKE $2)`,
|
|
[cycle, `${cycle}%`]
|
|
);
|
|
|
|
const fixed = await pool.query(
|
|
`SELECT cost_type, COUNT(*)::int AS cnt,
|
|
COALESCE(SUM(COALESCE(actual_amount,0)),0)::numeric AS total
|
|
FROM fixed_costs
|
|
WHERE cycle_code = $1 AND COALESCE(status,20) >= 20
|
|
GROUP BY cost_type
|
|
ORDER BY total DESC`,
|
|
[cycle]
|
|
);
|
|
|
|
const purchaseDup = await pool.query(
|
|
`SELECT order_code, sku_code, COUNT(*)::int AS cnt,
|
|
SUM(COALESCE(amount,0))::numeric AS total
|
|
FROM cost_details
|
|
WHERE cycle_code = $1 AND cost_type IN ('purchase','purchase_applied')
|
|
GROUP BY order_code, sku_code
|
|
HAVING COUNT(*) > 1 OR SUM(COALESCE(amount,0)) = 0
|
|
ORDER BY total DESC
|
|
LIMIT 20`,
|
|
[cycle]
|
|
);
|
|
|
|
const worst = await pool.query(
|
|
`SELECT o.order_code,
|
|
o.customer_code,
|
|
COALESCE(o.total_amount,0)::numeric AS rev,
|
|
COALESCE(c.cost,0)::numeric AS cost,
|
|
(COALESCE(o.total_amount,0) - COALESCE(c.cost,0))::numeric AS gp
|
|
FROM orders o
|
|
LEFT JOIN (
|
|
SELECT order_code, SUM(COALESCE(amount,0)) AS cost
|
|
FROM cost_details
|
|
WHERE cycle_code = $1 AND order_code IS NOT NULL
|
|
GROUP BY order_code
|
|
) c ON c.order_code = o.order_code
|
|
WHERE o.cycle_code = $1 AND o.order_status >= 20
|
|
ORDER BY gp ASC NULLS LAST
|
|
LIMIT 20`,
|
|
[cycle]
|
|
);
|
|
|
|
const negCnt = await pool.query(
|
|
`SELECT COUNT(*)::int AS cnt
|
|
FROM orders o
|
|
WHERE o.cycle_code = $1 AND o.order_status >= 20
|
|
AND COALESCE(o.total_amount,0) < (
|
|
SELECT COALESCE(SUM(cd.amount),0)
|
|
FROM cost_details cd
|
|
WHERE cd.cycle_code = o.cycle_code AND cd.order_code = o.order_code
|
|
)`,
|
|
[cycle]
|
|
);
|
|
|
|
const costTotal = cost.rows.reduce((s, r) => s + Number(r.amount_sum || 0), 0);
|
|
const revenue = Number(rev.rows[0].rev) || 0;
|
|
const gp = revenue - costTotal;
|
|
const pct = revenue > 0 ? r2((gp / revenue) * 100) : 0;
|
|
|
|
console.log(
|
|
JSON.stringify(
|
|
{
|
|
cycle,
|
|
formula: '毛利率 = (营收 - cost_details.amount合计) / 营收',
|
|
revenue: { ...rev.rows[0], revenue: r2(revenue) },
|
|
cost_total: r2(costTotal),
|
|
gross_profit: r2(gp),
|
|
gross_margin_pct: pct,
|
|
negative_margin_orders: negCnt.rows[0].cnt,
|
|
orphan_order_costs: orphan.rows[0],
|
|
free_overhead: free.rows[0],
|
|
costs_by_type: cost.rows.map((r) => ({
|
|
...r,
|
|
amount_sum: r2(r.amount_sum),
|
|
eff_sum: r2(r.eff_sum),
|
|
pct_of_cost: costTotal > 0 ? r2((Number(r.amount_sum) / costTotal) * 100) : 0,
|
|
})),
|
|
period_costs_no_order: periodOnly.rows,
|
|
fixed_costs_docs: fixed.rows,
|
|
purchase_dup_or_zero_sample: purchaseDup.rows,
|
|
worst_orders: worst.rows.map((r) => ({
|
|
...r,
|
|
rev: r2(r.rev),
|
|
cost: r2(r.cost),
|
|
gp: r2(r.gp),
|
|
margin_pct: Number(r.rev) > 0 ? r2((Number(r.gp) / Number(r.rev)) * 100) : null,
|
|
})),
|
|
},
|
|
null,
|
|
2
|
|
)
|
|
);
|
|
|
|
const purchaseLinked = await pool.query(
|
|
`SELECT COALESCE(SUM(COALESCE(cd.amount,0)),0)::numeric AS total, COUNT(*)::int AS cnt
|
|
FROM cost_details cd
|
|
WHERE cd.cycle_code = $1 AND cd.cost_type = 'purchase'
|
|
AND EXISTS (
|
|
SELECT 1 FROM orders o
|
|
WHERE o.order_code = cd.order_code AND o.cycle_code = cd.cycle_code
|
|
)`,
|
|
[cycle]
|
|
);
|
|
const purchaseOrphan = await pool.query(
|
|
`SELECT COALESCE(SUM(COALESCE(cd.amount,0)),0)::numeric AS total, COUNT(*)::int AS cnt,
|
|
array_agg(DISTINCT cd.order_code) AS order_codes
|
|
FROM cost_details cd
|
|
WHERE cd.cycle_code = $1 AND cd.cost_type = 'purchase'
|
|
AND cd.order_code IS NOT NULL
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM orders o
|
|
WHERE o.order_code = cd.order_code AND o.cycle_code = cd.cycle_code
|
|
)`,
|
|
[cycle]
|
|
);
|
|
const laborDetail = await pool.query(
|
|
`SELECT id, amount, allocation_method, allocated_amount, cost_subtype,
|
|
order_code, created_at
|
|
FROM cost_details WHERE cycle_code = $1 AND cost_type = 'labor'`,
|
|
[cycle]
|
|
);
|
|
const apiLike = await pool.query(
|
|
`SELECT COALESCE(cd.cost_type,'overhead') AS cost_type,
|
|
SUM(COALESCE(cd.amount,0))::numeric AS total
|
|
FROM cost_details cd
|
|
WHERE cd.cycle_code = $1
|
|
AND (
|
|
cd.order_code IS NULL
|
|
OR EXISTS (
|
|
SELECT 1 FROM orders o
|
|
WHERE o.order_code = cd.order_code AND o.cycle_code = cd.cycle_code
|
|
)
|
|
)
|
|
GROUP BY cd.cost_type`,
|
|
[cycle]
|
|
);
|
|
const apiCost = apiLike.rows.reduce((s, r) => s + Number(r.total || 0), 0);
|
|
const apiGp = revenue - apiCost;
|
|
const apiPct = revenue > 0 ? r2((apiGp / revenue) * 100) : 0;
|
|
|
|
console.log(
|
|
JSON.stringify(
|
|
{
|
|
api_view: {
|
|
note: '与 getCostAggregation 同口径(排除孤儿订单成本,含无订单期间成本)',
|
|
costs: apiLike.rows,
|
|
cost_total: r2(apiCost),
|
|
gross_profit: r2(apiGp),
|
|
gross_margin_pct: apiPct,
|
|
},
|
|
purchase_linked_to_orders: purchaseLinked.rows[0],
|
|
purchase_orphan_orders: {
|
|
...purchaseOrphan.rows[0],
|
|
order_codes: purchaseOrphan.rows[0]?.order_codes || [],
|
|
},
|
|
labor_detail: laborDetail.rows,
|
|
purchase_only_margin_pct:
|
|
revenue > 0
|
|
? r2(((revenue - Number(purchaseLinked.rows[0].total)) / revenue) * 100)
|
|
: 0,
|
|
},
|
|
null,
|
|
2
|
|
)
|
|
);
|
|
|
|
if (process.argv.includes('--detail')) {
|
|
const extras = await pool.query(
|
|
`SELECT id, cycle_code, cost_type, cost_subtype, amount, allocation_method,
|
|
order_code, reference_type, reference_id, created_at
|
|
FROM cost_details
|
|
WHERE cycle_code = $1 AND cost_type NOT IN ('purchase', 'purchase_applied')
|
|
ORDER BY created_at DESC`,
|
|
[cycle]
|
|
);
|
|
const fc = await pool.query(
|
|
`SELECT id, cycle_code, cost_type, budget_amount, actual_amount, allocation_method,
|
|
status, remark, created_at
|
|
FROM fixed_costs WHERE cycle_code = $1 ORDER BY created_at DESC`,
|
|
[cycle]
|
|
);
|
|
console.log(JSON.stringify({ non_purchase_costs: extras.rows, fixed_costs: fc.rows }, null, 2));
|
|
}
|
|
|
|
await pool.end();
|
|
})().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|