oms/backend/app/utils/rejectBillNo.js
liufumingren ec81f8b3cd feat(backend): 入库单/出库单全流程与 WMS 对接
- 入库单、出库单控制器与模型(状态流转、审核、驳回换号、作废、删除约束)
- 明细共用 stock_items:应/已数量、行级库存类型 ZP/CC、明细锁定
- lib/wms:单据类型常量(orderTypes)、奇门/openapi 客户端(qimenClient)、签名(sign)
- 库存联动:utils/stock.js、utils/completeInbound.js(入库完成加库存,幂等)
- 驳回换号:utils/rejectBillNo.js;旧 orders 模块同步调整
- 相关迁移:入库单 WMS 字段/审核/推送结果、明细计划与实际数量、单据类型落 WMS 编码、出库库存扣减标记等
2026-09-13 02:23:44 +08:00

34 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 驳回时生成新单号:在基础单号后追加 / 递增 `_N` 后缀(首次 `_1`,再次 `_2` …)。
*
* 背景:已推送(3) 的单据被驳回后回到待处理,若沿用原单号再次推送,外部系统会因
* 「单据已存在」(入库单无法取消时必然发生)或沿用已作废的外部单据而失败;
* 驳回即换号可保证「一次推送对应一个全新的外部单号」。
*
* 基础单号识别规则:
* 1) 系统生成的「{前缀}_{自增id}」(如 RK_2 / CK_2)视为**无后缀**的基础单号,
* 避免把 id 里的 `_2` 误判成后缀(这是与历史手填单号最大的差异点);
* 2) 其它单号(如历史单号 `12`)剥离末尾 `_数字` 作为基础单号。
*
* @param {string} billNo 当前单号
* @param {number|string} id 单据主键(用于还原系统生成的单号)
* @param {string} prefix 单号前缀,入库 RK / 出库 CK
* @return {string} 驳回后的新单号,如 RK_2 -> RK_2_1 -> RK_2_2
*/
function nextRejectedBillNo(billNo, id, prefix) {
const current = String(billNo == null ? '' : billNo).trim();
if (!current) return current;
const systemBase = `${prefix}_${id}`;
const matched = current.match(/^(.+)_(\d+)$/);
// 系统生成的单号本身带 `_id`,先判等,再决定是否剥离后缀
const isSystemBase = current === systemBase;
const base = isSystemBase || !matched ? current : matched[1];
const seq = !isSystemBase && matched && matched[1] === base ? Number(matched[2]) + 1 : 1;
return `${base}_${seq}`;
}
module.exports = nextRejectedBillNo;