Files
SupplierSystem/server/src/services/retailOrder.ts
T
Ubuntu 9aa7483244
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
fix(散客录单): 不再被通用推进显示成「待分拣」;客户显示名改为「散客录单」
开发者 2026-10-15 核查反馈两条:
①「为什么当前录单完毕 散客录单 直接库存抵扣完毕就是待分拣?其他单这样是正常的,但是散客录单是供应商的货物
   已经买出去了,录这个单纯粹就是抵扣库存的呀记录销售的呀」;
②「为什么还叫散客,应该叫散客录单」。

① 状态机收口(根因):`processingTaskService.maybeAdvanceOrderAfterFulfillment` 里本就有
「独立进货:全部入库即自动 90」的分支,而散客单没被拦 ⇒ 录完 + 全额抵扣后被推到 **45「待分拣」**。
现加散客守卫:**不参与状态自动推进**,停在 **30「待确认」**,由人在订单详情点「确认完成」走既有 30→90 通道。
与独立进货的区别写进注释:那张单是**进货**、入库即自动 90;这张单是**销售**、必须人工确认出库后才 90。
另:`confirmRetailOrderComplete` 增加**兼容归一化**(清分拣行 + 40/45/50 → 30 再收口),
让守卫上线前录下的历史散客单也能一键完成(实测:手工置 45 后确认 → 90 成功)。

② 显示名:`RETAIL_CUSTOMER_NAME`「散客」→「散客录单」;`20261046` 迁移同步改(新装即新名);
新增 `20261047_retail_customer_display_name.sql` 改**存量**行(只改旧默认名'散客'、
**不动**用户自定义名;RLS 安全:写入前注入 app.branch_code)。
踩到并修掉:`customers` **没有 `updated_at` 列**(照抄别的表会报错,迁移里已注明)。
本地已应用:br001/br002 均为「散客录单」;用户那张 45 的散客单已按新规则归一化为 30(清掉分拣行)。

真跑验证:建单 30 ⇒ 显式调 maybeAdvanceOrderAfterFulfillment ⇒ `{advanced:false,
message:'散客录单:停在待确认,由「确认完成」收口'}`、状态**仍 30**、分拣任务 **0**;
历史 45 单确认 → `{order_status:90, deducted:true, outbound_types:['retail_out']}`。

验证:server tsc 0 error;vitest 26 failed / 3032 passed(447 文件)——失败文件集**全部落在既有基线**
(comm 与基线清单比对为空),无新增;retail 两个测试文件 23 例全绿。
2026-10-15 09:58:45 +08:00

