feat(订单列表): 抵扣批次按钮改为立即重跑库存抵扣
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

- 状态行库存抵扣仍打开批次明细
- 操作列库存抵扣立即重跑并刷新,覆盖我方录单
- 非主系统单只重算本单 cover,不重新分配供应商
- 独立进货单仍禁止抵扣
This commit is contained in:
Ubuntu
2026-10-26 16:54:13 +08:00
parent 05b81348c4
commit db0cbb0fd9
4 changed files with 38 additions and 26 deletions
@@ -847,6 +847,12 @@ router.post('/orders/:orderCode/inventory-offset', authMiddleware, async (req: A
branchCode,
orderStatus: Number(order.order_status) || 0,
});
if (outcome.kind === 'independent_inbound') {
return res.status(400).json({
error: 'independent_inbound_no_offset',
error_description: '独立进货单不能用库存抵扣',
});
}
if (outcome.kind === 'not_main_system_order') {
return res.status(400).json({
error: 'not_main_system_order',
+22 -3
View File
@@ -2888,9 +2888,10 @@ export async function applyInventoryOffsetForMainSystemOrder(input: {
coveredAfter: number;
coveredDelta: number;
message: string;
allocation: ReprocessOrderResult;
allocation?: ReprocessOrderResult;
}
| { kind: 'not_main_system_order' }
| { kind: 'independent_inbound' }
| { kind: 'too_late'; status: number }
| { kind: 'invalid'; reason: string }
> {
@@ -2905,11 +2906,12 @@ export async function applyInventoryOffsetForMainSystemOrder(input: {
LIMIT 1`,
[branchCode, orderCode]
);
if (!ledger.rowCount) return { kind: 'not_main_system_order' };
const status = Number(input.orderStatus) || 0;
// 发车/配送中(>=50)已在扣库与出库,重跑抵扣会与在途库存打架
if (status >= 50) return { kind: 'too_late', status };
if (await isOrderIndependentInbound(orderCode, pool)) {
return { kind: 'independent_inbound' };
}
const sumCover = async (): Promise<number> => {
const res = await pool.query<{ n: number }>(
@@ -2919,6 +2921,23 @@ export async function applyInventoryOffsetForMainSystemOrder(input: {
return Number(res.rows[0]?.n) || 0;
};
const before = await sumCover();
// 非主系统录单也可重跑抵扣:只按本单现有明细重算 stock_cover,不重新分配供应商/不改已分配行。
if (!ledger.rowCount) {
const cover = await applyStockCoverForOrder(orderCode, branchCode);
const after = await sumCover();
const delta = Math.round((after - before) * 1000) / 1000;
const zeroReason = cover.zeroReasons?.[0];
return {
kind: 'ok',
coveredBefore: before,
coveredAfter: after,
coveredDelta: delta,
message:
delta > 0
? `库存抵扣完成:本次新增抵扣 ${delta}`
: `库存抵扣完成:本次未新增抵扣。原因:${zeroReason?.message || '无可抵扣量'}`,
};
}
/**
* ⚠️ **只抵扣、不替人分配**(2026-10-13 开发者口径):
* 这个入口是「库存抵扣」按钮 / 「选择抵扣商品」保存后的重跑。此前不带选项 ⇒
+8 -4
View File
@@ -35,6 +35,8 @@ describe('后端:POST /orders/:orderCode/inventory-offset', () => {
const svc = allocSvc.slice(svcIdx, svcIdx + 3200);
expect(svc).toContain('main_system_order_imports');
expect(svc).toContain("kind: 'not_main_system_order'");
expect(svc).toContain("kind: 'independent_inbound'");
expect(svc).toContain('applyStockCoverForOrder(orderCode, branchCode)');
expect(svc).toContain("kind: 'too_late'");
expect(svc).toContain('status >= 50');
// 路由只是薄壳:错误码映射 + 调用服务(SQL 一律在 service 里)
@@ -60,8 +62,8 @@ describe('后端:POST /orders/:orderCode/inventory-offset', () => {
* 抵扣从此轮不到它),与开发者「不能替他直接分配好给谁」正相反。
*/
expect(svc).toContain('preferItemSuppliers: true');
// 不直接调 applyStockCoverForOrder(那会留下"既抵扣又采购"的双记)
expect(svc).not.toContain('applyStockCoverForOrder(');
// 主系统单仍走分配路线;非主系统单只重算 stock_cover(上面的分支)
expect(svc).toContain('applyStockCoverForOrder(orderCode, branchCode)');
// 返回可展示的结果(抵扣增量)
expect(svc).toContain('coveredDelta');
expect(svc).toContain('message:');
@@ -69,14 +71,16 @@ describe('后端:POST /orders/:orderCode/inventory-offset', () => {
});
describe('前端:按钮的出现条件与在途锁', () => {
it('只在主系统分配单且状态 < 50 时出现', () => {
it('非独立进货、有抵扣项、状态 < 50 时出现', () => {
const idx = page.indexOf('const canApplyInventoryOffset');
expect(idx, '未找到出现条件').toBeGreaterThan(-1);
const fn = page.slice(idx, idx + 240);
expect(fn).toContain('order.is_main_system_order === true');
expect(fn).not.toContain('order.is_main_system_order === true');
expect(fn).toContain('stock_cover_items');
expect(fn).toContain('Number(order.order_status) < 50');
// 列表行里用它包住按钮(不是无条件渲染)
expect(page).toMatch(/canApplyInventoryOffset\(o\) \? \(/);
expect(page).not.toContain("t('orders.list.viewCoverBatches')");
});
it('有在途锁:点击期间禁用、结束后释放', () => {
@@ -3145,9 +3145,9 @@ export const OrderManagement: React.FC = () => {
* 后端最低层也已拦(`inventoryStockCoverService.applyStockCoverForOrderItem` 对哨兵单直接返回 0)。
*/
const canApplyInventoryOffset = (order: Order) =>
order.is_main_system_order === true &&
Number(order.order_status) < 50 &&
order.customer_code !== INDEPENDENT_INBOUND_CUSTOMER_CODE;
order.customer_code !== INDEPENDENT_INBOUND_CUSTOMER_CODE &&
(order.stock_cover_items?.length ?? 0) > 0;
const handleInventoryOffset = async (order: Order) => {
if (!canApplyInventoryOffset(order) || inventoryOffsetCode === order.order_code) return;
setInventoryOffsetCode(order.order_code);
@@ -4320,23 +4320,6 @@ export const OrderManagement: React.FC = () => {
>
{t('orders.list.view')}
</button>
{/**
* **列表直开「抵扣批次」弹窗**(2026-10-23 开发者:
* 「最好是无需打开订单详情」)——不必先展开详情,操作列直接开。
*/}
{canViewCoverBatches && (o.stock_cover_items?.length ?? 0) > 0 ? (
<button
type="button"
onClick={(e) => {
e.stopPropagation();
setCoverBatchesOrderCode(o.order_code);
}}
className="px-2 py-1 text-xs font-medium text-indigo-700 hover:bg-indigo-50 rounded-lg transition-colors"
title={t('orders.list.viewCoverBatchesTitle')}
>
{t('orders.list.viewCoverBatches')}
</button>
) : null}
{canApplyInventoryOffset(o) ? (
<button
type="button"