feat(主系统单): 无库存抵扣/无厂商的商品默认落自采(标记 INTERNAL + 建自采采购单,仅主系统分配单,幂等)
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

This commit is contained in:
Ubuntu
2026-10-20 10:26:10 +08:00
parent bccbab34d1
commit 0fd45f6ef8
2 changed files with 99 additions and 0 deletions
@@ -1540,6 +1540,24 @@ async function createDayOrder(params: {
);
// 来源主系统单号落列(幂等;只补 NULL):详情表按它做一级分组
/**
* **主系统分配单:无库存抵扣 ⇒ 默认自采**(2026-10-20 开发者裁定,仅主系统单)。
* 导入路径不走分配链路,故在此收口:标记 INTERNAL + 建自采采购单(幂等,失败不阻断导入)。
*/
try {
const { ensureSelfPurchaseForImportedOrder } = await import('./orderAllocationService');
const selfPurchaseCreated = await ensureSelfPurchaseForImportedOrder({
orderCode: result.order_code,
branchCode,
customerCode,
});
if (selfPurchaseCreated > 0) {
console.log(`[main-system-import] ${result.order_code} 无库存抵扣行默认自采:新建 ${selfPurchaseCreated} 张自采单`);
}
} catch (error) {
console.warn('[main-system-import] 默认自采收口失败(不影响导入):', (error as Error)?.message || error);
}
await stampMainOrderCodes({
branchCode,
ourOrderCode: result.order_code,
@@ -3042,3 +3042,84 @@ export async function purchaserDirectAllocation(
return result;
}
/**
* **主系统分配单:无库存抵扣 / 无真厂商 ⇒ 一律落自采**(2026-10-20 开发者裁定,方案 A,仅限主系统单)。
*
* 背景:主系统导入路径**不走**分配链路 ⇒ `selfPurchaseUnassigned` 的"无厂商默认自采"从不生效,
* 行上 `supplier_code` 停在 NULL(界面「未分配」),采购员待办里也没有这张自采单。
*
* 处理(幂等,单行失败不影响其它行):
* ① 选行:本单、未退货、供应商为空/`UNASSIGNED`/`INTERNAL`、无有效履约、无未完成采购单;
* ② 把这些行 `supplier_code` 标为 `INTERNAL`(= 自采);
* ③ 逐行建自采采购单(复用 `createPurchaseOrderForItem`,与部分抵扣补自采同一实现)。
*/
export async function ensureSelfPurchaseForImportedOrder(input: {
orderCode: string;
branchCode?: string | null;
customerCode?: string | null;
}): Promise<number> {
const orderCode = String(input.orderCode || '').trim();
if (!orderCode) return 0;
const client = await pool.connect();
try {
await client.query('BEGIN');
const rows = await client.query<OrderItemRow>(
`SELECT oi.id, oi.order_code, oi.sku_code, oi.item_name, oi.quantity, oi.unit, oi.price,
oi.category_code, oi.category_name, oi.supplier_code,
oi.processing_method, oi.processing_owner
FROM order_items oi
WHERE oi.order_code = $1
AND COALESCE(oi.is_returned, false) = false
AND (oi.supplier_code IS NULL OR TRIM(oi.supplier_code) = ''
OR UPPER(TRIM(oi.supplier_code)) IN ('UNASSIGNED', 'INTERNAL'))
AND NOT EXISTS (
SELECT 1 FROM purchase_orders po
WHERE po.order_code = oi.order_code
AND po.source_id = oi.id::text
AND po.purchase_status < 80
)`,
[orderCode]
);
if (!rows.rowCount) {
await client.query('COMMIT');
return 0;
}
const ids = rows.rows.map((r) => String(r.id));
// ② 标记为自采(界面供应商列显示 INTERNAL=自采)
await client.query(
`UPDATE order_items SET supplier_code = 'INTERNAL'
WHERE order_code = $1 AND id = ANY($2::uuid[])
AND (supplier_code IS NULL OR TRIM(supplier_code) = '' OR UPPER(TRIM(supplier_code)) = 'UNASSIGNED')`,
[orderCode, ids]
);
const primaryPurchaser = await fetchPrimaryPurchaserForBranch(input.branchCode);
let created = 0;
for (const row of rows.rows) {
try {
if (await isOrderItemFulfillmentValid(client, String(row.id))) continue;
const ok = await createPurchaseOrderForItem(
{
orderCode,
item: { ...row, sku_name: row.sku_name || row.item_name },
remark: '主系统分配单:无库存抵扣,默认自采',
purchaser: primaryPurchaser,
branchCode: input.branchCode,
customerCode: input.customerCode,
},
client
);
if (ok) created += 1;
} catch (err) {
console.warn('[import-self-purchase] 建自采单失败:', (err as Error)?.message || err);
}
}
await client.query('COMMIT');
return created;
} catch (error) {
await client.query('ROLLBACK').catch(() => undefined);
throw error;
} finally {
client.release();
}
}