fix(报价单): 商品新增写价改走报价单编辑链路
单里已有该商品只改价格,保留单位/成本价/加价率/历史名称快照;没有则新增一行。append-items 改走 applyQuoteSheetEditAndRequestDeliveryReview,价格类变更与报价单页编辑一样发起配送商销售价确认;前端不再传 cost_price 避免清零,旧入口已删除。
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
# 商品新增写入报价单:改走报价单编辑链路(已有则改价、没有则新增)
|
||||
|
||||
- 日期:2026-10-22
|
||||
- 开发者需求:「这块修改价格 相当于就是等同于在报价单那边 找到了这个商品进行修改;如果这个商品不存在于报价单就加入;
|
||||
相当于调用的接口应该是报价单那边的修改接口或者新增接口」。
|
||||
|
||||
## 1. 改前事实
|
||||
|
||||
| # | 事实 |
|
||||
|---|---|
|
||||
| F1 | 商品新增的「写入报价单」调 `POST /quote/sheets/:code/append-items` → `appendSkuPricesToQuoteSheet` → `updateQuoteSheet`,**只写本系统**,不经过报价单页的编辑通道 |
|
||||
| F2 | 合并逻辑虽然「已有则覆盖、没有则新增」,但会用商品档案值覆盖已有行的 `unit` / `sku_name`;且前端固定传 `cost_price: 0`、路由把 `markup_ratio` 归一成 `null` ⇒ 命中已有行时**成本价会被清零、加价率会被清空** |
|
||||
| F3 | 报价单页编辑 `PUT /quote/sheets/:code` 走 `applyQuoteSheetEditAndRequestDeliveryReview`:本系统**立即生效**,价格类变更再自动发起「配送商销售价确认」申请(`submitPriceProposal`,受 `MAIN_SYSTEM_QUOTE_PRICE_PROPOSAL` 开关控制) |
|
||||
| F4 | `append-items` 未走该通道 ⇒ 从商品新增写入的价格**不会**发起给对方审核的申请 |
|
||||
|
||||
## 2. 方案
|
||||
|
||||
1. 抽出纯函数 `mergeQuoteSheetItems(existingRows, incoming)`(`quoteSheetService.ts`):
|
||||
- 单里**已有**该 SKU ⇒ **只改 `price` / `sale_price`**,保留 `unit`、`cost_price`、`markup_ratio` 与历史名称快照;
|
||||
- 单里**没有** ⇒ 新增一行(用商品档案的 `sku_name` / `unit`,成本 0、加价率为空);
|
||||
- 其他商品原样保留。
|
||||
2. 新增 `appendSkuPricesAndRequestDeliveryReview`(`quoteSheetEditService.ts`):
|
||||
读取整单 → 合并 → 调用 `applyQuoteSheetEditAndRequestDeliveryReview`,**与报价单页编辑同一通道**。
|
||||
3. 路由 `POST /quote/sheets/:code/append-items` 改调该函数,响应改为 `{ data, delivery_handoff }`。
|
||||
4. 前端商品新增不再提交 `cost_price`(新行由服务端默认 0,已有行保留原成本)。
|
||||
5. 权限门槛保持不变(仍是 `customer_manage | order_manage | order_entry`)。
|
||||
6. 旧入口 `appendSkuPricesToQuoteSheet` 已无调用方(全仓仅剩定义),**删除**,避免留下第二条语义不同的写完路径。
|
||||
|
||||
## 3. 非目标
|
||||
|
||||
- 不改报价单页的编辑/审核界面与内部审批(内部改价审批已于 2026-10-18 删除,现为跨系统申请);
|
||||
- 不改价格口径与订单取价(仍读 `COALESCE(sale_price, price)`);
|
||||
- 不改商品档案字段;
|
||||
- 不在界面上新增提示文案(跨系统申请失败仍只体现在响应 `delivery_handoff`,前端暂不弹提示)。
|
||||
|
||||
## 4. 验收
|
||||
|
||||
1. 商品新增勾选一张执行中的报价单,填写报价单价:
|
||||
- 若该 SKU 已在单内 ⇒ 该行**只有价格变**,单位/成本价/加价率/历史名称不变;其他行不变;
|
||||
- 若不在单内 ⇒ 追加一行(名称/单位取商品档案,成本 0、加价率空)。
|
||||
2. 价格类变更会与报价单页编辑一样发起「配送商销售价确认」申请(开关开启时)。
|
||||
3. 无有效商品/价格时直接报错,不重写整单、不发申请。
|
||||
4. `cd server && npx tsc --noEmit`、`npm run typecheck` 通过;新增测试与既有报价契约测试全绿。
|
||||
@@ -156,6 +156,7 @@ AI 提示词清理已停用条目(7 条归档不删、清单过滤 archived、
|
||||
**商品新增/编辑:去除计价单位字段**:开发者附截图「我们不需要计价单位 给我去除」——弹窗中「计价单位」整块 UI(label + `SearchableSelect`)已删,字段顺序变为 规格 → 品牌 → 区域编号;随之失效的 4 条三语文案键(`form.pricingUnit*`)同步删除。⚠️ **后端列保留**:`formData.pricing_unit` 与提交参数未删,新商品仍默认等于单位、编辑既有商品沿用库中值,备货价归一(`prepPriceNormalizeService`)不受影响,见 [20261022-商品表单去除计价单位字段.md](./20261022-商品表单去除计价单位字段.md)。
|
||||
**商品新增:税收编码改为非必填**:开发者「税收编码别强制填写」——删掉 `handleSubmit` 开头那段「推荐未确认就不让保存」的拦截(新增商品时 `needs_confirmation && !taxCodeConfirmed` ⇒ toast 并 return),并删除随之失效的 `products.toast.taxConfirmNeeded` 三语文案。推荐卡片、「待确认」角标、「确认采用」按钮、其它候选均保留为**可选**;高置信度仍自动填码,后端本来就允许 `tax_code` 为空(无迁移),见 [20261022-商品税收编码改为非必填.md](./20261022-商品税收编码改为非必填.md)。
|
||||
**商品新增:按商品名一次给出税码 / 分类 / 规格建议**:开发者「输入商品名后调 AI 解析返回该用哪个税收编码,同时带上 AI 感觉的分类、规格,这些可以修改」,并约束「别把 `server/data/` 里的税收编码文件都丢上去」——沿用原接口 `GET /catalog/tax-code/suggest`,仍然只把官方库检索的 **top 15** 候选(本地索引,1820 条文件永不出服务端)与本分公司分类**本地预筛 top 20** 给模型,一次调用返回 `index` / `category_index` / `spec`;服务端按序号回查校验,AI **造不出税码与分类编码**。前端仅在字段为空且用户未手改时回填分类(复用税率联动)与规格,并用 `aiFillSkipRef` 跳过下一轮避免循环;`need_category` / `need_spec` 两个开关控制是否额外请 AI,不传时行为与改前一致,无 LLM 时只回规则引擎税码。⚠️ 提示词已改,线上已发布版本需走发布流程升 `version`,见 [20261022-商品新增AI推荐税码分类规格.md](./20261022-商品新增AI推荐税码分类规格.md)。
|
||||
**商品新增写入报价单:改走报价单编辑链路**:开发者「这块修改价格 相当于等同于在报价单那边找到了这个商品进行修改,不存在就加入,应该调报价单的修改/新增接口」——抽出 `mergeQuoteSheetItems`:单里已有该 SKU **只改价**(保留单位/成本价/加价率/历史名称快照),没有则新增行;`POST /quote/sheets/:code/append-items` 改走 `appendSkuPricesAndRequestDeliveryReview`,与报价单页编辑同通道(价格类变更会发起「配送商销售价确认」申请,受 `MAIN_SYSTEM_QUOTE_PRICE_PROPOSAL` 开关控制);前端不再提交 `cost_price`(避免命中已有行时清零),响应多一个 `delivery_handoff`(界面暂不提示)。权限门槛与价格口径未改,见 [20261022-商品新增写入报价单改走编辑链路.md](./20261022-商品新增写入报价单改走编辑链路.md)。
|
||||
|
||||
## 全量对齐与改造方案(2026-09-28)
|
||||
|
||||
|
||||
@@ -629,9 +629,9 @@ router.post('/quote/sheets/:code/append-items', authMiddleware, async (req: Auth
|
||||
const ctx = await getWorkflowCtx(req);
|
||||
const operatingBranch = readOperatingBranchCode(req);
|
||||
requireWorkflowPermission(ctx, ['customer_manage', 'order_manage', 'order_entry']);
|
||||
const { appendSkuPricesToQuoteSheet } = await import('../../../services/quoteSheetService');
|
||||
const { appendSkuPricesAndRequestDeliveryReview } = await import('../../../services/quoteSheetEditService');
|
||||
const items = Array.isArray(req.body?.items) ? req.body.items : [];
|
||||
const data = await appendSkuPricesToQuoteSheet({
|
||||
const result = await appendSkuPricesAndRequestDeliveryReview({
|
||||
quoteSheetCode: req.params.code,
|
||||
items: items.map((item: any) => ({
|
||||
sku_code: String(item.sku_code || ''),
|
||||
@@ -644,8 +644,9 @@ router.post('/quote/sheets/:code/append-items', authMiddleware, async (req: Auth
|
||||
})),
|
||||
ctx,
|
||||
operatingBranchCode: operatingBranch,
|
||||
requestedBy: req.userId || null,
|
||||
});
|
||||
return res.json({ data });
|
||||
return res.json({ data: result.data, delivery_handoff: result.delivery_handoff });
|
||||
} catch (error: any) {
|
||||
return res.status(error?.status || 500).json({ error: 'server_error', error_description: error?.message || '写入报价单失败' });
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import {
|
||||
getQuoteSheet,
|
||||
loadQuoteSheetForUpdate,
|
||||
mergeQuoteSheetItems,
|
||||
updateQuoteSheet,
|
||||
type QuoteSheetItemInput,
|
||||
type QuoteSheetItemRow,
|
||||
type QuoteSheetUpdateInput,
|
||||
} from './quoteSheetService';
|
||||
import { submitPriceProposal } from './quotePriceProposalService';
|
||||
@@ -113,3 +117,32 @@ export async function applyQuoteSheetEditAndRequestDeliveryReview(
|
||||
});
|
||||
return { applied: true, data, delivery_handoff };
|
||||
}
|
||||
|
||||
/**
|
||||
* 「按 SKU 写价」(商品新增里的报价区块)→ 走**与报价单页编辑同一通道**:
|
||||
* · 已有该 SKU ⇒ 只改价格;没有 ⇒ 新增一行;其他明细不动;
|
||||
* · 价格类变更自动发起「配送商销售价确认」申请(与 PUT 编辑一致)。
|
||||
*/
|
||||
export async function appendSkuPricesAndRequestDeliveryReview(input: {
|
||||
quoteSheetCode: string;
|
||||
items: QuoteSheetItemInput[];
|
||||
ctx?: TenantContext | null;
|
||||
operatingBranchCode?: string | null;
|
||||
requestedBy?: string | null;
|
||||
}) {
|
||||
const validIncoming = input.items.filter(
|
||||
(item) => String(item.sku_code || '').trim() && Number(item.sale_price ?? item.price) > 0
|
||||
);
|
||||
if (!validIncoming.length) throw new Error('请提供有效的商品与价格');
|
||||
|
||||
const detail = await getQuoteSheet(input.quoteSheetCode, input.ctx, input.operatingBranchCode);
|
||||
const existing = Array.isArray(detail.items) ? (detail.items as QuoteSheetItemRow[]) : [];
|
||||
|
||||
return applyQuoteSheetEditAndRequestDeliveryReview({
|
||||
quoteSheetCode: input.quoteSheetCode,
|
||||
items: mergeQuoteSheetItems(existing, validIncoming),
|
||||
ctx: input.ctx,
|
||||
operatingBranchCode: input.operatingBranchCode,
|
||||
requestedBy: input.requestedBy ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -818,21 +818,31 @@ export async function setQuoteSheetAutoSalePriceByCost(input: {
|
||||
return loadSheetDetail(input.quoteSheetCode);
|
||||
}
|
||||
|
||||
/** 向执行中的报价单追加/更新 SKU 价格(合并已有明细,不覆盖其他商品) */
|
||||
export async function appendSkuPricesToQuoteSheet(input: {
|
||||
quoteSheetCode: string;
|
||||
items: QuoteSheetItemInput[];
|
||||
ctx?: TenantContext | null;
|
||||
operatingBranchCode?: string | null;
|
||||
}) {
|
||||
const newItems = input.items.filter((i) => i.sku_code?.trim() && readSalePrice(i) > 0);
|
||||
if (!newItems.length) throw new Error('请提供有效的商品与价格');
|
||||
export type QuoteSheetItemRow = {
|
||||
sku_code?: string | null;
|
||||
sku_name?: string | null;
|
||||
sku_name_snapshot?: string | null;
|
||||
catalog_name?: string | null;
|
||||
unit?: string | null;
|
||||
price?: number | string | null;
|
||||
sale_price?: number | string | null;
|
||||
cost_price?: number | string | null;
|
||||
markup_ratio?: number | string | null;
|
||||
};
|
||||
|
||||
const detail = await getQuoteSheet(input.quoteSheetCode, input.ctx, input.operatingBranchCode);
|
||||
const existing = Array.isArray(detail.items) ? detail.items : [];
|
||||
/**
|
||||
* 把「按 SKU 写价」合并进报价单明细(与报价单页编辑同语义):
|
||||
* · 单里**已有**该 SKU ⇒ 只改价格,保留单位、成本价、加价率与历史名称快照;
|
||||
* · 单里**没有** ⇒ 新增一行(用商品档案的单位/名称,成本 0、加价率为空);
|
||||
* · 其他商品原样保留。
|
||||
*/
|
||||
export function mergeQuoteSheetItems(
|
||||
existingRows: QuoteSheetItemRow[],
|
||||
incoming: QuoteSheetItemInput[]
|
||||
): QuoteSheetItemInput[] {
|
||||
const merged = new Map<string, QuoteSheetItemInput>();
|
||||
|
||||
for (const row of existing) {
|
||||
for (const row of existingRows) {
|
||||
const skuCode = String(row.sku_code || '').trim();
|
||||
if (!skuCode) continue;
|
||||
merged.set(skuCode, {
|
||||
@@ -840,7 +850,7 @@ export async function appendSkuPricesToQuoteSheet(input: {
|
||||
// 写回时保留**历史快照**(审计/价格追溯),不用档案名覆盖它
|
||||
sku_name:
|
||||
row.sku_name_snapshot?.trim() ||
|
||||
(row.sku_name as string | undefined)?.trim() ||
|
||||
row.sku_name?.trim() ||
|
||||
row.catalog_name?.trim() ||
|
||||
undefined,
|
||||
unit: row.unit?.trim() || undefined,
|
||||
@@ -851,31 +861,29 @@ export async function appendSkuPricesToQuoteSheet(input: {
|
||||
});
|
||||
}
|
||||
|
||||
for (const item of newItems) {
|
||||
const skuCode = item.sku_code.trim();
|
||||
for (const item of incoming) {
|
||||
const skuCode = String(item.sku_code || '').trim();
|
||||
const salePrice = readSalePrice(item);
|
||||
if (!skuCode || salePrice <= 0) continue;
|
||||
|
||||
const prev = merged.get(skuCode);
|
||||
if (prev) {
|
||||
merged.set(skuCode, { ...prev, price: salePrice, sale_price: salePrice });
|
||||
continue;
|
||||
}
|
||||
|
||||
merged.set(skuCode, {
|
||||
sku_code: skuCode,
|
||||
sku_name: item.sku_name?.trim() || merged.get(skuCode)?.sku_name,
|
||||
unit: item.unit?.trim() || merged.get(skuCode)?.unit,
|
||||
price: readSalePrice(item),
|
||||
sale_price: readSalePrice(item),
|
||||
cost_price:
|
||||
item.cost_price !== undefined
|
||||
? readCostPrice(item)
|
||||
: merged.get(skuCode)?.cost_price || 0,
|
||||
markup_ratio:
|
||||
item.markup_ratio !== undefined
|
||||
? readMarkupRatio(item)
|
||||
: merged.get(skuCode)?.markup_ratio ?? null,
|
||||
sku_name: item.sku_name?.trim() || undefined,
|
||||
unit: item.unit?.trim() || undefined,
|
||||
price: salePrice,
|
||||
sale_price: salePrice,
|
||||
cost_price: item.cost_price !== undefined ? readCostPrice(item) : 0,
|
||||
markup_ratio: item.markup_ratio !== undefined ? readMarkupRatio(item) : null,
|
||||
});
|
||||
}
|
||||
|
||||
return updateQuoteSheet({
|
||||
quoteSheetCode: input.quoteSheetCode,
|
||||
items: [...merged.values()].filter((i) => readSalePrice(i) > 0),
|
||||
ctx: input.ctx,
|
||||
operatingBranchCode: input.operatingBranchCode,
|
||||
});
|
||||
return [...merged.values()].filter((item) => readSalePrice(item) > 0);
|
||||
}
|
||||
|
||||
type QuoteDb = Pick<import('pg').PoolClient, 'query'>;
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const getQuoteSheetMock = vi.fn();
|
||||
const loadQuoteSheetForUpdateMock = vi.fn();
|
||||
const updateQuoteSheetMock = vi.fn();
|
||||
const submitPriceProposalMock = vi.fn();
|
||||
|
||||
vi.mock('../src/services/quoteSheetService', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../src/services/quoteSheetService')>();
|
||||
return {
|
||||
...actual,
|
||||
getQuoteSheet: (...args: unknown[]) => getQuoteSheetMock(...args),
|
||||
loadQuoteSheetForUpdate: (...args: unknown[]) => loadQuoteSheetForUpdateMock(...args),
|
||||
updateQuoteSheet: (...args: unknown[]) => updateQuoteSheetMock(...args),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('../src/services/quotePriceProposalService', () => ({
|
||||
submitPriceProposal: (...args: unknown[]) => submitPriceProposalMock(...args),
|
||||
}));
|
||||
|
||||
import { mergeQuoteSheetItems } from '../src/services/quoteSheetService';
|
||||
import { appendSkuPricesAndRequestDeliveryReview } from '../src/services/quoteSheetEditService';
|
||||
|
||||
const existingRows = [
|
||||
{
|
||||
sku_code: 'SKU1',
|
||||
sku_name_snapshot: '土豆(历史名)',
|
||||
unit: '箱',
|
||||
price: 5,
|
||||
sale_price: 5,
|
||||
cost_price: 7,
|
||||
markup_ratio: 20,
|
||||
},
|
||||
{
|
||||
sku_code: 'SKU2',
|
||||
sku_name: '白菜',
|
||||
unit: '斤',
|
||||
price: 3,
|
||||
sale_price: 3,
|
||||
cost_price: 2,
|
||||
markup_ratio: null,
|
||||
},
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
getQuoteSheetMock.mockReset().mockResolvedValue({ quote_sheet_code: 'QS1', items: existingRows });
|
||||
loadQuoteSheetForUpdateMock.mockReset().mockResolvedValue({
|
||||
quote_sheet_code: 'QS1',
|
||||
branch_code: 'BR001',
|
||||
status: 1,
|
||||
approval_status: 30,
|
||||
auto_sale_price_by_cost: false,
|
||||
});
|
||||
updateQuoteSheetMock.mockReset().mockResolvedValue({ quote_sheet_code: 'QS1', items: [] });
|
||||
submitPriceProposalMock.mockReset().mockResolvedValue({ proposal: { id: 'P1', status: 'pending' } });
|
||||
});
|
||||
|
||||
describe('商品新增写入报价单:等同报价单页编辑', () => {
|
||||
it('已有该商品 ⇒ 只改价格,单位/成本价/加价率/历史名称都不动', async () => {
|
||||
const result = await appendSkuPricesAndRequestDeliveryReview({
|
||||
quoteSheetCode: 'QS1',
|
||||
items: [{ sku_code: 'SKU1', sku_name: '土豆(新名字)', unit: '斤', sale_price: 9.9 }],
|
||||
});
|
||||
|
||||
expect(result.applied).toBe(true);
|
||||
const passed = updateQuoteSheetMock.mock.calls[0][0] as {
|
||||
items: Array<Record<string, unknown>>;
|
||||
};
|
||||
const row = passed.items.find((item) => item.sku_code === 'SKU1');
|
||||
expect(row).toMatchObject({
|
||||
sku_code: 'SKU1',
|
||||
sale_price: 9.9,
|
||||
price: 9.9,
|
||||
unit: '箱',
|
||||
cost_price: 7,
|
||||
markup_ratio: 20,
|
||||
sku_name: '土豆(历史名)',
|
||||
});
|
||||
// 其他商品原样保留
|
||||
expect(passed.items.find((item) => item.sku_code === 'SKU2')?.sale_price).toBe(3);
|
||||
});
|
||||
|
||||
it('单里没有该商品 ⇒ 新增一行(成本 0、加价率为空)', async () => {
|
||||
await appendSkuPricesAndRequestDeliveryReview({
|
||||
quoteSheetCode: 'QS1',
|
||||
items: [{ sku_code: 'SKU_NEW', sku_name: '新土豆', unit: '斤', sale_price: 6.5 }],
|
||||
});
|
||||
|
||||
const passed = updateQuoteSheetMock.mock.calls[0][0] as {
|
||||
items: Array<Record<string, unknown>>;
|
||||
};
|
||||
expect(passed.items).toHaveLength(3);
|
||||
expect(passed.items.find((item) => item.sku_code === 'SKU_NEW')).toMatchObject({
|
||||
sale_price: 6.5,
|
||||
unit: '斤',
|
||||
cost_price: 0,
|
||||
markup_ratio: null,
|
||||
sku_name: '新土豆',
|
||||
});
|
||||
});
|
||||
|
||||
it('价格类变更会走同一条「配送商销售价确认」申请', async () => {
|
||||
const result = await appendSkuPricesAndRequestDeliveryReview({
|
||||
quoteSheetCode: 'QS1',
|
||||
items: [{ sku_code: 'SKU1', sale_price: 8 }],
|
||||
requestedBy: 'u-1',
|
||||
});
|
||||
|
||||
expect(submitPriceProposalMock).toHaveBeenCalledTimes(1);
|
||||
expect(submitPriceProposalMock.mock.calls[0][0]).toMatchObject({ quoteSheetCode: 'QS1' });
|
||||
expect(result.delivery_handoff).toMatchObject({ ok: true, proposal_id: 'P1' });
|
||||
});
|
||||
|
||||
it('没有有效商品/价格 ⇒ 直接报错,不重写整单', async () => {
|
||||
await expect(
|
||||
appendSkuPricesAndRequestDeliveryReview({
|
||||
quoteSheetCode: 'QS1',
|
||||
items: [{ sku_code: 'SKU1', sale_price: 0 }],
|
||||
})
|
||||
).rejects.toThrow('请提供有效的商品与价格');
|
||||
expect(updateQuoteSheetMock).not.toHaveBeenCalled();
|
||||
expect(submitPriceProposalMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('mergeQuoteSheetItems', () => {
|
||||
it('只改动传入的 SKU,其余行保持原值', () => {
|
||||
const merged = mergeQuoteSheetItems(existingRows, [{ sku_code: 'SKU2', sale_price: 4.5 }]);
|
||||
expect(merged.find((item) => item.sku_code === 'SKU2')).toMatchObject({
|
||||
sale_price: 4.5,
|
||||
cost_price: 2,
|
||||
unit: '斤',
|
||||
sku_name: '白菜',
|
||||
});
|
||||
expect(merged.find((item) => item.sku_code === 'SKU1')?.sale_price).toBe(5);
|
||||
});
|
||||
});
|
||||
@@ -1345,7 +1345,6 @@ export const SkuManagement: React.FC = () => {
|
||||
sku_name: formData.sku_name,
|
||||
price: quotePrice,
|
||||
sale_price: quotePrice,
|
||||
cost_price: 0,
|
||||
unit: formData.unit.trim() || '斤',
|
||||
}]);
|
||||
showToast(t('products.toast.createOkQuoted'), 'success');
|
||||
|
||||
Reference in New Issue
Block a user