'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;