原改动人:雨天 <oncwnuHbbvAwYZMdPF78PsWNPadI@git.weixin.qq.com> 代为提交:Lead 会话(本 commit 内容与该会话工作区逐字节一致,未做任何修改), 目的是不让在研成果丢失、并让工作树回到干净可部署状态。 提交前由 Lead 实跑验证: - 前端 npx tsc --noEmit 0 错误;后端 server npx tsc --noEmit 0 错误 - 本批新增的 7 个测试 54/54 通过;Lead 侧契约测试(散客录单/文案/成本列)42/42 通过 - bash tools/design-consistency/run-all.sh --verify:通过 8 / 未通过 1(check-ai-cost,既有红)/ 未执行 1(verify-facts,本地库不可连)—— 与基线一致 新增能力(各含 docs/design 设计文): - 独立进货不得有配送任务:生成侧兜底 + 补建候选 + 查询侧三道防线(实测 5 张独立进货单里 4 张有僵尸 delivery_tasks) - 主系统追加行零价闸门尊重其已声明的 allowUnpriced(否则整批分配行静默丢失) - 库存抵扣为 0 的原因可见(不再静默,原因判定与抵扣量同源并落到日志与「库存抵扣」入口) - 退货/删单跟随改独立 2 分钟周期(MAIN_SYSTEM_RETURN_FOLLOW_SEC,置 0 退回旧行为) - 加工领料可用本单自己的预留(放行必须与本单预留消费成对,避免短送与同一批算两次) - 出库拍照预览、主系统实收 / SKU 映射 / 单位变更 inbox 等配套 同一批包含对已废弃旧方案的回滚(design doc 与 README 索引行一并移除): - 独立进货建客户档案(方案甲)→ 回到哨兵方案;随之删除 migrations/20261044_independent_inbound_customer.sql - 分类新建去重(同名复用已有) - 分拣标签纸张尺寸预设 / 分拣标签可选客户名与区域 未包含:src/pages/orders/OrderManagementPage.tsx(0 字节空文件,疑似误建,既未提交也未删除)
78 lines
4.0 KiB
TypeScript
78 lines
4.0 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
/**
|
|
* **已出库的行退货 ⇒ 货回仓**(开发者 2026-10-13 口径)。
|
|
*
|
|
* 背景:分拣完毕即出库扣库存之后,主系统此时点退货 ⇒ 货其实还在/回到我方仓,
|
|
* 必须按**原批次**加回库存,否则"货回了、账上没有";改前 `stock_action='already_out'` 是**完全不动库存**的。
|
|
*
|
|
* 口径(开发者逐条确认):回补量 = **核货量**(拣 30、核货 29 ⇒ 回补 29);
|
|
* 差额必须**留痕**(新增 `return_shortage` 流水,前端三语文案同提交)。
|
|
*
|
|
* 设计:docs/design/20261013-分拣完毕即出库扣库存(全单统一).md §12
|
|
*/
|
|
const SRC = path.join(__dirname, '../src/services/orderItemReturnService.ts');
|
|
const src = readFileSync(SRC, 'utf8');
|
|
|
|
describe('已出库退货 ⇒ 货回仓', () => {
|
|
it('① 退货主流程在「已出库」时调用回补(未出库那条 reverse_in 行为不变)', () => {
|
|
expect(src).toContain('export async function restoreOutboundStockForReturn(');
|
|
const callIdx = src.indexOf('await restoreOutboundStockForReturn(db, {');
|
|
const reverseIdx = src.indexOf("if (stockAction === 'reverse_in') {");
|
|
expect(callIdx).toBeGreaterThan(-1);
|
|
expect(reverseIdx).toBeGreaterThan(-1);
|
|
// 回补挂在 reverse_in 之后(两条互斥:alreadyOut 与 reverse_in 不会同时成立)
|
|
expect(callIdx).toBeGreaterThan(reverseIdx);
|
|
expect(src.slice(callIdx - 400, callIdx)).toContain('if (alreadyOut)');
|
|
});
|
|
|
|
it('② 回补量按核货量(loaded_qty),没有核货量才退回出库总量', () => {
|
|
expect(src).toContain('SELECT COALESCE(SUM(loaded_qty), 0)::numeric AS qty FROM picking_items WHERE order_item_id = $1::uuid');
|
|
expect(src).toContain('if (target <= 0) target = outTotal;');
|
|
});
|
|
|
|
it('③ 差额写return_shortage 流水(负数 + 分拣损耗留痕),便于盘点对账', () => {
|
|
expect(src).toContain("'return_shortage'");
|
|
expect(src).toContain('退货差额(分拣损耗)');
|
|
expect(src).toContain('-shortage');
|
|
});
|
|
|
|
it('④ 幂等:该行已有 order_return_in 流水则直接返回', () => {
|
|
expect(src).toContain("operation_type = 'order_return_in'");
|
|
expect(src).toContain('return { restored: 0, shortage: 0, already: true };');
|
|
});
|
|
|
|
it('⑤ 回补到出库流水里的原批次,并把账外余额按份额退回', () => {
|
|
expect(src).toContain("operation_type IN ('picking_out', 'delivery_loading_out')");
|
|
expect(src).toContain('WHERE batch_no = $1');
|
|
expect(src).toContain("cost_source = 'inventory_cover'");
|
|
expect(src).toContain("costSource: 'cover_revert'");
|
|
});
|
|
|
|
it('⑤b 回补加库必须同时按 sku_code 限定(batch_no 非全局唯一,同批号跨 SKU 不得一起加)', () => {
|
|
/**
|
|
* `inventory` 唯一约束是 `(sku_code, batch_no)`:同一 batch_no 在不同 SKU 下可重名
|
|
* (批次号可由入库单手工指定)。只按 batch_no 的 UPDATE 会一次命中多行,
|
|
* 被退 SKU 与无关 SKU 各 +take,而 RETURNING 只取首行 ⇒ 库存账实失真、流水也写错。
|
|
*/
|
|
expect(src).toMatch(/UPDATE inventory[\s\S]{0,160}WHERE batch_no = \$1\s+AND sku_code = \$3/);
|
|
});
|
|
|
|
it('⑥ 不动 order_item_returns.stock_action 的 CHECK(动作以库存流水留痕)', () => {
|
|
expect(src).toContain("export type OrderItemReturnStockAction = 'none' | 'reverse_in' | 'already_out';");
|
|
});
|
|
|
|
it('⑦ 新增的两种库存流水类型在三语里都有文案(冻结规则:新增类型必须同提交补文案)', () => {
|
|
for (const lang of ['zh-CN', 'en', 'id']) {
|
|
const locale = readFileSync(
|
|
path.join(__dirname, `../../src/i18n/locales/${lang}/inventory.ts`),
|
|
'utf8'
|
|
);
|
|
expect(locale, `${lang} 缺 op.order_return_in`).toMatch(/'op\.order_return_in':\s*'[^']+'/);
|
|
expect(locale, `${lang} 缺 op.return_shortage`).toMatch(/'op\.return_shortage':\s*'[^']+'/);
|
|
}
|
|
});
|
|
});
|