102 lines
5.0 KiB
TypeScript
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.
/**
* 散客录单(销售单)—— 2026-10-15 开发者立项。
*
* 场景(开发者原话):「散客录单……解决我们的供应商今天卖出去了多少。录单解析完毕就好直接扣库存。
* **不需要采购、不需要分拣、不需要配送**,相当于就是一个销售单。流程为录完单后**默认库存抵扣**,
* 当前全部的库存抵扣完毕后,**点击一下确认就直接是完成**,相当于这个是**全额库存抵扣**,
* **不会给自采与供应商采集**,相当于就是今天他卖出去的散客。」
*
* 设计:docs/design/20261015-散客录单(全额库存抵扣·一键完成).md
* 决策(开发者 2026-10-15 全部选「甲」):
* D1 用**哨兵客户**「散客」识别;D2 库存不足则**拦住**不许确认;D3 **不生成**客户结算单(当场收款);
* D4 入口在**录单页开关**;D5 出库流水**新增 `retail_out`**(三语文案需批准)。
*
* ⚠️ 与「独立进货」(`independentInbound.ts`)的关系:**方向相反**,别混用两者的判定。
* · 独立进货 = **进货**:销售额**不计入**、验收入库、不扣库;
* · 散客 = **销售**:销售额**照常计入**(散客不在任何销售排除名单里)、**出库扣库**、无验收。
* 因此本模块**不提供** `salesVisible` 排除片段,只提供"不得分拣 / 不得配送 / 不得挂应收"三个排除片段。
*
* 关键取舍(与独立进货同源):**哨兵编码**,真实客户编码来自主系统(形如 `MAIN_BR002`),不会冲突。
*/
/** 哨兵客户编码 */
export const RETAIL_CUSTOMER_CODE = '__RETAIL__';
/** 录单下拉里显示的名字(开发者指定:显示为「散客录单」,2026-10-15 由「散客」改名) */
export const RETAIL_CUSTOMER_NAME = '散客录单';
export type RetailCustomerOption = {
customer_code: string;
company_name: string;
address: string | null;
settlement_days: number | null;
delivery_time: string | null;
};
/** 录单页注入的选项(散客不送货、不挂账期) */
export function retailCustomerOption(): RetailCustomerOption {
return {
customer_code: RETAIL_CUSTOMER_CODE,
company_name: RETAIL_CUSTOMER_NAME,
address: null,
settlement_days: null,
delivery_time: null,
};
}
/** 是否哨兵客户编码 */
export function isRetailCustomer(code?: string | null): boolean {
return String(code || '').trim() === RETAIL_CUSTOMER_CODE;
}
/**
* 排除散客单的片段。**只用在"散客不该出现的地方"**:
* · 分拣任务的生成与补建候选;
* · 配送任务的生成与补建候选、面向人的配送查询;
* · 客户结算单(当场收款,不挂应收)。
* ⚠️ **禁止**把它用在销售额/成本/毛利聚合上 —— 散客是真实销售,必须计入。
*/
export function nonRetailOrderSql(alias = 'o'): string {
return `${alias}.customer_code IS DISTINCT FROM '${RETAIL_CUSTOMER_CODE}'`;
}
/** 反向:只要散客单(排查/测试/散客日统计用) */
export function retailOrderOnlySql(alias = 'o'): string {
return `${alias}.customer_code = '${RETAIL_CUSTOMER_CODE}'`;
}
type Queryable = { query: (text: string, params?: unknown[]) => Promise<{ rows: any[] }> };
/** 该订单是否是「散客」单(哨兵客户)。给状态机/结算/任务生成各入口用 */
export async function isRetailOrder(orderCode: string, db: Queryable): Promise<boolean> {
const code = String(orderCode || '').trim();
if (!code) return false;
const result = await db.query(`SELECT customer_code FROM orders WHERE order_code = $1 LIMIT 1`, [code]);
return isRetailCustomer(result.rows[0]?.customer_code);
}
/**
* 幂等补「散客」客户行(新租户/存量兜底)。
*
* 为什么放在运行时而不是只靠迁移:`clone_tenant_schema()` 被多支迁移**整函数重写**过,
* 并行改动下再重写一次很容易互相覆盖;本函数用 `ON CONFLICT (customer_code) DO NOTHING`,
* 在租户上下文里调用即可,天然幂等、可随时补。
* 迁移 `20261046_retail_customer.sql` 负责**存量租户**的一次性补行。
*
* ⚠️ 必须在租户上下文(ALS)里调用:`customers` 是 FORCE ROW LEVEL SECURITY,
* 且 `branch_code` 必须与 `app.branch_code` 一致,否则 WITH CHECK 失败。
*/
export async function ensureRetailCustomer(db: Queryable, branchCode: string): Promise<void> {
const branch = String(branchCode || '').trim();
if (!branch) throw Object.assign(new Error('缺少分公司编码'), { status: 400, code: 'branch_required' });
await db.query(
`INSERT INTO customers
(customer_code, company_name, branch_code, status, source,
settlement_days, credit_limit, credit_frozen, credit_overdue_days,
cost_budget, sales_budget, rebate_category_ratios)
VALUES ($1, $2, $3, 10, 'retail', 0, 0, false, 0, 0, 0, '{}'::jsonb)
ON CONFLICT (customer_code) DO NOTHING`,
[RETAIL_CUSTOMER_CODE, RETAIL_CUSTOMER_NAME, branch]
);
}