库存列表支持内联编辑与报告管理
- 区域/单位/分类下拉、规格文本、可用数量与成本价均可列表内直接改 - 可用数量按本批次 inventory_id;规格/单位/分类另需 product_manage - 数字格不用 type=number,滚轮与方向键不再改值 - 点「存在报告」可按批次上传/删除报告(复用既有绑定接口) - 工具栏「上传报告」改为「批量上传报告」
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
# 库存管理列表:区域 / 规格 / 单位 / 可用数量 / 成本价 列表内直接改
|
||||
|
||||
- 日期:2026-10-23
|
||||
- 需求:开发者(附截图)「仓库管理这块 我需要在外面就能修改区域 规格 单位 可用数量(需要是这批的可用数量) 成本价 这块参考我们的客户列表的的效果」
|
||||
- 口径:**只是在列表里多一个入口,写路径/权限/留痕一律复用现有的**「修改」弹窗那套接口,不新增接口、不改库存语义。
|
||||
|
||||
## 1. 改前事实
|
||||
|
||||
| # | 事实 |
|
||||
|---|---|
|
||||
| F1 | `src/pages/inventory/InventoryManagement.tsx` 列表里「区域编号 / 规格 / 单位 / 可用数量 / 成本价」都是**只读文本**,要改必须点行尾「修改」打开弹窗(`openAdjustModal`)。 |
|
||||
| F2 | 弹窗提交分两条既有通道:可用数量 → `POST /inventory/adjust`(`adjustInventoryAvailableApi`,按 **批次 `inventory_id`**);批次属性/成本/商品档案 → `POST /inventory/batch-update`(`updateInventoryBatchApi`)。 |
|
||||
| F3 | 客户列表(`src/pages/customers/CustomerManagement.tsx`)的交互是「单元格本身就是输入框,失焦/回车提交、失败回退并 toast」——`CustomerInlineTextCell`。 |
|
||||
| F4 | 服务端权限:`batch-update` 需 `inventory_adjust`,且当请求体含 `category_code/spec/unit/origin` 时**另需 `product_manage`**;`/inventory/adjust` 需 `inventory_adjust`。备注为空时服务端补中性标签(`库存批次修改` / `库存调整`),**留痕不留空**。 |
|
||||
|
||||
## 2. 交互(对齐客户列表)
|
||||
|
||||
- 有权限的账号:这 5 列直接渲染输入框(`InventoryInlineCell`),**失焦或回车提交**;值没变不提交;提交失败 `notify.error` 并把值回退到服务端已知值。
|
||||
- 无权限的账号:仍渲染只读文本(与改前一致),不能通过内联入口越权。
|
||||
- 提交成功后 **重新拉列表**(`fetchInventory`)——因为「总价值 / 可调配 / 预留占用 / 统计 KPI」是由可用量与成本价派生的,本地 patch 会不一致。
|
||||
|
||||
## 3. 字段 ↔ 接口 ↔ 权限
|
||||
|
||||
| 列 | 组件形态 | 接口 | 门槛 |
|
||||
|---|---|---|---|
|
||||
| 区域编号 `storage_zone` | 文本 | `updateInventoryBatchApi` | `inventory_adjust` |
|
||||
| 规格 `spec` | 文本 | 同上(写商品档案,影响该 SKU 所有批次) | `inventory_adjust` **+ `product_manage`** |
|
||||
| 单位 `unit` | 文本 | 同上 | 同上 |
|
||||
| 成本价 `cost_price` | 数字(可空=清空) | 同上(成本改动仍走服务端既有重算) | `inventory_adjust` |
|
||||
| 可用数量 `available_qty` | 数字(≥ 0) | `adjustInventoryAvailableApi`(**按本批次 `inventory_id`**) | `inventory_adjust` |
|
||||
|
||||
## 4. 校验与留痕
|
||||
|
||||
- 可用数量:空 / 非数字 / < 0 ⇒ 抛 `inventory.toast.qtyInvalid`(**与服务端同一句**),不提交。
|
||||
- 成本价:空 ⇒ `null`(清空);< 0 ⇒ 拒绝。
|
||||
- 备注:内联提交传空串,由服务端补 `库存调整` / `库存批次修改` —— 与弹窗同一口径,**审计与 `inventory_logs` 双写不变**。
|
||||
- 成本价联动:仍由服务端 `inventoryBatchCostService` 重算未结算订单,前端只负责刷新列表。
|
||||
|
||||
## 5. 非目标
|
||||
|
||||
- 不动「修改」弹窗(保留,字段更全:生产日期/保质期/产地/分类等)。
|
||||
- 不做行内编辑 生产日期 / 保质期 / 产地 / 分类(弹窗里改)。
|
||||
- 不改 FIFO、预留、出库扣减、成本口径——一行未动。
|
||||
|
||||
## 6. 验收
|
||||
|
||||
1. 有 `inventory_adjust` 的账号在列表里可直接改区域/成本价/可用数量,改完列表数字与总价值同步刷新;
|
||||
2. 有 `product_manage` 才能改规格/单位,否则这两列仍是只读;
|
||||
3. 审计里能查到每次内联改动(`inventory_batch_edit` / `inventory_adjust`),备注是服务端补的中性标签;
|
||||
4. 契约测试:`src/pages/inventory/inventoryInlineEdit.contract.test.ts`(见下方证据)。
|
||||
|
||||
## 7. 证据(已回填)
|
||||
|
||||
- 契约测试 **6 条全绿**(`src/pages/inventory/inventoryInlineEdit.contract.test.ts`):
|
||||
五列接线、可用数量按批次 `inventory_id`、`product_manage` 门槛、失败回退与「值未变不提交」、静默刷新、
|
||||
备注留空(`remark: ''`)走服务端补中性标签。
|
||||
- 注入对拍两处:把 `saveInlineAvailableQty` 里的 `inventoryId: item.id` 换成 `item.sku_code` ⇒ **立即变红**;
|
||||
把 `canInlineEditArchiveFields` 的 `hasPermission('product_manage')` 去掉 ⇒ **立即变红**(均已还原)。
|
||||
- `npx tsc --noEmit` 通过(`onClick={fetchInventory}` 同步改成 `onClick={() => void fetchInventory()}`,因为该函数新增了 `{ silent }` 参数)。
|
||||
|
||||
## 8. 追加(同日):区域/单位改下拉、数字格去掉 number 步进、按钮改「批量上传报告」
|
||||
|
||||
开发者(附两张截图):「上传报告改为批量上传报告 然就我记得区域这个不是选择框吗 单位也是」「成本价这个不能存在滚轮滚动变动价格」。
|
||||
|
||||
- **按钮文案**:工具栏那颗按钮改用**新键** `inventory.batchReport.bulkButton` = 「批量上传报告」(en `Upload reports in bulk` / id `Unggah laporan massal`);
|
||||
`inventory.batchReport.button` 保持「上传报告」——它还被**修改弹窗里本批次补充报告**的小链接复用,不能一起改成批量。
|
||||
- **区域 / 单位改为下拉选择**(新增 `InventoryInlineSelectCell`):区域选项取页面已加载的 `zoneOptions`(后端 `zones`),
|
||||
单位选项取 `unitChoices`(`listUnitsApi({ activeOnly: true })`);**当前值不在列表里时自动补进选项**,否则下拉会显示成别的值;
|
||||
空选项复用既有文案 `adjust.zoneEmpty` / `adjust.unitEmpty`(选空 = 清空该字段,服务端写 NULL)。
|
||||
单位**不支持就地新建**(新建单位仍在「修改」弹窗里做)。
|
||||
- **数字格去掉 `type="number"`**(成本价 / 可用数量):改为 `type="text" inputMode="decimal"`,不再渲染上下箭头,
|
||||
滚轮/方向键也不会步进改值;校验仍在提交时做(可用量 ≥ 0、成本可空 = 清空、负数由服务端按既有口径拒绝)。
|
||||
|
||||
证据:契约测试 **9 条**(新增 3 条:区域/单位走 `InventoryInlineSelectCell` 且当前值不在列表里要补、
|
||||
数字格不得出现 `'number'` / `step=`、中文按钮文案);注入对拍:把 `type="text"` 改回 `number` ⇒ **立即变红**;
|
||||
把区域/单位换回文本框 ⇒ **立即变红**(均已还原)。
|
||||
|
||||
## 9. 追加(同日):区域/单位下拉改用 `SearchableSelect`(限制展开高度)
|
||||
|
||||
开发者(附截图,单位下拉一路铺到底):「选择的高度限制一下区域选择与单位选择 高度限制一下」。
|
||||
|
||||
原生 `<select>` 的展开列表由浏览器绘制,**CSS 无法限制其高度**。故改为项目统一的 `SearchableSelect`
|
||||
(「修改」弹窗里的区域/单位本来就是它):列表高度由组件钳在 **240px**(`DEFAULT_MENU_MAX_HEIGHT`)并可滚动、
|
||||
超出部分靠搜索;`portal` 把列表挂到 body 避免被表格/滚动容器裁剪,`maxVisibleOptions={30}` 控制一次渲染条数;
|
||||
`compact` 与行内输入框同高。
|
||||
|
||||
证据:契约测试 9 条(该条新增「必须用 `SearchableSelect` 且带 `maxVisibleOptions`」);
|
||||
注入对拍:把单元格里的 `<SearchableSelect` 换回 `<select` ⇒ **立即变红**(已还原);`npx tsc --noEmit` 通过。
|
||||
|
||||
## 10. 追加(同日):分类也可就地切换;报告在列表外就能上传/删除
|
||||
|
||||
开发者(附截图):「分类也可以点击进行切换 还有报告在外面也能删除 上传」。
|
||||
|
||||
- **分类**:商品名下面那行分类改为「可就地点选的标签」(`InventoryInlineSelectCell` + `categoryChoices`,空 = 未分类)。
|
||||
分类是**商品档案级**(影响该 SKU 所有批次),服务端 `batch-update` 对 `category_code` 另需 `product_manage`,
|
||||
所以同样走 `canInlineEditArchiveFields` 门槛;改完静默刷新,商品名下分类名与筛选同步。
|
||||
- **报告**:点「存在报告」格 → 弹出「批次报告」小面板:列出该批次已绑定报告(类型 / 编号 / 上传时间)并可**逐条删除**;
|
||||
面板里的「上传报告」复用 `BatchReportBindModal`(`inventory_id` 直绑,与「修改」弹窗同一条
|
||||
`POST /inventory/batch-reports/bind` 通道);删除走 `DELETE /inventory/batch-reports/:id`。
|
||||
列表「存在报告」数量在保存后**静默刷新**。
|
||||
- **零新增文案**:标题 `adjust.reportsLabel`(批次报告)、上传 `batchReport.button`(上传报告)、
|
||||
删除 `common.delete`、空态 `adjust.reportsEmpty`、关闭 `common.close`。
|
||||
- 权限沿用 `inventory_adjust`(与「修改」弹窗里报告区的可见口径一致)。
|
||||
|
||||
证据:契约测试 **11 条**(新增「分类就地切换」与「报告列表外上传/删除」两条);
|
||||
注入对拍:把分类选项换成 `unitChoices` ⇒ **立即变红**;把报告格入口换成 `openAdjustModal` ⇒ **立即变红**(均已还原)。
|
||||
File diff suppressed because one or more lines are too long
@@ -397,6 +397,7 @@ export const inventory: MessageDict = {
|
||||
'batchReport.presentCount': '{n} report(s)',
|
||||
'batchReport.inboundAt': 'Inbound',
|
||||
'batchReport.button': 'Upload report',
|
||||
'batchReport.bulkButton': 'Upload reports in bulk',
|
||||
'batchReport.title': 'Upload report',
|
||||
'batchReport.hint':
|
||||
'Upload a batch report (image or PDF). Product names and production dates are recognized and matched against stock-in batches; confirmed links are archived in this system only.',
|
||||
|
||||
@@ -404,6 +404,7 @@ export const inventory: MessageDict = {
|
||||
'batchReport.presentCount': '{n} laporan',
|
||||
'batchReport.inboundAt': 'Masuk',
|
||||
'batchReport.button': 'Unggah laporan',
|
||||
'batchReport.bulkButton': 'Unggah laporan massal',
|
||||
'batchReport.title': 'Unggah laporan',
|
||||
'batchReport.hint':
|
||||
'Unggah laporan batch (gambar atau PDF). Nama produk dan tanggal produksi dikenali lalu dicocokkan dengan batch stok masuk; tautan terkonfirmasi hanya diarsipkan di sistem ini.',
|
||||
|
||||
@@ -396,6 +396,7 @@ export const inventory: MessageDict = {
|
||||
'batchReport.presentCount': '存在 {n} 个',
|
||||
'batchReport.inboundAt': '入库',
|
||||
'batchReport.button': '上传报告',
|
||||
'batchReport.bulkButton': '批量上传报告',
|
||||
'batchReport.title': '上传报告',
|
||||
'batchReport.hint':
|
||||
'上传批次报告(图片或 PDF),自动识别商品名与生产日期并匹配入库批次;确认后仅在本系统留档。',
|
||||
|
||||
@@ -192,6 +192,136 @@ interface InventoryLog {
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
const INVENTORY_LIST_INPUT_CLASS =
|
||||
'w-full min-w-0 px-1.5 py-1 text-sm border border-slate-200 rounded-md bg-white dark:bg-slate-800 dark:border-slate-600 dark:text-white';
|
||||
|
||||
/**
|
||||
* 库存列表内联编辑单元格(2026-10-23 开发者:「在外面就能修改区域 规格 单位 可用数量 成本价」,
|
||||
* 交互对齐客户列表 `CustomerInlineTextCell`):失焦/回车提交、值没变不提交、失败 toast 并回退。
|
||||
* 只做入口;写路径仍是 `updateInventoryBatchApi` / `adjustInventoryAvailableApi`,无权限时调用方渲染只读文本。
|
||||
*/
|
||||
function InventoryInlineCell({
|
||||
value,
|
||||
ariaLabel,
|
||||
placeholder,
|
||||
inputKind = 'text',
|
||||
align,
|
||||
onCommit,
|
||||
}: {
|
||||
value: string;
|
||||
ariaLabel: string;
|
||||
placeholder?: string;
|
||||
inputKind?: 'text' | 'decimal';
|
||||
align?: 'left' | 'right';
|
||||
onCommit: (next: string) => Promise<void>;
|
||||
}) {
|
||||
const t = useT();
|
||||
const [text, setText] = useState(value);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setText(value);
|
||||
}, [value]);
|
||||
|
||||
const commit = async () => {
|
||||
if (saving) return;
|
||||
const next = text.trim();
|
||||
if (next === String(value).trim()) {
|
||||
setText(value);
|
||||
return;
|
||||
}
|
||||
setSaving(true);
|
||||
try {
|
||||
await onCommit(next);
|
||||
} catch (err: unknown) {
|
||||
notify.error(err instanceof Error ? err.message : t('inventory.toast.saveFail'));
|
||||
setText(value);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<input
|
||||
// 不用 type="number":浏览器会渲染上下箭头,滚轮/方向键直接改动数值(成本价最容易被误改)
|
||||
type="text"
|
||||
inputMode={inputKind === 'decimal' ? 'decimal' : undefined}
|
||||
value={text}
|
||||
aria-label={ariaLabel}
|
||||
placeholder={placeholder}
|
||||
disabled={saving}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
onBlur={() => void commit()}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') e.currentTarget.blur();
|
||||
}}
|
||||
className={`${INVENTORY_LIST_INPUT_CLASS}${align === 'right' ? ' text-right' : ''}`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表内下拉选择单元格(区域 / 单位):
|
||||
* 选项来自页面已加载的 `zoneOptions` / `unitChoices`,当前值不在列表里时补进去(否则会显示成别的值)。
|
||||
*/
|
||||
function InventoryInlineSelectCell({
|
||||
value,
|
||||
options,
|
||||
emptyLabel,
|
||||
onCommit,
|
||||
}: {
|
||||
value: string;
|
||||
options: Array<{ value: string; label: string }>;
|
||||
emptyLabel?: string;
|
||||
onCommit: (next: string) => Promise<void>;
|
||||
}) {
|
||||
const t = useT();
|
||||
const [saving, setSaving] = useState(false);
|
||||
const merged = useMemo(() => {
|
||||
const list = options.filter((opt) => opt.value !== '');
|
||||
if (value && !list.some((opt) => opt.value === value)) {
|
||||
return [{ value, label: value }, ...list];
|
||||
}
|
||||
return list;
|
||||
}, [options, value]);
|
||||
|
||||
/**
|
||||
* 用项目统一的 `SearchableSelect` 而不是原生 `<select>`:
|
||||
* 原生下拉的展开高度由浏览器决定、CSS 管不了(2026-10-23 开发者:「区域选择与单位选择 高度限制一下」),
|
||||
* 这里由组件把列表高度钳在 240px 内并可滚动,超出部分搜索。
|
||||
*/
|
||||
return (
|
||||
<SearchableSelect
|
||||
value={value}
|
||||
options={merged}
|
||||
placeholder={emptyLabel || ''}
|
||||
allowEmpty={Boolean(emptyLabel)}
|
||||
emptyLabel={emptyLabel}
|
||||
disabled={saving}
|
||||
compact
|
||||
hideSearchIcon
|
||||
portal
|
||||
maxVisibleOptions={30}
|
||||
className="w-full min-w-[4.5rem]"
|
||||
inputClassName={INVENTORY_LIST_INPUT_CLASS}
|
||||
onChange={(next) => {
|
||||
if (saving || next === value) return;
|
||||
setSaving(true);
|
||||
void (async () => {
|
||||
try {
|
||||
await onCommit(next);
|
||||
} catch (err: unknown) {
|
||||
notify.error(err instanceof Error ? err.message : t('inventory.toast.saveFail'));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
})();
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export const InventoryManagement: React.FC = () => {
|
||||
const t = useT();
|
||||
const { operatingBranchCode } = useOperatingBranch();
|
||||
@@ -199,6 +329,8 @@ export const InventoryManagement: React.FC = () => {
|
||||
const { search } = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const canAdjustInventory = hasPermission('inventory_adjust');
|
||||
/** 规格/单位写商品档案,服务端另需 product_manage —— 没权限这两列保持只读 */
|
||||
const canInlineEditArchiveFields = canAdjustInventory && hasPermission('product_manage');
|
||||
const [inventory, setInventory] = useState<InventoryItem[]>([]);
|
||||
const [inventoryLogs, setInventoryLogs] = useState<InventoryLog[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
@@ -597,14 +729,127 @@ export const InventoryManagement: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 列表内直接改(2026-10-23 开发者):写路径完全复用「修改」弹窗的两条既有接口,
|
||||
* 备注留空由服务端补中性标签(库存调整 / 库存批次修改),审计与流水口径一行未动。
|
||||
*/
|
||||
const patchInventoryRow = (id: string, patch: Partial<InventoryItem>) => {
|
||||
setInventory((rows) => rows.map((row) => (row.id === id ? { ...row, ...patch } : row)));
|
||||
};
|
||||
|
||||
const saveInlineBatchFields = async (
|
||||
item: InventoryItem,
|
||||
patch: {
|
||||
storage_zone?: string;
|
||||
spec?: string;
|
||||
unit?: string;
|
||||
cost_price?: number | null;
|
||||
category_code?: string;
|
||||
}
|
||||
) => {
|
||||
await updateInventoryBatchApi({
|
||||
inventoryId: item.id,
|
||||
remark: '',
|
||||
operatingBranchCode: operatingBranchCode || readOperatingBranchCode(),
|
||||
...patch,
|
||||
});
|
||||
const localPatch: Partial<InventoryItem> = {};
|
||||
if (patch.storage_zone !== undefined) localPatch.storage_zone = patch.storage_zone;
|
||||
if (patch.spec !== undefined) localPatch.spec = patch.spec;
|
||||
if (patch.unit !== undefined) localPatch.unit = patch.unit;
|
||||
if (patch.cost_price !== undefined) localPatch.cost_price = patch.cost_price ?? undefined;
|
||||
if (patch.category_code !== undefined) {
|
||||
localPatch.category_code = patch.category_code;
|
||||
localPatch.category_name =
|
||||
categoryChoices.find((c) => c.value === patch.category_code)?.label || undefined;
|
||||
}
|
||||
patchInventoryRow(item.id, localPatch);
|
||||
await fetchInventory({ silent: true });
|
||||
};
|
||||
|
||||
/** 可用数量:按**本批次** `inventory_id` 调整(不是 SKU 级),校验与服务端同句 */
|
||||
const saveInlineAvailableQty = async (item: InventoryItem, raw: string) => {
|
||||
const text = String(raw).trim();
|
||||
const qty = text === '' ? Number.NaN : Number(text);
|
||||
if (!Number.isFinite(qty) || qty < 0) throw new Error(t('inventory.toast.qtyInvalid'));
|
||||
const adjusted = await adjustInventoryAvailableApi({
|
||||
inventoryId: item.id,
|
||||
availableQty: qty,
|
||||
remark: '',
|
||||
operatingBranchCode: operatingBranchCode || readOperatingBranchCode(),
|
||||
});
|
||||
patchInventoryRow(item.id, { available_qty: qty });
|
||||
await fetchInventory({ silent: true });
|
||||
const msg = adjusted?.replenish_message;
|
||||
notify.success(
|
||||
msg ? t('inventory.toast.adjustedWithMsg', { msg }) : t('inventory.toast.adjusted')
|
||||
);
|
||||
};
|
||||
|
||||
/** 成本价:空 = 清空(null);负数交给服务端按既有口径拒绝 */
|
||||
const saveInlineCostPrice = async (item: InventoryItem, raw: string) => {
|
||||
const text = String(raw).trim();
|
||||
const next = text === '' ? null : Number(text);
|
||||
if (next != null && !Number.isFinite(next)) throw new Error(t('inventory.toast.saveFail'));
|
||||
await saveInlineBatchFields(item, { cost_price: next });
|
||||
};
|
||||
|
||||
/**
|
||||
* 列表内联「批次报告」(2026-10-23 开发者:「报告在外面也能删除 上传」):
|
||||
* 点「存在报告」格 → 列出该批次已绑定报告(可删)+「上传报告」→ 复用 `BatchReportBindModal`。
|
||||
* 写路径与弹窗里完全一致(`/inventory/batch-reports/bind`、`DELETE /inventory/batch-reports/:id`)。
|
||||
*/
|
||||
const [inlineReportItem, setInlineReportItem] = useState<InventoryItem | null>(null);
|
||||
const [inlineReports, setInlineReports] = useState<
|
||||
Array<{ id: string; report_type: string; report_no: string | null; uploaded_at: string | null }>
|
||||
>([]);
|
||||
const [inlineReportsLoading, setInlineReportsLoading] = useState(false);
|
||||
const [inlineReportBindOpen, setInlineReportBindOpen] = useState(false);
|
||||
|
||||
const loadInlineReports = async (inventoryId: string) => {
|
||||
setInlineReportsLoading(true);
|
||||
try {
|
||||
const rows = await fetchInventoryBatchReportsByInventoryApi(inventoryId);
|
||||
setInlineReports(Array.isArray(rows) ? rows : []);
|
||||
} catch {
|
||||
setInlineReports([]);
|
||||
} finally {
|
||||
setInlineReportsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const openInlineReports = (item: InventoryItem) => {
|
||||
setInlineReportItem(item);
|
||||
setInlineReports([]);
|
||||
void loadInlineReports(item.id);
|
||||
};
|
||||
|
||||
const closeInlineReports = () => {
|
||||
setInlineReportBindOpen(false);
|
||||
setInlineReportItem(null);
|
||||
};
|
||||
|
||||
const deleteInlineReport = async (reportId: string) => {
|
||||
try {
|
||||
await deleteInventoryBatchReportApi(reportId);
|
||||
setInlineReports((prev) => prev.filter((row) => row.id !== reportId));
|
||||
await fetchInventory({ silent: true });
|
||||
} catch (error: unknown) {
|
||||
notify.error(
|
||||
error instanceof Error ? error.message : t('inventory.batchReport.delFail')
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
setCurrentPage(1);
|
||||
}, [operatingBranchCode, searchQuery, onlyAvailable, withoutCost, expiryFilter]);
|
||||
|
||||
const fetchInventory = async () => {
|
||||
const fetchInventory = async (options: { silent?: boolean } = {}) => {
|
||||
const { isStale } = beginInventoryFetch();
|
||||
try {
|
||||
setLoading(true);
|
||||
// 内联编辑后的静默刷新:不切 loading,否则整表被 spinner 替换、正在编辑的输入框会被卸载
|
||||
if (!options.silent) setLoading(true);
|
||||
const result = await listInventoryManagementApi({
|
||||
page: currentPage,
|
||||
page_size: pageSize,
|
||||
@@ -634,7 +879,7 @@ export const InventoryManagement: React.FC = () => {
|
||||
if (isStale()) return;
|
||||
console.error('获取库存失败:', error);
|
||||
} finally {
|
||||
if (!isStale()) setLoading(false);
|
||||
if (!options.silent && !isStale()) setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1018,12 +1263,12 @@ export const InventoryManagement: React.FC = () => {
|
||||
className="app-button-secondary app-border flex items-center gap-2 px-3 py-1.5 border rounded-lg text-sm"
|
||||
>
|
||||
<FileText className="w-4 h-4 text-gray-600" />
|
||||
{t('inventory.batchReport.button')}
|
||||
{t('inventory.batchReport.bulkButton')}
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={fetchInventory}
|
||||
onClick={() => void fetchInventory()}
|
||||
className="app-button-secondary app-border flex items-center gap-2 px-3 py-1.5 border rounded-lg text-sm"
|
||||
>
|
||||
<RefreshCw className="w-4 h-4 text-gray-600" />
|
||||
@@ -1260,7 +1505,21 @@ export const InventoryManagement: React.FC = () => {
|
||||
>
|
||||
{item.sku_name}
|
||||
</p>
|
||||
<p className="text-xs text-gray-400">{item.category_name}</p>
|
||||
{canInlineEditArchiveFields ? (
|
||||
<div className="mt-1 w-[9.5rem]">
|
||||
{/* 分类可就地切换(商品档案级,影响该 SKU 所有批次) */}
|
||||
<InventoryInlineSelectCell
|
||||
value={item.category_code || ''}
|
||||
options={categoryChoices}
|
||||
emptyLabel={t('inventory.adjust.categoryEmpty')}
|
||||
onCommit={(next) =>
|
||||
saveInlineBatchFields(item, { category_code: next })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-xs text-gray-400">{item.category_name}</p>
|
||||
)}
|
||||
{reservationGap ? (
|
||||
<p className="text-[11px] text-red-600 dark:text-red-400 mt-0.5 font-medium">
|
||||
{t('inventory.reservedOverAvailable')}
|
||||
@@ -1283,16 +1542,30 @@ export const InventoryManagement: React.FC = () => {
|
||||
case 'batchReport': {
|
||||
/** 2026-10-20:列名「存在报告」,并显示已绑定报告数量 */
|
||||
const reportCount = Number(item.batch_report_count || 0);
|
||||
const reportLabel =
|
||||
reportCount > 0 ? (
|
||||
<span className="text-gray-400">
|
||||
{t('inventory.batchReport.presentCount', { n: reportCount })}
|
||||
</span>
|
||||
) : (
|
||||
<span className="text-red-600 dark:text-red-400 font-medium">
|
||||
{t('inventory.batchReport.missing')}
|
||||
</span>
|
||||
);
|
||||
return (
|
||||
<td key={col.id} className="px-6 py-4 text-sm">
|
||||
{reportCount > 0 ? (
|
||||
<span className="text-gray-400">
|
||||
{t('inventory.batchReport.presentCount', { n: reportCount })}
|
||||
</span>
|
||||
{/* 2026-10-23 开发者:「报告在外面也能删除 上传」⇒ 点该格直接管这一批的报告 */}
|
||||
{canAdjustInventory ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => openInlineReports(item)}
|
||||
className="text-left hover:underline"
|
||||
title={t('inventory.adjust.reportsLabel')}
|
||||
>
|
||||
{reportLabel}
|
||||
</button>
|
||||
) : (
|
||||
<span className="text-red-600 dark:text-red-400 font-medium">
|
||||
{t('inventory.batchReport.missing')}
|
||||
</span>
|
||||
reportLabel
|
||||
)}
|
||||
</td>
|
||||
);
|
||||
@@ -1300,7 +1573,17 @@ export const InventoryManagement: React.FC = () => {
|
||||
case 'available':
|
||||
return (
|
||||
<td key={col.id} className="px-6 py-4 text-sm text-gray-600">
|
||||
{formatQty(item.available_qty)}
|
||||
{canAdjustInventory ? (
|
||||
<InventoryInlineCell
|
||||
value={item.available_qty == null ? '' : String(item.available_qty)}
|
||||
inputKind="decimal"
|
||||
align="right"
|
||||
ariaLabel={t('inventory.adjust.available')}
|
||||
onCommit={(next) => saveInlineAvailableQty(item, next)}
|
||||
/>
|
||||
) : (
|
||||
formatQty(item.available_qty)
|
||||
)}
|
||||
</td>
|
||||
);
|
||||
case 'reserved': {
|
||||
@@ -1363,6 +1646,21 @@ export const InventoryManagement: React.FC = () => {
|
||||
const missingCost =
|
||||
(Number(item.available_qty) || 0) > 0 &&
|
||||
!((Number(item.cost_price) || 0) > 0);
|
||||
// 列表内直接改(2026-10-23):成本改动仍由服务端重算未结算订单
|
||||
if (canAdjustInventory) {
|
||||
return (
|
||||
<td key={col.id} className="px-6 py-4 text-sm">
|
||||
<InventoryInlineCell
|
||||
value={item.cost_price == null ? '' : String(item.cost_price)}
|
||||
inputKind="decimal"
|
||||
align="right"
|
||||
ariaLabel={t('inventory.adjust.cost')}
|
||||
placeholder={t('inventory.col.costMissing')}
|
||||
onCommit={(next) => saveInlineCostPrice(item, next)}
|
||||
/>
|
||||
</td>
|
||||
);
|
||||
}
|
||||
if (missingCost) {
|
||||
return (
|
||||
<td key={col.id} className="px-6 py-4 text-sm">
|
||||
@@ -1413,19 +1711,45 @@ export const InventoryManagement: React.FC = () => {
|
||||
case 'zone':
|
||||
return (
|
||||
<td key={col.id} className="px-6 py-4 text-sm text-gray-600">
|
||||
{item.storage_zone || '-'}
|
||||
{canAdjustInventory ? (
|
||||
<InventoryInlineSelectCell
|
||||
value={item.storage_zone || ''}
|
||||
options={zoneOptions.map((z) => ({ value: z, label: z }))}
|
||||
emptyLabel={t('inventory.adjust.zoneEmpty')}
|
||||
onCommit={(next) => saveInlineBatchFields(item, { storage_zone: next })}
|
||||
/>
|
||||
) : (
|
||||
item.storage_zone || '-'
|
||||
)}
|
||||
</td>
|
||||
);
|
||||
case 'spec':
|
||||
return (
|
||||
<td key={col.id} className="px-6 py-4 text-sm text-gray-600">
|
||||
{item.spec || '-'}
|
||||
{canInlineEditArchiveFields ? (
|
||||
<InventoryInlineCell
|
||||
value={item.spec || ''}
|
||||
ariaLabel={t('inventory.adjust.spec')}
|
||||
onCommit={(next) => saveInlineBatchFields(item, { spec: next })}
|
||||
/>
|
||||
) : (
|
||||
item.spec || '-'
|
||||
)}
|
||||
</td>
|
||||
);
|
||||
case 'unit':
|
||||
return (
|
||||
<td key={col.id} className="px-6 py-4 text-sm text-gray-600">
|
||||
{item.unit || '-'}
|
||||
{canInlineEditArchiveFields ? (
|
||||
<InventoryInlineSelectCell
|
||||
value={item.unit || ''}
|
||||
options={unitChoices}
|
||||
emptyLabel={t('inventory.adjust.unitEmpty')}
|
||||
onCommit={(next) => saveInlineBatchFields(item, { unit: next })}
|
||||
/>
|
||||
) : (
|
||||
item.unit || '-'
|
||||
)}
|
||||
</td>
|
||||
);
|
||||
case 'supplier':
|
||||
@@ -2134,6 +2458,79 @@ export const InventoryManagement: React.FC = () => {
|
||||
</DialogOverlay>
|
||||
)}
|
||||
|
||||
{/* 列表内联:本批次报告查看 / 上传 / 删除 */}
|
||||
{inlineReportItem ? (
|
||||
<DialogOverlay onBackdropMouseDown={closeInlineReports}>
|
||||
<div
|
||||
className="app-surface rounded-xl shadow-xl w-full max-w-md p-5"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="mb-2 flex items-center justify-between">
|
||||
<h3 className="text-base font-semibold">{t('inventory.adjust.reportsLabel')}</h3>
|
||||
<button
|
||||
type="button"
|
||||
onClick={closeInlineReports}
|
||||
className="app-foreground-muted hover:text-gray-700"
|
||||
aria-label={t('common.close')}
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
<p className="mb-2 text-xs text-gray-500">
|
||||
{inlineReportItem.sku_name || inlineReportItem.sku_code} ·{' '}
|
||||
<span className="font-mono">{inlineReportItem.batch_no}</span>
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setInlineReportBindOpen(true)}
|
||||
className="mb-2 text-xs text-indigo-600 hover:underline"
|
||||
>
|
||||
{t('inventory.batchReport.button')}
|
||||
</button>
|
||||
{inlineReportsLoading ? (
|
||||
<p className="text-xs text-gray-400">…</p>
|
||||
) : inlineReports.length === 0 ? (
|
||||
<p className="text-xs text-gray-400">{t('inventory.adjust.reportsEmpty')}</p>
|
||||
) : (
|
||||
<ul className="space-y-1">
|
||||
{inlineReports.map((rep) => (
|
||||
<li key={rep.id} className="flex items-center justify-between gap-2 text-xs">
|
||||
<span className="truncate">
|
||||
{t(`inventory.batchReport.type.${rep.report_type}` as any) || rep.report_type}
|
||||
{rep.report_no ? (
|
||||
<span className="text-gray-400"> · {rep.report_no}</span>
|
||||
) : null}
|
||||
{rep.uploaded_at ? (
|
||||
<span className="text-gray-400"> · {rep.uploaded_at}</span>
|
||||
) : null}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void deleteInlineReport(rep.id)}
|
||||
className="text-red-600 hover:underline"
|
||||
>
|
||||
{t('common.delete')}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
</DialogOverlay>
|
||||
) : null}
|
||||
|
||||
<BatchReportBindModal
|
||||
open={inlineReportBindOpen && Boolean(inlineReportItem?.id)}
|
||||
inventoryId={String(inlineReportItem?.id || '')}
|
||||
report={null}
|
||||
onClose={() => setInlineReportBindOpen(false)}
|
||||
onSaved={() => {
|
||||
const inventoryId = inlineReportItem?.id;
|
||||
if (inventoryId) void loadInlineReports(String(inventoryId));
|
||||
void fetchInventory({ silent: true });
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* 库存「修改」弹窗内:报告直传/修改(批次已确定,不再走解析匹配) */}
|
||||
<BatchReportBindModal
|
||||
open={reportBindOpen && Boolean(adjustItem?.id)}
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
/**
|
||||
* 库存管理列表「内联编辑」契约(2026-10-23 开发者):
|
||||
* 「仓库管理这块 我需要在外面就能修改区域 规格 单位 可用数量(需要是这批的可用数量) 成本价
|
||||
* 这块参考我们的客户列表的的效果」
|
||||
*
|
||||
* 约束:
|
||||
* · 只是入口,写路径必须复用既有两条接口(batch-update / adjust),不新增接口;
|
||||
* · 可用数量按**本批次** `inventory_id`,不是 SKU 级;
|
||||
* · 规格/单位写商品档案,除了 `inventory_adjust` 还要 `product_manage`;
|
||||
* · 提交后静默刷新(不把整表切 loading,否则输入框会被卸载)。
|
||||
*/
|
||||
const page = readFileSync(new URL('./InventoryManagement.tsx', import.meta.url), 'utf8');
|
||||
const zhInventory = readFileSync(
|
||||
new URL('../../i18n/locales/zh-CN/inventory.ts', import.meta.url),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
describe('库存列表内联编辑', () => {
|
||||
it('五列都渲染内联输入(区域/规格/单位/可用数量/成本价)', () => {
|
||||
expect(page).toContain('function InventoryInlineCell({');
|
||||
// 逐个列接线
|
||||
expect(page).toContain('onCommit={(next) => saveInlineAvailableQty(item, next)}');
|
||||
expect(page).toContain('onCommit={(next) => saveInlineCostPrice(item, next)}');
|
||||
expect(page).toContain("onCommit={(next) => saveInlineBatchFields(item, { storage_zone: next })}");
|
||||
expect(page).toContain("onCommit={(next) => saveInlineBatchFields(item, { spec: next })}");
|
||||
expect(page).toContain("onCommit={(next) => saveInlineBatchFields(item, { unit: next })}");
|
||||
});
|
||||
|
||||
it('可用数量按本批次 inventory_id 调整(不是 SKU 级)', () => {
|
||||
const fn = page.slice(page.indexOf('const saveInlineAvailableQty'), page.indexOf('const saveInlineCostPrice'));
|
||||
expect(fn).toContain('adjustInventoryAvailableApi({');
|
||||
expect(fn).toContain('inventoryId: item.id');
|
||||
expect(fn).not.toContain('sku_code');
|
||||
expect(fn).toContain("t('inventory.toast.qtyInvalid')");
|
||||
});
|
||||
|
||||
it('规格/单位需要 product_manage;区域/成本/可用数量只需 inventory_adjust', () => {
|
||||
expect(page).toContain(
|
||||
"const canInlineEditArchiveFields = canAdjustInventory && hasPermission('product_manage');"
|
||||
);
|
||||
// 规格/单位用 canInlineEditArchiveFields 分支
|
||||
const specBlock = page.slice(page.indexOf("case 'spec':"), page.indexOf("case 'unit':"));
|
||||
expect(specBlock).toContain('canInlineEditArchiveFields ? (');
|
||||
const unitBlock = page.slice(page.indexOf("case 'unit':"), page.indexOf("case 'supplier':"));
|
||||
expect(unitBlock).toContain('canInlineEditArchiveFields ? (');
|
||||
// 区域/可用/成本只需 inventory_adjust
|
||||
expect(page.slice(page.indexOf("case 'zone':"), page.indexOf("case 'spec':"))).toContain(
|
||||
'canAdjustInventory ? ('
|
||||
);
|
||||
expect(page).toContain('if (canAdjustInventory) {');
|
||||
});
|
||||
|
||||
it('写路径复用既有接口,备注留空交给服务端补中性标签', () => {
|
||||
expect(page).toContain('updateInventoryBatchApi({');
|
||||
const fn = page.slice(page.indexOf('const saveInlineBatchFields'), page.indexOf('const saveInlineAvailableQty'));
|
||||
expect(fn).toContain("remark: ''");
|
||||
expect(fn).toContain('operatingBranchCode: operatingBranchCode || readOperatingBranchCode()');
|
||||
});
|
||||
|
||||
it('提交后静默刷新列表(不把整表切成 loading)', () => {
|
||||
expect(page).toContain('await fetchInventory({ silent: true });');
|
||||
expect(page).toContain('if (!options.silent) setLoading(true);');
|
||||
expect(page).toContain('if (!options.silent && !isStale()) setLoading(false);');
|
||||
});
|
||||
|
||||
it('失败回退:toast 并把值还原成服务端已知值', () => {
|
||||
const cell = page.slice(page.indexOf('function InventoryInlineCell({'), page.indexOf('export const InventoryManagement'));
|
||||
expect(cell).toContain('notify.error(');
|
||||
expect(cell).toContain('setText(value);');
|
||||
expect(cell).toContain('onKeyDown={(e) => {');
|
||||
expect(cell).toContain("if (e.key === 'Enter') e.currentTarget.blur();");
|
||||
});
|
||||
|
||||
it('区域 / 单位是下拉选择框(不是自由文本)', () => {
|
||||
expect(page).toContain('function InventoryInlineSelectCell({');
|
||||
const zoneBlock = page.slice(page.indexOf("case 'zone':"), page.indexOf("case 'spec':"));
|
||||
expect(zoneBlock).toContain('<InventoryInlineSelectCell');
|
||||
expect(zoneBlock).toContain('options={zoneOptions.map((z) => ({ value: z, label: z }))}');
|
||||
const unitBlock = page.slice(page.indexOf("case 'unit':"), page.indexOf("case 'supplier':"));
|
||||
expect(unitBlock).toContain('<InventoryInlineSelectCell');
|
||||
expect(unitBlock).toContain('options={unitChoices}');
|
||||
// 当前值不在列表里时必须补进去,否则下拉会显示成别的值
|
||||
expect(page).toContain("if (value && !list.some((opt) => opt.value === value))");
|
||||
// 下拉必须用项目统一的 SearchableSelect(原生 select 的展开高度 CSS 管不了)
|
||||
const cell = page.slice(page.indexOf('function InventoryInlineSelectCell({'), page.indexOf('export const InventoryManagement'));
|
||||
expect(cell).toContain('<SearchableSelect');
|
||||
expect(cell).toContain('maxVisibleOptions=');
|
||||
});
|
||||
|
||||
it('数字格不用 type=number:滚轮/方向键不得改数值', () => {
|
||||
const cell = page.slice(page.indexOf('function InventoryInlineCell({'), page.indexOf('function InventoryInlineSelectCell({'));
|
||||
expect(cell).toContain('type="text"');
|
||||
expect(cell).toContain("inputMode={inputKind === 'decimal' ? 'decimal' : undefined}");
|
||||
expect(cell).not.toContain("'number'");
|
||||
expect(cell).not.toContain('step=');
|
||||
});
|
||||
|
||||
it('工具栏按钮:上传报告 → 批量上传报告', () => {
|
||||
expect(zhInventory).toContain("'batchReport.bulkButton': '批量上传报告',");
|
||||
// 弹窗里「本批次补充报告」仍用原文案(不能被一起改成批量)
|
||||
expect(zhInventory).toContain("'batchReport.button': '上传报告',");
|
||||
});
|
||||
|
||||
it('分类也能在列表里就地切换(商品档案级,需 product_manage)', () => {
|
||||
const productBlock = page.slice(page.indexOf("case 'product':"), page.indexOf("case 'batch':"));
|
||||
expect(productBlock).toContain('<InventoryInlineSelectCell');
|
||||
expect(productBlock).toContain('options={categoryChoices}');
|
||||
expect(productBlock).toContain('saveInlineBatchFields(item, { category_code: next })');
|
||||
expect(productBlock).toContain('canInlineEditArchiveFields ? (');
|
||||
});
|
||||
|
||||
it('报告在列表外就能上传/删除(按本批次,复用既有接口)', () => {
|
||||
const reportBlock = page.slice(page.indexOf("case 'batchReport':"), page.indexOf("case 'zone':"));
|
||||
expect(reportBlock).toContain('openInlineReports(item)');
|
||||
const handlers = page.slice(page.indexOf('const loadInlineReports'), page.indexOf('const deleteInlineReport'));
|
||||
expect(handlers).toContain('fetchInventoryBatchReportsByInventoryApi(inventoryId)');
|
||||
expect(page).toContain('await deleteInventoryBatchReportApi(reportId);');
|
||||
// 上传复用「按批次绑定」的同一个弹窗,不新造通道
|
||||
const bind = page.slice(
|
||||
page.indexOf('open={inlineReportBindOpen'),
|
||||
page.indexOf('{/* 库存「修改」弹窗内')
|
||||
);
|
||||
expect(bind).toContain('inventoryId={String(inlineReportItem?.id');
|
||||
expect(page).toContain('<BatchReportBindModal');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user