feat(抵扣批次): 列表「库存抵扣:…」点开看已抵扣批次明细(几号入库/生产日期/保质期/区域/抵扣多少)

开发者:「还有将已经被抵扣的批次展示出来呀 —— 几号入库的、生产日期、保质期、区域、
抵扣了多少都需要展示的」。展示面当轮确认:订单列表那行点开弹窗。

- 新增只读接口 GET /orders/:orderCode/stock-cover-batches(鉴权 + 委派,路由零 SQL),
  权限与 batch-reports/quality-report-files 同一套
- 批次来源①事实:picking_items.outbound_batches(抵扣写的 FIFO 计划 / 分拣选的批次)
  → source=plan;②无计划时按先进先出现算(ORDER BY created_at ASC, expiry_date ASC NULLS FIRST,
  与 stockCoverBatchPlanService.loadFifoBatches 逐字一致,有源码级守卫)→ source=fifo
- 字段:批次号 / 抵扣数量 / 入库日期 / 生产日期 / 保质期效期 / 区域;只列已抵扣且未退货的行;
  批次元数据从 inventory 现查(同一 batch_no 跨 SKU 重名 ⇒ 键带 sku_code)
- 前端:新弹窗 OrderStockCoverBatchesModal(加载/失败/空态齐、generation+订单号丢弃过期响应);
  列表那行有权限才可点且 stopPropagation(否则连带打开详情),无权限退回原文字
- 不新增界面文案:标题与字段标签全部复用既有词条(orders.list.inventoryOffset /
  inventory.inboundDate / inventory.col.prodDate / inventory.col.expiry / picking.batch.noZone 等),
  有契约测试逐键校验三语存在、且弹窗代码无硬编码中文
- 真库实测:纯抵扣单 ORD_API_20261013_BR003_0001 抄手皮×8 → STK202610118WEOS7
  (入库 2026-10-11 · 生产 2026-10-08 · 效期 2026-10-15 · 冷库A)source=fifo;
  有计划单 ORD_API_20261012_BR003_0002 三行 source=plan,批次/数量与库里逐字一致
