feat(main-system): 免入库单取货链路 + 实收数量「事实登记 + 事后同步」
免入库(主系统骑手上门取货): - 50 展示「待取货」、副文案「待主系统骑手上门取货」、不建我方配送任务(守卫与开关解耦) - 我方 50 即回写备货完成;两开关(SKIP_INBOUND_AWAIT_PICKUP / FULFILLMENT_EVENTS)改默认开 核货事实镜像(「核货完毕」标记): - 按主系统 lookup 的 actual_qty 落台账,新增 GET/POST /orders/:orderCode/main-system-prep-facts[/refresh] - 详情行内显示「核货完毕」(含核货量),分配对账兜底刷新 实收数量/单位口径变更(对方先改 + 我方事后同步): - 台账加 applied_at;inbound 与 picking_check 同为事实登记(驳回不回滚) - 同意才落我方值:免入库改装车核货量、送仓改验收实收(已入库走既有库存调整)、已结算/已结案只留痕 - 计划量与金额一起改,并同步结算投影(syncStatementsForOrderSafe) - 审核页语义改「主系统实际值(已生效)/ 是否同步」+ 已处理历史(双方时间与落值结论) 台账对照与回写修复: - 行级对照改整单一次性(同名商品不再重复认领)+ 存量纠正 healOrderPrepMapping - 堵住客户绑定回填旁路(免入库单不再被派我方骑手) - 回写触发函数改对象参数(修 branch/order 传反导致的静默不回写) 文档:设计文档 4 篇 +《给主系统 AI 的对接改造清单》§2.9(第六批契约) 回归:服务端全量与 HEAD 基线一致(24 个失败文件同集合);前端 140 文件 / 699 测试全绿;tsc 双端干净
This commit is contained in:
@@ -14,6 +14,21 @@ import {
|
||||
moveOrderToTrash,
|
||||
restoreOrderFromTrash,
|
||||
} from '../../../services/orderDeleteService';
|
||||
import { reportMainSystemStockout } from '../../../services/mainSystemStockout';
|
||||
import {
|
||||
listOrderActualFacts,
|
||||
refreshOrderActualFacts,
|
||||
} from '../../../services/mainSystemActualFacts';
|
||||
import {
|
||||
listActualQtyInbox,
|
||||
decideActualQtyRequest,
|
||||
} from '../../../services/mainSystemActualQtyInbox';
|
||||
import {
|
||||
isEnabled as isPriceQtyRequestEnabled,
|
||||
listPriceQtyRequests,
|
||||
submitPriceQtyRequest,
|
||||
toPriceQtyRequestDto,
|
||||
} from '../../../services/mainSystemPriceQtyRequest';
|
||||
import {
|
||||
getCustomerOrderPrefill,
|
||||
logOrderBatchAction,
|
||||
@@ -38,6 +53,7 @@ import { submitMobileCustomerOrder } from '../../../services/customerOrderSubmit
|
||||
import {
|
||||
syncOrderManageItems,
|
||||
updateOrderDeliveryDate,
|
||||
updateOrderDeliveryAddress,
|
||||
} from '../../../services/orderManageItemsService';
|
||||
import {
|
||||
isSmartAllocationEnabled,
|
||||
@@ -243,6 +259,297 @@ router.patch('/orders/:orderCode/items', authMiddleware, async (req: AuthRequest
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 明细「缺货」(开发者 2026-10-10)。
|
||||
*
|
||||
* 主系统分配过来的订单**不能删订单、不能删商品**,只留这一个动作:
|
||||
* 报主系统 `POST /preparations/status {status:40}`(主系统把该行**转自采**)⇒
|
||||
* 成功后再把我方这一行的履约收口(未完成采购作废、明细标缺货),从"待备货/未完成采购"里剔除。
|
||||
* 口径与顺序纪律见 services/mainSystemStockout.ts。
|
||||
*/
|
||||
router.post(
|
||||
'/orders/:orderCode/items/:itemId/stockout',
|
||||
authMiddleware,
|
||||
async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['order_edit', 'order_entry', 'order_manage', 'allocation_manage']);
|
||||
const orderCode = String(req.params.orderCode || '').trim();
|
||||
const itemId = String(req.params.itemId || '').trim();
|
||||
const order = await assertOrderCodeAccess(orderCode, ctx!);
|
||||
const outcome = await reportMainSystemStockout({
|
||||
branchCode: order.branch_code || ctx?.branchCode || '',
|
||||
orderCode,
|
||||
itemId,
|
||||
reason: req.body?.reason ?? req.body?.stockout_reason ?? null,
|
||||
userId: ctx?.userId ?? null,
|
||||
operatorRoles: ctx?.roles?.length ? ctx.roles.join(',') : null,
|
||||
ipAddress: req.ip || req.socket?.remoteAddress || null,
|
||||
});
|
||||
|
||||
if (outcome.kind === 'ok') {
|
||||
return res.json({
|
||||
data: {
|
||||
order_code: outcome.ourOrderCode,
|
||||
item_id: outcome.itemId,
|
||||
prep_id: outcome.prepId || null,
|
||||
already_reported: outcome.alreadyReported,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (outcome.kind === 'unavailable') {
|
||||
return res.status(502).json({ error: 'main_system_unavailable', error_description: outcome.reason });
|
||||
}
|
||||
if (outcome.kind === 'rejected') {
|
||||
return res.status(400).json({ error: 'main_system_rejected', error_description: outcome.reason });
|
||||
}
|
||||
if (outcome.kind === 'not_main_system_item') {
|
||||
return res.status(400).json({ error: 'not_main_system_item', error_description: outcome.reason });
|
||||
}
|
||||
return res.status(409).json({ error: 'invalid_state', error_description: outcome.reason });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '上报缺货失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* 跨系统改价 / 改量:提交申请(2026-10-11)。
|
||||
*
|
||||
* 主系统分配过来的订单行,**改销售价(= 主系统成本价)/ 改数量只能走这里**:
|
||||
* 免审项(补价、改低、减量)提交后主系统立即生效 ⇒ 我们同步落地;
|
||||
* 涨价/加量 ⇒ 主系统管理员审核,结果经 webhook `preparations.price_qty_decided` 回传。
|
||||
* 见 docs/design/20261011-跨系统改价改量审核.md(§4 C2/C6)。
|
||||
*/
|
||||
router.post(
|
||||
'/orders/:orderCode/items/:itemId/price-qty-request',
|
||||
authMiddleware,
|
||||
async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['order_edit', 'order_entry', 'order_manage', 'allocation_manage']);
|
||||
const orderCode = String(req.params.orderCode || '').trim();
|
||||
const itemId = String(req.params.itemId || '').trim();
|
||||
const order = await assertOrderCodeAccess(orderCode, ctx!);
|
||||
const branchCode = order.branch_code || ctx?.branchCode || '';
|
||||
const outcome = await submitPriceQtyRequest({
|
||||
branchCode,
|
||||
ourOrderCode: orderCode,
|
||||
ourOrderItemId: itemId,
|
||||
proposedPrice: req.body?.proposed_price ?? req.body?.price ?? null,
|
||||
proposedQty: req.body?.proposed_qty ?? req.body?.quantity ?? null,
|
||||
unit: req.body?.unit ?? null,
|
||||
reason: req.body?.reason ?? null,
|
||||
actor: {
|
||||
userId: ctx?.userId ?? null,
|
||||
roles: ctx?.roles?.length ? ctx.roles.join(',') : null,
|
||||
requestedBy: `${branchCode}:${ctx?.roles?.length ? ctx.roles.join('|') : 'operator'}:${ctx?.userId ?? ''}`,
|
||||
ipAddress: req.ip || req.socket?.remoteAddress || null,
|
||||
},
|
||||
clientRequestId: req.body?.client_request_id ?? null,
|
||||
});
|
||||
|
||||
if (outcome.kind === 'ok') {
|
||||
return res.json({
|
||||
data: {
|
||||
request: toPriceQtyRequestDto(outcome.row),
|
||||
status: outcome.status,
|
||||
review_required: outcome.reviewRequired,
|
||||
final_price: outcome.finalPrice,
|
||||
final_qty: outcome.finalQty,
|
||||
message: outcome.message,
|
||||
applied_locally: outcome.appliedLocally,
|
||||
apply_note: outcome.applyNote,
|
||||
},
|
||||
error: null,
|
||||
});
|
||||
}
|
||||
if (outcome.kind === 'disabled') {
|
||||
return res.status(503).json({ error: 'price_qty_disabled', error_description: outcome.reason });
|
||||
}
|
||||
if (outcome.kind === 'main_system_not_ready') {
|
||||
return res.status(503).json({ error: 'main_system_not_ready', error_description: outcome.reason });
|
||||
}
|
||||
if (outcome.kind === 'failed') {
|
||||
return res
|
||||
.status(outcome.retryable ? 502 : 400)
|
||||
.json({
|
||||
error: outcome.retryable ? 'main_system_unavailable' : 'main_system_rejected',
|
||||
error_description: outcome.reason,
|
||||
});
|
||||
}
|
||||
if (outcome.kind === 'not_main_system_order') {
|
||||
return res.status(400).json({ error: 'not_main_system_item', error_description: outcome.reason });
|
||||
}
|
||||
if (outcome.kind === 'no_change') {
|
||||
return res.status(400).json({ error: 'no_change', error_description: outcome.reason });
|
||||
}
|
||||
return res.status(400).json({ error: 'invalid_request', error_description: outcome.reason });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '提交改价/改量申请失败',
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* 跨系统改价 / 改量:申请列表(界面用;待审核 / 已生效 / 已通过 / 已驳回)。
|
||||
* 只读本系统台账(`public.main_system_price_qty_requests`),不触达主系统。
|
||||
*/
|
||||
router.get('/main-system/price-qty-requests', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['order_edit', 'order_entry', 'order_manage', 'allocation_manage', 'purchase_manage']);
|
||||
const requested = readOperatingBranchCode(req);
|
||||
const branchCode = getEffectiveBranchCode(ctx!, requested) || ctx?.branchCode || '';
|
||||
const status = req.query?.status ? String(req.query.status) : null;
|
||||
const ourOrderCode = req.query?.order_code ? String(req.query.order_code) : null;
|
||||
const rows = await listPriceQtyRequests({
|
||||
branchCode,
|
||||
status,
|
||||
ourOrderCode,
|
||||
limit: Number(req.query?.limit) || 100,
|
||||
});
|
||||
return res.json({
|
||||
data: rows.map((row) => toPriceQtyRequestDto(row)),
|
||||
enabled: isPriceQtyRequestEnabled(),
|
||||
error: null,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '查询改价/改量申请失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/* ── 对方发起、我方审核:实收数量/单位/完结后成本价申请(2026-10-11 第五批 Q8/Q9)──
|
||||
* · 列表:待我审核 + 已审历史;
|
||||
* · 审核:通过 / 改值后通过 / 驳回 —— 结论回传主系统**成功后才改本地台账**(见服务层)。
|
||||
* 核货节点(node=picking_check)为事实登记:驳回**不让对方回滚**;>=60/>=90 为先审后改。
|
||||
*/
|
||||
/**
|
||||
* 主系统备货行的**核货/入库事实**(2026-10-11,设计文档 `20261011-免入库核货完毕标记.md`)。
|
||||
*
|
||||
* · GET :**只读库**(不调外部)⇒ 主系统抖动也不会拖慢/拖挂订单详情;
|
||||
* · POST :显式拉一次 `POST /preparations/lookup` 落库后回读;失败返回 `refreshed:false` + 库内已有事实(**不 500**)。
|
||||
*
|
||||
* 只读镜像:不新增订单状态值、不改状态链、不回写主系统。
|
||||
*/
|
||||
router.get('/orders/:orderCode/main-system-prep-facts', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, [
|
||||
'order_manage',
|
||||
'order_view',
|
||||
'order_edit',
|
||||
'order_entry',
|
||||
'order_status_update',
|
||||
]);
|
||||
const requested = readOperatingBranchCode(req);
|
||||
const branchCode = getEffectiveBranchCode(ctx!, requested) || ctx?.branchCode || '';
|
||||
const orderCode = String(req.params.orderCode || '').trim();
|
||||
if (!orderCode) {
|
||||
return res.status(400).json({ error: 'invalid_request', error_description: '缺少订单号' });
|
||||
}
|
||||
const items = await listOrderActualFacts({ branchCode, ourOrderCode: orderCode });
|
||||
return res.json({ data: { items }, error: null });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '读取主系统核货事实失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/orders/:orderCode/main-system-prep-facts/refresh', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, [
|
||||
'order_manage',
|
||||
'order_view',
|
||||
'order_edit',
|
||||
'order_entry',
|
||||
'order_status_update',
|
||||
]);
|
||||
const requested = readOperatingBranchCode(req);
|
||||
const branchCode = getEffectiveBranchCode(ctx!, requested) || ctx?.branchCode || '';
|
||||
const orderCode = String(req.params.orderCode || '').trim();
|
||||
if (!orderCode) {
|
||||
return res.status(400).json({ error: 'invalid_request', error_description: '缺少订单号' });
|
||||
}
|
||||
// 失败不抛:刷新是"锦上添花",详情展示必须照常(用库内已存事实)
|
||||
const outcome = await refreshOrderActualFacts({ branchCode, ourOrderCode: orderCode });
|
||||
return res.json({
|
||||
data: { items: outcome.items, refreshed: outcome.refreshed, reason: outcome.reason ?? null },
|
||||
error: null,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '刷新主系统核货事实失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/main-system/actual-qty-requests', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['order_edit', 'order_entry', 'order_manage', 'allocation_manage', 'purchase_manage']);
|
||||
const requested = readOperatingBranchCode(req);
|
||||
const branchCode = getEffectiveBranchCode(ctx!, requested) || ctx?.branchCode || '';
|
||||
const rows = await listActualQtyInbox({
|
||||
branchCode,
|
||||
status: req.query?.status ? String(req.query.status) : null,
|
||||
limit: Number(req.query?.limit) || 100,
|
||||
});
|
||||
return res.json({ data: rows, error: null });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '查询待我审核的实收申请失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/main-system/actual-qty-requests/:id/decision', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, ['order_edit', 'order_entry', 'order_manage', 'allocation_manage']);
|
||||
const requested = readOperatingBranchCode(req);
|
||||
const branchCode = getEffectiveBranchCode(ctx!, requested) || ctx?.branchCode || '';
|
||||
const decision = String(req.body?.decision || '').toLowerCase();
|
||||
if (decision !== 'approved' && decision !== 'rejected') {
|
||||
return res.status(400).json({ error: 'invalid_request', error_description: '决策只能是 approved 或 rejected' });
|
||||
}
|
||||
const rawFinal = req.body?.final_value;
|
||||
const finalValue =
|
||||
rawFinal === null || rawFinal === undefined || rawFinal === '' ? null : Number(rawFinal);
|
||||
if (finalValue !== null && !Number.isFinite(finalValue)) {
|
||||
return res.status(400).json({ error: 'invalid_request', error_description: '最终值必须是数字' });
|
||||
}
|
||||
const data = await decideActualQtyRequest({
|
||||
branchCode,
|
||||
rowId: String(req.params.id || ''),
|
||||
decision,
|
||||
finalValue,
|
||||
reviewer: ctx?.userId || null,
|
||||
reason: req.body?.reason != null ? String(req.body.reason) : null,
|
||||
});
|
||||
return res.json({ data });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '审核实收申请失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.patch('/orders/:orderCode/delivery-date', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
@@ -277,6 +584,38 @@ router.patch('/orders/:orderCode/delivery-date', authMiddleware, async (req: Aut
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* 单独改「收货地址」(表头字段,不带明细,2026-10-11)。
|
||||
* 与配送日不同:地址是发车前最常见的改动,故**不限于管理员**,凡能编辑订单的人(order_edit /
|
||||
* order_entry / order_manage / allocation_manage)都可改;明细锁(40+)不拦它,`>= 90` 才拒。
|
||||
*/
|
||||
router.patch('/orders/:orderCode/delivery-address', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
requireWorkflowPermission(ctx, [
|
||||
'order_edit',
|
||||
'order_entry',
|
||||
'order_manage',
|
||||
'allocation_manage',
|
||||
]);
|
||||
const orderCode = String(req.params.orderCode || '').trim();
|
||||
await assertOrderCodeAccess(orderCode, ctx);
|
||||
const data = await updateOrderDeliveryAddress({
|
||||
orderCode,
|
||||
deliveryAddress: req.body?.delivery_address ?? null,
|
||||
operatorId: ctx?.userId || null,
|
||||
operatorRoles: ctx?.roles?.length ? ctx.roles.join(',') : null,
|
||||
});
|
||||
return res.json({ data });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({
|
||||
error: error?.code || 'server_error',
|
||||
error_description: error?.message || '修改收货地址失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/orders/reprocess-allocation', authMiddleware, async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
|
||||
Reference in New Issue
Block a user