fix: 调整厂家记账替代运费及组合装计件规则 #999

Merged
limeng merged 1 commits from fix/limeng-supplier-bookkeeping-rules into main 2026-11-09 09:16:56 +08:00
4 changed files with 133 additions and 32 deletions

View File

@ -119,37 +119,42 @@ class SupplierBookkeepingValuation
$packages[$package]['has_freight'] = true;
}
}
$estimatedPackages = $freightOnly ? array_filter($packages, static fn ($package) => !$package['has_freight'] || bccomp($package['amount'], '0', 6) === 0) : [];
if ($estimatedPackages !== []) {
$estimatedPackages = array_filter($packages, static fn ($package) => !$package['has_freight'] || bccomp($package['amount'], '0', 6) === 0);
if ($freightOnly && $estimatedPackages !== []) {
$shippedSkus = [];
foreach ($pool as $shipment) array_push($shippedSkus, ...array_map([Repository::class, 'sku'], Repository::numbers($shipment['barcode'])));
if (array_diff(array_keys($wanted), $shippedSkus) !== []) return $this->review($row, '原单发货明细未匹配全部工单商品,无法估算运费');
}
$packageCosts = [];
$usedBundleQuantity = false;
foreach ($pool as $shipment) {
$package = (string) $shipment['source_id'];
if ($freightOnly && !isset($estimatedPackages[$package])) continue;
if ($wanted === []) return $this->review($row, '工单没有商品编码,无法匹配替代运费所需成本');
$sku = Repository::sku($shipment['barcode']);
$components = array_map([Repository::class, 'sku'], Repository::numbers($shipment['barcode']));
if (count($components) > 1 && array_intersect(array_keys($wanted), $components) !== []) return $this->review($row, '多商品包裹只有合计数量,缺少逐商品发货数量');
if (!array_key_exists($sku, $wanted)) continue;
if (!self::decimal($shipment['qty']) || bccomp((string) $shipment['qty'], '0', 6) <= 0) {
return $this->review($row, '匹配发货数量缺失或无效');
$components = array_values(array_unique(array_map([Repository::class, 'sku'], Repository::numbers($shipment['barcode']))));
$bundle = count($components) > 1;
foreach ($components as $sku) {
if (!array_key_exists($sku, $wanted)) continue;
// 组合装导出只有合计数量,财务确认按各商品条码1件核价。
$lineQuantity = $bundle ? '1' : (string) ($shipment['qty'] ?? '');
if (!self::decimal($lineQuantity) || bccomp($lineQuantity, '0', 6) <= 0) {
return $this->review($row, '匹配发货数量缺失或无效');
}
$usedBundleQuantity = $usedBundleQuantity || $bundle;
$lineKey = $package . '|' . $sku;
if (isset($lines[$lineKey])) {
if (bccomp($lines[$lineKey], $lineQuantity, 6) !== 0) return $this->review($row, '同一包裹商品数量不一致,请核对');
continue;
}
$lines[$lineKey] = $lineQuantity;
$quantity = bcadd($quantity, $lineQuantity, 6);
$price = $costs[$sku] ?? null;
if (!self::decimal($price) || bccomp((string) $price, '0', 6) <= 0) return $this->review($row, '商品成本价缺失或为0:' . $sku);
$lineCost = bcmul((string) $price, $lineQuantity, 6);
$cost = bcadd($cost, $lineCost, 6);
$packageCosts[$package] = bcadd($packageCosts[$package] ?? '0', $lineCost, 6);
$wanted[$sku] = true;
}
$lineKey = $package . '|' . $sku;
if (isset($lines[$lineKey])) {
if (bccomp($lines[$lineKey], (string) $shipment['qty'], 6) !== 0) return $this->review($row, '同一包裹商品数量不一致,请核对');
continue;
}
$lines[$lineKey] = (string) $shipment['qty'];
$quantity = bcadd($quantity, (string) $shipment['qty'], 6);
$price = $costs[$sku] ?? null;
if (!self::decimal($price) || bccomp((string) $price, '0', 6) <= 0) return $this->review($row, '商品成本价缺失或为0:' . $sku);
$lineCost = bcmul((string) $price, (string) $shipment['qty'], 6);
$cost = bcadd($cost, $lineCost, 6);
$packageCosts[$package] = bcadd($packageCosts[$package] ?? '0', $lineCost, 6);
$wanted[$sku] = true;
}
if (!$freightOnly && in_array(false, $wanted, true)) return $this->review($row, '原单发货明细未匹配全部工单商品');
foreach (array_keys($estimatedPackages) as $package) {
@ -177,13 +182,16 @@ class SupplierBookkeepingValuation
} else {
$row['matched_cost'] = self::money($cost);
$ceiling = self::money(bcadd($cost, $freight, 6));
$freightLabel = $estimatedPackages === [] ? '实际运费' : '运费';
if (bccomp((string) $row['amount'], $ceiling, 2) > 0) {
$row['amount'] = $ceiling;
$basis = '原记账金额大于商品成本+实际运费,按成本+运费记账';
$basis = '原记账金额大于商品成本+' . $freightLabel . ',按成本+运费记账';
} else {
$basis = '原记账金额不大于商品成本+实际运费,保留记账金额';
$basis = '原记账金额不大于商品成本+' . $freightLabel . ',保留记账金额';
}
if ($estimatedPackages !== []) $basis .= ';原单运费缺失或为0,按商品成本×0.2替代运费';
}
if ($usedBundleQuantity) $basis .= ';组合装按各条码1件计';
$row['deduction_basis'] .= ';' . $basis;
$row['remark'] = trim((string) ($row['remark'] ?? '')) === '' ? $basis : $row['remark'] . "\n" . $basis;
if (bccomp((string) $row['amount'], '0', 2) === 0) $row['deduction_status'] = SupplierBookkeepingDeductionRule::STATUS_SKIP;

View File

@ -54,7 +54,7 @@
</div>
<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>
<p class="sb-rules">单号匹配前自动清理前后的单引号、@和空格。剩余可扣款工单按原快递单号匹配实际发货;未提供快递单号时按线上订单号匹配。查找期间为工单发起月份及前五个月(如10月取5—10月)。商品成本按商品编码匹配普通商品资料成本单价,再乘对应商品的实际发货数量;组合装只有合计数量、缺逐商品数量时,按各条码1件计,并在判定说明中标注。“补偿款项”“记账全款”取原记账金额与商品成本+运费的较小值;“运费损耗”按运费记账。运费优先取实际运费,包裹运费为空或0时按对应商品成本合计×20%替代,并在判定说明中标注。包裹运费只累计一次;仍缺发货、成本或非组合装发货数量等无法核算的工单标为待确认,不计入扣款合计。原金额及原备注保留,页面、筛选、合计和导出使用同一结果。</p>
@if($bookkeepingError)<div class="sb-error" role="alert">{{ $bookkeepingError }}</div>@endif
<section class="sb-panel" aria-label="厂家记账明细">
<div class="sb-table-scroll"><table><thead><tr>

View File

@ -285,4 +285,42 @@ class SupplierBookkeepingPageTest extends FeatureTestCase
$this->assertStringContainsString('SKU-B→新厂', $csv);
$this->assertStringNotContainsString('GD-MIXED', $csv);
}
public function test_bundle_and_replacement_freight_agree_on_page_totals_filters_and_export(): void
{
$this->mockRepository([
$this->row(['work_order_no' => 'GD-BUNDLE', 'bookkeeping_type' => '记账全款', 'amount' => '100.00', 'product_code' => "SKU-A\nSKU-B", 'tracking_no' => 'EXP-BUNDLE']),
$this->row(['work_order_no' => 'GD-COMPENSATION', 'amount' => '100.00', 'product_code' => 'SKU-A', 'tracking_no' => 'EXP-SINGLE']),
$this->row(['work_order_no' => 'GD-NO-SHIPMENT', 'amount' => '100.00', 'product_code' => "SKU-A\nSKU-B", 'tracking_no' => 'EXP-MISSING']),
], 'ready', true);
$shipping = \Mockery::mock(\Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository::class);
$shipping->shouldReceive('readyMonths')->andReturn(['2026-04', '2026-05', '2026-06', '2026-07', '2026-08', '2026-09']);
$shipping->shouldReceive('shipmentsFor')->andReturn([
['id' => 1, 'source_id' => 1, 'express_no' => 'EXP-BUNDLE', 'trade_no' => '5127787476014026744',
'barcode' => 'SKU-A,SKU-B', 'qty' => '9', 'freight' => '0', 'ship_time' => '2026-08-01 00:00:00'],
['id' => 2, 'source_id' => 2, 'express_no' => 'EXP-SINGLE', 'trade_no' => '5127787476014026744',
'barcode' => 'SKU-A', 'qty' => '2', 'freight' => null, 'ship_time' => '2026-08-01 00:00:00'],
]);
$shipping->shouldReceive('costsFor')->andReturn(['SKU-A' => '20.00', 'SKU-B' => '5.00']);
$this->app->instance(\Modules\EcommerceFinance\Repositories\SupplierBookkeepingValuationRepository::class, $shipping);
$this->loginAs()->get($this->url())->assertOk()
->assertSee('组合装按各条码1件计')
->assertSee('原单运费缺失或为0,按商品成本×0.2替代运费');
$data = app(\Modules\EcommerceFinance\View\SupplierBookkeepingPageData::class)->page();
$this->assertSame(7800, $data['bookkeepingSummary']['deduction_cents']);
$this->assertSame(2, $data['bookkeepingSummary']['deduction_count']);
$this->assertSame(1, $data['bookkeepingSummary']['review']);
$this->get($this->url(['deduction' => '扣款']))->assertOk()->assertSee('GD-BUNDLE')->assertSee('GD-COMPENSATION')->assertDontSee('GD-NO-SHIPMENT');
$this->get($this->url(['deduction' => '待确认']))->assertOk()->assertSee('GD-NO-SHIPMENT')->assertDontSee('GD-BUNDLE');
$csv = $this->get(route('business.supplier-bookkeeping.export', [
'company_id' => $this->company->id, 'deduction' => '扣款',
'columns' => 'work_order_no,amount,original_amount,matched_cost,matched_freight,matched_quantity,deduction_basis',
]))->assertOk()->streamedContent();
$this->assertStringContainsString('GD-BUNDLE,30.00,100.00,25.00,5.00,2,', $csv);
$this->assertStringContainsString('GD-COMPENSATION,48.00,100.00,40.00,8.00,2,', $csv);
$this->assertStringContainsString('组合装按各条码1件计', $csv);
$this->assertStringContainsString('原单运费缺失或为0,按商品成本×0.2替代运费', $csv);
$this->assertStringNotContainsString('GD-NO-SHIPMENT', $csv);
}
}

