feat(订单详情成本): 补齐「采购成本 + 库存抵扣成本」两个值的落盘(上一提交只带上了测试文件改名)
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s

This commit is contained in:
Ubuntu
2026-10-14 19:32:39 +08:00
parent 72435fb1f2
commit 16d8ff8a77
7 changed files with 65 additions and 80 deletions
@@ -832,22 +832,7 @@ export const OrderDetailDeliveryNotePanel: React.FC<OrderDetailDeliveryNotePanel
</td>
{showCostPrice ? (
<td className="border border-slate-700 dark:border-slate-500 px-2 py-1.5 text-center tabular-nums w-24">
{item.display_cost_price != null ? (
/**
* **有库存抵扣的行:成本价 = 计算后的混合均价**(开发者 2026-10-14):
* (缺口量 × 缺口价 + Σ被抵扣批次数量×批次成本价) ÷ 数量。
* 只读 —— 均价不是可输入的单一值;要改缺口价请到「自采 / 供应商备货」列表
* (改价会回写订单行 unit_cost,见方案 A)。
*/
<span
className="font-bold"
title={t('orders.detail.blendedCostHint', {
cover: formatOrderAmount(item.cover_cost_amount || 0),
})}
>
{formatOrderAmount(item.display_cost_price)}
</span>
) : canEditPrice && rowEditable ? (
{canEditPrice && rowEditable ? (
<OrderItemPriceInput
price={Number(item.cost_price) || 0}
onPriceChange={(cost_price) =>
@@ -865,6 +850,22 @@ export const OrderDetailDeliveryNotePanel: React.FC<OrderDetailDeliveryNotePanel
{formatOrderAmount(item.cost_price)}
</span>
)}
{/**
* **库存抵扣成本**(与采购成本**分开**显示,开发者 2026-10-14):
* 单价 = 被抵扣批次加权均价(抵扣成本 ÷ 抵扣量);上方是采购成本(自采/备货
* 列表改价只影响上方那个数)。
*/}
{item.cover_unit_cost != null ? (
<p
className="app-foreground-muted mt-0.5 text-[11px] font-normal"
title={t('orders.detail.coverCostHint', {
cover: formatOrderAmount(item.cover_cost_amount || 0),
qty: Number(item.cover_qty) || 0,
})}
>
抵扣 {formatOrderAmount(item.cover_unit_cost)}
</p>
) : null}
</td>
) : null}
{hideSalePrice ? null : (
+1 -1
View File
@@ -388,7 +388,7 @@ export const orders: MessageDict = {
'detail.selfPurchase': 'Self-purchase',
'detail.unassigned': 'Unassigned',
'detail.stockCover': 'Inventory cover',
'detail.blendedCostHint': 'Inventory cover cost ¥{cover} included; unit cost = (gap + cover) ÷ qty',
'detail.coverCostHint': 'Inventory cover cost = weighted average of covered batches; ¥{cover} booked for this line ({qty} covered)',
'detail.newSupplierOpt': '+ New supplier…',
'detail.hintReturned': 'Already returned',
'detail.stockout': 'Out of stock',
+1 -1
View File
@@ -388,7 +388,7 @@ export const orders: MessageDict = {
'detail.selfPurchase': 'Swakelola',
'detail.unassigned': 'Belum dialokasi',
'detail.stockCover': 'Penutupan stok',
'detail.blendedCostHint': 'Biaya penutupan stok ¥{cover} sudah termasuk; biaya satuan = (kekurangan + penutupan) ÷ jumlah',
'detail.coverCostHint': 'Biaya penutupan stok = rata-rata tertimbang batch; ¥{cover} untuk baris ini ({qty} ditutup)',
'detail.newSupplierOpt': '+ Pemasok baru…',
'detail.hintReturned': 'Sudah retur',
'detail.stockout': 'Stok habis',
+1 -1
View File
@@ -379,7 +379,7 @@ export const orders: MessageDict = {
'detail.selfPurchase': '自采',
'detail.unassigned': '未分配',
'detail.stockCover': '库存抵扣',
'detail.blendedCostHint': '库存抵扣成本 ¥{cover} 已计入;成本价 =(缺口成本 + 抵扣成本)÷ 数量',
'detail.coverCostHint': '库存抵扣成本 = 被抵扣批次加权均价;本行已计入 ¥{cover}(抵扣 {qty} 斤)',
'detail.newSupplierOpt': '+ 新建供应商...',
'detail.hintReturned': '已退货',
'detail.stockout': '缺货',
@@ -153,7 +153,7 @@ import {
isSelfPurchaseReadyStatus,
purchaseReceivedQtyFromOrder,
resolveOrderItemReceivedQty,
resolveBlendedCostPrice,
resolveCoverUnitCost,
pickInboundPhotosForLine,
pickQualityReportFilesForLine,
normalizeInboundPhotoSupplier,
@@ -1265,24 +1265,16 @@ export const OrderManagement: React.FC = () => {
...item,
cost_price: resolveDisplayCostPrice(item, quoteCost),
/**
* 展示用「成本价」:有抵扣的行 = **计算后的混合均价**
* =(缺口量 × 缺口价 + Σ批次数量×批次成本价)÷ 数量;
* 无抵扣 / 批次缺成本价 ⇒ undefined(界面回退 `cost_price`,即缺口价)。
* ⚠️ 只读展示字段,**不参与保存**(保存走 cost_price → 订单行 unit_cost)。
* **库存抵扣成本**(只读展示,不参与保存):被抵扣批次的加权均价 = 抵扣成本 ÷ 抵扣量。
* 采购成本那一半仍走 `cost_price`(可编辑、可被自采/备货列表改价)——
* 两者在订单详情**分开显示**,不做混合均价(开发者 2026-10-14)。
*/
...(() => {
const coverQty = Number(item.stock_cover_qty) || 0;
const coverCost = coverCostByItemId.get(String(item.id));
if (coverQty <= 0 || coverCost == null) return {};
return {
display_cost_price: resolveBlendedCostPrice({
unitCost: resolveDisplayCostPrice(item, quoteCost),
quantity: item.quantity,
coverQty,
coverCost,
}),
cover_cost_amount: coverCost,
};
const coverUnit = resolveCoverUnitCost({ coverQty, coverCost });
if (coverUnit == null) return {};
return { cover_unit_cost: coverUnit, cover_cost_amount: coverCost, cover_qty: coverQty };
})(),
skus: sku,
supplier_code: displaySupplierCode,
@@ -1,42 +1,39 @@
import { describe, expect, it } from 'vitest';
import { resolveBlendedCostPrice } from './orderManagementHelpers';
import { resolveCoverUnitCost } from './orderManagementHelpers';
/**
* **订单侧「成本价」= 计算后的混合均价**(开发者 2026-10-14)。
* **订单详情:库存抵扣成本与采购成本「分开显示」**(开发者 2026-10-14 定稿)。
*
* 口径:成本价 =(缺口量 × 缺口价 + Σ(被抵扣批次数量 × 批次成本价))÷ 数量;
* 抵扣成本取自**被抵扣的库存批次**(开发者例:土豆抵扣 30 斤 = 10 斤 @¥3 + 20 斤 @¥4 ⇒ ¥110)。
* ⚠️ 只算展示值,不动 `order_items.unit_cost` 的存储语义(§5.1:unit_cost = 缺口价)。
* - 采购成本 = 缺口那部分(`order_items.unit_cost`),自采/备货列表改价只影响它(方案 A 回写);
* - **库存抵扣成本** = 被抵扣批次的**加权均价** = 抵扣成本 ÷ 抵扣量,取自**被抵扣的库存批次**
* (Σ 批次数量 × 批次成本价)——开发者例:土豆抵扣 30 斤 = 10 斤 @¥3 + 20 斤 @¥4
* ⇒ 抵扣成本 ¥110 ⇒ 单价 **¥3.6667/斤**。
* - 抵扣成本不可得(批次未设成本价)⇒ 返回 undefined(界面显示「—」),**不要当 0**。
*/
describe('订单侧成本价:混合均价', () => {
it('开发者例(土豆全抵):10 斤 @¥3 + 20 斤 @¥4 ⇒ ¥3.6667', () => {
expect(
resolveBlendedCostPrice({ unitCost: 0, quantity: 30, coverQty: 30, coverCost: 30 + 80 })
).toBe(3.6667);
describe('库存抵扣成本单价(批次加权均价)', () => {
it('开发者例(土豆):10 斤 @¥3 + 20 斤 @¥4 ⇒ ¥3.6667', () => {
expect(resolveCoverUnitCost({ coverQty: 30, coverCost: 30 + 80 })).toBe(3.6667);
});
it('部分抵扣(苹果:抵 30 ¥90 + 缺口 10 斤 @¥3.1)⇒ ¥3.025', () => {
expect(
resolveBlendedCostPrice({ unitCost: 3.1, quantity: 40, coverQty: 30, coverCost: 90 })
).toBe(3.025);
it('苹果:抵扣 30 斤、批次成本 ¥90 ⇒ ¥3', () => {
expect(resolveCoverUnitCost({ coverQty: 30, coverCost: 90 })).toBe(3);
});
it('无抵扣 ⇒ 原样返回缺口价(一行不变)', () => {
expect(resolveBlendedCostPrice({ unitCost: 3, quantity: 20, coverQty: 0, coverCost: 0 })).toBe(3);
expect(resolveBlendedCostPrice({ unitCost: 3, quantity: 20 })).toBe(3);
it('多批次不同价 ⇒ 加权均价(不是简单算术平均)', () => {
expect(resolveCoverUnitCost({ coverQty: 10, coverCost: 10 * 3 + 20 * 4 })).toBe(11);
});
it('批次缺成本价(coverCost 不可得)⇒ 回退缺口价,绝不显示 0', () => {
expect(resolveBlendedCostPrice({ unitCost: 3, quantity: 20, coverQty: 10, coverCost: null })).toBe(3);
expect(resolveBlendedCostPrice({ unitCost: 3, quantity: 20, coverQty: 10, coverCost: NaN })).toBe(3);
it('批次未设成本价 / 无抵扣 ⇒ undefined(界面显示「—」,不显示 0)', () => {
expect(resolveCoverUnitCost({ coverQty: 5, coverCost: null })).toBeUndefined();
expect(resolveCoverUnitCost({ coverQty: 0, coverCost: 0 })).toBeUndefined();
expect(resolveCoverUnitCost({})).toBeUndefined();
});
it('数量为 0/缺省 ⇒ 回退缺口价(不做除零)', () => {
expect(resolveBlendedCostPrice({ unitCost: 2.5, quantity: 0, coverQty: 0, coverCost: 10 })).toBe(2.5);
expect(resolveBlendedCostPrice({ unitCost: 2.5, coverQty: 1, coverCost: 10 })).toBe(2.5);
it('抵扣量为 0 不做除零', () => {
expect(resolveCoverUnitCost({ coverQty: 0, coverCost: 10 })).toBeUndefined();
});
it('全额抵扣且缺口价为 0 ⇒ 就是批次均价', () => {
expect(resolveBlendedCostPrice({ unitCost: 0, quantity: 8, coverQty: 8, coverCost: 24 })).toBe(3);
it('0 是合法成本价(允许成本价为 0 开关)⇒ 返回 0,而不是 undefined', () => {
expect(resolveCoverUnitCost({ coverQty: 8, coverCost: 0 })).toBe(0);
});
});
@@ -60,13 +60,15 @@ export interface OrderItem {
/** 成本价:报价单优先;缺失时展示本单备货/自采价 */
cost_price?: number;
/**
* **订单侧展示用「成本价」= 计算后的混合均价**(开发者 2026-10-14):
* (缺口量 × 缺口价 + Σ(被抵扣批次数量 × 批次成本价))÷ 数量。
* ⚠️ 只用于**展示**;编辑/提交仍走 `cost_price`(= 行 `unit_cost`,缺口价),不得写回库。
* **库存抵扣成本单价**(开发者 2026-10-14):被抵扣批次的加权均价 = 抵扣成本 ÷ 抵扣量。
* 订单详情把它与「采购成本」**分开显示**(不再折算混合均价):
* 自采/备货列表改价只影响采购那一半,抵扣这一半恒为批次均价。
*/
display_cost_price?: number;
cover_unit_cost?: number;
/** 该行被抵扣部分的成本(Σ 批次数量 × 批次成本价) */
cover_cost_amount?: number;
/** 该行被抵扣的量(`order_items.stock_cover_qty` 快照,供 tooltip 展示) */
cover_qty?: number;
amount: number;
unit: string;
ordered_quantity?: number | null;
@@ -240,30 +242,23 @@ export function resolveOrderItemReceivedQty(input: {
}
/**
* 订单侧「成本价」展示:**计算后的混合均价**(开发者 2026-10-14 定稿)。
* **库存抵扣成本的单价**(开发者 2026-10-14 定稿)= 抵扣成本 ÷ 抵扣量。
*
* 抵扣成本取自**被抵扣的库存批次**(Σ 批次数量 × 批次成本价)——开发者例:
* 土豆抵扣 30 斤 = 10 斤 @¥3 + 20 斤 @¥4 ⇒ 抵扣成本 ¥110、均价 **¥3.6667/斤**。
* 无抵扣、或批次未设成本价(`coverCost` 不可得)时**回退缺口价**,绝不把成本显示成 0。
* ⚠️ 只算展示值,不动 `order_items.unit_cost` 的存储语义(§5.1)。
* 土豆抵扣 30 斤 = 10 斤 @¥3 + 20 斤 @¥4 ⇒ 抵扣成本 ¥110 ⇒ 单价 **¥3.6667/斤**。
*
* ⚠️ 订单详情**不做混合均价**:采购那半与抵扣这半**分开显示**,自采/备货列表改价只影响采购那半。
* 抵扣成本不可得(批次未设成本价)⇒ 返回 undefined(界面显示「—」),**不要当 0 显示**。
*/
export function resolveBlendedCostPrice(input: {
unitCost?: number | null;
quantity?: number | null;
export function resolveCoverUnitCost(input: {
coverQty?: number | null;
coverCost?: number | null;
}): number {
const unit = Number(input.unitCost) || 0;
const qty = Number(input.quantity) || 0;
}): number | undefined {
const coverQty = Math.max(0, Number(input.coverQty) || 0);
// ⚠️ `null/undefined` = 批次没给成本价(不可得)⇒ 回退缺口价,**不要当 0**;
// 0 是合法成本价(「允许成本价为 0」开关)⇒ 照常参与均价计算。
if (input.coverCost == null) return unit;
if (coverQty <= 0 || input.coverCost == null) return undefined;
const coverCost = Number(input.coverCost);
if (qty <= 0) return unit;
if (coverQty <= 0 || !Number.isFinite(coverCost) || coverCost < 0) return unit;
const gap = Math.max(0, qty - coverQty);
return Math.round(((gap * unit + coverCost) / qty) * 10000) / 10000;
if (!Number.isFinite(coverCost) || coverCost < 0) return undefined;
return Math.round((coverCost / coverQty) * 10000) / 10000;
}
/** 订单详情「计划数量」展示:用下单量快照,避免履约后把结算量当计划 */