Files
SupplierSystem/server/tests/inboundListPriceCaliber.test.ts
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

67 lines
3.1 KiB
TypeScript

import { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* 入库列表「单价 / 金额」口径(2026-09-24 开发者裁决)。
*
* 口径:**入库单价 = 真实成本价,取「订单行当前成本价」优先**,其后依次
* 入库时的实收快照 → 备货价 → 独立入库单价 → 0。
*
* 背景(为什么不是"快照优先"):原实现把「备货价」放第一、`inspections.purchase_price` 第二,
* 且 2026-09-22 `orderEnteredCostWritebackService` 明确排除「已验收(入库)」行 →
* 入库当时写下的快照**永远不再变**,于是「订单里改了成本价、入库列表不跟着动」。
* 2026-09-24 开发者要求:订单侧改成本价,入库侧价格必须跟着变;且入库单价要等于真实成本价。
*
* 注意:这是**显示层**取值改动 —— 不改写历史 `inspections.purchase_price`、
* 不改写备货价/自采单(数据层仍按 2026-09-22 的「已结算/已入库排除」保护)。
*/
const SRC = readFileSync(
path.join(__dirname, '../src/services/inspectionQueryService.ts'),
'utf8'
);
/** 取 inboundPriceSql 那段(从定义到 `)::numeric` 结束) */
function inboundPriceSql(): string {
const start = SRC.indexOf('const inboundPriceSql = `');
expect(start).toBeGreaterThan(-1);
const end = SRC.indexOf(')::numeric`', start);
expect(end).toBeGreaterThan(start);
return SRC.slice(start, end);
}
describe('入库列表单价口径:订单行当前成本优先', () => {
it('第①优先是订单行 unit_cost(订单改价后入库跟着变)', () => {
const sql = inboundPriceSql();
const idxOrderItem = sql.indexOf('SELECT oi.unit_cost');
const idxSnapshot = sql.indexOf('NULLIF(i.purchase_price, 0)');
const idxPrep = sql.indexOf('SELECT sp.purchase_price');
const idxSpi = sql.indexOf('SELECT spi.unit_cost');
expect(idxOrderItem).toBeGreaterThan(-1);
// 顺序:订单行成本 → 实收快照 → 备货价 → 独立入库单价(原实现备货价在第一,必须已挪后)
expect(idxOrderItem).toBeLessThan(idxSnapshot);
expect(idxSnapshot).toBeLessThan(idxPrep);
expect(idxPrep).toBeLessThan(idxSpi);
});
it('订单行成本不受 0 值影响(显式 0 是真实成本)', () => {
// 直接读 oi.unit_cost,禁止写成 NULLIF(oi.unit_cost, 0) 之类把 0 当缺失
const sql = inboundPriceSql();
expect(sql).toContain('SELECT oi.unit_cost');
expect(sql).not.toContain('NULLIF(oi.unit_cost');
});
it('同单同 SKU 多行时按「供应商一致 → sort_order → id」确定性取行', () => {
const sql = inboundPriceSql();
expect(sql).toContain("CASE WHEN COALESCE(oi.supplier_code, '') = COALESCE(i.supplier_code, '') THEN 0 ELSE 1 END");
expect(sql).toContain('oi.sort_order ASC NULLS LAST');
});
it('该表达式同时用于列表行单价与表脚金额(两处必须同源)', () => {
expect(SRC).toContain('${inboundPriceSql} AS inbound_unit_price');
expect(SRC).toContain('COALESCE(SUM(${inboundQtySql} * ${inboundPriceSql}), 0)');
});
});