fix: 厂家记账拆分实际商品编码补全供应商后依序核算 #980
@ -7,6 +7,7 @@
|
||||
1. 对“补偿款项”“记账全款”(兼容“记账全额”),原金额大于0且小于50元,先判断原金额÷商品买家实付合计是否小于40%。命中后金额为0,不扣款,追加备注 `小于50且小于0.4,记账改0`。
|
||||
2. 未命中的,再用工单 `actual_paid_amount`(页面“实付金额”)作分母,独立判断同样的严格边界。任一轮实付缺失、无效或为0时,只跳过该轮;另一轮仍可正常判断。多商品买家实付缺一项时,不把部分金额当作完整合计。
|
||||
3. 保留厂家记账配置、代发仓发错货限制和多供应商待确认规则。只有已判定可扣款的剩余工单进入核价,不用核价推翻“不扣款”或“待确认”。
|
||||
工单供应商为空时,先按普通商品编码匹配商品资料;完整的 `ZH-/MKH-商品编码-数量-商品编码-数量` 组合编码拆出组件后逐个精确匹配,普通商品精确命中优先。运营组合套装从六个月内原单发货明细拆出实际商品编码,必须覆盖全部指定单号并核对订单;未发出或未完整匹配时不采用部分明细。每个组件都查到商品资料供应商后,才按当前配置、小额归零和核价规则继续处理。保留原编码,展示实际商品编码、逐编码供应商匹配说明;任一组件缺供应商或多供应商判定不一致仍待确认,不使用部分供应商扣款。不覆盖工单已有供应商。
|
||||
4. 补偿、全款按原单商品的实际发货数量乘普通商品资料当前成本单价,再加实际包裹运费,与原金额取较小值。成本按商品编码精确匹配商品资料的当前 `cost_price`,不代表历史月份成本。
|
||||
5. 运费损耗优先按正数实际运费记账,不受原金额限制;原金额为0也需要匹配。原单包裹运费为空或0时,按对应工单商品的当前成本单价×实际发货数量×0.2替代。多个包裹中只替代缺运费的包裹,其余仍取实际运费。替代需要有效商品成本与逐商品发货数量,缺失或数量含糊时待确认;已有正数实际运费不要求商品成本和数量。
|
||||
|
||||
|
||||
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\EcommerceFinance\Services;
|
||||
|
||||
use Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository;
|
||||
use Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository as Shipping;
|
||||
use Throwable;
|
||||
|
||||
/** 缺供应商的工单,先还原普通商品编码,再按商品资料补充供应商。 */
|
||||
class SupplierBookkeepingProductResolver
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProductCatalogMirrorRepository $products,
|
||||
private readonly Shipping $shipping,
|
||||
) {}
|
||||
|
||||
public function apply(array $rows): array
|
||||
{
|
||||
$missing = array_filter($rows, static fn ($row) => trim((string) ($row['supplier'] ?? '')) === '');
|
||||
if ($missing === []) return $rows;
|
||||
try {
|
||||
$plans = [];
|
||||
$codes = [];
|
||||
foreach ($missing as $key => $row) {
|
||||
$original = array_map([Shipping::class, 'sku'], Shipping::numbers($row['product_code'] ?? ''));
|
||||
$plans[$key] = [];
|
||||
foreach ($original as $code) {
|
||||
$parts = self::encodedComponents($code);
|
||||
$plans[$key][$code] = $parts;
|
||||
array_push($codes, $code, ...$parts);
|
||||
}
|
||||
}
|
||||
$catalog = $this->catalog($codes);
|
||||
$needsShipping = [];
|
||||
foreach ($plans as $key => $plan) {
|
||||
// 运营套装的编号本身不含组件,必须有原单发货明细作依据。
|
||||
if (count($plan) === 1) {
|
||||
$code = (string) array_key_first($plan);
|
||||
if (!isset($catalog[$code]) && preg_match('/^(?:MKH-)?运营组合套装/u', $code)) $needsShipping[$key] = $missing[$key];
|
||||
}
|
||||
}
|
||||
if ($needsShipping !== []) {
|
||||
$shipments = $this->shipping->shipmentsFor(array_values($needsShipping));
|
||||
$extraCodes = [];
|
||||
foreach ($needsShipping as $key => $row) {
|
||||
$parts = $this->shippedComponents($row, $shipments);
|
||||
$plans[$key][array_key_first($plans[$key])] = $parts;
|
||||
array_push($extraCodes, ...$parts);
|
||||
}
|
||||
$catalog += $this->catalog($extraCodes);
|
||||
}
|
||||
foreach ($plans as $key => $plan) {
|
||||
$resolved = [];
|
||||
$suppliers = [];
|
||||
$unresolved = [];
|
||||
$details = [];
|
||||
foreach ($plan as $code => $parts) {
|
||||
// 普通商品精确命中优先,不拆合法商品编码内部的连字符。
|
||||
foreach (isset($catalog[$code]) ? [$code] : ($parts !== [] ? $parts : [$code]) as $part) {
|
||||
$resolved[$part] = true;
|
||||
$supplier = trim((string) ($catalog[$part]['supplier_name'] ?? ''));
|
||||
$details[$part] = $part . '→' . ($supplier !== '' ? $supplier : '未匹配供应商');
|
||||
if ($supplier === '') $unresolved[] = $part;
|
||||
else $suppliers[$supplier] = true;
|
||||
}
|
||||
}
|
||||
$rows[$key]['resolved_product_code'] = implode("\n", array_keys($resolved));
|
||||
$rows[$key]['supplier_match_basis'] = '按商品编码匹配商品资料:' . implode(';', $details);
|
||||
// 任一组件未匹配时不使用部分供应商作扣款判定。
|
||||
if ($resolved !== [] && $unresolved === []) $rows[$key]['supplier'] = implode("\n", array_keys($suppliers));
|
||||
}
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
foreach (array_keys($missing) as $key) $rows[$key]['supplier_match_basis'] = '商品资料或原单发货明细暂不可读取,供应商待确认';
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function catalog(array $codes): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (array_chunk(array_values(array_unique($codes)), 150) as $chunk) {
|
||||
foreach ($this->products->normalProductsBySkuCodes($chunk) as $product) {
|
||||
$code = Shipping::sku($product->sku_id);
|
||||
if (!isset($result[$code]) || (int) $product->autoid > (int) $result[$code]['autoid']) $result[$code] = (array) $product;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/** 仅识别完整的“ZH-/MKH- 商品编码-数量”结构,组件仍须逐个命中普通商品资料。 */
|
||||
private static function encodedComponents(string $code): array
|
||||
{
|
||||
if (!preg_match('/^(?:ZH-|MKH-)(.+)$/u', $code, $prefix)) return [];
|
||||
preg_match_all('/(.+?)-([1-9]\d*)(?:-|$)/u', $prefix[1], $matches, PREG_SET_ORDER);
|
||||
if (count($matches) < 2 || implode('', array_column($matches, 0)) !== $prefix[1]) return [];
|
||||
return array_values(array_unique(array_map(static fn ($match) => Shipping::sku($match[1]), $matches)));
|
||||
}
|
||||
|
||||
private function shippedComponents(array $row, array $shipments): array
|
||||
{
|
||||
[$from, $to] = Shipping::window($row);
|
||||
$tracking = Shipping::numbers($row['tracking_no'] ?? '');
|
||||
$orders = Shipping::numbers($row['online_order_no'] ?? '');
|
||||
$wanted = $tracking !== [] ? $tracking : $orders;
|
||||
if ($wanted === []) return [];
|
||||
$matched = [];
|
||||
$codes = [];
|
||||
foreach ($shipments as $shipment) {
|
||||
$references = $tracking !== [] ? $shipment['tracking_numbers'] : $shipment['order_numbers'];
|
||||
if (array_intersect($wanted, $references) === []) continue;
|
||||
if ($orders !== [] && array_intersect($orders, $shipment['order_numbers']) === []) continue;
|
||||
if ($shipment['ship_time'] === null || $shipment['ship_time'] < $from || $shipment['ship_time'] >= $to) continue;
|
||||
if (!in_array(trim((string) ($shipment['unshipped'] ?? '')), ['', '#N/A'], true)) return [];
|
||||
array_push($matched, ...$references);
|
||||
array_push($codes, ...array_map([Shipping::class, 'sku'], Shipping::numbers($shipment['barcode'])));
|
||||
}
|
||||
return array_diff($wanted, $matched) === [] ? array_values(array_unique($codes)) : [];
|
||||
}
|
||||
}
|
||||
@ -34,7 +34,7 @@ class SupplierBookkeepingValuation
|
||||
}
|
||||
$skus = [];
|
||||
foreach ($candidates as $row) {
|
||||
array_push($skus, ...Repository::numbers($row['product_code'] ?? ''));
|
||||
array_push($skus, ...Repository::numbers(($row['resolved_product_code'] ?? '') !== '' ? $row['resolved_product_code'] : ($row['product_code'] ?? '')));
|
||||
}
|
||||
try {
|
||||
$costs = $this->repository->costsFor(array_values(array_unique($skus)));
|
||||
@ -99,7 +99,7 @@ class SupplierBookkeepingValuation
|
||||
$packages = [];
|
||||
$quantity = '0';
|
||||
$cost = '0';
|
||||
$wanted = array_fill_keys(array_map([Repository::class, 'sku'], Repository::numbers($row['product_code'] ?? '')), false);
|
||||
$wanted = array_fill_keys(array_map([Repository::class, 'sku'], Repository::numbers(($row['resolved_product_code'] ?? '') !== '' ? $row['resolved_product_code'] : ($row['product_code'] ?? ''))), false);
|
||||
$freightOnly = $row['bookkeeping_type'] === '运费损耗';
|
||||
$lines = [];
|
||||
if (!$freightOnly && $wanted === []) return $this->review($row, '工单没有商品编码,无法匹配成本');
|
||||
|
||||
@ -7,6 +7,7 @@ use Modules\EcommerceFinance\Repositories\SupplierBookkeepingWorkOrderRepository
|
||||
use Modules\EcommerceFinance\Services\SupplierBookkeepingConfigStore;
|
||||
use Modules\EcommerceFinance\Services\SupplierBookkeepingDeductionRule as Rule;
|
||||
use Modules\EcommerceFinance\Services\SupplierBookkeepingValuation;
|
||||
use Modules\EcommerceFinance\Services\SupplierBookkeepingProductResolver;
|
||||
|
||||
/** 供应商应付 → 厂家记账(聚工单“记账”工单镜像)。 */
|
||||
class SupplierBookkeepingPageData
|
||||
@ -37,7 +38,8 @@ class SupplierBookkeepingPageData
|
||||
'tracking_no' => '物流单号', 'shop_name' => '店铺名称', 'platform' => '平台', 'shipping_warehouse' => '订单发货仓库',
|
||||
'status' => '工单状态', 'processing_status' => '记账处理状态', 'counterparty_agreed' => '被记账方是否同意记账',
|
||||
'counterparty_note' => '被记账方处理说明', 'specialist_result' => '专员记账判定结果', 'specialist_note' => '专员处理说明',
|
||||
'updated_at' => '修改时间', 'work_order_type' => '工单类型', 'product_code' => '商品编码', 'style_code' => '款式编码',
|
||||
'updated_at' => '修改时间', 'work_order_type' => '工单类型', 'product_code' => '商品编码', 'resolved_product_code' => '实际商品编码',
|
||||
'supplier_match_basis' => '供应商匹配说明', 'style_code' => '款式编码',
|
||||
'other_attribute_6' => '其它属性6', 'product_name' => '商品名称', 'platform_sku_id' => '平台商品skuId',
|
||||
'platform_product_id' => '平台商品id', 'product_quantity' => '商品数量', 'product_price' => '商品价格',
|
||||
'product_amount' => '商品金额', 'buyer_paid_amount' => '买家实付', 'supplier' => '供应商',
|
||||
@ -50,6 +52,7 @@ class SupplierBookkeepingPageData
|
||||
private readonly Repository $repository,
|
||||
private readonly SupplierBookkeepingConfigStore $configs,
|
||||
private readonly SupplierBookkeepingValuation $valuation,
|
||||
private readonly SupplierBookkeepingProductResolver $products,
|
||||
) {}
|
||||
|
||||
/** @return array<string,string> */
|
||||
@ -107,20 +110,27 @@ class SupplierBookkeepingPageData
|
||||
$matched = [];
|
||||
foreach ($rows ?? $this->repository->rows() as $row) {
|
||||
$row['original_amount'] = $row['amount'];
|
||||
$row += ['matched_cost' => null, 'matched_freight' => null, 'matched_quantity' => '', 'matched_tracking_no' => '', 'valuation_window' => ''];
|
||||
// 配置随时可改,判定不进镜像结果缓存,每次按当前配置计算。
|
||||
$row = Rule::decide($row, $configs) + $row;
|
||||
$row += ['matched_cost' => null, 'matched_freight' => null, 'matched_quantity' => '', 'matched_tracking_no' => '', 'valuation_window' => '', 'resolved_product_code' => '', 'supplier_match_basis' => ''];
|
||||
if ($from !== '' && $row['started_at'] < $from) continue;
|
||||
if ($to !== '' && $row['started_at'] > $to) continue;
|
||||
if ($filters['reason'] !== '' && $row['bookkeeping_reason'] !== $filters['reason'] && $row['reason_level_1'] !== $filters['reason']) continue;
|
||||
if ($filters['status'] !== '' && $row['status'] !== $filters['status']) continue;
|
||||
if ($filters['bookkeeping_type'] !== '' && $row['bookkeeping_type'] !== $filters['bookkeeping_type']) continue;
|
||||
foreach (self::TEXT_FILTERS as $key => $label) {
|
||||
if ($filters[$key] !== '' && mb_stripos((string) ($row[$key] ?? ''), $filters[$key]) === false) continue 2;
|
||||
}
|
||||
$matched[] = $row;
|
||||
}
|
||||
foreach ($this->valuation->apply($matched) as $row) {
|
||||
$resolved = [];
|
||||
foreach ($this->products->apply($matched) as $row) {
|
||||
// 先补全供应商,再按当前配置及既定金额顺序判断;不缓存判定。
|
||||
$row = Rule::decide($row, $configs) + $row;
|
||||
if ($row['supplier_match_basis'] !== '') $row['deduction_basis'] = $row['supplier_match_basis'] . ';' . $row['deduction_basis'];
|
||||
foreach (self::TEXT_FILTERS as $key => $label) {
|
||||
$value = (string) ($row[$key] ?? '');
|
||||
if ($key === 'product_code') $value .= "\n" . $row['resolved_product_code'];
|
||||
if ($filters[$key] !== '' && mb_stripos($value, $filters[$key]) === false) continue 2;
|
||||
}
|
||||
$resolved[] = $row;
|
||||
}
|
||||
foreach ($this->valuation->apply($resolved) as $row) {
|
||||
if ($deduction !== '' && $row['deduction_status'] !== $deduction) continue;
|
||||
yield $row;
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@
|
||||
<div class="is-key"><span>厂家扣款金额(元)</span><strong>{{ number_format($bookkeepingSummary['deduction_cents'] / 100, 2) }}</strong></div>
|
||||
<div><span>待确认</span><strong class="{{ $bookkeepingSummary['review'] > 0 ? 'sb-warn-text' : '' }}">{{ number_format($bookkeepingSummary['review']) }}</strong></div>
|
||||
</div>
|
||||
<details class="sb-rules"><summary>接入口径</summary><p>与聚工单「协同工单列表 → 记账」视图一致:工单类型为“记账”;工单状态为待审核、已拒绝、待处理、处理中、已完成(不含已取消);记账原因为“厂家记账”下 12 项与“仓库记账”下 5 项二级原因;发起时间不早于 2026-09-01。同一工单号只取最新一版,状态或原因在聚工单变更后随镜像同步自动更新;多件商品的工单在商品列逐行展示,记账金额只计一次。数据只读,不在此修改。</p><p>厂家扣款判定:记账原因为“厂家记账”的,按「厂家记账配置」中供应商的记账方式判定:“正常记账”以及配置表中没有的供应商(含配置不在生效期内)计入扣款,“仅代发仓发错货记账”“不记账”不扣;记账原因为“仓库记账”的,只有订单发货仓库为“某某代发仓”且二级原因为“仓库发错货”时才作为厂家扣款项(供应商配置为“不记账”的除外),其余仓库记账一律不扣。工单未带供应商、或多件商品分属不同供应商且判定不一致的显示“待确认”,不计入扣款金额。</p></details>
|
||||
<details class="sb-rules"><summary>接入口径</summary><p>与聚工单「协同工单列表 → 记账」视图一致:工单类型为“记账”;工单状态为待审核、已拒绝、待处理、处理中、已完成(不含已取消);记账原因为“厂家记账”下 12 项与“仓库记账”下 5 项二级原因;发起时间不早于 2026-09-01。同一工单号只取最新一版,状态或原因在聚工单变更后随镜像同步自动更新;多件商品的工单在商品列逐行展示,记账金额只计一次。数据只读,不在此修改。</p><p>厂家扣款判定:记账原因为“厂家记账”的,按「厂家记账配置」中供应商的记账方式判定:“正常记账”以及配置表中没有的供应商(含配置不在生效期内)计入扣款,“仅代发仓发错货记账”“不记账”不扣;记账原因为“仓库记账”的,只有订单发货仓库为“某某代发仓”且二级原因为“仓库发错货”时才作为厂家扣款项(供应商配置为“不记账”的除外),其余仓库记账一律不扣。工单未带供应商时先按普通商品编码、可识别组合编码或原单发货明细拆出实际商品编码,到商品资料匹配供应商,再按顺序判断。仍缺供应商、或多件商品分属不同供应商且判定不一致的显示“待确认”,不计入扣款金额。</p></details>
|
||||
<p class="sb-rules">小额免记账:“补偿款项”“记账全款”依次用买家实付合计、订单实付金额判断,原记账金额小于50元且原记账金额 ÷ 对应实付小于40%时改为0,备注“小于50且小于0.4,记账改0”。金额等于50元、比例等于40%时不免记账;实付缺失、无效或为0的一轮不自动改0。</p>
|
||||
<p class="sb-rules">单号匹配前自动清理前后的单引号、@和空格。剩余可扣款工单按原快递单号匹配实际发货;未提供快递单号时按线上订单号匹配。查找期间为工单发起月份及前五个月(如10月取5—10月)。商品成本按商品编码匹配普通商品资料成本单价,再乘对应商品的实际发货数量;“补偿款项”“记账全款”取原记账金额与商品成本+实际运费的较小值。“运费损耗”优先取实际运费,包裹运费为空或0时按对应商品成本合计×20%替代,并在判定说明中标注。包裹运费只累计一次;缺发货、成本或逐商品数量等无法核算的工单标为待确认,不计入扣款合计。原金额及原备注保留,页面、筛选、合计和导出使用同一结果。</p>
|
||||
@if($bookkeepingError)<div class="sb-error" role="alert">{{ $bookkeepingError }}</div>@endif
|
||||
|
||||
@ -246,4 +246,43 @@ class SupplierBookkeepingPageTest extends FeatureTestCase
|
||||
$this->assertStringNotContainsString('GD-NO-COST', $csv);
|
||||
$this->assertStringNotContainsString('GD-SKIP', $csv);
|
||||
}
|
||||
|
||||
public function test_missing_supplier_is_resolved_before_rules_costing_filters_and_export(): void
|
||||
{
|
||||
$this->mockRepository([
|
||||
$this->row(['work_order_no' => 'GD-RESOLVED', 'supplier' => '', 'product_code' => '运营组合套装TEST', 'tracking_no' => 'EXP-1', 'amount' => '100.00']),
|
||||
$this->row(['work_order_no' => 'GD-SKIP', 'supplier' => '', 'product_code' => 'ZH-SKU-C-1-SKU-D-1', 'amount' => '100.00']),
|
||||
$this->row(['work_order_no' => 'GD-MIXED', 'supplier' => '', 'product_code' => 'ZH-SKU-A-1-SKU-C-1', 'amount' => '100.00']),
|
||||
$this->row(['work_order_no' => 'GD-MISSING', 'supplier' => '', 'product_code' => 'ZH-SKU-A-1-NOTFOUND-1', 'amount' => '100.00']),
|
||||
$this->row(['work_order_no' => 'GD-SMALL', 'supplier' => '', 'product_code' => 'SKU-A', 'amount' => '30.00', 'actual_paid_amount' => '100.00']),
|
||||
], 'ready', true);
|
||||
$catalog = \Mockery::mock(\Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository::class);
|
||||
$catalog->shouldReceive('normalProductsBySkuCodes')->andReturnUsing(static function ($codes) {
|
||||
$products = [];
|
||||
foreach (['SKU-A' => '新厂', 'SKU-B' => '新厂', 'SKU-C' => '尊享', 'SKU-D' => '尊享'] as $code => $supplier) {
|
||||
if (in_array($code, $codes, true)) $products[] = (object) ['autoid' => 1, 'sku_id' => $code, 'supplier_name' => $supplier];
|
||||
}
|
||||
return $products;
|
||||
});
|
||||
$this->app->instance(\Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository::class, $catalog);
|
||||
$shipping = \Mockery::mock(\Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository::class);
|
||||
$shipments = [];
|
||||
foreach (['SKU-A', 'SKU-B'] as $sku) $shipments[] = ['id' => $sku, 'source_id' => 'parcel', 'express_no' => 'EXP-1', 'trade_no' => '5127787476014026744',
|
||||
'tracking_numbers' => ['EXP-1'], 'order_numbers' => ['5127787476014026744'], 'barcode' => $sku, 'qty' => '1', 'freight' => '8.50', 'ship_time' => '2026-08-01 00:00:00'];
|
||||
$shipping->shouldReceive('shipmentsFor')->andReturn($shipments);
|
||||
$shipping->shouldReceive('readyMonths')->andReturn(['2026-04', '2026-05', '2026-06', '2026-07', '2026-08', '2026-09']);
|
||||
$shipping->shouldReceive('costsFor')->with(['SKU-A', 'SKU-B'])->andReturn(['SKU-A' => '20.00', 'SKU-B' => '30.00']);
|
||||
$this->app->instance(\Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository::class, $shipping);
|
||||
$this->loginAs()->get($this->url())->assertOk()->assertSee('实际商品编码')->assertSee('供应商匹配说明')->assertSee('SKU-B→新厂');
|
||||
$data = app(\Modules\EcommerceFinance\View\SupplierBookkeepingPageData::class)->page();
|
||||
$this->assertSame(5850, $data['bookkeepingSummary']['deduction_cents']);
|
||||
$this->assertSame(1, $data['bookkeepingSummary']['deduction_count']);
|
||||
$this->assertSame(2, $data['bookkeepingSummary']['review']);
|
||||
$this->get($this->url(['product_code' => 'SKU-B', 'supplier' => '新厂', 'deduction' => '扣款']))->assertOk()->assertSee('GD-RESOLVED')->assertDontSee('GD-MIXED');
|
||||
$csv = $this->get(route('business.supplier-bookkeeping.export', ['company_id' => $this->company->id, 'deduction' => '扣款',
|
||||
'columns' => 'work_order_no,amount,resolved_product_code,supplier,supplier_match_basis']))->assertOk()->streamedContent();
|
||||
$this->assertStringContainsString('GD-RESOLVED,58.50,', $csv);
|
||||
$this->assertStringContainsString('SKU-B→新厂', $csv);
|
||||
$this->assertStringNotContainsString('GD-MIXED', $csv);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\EcommerceFinance;
|
||||
|
||||
use Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository;
|
||||
use Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository as Shipping;
|
||||
use Modules\EcommerceFinance\Services\SupplierBookkeepingDeductionRule as Rule;
|
||||
use Modules\EcommerceFinance\Services\SupplierBookkeepingProductResolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SupplierBookkeepingProductResolverTest extends TestCase
|
||||
{
|
||||
private function row(array $patch = []): array
|
||||
{
|
||||
return array_replace(['supplier' => '', 'product_code' => 'SKU-A', 'started_at' => '2026-10-15', 'tracking_no' => 'EXP-1',
|
||||
'online_order_no' => 'ORDER-1', 'reason_level_1' => '厂家记账', 'bookkeeping_type' => '补偿款项', 'amount' => '100.00'], $patch);
|
||||
}
|
||||
|
||||
private function service(array $products, array $shipments = []): SupplierBookkeepingProductResolver
|
||||
{
|
||||
$catalog = $this->createMock(ProductCatalogMirrorRepository::class);
|
||||
$catalog->method('normalProductsBySkuCodes')->willReturnCallback(static fn ($codes) => array_values(array_filter($products,
|
||||
static fn ($product) => in_array(Shipping::sku($product->sku_id), $codes, true))));
|
||||
$shipping = $this->createMock(Shipping::class);
|
||||
$shipping->method('shipmentsFor')->willReturn($shipments);
|
||||
return new SupplierBookkeepingProductResolver($catalog, $shipping);
|
||||
}
|
||||
|
||||
private function product(string $code, string $supplier, int $id = 1): object
|
||||
{
|
||||
return (object) ['autoid' => $id, 'sku_id' => $code, 'supplier_name' => $supplier];
|
||||
}
|
||||
|
||||
public function test_exact_normal_product_wins_over_encoded_shape_and_latest_catalog_row_wins(): void
|
||||
{
|
||||
$code = 'MKH-SKU-A-1-SKU-B-1';
|
||||
$row = $this->service([$this->product($code, '当前厂', 2), $this->product($code, '旧厂'), $this->product('SKU-A', '其他厂')])
|
||||
->apply([$this->row(['product_code' => " '@mkh-sku-a-1-sku-b-1 "])])[0];
|
||||
$this->assertSame('当前厂', $row['supplier']);
|
||||
$this->assertSame($code, $row['resolved_product_code']);
|
||||
$this->assertSame(" '@mkh-sku-a-1-sku-b-1 ", $row['product_code']);
|
||||
}
|
||||
|
||||
public function test_encoded_components_use_each_catalog_supplier_before_existing_deduction_rules(): void
|
||||
{
|
||||
$row = $this->service([$this->product('SKU-A+B', '厂甲'), $this->product('GBGJ-SKU-B', '厂乙')])
|
||||
->apply([$this->row(['product_code' => 'ZH-SKU-A+B-2-GBGJ-SKU-B-1'])])[0];
|
||||
$this->assertSame("SKU-A+B\nGBGJ-SKU-B", $row['resolved_product_code']);
|
||||
$this->assertSame("厂甲\n厂乙", $row['supplier']);
|
||||
$this->assertStringContainsString('GBGJ-SKU-B→厂乙', $row['supplier_match_basis']);
|
||||
$this->assertSame(Rule::STATUS_DEDUCT, Rule::decide($row, [])['deduction_status']);
|
||||
$this->assertSame(Rule::STATUS_REVIEW, Rule::decide($row, ['厂乙' => ['mode' => Rule::MODE_NONE]])['deduction_status']);
|
||||
$this->assertSame(Rule::STATUS_SKIP, Rule::decide($row, ['厂甲' => ['mode' => Rule::MODE_NONE], '厂乙' => ['mode' => Rule::MODE_NONE]])['deduction_status']);
|
||||
$this->assertSame('0.00', Rule::decide(array_replace($row, ['amount' => '30.00', 'actual_paid_amount' => '100.00']), [])['amount']);
|
||||
}
|
||||
|
||||
public function test_unknown_component_and_latest_blank_supplier_cannot_use_partial_supplier_names(): void
|
||||
{
|
||||
foreach ([[$this->product('SKU-A', '厂甲')], [$this->product('SKU-A', '厂甲'), $this->product('SKU-B', '旧厂'), $this->product('SKU-B', '', 2)]] as $products) {
|
||||
$row = $this->service($products)->apply([$this->row(['product_code' => 'ZH-SKU-A-1-SKU-B-1'])])[0];
|
||||
$this->assertSame('', $row['supplier']);
|
||||
$this->assertStringContainsString('SKU-B→未匹配供应商', $row['supplier_match_basis']);
|
||||
$this->assertSame(Rule::STATUS_REVIEW, Rule::decide($row, [])['deduction_status']);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_named_bundle_uses_only_complete_original_shipment_references(): void
|
||||
{
|
||||
$shipment = ['tracking_numbers' => ['EXP-1'], 'order_numbers' => ['ORDER-1'], 'ship_time' => '2026-10-01 00:00:00',
|
||||
'barcode' => 'SKU-A,SKU-B', 'unshipped' => '#N/A'];
|
||||
$products = [$this->product('SKU-A', '同厂'), $this->product('SKU-B', '同厂')];
|
||||
$row = $this->service($products, [$shipment])->apply([$this->row(['product_code' => '运营组合套装123', 'tracking_no' => " '@EXP-1 "])])[0];
|
||||
$this->assertSame('同厂', $row['supplier']);
|
||||
$this->assertSame("SKU-A\nSKU-B", $row['resolved_product_code']);
|
||||
foreach ([['tracking_numbers' => ['EXP-OTHER']], ['order_numbers' => ['ORDER-OTHER']], ['ship_time' => '2026-04-30 00:00:00'], ['unshipped' => 'EXP-1']] as $patch) {
|
||||
$failed = $this->service($products, [array_replace($shipment, $patch)])->apply([$this->row(['product_code' => '运营组合套装123'])])[0];
|
||||
$this->assertSame('', $failed['supplier']);
|
||||
}
|
||||
$partial = $this->service($products, [$shipment])->apply([$this->row(['product_code' => '运营组合套装123', 'tracking_no' => 'EXP-1,EXP-2'])])[0];
|
||||
$this->assertSame('', $partial['supplier']);
|
||||
}
|
||||
|
||||
public function test_existing_supplier_is_preserved_without_reading_product_or_shipping_data(): void
|
||||
{
|
||||
$products = $this->createMock(ProductCatalogMirrorRepository::class);
|
||||
$products->expects($this->never())->method('normalProductsBySkuCodes');
|
||||
$shipping = $this->createMock(Shipping::class);
|
||||
$shipping->expects($this->never())->method('shipmentsFor');
|
||||
$rows = [$this->row(['supplier' => '工单厂家'])];
|
||||
$this->assertSame($rows, (new SupplierBookkeepingProductResolver($products, $shipping))->apply($rows));
|
||||
}
|
||||
}
|
||||
@ -54,6 +54,14 @@ class SupplierBookkeepingValuationTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_resolved_real_product_code_is_used_for_catalog_cost_instead_of_bundle_parent(): void
|
||||
{
|
||||
$row = $this->service([$this->shipment()])->apply([$this->row(['product_code' => 'ZH-SKU-A-2', 'resolved_product_code' => 'SKU-A'])])[0];
|
||||
$this->assertSame('40.00', $row['matched_cost']);
|
||||
$this->assertSame('48.50', $row['amount']);
|
||||
$this->assertSame('ZH-SKU-A-2', $row['product_code']);
|
||||
}
|
||||
|
||||
public function test_keeps_lower_or_equal_bookkeeping_amount_and_rounds_ceiling_once(): void
|
||||
{
|
||||
$service = $this->service([$this->shipment()]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user