来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
88 lines
3.6 KiB
JavaScript
88 lines
3.6 KiB
JavaScript
const { Pool } = require('pg');
|
|
|
|
async function main() {
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
const cycle = '202607';
|
|
|
|
// If we rebuilt cost using COALESCE(po.actual, po.purchase, insp.purchase) what margins look like
|
|
const sim = await pool.query(
|
|
`WITH latest_insp AS (
|
|
SELECT DISTINCT ON (i.order_code, i.sku_code)
|
|
i.order_code, i.sku_code, i.actual_qty, i.purchase_price AS insp_price
|
|
FROM inspections i
|
|
JOIN orders o ON o.order_code = i.order_code AND o.cycle_code = $1
|
|
WHERE COALESCE(i.actual_qty,0) > 0
|
|
ORDER BY i.order_code, i.sku_code, i.inspection_time DESC NULLS LAST, i.created_at DESC, i.id DESC
|
|
),
|
|
priced AS (
|
|
SELECT li.order_code, li.sku_code, li.actual_qty,
|
|
li.insp_price,
|
|
COALESCE(po.actual_price, po.purchase_price) AS po_price,
|
|
oi.price AS sell_price,
|
|
COALESCE(
|
|
NULLIF(COALESCE(po.actual_price, po.purchase_price), 0),
|
|
NULLIF(li.insp_price, 0)
|
|
) AS better_price
|
|
FROM latest_insp li
|
|
LEFT JOIN purchase_orders po ON po.order_code = li.order_code AND po.sku_code = li.sku_code
|
|
LEFT JOIN LATERAL (
|
|
SELECT price FROM order_items oi
|
|
WHERE oi.order_code = li.order_code AND oi.sku_code = li.sku_code LIMIT 1
|
|
) oi ON TRUE
|
|
),
|
|
sku_cost AS (
|
|
SELECT sku_code,
|
|
ROUND(SUM(actual_qty * insp_price)::numeric,2) AS cost_insp,
|
|
ROUND(SUM(actual_qty * better_price)::numeric,2) AS cost_better,
|
|
COUNT(*) FILTER (
|
|
WHERE po_price IS NOT NULL AND po_price IS DISTINCT FROM insp_price
|
|
)::int AS po_diff_lines
|
|
FROM priced
|
|
WHERE COALESCE(better_price,0) > 0
|
|
GROUP BY sku_code
|
|
),
|
|
sales AS (
|
|
SELECT oi.sku_code,
|
|
ROUND(SUM(CASE WHEN COALESCE(oi.amount,0)>0 THEN oi.amount
|
|
ELSE oi.price*oi.quantity END)::numeric,2) AS sales
|
|
FROM order_items oi
|
|
JOIN orders o ON o.order_code = oi.order_code
|
|
WHERE o.cycle_code = $1 AND o.order_status >= 20
|
|
GROUP BY oi.sku_code
|
|
)
|
|
SELECT COUNT(*) FILTER (WHERE s.sales > 0 AND ABS(s.sales - c.cost_insp) < 0.011)::int AS zero_with_insp,
|
|
COUNT(*) FILTER (WHERE s.sales > 0 AND ABS(s.sales - c.cost_better) < 0.011)::int AS zero_with_better,
|
|
COUNT(*) FILTER (WHERE s.sales > 0 AND (s.sales - c.cost_insp) / s.sales * 100 > 1)::int AS gt1pct_insp,
|
|
COUNT(*) FILTER (WHERE s.sales > 0 AND (s.sales - c.cost_better) / s.sales * 100 > 1)::int AS gt1pct_better,
|
|
ROUND(SUM(c.cost_insp)::numeric,2) AS sum_insp,
|
|
ROUND(SUM(c.cost_better)::numeric,2) AS sum_better,
|
|
SUM(c.po_diff_lines)::int AS po_diff_lines
|
|
FROM sku_cost c
|
|
JOIN sales s ON s.sku_code = c.sku_code`,
|
|
[cycle]
|
|
);
|
|
console.log('rebuild simulation', sim.rows[0]);
|
|
|
|
// supplier prep: is supply price intentionally = sell?
|
|
const prep = await pool.query(
|
|
`SELECT COUNT(*)::int AS n,
|
|
COUNT(*) FILTER (WHERE sp.purchase_price IS NOT DISTINCT FROM oi.price)::int AS prep_eq_sell
|
|
FROM supplier_preparations sp
|
|
JOIN orders o ON o.order_code = sp.order_code AND o.cycle_code = $1
|
|
JOIN LATERAL (
|
|
SELECT price FROM order_items oi
|
|
WHERE oi.order_code = sp.order_code AND oi.sku_code = sp.sku_code LIMIT 1
|
|
) oi ON TRUE
|
|
WHERE COALESCE(sp.purchase_price,0) > 0`,
|
|
[cycle]
|
|
);
|
|
console.log('prep vs sell', prep.rows[0]);
|
|
|
|
await pool.end();
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|