商品成本:口径不一致行全部计算成本 (#492)

所有匹配到单价的行统一按数量 × 单价计算成本,缺单价行继续待核验。
This commit is contained in:
chenxi 2026-10-24 14:34:08 +08:00
parent f340a295d5
commit ab308fe388
4 changed files with 62 additions and 67 deletions

View File

@ -65,15 +65,6 @@ class SyncStoreReceiptProductCost extends Command
private const REMARK_NO_PRICE = '未匹配商品单价';
/**
* 数量单位与单价口径不一致:定制配件这类明细行,聚水潭把配件尺寸/件数写进了「商品数量」
* (例如数量 9524、金额 2000.04,实际单价 0.21),再乘成品的月加权/代发单价会放大几百倍。
* 成本单价高于该行明细单价 3 倍以上时判定为口径不一致,成本留空不计入合计。
*/
private const REMARK_UNIT_MISMATCH = '数量与单价口径不一致';
private const UNIT_MISMATCH_FACTOR = 3.0;
/** 单价来源:月加权平均单价(首选)与线上代发价格(兜底)。 */
private const PRICE_SOURCE_WEIGHTED = '月加权平均单价';
@ -300,7 +291,6 @@ class SyncStoreReceiptProductCost extends Command
$missingWarehouseRows = 0;
$fallbackPriceRows = 0;
$catalogPriceRows = 0;
$mismatchRows = 0;
$matchedPriceRows = 0;
$placeholderExcludedRows = 0;
$bundleParentExcludedRows = 0;
@ -352,59 +342,42 @@ class SyncStoreReceiptProductCost extends Command
$catalogPriceRows++;
}
}
$quantity = round($line['qty'], 4);
$unitPrice = $price === null ? null : round((float) $price, 2);
$cost = $unitPrice === null ? null : round($quantity * $unitPrice, 2);
// 数量单位与单价口径校验:该行明细单价 = 明细金额 ÷ 数量。
// 成本单价高于明细单价 3 倍以上说明两边单位不是一回事(定制配件行),成本留空。
$detailUnitPrice = $quantity > 0 ? (float) $line['amount'] / $quantity : 0.0;
$unitMismatch = $unitPrice !== null
&& $detailUnitPrice > 0
&& $unitPrice > $detailUnitPrice * self::UNIT_MISMATCH_FACTOR;
if ($unitMismatch) {
$cost = null;
$unitPrice = null;
$priceSource = null;
}
$priced = $this->calculateLineCost((float) $line['qty'], $price);
$quantity = $priced['quantity'];
$unitPrice = $priced['unit_price'];
$cost = $priced['cost'];
$remarks = [];
if ($unitMismatch) {
$remarks[] = self::REMARK_UNIT_MISMATCH;
$mismatchRows++;
}
if ($short === '') {
$remarks[] = self::REMARK_NO_WAREHOUSE;
$missingWarehouseRows++;
}
if ($unitPrice === null) {
if (! $unitMismatch) {
$remarks[] = self::REMARK_NO_PRICE;
$missingPriceRows++;
$pendingCostRows[] = [
'platform' => (string) $bill->platform,
'store_name' => (string) $bill->store_name,
'online_order_no' => $displayOrderNo,
'warehouse' => $line['warehouse'] !== '' ? $line['warehouse'] : null,
'warehouse_short_name' => $short !== '' ? $short : null,
'product_code' => $line['sku'],
'original_platform_sku' => $line['original_platform_sku'] ?? null,
'shop_sku_id' => $line['shop_sku_id'] ?? null,
'shop_i_id' => $line['shop_i_id'] ?? null,
'properties_value' => $line['properties_value'] ?? null,
'mapping_status' => $line['platform_mapping_status'] ?? null,
'mapping_candidates' => $line['platform_mapping_candidates'] ?? [],
'product_quantity' => $quantity,
'reason' => $short === ''
? '未匹配仓库简称'
: (($line['platform_mapping_status'] ?? null) === 'ambiguous'
? '平台 shop_sku_id 对应多个历史内部商品编码'
: (($line['platform_mapping_status'] ?? null) === 'missing'
? '平台 shop_sku_id 未找到历史内部商品编码'
: '仓库简称 + 商品编码在月加权、线上代发、普通商品资料中均无有效单价')),
'code_type' => $this->pendingCodeType((string) $line['sku']),
];
}
$remarks[] = self::REMARK_NO_PRICE;
$missingPriceRows++;
$pendingCostRows[] = [
'platform' => (string) $bill->platform,
'store_name' => (string) $bill->store_name,
'online_order_no' => $displayOrderNo,
'warehouse' => $line['warehouse'] !== '' ? $line['warehouse'] : null,
'warehouse_short_name' => $short !== '' ? $short : null,
'product_code' => $line['sku'],
'original_platform_sku' => $line['original_platform_sku'] ?? null,
'shop_sku_id' => $line['shop_sku_id'] ?? null,
'shop_i_id' => $line['shop_i_id'] ?? null,
'properties_value' => $line['properties_value'] ?? null,
'mapping_status' => $line['platform_mapping_status'] ?? null,
'mapping_candidates' => $line['platform_mapping_candidates'] ?? [],
'product_quantity' => $quantity,
'reason' => $short === ''
? '未匹配仓库简称'
: (($line['platform_mapping_status'] ?? null) === 'ambiguous'
? '平台 shop_sku_id 对应多个历史内部商品编码'
: (($line['platform_mapping_status'] ?? null) === 'missing'
? '平台 shop_sku_id 未找到历史内部商品编码'
: '仓库简称 + 商品编码在月加权、线上代发、普通商品资料中均无有效单价')),
'code_type' => $this->pendingCodeType((string) $line['sku']),
];
} else {
$matchedPriceRows++;
$quantityTotal += $quantity;
@ -446,12 +419,11 @@ class SyncStoreReceiptProductCost extends Command
$this->info(sprintf(' 商品数量合计:%s', number_format($quantityTotal, 2)));
$this->info(sprintf(' 商品成本合计:%s', number_format($costTotal, 2)));
$this->info(sprintf(
' 未匹配仓库简称:%d 行;线上代发价格兜底:%d 行;商品资料成本价兜底:%d 行;仍未匹配商品单价:%d 行;数量与单价口径不一致(不计成本):%d 行',
' 未匹配仓库简称:%d 行;线上代发价格兜底:%d 行;商品资料成本价兜底:%d 行;仍未匹配商品单价:%d 行',
$missingWarehouseRows,
$fallbackPriceRows,
$catalogPriceRows,
$missingPriceRows,
$mismatchRows
));
$pendingCostQuantity = array_sum(array_column($pendingCostRows, 'product_quantity'));
$this->info(sprintf(
@ -486,7 +458,6 @@ class SyncStoreReceiptProductCost extends Command
'platform_sku_mapping_missing_rows' => $mappingMissingRows,
'pending_cost_rows' => $missingPriceRows,
'pending_cost_quantity' => round((float) $pendingCostQuantity, 4),
'unit_mismatch_rows' => $mismatchRows,
'missing_warehouse_rows' => $missingWarehouseRows,
'fallback_purchase_price_rows' => $fallbackPriceRows,
'fallback_catalog_price_rows' => $catalogPriceRows,
@ -678,6 +649,21 @@ class SyncStoreReceiptProductCost extends Command
];
}
/**
* @return array{quantity:float,unit_price:?float,cost:?float}
*/
private function calculateLineCost(float $quantity, ?float $unitPrice): array
{
$quantity = round($quantity, 4);
$unitPrice = $unitPrice === null ? null : round($unitPrice, 2);
return [
'quantity' => $quantity,
'unit_price' => $unitPrice,
'cost' => $unitPrice === null ? null : round($quantity * $unitPrice, 2),
];
}
private function pendingCodeType(string $code): string
{
$code = mb_strtoupper(trim($code));

View File

@ -2316,12 +2316,11 @@ class Pages extends Controller
$agg = $aggQuery->selectRaw('
SUM(CASE WHEN unit_price IS NOT NULL THEN product_quantity ELSE 0 END) as quantity_total,
SUM(CASE WHEN unit_price IS NOT NULL THEN product_cost ELSE 0 END) as cost_total,
SUM(CASE WHEN unit_price IS NULL AND (remark IS NULL OR remark NOT LIKE \'%口径不一致%\') THEN 1 ELSE 0 END) as missing_price_rows,
SUM(CASE WHEN unit_price IS NULL AND (remark IS NULL OR remark NOT LIKE \'%口径不一致%\') THEN product_quantity ELSE 0 END) as missing_price_quantity,
SUM(CASE WHEN unit_price IS NULL THEN 1 ELSE 0 END) as missing_price_rows,
SUM(CASE WHEN unit_price IS NULL THEN product_quantity ELSE 0 END) as missing_price_quantity,
SUM(CASE WHEN warehouse_short_name IS NULL THEN 1 ELSE 0 END) as missing_warehouse_rows,
SUM(CASE WHEN price_source LIKE \'线上代发价格%\' THEN 1 ELSE 0 END) as fallback_price_rows,
SUM(CASE WHEN price_source = \'普通商品资料成本价\' THEN 1 ELSE 0 END) as catalog_price_rows,
SUM(CASE WHEN remark LIKE \'%口径不一致%\' THEN 1 ELSE 0 END) as unit_mismatch_rows
SUM(CASE WHEN price_source = \'普通商品资料成本价\' THEN 1 ELSE 0 END) as catalog_price_rows
')->first();
$options = cache()->remember(
@ -2368,7 +2367,6 @@ class Pages extends Controller
'missing_warehouse_rows' => (int) ($agg->missing_warehouse_rows ?? 0),
'fallback_price_rows' => (int) ($agg->fallback_price_rows ?? 0),
'catalog_price_rows' => (int) ($agg->catalog_price_rows ?? 0),
'unit_mismatch_rows' => (int) ($agg->unit_mismatch_rows ?? 0),
],
'productCostMonths' => $months,
'productCostPlatforms' => array_values(array_map('strval', $options['platforms'] ?? [])),

View File

@ -123,8 +123,7 @@
其中常州简尚家居有限公司的「DF-」商品行例外保留;先执行商品编码全局排除,再执行仓库过滤。
订单状态为 Cancelled(已取消)的明细行不参与商品成本。
同一订单内只要存在带仓库的订单行,空仓库的汇总/占位行即丢弃;若带仓库行全部是 Cancelled,则整单不计入。
定制配件这类明细行,聚水潭把配件件数写进了「商品数量」(例如数量 9524、金额 2000.04,实际单价 0.21),
乘成品的月加权/代发单价会放大几百倍;成本单价高于该行明细单价 3 倍时判为「数量与单价口径不一致」,成本留空、不计入合计。
商品数量与商品单价之间不再设置倍率排除规则;匹配到有效单价的行全部按商品数量 × 商品单价计算成本。
页面默认展示全部数据:已匹配/未匹配商品单价的行、仓库映射表里标记为「-不看」的仓
(次品 / 销退 / 虚拟 / 信誉订单 / 京东云仓等)都纳入列表与统计。
</div>
@ -136,7 +135,6 @@
<div class="item {{ ($summary['pending_cost_rows'] ?? 0) > 0 ? 'is-warn' : '' }}"><span>待核验成本(空单价,未计入成本合计)</span><b>{{ number_format((int) ($summary['pending_cost_rows'] ?? 0)) }} 行 / {{ $qty($summary['pending_cost_quantity'] ?? null) }} 件</b></div>
<div class="item"><span>其中用线上代发价格兜底</span><b>{{ number_format((int) ($summary['fallback_price_rows'] ?? 0)) }}</b></div>
<div class="item"><span>其中用商品资料成本价兜底</span><b>{{ number_format((int) ($summary['catalog_price_rows'] ?? 0)) }}</b></div>
<div class="item"><span>数量与单价口径不一致(不计成本)</span><b>{{ number_format((int) ($summary['unit_mismatch_rows'] ?? 0)) }}</b></div>
<div class="item {{ ($summary['missing_warehouse_rows'] ?? 0) > 0 ? 'is-warn' : '' }}"><span>未匹配仓库简称行数</span><b>{{ number_format((int) ($summary['missing_warehouse_rows'] ?? 0)) }}</b></div>
</div>

View File

@ -93,6 +93,19 @@ class StoreReceiptProductCostLineTest extends TestCase
$this->assertSame([['SG006367N1028V01', 6.0, true]], $components);
}
public function test_priced_line_always_calculates_cost_without_unit_mismatch_override(): void
{
$command = new SyncStoreReceiptProductCost();
$method = new ReflectionMethod($command, 'calculateLineCost');
$method->setAccessible(true);
$result = $method->invoke($command, 9524.0, 58.0);
$this->assertSame(9524.0, $result['quantity']);
$this->assertSame(58.0, $result['unit_price']);
$this->assertSame(552392.0, $result['cost']);
}
/** @return array{0:list<array<string,mixed>>,1:int,2:int} */
private function costLines(array $rows): array
{