View File

@ -145,12 +145,11 @@ class SupplierBookkeepingValuationTest extends TestCase
$this->assertSame(['2025-09-01 00:00:00', '2026-03-01 00:00:00'], Repository::window(['started_at' => '2026-02-28']));
}
public function test_missing_cost_quantity_freight_or_goods_stays_pending_instead_of_silently_zeroing(): void
public function test_missing_cost_quantity_or_goods_and_invalid_freight_stay_pending(): void
{
foreach ([
[[$this->shipment()], []], [[$this->shipment()], ['SKU-A' => '0']],
[[$this->shipment(['qty' => null])], ['SKU-A' => '20']],
[[$this->shipment(['freight' => null])], ['SKU-A' => '20']],
[[$this->shipment(['freight' => '-1'])], ['SKU-A' => '20']],
[[$this->shipment(['barcode' => 'SKU-B'])], ['SKU-A' => '20']],
] as [$shipments, $costs]) {
@ -177,17 +176,74 @@ class SupplierBookkeepingValuationTest extends TestCase
$this->assertSame('100.00', $row['amount']);
}
public function test_aggregate_multi_sku_quantity_cannot_be_applied_to_each_product(): void
public function test_bundle_costs_each_matching_barcode_once_without_using_aggregate_quantity(): void
{
$row = $this->service([$this->shipment(['barcode' => 'SKU-A,SKU-B', 'qty' => '3'])])
->apply([$this->row()])[0];
$this->assertSame('待确认', $row['deduction_status']);
$this->assertStringContainsString('缺少逐商品发货数量', $row['deduction_basis']);
foreach (['3', null] as $quantity) {
$shipment = $this->shipment(['barcode' => 'SKU-A,SKU-B', 'qty' => $quantity]);
$service = $this->service([$shipment, array_replace($shipment, ['id' => 2])], ['SKU-A' => '20', 'SKU-B' => '5']);
$row = $service->apply([$this->row(['product_code' => "SKU-A\nSKU-B"])])[0];
$this->assertSame('33.50', $row['amount']);
$this->assertSame('25.00', $row['matched_cost']);
$this->assertSame('8.50', $row['matched_freight']);
$this->assertSame('2', $row['matched_quantity']);
$this->assertSame('扣款', $row['deduction_status']);
$this->assertStringContainsString('组合装按各条码1件计', $row['deduction_basis']);
$this->assertStringContainsString('组合装按各条码1件计', $row['remark']);
$partial = $service->apply([$this->row()])[0];
$this->assertSame('28.50', $partial['amount']);
$this->assertSame('1', $partial['matched_quantity']);
}
$freight = $this->service([$this->shipment(['barcode' => 'SKU-A,SKU-B', 'qty' => null])])
->apply([$this->row(['bookkeeping_type' => '运费损耗'])])[0];
$this->assertSame('8.50', $freight['amount']);
}
public function test_full_and_compensation_use_cost_based_freight_when_missing_or_zero(): void
{
foreach (['记账全款', '记账全额', '补偿款项'] as $type) {
foreach ([null, '', '0', '0.00'] as $freight) {
$service = $this->service([$this->shipment(['freight' => $freight])]);
$row = $service->apply([$this->row(['bookkeeping_type' => $type])])[0];
$this->assertSame('48.00', $row['amount']);
$this->assertSame('40.00', $row['matched_cost']);
$this->assertSame('8.00', $row['matched_freight']);
$this->assertSame('100.00', $row['original_amount']);
$this->assertSame('扣款', $row['deduction_status']);
$this->assertStringContainsString('原单运费缺失或为0,按商品成本×0.2替代运费', $row['deduction_basis']);
$this->assertStringContainsString('原单运费缺失或为0,按商品成本×0.2替代运费', $row['remark']);
foreach (['30.00', '48.00'] as $amount) {
$kept = $service->apply([$this->row(['bookkeeping_type' => $type, 'amount' => $amount])])[0];
$this->assertSame($amount, $kept['amount']);
}
}
}
}
public function test_bundle_estimates_only_missing_package_freight_and_preserves_lower_amount(): void
{
$service = $this->service([
$this->shipment(),
$this->shipment(['id' => 2, 'source_id' => 20, 'express_no' => 'EXP-2', 'barcode' => 'SKU-A,SKU-B', 'qty' => '9', 'freight' => '0']),
$this->shipment(['id' => 3, 'source_id' => 20, 'express_no' => 'EXP-2', 'barcode' => 'SKU-A,SKU-B', 'qty' => '9', 'freight' => null]),
], ['SKU-A' => '20', 'SKU-B' => '5']);
foreach (['补偿款项', '记账全款', '运费损耗'] as $type) {
$row = $service->apply([$this->row(['tracking_no' => '', 'product_code' => "SKU-A\nSKU-B", 'bookkeeping_type' => $type])])[0];
$this->assertSame($type === '运费损耗' ? '13.50' : '78.50', $row['amount']);
$this->assertSame('13.50', $row['matched_freight']);
$this->assertSame($type === '运费损耗' ? '25.00' : '65.00', $row['matched_cost']);
$this->assertSame($type === '运费损耗' ? '2' : '4', $row['matched_quantity']);
$this->assertStringContainsString('组合装按各条码1件计', $row['deduction_basis']);
}
$kept = $service->apply([$this->row(['tracking_no' => '', 'product_code' => "SKU-A\nSKU-B", 'amount' => '60.00'])])[0];
$this->assertSame('60.00', $kept['amount']);
$missingCost = $this->service([$this->shipment(['barcode' => 'SKU-A,SKU-B'])])
->apply([$this->row(['product_code' => "SKU-A\nSKU-B"])])[0];
$this->assertSame('待确认', $missingCost['deduction_status']);
$this->assertStringContainsString('商品成本价缺失或为0:SKU-B', $missingCost['deduction_basis']);
$missingShipment = $service->apply([$this->row(['tracking_no' => 'MISSING', 'product_code' => "SKU-A\nSKU-B"])])[0];
$this->assertSame('待确认', $missingShipment['deduction_status']);
}
public function test_duplicate_package_sku_rows_do_not_double_quantity_and_conflicts_require_review(): void
{
$row = $this->service([$this->shipment(), $this->shipment(['id' => 2])])->apply([$this->row()])[0];
@ -295,7 +351,6 @@ class SupplierBookkeepingValuationTest extends TestCase
foreach ([
[['freight' => null], []], [['freight' => '0'], ['SKU-A' => '0']],
[['freight' => '0', 'qty' => null], ['SKU-A' => '20']],
[['freight' => '0', 'barcode' => 'SKU-A,SKU-B'], ['SKU-A' => '20']],
[['freight' => '-1'], ['SKU-A' => '20']],
] as [$shipment, $costs]) {
$row = $this->service([$this->shipment($shipment)], $costs)->apply([$this->row(['bookkeeping_type' => '运费损耗'])])[0];