Files
SupplierSystem/scripts/verify-purchase-cost-sku.cjs
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

125 lines
3.9 KiB
JavaScript

/**
* 核验采购成本 order+sku 归因(海天白醋 / 细木耳等)。
* node scripts/verify-purchase-cost-sku.cjs [--cycle=202607]
*/
const fs = require('fs');
const path = require('path');
const { Client } = require('../server/node_modules/pg');
function loadDatabaseUrl() {
if (process.env.DATABASE_URL) return process.env.DATABASE_URL;
const envPath = path.join(__dirname, '..', 'server', '.env');
const text = fs.readFileSync(envPath, 'utf8');
const m = text.match(/^DATABASE_URL\s*=\s*(.+)$/m);
return m ? m[1].trim().replace(/^["']|["']$/g, '') : null;
}
function round2(n) {
return Math.round(Number(n || 0) * 100) / 100;
}
const cycle = (process.argv.find((a) => a.startsWith('--cycle=')) || '--cycle=202607').split(
'='
)[1];
(async () => {
const c = new Client({ connectionString: loadDatabaseUrl() });
await c.connect();
const cases = [
{
name: '海天白醋',
sku_code: 'SKU78663476563',
order_code: 'ORD1783853251437',
// 验收行合计(本 SKU);禁止再出现整单 ~3024
maxPurchase: 200,
},
{
name: '细木耳',
sku_code: 'SKU78665536419',
order_code: 'ORD1783852744241',
maxPurchase: 100,
},
];
const results = [];
let failed = 0;
for (const tc of cases) {
const insp = await c.query(
`SELECT ROUND(SUM(COALESCE(actual_qty,0)*COALESCE(purchase_price,0))::numeric,2) AS amt
FROM inspections WHERE order_code=$1 AND sku_code=$2`,
[tc.order_code, tc.sku_code]
);
const purchase = await c.query(
`SELECT COALESCE(SUM(amount),0)::numeric AS amt, COUNT(*)::int AS n
FROM cost_details
WHERE cost_type='purchase' AND order_code=$1 AND sku_code=$2`,
[tc.order_code, tc.sku_code]
);
const orderTotal = await c.query(
`SELECT COALESCE(SUM(amount),0)::numeric AS amt, COUNT(*)::int AS n
FROM cost_details WHERE cost_type='purchase' AND order_code=$1`,
[tc.order_code]
);
const inspOrder = await c.query(
`SELECT ROUND(SUM(COALESCE(actual_qty,0)*COALESCE(purchase_price,0))::numeric,2) AS amt
FROM inspections WHERE order_code=$1`,
[tc.order_code]
);
const skuPurchase = round2(purchase.rows[0].amt);
const skuInsp = round2(insp.rows[0].amt);
const okSku =
Math.abs(skuPurchase - skuInsp) < 0.02 &&
skuPurchase > 0 &&
skuPurchase <= tc.maxPurchase &&
skuPurchase < round2(orderTotal.rows[0].amt) * 0.5;
const okOrder =
Math.abs(round2(orderTotal.rows[0].amt) - round2(inspOrder.rows[0].amt)) < 0.05 &&
orderTotal.rows[0].n > 1;
if (!okSku || !okOrder) failed += 1;
results.push({
...tc,
sku_inspection_cost: skuInsp,
sku_purchase_cost: skuPurchase,
sku_purchase_rows: purchase.rows[0].n,
order_purchase_total: round2(orderTotal.rows[0].amt),
order_inspection_total: round2(inspOrder.rows[0].amt),
order_purchase_rows: orderTotal.rows[0].n,
ok_sku: okSku,
ok_order_total: okOrder,
});
}
const top = await c.query(
`SELECT cd.sku_code, s.sku_name, SUM(cd.amount)::numeric AS purchase_cost
FROM cost_details cd
LEFT JOIN skus s ON s.sku_code = cd.sku_code
WHERE cd.cost_type='purchase' AND cd.cycle_code=$1 AND cd.sku_code IS NOT NULL
GROUP BY cd.sku_code, s.sku_name
ORDER BY SUM(cd.amount) DESC
LIMIT 5`,
[cycle]
);
const multiSkuOrders = await c.query(
`SELECT order_code, COUNT(*)::int AS n
FROM cost_details
WHERE cost_type='purchase' AND cycle_code=$1 AND sku_code IS NOT NULL
GROUP BY order_code
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
LIMIT 5`,
[cycle]
);
console.log(JSON.stringify({ cycle, results, top5: top.rows, multiSkuOrders: multiSkuOrders.rows, failed }, null, 2));
await c.end();
process.exit(failed > 0 ? 1 : 0);
})().catch((e) => {
console.error(e);
process.exit(1);
});