库存调整的操作日志留全「修改前 / 修改后 / 改了什么」
开发者反馈:「调整了库存后 修改前是什么修改后什么 改的什么 需要在操作日志里面留下记录」。
盘点三条库存审计(改前):
- inventory_adjust(改可用数量):审计有 batch_no/sku_code/sku_name + 前值 available_qty/quantity +
后值 available_qty/quantity/delta/remark,前端也有 formatInventoryAdjustDetail(可用 前→后)——本就够用;
- inventory_batch_edit(同弹窗改批次属性/成本,或改商品档案分类/规格/单位/产地):
old_value 只有批次级 5 个字段(storage_zone/shelf_life/expiry_date/production_date/cost_price),
**既没有商品/批次身份、也没有商品档案前值**;前端**没有 op 标签、没有详情格式化**
⇒ 落到兜底只显示「库存 <id>」,看不出改的是谁、改前多少;
- inventory_batch_cost_adjust(抵扣弹窗改成本):审计前后值齐全,但前端同样没有标签与格式化。
服务端(inventoryBatchEditService):
- old_value 补 batch_no / sku_code 身份;
- 补商品档案前值(category_code/spec/unit/origin)——此前这两处前值根本没进审计;
- 提交后按同一组 snake_case 列名重读一份「修改后」快照写入 newValue(inventory LEFT JOIN skus),
使 changed 里每个字段都能渲染 前 → 后,**含由 生产日期+保质期 推导的 expiry_date**
(它不在请求体里,只看 input 拿不到);
- 快照查询失败只 console.error,绝不影响审计写入、更不回滚业务。
前端(OperationLogs.tsx + 三语 system.ts):
- 新增分发 inventory_batch_edit / inventory_batch_cost_adjust → 两个详情格式化;
- 渲染:商品 X · 编码 Y · 批次 Z · 改 N 项 · 成本价 2 → 3、区域 - → A区域 · 备注 …(空值 '-');
改成本的另显示「重算 N 张未结算单」;
- 新增文案(中/英/印尼):两个 op 标签 + 成本价/区域/生产日期/保质期/有效期/分类/规格/产地 的
「{from} → {to}」、改 N 项、重算 N 张、兜底「库存批次修改」;单位复用既有 logs.detail.unit。
验证:
- src/pages/system/OperationLogs.inventoryBatchAudit.test.ts(新增 7 例,假 t 断言字段映射):
真实审计行复刻(成本 2→3 + 身份 + 备注)、多字段含空前值(- → 值)、推导字段 expiry_date、
历史行不臆造改动项、全空走兜底、改成本显示重算单数、两 op 已接线且三语键齐全;
- server/tests/inventoryBatchCostRecalc.test.ts:原「新审计形状」一例更新为含 afterAudit,
并新增一例钉住「身份 + 商品档案前值 + 提交后快照 + 快照失败不影响审计」;
- server / web 两端 npx tsc --noEmit 均 0 错误;
- server 全量 26 failed files / 33 failed tests(3094 passed)= 基线;
前端全量 3 failed files / 6 failed tests(972 passed)= 基线(既知 skuCostThirdBucket /
PickingManagement.outboundPhotoPreview / SystemConfig.allowZeroCost);
- 新增的 after 快照 SQL 已对真实 schema 只读校验(inventory LEFT JOIN skus 全列可查);
- 设计门禁 run-all.sh --verify:8 通过 / 1 红(check-ai-cost 既知不可调绿)/ 1 未执行(verify-facts);
- 未部署(前端+后端都需重新构建部署)。
设计文档:docs/design/20261015-库存调整操作日志留全前后值.md
未覆盖:/api/data 通用网关直写库存仍不写这类结构化审计(既有已知异常 A4,不在本次范围)。
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
# 库存调整的操作日志要留全「修改前 / 修改后 / 改了什么」
|
||||
|
||||
> 2026-10-15 · 开发者反馈:「调整了库存后 修改前是什么修改后什么 改的什么 需要在操作日志里面留下记录」
|
||||
|
||||
## 1. 现状盘点(三条与库存调整相关的审计)
|
||||
|
||||
| 操作类型 | 触发面 | 审计内容(改前) | 操作日志展示(改前) |
|
||||
| -------- | ------ | ---------------- | -------------------- |
|
||||
| `inventory_adjust` | 库存管理「修改可用库存」改**可用数量** | ✅ 有 `batch_no/sku_code/sku_name` + 前值 `available_qty/quantity` + 后值 `available_qty/quantity/delta/remark` | ✅ 有格式化 `formatInventoryAdjustDetail`(商品/编码/批次/可用 前→后/备注) |
|
||||
| `inventory_batch_edit` | 同一弹窗改**批次属性**(区域/生产日期/保质期/成本价)或**商品档案**(分类/规格/单位/产地) | ⚠️ `old_value` 只有批次级 5 个字段(无身份、无商品档案前值) | ❌ 没有 op 标签、**没有格式化** ⇒ 落到兜底只显示「库存 <id>」 |
|
||||
| `inventory_batch_cost_adjust` | 「库存抵扣」弹窗改批次成本 | ✅ 有 `batch_no/sku_code/cost_price` 前后值 | ❌ 同样没有 op 标签与格式化 |
|
||||
|
||||
⇒ 用户点名的正是后两条:**日志里看不出改的是哪个商品/批次、改前是多少**。
|
||||
|
||||
## 2. 改动
|
||||
|
||||
### 服务端(`server/src/services/inventoryBatchEditService.ts`)
|
||||
|
||||
1. **补身份**:`old_value` 增加 `batch_no` / `sku_code`(否则日志连"改的谁"都没有);
|
||||
2. **补商品档案前值**:把商品档案块的 `beforeProduct`(`category_code/spec/unit/origin`)提到审计里(在此之前只记了批次级前值 ⇒「分类 从 X 改成 Y」里的 X 根本不存在);
|
||||
3. **补"修改后"快照**:提交后按**同一组 snake_case 列名**重读一遍(`inventory LEFT JOIN skus`),写进 `newValue`。这样 `changed` 里的每个字段都能渲染 `前 → 后` —— 包括**由 生产日期+保质期 推导的 `expiry_date`**(它不在请求体里,只看 `input` 拿不到);
|
||||
4. 快照查询失败**不影响审计写入**,更不回滚业务(`console.error` 后继续)。
|
||||
|
||||
### 前端(`src/pages/system/OperationLogs.tsx` + 三语 `system.ts`)
|
||||
|
||||
1. 新增详情分发:`inventory_batch_edit` → `formatInventoryBatchEditDetail`、`inventory_batch_cost_adjust` → `formatInventoryBatchCostAdjustDetail`;
|
||||
2. 批次修改的详情渲染为:`商品 X · 编码 Y · 批次 Z · 改 N 项 · 成本价 2 → 3、区域 - → A区域 · 备注 …`(空值用 `-`,沿用 `formatScalar`);
|
||||
3. 新增三条文案(中/英/印尼):两个 op 标签 + 详情里的字段标签(`成本价/区域/生产日期/保质期/有效期/分类/规格/产地 前→后`、`改 N 项`、`重算 N 张未结算单`、兜底「库存批次修改」);`单位 {from} → {to}` 复用既有键 `logs.detail.unit`。
|
||||
|
||||
## 3. 验收
|
||||
|
||||
1. 打开「系统管理 → 操作日志」,一条 `inventory_batch_edit` 应显示:**哪个商品/批次 + 改了几项 + 每项的 前 → 后 + 备注**;改成本时还能看到「重算 N 张未结算单」。
|
||||
2. `inventory_batch_cost_adjust`(抵扣弹窗改成本)同样显示批次 + 成本价 前→后 + 重算单数 + 备注。
|
||||
3. 历史行(老 `old_value` 没有新字段、`new_value` 没有 `changed`):至少显示备注或兜底文案,**不得出现空白**。
|
||||
4. `src/pages/system/OperationLogs.inventoryBatchAudit.test.ts`(新增,7 例,假 `t` 断言字段映射)+ `server/tests/inventoryBatchCostRecalc.test.ts`(更新 2 例钉住新审计形状);两端 `npx tsc --noEmit` 0 错误;全量 vitest 失败集与基线一致。
|
||||
|
||||
## 4. 影响面与回滚
|
||||
|
||||
- 影响面:**只增加审计字段与展示**;库存数量/成本/FIFO/预留/抵扣回算/权限一律未动。审计多一次提交后的只读 `SELECT`(单行主键查询)。
|
||||
- 回滚:`git revert` 本提交(前端需重新构建部署)。
|
||||
- 未覆盖:`/api/data` 通用网关直写库存仍不写这类结构化审计(属既有已知异常 A4,不在本次范围)。
|
||||
@@ -96,6 +96,7 @@ AI 提示词清理已停用条目(7 条归档不删、清单过滤 archived、
|
||||
**订单详情补回「收货地址」编辑入口(自建单)**:开发者报障「本地这单地址无法点击编辑」——实测两条路都不通:列表行内地址编辑要求 `is_main_system_order === true`(**只给主系统分配单**),而详情里那条地址行又跟着 `SHOW_ORDER_DETAIL_SHEET_HEADER = false`(常量,有契约测试)的送货单表头**整块被关掉**,`canEditAddress`/`addressEditing`/`onHeaderChange(delivery_address)` 全成死代码 ⇒ Web 上无处改自建单地址(只有录单时能填;小程序详情可改)。按裁决**方案甲**:把地址这一行抽成 `OrderDeliveryAddressEditor`(三态与文案原样复用、不新增文案)**在关闭块之外**渲染,其余表头字段仍隐藏;页面零改动(props 早已传入);状态门槛不变(`PATCH /orders/:code/delivery-address` 窗口为未发车 `<60`),见 [20261015-订单详情补回收货地址编辑入口(自建单).md](./20261015-订单详情补回收货地址编辑入口(自建单).md)。
|
||||
**备货改价不得写坏「库存抵扣成本价」**:开发者报障「我只改了采购成本价和采购数量,抵扣成本价怎么从 2.00 变 2.76 了」——实测订单 `ORD_WEB_20261015_BR002_0002`/抄手皮:库存抵扣账本行 `cost_details(inventory_cover)` 金额由 **10.00**(5 斤 × 批次成本 2.00,`allocated_amount` 仍留着 10.00 作原值指纹)被写成 **13.80**,而 13.80 = 操作日志里那次 `update-purchase-price{prepare_qty:6, purchase_price:2.3}` 的 6 × 2.30;根因是备货改价的成本闭环 `closePurchaseCostLoopAfterPrepPriceChange` 选行 `SELECT … LIMIT 1` **无来源过滤**,部分抵扣行有两行 `purchase`(`inventory_cover` + `order_goods`)时命中了**抵扣行**,且金额用**备货量**而非**缺口量**。按裁决**方案甲**:金额改为 `(计划量 − 抵扣量) × 单价`(与 `orderCostPostingService` 缺口口径一致)、目标只认 `remark='unit_cost_sync'` 占位行、有正式采购账时跳过、**绝不写 `inventory_cover`**(抵扣行只由 FIFO 批次成本回算写);备货量只是"打算买多少",实采量/实采价走验收正式账,多采留库走 `inventory_holding` 持有桶、被抵扣时再搬进来(分公司总额不变),见 [20261015-备货改价不得写坏库存抵扣成本价(缺口口径).md](./20261015-备货改价不得写坏库存抵扣成本价(缺口口径).md)。
|
||||
**库存「调整备注」改为选填**:开发者指令「当前这块 调整备注不是必填选项 改为可填」——原「修改可用库存」弹窗**前后端都拦空备注**(前端 `submitAdjust()` 提示 `remarkRequired` + 标签红星;服务端 `adjustInventoryAvailableQty`(`POST /inventory/adjust`)与 `updateInventoryBatchAttributes`(`POST /inventory/batch-update`)各自 `throw`),**只改前端会 400**,故同批放开两处;空备注补中性标签(`'库存调整'` / `'库存批次修改'`,沿用同仓 `|| '入库量纠错'` 先例)保证**留痕不留空**。该「备注必填」原是 [inventory-freeze](../../.cursor/rules/inventory-freeze.mdc) 点名保护的并列三项之一,本次**只放开这一项**,同批的「双日志 / 已拣未出库允许调高 / 权限收窄 / 网关禁写」一字未动,并已同步 `AGENTS.md` 与规则文档,见 [20261015-库存调整备注改为选填.md](./20261015-库存调整备注改为选填.md)。
|
||||
**库存调整的操作日志留全「修改前/修改后/改了什么」**:开发者反馈「调整了库存后 修改前是什么修改后什么 改的什么 需要在操作日志里面留下记录」——盘点三条库存审计:`inventory_adjust`(可用数量)本就有前后值且有格式化;而 `inventory_batch_edit`(批次属性/成本)**审计缺身份(无商品/批次)、缺商品档案前值**,前端也**没有 op 标签与格式化** ⇒ 落到兜底只显示「库存 <id>」;`inventory_batch_cost_adjust`(抵扣弹窗改成本)同样没有标签与格式化。修法:服务端补 `batch_no/sku_code` 身份、补商品档案(分类/规格/单位/产地)前值、并在**提交后按同一组列名重读一份「修改后」快照**(含由生产日期+保质期推导的 `expiry_date`,它不在请求体里);前端新增两个 op 标签(三语)与两个详情格式化,逐字段渲染 `前 → 后`、`改 N 项`、备注与重算单数,见 [20261015-库存调整操作日志留全前后值.md](./20261015-库存调整操作日志留全前后值.md)。
|
||||
|
||||
## 全量对齐与改造方案(2026-09-28)
|
||||
|
||||
|
||||
@@ -127,6 +127,12 @@ export async function updateInventoryBatchAttributes(
|
||||
};
|
||||
|
||||
const changed: string[] = [];
|
||||
/**
|
||||
* 商品档案(分类/规格/单位/产地)的**修改前**值:本轮改动前只记了批次级前值,
|
||||
* 于是操作日志里"分类 从 X 改成 Y"根本没有 X。开发者也点名要求
|
||||
* 「修改前是什么 / 修改后什么 / 改的什么 都要留痕」⇒ 前值补全。
|
||||
*/
|
||||
let beforeProductAudit: Record<string, string | null> | null = null;
|
||||
|
||||
// ── 批次级 ────────────────────────────────────────
|
||||
const batchSets: string[] = [];
|
||||
@@ -245,6 +251,7 @@ export async function updateInventoryBatchAttributes(
|
||||
unit: sku.unit == null ? null : String(sku.unit),
|
||||
origin: sku.origin == null ? null : String(sku.origin),
|
||||
};
|
||||
beforeProductAudit = { ...beforeProduct };
|
||||
|
||||
// 分类 / 规格 / 单位 → 写商品真源 product_catalog(触发器同步 skus)
|
||||
const pcSets: string[] = [];
|
||||
@@ -324,13 +331,56 @@ export async function updateInventoryBatchAttributes(
|
||||
|
||||
// 审计(提交后写,失败不回滚主流程)
|
||||
try {
|
||||
/**
|
||||
* 「修改后」快照:提交后按同一组字段名重读一遍(含商品档案),
|
||||
* 使 操作日志 能对**每个改动字段**渲染 `前 → 后`(含由 生产日期+保质期 推导的效期,
|
||||
* 它不在请求体里,光看 input 是拿不到的)。
|
||||
*/
|
||||
let afterAudit: Record<string, unknown> = {};
|
||||
try {
|
||||
const afterRow = await client.query(
|
||||
`SELECT i.batch_no, i.sku_code, s.sku_name,
|
||||
i.storage_zone, i.shelf_life, i.expiry_date, i.production_date, i.cost_price,
|
||||
s.category_code, s.spec, s.unit, s.origin
|
||||
FROM inventory i
|
||||
LEFT JOIN skus s ON s.sku_code = i.sku_code
|
||||
WHERE i.id = $1::uuid
|
||||
LIMIT 1`,
|
||||
[result.id]
|
||||
);
|
||||
const a = afterRow.rows[0] || {};
|
||||
afterAudit = {
|
||||
batch_no: a.batch_no == null ? null : String(a.batch_no),
|
||||
sku_code: a.sku_code == null ? null : String(a.sku_code),
|
||||
sku_name: a.sku_name == null ? null : String(a.sku_name),
|
||||
storage_zone: a.storage_zone == null ? null : String(a.storage_zone),
|
||||
shelf_life: a.shelf_life == null ? null : String(a.shelf_life),
|
||||
expiry_date: a.expiry_date == null ? null : toDateOnly(a.expiry_date),
|
||||
production_date: a.production_date == null ? null : toDateOnly(a.production_date),
|
||||
cost_price: a.cost_price == null ? null : Number(a.cost_price),
|
||||
category_code: a.category_code == null ? null : String(a.category_code),
|
||||
spec: a.spec == null ? null : String(a.spec),
|
||||
unit: a.unit == null ? null : String(a.unit),
|
||||
origin: a.origin == null ? null : String(a.origin),
|
||||
};
|
||||
} catch (afterError) {
|
||||
console.error('[inventoryBatchEdit] after-snapshot failed (审计仍写):', afterError);
|
||||
}
|
||||
|
||||
await writeAuditLog({
|
||||
operatorId: input.operatorId || null,
|
||||
operationType: 'inventory_batch_edit',
|
||||
targetTable: 'inventory',
|
||||
targetId: result.id,
|
||||
oldValue: { ...beforeBatch },
|
||||
newValue: { ...input, changed, cost_recalc: costRecalc },
|
||||
// 身份(商品/批次)+ 批次级前值 + 商品档案前值 ⇒ "改的是谁、改前是什么"
|
||||
oldValue: {
|
||||
batch_no: String(row.batch_no || ''),
|
||||
sku_code: skuCode,
|
||||
...beforeBatch,
|
||||
...(beforeProductAudit || {}),
|
||||
},
|
||||
// 请求体(camelCase)+ 同名列的"修改后"快照 + 改动清单 ⇒ "改成什么、改了什么"
|
||||
newValue: { ...input, ...afterAudit, changed, cost_recalc: costRecalc },
|
||||
branchCode,
|
||||
actionCategory: 'inventory',
|
||||
});
|
||||
|
||||
@@ -72,6 +72,20 @@ describe('库存管理「修改」这条入口', () => {
|
||||
it('把重算结果回给调用方并写进审计(可追溯改了几个未结算单)', () => {
|
||||
expect(editService).toContain('cost_recalc?: { affected: number; skipped_settled: number } | null;');
|
||||
expect(editService).toContain('cost_recalc: costRecalc,');
|
||||
expect(editService).toContain('newValue: { ...input, changed, cost_recalc: costRecalc },');
|
||||
// 2026-10-15:审计补「身份 + 商品档案前值 + 改造后的同名列快照」⇒ 日志能显示 前 → 后
|
||||
expect(editService).toContain('newValue: { ...input, ...afterAudit, changed, cost_recalc: costRecalc },');
|
||||
expect(editService).toContain('...(beforeProductAudit || {}),');
|
||||
});
|
||||
|
||||
it('审计留全前后值:身份/批次级前值 + 提交后重读的同名列快照', () => {
|
||||
// 身份(否则日志里看不出改的是哪个商品/批次,只会显示「库存 <id>」)
|
||||
expect(editService).toContain('batch_no: String(row.batch_no || \'\'),');
|
||||
expect(editService).toContain('...beforeBatch,');
|
||||
// 改造后重读一遍(含推导出来的 expiry_date —— 它不在请求体里)
|
||||
expect(editService).toContain('let afterAudit: Record<string, unknown> = {};');
|
||||
expect(editService).toMatch(/SELECT i\.batch_no, i\.sku_code, s\.sku_name/);
|
||||
expect(editService).toContain('expiry_date: a.expiry_date == null ? null : toDateOnly(a.expiry_date),');
|
||||
// 快照失败不得影响审计写入(更不能回滚业务)
|
||||
expect(editService).toContain("console.error('[inventoryBatchEdit] after-snapshot failed (审计仍写):', afterError);");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -507,6 +507,8 @@ export const system: MessageDict = {
|
||||
"logs.op.resolve_ticket": "Resolve exception ticket",
|
||||
"logs.op.close_ticket": "Close exception ticket",
|
||||
"logs.op.inventory_adjust": "Adjust inventory",
|
||||
"logs.op.inventory_batch_edit": "Edit inventory batch",
|
||||
"logs.op.inventory_batch_cost_adjust": "Adjust batch cost",
|
||||
"logs.op.delivery_loading_qty_edit": "Edit loaded quantity",
|
||||
"logs.op.aftersale_handle": "Handle after-sales",
|
||||
"logs.op.statement_confirm": "Confirm statement",
|
||||
@@ -664,6 +666,17 @@ export const system: MessageDict = {
|
||||
"logs.detail.qtyValue": "Qty {qty}",
|
||||
"logs.detail.requiredQty": "Required {qty}",
|
||||
"logs.detail.availableQty": "Available {from} → {to}",
|
||||
"logs.detail.batchEditChangedCount": "{n} field(s) changed",
|
||||
"logs.detail.costPriceChange": "Cost {from} → {to}",
|
||||
"logs.detail.storageZoneChange": "Zone {from} → {to}",
|
||||
"logs.detail.productionDateChange": "Production date {from} → {to}",
|
||||
"logs.detail.shelfLifeChange": "Shelf life {from} → {to}",
|
||||
"logs.detail.expiryDateChange": "Expiry {from} → {to}",
|
||||
"logs.detail.categoryChange": "Category {from} → {to}",
|
||||
"logs.detail.specChange": "Spec {from} → {to}",
|
||||
"logs.detail.originChange": "Origin {from} → {to}",
|
||||
"logs.detail.costAdjustRecalc": "Recalculated {n} unsettled order(s)",
|
||||
"logs.detail.batchEditFallback": "Inventory batch update",
|
||||
"logs.detail.skuCode": "SKU {code}",
|
||||
"logs.detail.batchNo": "Batch {no}",
|
||||
"logs.detail.inventoryReplenish": "Released {n} reservations",
|
||||
|
||||
@@ -507,6 +507,8 @@ export const system: MessageDict = {
|
||||
"logs.op.resolve_ticket": "Selesaikan tiket pengecualian",
|
||||
"logs.op.close_ticket": "Tutup tiket pengecualian",
|
||||
"logs.op.inventory_adjust": "Sesuaikan inventaris",
|
||||
"logs.op.inventory_batch_edit": "Ubah batch inventaris",
|
||||
"logs.op.inventory_batch_cost_adjust": "Sesuaikan biaya batch",
|
||||
"logs.op.delivery_loading_qty_edit": "Ubah qty muat",
|
||||
"logs.op.aftersale_handle": "Tangani purna jual",
|
||||
"logs.op.statement_confirm": "Konfirmasi statement",
|
||||
@@ -664,6 +666,17 @@ export const system: MessageDict = {
|
||||
"logs.detail.qtyValue": "Qty {qty}",
|
||||
"logs.detail.requiredQty": "Harus {qty}",
|
||||
"logs.detail.availableQty": "Tersedia {from} → {to}",
|
||||
"logs.detail.batchEditChangedCount": "{n} bidang diubah",
|
||||
"logs.detail.costPriceChange": "Biaya {from} → {to}",
|
||||
"logs.detail.storageZoneChange": "Zona {from} → {to}",
|
||||
"logs.detail.productionDateChange": "Tanggal produksi {from} → {to}",
|
||||
"logs.detail.shelfLifeChange": "Masa simpan {from} → {to}",
|
||||
"logs.detail.expiryDateChange": "Kedaluwarsa {from} → {to}",
|
||||
"logs.detail.categoryChange": "Kategori {from} → {to}",
|
||||
"logs.detail.specChange": "Spesifikasi {from} → {to}",
|
||||
"logs.detail.originChange": "Asal {from} → {to}",
|
||||
"logs.detail.costAdjustRecalc": "Hitung ulang {n} pesanan belum selesai",
|
||||
"logs.detail.batchEditFallback": "Perubahan batch inventaris",
|
||||
"logs.detail.skuCode": "SKU {code}",
|
||||
"logs.detail.batchNo": "Batch {no}",
|
||||
"logs.detail.inventoryReplenish": "Lepas {n} reservasi",
|
||||
|
||||
@@ -507,6 +507,8 @@ export const system: MessageDict = {
|
||||
"logs.op.resolve_ticket": "处理异常工单",
|
||||
"logs.op.close_ticket": "关闭异常工单",
|
||||
"logs.op.inventory_adjust": "调整库存",
|
||||
"logs.op.inventory_batch_edit": "修改库存批次",
|
||||
"logs.op.inventory_batch_cost_adjust": "修改批次成本",
|
||||
"logs.op.delivery_loading_qty_edit": "核货改实配",
|
||||
"logs.op.aftersale_handle": "处理售后",
|
||||
"logs.op.statement_confirm": "确认结算单",
|
||||
@@ -664,6 +666,17 @@ export const system: MessageDict = {
|
||||
"logs.detail.qtyValue": "数量 {qty}",
|
||||
"logs.detail.requiredQty": "应配 {qty}",
|
||||
"logs.detail.availableQty": "可用 {from} → {to}",
|
||||
"logs.detail.batchEditChangedCount": "改 {n} 项",
|
||||
"logs.detail.costPriceChange": "成本价 {from} → {to}",
|
||||
"logs.detail.storageZoneChange": "区域 {from} → {to}",
|
||||
"logs.detail.productionDateChange": "生产日期 {from} → {to}",
|
||||
"logs.detail.shelfLifeChange": "保质期 {from} → {to}",
|
||||
"logs.detail.expiryDateChange": "有效期 {from} → {to}",
|
||||
"logs.detail.categoryChange": "分类 {from} → {to}",
|
||||
"logs.detail.specChange": "规格 {from} → {to}",
|
||||
"logs.detail.originChange": "产地 {from} → {to}",
|
||||
"logs.detail.costAdjustRecalc": "重算 {n} 张未结算单",
|
||||
"logs.detail.batchEditFallback": "库存批次修改",
|
||||
"logs.detail.skuCode": "编码 {code}",
|
||||
"logs.detail.batchNo": "批次 {no}",
|
||||
"logs.detail.inventoryReplenish": "释放预留 {n} 条",
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
formatInventoryBatchCostAdjustDetail,
|
||||
formatInventoryBatchEditDetail,
|
||||
} from './OperationLogs';
|
||||
|
||||
/**
|
||||
* 库存调整的「操作日志」必须能看清 **修改前是什么 / 修改后是什么 / 改了什么**
|
||||
* (开发者 2026-10-15 反馈:调整了库存后,操作日志里看不出前后值)。
|
||||
*
|
||||
* 现场(本地库 BR002 的真实审计行):
|
||||
* operation_type='inventory_batch_edit',old_value 只有批次级 5 个字段、
|
||||
* new_value 是 camelCase 请求体 + changed 清单 —— 且**没有商品/批次身份**,
|
||||
* 落到兜底分支只显示「库存 <id>」,看不出改的是哪个商品、改前是多少。
|
||||
* 本次:服务端补齐「身份 + 商品档案前值 + 改造后的同名列快照」,前端按 changed 逐字段渲染前→后。
|
||||
*/
|
||||
|
||||
/** 假 t:把 key 与插值原样吐出,便于断言"字段映射对不对" */
|
||||
const t = (key: string, vars?: Record<string, string | number>) =>
|
||||
vars ? `${key}|${JSON.stringify(vars)}` : key;
|
||||
|
||||
type Args = Parameters<typeof formatInventoryBatchEditDetail>;
|
||||
const makeLog = (old_value: Record<string, unknown> | null, new_value: Record<string, unknown> | null) =>
|
||||
({
|
||||
id: 'log-1',
|
||||
operator_id: null,
|
||||
operator_name: null,
|
||||
operator_role: null,
|
||||
operation_type: 'inventory_batch_edit',
|
||||
target_table: 'inventory',
|
||||
target_id: 'e89038b6-f0ff-48da-b173-51bd7144d3e8',
|
||||
branch_code: 'BR002',
|
||||
old_value,
|
||||
new_value,
|
||||
created_at: '2026-10-15 16:40:00+08',
|
||||
}) as Args[0];
|
||||
|
||||
describe('操作日志 · 库存批次修改的前后值', () => {
|
||||
it('真实行复刻:改成本 2 → 3,带商品/批次身份与备注', () => {
|
||||
const text = formatInventoryBatchEditDetail(
|
||||
makeLog(
|
||||
{
|
||||
batch_no: 'IN202610154AZ1ZD',
|
||||
sku_code: 'SKU97144738818',
|
||||
storage_zone: null,
|
||||
shelf_life: null,
|
||||
expiry_date: null,
|
||||
production_date: '2026-10-15',
|
||||
cost_price: 2,
|
||||
category_code: null,
|
||||
spec: null,
|
||||
unit: '斤',
|
||||
origin: null,
|
||||
},
|
||||
{
|
||||
remark: '盘点差异',
|
||||
changed: ['cost_price'],
|
||||
costPrice: 3,
|
||||
batch_no: 'IN202610154AZ1ZD',
|
||||
sku_code: 'SKU97144738818',
|
||||
sku_name: '抄手皮',
|
||||
cost_price: 3,
|
||||
}
|
||||
),
|
||||
t
|
||||
);
|
||||
expect(text).toContain('system.logs.detail.product|{"name":"抄手皮"}');
|
||||
expect(text).toContain('system.logs.detail.batchNo|{"no":"IN202610154AZ1ZD"}');
|
||||
expect(text).toContain('system.logs.detail.batchEditChangedCount|{"n":1}');
|
||||
expect(text).toContain('system.logs.detail.costPriceChange|{"from":"2","to":"3"}');
|
||||
expect(text).toContain('system.logs.detail.remark|{"remark":"盘点差异"}');
|
||||
});
|
||||
|
||||
it('多字段改动(含空前值):逐字段渲染 前 → 后,空值显示 -', () => {
|
||||
const text = formatInventoryBatchEditDetail(
|
||||
makeLog(
|
||||
{ sku_code: 'SKU1', category_code: null, spec: null, unit: null, storage_zone: null, cost_price: 5 },
|
||||
{
|
||||
changed: ['storage_zone', 'category_code', 'unit'],
|
||||
sku_code: 'SKU1',
|
||||
storage_zone: 'A区域',
|
||||
category_code: 'BR002_CAT97143386573',
|
||||
unit: '斤',
|
||||
}
|
||||
),
|
||||
t
|
||||
);
|
||||
expect(text).toContain('system.logs.detail.batchEditChangedCount|{"n":3}');
|
||||
expect(text).toContain('system.logs.detail.storageZoneChange|{"from":"-","to":"A区域"}');
|
||||
expect(text).toContain('system.logs.detail.categoryChange|{"from":"-","to":"BR002_CAT97143386573"}');
|
||||
expect(text).toContain('system.logs.detail.unit|{"from":"-","to":"斤"}');
|
||||
});
|
||||
|
||||
it('推导字段(有效期)也能显示前后:它不在请求体里,必须靠服务端 after 快照', () => {
|
||||
const text = formatInventoryBatchEditDetail(
|
||||
makeLog(
|
||||
{ sku_code: 'SKU1', production_date: '2026-10-01', shelf_life: '7天', expiry_date: '2026-10-08' },
|
||||
{
|
||||
changed: ['production_date', 'shelf_life', 'expiry_date'],
|
||||
sku_code: 'SKU1',
|
||||
production_date: '2026-10-15',
|
||||
shelf_life: '6个月',
|
||||
expiry_date: '2027-04-13',
|
||||
}
|
||||
),
|
||||
t
|
||||
);
|
||||
expect(text).toContain('system.logs.detail.productionDateChange|{"from":"2026-10-01","to":"2026-10-15"}');
|
||||
expect(text).toContain('system.logs.detail.shelfLifeChange|{"from":"7天","to":"6个月"}');
|
||||
expect(text).toContain('system.logs.detail.expiryDateChange|{"from":"2026-10-08","to":"2027-04-13"}');
|
||||
});
|
||||
|
||||
it('历史行(没有 changed 清单):至少显示备注,不臆造改动项', () => {
|
||||
const text = formatInventoryBatchEditDetail(
|
||||
makeLog({ cost_price: 2, shelf_life: null }, { remark: 's', costPrice: 3 }),
|
||||
t
|
||||
);
|
||||
expect(text).toContain('system.logs.detail.remark|{"remark":"s"}');
|
||||
expect(text).not.toContain('batchEditChangedCount');
|
||||
});
|
||||
|
||||
it('审计内容全空才走兜底文案,不显示空白', () => {
|
||||
const text = formatInventoryBatchEditDetail(makeLog(null, null), t);
|
||||
expect(text).toBe('system.logs.detail.batchEditFallback');
|
||||
});
|
||||
|
||||
it('抵扣弹窗「改成本」也显示前后值 + 重算单数', () => {
|
||||
const text = formatInventoryBatchCostAdjustDetail(
|
||||
makeLog(
|
||||
{ batch_no: 'IN202610154AZ1ZD', sku_code: 'SKU97144738818', cost_price: 2 },
|
||||
{
|
||||
batch_no: 'IN202610154AZ1ZD',
|
||||
sku_code: 'SKU97144738818',
|
||||
cost_price: 3,
|
||||
remark: '批次成本改价',
|
||||
recalculated_orders: 1,
|
||||
skipped_settled: 0,
|
||||
}
|
||||
) as never,
|
||||
t
|
||||
);
|
||||
expect(text).toContain('system.logs.detail.batchNo|{"no":"IN202610154AZ1ZD"}');
|
||||
expect(text).toContain('system.logs.detail.costPriceChange|{"from":"2","to":"3"}');
|
||||
expect(text).toContain('system.logs.detail.costAdjustRecalc|{"n":1}');
|
||||
expect(text).toContain('system.logs.detail.remark|{"remark":"批次成本改价"}');
|
||||
});
|
||||
});
|
||||
|
||||
describe('操作日志 · 接线与文案', () => {
|
||||
it('两个 op 都接进详情分发,并且三语都有 op 标签', () => {
|
||||
const page = require('node:fs').readFileSync(
|
||||
new URL('./OperationLogs.tsx', import.meta.url),
|
||||
'utf8'
|
||||
) as string;
|
||||
expect(page).toContain("if (op === 'inventory_batch_edit') {");
|
||||
expect(page).toContain("if (op === 'inventory_batch_cost_adjust') {");
|
||||
const zh = require('node:fs').readFileSync(
|
||||
new URL('../../i18n/locales/zh-CN/system.ts', import.meta.url),
|
||||
'utf8'
|
||||
) as string;
|
||||
const en = require('node:fs').readFileSync(
|
||||
new URL('../../i18n/locales/en/system.ts', import.meta.url),
|
||||
'utf8'
|
||||
) as string;
|
||||
const id = require('node:fs').readFileSync(
|
||||
new URL('../../i18n/locales/id/system.ts', import.meta.url),
|
||||
'utf8'
|
||||
) as string;
|
||||
for (const dict of [zh, en, id]) {
|
||||
expect(dict).toContain('"logs.op.inventory_batch_edit"');
|
||||
expect(dict).toContain('"logs.op.inventory_batch_cost_adjust"');
|
||||
expect(dict).toContain('"logs.detail.costPriceChange"');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -493,6 +493,71 @@ function formatInventoryAdjustDetail(log: LogItem, t: LogsTFn) {
|
||||
.join(' · ') || `${resolveTargetTableLabel(log.target_table, t)} ${log.target_id || ''}`.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 库存批次「修改可用库存」的字段展示顺序(与后端审计快照的 snake_case 列名一一对应)。
|
||||
* 每项都渲染「前 → 后」,让操作日志能看清**修改前是什么、修改后是什么、改了什么**。
|
||||
*/
|
||||
const INVENTORY_BATCH_AUDIT_FIELDS: Array<{ field: string; labelKey: string }> = [
|
||||
{ field: 'cost_price', labelKey: 'costPriceChange' },
|
||||
{ field: 'storage_zone', labelKey: 'storageZoneChange' },
|
||||
{ field: 'production_date', labelKey: 'productionDateChange' },
|
||||
{ field: 'shelf_life', labelKey: 'shelfLifeChange' },
|
||||
{ field: 'expiry_date', labelKey: 'expiryDateChange' },
|
||||
{ field: 'category_code', labelKey: 'categoryChange' },
|
||||
{ field: 'spec', labelKey: 'specChange' },
|
||||
{ field: 'unit', labelKey: 'unit' },
|
||||
{ field: 'origin', labelKey: 'originChange' },
|
||||
];
|
||||
|
||||
/** 库存批次属性/成本修改(`inventory_batch_edit`):身份 + 改了什么 + 逐字段前→后 + 备注 */
|
||||
export function formatInventoryBatchEditDetail(log: LogItem, t: LogsTFn) {
|
||||
const before = log.old_value || {};
|
||||
const after = log.new_value || {};
|
||||
const changed = Array.isArray(after.changed) ? after.changed.map(String) : [];
|
||||
const name = String(after.sku_name || before.sku_name || before.sku_code || after.sku_code || '').trim();
|
||||
const skuCode = String(after.sku_code || before.sku_code || '').trim();
|
||||
const batchNo = String(after.batch_no || before.batch_no || '').trim();
|
||||
const parts: string[] = [];
|
||||
if (name) parts.push(t('system.logs.detail.product', { name }));
|
||||
if (skuCode && skuCode !== name) parts.push(t('system.logs.detail.skuCode', { code: skuCode }));
|
||||
if (batchNo) parts.push(t('system.logs.detail.batchNo', { no: batchNo }));
|
||||
const fieldTexts = INVENTORY_BATCH_AUDIT_FIELDS.filter((item) =>
|
||||
changed.includes(item.field)
|
||||
).map((item) =>
|
||||
t(`system.logs.detail.${item.labelKey}`, {
|
||||
from: formatScalar(before[item.field]),
|
||||
to: formatScalar(after[item.field]),
|
||||
})
|
||||
);
|
||||
if (fieldTexts.length) {
|
||||
parts.push(t('system.logs.detail.batchEditChangedCount', { n: fieldTexts.length }));
|
||||
parts.push(fieldTexts.join('、'));
|
||||
}
|
||||
if (after.remark) parts.push(t('system.logs.detail.remark', { remark: String(after.remark) }));
|
||||
return parts.filter(Boolean).join(' · ') || t('system.logs.detail.batchEditFallback');
|
||||
}
|
||||
|
||||
/** 抵扣弹窗「改成本」(`inventory_batch_cost_adjust`):批次 + 成本价前→后 + 重算单数 + 备注 */
|
||||
export function formatInventoryBatchCostAdjustDetail(log: LogItem, t: LogsTFn) {
|
||||
const before = log.old_value || {};
|
||||
const after = log.new_value || {};
|
||||
const skuCode = String(after.sku_code || before.sku_code || '').trim();
|
||||
const batchNo = String(after.batch_no || before.batch_no || '').trim();
|
||||
const recalculated = Number(after.recalculated_orders) || 0;
|
||||
return [
|
||||
skuCode ? t('system.logs.detail.product', { name: skuCode }) : null,
|
||||
batchNo ? t('system.logs.detail.batchNo', { no: batchNo }) : null,
|
||||
t('system.logs.detail.costPriceChange', {
|
||||
from: formatScalar(before.cost_price),
|
||||
to: formatScalar(after.cost_price),
|
||||
}),
|
||||
recalculated > 0 ? t('system.logs.detail.costAdjustRecalc', { n: recalculated }) : null,
|
||||
after.remark ? t('system.logs.detail.remark', { remark: String(after.remark) }) : null,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' · ') || t('system.logs.detail.batchEditFallback');
|
||||
}
|
||||
|
||||
function formatInspectionPhotoDeleteDetail(log: LogItem, t: LogsTFn) {
|
||||
const nv = log.new_value || {};
|
||||
const idx = nv.photo_index != null ? Number(nv.photo_index) : null;
|
||||
@@ -951,6 +1016,12 @@ function formatLogDetail(log: LogItem, t: LogsTFn) {
|
||||
if (op === 'inventory_adjust') {
|
||||
return formatInventoryAdjustDetail(log, t);
|
||||
}
|
||||
if (op === 'inventory_batch_edit') {
|
||||
return formatInventoryBatchEditDetail(log, t);
|
||||
}
|
||||
if (op === 'inventory_batch_cost_adjust') {
|
||||
return formatInventoryBatchCostAdjustDetail(log, t);
|
||||
}
|
||||
if (op === 'inspection_photo_delete') {
|
||||
return formatInspectionPhotoDeleteDetail(log, t);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user