fix(价格回写): 名称仅精确解析并校验客户所属分公司
避免 %/_ 通配符把成交价写到无关商品;跨分公司客户直接跳过,不在错误租户建价目行。
This commit is contained in:
@@ -8,7 +8,7 @@ import { writeAuditLog } from './auditService';
|
||||
* 规则:
|
||||
* 1) 客户执行中报价单含该 SKU:若报价单绑定客户数 > 1,跳过(共享报价单不串价);
|
||||
* 否则写 quote item sale_price+price,并置 sale_price_manual=true;
|
||||
* 2) 无报价单:只更新已存在的 customer_skus(不 INSERT);
|
||||
* 2) 无报价单:更新 customer_skus;正常路径由 syncCustomerMappings 建行,缺失时 upsert 兜底;
|
||||
* 3) 两者都没有 / 加菜 / price<=0:跳过。
|
||||
* 只写价格主数据,不写订单/结算/库存;所有写用 IS DISTINCT FROM 避免同值抖动。
|
||||
*/
|
||||
@@ -59,7 +59,8 @@ export async function writeBackOrderSalePrices(input: {
|
||||
result.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
// 提交行可能只有名称(syncCustomerMappings 按名称建了库行);这里补解析,避免价没写。
|
||||
// 提交行可能只有名称(syncCustomerMappings 按名称建了库行);这里只做**精确名**解析。
|
||||
// 绝不把用户文本直接拼 ILIKE:`%`/`_` 会把成交价写到无关商品(红队已实测)。
|
||||
if (!skuCode && item?.item_name) {
|
||||
const name = trim(item.item_name);
|
||||
const params: unknown[] = [name];
|
||||
@@ -76,16 +77,6 @@ export async function writeBackOrderSalePrices(input: {
|
||||
params
|
||||
);
|
||||
skuCode = trim(found.rows[0]?.sku_code);
|
||||
if (!skuCode) {
|
||||
const fuzzy = await pool.query(
|
||||
`SELECT sku_code FROM skus
|
||||
WHERE sku_name ILIKE $1 AND status = 1 ${branchFilter}
|
||||
ORDER BY created_at ASC NULLS LAST
|
||||
LIMIT 1`,
|
||||
[params[0], ...params.slice(1)]
|
||||
);
|
||||
skuCode = trim(fuzzy.rows[0]?.sku_code);
|
||||
}
|
||||
}
|
||||
if (!skuCode) {
|
||||
result.skipped += 1;
|
||||
@@ -97,6 +88,18 @@ export async function writeBackOrderSalePrices(input: {
|
||||
}
|
||||
if (!entries.size) return result;
|
||||
|
||||
// 防御:订单分支必须与客户所属分支一致,避免在错误租户里建出幽灵客户价目行。
|
||||
if (branchCode) {
|
||||
const customerCheck = await pool.query(
|
||||
`SELECT 1 FROM customers WHERE customer_code = $1 AND branch_code = $2 LIMIT 1`,
|
||||
[customerCode, branchCode]
|
||||
);
|
||||
if (!customerCheck.rowCount) {
|
||||
result.skipped += entries.size;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
const quoteChanges: Array<{ sku_code: string; sale_price: number; quote_sheet_code: string }> = [];
|
||||
const customerSkuChanges: Array<{ sku_code: string; sale_price: number }> = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user