fix(回退事故): 恢复被 29fbf49 代提交覆盖掉的新版本
29fbf49「代提交另一会话遗留在工作区的整批改动」用的是**旧工作树**,落地时把\n比它新的提交覆盖回旧版。本次按文件恢复被误回退的内容(其余不动):\n\n- 分拣标签:客户/区域两个开关、块高只包内容+强制分页、默认纸型 80×60\n (pickingLabelPrint.ts/.test.ts、PickingManagement.tsx)\n- 小程序打印机选择弹层 root-portal + z-index=10000(弹窗被 tab 栏遮挡的修复)\n- 分类新建去重(productCatalog.ts + 契约测试 + 设计文)\n- 迁移 20261044(独立进货建客户档案·方案甲)与对应设计文\n- docs/design/README.md 被删掉的 4 条索引行\n\n29fbf49 里的新增工作(出库照片预览、加工领料预留、散客录单守卫等)保持原样。
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
# 分拣标签:可选打印「客户」与「区域」
|
||||
|
||||
> 开发者 2026-10-14(附弹窗截图):「当前还需要打印一下每一个商品的所在区域 相当于就是可以选择
|
||||
> 是否需要客户名 是否需要携带区域 默认都是需要的」
|
||||
> —— 即:打印弹窗给出**两个开关**,**默认都开**。
|
||||
|
||||
## 1. 背景 / 现状
|
||||
|
||||
- 标签版式见 [20261013-分拣标签打印改蓝牙与浏览器直连.md](./20261013-分拣标签打印改蓝牙与浏览器直连.md) §2.1:
|
||||
标题 / 客户 / 品名 / 重量 / 日期 + 右上二维码及码文 —— **没有区域**。
|
||||
- 区域数据本来就在行上:`picking_items.storage_zone`(注释即「商品默认区域(`skus.storage_zone`)」,
|
||||
`src/utils/picking.ts`);打印弹窗也已能**按区域排队并分组**(`sortItemsForLabelPrint` / `labelGroupKey`),
|
||||
只是这一列没进标签。
|
||||
- 现场:区域空值时弹窗分组标题显示 `—`(`{key || '—'}`)。
|
||||
|
||||
## 2. 决策
|
||||
|
||||
| 项 | 口径 |
|
||||
|---|---|
|
||||
| 两个开关 | 「客户」「区域」,**默认都开**(开发者原话「默认都是需要的」) |
|
||||
| 位置 | 打印弹窗工具栏,「打印标签(N)」全选之后、纸张尺寸之前 |
|
||||
| 文案 | **复用既有 i18n key**(`picking.scope.customer` / `picking.scope.zone`,三语齐备)⇒ **不新增 key、不新增小字** |
|
||||
| 记忆 | 与纸张/旋转同存 `localStorage['picking-label-paper']`(新增 `showCustomer` / `showZone`);老数据缺键 ⇒ 默认 `true` |
|
||||
| 生效面 | **两条入口同一套设置**:批量「打印标签」+ 明细弹层行内「打印标签」 |
|
||||
| 区域取值 | `picking_items.storage_zone`;为空时打 `区域:—`(与弹窗分组标题同口径,不做「空则不占行」的隐式行为) |
|
||||
|
||||
### 2.1 版面(行序)
|
||||
|
||||
```
|
||||
分拣标签 ← 标题(恒定)
|
||||
客户:{客户名} ← 可关
|
||||
区域:{区域} ← 可关(新增,紧跟客户之后)
|
||||
品名:{品名} ← 恒定
|
||||
重量:X.XX 斤 ← 恒定(计数单位则「数量:X.XX 单位」)
|
||||
日期:{YYYY-MM-DD} ← 恒定
|
||||
[QR 110×110] ← 右上角,内容=商品条码
|
||||
[码文]
|
||||
```
|
||||
|
||||
- **关掉的行不占位**:行 `top` 按**实际显示的行序**计算,后面的行整体上移,不留空行
|
||||
(`buildPickingLabelHtml` 由 `lineSpecs` 生成规则,不再写死 `.t-cust/.t-name/…` 的固定 top)。
|
||||
- **块高随实际行数收缩**:仍取「文字行下沿」与「二维码列下沿」的较大值 + 下边距 `insetY`
|
||||
(见 [20261014-分拣标签纸张尺寸预设选择.md](./20261014-分拣标签纸张尺寸预设选择.md) §2.4)。
|
||||
实测:80×60 纸 6 行 ⇒ 27.34mm;50×30 纸 6 行 ⇒ 23.74mm;50×30 关掉客户+区域(4 行)⇒ 20.14mm。
|
||||
|
||||
## 3. 不动的(冻结面 / 范围外)
|
||||
|
||||
- 版式常量:字号 dots、行距 28 dots、二维码 110 dots、四周安全边距 —— 沿用。
|
||||
- 必打行(标题 / 品名 / 重量 / 日期)与重量行换算口径(`formatLabelWeightLine`)—— 未改。
|
||||
- 打印通道(`printHtmlViaHiddenIframe`,`delivery-note-print` 冻结面)、二维码内容(商品条码)—— 未改。
|
||||
- 弹窗其它控件(排队维度、全选、纸张尺寸预设、旋转、测试打印标尺)—— 未改。
|
||||
- ⚠️ **小程序 BLE 标签本次未同步**(开发者本次点的是 Web 打印弹窗):如需两端一致,另行下达。
|
||||
|
||||
## 4. 验收标准(可执行)
|
||||
|
||||
1. 打印弹窗出现「客户」「区域」两个复选框,**默认都勾选**;勾选状态刷新后仍在(localStorage)。
|
||||
2. 默认打印:标签含 `客户:…` 与 `区域:…` 两行,且**区域排在客户之后**(`pickingLabelPrint.test.ts` 钉住 6 行 top)。
|
||||
3. 关「客户」⇒ 不输出 `客户:…`,其后的行整体上移一行(`区域` 顶到第 2 行)。
|
||||
4. 关「区域」⇒ 不输出 `区域:…`;两个都关 ⇒ 回到 4 行(标题/品名/重量/日期),块高随之收缩。
|
||||
5. 区域为空 ⇒ 打 `区域:—`;区域含 HTML 特殊字符 ⇒ 正确转义。
|
||||
6. 明细弹层行内「打印标签」与批量打印**出品一致**(同一套开关 + 同一套纸张/旋转)。
|
||||
7. 回归:`pickingLabelPrint` 单测全绿;`npm run typecheck` 过;i18n 无新增 key(复用即三语齐备)。
|
||||
|
||||
## 5. 变更清单
|
||||
|
||||
| 文件 | 类型 | 意图 |
|
||||
|---|---|---|
|
||||
| `src/utils/pickingLabelPrint.ts` | 改 | `PickingLabel` 增 `zone`;`buildPickingLabelHtml` / `printPickingLabels` 增 `showCustomer` / `showZone`(默认 true);行位置改为按实际行序生成;块高按实际行数算 |
|
||||
| `src/pages/picking/PickingManagement.tsx` | 改 | 弹窗加两个复选框(复用 `picking.scope.customer` / `picking.scope.zone`);`loadLabelPaper` / `patchLabelPaper` 记忆;两处打印入口传 `zone` 与开关 |
|
||||
| `src/utils/pickingLabelPrint.test.ts` | 改 | 补默认 6 行 / 关行上移 / 空区域「—」与转义;更新块高锚点 |
|
||||
| `docs/design/20261014-分拣标签可选客户名与区域.md` | 新增 | 本文 |
|
||||
|
||||
## 6. 回滚
|
||||
|
||||
纯前端、无迁移、无数据、无接口:`git revert` 即回到「标签只有客户、无区域」。
|
||||
`localStorage` 里多出的两个键会被旧代码忽略(旧版只读 `widthMm/heightMm/rotationDeg`)。
|
||||
@@ -0,0 +1,153 @@
|
||||
# 分拣标签打印:弹窗内增加「纸张尺寸」预设选择
|
||||
|
||||
> 开发者 2026-10-14:「我们打印时不是要选择纸张尺寸吗?要不打印时我们也进行一个选择?」
|
||||
> —— 打印对话框里的纸张尺寸要与其一致,手填 mm 容易填错(现场实测:填成 80×60 而实物签是 80×30,
|
||||
> 导致一页占 2 张签、每签后跟一张空白)。
|
||||
|
||||
## 1. 背景 / 现状
|
||||
|
||||
- 弹窗原本只有两个**手填**输入:纸张宽(mm) / 纸张高(mm),存 `localStorage['picking-label-paper']`。
|
||||
- 打印时 `@page { size: Wmm Hmm }`,**Chrome 打印对话框会跟着显示 `Custom(W x H)`** ⇒ 页高由这里决定。
|
||||
- 页高必须**等于一张实物标签的高**;否则一页会盖住多张(留白)或不完整。
|
||||
- 现场偏差就出在这里:没人会去量标签、也不知道该填多少。
|
||||
|
||||
## 2. 决策
|
||||
|
||||
在弹窗里加一个**「纸张尺寸」下拉**,选项**对齐打印机驱动「纸张尺寸」列表里的常见标签规格**(含本机
|
||||
GP-3120 列表中的 `80×30`);选中即把宽/高写入内部值(仍可切到自定义手填)。
|
||||
|
||||
**交互(避免与手填框两处重复,2026-10-14 开发者追问「纸张尺寸不就是了吗」)**:
|
||||
|
||||
| 选项 | 效果 |
|
||||
|---|---|
|
||||
| 选具体规格(如 `80 × 30`) | 写入宽/高,**收起**「纸张宽/高」手填框(预设已含宽高) |
|
||||
| 选「自定义」 | **展开**「纸张宽/高」手填框,可填任意 mm |
|
||||
| 老 localStorage 值不在预设内 | 下拉显示「自定义」,宽/高框展开 |
|
||||
|
||||
| 项 | 口径 |
|
||||
|---|---|
|
||||
| 预设来源 | 打印机驱动纸型列表的常见值:40×25/40×40/40×45/40×50/40×60/40×70/40×80、45×35、50×20/50×30/50×50/50×60/50×80、60×40/60×60/60×100、70×20/70×50/70×60/70×80、80×30/80×40/80×60 |
|
||||
| 选中预设 | `patchLabelPaper({ width, height })`(沿用既有写入 + localStorage 记忆) |
|
||||
| 手填框 | 仅在「自定义」时显示;宽/高仍是真正的存储值 |
|
||||
| 打印代码 | 本节改动本身**不碰**打印代码(只让「宽/高」更容易填对);后来 §2.1–§2.4 才改 `pickingLabelPrint` |
|
||||
|
||||
## 2.1 「旋转」开关(2026-10-14 追加,开发者:预览是横的、实物却竖着)
|
||||
|
||||
**现象**:预览页面是 80×30 横版且内容位置正确,**实物却转了 90°**。⇒ 旋转发生在**打印机驱动/纸型层**,
|
||||
网页无法直接控制驱动的方向。
|
||||
|
||||
**处置**:在弹窗加一个**「旋转」下拉:0°(默认)/ 90° / 180° / 270°**,用于**反向抵消**驱动自身的旋转。
|
||||
|
||||
| 旋转 | `@page` | 标签块 | 结构 |
|
||||
|---|---|---|---|
|
||||
| 0°(默认) | `W H` | 宽 `W`、高「只包住内容」 | **一签一页**(块高 + `break-after: page`,详见 §2.4) |
|
||||
| 90° / 270° | **`H W`(转置)** | `W × (H-0.5)` 绝对定位 + `rotate(±90deg)` | 一页一签(`.pick-page` 页面框 + `break-after`) |
|
||||
| 180° | `W H` | 同上 + `rotate(180deg)` | 一页一签 |
|
||||
|
||||
口径:非法值回落 0°(保持现状);旋转角度与纸张宽/高一起存 `localStorage`。因为旋转 90/270 时
|
||||
页面尺寸会转置,每签必须独占一页(否则流程盒与页宽对不上)。
|
||||
|
||||
> 根因在驱动:最彻底的做法是在**打印对话框选驱动自带的纸型**(如 `80mm * 30mm`)而非 Custom。
|
||||
> 本开关是**页面侧能做的唯一补偿**,同时适配“各人纸型不同”的场景。
|
||||
|
||||
## 2.2 四周安全边距(2026-10-14,开发者:“无论纸张大小四周都该留足够比例的位置”)
|
||||
|
||||
**要求**:不管纸张多大,内容**四面都留出按比例的边距**,不贴边、不被裁。
|
||||
|
||||
**实现**(`src/utils/pickingLabelPrint.ts`):
|
||||
|
||||
```
|
||||
insetX = max(1mm, 纸张宽 × 6%, 纸张宽 − 70mm)
|
||||
insetY = max(1mm, 标签高 × 6%)
|
||||
```
|
||||
|
||||
- **6% 比例边距**:适配“不同尺寸的纸”;
|
||||
- **再加硬约束 `SAFE_PRINT_WIDTH_MM = 70`**:80mm 卷的热敏/标签机打印头可打印宽度只有 ~70–72mm
|
||||
⇒ 左右各让出 `纸张宽 − 70`,取“驱动居中铺”与“左对齐铺”两种映射的**安全交集**
|
||||
(80mm 纸 ⇒ inset = 10mm,内容落在 **10–70mm**;无论哪种映射都不会被裁)。
|
||||
|
||||
四面的边距同时约束:文字左边界 = 二维码右边界 = `insetX`;首行顶部 = `insetY`;行距 = 28 dots = 3.5mm。
|
||||
|
||||
> **另需(对话框侧)**:`@page { margin: 0 }` 会被打印对话框的「边距」设置盖掉(默认约 10mm)。
|
||||
> §2.4 已把标签块高改为「只包住内容」,**边距选「默认」或「无」都能出一签一页**;不必强求选「无」(早期版本依赖选「无」,已废弃)。
|
||||
|
||||
## 2.3 「测试打印」(标尺页,2026-10-14 追加)
|
||||
|
||||
**为什么**:现场出现“文字在、二维码整块没了、右边空白”——这种剪裁形状靠猜无法定位(页面宽 → 实际打印区、
|
||||
原点、方向三个变量叠在一起),所以给一个**可测量的**工具。
|
||||
|
||||
弹窗底部新增**「测试打印」**按钮:用**同一套纸张/旋转设置**打 2 页**标尺页**:
|
||||
整页边框 + 每 5mm 短刻度、每 10mm 长刻度 + 毫米数字(左上起算)+ 中心标注 `纸张宽 × 高 mm 旋转 X°`。
|
||||
|
||||
用法:打一张 → 拍照 → 从图上直接读出:
|
||||
- **从第几毫米开始/结束能打**(可打印区与左右/上下偏移);
|
||||
- **方向到底转没转**(看数字与边框的方向)。
|
||||
|
||||
拿到这两个值后就能一次性把常量/边距调到适配该机型,不再反复试。纯打印 HTML,**不写库、无迁移**。
|
||||
|
||||
**新增文案**(2 条,三语同步):`picking.modal.paperPreset`(纸张尺寸)、`picking.modal.paperCustom`(自定义)。
|
||||
|
||||
## 2.4 一签一页改由「块高只包住内容 + 强制分页」保证(2026-10-14 二次修正)
|
||||
|
||||
**现场实测(GP-3120TUB + 80mm 卷 + 实物签 80×60)**:
|
||||
|
||||
| 对话框纸型 / 边距 | 结果 |
|
||||
|---|---|
|
||||
| `Custom(80mm x 60mm)` + 边距「默认」 | 两张内容都在,但**第 2 张位置不对** |
|
||||
| 同上 + 边距「无」 | **打两张耗三张、中间夹一张空白** |
|
||||
|
||||
**根因**:§2.2 之前的块高 = 纸高(80×60 ⇒ 59.5mm),而**打印对话框的「边距」会盖掉 `@page{margin:0}`**
|
||||
(默认约 10mm,机头另有死区)⇒ 可用高度只有 ~40mm,块溢出后被浏览器**拆到下一页**,
|
||||
把后面每张都挤错位(“空白”就是溢出到下一页的那段空盒子)。
|
||||
旧版靠「自然平铺」也不成立:平铺要求块高**恰好等于可用高度**,而可用高度由对话框决策,页面侧量不到。
|
||||
|
||||
**修正(`src/utils/pickingLabelPrint.ts`)**:
|
||||
|
||||
```
|
||||
blockHeightMm = min( 纸高 − 0.5, max(文字块下沿, 二维码列下沿) + insetY )
|
||||
↑ 80×60 实测 ⇒ 23.84mm(远小于任何纸型的可用高)
|
||||
一签一页 = .pick-label { page-break-after: always } + .pick-label:last-child { auto }
|
||||
```
|
||||
|
||||
⇒ 块永远不溢出,**边距选「默认」或「无」都是一签一页**;末张复位分页,防尾部多出一张空白。
|
||||
旋转 90/270/180 仍用整页高(180° 以中心为原点旋转,改块高会把内容挪位)。
|
||||
|
||||
> ⚠️ 代价:内容只占标签**上部**(80×60 时约占上面 24mm),下余空白。
|
||||
> ⚠️ 不要改回「块高 = 纸高」(会重现本次夹空白);不要用百分比高(打印宿主 `@media print` 把 `html/body` 高度置 auto,百分比会失效)。
|
||||
> ⚠️ 驱动纸型若是「一张含多枚标签」(2-up),强制分页会浪费另一半——本项目实测纸型为单枚(`Custom(80mm x 60mm)`),故取「一签一页」更稳。
|
||||
|
||||
## 2.5 默认纸型改为 80×60(2026-10-14,开发者:「80×60 先设置为这个」)
|
||||
|
||||
- 代码默认值 `DEFAULT_PAPER`(`src/utils/pickingLabelPrint.ts`)与弹窗兜底 `loadLabelPaper()`
|
||||
(`src/pages/picking/PickingManagement.tsx`)由 `50×30` 改为 **`80×60`**(实物签尺寸,GP-3120TUB 80mm 卷)。
|
||||
旧默认 50×30 是照旧安卓秤端版面写的占位值,现场实物签并不是它。
|
||||
- **旋转默认仍为 `0°`**;「客户」「区域」默认仍都打(见 [20261014-分拣标签可选客户名与区域.md](./20261014-分拣标签可选客户名与区域.md))。
|
||||
- ⚠️ **只影响「没存过设置」的浏览器**:`localStorage['picking-label-paper']` 里已有宽/高时以存的为准
|
||||
⇒ 在旧默认下用过的机器需在弹窗下拉里手动选一次 `80 × 60`,之后一直记住。
|
||||
|
||||
## 3. 验收标准(可执行)
|
||||
|
||||
1. 弹窗出现「纸张尺寸」下拉,含 `80 × 30`;选中后宽/高变为 80 / 30,并持久化(刷新仍在);此时**不显示**宽/高手填框。
|
||||
2. 下拉选「自定义」⇒ 展开「纸张宽/高」手填框,可填任意 mm。
|
||||
3. 存储值不在预设内(老数据)⇒ 下拉显示「自定义」、宽/高框展开。
|
||||
4. 打印 HTML 的 `@page` 与 `.pick-label` 宽高跟所填值一致(既有单测已覆盖)。
|
||||
5. **旋转 0°**:不包页面框、无 `transform`;块高 = `min(纸高 − 0.5, 内容下沿 + insetY)`(80×60 ⇒ `23.84mm`),每张 `.pick-label` 都 `page-break-after: always`、末张 `auto`(一签一页);**旋转 90°/270°**:`@page` 转置且标签块 `rotate(±90deg)`、一页一签(仍用整页高);**180°**:尺寸不变、`rotate(180deg)`;非法值回落 0°。
|
||||
6. 真机:选与驱动一致的尺寸 + 能对上的旋转角 ⇒ 一签一页、方向正、不夹空白;**打印对话框边距选「默认」或「无」均不得出现空白页**(打 2 张不得耗 3 张)。
|
||||
7. 回归:`pickingLabelPrint` 单测全绿;`typecheck` 过;i18n 三语齐全。
|
||||
8. 默认纸型:清掉 `localStorage['picking-label-paper']` 后打开弹窗 ⇒ 纸张尺寸显示 `80 × 60`、旋转 `0°`;
|
||||
非法宽/高(0/负数)也回落 `80 × 60`(`pickingLabelPrint.test.ts` 钉住)。
|
||||
|
||||
## 4. 变更清单
|
||||
|
||||
| 文件 | 类型 | 意图 |
|
||||
|---|---|---|
|
||||
| `src/pages/picking/PickingManagement.tsx` | 改 | 弹窗加「纸张尺寸」预设下拉 + 「旋转」下拉 + 「测试打印」(含常量表、显隐与联动) |
|
||||
| `src/utils/pickingLabelPrint.ts` | 改 | `buildPickingLabelHtml` 支持 `rotation`(转置 `@page` + 块旋转 + 旋转模式下一页一签);内容宽度内缩到可打印宽度(`min(纸张宽,72mm)`);新增 `buildPickingLabelRulerHtml`/`printPickingLabelRuler` |
|
||||
| `src/utils/pickingLabelPrint.ts` | 改(§2.4 二次修正) | 旋转 0° 的块高改为「只包住内容」(不再等于纸高)+ 强制分页(末张复位),修「边距选无 ⇒ 打两张耗三张、中间夹空白」;旋转模式仍用整页高 |
|
||||
| `src/i18n/locales/{zh-CN,en,id}/picking.ts` | 改 | 新增 `modal.paperPreset` / `modal.paperCustom` / `modal.rotation` / `modal.testPrint` |
|
||||
| `src/utils/pickingLabelPrint.test.ts` | 改 | 补旋转 0/90/180/270 与非法值、宽纸二维码内缩、标尺页用例 |
|
||||
| `docs/design/20261014-分拣标签纸张尺寸预设选择.md` | 新增 | 本文 |
|
||||
|
||||
## 5. 回滚
|
||||
|
||||
纯前端、无迁移、无数据变更:`git revert` 即回到「只有两个手填框」。
|
||||
@@ -0,0 +1,99 @@
|
||||
# 新建分类去重:同名同父已存在则复用(不再重复创建)
|
||||
|
||||
> 报障 2026-10-14(开发者):「新建分类的接口 我们的后台是不是没有做这个分类是否存在的判断?
|
||||
> 假设这个分类已经存在了应该是不会再次创建了吧」。
|
||||
>
|
||||
> **结论**:确实没有。同一个功能有两套实现 —— **后端**(采购/小程序/库存接口)
|
||||
> `categoryCatalogService.createQuickCatalogCategory` **有查重**;而**订单录入 / 订单详情 /
|
||||
> 分类管理 / 供应商经营品类 / 分类批量导入**走的是**前端** `insertCatalogCategory` **裸插入、
|
||||
> 无查重**,且 `product_catalog` 的分类唯一键只有 `(branch_code, code)`(`code` 随机生成)
|
||||
> ⇒ **同名不同 code 照样能插进去**(已复现)。
|
||||
|
||||
## 1. 目标 / 非目标
|
||||
|
||||
**目标(本轮)**:在前端**唯一写入出口** `insertCatalogCategory` 前加一次查重 ——
|
||||
「同分公司 + 同父级 + `TRIM(name)` 精确相等」已存在 ⇒ **直接复用已有分类**,不再新建;零迁移。
|
||||
|
||||
**非目标(本轮不做,见 §4)**:给 `product_catalog` 加「分类名唯一」DB 约束(方案 3)。
|
||||
原因:需先在生产清点存量重复,且 `category_code` 被 12 张表引用,清理会改历史归属 ——
|
||||
**必须先拿到线上只读清点结果再单独施工**。
|
||||
|
||||
## 2. 设计
|
||||
|
||||
**落点**:`src/utils/productCatalog.ts` 的 `insertCatalogCategory`(6 个调用方共用的唯一出口)。
|
||||
|
||||
```
|
||||
insertCatalogCategory(input)
|
||||
① 解析 branch(现有逻辑不变)
|
||||
② input.code 显式传入 ⇒ 跳过查重(调用方要的是「确保这个 code 存在」)
|
||||
③ 否则:按 (item_type='category', branch_code, parent_code, TRIM(name)) 查一次
|
||||
命中 ⇒ 返回已有 code(不插新行、不动缓存)
|
||||
④ 未命中 ⇒ 走原插入逻辑
|
||||
```
|
||||
|
||||
| 项 | 口径 |
|
||||
|---|---|
|
||||
| 匹配范围 | **同分公司** + **同父级**(一级 = `parent_code IS NULL`;二级 = 同 `parent_code`)+ **`TRIM(name)` 精确相等** |
|
||||
| 大小写 | **不做 `lower()` 归一**(`ABC` 与 `abc` 视为两个分类)——与后端 service、批量导入 `normalizeName`(= `trim`)**三处一致**(避免「预检放过、DB 约束拦下」的口径分裂) |
|
||||
| 保留哪条 | `created_at ASC NULLS LAST, code ASC` 的第一条(与后端 service 同口径) |
|
||||
| 显式 code | 传了 `input.code` ⇒ 不查重(保持「按指定 code 建」的语义) |
|
||||
| 读失败 | **fail-open**:查重是兜底,读报错只记日志、继续新建(无唯一约束时与旧行为一致,不引入回归) |
|
||||
| 禁用分类 | 命中**停用**(`status≠1`)的同名分类也复用 —— ⚠️ 录单页下拉是 `enabledOnly`,会**看不到**它;是否加提示(新增文案)**待开发者批准**,本轮不加 |
|
||||
|
||||
**为什么放 `insertCatalogCategory` 而不是 `createQuickCatalogCategory`**:前者是**全部**前端新建路径的
|
||||
唯一出口(录单/订单详情/分类管理/供应商品类/批量导入/`ensureCatalogCategoryChain`),一处修、全覆盖。
|
||||
`ensureCatalogCategoryChain` 与 `categoryBulkImport` 本来就有内存级预检,本改动对它们是**兜底加强**(无行为变化)。
|
||||
|
||||
## 3. 验收标准(可执行)
|
||||
|
||||
1. 订单录入(或订单详情)「新建分类」输入一个**已存在**的名字 ⇒ **不再新增**,回填的正是已有分类的 code。
|
||||
2. 分类管理「新增」输入已存在名字 ⇒ 不新增(刷新后仍是一条)。
|
||||
3. 不同父级的同名分类**互不影响**(一级「干杂」与某二级下的「干杂」各建各的)。
|
||||
4. 大小写/前后空格:`TRIM` 后相等即命中;仅大小写不同**视为不同**分类。
|
||||
5. 传 `input.code` 的调用(当前无)**不查重**。
|
||||
6. 普通新建(名字不存在)行为不变;回归:分类相关测试全绿。
|
||||
|
||||
## 4. 方案 3(DB 唯一约束)为何暂缓
|
||||
|
||||
| # | 阻塞点 |
|
||||
|---|---|
|
||||
| R1 | 存量重复会让 `CREATE UNIQUE INDEX` **迁移直接失败**;而 `category_code` 被 **12 张表**引用(`order_items` / `product_catalog.parent_code` / `supplier_categories` / `customer_skus` / `quality_reports` / `smart_classification_rules` / `bid_round_items` / `supplier_preparations` / `market_*` / `categories`)⇒ 清重复会改历史归属 |
|
||||
| R2 | 表达式口径必须与 §2 一致(`trim`),否则「预检放过、索引拦下」→ 用户看到裸 `23505` |
|
||||
| R3 | 前端 shim **不支持 `upsert/onConflict`**(实测)⇒ 竞态下仍需 `catch` 回退 |
|
||||
| R4 | `status`(停用)是否入唯一键未裁决 |
|
||||
|
||||
**下一步**:在生产只读跑一次重复清点(SQL 见对话记录 / 本文附 §5),据结果决定方案 3 是否可行。
|
||||
|
||||
## 5. 生产只读清点(无写入)
|
||||
|
||||
```sql
|
||||
DO $$
|
||||
DECLARE sch text; n int;
|
||||
BEGIN
|
||||
FOR sch IN
|
||||
SELECT nspname FROM pg_namespace
|
||||
WHERE nspname IN ('tenant_template','legacy_public') OR nspname LIKE 'tenant_%'
|
||||
LOOP
|
||||
IF to_regclass(format('%I.product_catalog', sch)) IS NULL THEN CONTINUE; END IF;
|
||||
EXECUTE format($f$
|
||||
SELECT count(*) FROM (
|
||||
SELECT branch_code, COALESCE(parent_code,'') pc, trim(name) nm
|
||||
FROM %I.product_catalog WHERE item_type='category'
|
||||
GROUP BY 1,2,3 HAVING count(*) > 1
|
||||
) t$f$, sch) INTO n;
|
||||
IF n > 0 THEN RAISE NOTICE '%:% 组同名分类', sch, n; END IF;
|
||||
END LOOP;
|
||||
END $$;
|
||||
```
|
||||
|
||||
## 6. 变更清单
|
||||
|
||||
| 文件 | 类型 | 意图 |
|
||||
|---|---|---|
|
||||
| `src/utils/productCatalog.ts` | 改 | `insertCatalogCategory` 加「同名同父复用」查重 + 私有查重函数 |
|
||||
| `src/utils/productCatalog.createCategoryDedupe.contract.test.ts` | 新增 | §3 的口径契约 |
|
||||
| `docs/design/20261014-分类新建去重(同名复用已有).md` | 新增 | 本文 |
|
||||
|
||||
## 7. 回滚
|
||||
|
||||
纯前端、无迁移、无数据变更:`git revert` 即回到「裸插入」现状。
|
||||
@@ -0,0 +1,114 @@
|
||||
# 独立进货:改为**系统默认客户档案**(方案甲)—— 客户面自动可见,客户结算仍恒为 0
|
||||
|
||||
> 裁决 2026-10-14(开发者口径,本仓库既有的 [20261012-独立进货(自用哨兵客户)](./20261012-独立进货(自用哨兵客户).md) 的修订):
|
||||
> 「我们的独立进货这个客户也是存在的,可以在**客户列表**看到,还有**订单管理接口的筛选**这些地方,
|
||||
> 都可以被查询到,相当于这就是一个**系统默认的客户**,就是其自己。只需要确保**客户结算界面他的结算金额为 0** 就行 ——
|
||||
> 因为选择独立进货的相当于就是补充库存,补充库存完毕后有客户要买会自己录单、库存抵扣这样走的。」
|
||||
>
|
||||
> 冲突即口径:原设计(2026-10-12)**刻意不落 `customers`** ⇒ 客户列表 / 订单管理客户筛选 / 客户结算客户下拉
|
||||
> 都读 `customers`、看不到它。开发者现裁决改为**正常客户行**。本次按新口径落地,旧文 §2「不建行」一句作废。
|
||||
|
||||
## 1. 目标 / 非目标
|
||||
|
||||
**目标**:把「独立进货」正式建成 `customers` 里的**一行正常客户**(编码沿用哨兵常量),
|
||||
使客户列表、订单管理客户筛选、客户结算客户下拉、录单客户下拉**自动**都能看到它;
|
||||
**客户结算金额恒为 0**(不生成该客户的客户结算)。
|
||||
|
||||
**非目标**:不改状态链、不改供应商结算(应付厂商)、不改销售口径排除、不改录单/入库/结案任何判据;
|
||||
不新增任何前端文案。
|
||||
|
||||
## 2. 关键设计:幂等建档,而不是改判据
|
||||
|
||||
| 选择 | 理由 |
|
||||
|---|---|
|
||||
| 编码/名字**沿用哨兵常量**(`__INDEPENDENT_INBOUND__` / 「独立进货」) | 订单上的 `customer_code` 无需迁移;前后端同源常量不变 |
|
||||
| 建行**幂等**(`ON CONFLICT (customer_code) DO NOTHING`) | 存量库(如某环境已手工建过)重跑不出重复行;**不覆盖**已有行(含用户改过的名字) |
|
||||
| 覆盖面**存量 + 新租户**两处 | 存量走迁移的 `DO` 循环;新租户走 `clone_tenant_schema()` 末尾补插(模板只克隆结构、不克隆数据,故必须改函数) |
|
||||
| 只插**一个固定常量客户** | 不是「本地数据覆盖线上」(不存在业务数据搬运),符合本地库禁令 |
|
||||
|
||||
**客户结算为何仍为 0**:这不是本次新加的判据 —— 建行**不改变**既有短路:
|
||||
`financeReconciliationService.runUpsertCustomerStatementForOrder` 对哨兵单**直接 `return 'skipped'`**
|
||||
([20261012 文 §5](./20261012-独立进货(自用哨兵客户).md) 已定稿)。故建行后该客户**没有任何客户结算**,
|
||||
客户结算页里它的金额只会是 0(选它过滤时空)。
|
||||
|
||||
## 3. 明确**不动**的(哨兵豁免一律保留)
|
||||
|
||||
建行**只为让客户面能看到它**,业务判据一处不改:
|
||||
|
||||
| 面 | 落点 | 处置 |
|
||||
|---|---|---|
|
||||
| 录单校验放行 | `orderEntryScopeService.assertOrderEntryCustomerAccess` | 不改(哨兵仍在查 `customers` 之前短路) |
|
||||
| 入库即完成 / 不被强推 | `processingTaskService` / `orderStatusReconcileService` / `orderAutoAdvanceService` | 不改 |
|
||||
| 不生成拣货/配送任务 | `pickingService` / `orderWorkflowFulfillmentService` | 不改 |
|
||||
| 不计销售 | `salesVisibleOrderSql()`(看板/返利/毛利) | 不改 |
|
||||
| 客户结算不生成 | `financeReconciliationService` 的哨兵短路 | **不改**(这就是「金额 0」的保证) |
|
||||
| 供应商结算照常 | `ensureSupplierStatementsForOrder` | 不改 |
|
||||
| 信用校验放行 | `customerBudgetService.checkCustomerCredit` | 不改 |
|
||||
|
||||
> ⚠️ **为什么保留哨兵豁免**:主系统分配单里同一 `customer_code` 可能**不带** `customers` 行
|
||||
> (真库教训:占位客户 `status=1` 白屏)。豁免是「编码即哨兵」的判据,与「表里有没有这行」解耦 ⇒ **不删**。
|
||||
> 换句话说:**建行是新增的可见性,不是豁免成立的前提**。
|
||||
|
||||
## 4. 前端:录单下拉去重(不新增文案)
|
||||
|
||||
录单页原把 `INDEPENDENT_INBOUND_CUSTOMER_OPTION` **无条件**注入下拉首项(为的是「表里没有也能选」)。
|
||||
建行后 `customers` 查询(`status in (10,20)`)已含哨兵行 ⇒ **会出现两条「独立进货」**。
|
||||
故改为**按编码去重**:客户列表里已有哨兵行就用库里的,没有才注入兜底项。
|
||||
|
||||
| 项 | 说明 |
|
||||
|---|---|
|
||||
| 文件 | `src/pages/orders/CustomerOrder.tsx`(`fetchInitialData`) |
|
||||
| 语义 | 库里有 ⇒ 不注入;库里没有 ⇒ 注入(保持旧兜底) |
|
||||
| 其它面 | 客户列表/订单筛选/客户结算下拉**本就**读 `customers`,不做任何注入 ⇒ 自动出现,无需改 |
|
||||
|
||||
## 5. 迁移(存量 + 新租户)
|
||||
|
||||
文件 `migrations/20261044_independent_inbound_customer.sql`:
|
||||
|
||||
1. 新增 `public.ensure_independent_inbound_customer(p_schema TEXT)`:幂等插入一行固定常量客户;
|
||||
非 `tenant_*` / `tenant_template` / 无 `customers` 表 ⇒ 直接返回。
|
||||
2. `CREATE OR REPLACE FUNCTION public.clone_tenant_schema(...)`:以最新定义(`20260910_tenant_core_rls_on_clone.sql`)
|
||||
逐字重写,仅在 `RETURN` 之前补一行 `PERFORM public.ensure_independent_inbound_customer(v_schema);`(**新租户**)。
|
||||
3. `DO` 循环遍历真实租户 schema(`tenant_%` 且非 `tenant_template`),逐库补插(**存量**)。
|
||||
4. 末尾 `DO` 断言:每个目标 schema **恰有 1 行**该编码。
|
||||
|
||||
**安全口径(RLS FORCE)**:租户 `customers` 为 `ENABLE + FORCE ROW LEVEL SECURITY`,策略要求
|
||||
`branch_code::text = current_setting('app.branch_code', true)`;迁移连接角色是表 owner(`supplier`,
|
||||
非 superuser、无 `BYPASSRLS`)⇒ 直接 INSERT 会因 `WITH CHECK` 失败。
|
||||
故插入前 `set_config('app.branch_code', <该租户 branch_code>, true)`(事务级),插完**恢复**原值。
|
||||
`branch_code` 取值优先 `public.tenant_schema_registry`,缺失回退 `upper(regexp_replace(schema,'^tenant_',''))`。
|
||||
|
||||
| 建行字段 | 值 |
|
||||
|---|---|
|
||||
| `customer_code` | `__INDEPENDENT_INBOUND__` |
|
||||
| `company_name` | `独立进货` |
|
||||
| `branch_code` | 该租户分公司编码(与 RLS 判据一致) |
|
||||
| `status` | `10`(客户列表/订单筛选 `in (10,20)` 命中) |
|
||||
| `source` | `independent_inbound`(与主系统导入占位客户 `main_system` 可区分) |
|
||||
| `settlement_days` / `credit_limit` / `cost_budget` / `sales_budget` | `0` |
|
||||
| `credit_frozen` | `false`;`credit_overdue_days` `0`;`rebate_category_ratios` `{}` |
|
||||
|
||||
## 6. 验收标准(可执行)
|
||||
|
||||
1. **存量库**跑迁移后,每个真实租户 `customers` 各**恰有 1 行** `company_name='独立进货' / status=10`;**再跑一次行数不变**(幂等)。
|
||||
2. **新租户**(调用 `clone_tenant_schema`)克隆完成后同样**有这 1 行**。
|
||||
3. 客户列表、订单管理客户筛选、客户结算客户下拉、录单客户下拉**各出现且只出现一次**「独立进货」。
|
||||
4. **客户结算金额 0**:对哨兵单不生成任何客户结算(`runUpsertCustomerStatementForOrder` 仍 `return 'skipped'`)。
|
||||
5. **回归**:普通订单/普通客户一切行为不变;`server/tests/independentInbound*.test.ts` 全绿(豁免断言不变)。
|
||||
|
||||
## 7. 变更清单
|
||||
|
||||
| 文件 | 类型 | 意图 |
|
||||
|---|---|---|
|
||||
| `migrations/20261044_independent_inbound_customer.sql` | 新增 | 存量补插 + `clone_tenant_schema` 补插(新租户)+ 断言 |
|
||||
| `src/pages/orders/CustomerOrder.tsx` | 改 | 录单下拉按编码去重(避免两条「独立进货」) |
|
||||
| `src/utils/independentInbound.ts` | 改 | 头注释纠偏(已建客户档案,不再是「不落 customers」) |
|
||||
| `server/src/services/independentInbound.ts` | 改 | 头注释纠偏(同上) |
|
||||
| `server/tests/independentInbound.test.ts` | 改 | `①` 组改为「已建档」,加迁移契约断言 |
|
||||
| `src/components/order/OrderParsedItemRow.independentInbound.test.ts` | 改 | 加「下拉去重」契约断言 |
|
||||
| `docs/design/20261014-独立进货建客户档案(方案甲).md` | 新增 | 本文 |
|
||||
|
||||
## 8. 回滚
|
||||
|
||||
代码:`git revert`。数据:`DELETE FROM <tenant>.customers WHERE customer_code='__INDEPENDENT_INBOUND__'`
|
||||
(该行不被任何订单外键引用;订单上的 `customer_code` 无外键,回退后回到「客户面看不到」的旧行为)。
|
||||
@@ -0,0 +1,197 @@
|
||||
-- 独立进货:把哨兵客户正式建成 customers 里的**正常客户行**(方案甲,2026-10-14 开发者裁决)。
|
||||
--
|
||||
-- 设计:docs/design/20261014-独立进货建客户档案(方案甲).md
|
||||
--
|
||||
-- 背景:原设计(2026-10-12)刻意**不落 `customers`** ⇒ 客户列表 / 订单管理客户筛选 /
|
||||
-- 客户结算客户下拉都读 `customers`,看不到它。开发者裁决改为**正常客户行**:
|
||||
-- 编码/名字沿用哨兵常量(订单上 customer_code 无需迁移),客户结算仍恒为 0
|
||||
-- (`financeReconciliationService` 对哨兵单直接 skipped —— 本迁移**不动**该短路)。
|
||||
--
|
||||
-- 隔离边界(勿破):
|
||||
-- 1) 只 INSERT 一个**固定常量客户**(编码/名字来自哨兵常量),不碰任何订单/结算/库存数据,
|
||||
-- 也不是「本地数据覆盖线上」;
|
||||
-- 2) 幂等:`ON CONFLICT (customer_code) DO NOTHING` —— 存量已手工建过则保留原样(含用户改过的名字);
|
||||
-- 3) 存量 + 新租户两处都覆盖:存量走本文件末尾的 DO 循环,新租户走 `clone_tenant_schema()` 末尾补插。
|
||||
--
|
||||
-- ⚠️ 租户 `customers` 为 FORCE ROW LEVEL SECURITY,且迁移连接角色是表 owner(非 superuser、无 BYPASSRLS)
|
||||
-- ⇒ 写入前必须 `set_config('app.branch_code', <该租户 branch_code>, true)`,否则 WITH CHECK 失败
|
||||
-- (new row violates row-level security policy)。插完恢复原值,避免污染调用方会话。
|
||||
|
||||
-- 1) 幂等建档助手:给单个租户 schema 补一行「独立进货」客户
|
||||
CREATE OR REPLACE FUNCTION public.ensure_independent_inbound_customer(p_schema TEXT)
|
||||
RETURNS BOOLEAN
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_branch TEXT;
|
||||
v_prev TEXT;
|
||||
v_inserted INTEGER := 0;
|
||||
BEGIN
|
||||
IF p_schema IS NULL
|
||||
OR p_schema NOT LIKE 'tenant_%'
|
||||
OR p_schema = 'tenant_template' THEN
|
||||
RETURN FALSE;
|
||||
END IF;
|
||||
IF to_regclass(format('%I.customers', p_schema)) IS NULL THEN
|
||||
RETURN FALSE;
|
||||
END IF;
|
||||
|
||||
SELECT branch_code INTO v_branch
|
||||
FROM public.tenant_schema_registry
|
||||
WHERE schema_name = p_schema
|
||||
LIMIT 1;
|
||||
|
||||
v_branch := COALESCE(NULLIF(trim(v_branch), ''), upper(regexp_replace(p_schema, '^tenant_', '')));
|
||||
|
||||
v_prev := current_setting('app.branch_code', true);
|
||||
PERFORM set_config('app.branch_code', v_branch, true);
|
||||
|
||||
EXECUTE format(
|
||||
'INSERT INTO %I.customers
|
||||
(customer_code, company_name, branch_code, status, source,
|
||||
settlement_days, credit_limit, credit_frozen, credit_overdue_days,
|
||||
cost_budget, sales_budget, rebate_category_ratios)
|
||||
VALUES (''__INDEPENDENT_INBOUND__'', ''独立进货'', %L, 10, ''independent_inbound'',
|
||||
0, 0, false, 0, 0, 0, ''{}''::jsonb)
|
||||
ON CONFLICT (customer_code) DO NOTHING',
|
||||
p_schema,
|
||||
v_branch
|
||||
);
|
||||
GET DIAGNOSTICS v_inserted = ROW_COUNT;
|
||||
|
||||
PERFORM set_config('app.branch_code', COALESCE(v_prev, ''), true);
|
||||
RETURN v_inserted > 0;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- 2) 新租户入口:以 20260910_tenant_core_rls_on_clone.sql 的最新定义为基底逐字重写,
|
||||
-- 仅在 RETURN 之前补一行「独立进货」客户(模板只克隆结构、不克隆数据 ⇒ 必须在此处补)。
|
||||
CREATE OR REPLACE FUNCTION public.clone_tenant_schema(
|
||||
p_branch_code TEXT,
|
||||
p_status TEXT DEFAULT 'pending'
|
||||
) RETURNS TEXT
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_schema TEXT;
|
||||
t TEXT;
|
||||
v_status TEXT := lower(COALESCE(NULLIF(trim(p_status), ''), 'pending'));
|
||||
v_source TEXT := 'tenant_template';
|
||||
BEGIN
|
||||
IF v_status NOT IN ('pending', 'active') THEN
|
||||
RAISE EXCEPTION 'invalid tenant schema status: %', p_status;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT 1 FROM public.branches WHERE branch_code = upper(trim(p_branch_code))) THEN
|
||||
IF NOT EXISTS (SELECT 1 FROM public.branches WHERE branch_code = trim(p_branch_code)) THEN
|
||||
RAISE EXCEPTION 'branch not found: %', p_branch_code;
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
v_schema := public.tenant_schema_name(p_branch_code);
|
||||
EXECUTE format('CREATE SCHEMA IF NOT EXISTS %I', v_schema);
|
||||
|
||||
IF to_regclass('tenant_template.orders') IS NULL
|
||||
AND to_regclass('tenant_template.product_catalog') IS NULL THEN
|
||||
IF to_regclass('legacy_public.orders') IS NOT NULL THEN
|
||||
v_source := 'legacy_public';
|
||||
ELSE
|
||||
v_source := 'public';
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
FOREACH t IN ARRAY public.tenant_business_tables()
|
||||
LOOP
|
||||
IF to_regclass(format('%I.%I', v_source, t)) IS NOT NULL THEN
|
||||
PERFORM public._clone_table_structure(v_source, v_schema, t);
|
||||
ELSIF to_regclass(format('tenant_template.%I', t)) IS NOT NULL THEN
|
||||
PERFORM public._clone_table_structure('tenant_template', v_schema, t);
|
||||
ELSIF to_regclass(format('legacy_public.%I', t)) IS NOT NULL THEN
|
||||
PERFORM public._clone_table_structure('legacy_public', v_schema, t);
|
||||
ELSIF to_regclass(format('public.%I', t)) IS NOT NULL THEN
|
||||
PERFORM public._clone_table_structure('public', v_schema, t);
|
||||
END IF;
|
||||
END LOOP;
|
||||
|
||||
INSERT INTO public.tenant_schema_registry (branch_code, schema_name, status, updated_at)
|
||||
VALUES (trim(p_branch_code), v_schema, v_status, NOW())
|
||||
ON CONFLICT (branch_code) DO UPDATE
|
||||
SET schema_name = EXCLUDED.schema_name,
|
||||
status = CASE
|
||||
WHEN public.tenant_schema_registry.status = 'active' THEN 'active'
|
||||
ELSE EXCLUDED.status
|
||||
END,
|
||||
updated_at = NOW();
|
||||
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'canteen_app_rls') THEN
|
||||
EXECUTE format('GRANT USAGE ON SCHEMA %I TO canteen_app_rls', v_schema);
|
||||
EXECUTE format('GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA %I TO canteen_app_rls', v_schema);
|
||||
EXECUTE format('GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA %I TO canteen_app_rls', v_schema);
|
||||
END IF;
|
||||
EXCEPTION
|
||||
WHEN insufficient_privilege THEN
|
||||
RAISE NOTICE 'skip grants on % for canteen_app_rls: %', v_schema, SQLERRM;
|
||||
END;
|
||||
|
||||
PERFORM public.ensure_product_catalog_sync_trigger(v_schema);
|
||||
PERFORM public.ensure_orphan_fulfillment_trigger(v_schema);
|
||||
PERFORM public.ensure_tenant_core_foreign_keys(v_schema);
|
||||
PERFORM public.apply_tenant_core_rls(v_schema);
|
||||
|
||||
-- 新租户:补一行系统默认客户「独立进货」(RLS 已生效,助手内部注入 branch_code)
|
||||
PERFORM public.ensure_independent_inbound_customer(v_schema);
|
||||
|
||||
RETURN v_schema;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- 3) 存量:给每个真实租户 schema 补插(幂等)
|
||||
DO $$
|
||||
DECLARE
|
||||
sch text;
|
||||
n INTEGER := 0;
|
||||
BEGIN
|
||||
FOR sch IN
|
||||
SELECT nspname
|
||||
FROM pg_namespace
|
||||
WHERE nspname LIKE 'tenant_%'
|
||||
AND nspname <> 'tenant_template'
|
||||
ORDER BY nspname
|
||||
LOOP
|
||||
IF public.ensure_independent_inbound_customer(sch) THEN
|
||||
n := n + 1;
|
||||
END IF;
|
||||
END LOOP;
|
||||
RAISE NOTICE 'independent inbound customer ensured on % schema(s) (freshly inserted)', n;
|
||||
END $$;
|
||||
|
||||
-- 4) 自证:每个真实租户 schema 恰有 1 行该编码
|
||||
-- ⚠️ 计数同样受 FORCE RLS 约束 ⇒ 逐 schema 注入 app.branch_code 后再读,否则恒为 0。
|
||||
DO $$
|
||||
DECLARE
|
||||
sch text;
|
||||
v_branch text;
|
||||
cnt INTEGER;
|
||||
BEGIN
|
||||
FOR sch, v_branch IN
|
||||
SELECT n.nspname,
|
||||
COALESCE(NULLIF(trim(r.branch_code), ''), upper(regexp_replace(n.nspname, '^tenant_', '')))
|
||||
FROM pg_namespace n
|
||||
LEFT JOIN public.tenant_schema_registry r ON r.schema_name = n.nspname
|
||||
WHERE n.nspname LIKE 'tenant_%'
|
||||
AND n.nspname <> 'tenant_template'
|
||||
AND to_regclass(format('%I.customers', n.nspname)) IS NOT NULL
|
||||
ORDER BY n.nspname
|
||||
LOOP
|
||||
PERFORM set_config('app.branch_code', v_branch, true);
|
||||
EXECUTE format(
|
||||
'SELECT count(*) FROM %I.customers WHERE customer_code = ''__INDEPENDENT_INBOUND__''',
|
||||
sch
|
||||
) INTO cnt;
|
||||
IF cnt <> 1 THEN
|
||||
RAISE EXCEPTION '迁移未生效:% 的独立进货客户行数 = %(应为 1)', sch, cnt;
|
||||
END IF;
|
||||
END LOOP;
|
||||
PERFORM set_config('app.branch_code', '', true);
|
||||
END $$;
|
||||
@@ -1,23 +1,30 @@
|
||||
<van-popup show="{{show}}" position="center" bind:close="onClose">
|
||||
<view class="sheet">
|
||||
<view class="sheet-title">选择蓝牙热敏机</view>
|
||||
<view class="sheet-hint">只连 BLE 小票机。得力 M1022W 等 A4 激光机请用「A4 文档」打印,不要在这里搜索。</view>
|
||||
<view wx:if="{{error}}" class="sheet-error">{{error}}</view>
|
||||
<view wx:if="{{scanning}}" class="sheet-muted">正在搜索附近设备…</view>
|
||||
<view wx:elif="{{!devices.length && !error}}" class="sheet-muted">未发现带名称的 BLE 设备,请打开打印机并靠近手机</view>
|
||||
<view
|
||||
wx:for="{{devices}}"
|
||||
wx:key="deviceId"
|
||||
class="device-row"
|
||||
data-id="{{item.deviceId}}"
|
||||
bindtap="onPick"
|
||||
>
|
||||
<view class="device-name">{{item.name}}</view>
|
||||
<view class="device-rssi">信号 {{item.RSSI}}</view>
|
||||
<!-- 必须 root-portal:三个面板(分拣/备货/采购)都嵌在 pages/home 的
|
||||
`.home-purchase-embed.anim-rise` 工作区里,该容器因 `animation-fill-mode: both`
|
||||
永久保留 `transform: translateY(0)` ⇒ 成为 `position: fixed` 的包含块,
|
||||
弹层既不按视口居中,又困在它的层叠上下文内,被底部 tab 栏(`.tab-bar` z-index:100)盖住。
|
||||
z-index 取 10000,与面板内其它弹层(batch-mask / modal-mask)一致。 -->
|
||||
<root-portal enable="{{true}}">
|
||||
<van-popup show="{{show}}" position="center" z-index="10000" bind:close="onClose">
|
||||
<view class="sheet">
|
||||
<view class="sheet-title">选择蓝牙热敏机</view>
|
||||
<view class="sheet-hint">只连 BLE 小票机。得力 M1022W 等 A4 激光机请用「A4 文档」打印,不要在这里搜索。</view>
|
||||
<view wx:if="{{error}}" class="sheet-error">{{error}}</view>
|
||||
<view wx:if="{{scanning}}" class="sheet-muted">正在搜索附近设备…</view>
|
||||
<view wx:elif="{{!devices.length && !error}}" class="sheet-muted">未发现带名称的 BLE 设备,请打开打印机并靠近手机</view>
|
||||
<view
|
||||
wx:for="{{devices}}"
|
||||
wx:key="deviceId"
|
||||
class="device-row"
|
||||
data-id="{{item.deviceId}}"
|
||||
bindtap="onPick"
|
||||
>
|
||||
<view class="device-name">{{item.name}}</view>
|
||||
<view class="device-rssi">信号 {{item.RSSI}}</view>
|
||||
</view>
|
||||
<view class="sheet-actions">
|
||||
<view class="sheet-btn" bindtap="scan">重新搜索</view>
|
||||
<view class="sheet-btn sheet-btn--ghost" bindtap="onClose">取消</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="sheet-actions">
|
||||
<view class="sheet-btn" bindtap="scan">重新搜索</view>
|
||||
<view class="sheet-btn sheet-btn--ghost" bindtap="onClose">取消</view>
|
||||
</view>
|
||||
</view>
|
||||
</van-popup>
|
||||
</van-popup>
|
||||
</root-portal>
|
||||
|
||||
@@ -140,23 +140,38 @@ const LABEL_PAPER_PRESETS: Array<{ w: number; h: number }> = [
|
||||
|
||||
const labelPaperKey = (w: unknown, h: unknown) => `${Number(w) || 0}x${Number(h) || 0}`;
|
||||
|
||||
function loadLabelPaper(): { width: string; height: string; rotation: string } {
|
||||
function loadLabelPaper(): {
|
||||
width: string;
|
||||
height: string;
|
||||
rotation: string;
|
||||
showCustomer: boolean;
|
||||
showZone: boolean;
|
||||
} {
|
||||
try {
|
||||
const raw = localStorage.getItem(LABEL_PAPER_STORAGE_KEY);
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw) as { widthMm?: number; heightMm?: number; rotationDeg?: number };
|
||||
const parsed = JSON.parse(raw) as {
|
||||
widthMm?: number;
|
||||
heightMm?: number;
|
||||
rotationDeg?: number;
|
||||
showCustomer?: boolean;
|
||||
showZone?: boolean;
|
||||
};
|
||||
if (parsed?.widthMm && parsed?.heightMm) {
|
||||
return {
|
||||
width: String(parsed.widthMm),
|
||||
height: String(parsed.heightMm),
|
||||
rotation: String(parsed.rotationDeg || 0),
|
||||
/** 老数据没这两项 ⇒ 默认都打(2026-10-14 开发者:「默认都是需要的」) */
|
||||
showCustomer: parsed.showCustomer !== false,
|
||||
showZone: parsed.showZone !== false,
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
return { width: '50', height: '30', rotation: '0' };
|
||||
return { width: '80', height: '60', rotation: '0', showCustomer: true, showZone: true };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1663,18 +1678,27 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
const handlePrintItemLabel = async (item: PickingItemRow) => {
|
||||
if (!selectedTask) return;
|
||||
try {
|
||||
await printPickingLabels([
|
||||
await printPickingLabels(
|
||||
[
|
||||
{
|
||||
companyName: selectedTask.company_name || '',
|
||||
itemName: item.item_name || '',
|
||||
qty: settlementQtyOf(item.id, item),
|
||||
unit: item.unit || '',
|
||||
date: selectedTask.delivery_date
|
||||
? formatPickingTaskCalendarDay(selectedTask.delivery_date)
|
||||
: '',
|
||||
barcode: item.label_barcode || item.barcode || item.sku_code || '',
|
||||
zone: item.storage_zone || '',
|
||||
},
|
||||
],
|
||||
{
|
||||
companyName: selectedTask.company_name || '',
|
||||
itemName: item.item_name || '',
|
||||
qty: settlementQtyOf(item.id, item),
|
||||
unit: item.unit || '',
|
||||
date: selectedTask.delivery_date
|
||||
? formatPickingTaskCalendarDay(selectedTask.delivery_date)
|
||||
: '',
|
||||
barcode: item.label_barcode || item.barcode || item.sku_code || '',
|
||||
},
|
||||
]);
|
||||
paper: { widthMm: Number(labelPaper.width), heightMm: Number(labelPaper.height) },
|
||||
rotation: Number(labelPaper.rotation) || 0,
|
||||
showCustomer: labelPaper.showCustomer,
|
||||
showZone: labelPaper.showZone,
|
||||
}
|
||||
);
|
||||
} catch (error: unknown) {
|
||||
notify.error(error instanceof Error ? error.message : t('picking.toast.printFail'));
|
||||
}
|
||||
@@ -1783,7 +1807,15 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
setLabelPrintChecked(Object.fromEntries(labelPrintOrdered.map((item) => [item.id, checked])));
|
||||
};
|
||||
|
||||
const patchLabelPaper = (patch: Partial<{ width: string; height: string; rotation: string }>) => {
|
||||
const patchLabelPaper = (
|
||||
patch: Partial<{
|
||||
width: string;
|
||||
height: string;
|
||||
rotation: string;
|
||||
showCustomer: boolean;
|
||||
showZone: boolean;
|
||||
}>
|
||||
) => {
|
||||
setLabelPaper((prev) => {
|
||||
const next = { ...prev, ...patch };
|
||||
try {
|
||||
@@ -1793,6 +1825,8 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
widthMm: Number(next.width) || 0,
|
||||
heightMm: Number(next.height) || 0,
|
||||
rotationDeg: Number(next.rotation) || 0,
|
||||
showCustomer: next.showCustomer !== false,
|
||||
showZone: next.showZone !== false,
|
||||
})
|
||||
);
|
||||
} catch {
|
||||
@@ -1828,11 +1862,14 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
unit: item.unit || '',
|
||||
date: task?.delivery_date ? formatPickingTaskCalendarDay(task.delivery_date) : '',
|
||||
barcode: item.label_barcode || item.barcode || item.sku_code || '',
|
||||
zone: item.storage_zone || '',
|
||||
};
|
||||
}),
|
||||
{
|
||||
paper: { widthMm: Number(labelPaper.width), heightMm: Number(labelPaper.height) },
|
||||
rotation: Number(labelPaper.rotation) || 0,
|
||||
showCustomer: labelPaper.showCustomer,
|
||||
showZone: labelPaper.showZone,
|
||||
}
|
||||
);
|
||||
setLabelPrintModalOpen(false);
|
||||
@@ -2781,6 +2818,23 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
/>
|
||||
{t('picking.modal.printLabelCount', { count: labelPrintCheckedItems.length })}
|
||||
</label>
|
||||
{/* 标签内容开关(默认都打):文案复用既有「客户」「区域」,未新增 i18n key */}
|
||||
<label className="flex items-center gap-2 text-sm text-gray-600 select-none">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={labelPaper.showCustomer}
|
||||
onChange={(e) => patchLabelPaper({ showCustomer: e.target.checked })}
|
||||
/>
|
||||
{t('picking.scope.customer')}
|
||||
</label>
|
||||
<label className="flex items-center gap-2 text-sm text-gray-600 select-none">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={labelPaper.showZone}
|
||||
onChange={(e) => patchLabelPaper({ showZone: e.target.checked })}
|
||||
/>
|
||||
{t('picking.scope.zone')}
|
||||
</label>
|
||||
<label className="flex items-center gap-1 text-sm text-gray-600">
|
||||
{t('picking.modal.paperPreset')}
|
||||
<select
|
||||
|
||||
@@ -61,9 +61,9 @@ describe('sortItemsForLabelPrint(批量打印排队)', () => {
|
||||
});
|
||||
|
||||
describe('buildPickingLabelHtml(分拣标签 · 浏览器打印)', () => {
|
||||
it('默认纸张 50×30mm(可被传入覆盖)', () => {
|
||||
it('默认纸张 80×60mm(实物签尺寸;可被传入覆盖)', () => {
|
||||
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 10, unit: '斤' }]);
|
||||
expect(html).toContain('@page { size: 50mm 30mm; margin: 0; }');
|
||||
expect(html).toContain('@page { size: 80mm 60mm; margin: 0; }');
|
||||
expect(html).toContain('class="pick-label"');
|
||||
expect(html).toContain('分拣标签');
|
||||
});
|
||||
@@ -74,22 +74,22 @@ describe('buildPickingLabelHtml(分拣标签 · 浏览器打印)', () => {
|
||||
});
|
||||
expect(html).toContain('@page { size: 80mm 60mm; margin: 0; }');
|
||||
expect(html).toMatch(/\.pick-label \{[^}]*width: 80mm/);
|
||||
// 块高**只包住内容**(不是 59.5mm = 纸高):insetY 3.57 + 内容下沿 20.27 + 下边距 3.57 ≈ 23.84
|
||||
expect(html).toMatch(/\.pick-label \{[^}]*height: 23\.84mm/);
|
||||
// 块高**只包住内容**(不是 59.5mm = 纸高):insetY 3.57 + 末行下沿 23.77 + 下边距 3.57 = 27.34
|
||||
expect(html).toMatch(/\.pick-label \{[^}]*height: 27\.34mm/);
|
||||
|
||||
const bad = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
|
||||
paper: { widthMm: 0, heightMm: -3 },
|
||||
});
|
||||
expect(bad).toContain('@page { size: 50mm 30mm; margin: 0; }');
|
||||
expect(bad).toContain('@page { size: 80mm 60mm; margin: 0; }');
|
||||
});
|
||||
|
||||
it('一签一页:块高只包住内容 + 强制分页(防对话框边距把块挤到下一页)', () => {
|
||||
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤' }]);
|
||||
// 回归①(2026-10-14 实测):块高 = 纸高 时,打印对话框的「边距」会盖掉 @page{margin:0}、
|
||||
// 把可用高度压小 ⇒ 块溢出被浏览器拆到下一页 ⇒ 打两张耗三张、中间夹一张空白。
|
||||
// 块高必须**小于**纸高,且 50×30 时块高 ≤ 20.24mm(远小于任何纸型的可用高)。
|
||||
expect(html).not.toMatch(/\.pick-label \{[^}]*height: 29\.5mm/);
|
||||
expect(html).toMatch(/\.pick-label \{[^}]*height: 20\.24mm/);
|
||||
// 块高必须**小于**纸高,且默认 80×60(6 行)时为 27.34mm(远小于任何纸型的可用高)。
|
||||
expect(html).not.toMatch(/\.pick-label \{[^}]*height: 59\.5mm/);
|
||||
expect(html).toMatch(/\.pick-label \{[^}]*height: 27\.34mm/);
|
||||
expect(html).toMatch(/\.pick-label \{[^}]*page-break-inside: avoid/);
|
||||
// 一签一页改靠**强制分页**;末张复位,防尾部多出一张空白页
|
||||
expect(html).toMatch(/\.pick-label \{[^}]*page-break-after: always/);
|
||||
@@ -194,6 +194,55 @@ describe('buildPickingLabelHtml(分拣标签 · 浏览器打印)', () => {
|
||||
expect(html).toContain('@page { size: 80mm 30mm; margin: 0; }');
|
||||
});
|
||||
|
||||
it('默认打「客户」与「区域」两行(区域紧跟客户之后)', () => {
|
||||
const html = buildPickingLabelHtml(
|
||||
[{ itemName: '大葱', qty: 1, unit: '斤', companyName: '甲客户', zone: 'A区' }],
|
||||
{ paper: { widthMm: 50, heightMm: 30 } }
|
||||
);
|
||||
expect(html).toContain('客户:甲客户');
|
||||
expect(html).toContain('区域:A区');
|
||||
// 6 行自上而下:insetY 1.77 + n × 3.5
|
||||
expect(html).toMatch(/\.t-title \{ top: 1\.770mm/);
|
||||
expect(html).toMatch(/\.t-cust \{ top: 5\.270mm/);
|
||||
expect(html).toMatch(/\.t-zone \{ top: 8\.770mm/);
|
||||
expect(html).toMatch(/\.t-name \{ top: 12\.270mm/);
|
||||
expect(html).toMatch(/\.t-weight \{ top: 15\.770mm/);
|
||||
expect(html).toMatch(/\.t-date \{ top: 19\.270mm/);
|
||||
});
|
||||
|
||||
it('可关「客户」/「区域」:关掉的行不输出,后面的行上移(不留空行)', () => {
|
||||
const label = { itemName: '大葱', qty: 1, unit: '斤', companyName: '甲客户', zone: 'A区' };
|
||||
const paper = { widthMm: 50, heightMm: 30 };
|
||||
const noCust = buildPickingLabelHtml([label], { paper, showCustomer: false });
|
||||
expect(noCust).not.toContain('客户:');
|
||||
expect(noCust).toMatch(/\.t-zone \{ top: 5\.270mm/);
|
||||
expect(noCust).toMatch(/\.t-name \{ top: 8\.770mm/);
|
||||
|
||||
const noZone = buildPickingLabelHtml([label], { paper, showZone: false });
|
||||
expect(noZone).not.toContain('区域:');
|
||||
expect(noZone).toMatch(/\.t-name \{ top: 8\.770mm/);
|
||||
|
||||
const none = buildPickingLabelHtml([label], { paper, showCustomer: false, showZone: false });
|
||||
expect(none).not.toContain('客户:');
|
||||
expect(none).not.toContain('区域:');
|
||||
expect(none).toMatch(/\.t-title \{ top: 1\.770mm/);
|
||||
expect(none).toMatch(/\.t-name \{ top: 5\.270mm/);
|
||||
expect(none).toMatch(/\.t-weight \{ top: 8\.770mm/);
|
||||
expect(none).toMatch(/\.t-date \{ top: 12\.270mm/);
|
||||
// 行少了 ⇒ 块高跟着收缩(50×30 四行 = 20.14mm,仍由二维码列主导)
|
||||
expect(none).toMatch(/\.pick-label \{[^}]*height: 20\.14mm/);
|
||||
});
|
||||
|
||||
it('区域为空打「—」(与弹窗分组标题同口径),且仍做 HTML 转义', () => {
|
||||
const empty = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }]);
|
||||
expect(empty).toContain('区域:—');
|
||||
|
||||
const escaped = buildPickingLabelHtml([
|
||||
{ itemName: 'a', qty: 1, unit: '斤', zone: '<b>&区</b>' },
|
||||
]);
|
||||
expect(escaped).toContain('区域:<b>&区</b>');
|
||||
});
|
||||
|
||||
it('有 barcode 时内联二维码 SVG;无则不输出', () => {
|
||||
const withQr = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤', barcode: 'SKU-1' }]);
|
||||
expect(withQr).toContain('class="t-qr"');
|
||||
|
||||
@@ -18,15 +18,18 @@ export interface PickingLabel {
|
||||
date?: string | null;
|
||||
/** 二维码内容与二维码下方码文(旧秤端即商品条码) */
|
||||
barcode?: string | null;
|
||||
/** 商品所在区域(`picking_items.storage_zone`)——2026-10-14 开发者要求可打到标签上 */
|
||||
zone?: string | null;
|
||||
}
|
||||
|
||||
/** 标签纸张尺寸(mm)——**不写死**,由打印弹窗传入并记住;默认 50×30 */
|
||||
/** 标签纸张尺寸(mm)——**不写死**,由打印弹窗传入并记住;默认 80×60(实物签尺寸,2026-10-14 开发者指定) */
|
||||
export interface PickingLabelPaper {
|
||||
widthMm: number;
|
||||
heightMm: number;
|
||||
}
|
||||
|
||||
const DEFAULT_PAPER: PickingLabelPaper = { widthMm: 50, heightMm: 30 };
|
||||
/** 默认纸型=实物签 80×60(GP-3120TUB,2026-10-14 开发者:「80×60 先设置为这个」) */
|
||||
const DEFAULT_PAPER: PickingLabelPaper = { widthMm: 80, heightMm: 60 };
|
||||
|
||||
/** 夹在合理区间内,非法值回落默认(防 0/负数/NaN 把版面弄崩) */
|
||||
function normalizePaper(paper?: Partial<PickingLabelPaper> | null): PickingLabelPaper {
|
||||
@@ -240,26 +243,48 @@ ${innerRules}
|
||||
</style></head><body>${pages}</body></html>`;
|
||||
}
|
||||
|
||||
/** 生成标签页(纸张尺寸 + 旋转可配);旋转 0° 时**一签一页**(块只包住内容 + 强制分页)。 */
|
||||
/** 生成标签页(纸张尺寸 + 旋转 + 客户/区域开关可配);旋转 0° 时**一签一页**(块只包住内容 + 强制分页)。 */
|
||||
export function buildPickingLabelHtml(
|
||||
labels: PickingLabel[],
|
||||
options?: {
|
||||
paper?: Partial<PickingLabelPaper> | null;
|
||||
/** 旋转角度:0(默认)| 90 | 180 | 270 —— 用于抵消打印机驱动自身的旋转 */
|
||||
rotation?: PickingLabelRotation | number | null;
|
||||
/** 是否打「客户:…」行(默认 true;false 时该行不输出、后面的行上移) */
|
||||
showCustomer?: boolean | null;
|
||||
/** 是否打「区域:…」行(默认 true,2026-10-14 开发者要求) */
|
||||
showZone?: boolean | null;
|
||||
}
|
||||
): string {
|
||||
const paper = normalizePaper(options?.paper);
|
||||
const rotation = normalizeRotation(options?.rotation);
|
||||
const showCustomer = options?.showCustomer !== false;
|
||||
const showZone = options?.showZone !== false;
|
||||
/** 版面行(自上而下):标题 / 客户? / 区域? / 品名 / 重量 / 日期 —— 关掉的行不占位,后面的行上移 */
|
||||
const lineSpecs: Array<{ cls: string; fontDots: number; bold?: boolean }> = [
|
||||
{ cls: 't-title', fontDots: 24, bold: true },
|
||||
];
|
||||
if (showCustomer) lineSpecs.push({ cls: 't-cust', fontDots: 20 });
|
||||
if (showZone) lineSpecs.push({ cls: 't-zone', fontDots: 20 });
|
||||
lineSpecs.push({ cls: 't-name', fontDots: 20 });
|
||||
lineSpecs.push({ cls: 't-weight', fontDots: 26, bold: true });
|
||||
lineSpecs.push({ cls: 't-date', fontDots: 18 });
|
||||
|
||||
const sections = (labels || []).map((label) => {
|
||||
const qr = qrSvg(label.barcode);
|
||||
const codeText = String(label.barcode || '').trim();
|
||||
const lines: string[] = ['<div class="t-title">分拣标签</div>'];
|
||||
if (showCustomer) {
|
||||
lines.push(`<div class="t-cust">${esc(`客户:${String(label.companyName || '').trim() || '客户'}`)}</div>`);
|
||||
}
|
||||
if (showZone) {
|
||||
lines.push(`<div class="t-zone">${esc(`区域:${String(label.zone || '').trim() || '—'}`)}</div>`);
|
||||
}
|
||||
lines.push(`<div class="t-name">${esc(`品名:${String(label.itemName || '').trim() || '商品'}`)}</div>`);
|
||||
lines.push(`<div class="t-weight">${esc(formatLabelWeightLine(label.qty, label.unit))}</div>`);
|
||||
lines.push(`<div class="t-date">${esc(`日期:${String(label.date || '').trim() || todayIso()}`)}</div>`);
|
||||
return `<section class="pick-label">
|
||||
<div class="t-title">分拣标签</div>
|
||||
<div class="t-cust">${esc(`客户:${String(label.companyName || '').trim() || '客户'}`)}</div>
|
||||
<div class="t-name">${esc(`品名:${String(label.itemName || '').trim() || '商品'}`)}</div>
|
||||
<div class="t-weight">${esc(formatLabelWeightLine(label.qty, label.unit))}</div>
|
||||
<div class="t-date">${esc(`日期:${String(label.date || '').trim() || todayIso()}`)}</div>
|
||||
${lines.join('\n')}
|
||||
${qr ? `<div class="t-qr">${qr}</div>` : ''}
|
||||
${codeText ? `<div class="t-code">${esc(codeText)}</div>` : ''}
|
||||
</section>`;
|
||||
@@ -289,23 +314,31 @@ ${codeText ? `<div class="t-code">${esc(codeText)}</div>` : ''}
|
||||
* 可用高度因此小于纸高;块高若等于纸高就溢出、被浏览器拆到下一页 ⇒ 实测「打两张耗三张、中间夹空白」。
|
||||
* 块只包住内容 ⇒ 无论对话框边距选「默认」还是「无」都不会溢出(前提:可用高 ≥ 块高)。
|
||||
* 一签一页改由**强制分页**保证(见 assembleLabelDocument 的 pageBreakPerLabel)。
|
||||
* 取「文字块 5 行」与「二维码 + 编号列」两者的下沿,再加下边距 insetY(行高按字号 ×1.2 估)。
|
||||
* 取「文字行下沿」与「二维码 + 编号列下沿」两者的较大值,再加下边距 insetY(行高按字号 ×1.2 估)。
|
||||
*/
|
||||
const textBottomMm = insetYMm + 4 * lineStepMm + 18 * DOT_MM * 1.2;
|
||||
const lineBottomMm = Math.max(
|
||||
...lineSpecs.map(
|
||||
(line, index) => insetYMm + index * lineStepMm + line.fontDots * DOT_MM * 1.2
|
||||
)
|
||||
);
|
||||
const qrBottomMm = insetYMm + qrSizeMm + 0.75 + 14 * DOT_MM * 1.2;
|
||||
const blockHeightMm = Math.min(
|
||||
paper.heightMm - 0.5,
|
||||
Math.max(textBottomMm, qrBottomMm) + insetYMm
|
||||
Math.max(lineBottomMm, qrBottomMm) + insetYMm
|
||||
);
|
||||
const lineTop = (index: number) => mmf(insetYMm + index * lineStepMm);
|
||||
|
||||
/** 标签内部的文字/二维码规则(两种模式共用;四周留比例安全边距) */
|
||||
/** 标签内部的文字/二维码规则(两种模式共用;四周留比例安全边距;行位置按**实际显示的行**排) */
|
||||
const lineRules = lineSpecs
|
||||
.map(
|
||||
(line, index) =>
|
||||
`.pick-label .${line.cls} { top: ${lineTop(index)}; font-size: ${mm(line.fontDots)};${
|
||||
line.bold ? ' font-weight: 700;' : ''
|
||||
} }`
|
||||
)
|
||||
.join('\n');
|
||||
const innerRules = `.pick-label > div { position: absolute; left: ${mmf(insetXMm)}; right: ${mmf(insetXMm + qrSizeMm + 1)}; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.pick-label .t-title { top: ${lineTop(0)}; font-size: ${mm(24)}; font-weight: 700; }
|
||||
.pick-label .t-cust { top: ${lineTop(1)}; font-size: ${mm(20)}; }
|
||||
.pick-label .t-name { top: ${lineTop(2)}; font-size: ${mm(20)}; }
|
||||
.pick-label .t-weight { top: ${lineTop(3)}; font-size: ${mm(26)}; font-weight: 700; }
|
||||
.pick-label .t-date { top: ${lineTop(4)}; font-size: ${mm(18)}; }
|
||||
${lineRules}
|
||||
.pick-label .t-qr { left: auto; right: ${mmf(insetXMm)}; top: ${mmf(insetYMm)}; width: ${mmf(qrSizeMm)}; height: ${mmf(qrSizeMm)}; }
|
||||
.pick-label .t-qr svg { width: 100%; height: 100%; }
|
||||
.pick-label .t-code { left: auto; right: ${mmf(insetXMm)}; top: ${mmf(insetYMm + qrSizeMm + 0.75)}; width: ${mmf(qrSizeMm)}; text-align: center; font-size: ${mm(14)}; }`;
|
||||
@@ -373,6 +406,10 @@ export async function printPickingLabels(
|
||||
options?: {
|
||||
paper?: Partial<PickingLabelPaper> | null;
|
||||
rotation?: PickingLabelRotation | number | null;
|
||||
/** 是否打「客户:…」行(默认 true) */
|
||||
showCustomer?: boolean | null;
|
||||
/** 是否打「区域:…」行(默认 true) */
|
||||
showZone?: boolean | null;
|
||||
}
|
||||
): Promise<void> {
|
||||
const list = (labels || []).filter(Boolean);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
/**
|
||||
* 新建分类去重(2026-10-14 开发者报障):
|
||||
* 「新建分类的接口 我们的后台是不是没有做这个分类是否存在的判断?假设这个分类已经存在了应该是不会再次创建了吧」
|
||||
*
|
||||
* 前端 `insertCatalogCategory`(录单/订单详情/分类管理/供应商品类/批量导入共用的唯一出口)
|
||||
* 此前**裸插入、无查重** ⇒ 同名不同 code 可重复创建。现加:
|
||||
* 同分公司 + 同父级 + `TRIM(name)` 精确相等 ⇒ 复用已有分类(与后端 service / 批量导入 `normalizeName` 同口径)。
|
||||
*
|
||||
* 设计:docs/design/20261014-分类新建去重(同名复用已有).md
|
||||
*/
|
||||
const src = readFileSync(path.join(__dirname, 'productCatalog.ts'), 'utf8');
|
||||
|
||||
describe('新建分类去重:同名同父复用已有', () => {
|
||||
it('① 有独立查重函数,按「同分公司 + 同父级 + TRIM(name)」查', () => {
|
||||
expect(src).toContain('async function findExistingCatalogCategoryCode(');
|
||||
expect(src).toContain(".eq('item_type', 'category')");
|
||||
expect(src).toContain(".eq('branch_code', branch)");
|
||||
// 一级:IS NULL;二级:同 parent_code
|
||||
expect(src).toContain(".is('parent_code', null)");
|
||||
expect(src).toContain(".eq('parent_code', parentCode)");
|
||||
// 精确相等(TRIM),不做 lower 归一
|
||||
expect(src).toContain("String(row.name ?? '').trim() === trimmedName");
|
||||
});
|
||||
|
||||
it('② 插入前先查重,命中直接返回已有 code(不插新行)', () => {
|
||||
const start = src.indexOf('export async function insertCatalogCategory(');
|
||||
expect(start).toBeGreaterThan(-1);
|
||||
const body = src.slice(start, start + 3200);
|
||||
expect(body).toContain('findExistingCatalogCategoryCode(');
|
||||
// 查重必须在真正 INSERT 之前
|
||||
expect(body.indexOf('findExistingCatalogCategoryCode(')).toBeLessThan(body.indexOf('.insert({'));
|
||||
expect(body).toContain('if (existingCode) return existingCode;');
|
||||
// 插入使用 trim 后的名字(与查重口径一致)
|
||||
expect(body).toContain('name: trimmedName,');
|
||||
});
|
||||
|
||||
it('③ 传了显式 code 不查重(保持「按该 code 建」语义)', () => {
|
||||
const start = src.indexOf('export async function insertCatalogCategory(');
|
||||
const body = src.slice(start, start + 3200);
|
||||
expect(body).toContain('if (!input.code) {');
|
||||
expect(body).toContain('ensureBranchCategoryCode(branch, input.code)');
|
||||
});
|
||||
|
||||
it('④ 查重读失败 fail-open(只记日志、继续新建,不引入回归)', () => {
|
||||
const start = src.indexOf('export async function insertCatalogCategory(');
|
||||
const body = src.slice(start, start + 3200);
|
||||
expect(body).toContain('catch (error) {');
|
||||
expect(body).toContain('新建分类查重失败(继续新建)');
|
||||
});
|
||||
});
|
||||
@@ -124,6 +124,34 @@ export async function generateCatalogCategoryCode(branchCode?: string | null): P
|
||||
return generateBranchCategoryCode(branch);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同名同父分类查重(2026-10-14):
|
||||
* 新建分类前按「同分公司 + 同父级 + `TRIM(name)` 精确相等」找已存在的分类,命中就复用它。
|
||||
* 口径与后端 `categoryCatalogService.createQuickCatalogCategory`、批量导入 `normalizeName`(= trim)一致;
|
||||
* ⚠️ 大小写不同视为不同分类(不做 `lower()` 归一)。
|
||||
* 设计:docs/design/20261014-分类新建去重(同名复用已有).md
|
||||
*/
|
||||
async function findExistingCatalogCategoryCode(
|
||||
branch: string,
|
||||
parentCode: string | null,
|
||||
trimmedName: string
|
||||
): Promise<string | null> {
|
||||
let query = supabase
|
||||
.from('product_catalog')
|
||||
.select('code, name')
|
||||
.eq('item_type', 'category')
|
||||
.eq('branch_code', branch)
|
||||
.order('created_at', { ascending: true })
|
||||
.order('code', { ascending: true });
|
||||
query = parentCode ? query.eq('parent_code', parentCode) : query.is('parent_code', null);
|
||||
const { data, error } = await query;
|
||||
if (error) throw error;
|
||||
const hit = ((data as Array<{ code: string; name: string | null }> | null) || []).find(
|
||||
(row) => String(row.name ?? '').trim() === trimmedName
|
||||
);
|
||||
return hit ? String(hit.code) : null;
|
||||
}
|
||||
|
||||
export async function insertCatalogCategory(input: {
|
||||
code?: string;
|
||||
name: string;
|
||||
@@ -145,13 +173,30 @@ export async function insertCatalogCategory(input: {
|
||||
if (!branch) {
|
||||
throw new Error('创建分类须选择经营分公司');
|
||||
}
|
||||
const trimmedName = input.name.trim();
|
||||
/**
|
||||
* 同名同父分类已存在 ⇒ 直接复用,不再新建(防重复,2026-10-14)。
|
||||
* 传了显式 `code` 时不查重(调用方要的是「按该 code 建」);查重是兜底,读失败不阻断新建(fail-open)。
|
||||
*/
|
||||
if (!input.code) {
|
||||
try {
|
||||
const existingCode = await findExistingCatalogCategoryCode(
|
||||
branch,
|
||||
input.parent_code || null,
|
||||
trimmedName
|
||||
);
|
||||
if (existingCode) return existingCode;
|
||||
} catch (error) {
|
||||
console.error('新建分类查重失败(继续新建):', error);
|
||||
}
|
||||
}
|
||||
const code = input.code
|
||||
? ensureBranchCategoryCode(branch, input.code)
|
||||
: await generateCatalogCategoryCode(branch);
|
||||
const { error } = await supabase.from('product_catalog').insert({
|
||||
item_type: 'category',
|
||||
code,
|
||||
name: input.name.trim(),
|
||||
name: trimmedName,
|
||||
branch_code: branch,
|
||||
parent_code: input.parent_code || null,
|
||||
level: input.level ?? (input.parent_code ? 2 : 1),
|
||||
|
||||
Reference in New Issue
Block a user