feat(小程序订单明细): 供应商列补「库存抵扣」选项——后端返回已抵扣量/可抵扣池,前端按 Web 同规则逐行判定可见性
This commit is contained in:
@@ -334,3 +334,19 @@ Web `ProcessingManagement.standaloneUi.contract.test.ts` 新增 2 条(默认
|
||||
| 人工设 9.99 × 2 行(原料成本 12) | 19.98 **保留人工值、不补差 ✅** |
|
||||
|
||||
契约测试:后端 +1 条(新增「总价值守恒」断言),Web +1 条、小程序 +1 条(只人工改价才传 unit_cost);**去掉残差补差后后端测试变红**,还原复绿。
|
||||
|
||||
## 13. 小程序订单明细:供应商列补「库存抵扣」选项(与 Web 对齐)
|
||||
|
||||
开发者(截图:小程序商品明细「白心火龙果」「木瓜」供应商列显示**未分配**):「小程序方面是不是对应这种库存完全抵扣的 供应商列 缺少一个库存抵扣的选项 与 web 界面不一样?」
|
||||
|
||||
**是,确实缺**(小程序 `buildSupplierPicker` 只生成 未分配 / 自采 / 厂商 三类,没有 `STOCK_COVER`)。
|
||||
|
||||
**修法(与 Web 同一口径,不发明新规则)**:
|
||||
1. **后端**(`server/src/routes/mobile/mobileAdminRoutes.ts` 的 `GET /mobile/orders/:orderCode`):
|
||||
- 明细行补 `stock_cover_qty`(已抵扣量);
|
||||
- 新增 `coverable_by_sku`(本单每个 SKU 的可抵扣池,口径 = Web 的 `resolveCoverableQtyForSku`,只读、不改抵扣量)。
|
||||
2. **小程序**:新增纯函数 `miniprogram/utils/stockCoverOption.js`(`resolveStockCoverOptionVisibleIds`)——与 Web `resolveStockCoverOptionVisible` **逐行同规则**:
|
||||
- 已是 `STOCK_COVER` 的行永远可见;已全额覆盖 ⇒ 可见且不消耗池子;否则按行序从池子贪心扣减,够缺口才可见;
|
||||
3. 供应商下拉改为**按行选项**(`item.supplierPickerLabels`),可见时在「自采」之后插入「库存抵扣」;选中后保存 `supplier_code='STOCK_COVER'`(后端已支持,与 Web 手工选同一条路)。
|
||||
|
||||
**测试**:`miniprogram/utils/stockCoverOption.test.js` 5 条(全额已抵扣可见 / 池子按行序贪心 / 池子不足不可见 / 当前值保留 / 部分抵扣按缺口判);**把贪心扣减删掉后 1 条失败**,还原通过。后端/前端 tsc 均 0 error;全量 1416 项 / 1412 passed(4 条既有红灯无关)。
|
||||
|
||||
@@ -239,6 +239,11 @@ function emptyItem(categoryOptions = [], orderStatus = 0) {
|
||||
);
|
||||
}
|
||||
|
||||
const {
|
||||
STOCK_COVER_SUPPLIER_CODE,
|
||||
resolveStockCoverOptionVisibleIds,
|
||||
} = require('../../utils/stockCoverOption');
|
||||
|
||||
function buildSupplierPicker(suppliers = []) {
|
||||
const options = [
|
||||
{ supplier_code: '', company_name: '未分配', label: '未分配' },
|
||||
@@ -262,10 +267,23 @@ function buildSupplierPicker(suppliers = []) {
|
||||
};
|
||||
}
|
||||
|
||||
function withSupplierDisplay(item, supplierOptions = []) {
|
||||
function withSupplierDisplay(item, supplierOptions = [], options) {
|
||||
const showStockCover = Boolean(options && options.showStockCover);
|
||||
const code = normalizeManageSupplierCode(item.supplier_code);
|
||||
const idx = supplierOptions.findIndex((opt) => String(opt.supplier_code || '') === code);
|
||||
const option = idx >= 0 ? supplierOptions[idx] : supplierOptions[0];
|
||||
/**
|
||||
* 每行自己的供应商选项:基础项(未分配/自采/厂商)+ **可见时**追加「库存抵扣」。
|
||||
* 插在「自采」之后,与 Web 下拉顺序一致;当前值已是 STOCK_COVER 的行必须保留该项。
|
||||
*/
|
||||
const baseOptions = supplierOptions || [];
|
||||
const rowOptions = showStockCover
|
||||
? [
|
||||
...baseOptions.slice(0, 2),
|
||||
{ supplier_code: STOCK_COVER_SUPPLIER_CODE, company_name: '库存抵扣', label: '库存抵扣' },
|
||||
...baseOptions.slice(2),
|
||||
]
|
||||
: baseOptions;
|
||||
const idx = rowOptions.findIndex((opt) => String(opt.supplier_code || '') === code);
|
||||
const option = idx >= 0 ? rowOptions[idx] : rowOptions[0];
|
||||
return {
|
||||
...item,
|
||||
supplier_code: code,
|
||||
@@ -275,8 +293,14 @@ function withSupplierDisplay(item, supplierOptions = []) {
|
||||
: code
|
||||
? option?.company_name || item.supplier_name || ''
|
||||
: '',
|
||||
supplier_display: supplierDisplayLabel(code, option, item.supplier_name),
|
||||
supplier_display:
|
||||
code === STOCK_COVER_SUPPLIER_CODE
|
||||
? '库存抵扣'
|
||||
: supplierDisplayLabel(code, option, item.supplier_name),
|
||||
supplier_index: idx >= 0 ? idx : 0,
|
||||
/** 本行下拉用(含「库存抵扣」可见性) */
|
||||
supplierOptions: rowOptions,
|
||||
supplierPickerLabels: rowOptions.map((opt) => opt.label),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -347,7 +371,8 @@ function normalizeEditItem(row = {}, categoryOptions = [], supplierOptions = [],
|
||||
categoryOptions
|
||||
)
|
||||
),
|
||||
supplierOptions
|
||||
supplierOptions,
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
@@ -509,10 +534,31 @@ Page({
|
||||
const smartAllocationEnabled = order.smart_allocation_enabled !== false;
|
||||
const canEditItems = Boolean(order.can_edit_items);
|
||||
const orderStatus = Number(order.order_status) || 0;
|
||||
/**
|
||||
* 「库存抵扣」可见性(与 Web 同口径贪心):每行先带上 `cover_qty`(已抵扣量)
|
||||
* 与 `coverable_qty`(本 SKU 可抵扣池),再按行序判断谁能出现该选项。
|
||||
*/
|
||||
const coverableBySku = (order && order.coverable_by_sku) || {};
|
||||
const itemsForCover = items.map((row) => ({
|
||||
...row,
|
||||
cover_qty: Number(row.stock_cover_qty) || 0,
|
||||
coverable_qty: Number(coverableBySku[String(row.sku_code || '').trim()]) || 0,
|
||||
}));
|
||||
const stockCoverVisibleIds = resolveStockCoverOptionVisibleIds(itemsForCover);
|
||||
const editItems = canEditItems
|
||||
? (items.length
|
||||
? items.map((row) => normalizeEditItem(row, categoryOptions, supplierOptions, orderStatus))
|
||||
: [withSupplierDisplay(emptyItem(categoryOptions, orderStatus), supplierOptions)])
|
||||
? itemsForCover.map((row) =>
|
||||
normalizeEditItem(row, categoryOptions, supplierOptions, orderStatus, {
|
||||
showStockCover:
|
||||
stockCoverVisibleIds.has(String(row.id || '')) ||
|
||||
String(row.supplier_code || '').trim().toUpperCase() === STOCK_COVER_SUPPLIER_CODE,
|
||||
})
|
||||
)
|
||||
: [
|
||||
withSupplierDisplay(emptyItem(categoryOptions, orderStatus), supplierOptions, {
|
||||
showStockCover: false,
|
||||
}),
|
||||
])
|
||||
: [];
|
||||
this._loadedSupplierById = buildLoadedSupplierSnapshot(items);
|
||||
/**
|
||||
@@ -1026,10 +1072,19 @@ Page({
|
||||
onSupplierPick(e) {
|
||||
const index = Number(e.currentTarget.dataset.index);
|
||||
const pickIndex = Number(e.detail.value);
|
||||
const option = this.data.supplierOptions[pickIndex];
|
||||
const row = this.data.editItems[index];
|
||||
/** 每行有自己的选项(含「库存抵扣」可见性)⇒ 必须按本行取,不能用全局列表 */
|
||||
const rowOptions = (row && row.supplierOptions) || this.data.supplierOptions;
|
||||
const option = rowOptions[pickIndex];
|
||||
if (!Number.isFinite(index) || !option) return;
|
||||
const editItems = this.data.editItems.map((item, idx) =>
|
||||
idx === index ? withSupplierDisplay({ ...item, supplier_code: option.supplier_code }, this.data.supplierOptions) : item
|
||||
idx === index
|
||||
? withSupplierDisplay(
|
||||
{ ...item, supplier_code: option.supplier_code },
|
||||
this.data.supplierOptions,
|
||||
{ showStockCover: Boolean(item.supplierOptions && item.supplierOptions.some((opt) => opt.supplier_code === STOCK_COVER_SUPPLIER_CODE)) }
|
||||
)
|
||||
: item
|
||||
);
|
||||
this.setData({ editItems });
|
||||
},
|
||||
|
||||
@@ -137,7 +137,7 @@
|
||||
<view class="g-td c-sup">
|
||||
<picker
|
||||
mode="selector"
|
||||
range="{{supplierPickerLabels}}"
|
||||
range="{{item.supplierPickerLabels || supplierPickerLabels}}"
|
||||
value="{{item.supplier_index}}"
|
||||
data-index="{{index}}"
|
||||
bindchange="onSupplierPick"
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 「库存抵扣」供应商选项可见性——与 Web `resolveStockCoverOptionVisible`
|
||||
* (`src/pages/orders/orderManagement/orderManagementHelpers.ts`)**逐行同口径**。
|
||||
*
|
||||
* 2026-10-23 开发者:小程序订单明细的供应商列缺「库存抵扣」,与 Web 不一致。
|
||||
* 语义(后端权威 `inventoryStockCoverService.STOCK_COVER_SUPPLIER_SQL`):
|
||||
* 只有**全额**能抵扣(抵扣量 ≥ 计划量)的行才允许标 `STOCK_COVER`;部分抵扣时缺口仍要厂商/自采。
|
||||
* · 已是 STOCK_COVER 的行永远可见(当前值必须在选项里);
|
||||
* · 已全额覆盖(快照口径)⇒ 可见,且**不消耗池子**;
|
||||
* · 否则按行序从该 SKU 池子里**贪心扣减**,够本行缺口才可见。
|
||||
* 纯函数(无 wx 依赖),供页面与 node 契约测试共用。
|
||||
*/
|
||||
const STOCK_COVER_SUPPLIER_CODE = 'STOCK_COVER';
|
||||
|
||||
function resolveStockCoverOptionVisibleIds(items = []) {
|
||||
const visible = new Set();
|
||||
const remainBySku = new Map();
|
||||
for (const item of items || []) {
|
||||
const id = String((item && item.id) || '').trim();
|
||||
if (!id) continue;
|
||||
if (String(item.supplier_code || '').trim().toUpperCase() === STOCK_COVER_SUPPLIER_CODE) {
|
||||
visible.add(id);
|
||||
continue;
|
||||
}
|
||||
const sku = String(item.sku_code || '').trim();
|
||||
const qty = Math.max(0, Number(item.quantity) || 0);
|
||||
const covered = Math.max(0, Number(item.cover_qty) || 0);
|
||||
if (!sku || qty <= 0) continue;
|
||||
if (covered + 1e-9 >= qty) {
|
||||
visible.add(id);
|
||||
continue;
|
||||
}
|
||||
if (!remainBySku.has(sku)) {
|
||||
remainBySku.set(sku, Math.max(0, Number(item.coverable_qty) || 0));
|
||||
}
|
||||
const need = Math.max(0, qty - covered);
|
||||
const remain = remainBySku.get(sku) || 0;
|
||||
if (remain + 1e-9 >= need) {
|
||||
visible.add(id);
|
||||
remainBySku.set(sku, Math.max(0, remain - need));
|
||||
}
|
||||
}
|
||||
return visible;
|
||||
}
|
||||
|
||||
module.exports = { STOCK_COVER_SUPPLIER_CODE, resolveStockCoverOptionVisibleIds };
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 小程序「库存抵扣」供应商选项可见性契约(与 Web `resolveStockCoverOptionVisible` 同口径)。
|
||||
* 2026-10-23 开发者:小程序订单明细供应商列缺「库存抵扣」,与 Web 不一致。
|
||||
*/
|
||||
const assert = require('assert');
|
||||
const { resolveStockCoverOptionVisibleIds, STOCK_COVER_SUPPLIER_CODE } = require('./stockCoverOption');
|
||||
|
||||
let failed = 0;
|
||||
function it(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(' ✓', name);
|
||||
} catch (err) {
|
||||
failed += 1;
|
||||
console.log(' ✗', name, '→', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
it('全额已抵扣(cover_qty ≥ quantity)⇒ 该行出现库存抵扣', () => {
|
||||
const ids = resolveStockCoverOptionVisibleIds([
|
||||
{ id: 'a', sku_code: 'S1', quantity: 10, cover_qty: 10, coverable_qty: 0 },
|
||||
]);
|
||||
assert.deepStrictEqual([...ids], ['a']);
|
||||
});
|
||||
|
||||
it('池子够全额 ⇒ 可见,并按行序扣减池子(第二行只剩 5 斤就不够 10 斤)', () => {
|
||||
const ids = resolveStockCoverOptionVisibleIds([
|
||||
{ id: 'a', sku_code: 'S1', quantity: 10, cover_qty: 0, coverable_qty: 15 },
|
||||
{ id: 'b', sku_code: 'S1', quantity: 10, cover_qty: 0, coverable_qty: 15 },
|
||||
]);
|
||||
assert.deepStrictEqual([...ids], ['a']);
|
||||
});
|
||||
|
||||
it('池子不足 ⇒ 不出现(抵不满不给选,与 Web 同口径)', () => {
|
||||
const ids = resolveStockCoverOptionVisibleIds([
|
||||
{ id: 'a', sku_code: 'S1', quantity: 10, cover_qty: 0, coverable_qty: 3 },
|
||||
]);
|
||||
assert.deepStrictEqual([...ids], []);
|
||||
});
|
||||
|
||||
it('当前值已是 STOCK_COVER 的行永远可见(否则下拉看不到当前值)', () => {
|
||||
const ids = resolveStockCoverOptionVisibleIds([
|
||||
{ id: 'a', sku_code: 'S1', quantity: 10, cover_qty: 0, coverable_qty: 0, supplier_code: STOCK_COVER_SUPPLIER_CODE },
|
||||
]);
|
||||
assert.deepStrictEqual([...ids], ['a']);
|
||||
});
|
||||
|
||||
it('部分抵扣的行:差多少就按池子够不够判', () => {
|
||||
const ids = resolveStockCoverOptionVisibleIds([
|
||||
{ id: 'a', sku_code: 'S1', quantity: 10, cover_qty: 6, coverable_qty: 4 },
|
||||
{ id: 'b', sku_code: 'S1', quantity: 10, cover_qty: 0, coverable_qty: 4 },
|
||||
]);
|
||||
assert.deepStrictEqual([...ids], ['a']);
|
||||
});
|
||||
|
||||
if (failed) {
|
||||
console.error(`\n ${failed} 条失败`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('\n 全部通过');
|
||||
@@ -313,6 +313,7 @@ router.get('/orders/:orderCode', authMiddleware, async (req: AuthRequest, res: R
|
||||
}
|
||||
const items = await pool.query(
|
||||
`SELECT oi.id, oi.item_name, oi.sku_code, oi.quantity, oi.unit, oi.price, oi.amount,
|
||||
COALESCE(oi.stock_cover_qty, 0) AS stock_cover_qty,
|
||||
oi.unit_cost AS cost_price, oi.remark, COALESCE(oi.is_extra_dish, false) AS is_extra_dish,
|
||||
COALESCE(oi.is_returned, false) AS is_returned, oi.return_code,
|
||||
(
|
||||
@@ -360,6 +361,22 @@ router.get('/orders/:orderCode', authMiddleware, async (req: AuthRequest, res: R
|
||||
const canEditOrderData =
|
||||
isAdmin || ctx.permissions.includes('order_edit') || ctx.permissions.includes('order_entry');
|
||||
const smartAllocationEnabled = await isSmartAllocationEnabled(order.branch_code);
|
||||
/**
|
||||
* **小程序也出「库存抵扣」供应商选项**(2026-10-23 开发者:小程序供应商列缺库存抵扣,与 Web 不一致)。
|
||||
* 池子口径与 Web 完全一致:`resolveCoverableQtyForSku`(物理自由库存 − 他单活跃预留)。
|
||||
* 这里只读,不改任何抵扣量;是否可选的贪心规则由前端按行序判定(与 Web 同一个函数)。
|
||||
*/
|
||||
const { resolveCoverableQtyForSku } = await import('../../services/inventoryStockCoverService');
|
||||
const coverableSkus = [
|
||||
...new Set(items.rows.map((row) => String(row.sku_code || '').trim()).filter(Boolean)),
|
||||
];
|
||||
const coverablePairs = await Promise.all(
|
||||
coverableSkus.map(async (skuCode) => [
|
||||
skuCode,
|
||||
await resolveCoverableQtyForSku(pool, skuCode, order.branch_code || null).catch(() => 0),
|
||||
] as const)
|
||||
);
|
||||
const coverableBySku: Record<string, number> = Object.fromEntries(coverablePairs);
|
||||
const suppliers = await pool.query(
|
||||
`SELECT supplier_code, company_name
|
||||
FROM suppliers
|
||||
@@ -382,6 +399,7 @@ router.get('/orders/:orderCode', authMiddleware, async (req: AuthRequest, res: R
|
||||
canEditOrderItemsForStatus(status, isAdmin, canEditCompleted, canForceEdit),
|
||||
smart_allocation_enabled: smartAllocationEnabled,
|
||||
suppliers: suppliers.rows,
|
||||
coverable_by_sku: coverableBySku,
|
||||
items: items.rows.map((row) => ({
|
||||
...row,
|
||||
can_hard_delete: !row.is_returned && !row.inbound_done,
|
||||
|
||||
Reference in New Issue
Block a user