fix(逆向附加费): 支持超长超重按公斤计费 (#1773)

This commit is contained in:
limeng 2026-12-06 12:08:08 +08:00
parent fc8415421a
commit 2c4062d9e2
2 changed files with 80 additions and 0 deletions

View File

@ -104,6 +104,21 @@ final class BillSurchargeQuoteCalculator
return [['settlement_amount' => $amount, 'evidence' => $evidence . '取本票已核算发出运费 ' . number_format($amount, 2, '.', '') . ' 元']];
}
if ($rule->mode === '按尺寸/最长边') {
$combined = $this->sizeWeightRule($rule);
if ($combined !== null) {
$weight = $freight['charge_weight'] ?? null;
if (! is_numeric($weight) || (float) $weight <= 0) return '缺少已核算计费重量,无法检查超长超重费';
$sides = [(float) ($ship['length_cm'] ?? 0), (float) ($ship['width_cm'] ?? 0), (float) ($ship['height_cm'] ?? 0)];
if (min($sides) <= 0) return '发货商品缺少完整尺寸,无法检查超长超重费';
$max = max($sides);
$eligible = $max > $combined['length_cm'] || (float) $weight > $combined['weight'];
$amount = $eligible ? round(max($combined['minimum'], (float) $weight * $combined['rate']), 2) : 0.0;
return [['settlement_amount' => $amount, 'evidence' =>
'最长边 ' . $max . 'cm,计费重量 ' . $weight . 'kg;报价条件:最长边>' . $combined['length_cm']
. 'cm 或计费重量>' . $combined['weight'] . 'kg;'
. ($eligible ? 'max(' . $weight . '×' . $combined['rate'] . '元/kg,最低' . $combined['minimum'] . '元)' : '未达到条件,不计费')
. ',按票计一次']];
}
if (! preg_match('/^(?:单边(?:长)?超|最长边超过?)\s*(\d+(?:\.\d+)?)\s*(?:cm|厘米)$/iu', trim($rule->condition), $threshold)
|| ! preg_match('/^(\d+(?:\.\d+)?)\s*元?\s*\/\s*单$/u', trim($rule->standard), $price)) return '尺寸计费条件或单价尚未支持自动核验';
$sides = [(float) ($ship['length_cm'] ?? 0), (float) ($ship['width_cm'] ?? 0), (float) ($ship['height_cm'] ?? 0)];
@ -144,6 +159,20 @@ final class BillSurchargeQuoteCalculator
return $result ?: ($found ? '对应工单附加费未审核通过' : '未找到对应单号、费用类型和快递的客服费用明细');
}
/** 同一张报价明确列出超长或超重条件,按整票计费重量和最低金额收取。 */
private function sizeWeightRule(object $rule): ?array
{
$pattern = '/^最长边大于(\d+(?:\.\d+)?)米[;;]②?计费重量大于(\d+(?:\.\d+)?)KG[,,]即认定为超长超重货[,,]该单货物将在原收费基础上加收超长超重费[。.]加收标准按照计费重量[×*](\d+(?:\.\d+)?)元\/KG计算[,,]最低加收(\d+(?:\.\d+)?)元\/(?:票|单)[。.]?$/iu';
$condition = preg_replace('/\s+/u', '', $rule->condition);
$standard = preg_replace('/\s+/u', '', $rule->standard);
if (! preg_match($pattern, $condition, $c) || ! preg_match($pattern, $standard, $s)
|| array_map('floatval', array_slice($c, 1)) !== array_map('floatval', array_slice($s, 1))) return null;
if (min(array_map('floatval', array_slice($c, 1))) <= 0) return null;
return ['length_cm' => (float) $c[1] * 100, 'weight' => (float) $c[2],
'rate' => (float) $c[3], 'minimum' => (float) $c[4]];
}
private function buyerReason(object $rule, array $orders, array $confirmations, bool $requireBuyer = true, bool $allowDamage = false): array|string
{
$reasons = [];

View File

@ -175,4 +175,55 @@ final class ReverseCourierBillTest extends FeatureTestCase
$this->assertArrayNotHasKey('_unshipped', $row);
$this->assertNotNull($row['qc_freight']);
}
public function test_combined_size_weight_surcharge_uses_return_package_weight_and_quote_rate(): void
{
$text = '最长边大于2米;②计费重量大于60KG,即认定为超长超重货,该单货物将在原收费基础上加收超长超重费。加收标准按照计费重量*1元/KG计算,最低加收20元/票。';
$ruleId = DB::table('business_courier_surcharge_quotes')->insertGetId([
'company_id' => $this->company->id, 'courier' => '中通快运逆向', 'scope' => '自有快递',
'reconciler' => '测试', 'types' => json_encode(['超长费', '超重费']), 'mode' => '按尺寸/最长边',
'standard' => $text, 'condition' => $text, 'effective_at' => '2026-01-01', 'status' => 'active',
]);
$this->pickup(1, 'EDGE', [], ['grossWeight' => 60, 'length' => 200, 'width' => 1, 'height' => 1]);
$this->pickup(2, 'HEAVY', [], ['grossWeight' => 60.01, 'length' => 100, 'width' => 1, 'height' => 1]);
$this->pickup(3, 'LONG', [], ['grossWeight' => 10, 'length' => 201, 'width' => 1, 'height' => 1]);
$this->pickup(4, 'MULTI', [], ['grossWeight' => 35.2, 'length' => 100, 'width' => 1, 'height' => 1, 'returnQuantity' => 2]);
$this->pickup(5, 'MISSING', [], ['grossWeight' => 0, 'length' => 0, 'width' => 0, 'height' => 0]);
$this->pickup(6, 'CANCELLED', ['orderStatus' => 'ExpCanceled']);
$this->pickup(7, 'OTHER', ['express_company' => '德邦']);
$input = array_map(fn ($no) => ['waybill_no' => $no, 'item' => '超重费'], ['EDGE', 'HEAVY', 'LONG', 'MULTI', 'MISSING', 'CANCELLED', 'OTHER']);
$calc = app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
$result = $calc->calculate($input, 'courier', '2026-11', ['中通快运逆向']);
foreach (['EDGE' => 0, 'HEAVY' => 61, 'LONG' => 20, 'MULTI' => 71] as $no => $amount) {
$this->assertEquals($amount, $result['rows'][$no]['超重费'][0]->settlement_amount);
}
foreach (['MISSING', 'CANCELLED'] as $no) $this->assertStringContainsString('缺少已核算计费重量', $result['notes'][$no]['超重费']);
$this->assertStringContainsString('没有启用', $result['notes']['OTHER']['超重费']);
$this->assertSame('退件取件管理', app(ShippingLookup::class)->details(['MULTI'], ['中通快运逆向'])['MULTI']['data_source']);
// 一票多商品仍只计一份附加费,重复账单行不能按对方金额再次计费。
$db = DB::connection('business_mirror');
$group = app(CourierBillStore::class)->saveGroup(null, ['name' => '中通快运逆向', 'express_names' => ['中通快运逆向']]);
$statement = $db->table('courier_bill_statements')->insertGetId(['group_id' => $group, 'bill_month' => '2026-11', 'status' => 'open']);
$file = $db->table('courier_bill_files')->insertGetId(['statement_id' => $statement, 'group_id' => $group,
'bill_month' => '2026-11', 'status' => 'parsed', 'original_name' => '测试账单.xlsx', 'stored_path' => 'test.xlsx']);
foreach ([2, 3] as $line) $db->table('courier_bill_surcharges')->insert([
'file_id' => $file, 'sheet' => '附加费', 'row_no' => $line, 'waybill_no' => 'MULTI', 'item' => '超重费', 'amount' => 999,
]);
$matcher = app(\App\Support\CourierBills\SurchargeSettlementMatcher::class);
$matcher->courier($statement);
$matched = $matcher->query('courier', $statement)->orderBy('id')->get();
$this->assertEquals(71, $matched->sum('self_amount'));
$this->assertSame('重复', $matched[1]->match_status);
$this->assertEquals(76.45, app(ShippingLookup::class)->summaries(['MULTI'], ['中通快运逆向'])['MULTI']['calc_freight']);
// 单价和最低金额取报价;不一致或追加未支持的条件不得默默忽略。
$changed = str_replace(['*1元/KG', '20元/票'], ['*2元/KG', '50元/票'], $text);
DB::table('business_courier_surcharge_quotes')->where('id', $ruleId)->update(['standard' => $changed, 'condition' => $changed]);
$revised = $calc->calculate($input, 'courier', '2026-11', ['中通快运逆向']);
$this->assertEquals(122, $revised['rows']['HEAVY']['超重费'][0]->settlement_amount);
$this->assertEquals(50, $revised['rows']['LONG']['超重费'][0]->settlement_amount);
DB::table('business_courier_surcharge_quotes')->where('id', $ruleId)->update(['condition' => $changed . '并且客服通过']);
$this->assertStringContainsString('尚未支持自动核验', $calc->calculate($input, 'courier', '2026-11', ['中通快运逆向'])['notes']['HEAVY']['超重费']);
}
}