- 设计文档:docs/design/20261013-订单抵扣批次明细.md
This commit is contained in:
Ubuntu
2026-10-13 10:11:38 +08:00
parent 164a8a6123
commit 65d56ee988
9 changed files with 874 additions and 5 deletions
@@ -0,0 +1,104 @@
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
/**
* 列表「库存抵扣:…」点开看**已抵扣批次明细**(2026-10-13 开发者要求):
* 批次号 / 抵扣数量 / 几号入库 / 生产日期 / 保质期(效期)/ 区域。
*
* 钉住三件容易做反的事:
* ① **不新增文案**:弹窗里的每个 `t('…')` 都必须是三语已有的词条(界面文案属产品决策,
* 没有明确要求不许自己造词 —— 见 `.cursor/rules/frontend-copy-changes.mdc`);
* ② **过期响应不得写状态**:关掉/切单后旧响应回来必须被丢弃(generation + 订单号);
* ③ 列表那行的入口必须 `stopPropagation`,否则点一下会连带打开订单详情。
*/
const modal = readFileSync(
new URL('./OrderStockCoverBatchesModal.tsx', import.meta.url),
'utf8'
);
const page = readFileSync(
new URL('../../pages/orders/orderManagement/OrderManagementPage.tsx', import.meta.url),
'utf8'
);
const api = readFileSync(new URL('../../utils/workflowApi.ts', import.meta.url), 'utf8');
const locales = ['zh-CN', 'en', 'id'] as const;
const localeSource = (locale: string, ns: string) =>
readFileSync(new URL(`../../i18n/locales/${locale}/${ns}.ts`, import.meta.url), 'utf8');
/** 弹窗用到的所有 i18n key */
function usedKeys(): string[] {
const keys = new Set<string>();
for (const match of modal.matchAll(/t\('([^']+)'/g)) keys.add(match[1]);
return [...keys];
}
describe('已抵扣批次明细弹窗', () => {
it('① 接口打到只读端点,类型与字段齐(入库/生产/效期/区域/数量)', () => {
expect(api).toContain('export async function listOrderStockCoverBatchesApi');
expect(api).toContain('/stock-cover-batches');
for (const field of ['inbound_date', 'production_date', 'expiry_date', 'storage_zone']) {
expect(api).toContain(field);
}
});
it('② 不新增文案:用到的 key 三语全都已存在', () => {
const keys = usedKeys();
// 这几个是本次展示必需的既有词条,明确点名(少一个就说明被改成了自造词)
expect(keys).toContain('orders.list.inventoryOffset');
expect(keys).toContain('inventory.inboundDate');
expect(keys).toContain('inventory.col.prodDate');
expect(keys).toContain('inventory.col.expiry');
expect(keys).toContain('picking.batch.noZone');
expect(keys).toContain('picking.batch.none');
expect(keys).toContain('picking.batch.fail');
expect(keys).toContain('common.loading');
expect(keys).toContain('common.close');
for (const key of keys) {
const [ns, ...rest] = key.split('.');
const leaf = rest.join('.');
for (const locale of locales) {
expect(
localeSource(locale, ns).includes(`'${leaf}'`) || localeSource(locale, ns).includes(`${leaf}:`),
`${locale}/${ns}.ts 缺少词条 ${key}`
).toBe(true);
}
}
// 不许在弹窗里硬编码中文(注释除外:只查代码行)
const codeOnly = modal
.split('\n')
.filter((line) => {
const trimmed = line.trim();
return !trimmed.startsWith('*') && !trimmed.startsWith('//') && !trimmed.startsWith('/*');
})
.join('\n');
expect(codeOnly).not.toMatch(/[\u4e00-\u9fa5]/);
});
it('③ 过期响应不得写状态:generation + 订单号守卫', () => {
expect(modal).toContain('genRef');
expect(modal).toContain('if (gen === genRef.current)');
expect(modal).toContain('genRef.current += 1');
expect(modal).toContain("String(orderCode || '').trim()");
});
it('④ 批次行把五个字段都渲染出来(缺一个就是漏需求)', () => {
expect(modal).toContain('batch.batch_no');
expect(modal).toContain('batch.qty');
expect(modal).toContain("t('inventory.inboundDate'");
expect(modal).toContain("t('inventory.col.prodDate')");
expect(modal).toContain("t('inventory.col.expiry')");
expect(modal).toContain('batch.storage_zone');
// 空值不许渲染成空白
expect(modal).toContain("const dateOrDash");
});
it('⑤ 列表入口:有权限才可点,点了 stopPropagation,并挂上弹窗', () => {
expect(page).toContain('OrderStockCoverBatchesModal');
expect(page).toContain('coverBatchesOrderCode');
expect(page).toContain('canViewCoverBatches');
expect(page).toContain('setCoverBatchesOrderCode(o.order_code)');
// 无权限时退回原来的纯文字
expect(page).toContain("t('orders.list.stockPrefix'");
});
});
@@ -0,0 +1,151 @@
import React, { useEffect, useRef, useState } from 'react';
import { X } from 'lucide-react';
import { useI18n } from '../../i18n';
import { notify } from '../../utils/notify';
import { listOrderStockCoverBatchesApi, type OrderStockCoverBatchItem } from '../../utils/workflowApi';
/**
* 订单列表那行「库存抵扣:…」点开 ⇒ 看**已抵扣的批次明细**(2026-10-13 开发者要求):
* 「几号入库的、生产日期、保质期、区域、抵扣了多少都需要展示」。
*
* 数据:`GET /orders/:code/stock-cover-batches`(只读)。
* · `source='plan'` —— 库里已有的批次事实(抵扣时写的 FIFO 计划 / 分拣选的批次);
* · `source='fifo'` —— 该行还没有计划,按**先进先出**(先入库先出)算给操作员看的。
* 文案:**不新增词条** —— 标题复用 `orders.list.inventoryOffset`(库存抵扣),
* 字段标签复用 `inventory.inboundDate` / `inventory.col.prodDate` / `inventory.col.expiry`,
* 无区域复用 `picking.batch.noZone`,加载/失败/空态复用 `common.loading` / `picking.batch.fail` /
* `picking.batch.none`。
*
* 响应按「generation + 订单号」丢弃过期(关掉再打开、切单后旧响应不得写状态)。
*/
export interface OrderStockCoverBatchesModalProps {
open: boolean;
orderCode: string;
onClose: () => void;
}
export const OrderStockCoverBatchesModal: React.FC<OrderStockCoverBatchesModalProps> = ({
open,
orderCode,
onClose,
}) => {
const { t } = useI18n();
const [items, setItems] = useState<OrderStockCoverBatchItem[]>([]);
const [loading, setLoading] = useState(false);
const genRef = useRef(0);
useEffect(() => {
if (!open) return;
const code = String(orderCode || '').trim();
if (!code) {
setItems([]);
return;
}
const gen = ++genRef.current;
setLoading(true);
void listOrderStockCoverBatchesApi(code)
.then((res) => {
if (gen === genRef.current) setItems(res.items || []);
})
.catch(() => {
if (gen === genRef.current) {
setItems([]);
notify.error(t('picking.batch.fail'));
}
})
.finally(() => {
if (gen === genRef.current) setLoading(false);
});
}, [open, orderCode, t]);
useEffect(() => {
if (!open) {
// 关掉即作废在途响应,并清空,避免下次打开闪旧数据
genRef.current += 1;
setItems([]);
setLoading(false);
}
}, [open]);
if (!open) return null;
const dateOrDash = (value: string | null | undefined) => (value ? String(value) : '—');
return (
<div
className="fixed inset-0 z-[110] bg-black/50 flex items-center justify-center p-4"
onClick={onClose}
role="dialog"
aria-modal="true"
aria-label={t('orders.list.inventoryOffset')}
>
<div
className="app-surface w-full max-w-xl rounded-xl border app-border shadow-xl p-4 max-h-[85vh] flex flex-col gap-3"
onClick={(event) => event.stopPropagation()}
>
<div className="flex items-center justify-between gap-2">
<div className="min-w-0">
<h3 className="text-base font-semibold">{t('orders.list.inventoryOffset')}</h3>
<p className="text-xs text-slate-500 truncate">{orderCode}</p>
</div>
<button type="button" onClick={onClose} aria-label={t('common.close')}>
<X className="w-5 h-5 text-slate-400" />
</button>
</div>
<div className="overflow-y-auto border app-border rounded-lg max-h-[60vh] divide-y divide-slate-100 dark:divide-slate-800">
{loading ? (
<p className="px-3 py-6 text-sm text-slate-400 text-center">{t('common.loading')}</p>
) : items.length === 0 ? (
<p className="px-3 py-6 text-sm text-slate-400 text-center">{t('picking.batch.none')}</p>
) : (
items.map((item) => (
<div key={item.order_item_id} className="px-3 py-2.5">
<div className="flex items-baseline gap-2">
<span className="text-sm font-medium truncate">
{item.item_name || item.sku_code || '—'}
</span>
<span className="ml-auto text-xs text-slate-500 shrink-0">
×{item.cover_qty}
{item.unit || ''}
</span>
</div>
{item.sku_code ? (
<p className="text-xs text-slate-400 truncate">{item.sku_code}</p>
) : null}
{item.batches.length === 0 ? (
<p className="mt-1 text-xs text-slate-400">{t('picking.batch.none')}</p>
) : (
<div className="mt-1.5 space-y-1.5">
{item.batches.map((batch) => (
<div key={`${item.order_item_id}-${batch.batch_no}`} className="min-w-0">
<div className="flex items-baseline gap-2">
<span className="text-xs text-slate-700 dark:text-slate-200 truncate">
{batch.batch_no}
</span>
<span className="ml-auto text-xs text-slate-500 shrink-0">
×{batch.qty}
{item.unit || ''}
</span>
</div>
<p className="text-xs text-slate-500 truncate">
{batch.storage_zone || t('picking.batch.noZone')}
{' · '}
{t('inventory.inboundDate', { date: dateOrDash(batch.inbound_date) })}
{' · '}
{`${t('inventory.col.prodDate')} ${dateOrDash(batch.production_date)}`}
{' · '}
{`${t('inventory.col.expiry')} ${dateOrDash(batch.expiry_date)}`}
</p>
</div>
))}
</div>
)}
</div>
))
)}
</div>
</div>
</div>
);
};