feat(分拣): 列表多选拣货单批量打印标签
表头全选 + 行内多选;选中后按「按订单/按分类/按商品/按区域」排序,一次生成多页标签并打印,免去逐单进详情逐个点。复用既有文案(无新 key);区域排序遵循分公司区域顺序。商品行 SELECT 补 category_name(纯读,无迁移)。同步更新 mpPickingScopeSelect 断言(2026-10-13 重新加回小程序 BLE 打印,仍不走旧 print_jobs 链路)。
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
# 分拣列表:多选拣货单批量打印标签(按订单/分类/商品/区域排队)
|
||||
|
||||
> 立项 2026-10-13。开发者口径(原话):
|
||||
> 「这块不能一个个打印拣货单旁边设置一个多选(存在一个全选按钮)多选后将商品整理一下
|
||||
> 它可以按照分类 区域 订单 商品 选择性的进行一个排排队打印 而不是一个个点击一个个打印」。
|
||||
>
|
||||
> 承接:[20261013-分拣标签打印改蓝牙与浏览器直连.md](./20261013-分拣标签打印改蓝牙与浏览器直连.md)(标签版式与通道已定)。
|
||||
|
||||
## 0. 一句话
|
||||
|
||||
分拣**列表页**加一列多选(表头**全选**);勾若干拣货单后,把它们的商品行**按所选维度
|
||||
(订单 / 分类 / 商品 / 区域)排序**,**一次**生成多页标签并打印,免去逐单进详情逐个点打印。
|
||||
|
||||
## 1. 背景
|
||||
|
||||
- 2026-10-13 已把分拣标签打印改成新通道(小程序 BLE / Web 浏览器打印),但入口只在**明细弹层逐行**(一行一次点击)。
|
||||
- 列表页(`/picking`)本来就一次拉回**全部列出的拣货单 + 它们的商品行**(`fetchTasks` →
|
||||
`setItemsByPickCode(grouped)`,接口 `GET /picking/tasks` 同时返回 `data` 与 `items`)⇒
|
||||
批量打印**无需新接口**。
|
||||
- 列表表格列由 `useReorderableTableColumns` 渲染,`listColumnCount` 为可见列数。
|
||||
|
||||
## 2. 方案要点
|
||||
|
||||
### 2.1 列表多选
|
||||
|
||||
- 表格**最左**新增一列复选框(不可重排,渲染在 `listOrderedColumns` 之前):
|
||||
- 表头:**全选** —— 勾选/取消**本页**全部行(`tasksPage.pageRows`)。
|
||||
- 行内:勾选该拣货单(选中键 = `pick_code`)。
|
||||
- 选中状态按 `pick_code` 记录,**跨页保留**(翻页后已选仍算)。
|
||||
- `colSpan` 需 +1(表头行、加载行、空态行)。
|
||||
|
||||
### 2.2 批量打印工具条
|
||||
|
||||
选中数 > 0 时,在表格上方显示一条工具条:
|
||||
|
||||
| 控件 | 说明 |
|
||||
|---|---|
|
||||
| 排序维度 `<select>` | 选项复用既有 i18n:`picking.mode.order`(按订单)/ `mode.category`(按分类)/ `mode.sku`(按商品)/ `mode.zone`(按区域) |
|
||||
| 打印按钮 | 文案复用既有 `picking.modal.printLabelCount`(`打印标签({count})`,count = **标签张数**) |
|
||||
|
||||
打印走的仍是既有 `printPickingLabels(labels)`(浏览器打印通道,一签一页)。
|
||||
**不新增文案 key**(§5 有清单)。
|
||||
|
||||
### 2.3 排队(排序)口径
|
||||
|
||||
`sortItemsForLabelPrint(items, groupBy, { zoneOrder })`(新增,带单测):
|
||||
|
||||
1. 主键=分组值:订单=`order_code`、分类=`category_name`、商品=`sku_code||item_name`、区域=`storage_zone`;
|
||||
2. 分组值相同的,再按 `sort_order`、最后按 `item_name`;
|
||||
3. 区域:若传入了分公司**区域顺序**(页面已有 `zoneOrder` 状态),按该顺序排,未列入的区域排最后(再按名称)。
|
||||
|
||||
> 只是**标签页的先后**,不改变标签内容与版式(沿用 §承接文档 的旧秤端版式)。
|
||||
|
||||
### 2.4 数据来源
|
||||
|
||||
- 标签字段与明细行单张打印**同口径**:客户=`task.company_name`、品名=`item.item_name`、
|
||||
数量=`picked_weight → picked_qty → required_qty`、单位=`item.unit`、日期=`task.delivery_date`、
|
||||
条码=`item.barcode || item.sku_code`。
|
||||
- 「分类」需要商品行带 `category_name` —— `order_items` 本就有该列,但
|
||||
`PICKING_ITEMS_ENRICHED_SQL` 未选 ⇒ **补选一列**(纯读、additive,`oi` 已在 JOIN 中)。
|
||||
|
||||
## 3. 不动的(冻结面)
|
||||
|
||||
- 标签内容/版式/通道**沿用**承接文档,不在此变(`picking-freeze` 亦未触动)。
|
||||
- 不新增接口、不改表、无迁移(只给既有 SELECT 补一列)。
|
||||
- 明细弹层里逐行/「打印标签」按钮**保持原样**(本页只是并列新增批量入口)。
|
||||
- 不做「一键完成 / 改状态」等写操作:批量打印**只读**(与单张打印一致,不写库不推进状态)。
|
||||
|
||||
## 4. 改动面
|
||||
|
||||
| 文件 | 新增/改 | 意图 |
|
||||
|---|---|---|
|
||||
| `server/src/services/pickingService.ts` | 改 | `PICKING_ITEMS_ENRICHED_SQL` 补选 `oi.category_name AS category_name` |
|
||||
| `src/utils/picking.ts` | 改 | `PickingItemRow` 加 `category_name` |
|
||||
| `src/utils/pickingLabelPrint.ts` | 改 | 新增 `PickingLabelGroupBy` + `sortItemsForLabelPrint` |
|
||||
| `src/utils/pickingLabelPrint.test.ts` | 改 | 排序单测 |
|
||||
| `src/pages/picking/PickingManagement.tsx` | 改 | 多选列 + 全选 + 工具条 + 批量打印 handler |
|
||||
|
||||
## 5. 复用的既有文案(不新增 key)
|
||||
|
||||
| key | 中文 |
|
||||
|---|---|
|
||||
| `picking.aria.selectAllPrint` | 全选打印行 |
|
||||
| `picking.aria.selectItem` | 选择 {name} |
|
||||
| `picking.mode.order` / `mode.category` / `mode.sku` / `mode.zone` | 按订单 / 按分类 / 按商品 / 按区域 |
|
||||
| `picking.modal.printLabel` / `printLabelCount` | 打印标签 / 打印标签({count}) |
|
||||
| `picking.toast.needPrintRows` / `printFail` | (既有校验/失败提示) |
|
||||
|
||||
## 6. 验收标准
|
||||
|
||||
- [ ] 列表表头出现全选复选框;勾选可全选/取消**本页**行;行内复选框可单行勾选。
|
||||
- [ ] 选中 > 0 时出现工具条:排序维度下拉(按订单/分类/商品/区域)+「打印标签(N)」,N = 选中拣货单的商品行总数。
|
||||
- [ ] 点打印:按所选维度排序后**一次**生成 N 页标签并弹浏览器打印对话框。
|
||||
- [ ] 排序维度切换后打印顺序随之改变(订单/分类/商品/区域各验一次;区域遵循分公司区域顺序)。
|
||||
- [ ] 翻页后已选仍保留并可打印。
|
||||
- [ ] 不影响未勾选时的列表/分页/筛选/分配;不影响明细弹层现有打印。
|
||||
- [ ] 前端 `npm run typecheck` ✅;`pickingLabelPrint` 单测 ✅;后端 `webPickingRemotePrint` ✅。
|
||||
- [ ] 只读:不写库、不推进订单状态、无迁移。
|
||||
|
||||
## 7. 风险与回滚
|
||||
|
||||
| 风险 | 处置 |
|
||||
|---|---|
|
||||
| 选择跨页保留造成「多打了」 | 打印按钮显示张数 N,用户可见;全选只作用于当前页 |
|
||||
| 区域排序与「区域顺序」不一致 | 传入 `zoneOrder` 按配置排,未配置则按名称 |
|
||||
| 列表行数 > 接口上限(100 单)时商品行缺失 | 仅当前页可选(≤20 行),落在已拉取范围内;缺失行不产生标签 |
|
||||
| 回滚 | 纯前端 + 一列 SELECT:还原 §4 文件即可;无迁移、无数据 |
|
||||
|
||||
## 8. 修订记录
|
||||
|
||||
| 版本 | 日期 | 变更 |
|
||||
|---|---|---|
|
||||
| v1 | 2026-10-13 | 列表多选(含全选)+ 按订单/分类/商品/区域排序后批量打印标签 |
|
||||
@@ -71,6 +71,7 @@ AI 提示词清理已停用条目(7 条归档不删、清单过滤 archived、
|
||||
订单详情打开后**丢失「主系统单」标记**(`openOrderDetail` 直连 `orders.select('*')` 重建 `selectedOrder`,而 `is_main_system_order` 是列表接口的 `EXISTS` 别名、**不是表列** ⇒ 秒级窗口后被覆盖为 undefined ⇒ 主系统单偶发出现「添加」、「缺货」消失);改为**以列表行为基底、真实列用最新值覆盖**)见 [20261013-订单详情打开丢主系统标记.md](./20261013-订单详情打开丢主系统标记.md)。
|
||||
小程序分拣**「按区域」看不到区域**(`taskPickerLabel` 只处理 `category`/`sku`、`zone` 落到兜底显示**订单号** ⇒ 一个订单拆出的多张区域拣货单在选择器里长得一样;`tab_title` 那处改动在 wxml 里未渲染)见 [20261013-小程序分拣区域标签缺失.md](./20261013-小程序分拣区域标签缺失.md)。
|
||||
分拣**标签打印换通道**(不再走 `print_jobs` → 安卓称重终端 USB:小程序改 **BLE 热敏直连**、Web 改**浏览器打印**打热敏/小票机;标签字段沿用旧秤端;不清旧链路、无迁移、无后端改动)见 [20261013-分拣标签打印改蓝牙与浏览器直连.md](./20261013-分拣标签打印改蓝牙与浏览器直连.md)。
|
||||
分拣**列表多选批量打印标签**(表头全选 + 行内多选;勾选多个拣货单后按**订单/分类/商品/区域**排序、一次生成多页标签;复用既有文案不新增 key;仅给商品行 SELECT 补一列 `category_name`,无迁移)见 [20261013-分拣列表批量打印标签.md](./20261013-分拣列表批量打印标签.md)。
|
||||
|
||||
## 全量对齐与改造方案(2026-09-28)
|
||||
|
||||
|
||||
@@ -2990,6 +2990,8 @@ const PICKING_ITEMS_ENRICHED_SQL = `
|
||||
SELECT pi.id, pi.pick_code, pi.order_code, pi.order_item_id, pi.item_name, pi.required_qty, pi.picked_qty,
|
||||
pi.picked_weight, pi.unit,
|
||||
pi.review_status, pi.diff_reason, pi.sort_order,
|
||||
-- 商品分类(分拣列表批量打印按「分类」排队用;纯读,oi 已在 JOIN 中)
|
||||
oi.category_name AS category_name,
|
||||
COALESCE(
|
||||
NULLIF(TRIM(pi.sku_code), ''),
|
||||
NULLIF(TRIM(oi.sku_code), ''),
|
||||
|
||||
@@ -29,13 +29,17 @@ describe('小程序分拣范围选择框', () => {
|
||||
expect(js).not.toContain('this.applyPickingDayView()');
|
||||
});
|
||||
|
||||
it('日期轴仍在;小程序不做打印(2026-10-12 起)', () => {
|
||||
it('日期轴仍在;小程序打印走 BLE 新通道(2026-10-13 起)', () => {
|
||||
expect(wxml).toContain('date-axis');
|
||||
expect(js).toContain('confirmAllPicks');
|
||||
// 小程序不做打印:行上打印按钮与下发打印的代码均已移除
|
||||
expect(wxml).not.toContain('printCurrentPick');
|
||||
expect(wxml).not.toContain('btn-print-row');
|
||||
expect(js).not.toContain('printCurrentPick');
|
||||
// 2026-10-13 开发者要求重新加回分拣「打印」:走 **BLE 热敏直连**(utils/pickingLabelPrint),
|
||||
// 仍**不走**旧 print_jobs / 称重终端链路(无 printPickLabels / 设备下发)。
|
||||
expect(wxml).toContain('printCurrentPick');
|
||||
expect(wxml).toContain('btn-print-row');
|
||||
expect(js).toContain('printCurrentPick');
|
||||
expect(js).toContain('pickingLabelPrint');
|
||||
expect(js).not.toContain('printPickLabels');
|
||||
expect(js).not.toContain('fetchPickingWeighDevices');
|
||||
expect(js).not.toContain('createPickingPrintJobs');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -60,7 +60,7 @@ import {
|
||||
type PickingTaskRow,
|
||||
} from '../../utils/picking';
|
||||
import { buildRoleLabelResolver } from '../../utils/roleLabel';
|
||||
import { printPickingLabels } from '../../utils/pickingLabelPrint';
|
||||
import { printPickingLabels, sortItemsForLabelPrint, type PickingLabelGroupBy } from '../../utils/pickingLabelPrint';
|
||||
import type { WeighMatchToleranceMode, WeighDeviceRow, PrintJobRow, PickingOutboundBatches } from '../../utils/workflowApi';
|
||||
import { useCategoryRows } from '../../hooks/useCategoryRows';
|
||||
import type { DateRangeValue } from '../../components/ui/DateRangeFilterButton';
|
||||
@@ -446,6 +446,10 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
const [savingRules, setSavingRules] = useState(false);
|
||||
const [applyingRules, setApplyingRules] = useState(false);
|
||||
const [printSelectedIds, setPrintSelectedIds] = useState<Record<string, boolean>>({});
|
||||
/** 列表页批量打印:按 pick_code 多选(跨页保留)+ 排队维度 */
|
||||
const [listPrintSelected, setListPrintSelected] = useState<Record<string, boolean>>({});
|
||||
const [listPrintGroupBy, setListPrintGroupBy] = useState<PickingLabelGroupBy>('order');
|
||||
const [listPrinting, setListPrinting] = useState(false);
|
||||
const [weighDevices, setWeighDevices] = useState<WeighDeviceRow[]>([]);
|
||||
const [printDeviceId, setPrintDeviceId] = useState('');
|
||||
const [printJobs, setPrintJobs] = useState<PrintJobRow[]>([]);
|
||||
@@ -1667,6 +1671,69 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
resetKey: `${searchQuery}|${pickerFilter}|${activeTab}|tasks`,
|
||||
});
|
||||
|
||||
const tasksByCode = useMemo(() => {
|
||||
const map: Record<string, PickingTaskRow> = {};
|
||||
for (const task of tasks) map[task.pick_code] = task;
|
||||
return map;
|
||||
}, [tasks]);
|
||||
|
||||
const selectedListPickCodes = useMemo(
|
||||
() => Object.keys(listPrintSelected).filter((code) => listPrintSelected[code]),
|
||||
[listPrintSelected]
|
||||
);
|
||||
|
||||
const batchPrintItems = useMemo(
|
||||
() => selectedListPickCodes.flatMap((code) => itemsByPickCode[code] || []),
|
||||
[selectedListPickCodes, itemsByPickCode]
|
||||
);
|
||||
|
||||
const pageAllPrintSelected =
|
||||
tasksPage.pageRows.length > 0 &&
|
||||
tasksPage.pageRows.every((task) => listPrintSelected[task.pick_code]);
|
||||
|
||||
const toggleListPrintRow = (pickCode: string, checked: boolean) => {
|
||||
setListPrintSelected((prev) => ({ ...prev, [pickCode]: checked }));
|
||||
};
|
||||
|
||||
const toggleListPrintPage = (checked: boolean) => {
|
||||
setListPrintSelected((prev) => {
|
||||
const next = { ...prev };
|
||||
for (const task of tasksPage.pageRows) next[task.pick_code] = checked;
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
/** 列表批量打印标签:按所选维度(订单/分类/商品/区域)排队后一次生成多页(浏览器打印通道) */
|
||||
const handleBatchPrintLabels = async () => {
|
||||
if (!batchPrintItems.length) {
|
||||
notify.error(t('picking.toast.needPrintRows'));
|
||||
return;
|
||||
}
|
||||
setListPrinting(true);
|
||||
try {
|
||||
const ordered = sortItemsForLabelPrint(batchPrintItems, listPrintGroupBy, { zoneOrder });
|
||||
await printPickingLabels(
|
||||
ordered.map((item) => {
|
||||
const task = tasksByCode[item.pick_code];
|
||||
const weight = Number(item.picked_weight) || 0;
|
||||
const picked = Number(item.picked_qty) || 0;
|
||||
return {
|
||||
companyName: task?.company_name || '',
|
||||
itemName: item.item_name || '',
|
||||
qty: weight > 0 ? weight : picked > 0 ? picked : Number(item.required_qty) || 0,
|
||||
unit: item.unit || '',
|
||||
date: task?.delivery_date ? formatPickingTaskCalendarDay(task.delivery_date) : '',
|
||||
barcode: item.barcode || item.sku_code || '',
|
||||
};
|
||||
})
|
||||
);
|
||||
} catch (error: unknown) {
|
||||
notify.error(error instanceof Error ? error.message : t('picking.toast.printFail'));
|
||||
} finally {
|
||||
setListPrinting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const stats = {
|
||||
pending: tasks.filter((t) => Number(t.pick_status) < 30).length,
|
||||
unassigned: tasks.filter(
|
||||
@@ -2310,9 +2377,40 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
{activeTab !== 'print-jobs' && (
|
||||
<div className="app-surface app-border rounded-xl border overflow-hidden">
|
||||
<>
|
||||
{selectedListPickCodes.length > 0 && (
|
||||
<div className="flex flex-wrap items-center gap-3 border-b app-border bg-gray-50 px-4 py-2">
|
||||
<select
|
||||
value={listPrintGroupBy}
|
||||
onChange={(e) => setListPrintGroupBy(e.target.value as PickingLabelGroupBy)}
|
||||
className="app-input app-focus-ring px-2 py-1.5 border rounded-md text-sm"
|
||||
>
|
||||
<option value="order">{t('picking.mode.order')}</option>
|
||||
<option value="category">{t('picking.mode.category')}</option>
|
||||
<option value="sku">{t('picking.mode.sku')}</option>
|
||||
<option value="zone">{t('picking.mode.zone')}</option>
|
||||
</select>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void handleBatchPrintLabels()}
|
||||
disabled={listPrinting || !batchPrintItems.length}
|
||||
className="inline-flex items-center gap-2 px-3 py-1.5 border border-gray-200 rounded-md text-sm hover:bg-gray-50 disabled:opacity-50"
|
||||
>
|
||||
<Printer className="w-4 h-4" />
|
||||
{t('picking.modal.printLabelCount', { count: batchPrintItems.length })}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<table className="min-w-full divide-y divide-gray-200">
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th className="px-4 py-3 w-10">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={pageAllPrintSelected}
|
||||
onChange={(e) => toggleListPrintPage(e.target.checked)}
|
||||
aria-label={t('picking.aria.selectAllPrint')}
|
||||
/>
|
||||
</th>
|
||||
{listOrderedColumns.map((col) => {
|
||||
const headerProps = getListHeaderProps(col.id);
|
||||
return (
|
||||
@@ -2335,9 +2433,9 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200">
|
||||
{loading ? (
|
||||
<tr><td colSpan={listColumnCount} className="py-10 text-center text-gray-500">{t('picking.emptyLoading')}</td></tr>
|
||||
<tr><td colSpan={listColumnCount + 1} className="py-10 text-center text-gray-500">{t('picking.emptyLoading')}</td></tr>
|
||||
) : filteredTasks.length === 0 ? (
|
||||
<tr><td colSpan={listColumnCount} className="py-10 text-center text-gray-500">
|
||||
<tr><td colSpan={listColumnCount + 1} className="py-10 text-center text-gray-500">
|
||||
{activeTab === 'history' ? t('picking.emptyHistory') : t('picking.emptyTasks')}
|
||||
</td></tr>
|
||||
) : (
|
||||
@@ -2361,6 +2459,14 @@ function assigneeLabel(user: PickingAssignee, roleLabel: (code: string) => strin
|
||||
: t('picking.scope.order');
|
||||
return (
|
||||
<tr key={task.pick_code} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-4">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={Boolean(listPrintSelected[task.pick_code])}
|
||||
onChange={(e) => toggleListPrintRow(task.pick_code, e.target.checked)}
|
||||
aria-label={t('picking.aria.selectItem', { name: task.pick_code })}
|
||||
/>
|
||||
</td>
|
||||
{listOrderedColumns.map((col) => {
|
||||
switch (col.id) {
|
||||
case 'pick_code':
|
||||
|
||||
@@ -29,6 +29,8 @@ export interface PickingItemRow {
|
||||
review_status: number;
|
||||
diff_reason?: string | null;
|
||||
barcode?: string | null;
|
||||
/** 商品分类(`order_items.category_name`),分拣列表批量打印按「分类」排队用 */
|
||||
category_name?: string | null;
|
||||
sort_order?: number | null;
|
||||
container_code?: string | null;
|
||||
staged_at?: string | null;
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { buildPickingLabelHtml, formatLabelWeightLine } from './pickingLabelPrint';
|
||||
import {
|
||||
buildPickingLabelHtml,
|
||||
formatLabelWeightLine,
|
||||
sortItemsForLabelPrint,
|
||||
} from './pickingLabelPrint';
|
||||
|
||||
describe('formatLabelWeightLine(与旧秤端逐字一致)', () => {
|
||||
it('重量单位统一换算为「斤」', () => {
|
||||
@@ -15,6 +19,46 @@ describe('formatLabelWeightLine(与旧秤端逐字一致)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('sortItemsForLabelPrint(批量打印排队)', () => {
|
||||
// 分组值用 ASCII,避免断言依赖中文拼音排序
|
||||
const items = [
|
||||
{ item_name: 'C-白菜', order_code: 'O2', category_name: 'CAT-B', storage_zone: 'Z-B', sku_code: 'SKU-3', sort_order: 2 },
|
||||
{ item_name: 'A-苹果', order_code: 'O1', category_name: 'CAT-A', storage_zone: 'Z-A', sku_code: 'SKU-1', sort_order: 1 },
|
||||
{ item_name: 'B-菠菜', order_code: 'O1', category_name: 'CAT-A', storage_zone: 'Z-A', sku_code: 'SKU-2', sort_order: 1 },
|
||||
];
|
||||
|
||||
it('按订单分组,组内按 sort_order 再按品名', () => {
|
||||
const out = sortItemsForLabelPrint(items, 'order').map((i) => i.item_name);
|
||||
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
|
||||
});
|
||||
|
||||
it('按分类分组', () => {
|
||||
const out = sortItemsForLabelPrint(items, 'category').map((i) => i.item_name);
|
||||
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
|
||||
});
|
||||
|
||||
it('按商品(SKU)分组', () => {
|
||||
const out = sortItemsForLabelPrint(items, 'sku').map((i) => i.item_name);
|
||||
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
|
||||
});
|
||||
|
||||
it('按区域分组,且尊重分公司区域顺序', () => {
|
||||
const ordered = sortItemsForLabelPrint(items, 'zone', { zoneOrder: ['Z-B', 'Z-A'] }).map((i) => i.storage_zone);
|
||||
expect(ordered).toEqual(['Z-B', 'Z-A', 'Z-A']);
|
||||
});
|
||||
|
||||
it('未配置区域顺序时按名称排', () => {
|
||||
const fallback = sortItemsForLabelPrint(items, 'zone').map((i) => i.storage_zone);
|
||||
expect(fallback).toEqual(['Z-A', 'Z-A', 'Z-B']);
|
||||
});
|
||||
|
||||
it('不改动原数组', () => {
|
||||
const before = items.map((i) => i.item_name);
|
||||
sortItemsForLabelPrint(items, 'sku');
|
||||
expect(items.map((i) => i.item_name)).toEqual(before);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildPickingLabelHtml(分拣标签 · 浏览器打印)', () => {
|
||||
it('50×30mm 纸宽,一签一分区', () => {
|
||||
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 10, unit: '斤' }]);
|
||||
|
||||
@@ -20,6 +20,58 @@ export interface PickingLabel {
|
||||
barcode?: string | null;
|
||||
}
|
||||
|
||||
/** 批量打印时的排队维度 */
|
||||
export type PickingLabelGroupBy = 'order' | 'category' | 'sku' | 'zone';
|
||||
|
||||
/** 可参与排队的商品行最小字段集 */
|
||||
export interface LabelSortableItem {
|
||||
order_code?: string | null;
|
||||
category_name?: string | null;
|
||||
storage_zone?: string | null;
|
||||
item_name?: string | null;
|
||||
sku_code?: string | null;
|
||||
sort_order?: number | null;
|
||||
}
|
||||
|
||||
function groupValueOf(item: LabelSortableItem, groupBy: PickingLabelGroupBy): string {
|
||||
if (groupBy === 'order') return String(item.order_code || '').trim();
|
||||
if (groupBy === 'category') return String(item.category_name || '').trim();
|
||||
if (groupBy === 'zone') return String(item.storage_zone || '').trim();
|
||||
return String(item.sku_code || item.item_name || '').trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量打印标签前的排队:主键=所选维度,同键再按 sort_order、最后按品名。
|
||||
* 区域可传入分公司区域顺序(`zoneOrder`):已列入的按顺序,未列入的排最后(再按名称)。
|
||||
*/
|
||||
export function sortItemsForLabelPrint<T extends LabelSortableItem>(
|
||||
items: T[],
|
||||
groupBy: PickingLabelGroupBy,
|
||||
options?: { zoneOrder?: string[] }
|
||||
): T[] {
|
||||
const zoneRank =
|
||||
groupBy === 'zone' && Array.isArray(options?.zoneOrder)
|
||||
? new Map(options!.zoneOrder.map((z, i) => [String(z).trim(), i]))
|
||||
: null;
|
||||
const compareGroup = (a: string, b: string) => {
|
||||
if (zoneRank) {
|
||||
const ra = zoneRank.has(a) ? zoneRank.get(a)! : Number.MAX_SAFE_INTEGER;
|
||||
const rb = zoneRank.has(b) ? zoneRank.get(b)! : Number.MAX_SAFE_INTEGER;
|
||||
if (ra !== rb) return ra - rb;
|
||||
}
|
||||
return a.localeCompare(b, 'zh-CN');
|
||||
};
|
||||
return [...items].sort((a, b) => {
|
||||
const ga = groupValueOf(a, groupBy);
|
||||
const gb = groupValueOf(b, groupBy);
|
||||
if (ga !== gb) return compareGroup(ga, gb);
|
||||
const sa = Number(a.sort_order) || 0;
|
||||
const sb = Number(b.sort_order) || 0;
|
||||
if (sa !== sb) return sa - sb;
|
||||
return String(a.item_name || '').localeCompare(String(b.item_name || ''), 'zh-CN');
|
||||
});
|
||||
}
|
||||
|
||||
/** 与秤端 203dpi(8 dot/mm)一致 */
|
||||
const DOT_MM = 1 / 8;
|
||||
const LABEL_W_DOTS = 400; // 50mm
|
||||
|
||||
Reference in New Issue
Block a user