开发者 2026-10-12 立项(原话要点):「名字叫独立进货」;「商品全部入库了就行,自己就完成」;
「注意那个主数据不卡流程,别把这种单子也强制推到那个待配送去了」;
「厂商应付就是我们现在的供应商结算体系,不用动」;「这种单子别计入销售,就是纯纯的一个进货单」。
设计:docs/design/20261012-独立进货(自用哨兵客户).md
关键取舍:**哨兵客户编码、不建 customers 行** —— 实测租户库 orders.customer_code 无外键,
所以哨兵可直接落订单;它不在 customers 里 ⇒ 客户管理/绑定/预算/回款/客户结算**天然看不到**、
也无从删除(符合"只在录单可见、不可删除")。
落点(逐处,避免被任何路径推到待配送):
· 录单:orderEntryScopeService 放行哨兵(仍要求录单员已绑定分公司);
Web 录单页客户下拉注入首项「独立进货」(src/pages/orders/CustomerOrder.tsx);
· 完成:maybeAdvanceOrderAfterFulfillment + 15 分钟对账 ⇒ 全部明细验收入库(passed) 即
completeOrderWithoutDriverDelivery 直接结案 90;未齐套保持原状态(不进 45/50);
· 结案口允许独立进货从 **40 待验收** 结案(普通单仍只 30/50,行为不变);
· 主数据不卡流程 autoAdvanceOrderToDelivery ⇒ 直接 skip(skippedReason='独立进货单,跳过定时强推');
· 分拣:syncMissingPickingTasks 候选查询排除哨兵单(不生成拣货任务);
· 结算:客户结算唯一入口 runUpsertCustomerStatementForOrder 对哨兵单直接 'skipped';
**供应商结算(应付厂商)零改动**,仍按订单行 qty×unit_cost;
· 成本价:结案口硬拦 unit_cost<=0(independent_inbound_cost_required)——0 成本会让应付无意义;
· 销售口径:新增统一谓词 salesVisibleOrderSql,已排除 看板 KPI(当期/上期)、客户返利、SKU 毛利/排行;
**成本口径不动**(进货成本照旧计入,这正是"记账"要的)。
真库验证(BR003 fixture,用后即删、残留 0):
① autoAdvanceOrderToDelivery → advanced:false / '独立进货单,跳过定时强推' ✅
② upsertCustomerStatementForOrder → 'skipped',customer_statements 0 行 ✅
③ reconcile → steps ['独立进货:尚未全部入库,保持当前状态'],状态保持 30,拣货任务 0 / 配送任务 0 ✅
测试:server/tests/independentInbound.test.ts(12 例);全量 25 个失败文件与基线一致(0 新增失败);
两端 tsc ✅;门禁 8/1/1(check-ai-cost 既定红、verify-facts 无 psql)。
95 lines
3.9 KiB
TypeScript
95 lines
3.9 KiB
TypeScript
import { pool } from '../db';
|
|
import { loadTenantContext } from './tenantScope';
|
|
import { isIndependentInboundCustomer } from './independentInbound';
|
|
|
|
export type OrderEntryScope = {
|
|
branchCode: string | null;
|
|
isSuperAdmin: boolean;
|
|
};
|
|
|
|
export async function loadOrderEntryScope(userId: string): Promise<OrderEntryScope> {
|
|
const ctx = await loadTenantContext(userId);
|
|
if (!ctx) {
|
|
throw Object.assign(new Error('请先登录'), { status: 401, code: 'invalid_token' });
|
|
}
|
|
return {
|
|
branchCode: ctx.branchCode,
|
|
isSuperAdmin: ctx.isSuperAdmin,
|
|
};
|
|
}
|
|
|
|
export async function assertOrderEntryCustomerAccess(customerCode: string, scope: OrderEntryScope) {
|
|
/**
|
|
* 「独立进货」哨兵客户(2026-10-12 开发者立项):它不是 `customers` 里的真实客户,
|
|
* 而是录单处一个内建选项(用于从厂商进货、入库即完成)。这里显式放行,
|
|
* 但**仍要求录单员已绑定分公司**(订单必须落到某个分公司)。
|
|
*/
|
|
if (isIndependentInboundCustomer(customerCode)) {
|
|
if (!scope.branchCode && !scope.isSuperAdmin) {
|
|
throw Object.assign(new Error('录单员未绑定分公司,请联系管理员'), { status: 403, code: 'invalid_branch' });
|
|
}
|
|
return;
|
|
}
|
|
|
|
const result = await pool.query(`SELECT branch_code FROM customers WHERE customer_code = $1`, [customerCode]);
|
|
if (!result.rowCount) {
|
|
throw Object.assign(new Error('客户不存在'), { status: 404, code: 'not_found' });
|
|
}
|
|
const customerBranch = result.rows[0]?.branch_code as string | null;
|
|
|
|
if (scope.isSuperAdmin && !scope.branchCode) {
|
|
return;
|
|
}
|
|
|
|
if (!scope.branchCode) {
|
|
throw Object.assign(new Error('录单员未绑定分公司,请联系管理员'), { status: 403, code: 'invalid_branch' });
|
|
}
|
|
|
|
if (customerBranch && customerBranch !== scope.branchCode) {
|
|
throw Object.assign(new Error('无权为该分公司外的客户录单'), { status: 403, code: 'forbidden' });
|
|
}
|
|
}
|
|
|
|
export async function resolveOrderBranchForEntry(scope: OrderEntryScope, customerCode: string): Promise<string> {
|
|
// 独立进货:哨兵不在 customers 里 ⇒ 直接落到录单员自己的分公司
|
|
if (isIndependentInboundCustomer(customerCode)) {
|
|
if (scope.branchCode) return scope.branchCode;
|
|
throw Object.assign(new Error('无法确定订单所属分公司'), { status: 400, code: 'invalid_branch' });
|
|
}
|
|
const result = await pool.query(`SELECT branch_code FROM customers WHERE customer_code = $1`, [customerCode]);
|
|
const customerBranch = result.rows[0]?.branch_code as string | null;
|
|
if (customerBranch) return customerBranch;
|
|
if (scope.branchCode) return scope.branchCode;
|
|
throw Object.assign(new Error('无法确定订单所属分公司'), { status: 400, code: 'invalid_branch' });
|
|
}
|
|
|
|
/** 语音/图片解析时使用客户所属分公司,避免管理员账号 profile 无 branch_code 时解析失败 */
|
|
export async function resolveParseBranchCode(scope: OrderEntryScope, customerCode: string): Promise<string | null> {
|
|
const result = await pool.query(`SELECT branch_code FROM customers WHERE customer_code = $1`, [customerCode]);
|
|
const customerBranch = result.rows[0]?.branch_code as string | null;
|
|
return customerBranch || scope.branchCode;
|
|
}
|
|
|
|
export async function listOrderEntryCustomers(scope: OrderEntryScope) {
|
|
const params: string[] = [];
|
|
let branchSql = '';
|
|
|
|
if (scope.branchCode) {
|
|
params.push(scope.branchCode);
|
|
branchSql = ` AND branch_code = $${params.length}`;
|
|
} else if (!scope.isSuperAdmin) {
|
|
throw Object.assign(new Error('录单员未绑定分公司,请联系管理员'), { status: 403, code: 'invalid_branch' });
|
|
}
|
|
|
|
const result = await pool.query(
|
|
`SELECT customer_code, company_name, contact_phone, address, branch_code, delivery_time
|
|
FROM customers
|
|
WHERE status IN (10, 20)
|
|
${branchSql}
|
|
ORDER BY company_name ASC, created_at DESC
|
|
LIMIT 200`,
|
|
params
|
|
);
|
|
return result.rows;
|
|
}
|