From ccaf0c51bb9c068a2835a863c1d40eefadfdb6bb Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Tue, 1 Dec 2026 18:50:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A1=A5=E9=BD=90=E5=BF=AB=E9=80=92?= =?UTF-8?q?=E6=8A=A5=E4=BB=B7=E8=AE=A1=E8=B4=B9=E6=96=B9=E5=BC=8F=E5=B9=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=A1=AE=E8=AE=A4=E7=9A=84=E9=9B=B6=E8=BF=90?= =?UTF-8?q?=E8=B4=B9=E5=9C=B0=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Freight/FreightCarrierRules.php | 38 +++++ .../Services/Freight/FreightQuoteRoutes.php | 102 +++++++++++++ .../Freight/ShippingFreightCalculator.php | 71 ++++++++- .../MissingCourierBillingRulesTest.php | 142 ++++++++++++++++++ 4 files changed, 350 insertions(+), 3 deletions(-) create mode 100644 modules/EcommerceFinance/Services/Freight/FreightQuoteRoutes.php create mode 100644 tests/Unit/EcommerceFinance/MissingCourierBillingRulesTest.php diff --git a/modules/EcommerceFinance/Services/Freight/FreightCarrierRules.php b/modules/EcommerceFinance/Services/Freight/FreightCarrierRules.php index 0dc9a458..17d0b966 100644 --- a/modules/EcommerceFinance/Services/Freight/FreightCarrierRules.php +++ b/modules/EcommerceFinance/Services/Freight/FreightCarrierRules.php @@ -32,6 +32,35 @@ final class FreightCarrierRules public static function all(): array { return [ + '尚然极兔' => [ + 'mode' => 'E', 'coef' => 8000, + // 原表将 2-3、3-4 保存为 Excel 日期序列,仅修正这两家表头。 + 'header_aliases' => ['44595' => '2-3kg', '44624' => '3-4kg'], + ], + '尚然申通' => [ + 'mode' => 'E', 'coef' => 8000, + 'header_aliases' => ['44595' => '2-3kg', '44624' => '3-4kg'], + ], + '森百禾韵达快运' => self::senbaihe(), + '森百禾中通快运' => self::senbaihe(), + '森百禾顺丰干配' => self::senbaihe(), + '森百禾德邦快运' => self::senbaihe(), + '森百禾百世快运' => self::senbaihe(), + '常州武进' => ['mode' => 'A', 'coef' => 6000, 'add' => 1, 'add_label' => '每票加收'], + '9999' => [ + 'mode' => 'C2', 'coef' => 9000, 'first' => 5, 'minw' => 5, + 'fc' => 1, 'rc' => 2, 'rate_break' => [20, 3], 'max_weight' => 50, 'max_length' => 160, + ], + '常州方圆' => [ + 'mode' => 'C2', 'coef' => 9000, 'first' => 5, 'minw' => 5, + 'fc' => 1, 'rc' => 2, 'rate_break' => [20, 3], 'max_weight' => 50, 'max_length' => 160, + ], + '好省邮顺心捷达' => [ + 'mode' => 'ROUTE_C2', 'coef' => 8000, 'first' => 20, 'minw' => 20, + 'fc' => 2, 'rc' => 3, 'rate_break' => [70, 4], + ], + '好省邮顺丰' => ['mode' => 'CITY_FIRST', 'coef' => 6000], + '好省邮百世' => ['mode' => 'ROUTE_TIERS', 'coef' => null, 'max_weight' => 130], '新北中通' => [ 'mode' => 'A', 'coef' => 8000, @@ -1278,6 +1307,15 @@ final class FreightCarrierRules ]; } + private static function senbaihe(): array + { + // 用户确认:所列省份为每单总运费,价格仍取当前生效报价单元格。 + return ['mode' => 'PERQTY', 'unlisted_price' => 0, 'excluded_destinations' => [ + '新疆' => [], '西藏' => [], '安徽' => ['黄山', '绩溪'], + '青海' => ['玉树'], '云南' => ['大理'], + ]]; + } + /** @return array|null */ public static function for(string $carrier): ?array { diff --git a/modules/EcommerceFinance/Services/Freight/FreightQuoteRoutes.php b/modules/EcommerceFinance/Services/Freight/FreightQuoteRoutes.php new file mode 100644 index 00000000..bae3af49 --- /dev/null +++ b/modules/EcommerceFinance/Services/Freight/FreightQuoteRoutes.php @@ -0,0 +1,102 @@ + preg_quote($p, '/'), QuoteSheet::PROVINCES)); + if (preg_match('/(?:^|[-—\s])(' . $names . ')/u', $warehouse, $match)) { + return $match[1]; + } + + return ''; + } + + /** @return array{0:?array,1:?string,2:?string} */ + public static function provinceRow(array $vals, array $pkg): array + { + $origin = self::originProvince($pkg); + $destination = QuoteSheet::province($pkg['receiver_state'] ?? ''); + if ($origin === '') { + return [null, null, '缺少明确的始发省份,无法匹配源目报价']; + } + $label = $origin . '→' . $destination; + $rows = []; + foreach ($vals as $row) { + if (is_array($row) && QuoteSheet::province($row[0] ?? '') === $origin + && QuoteSheet::province($row[1] ?? '') === $destination) { + $rows[] = array_map([QuoteSheet::class, 'num'], $row); + } + } + + return self::unique($rows, $label, '报价无此始发省与目的省组合(' . $label . ')'); + } + + /** @return array{0:?array,1:?string,2:?string} */ + public static function cityRow(array $vals, array $pkg): array + { + $province = QuoteSheet::province($pkg['receiver_state'] ?? ''); + $city = self::city((string) ($pkg['receiver_city'] ?? '')); + if ($city === '') { + return [null, null, '缺少收货城市,无法匹配城市报价']; + } + $rows = []; + $sameCityRows = []; + foreach ($vals as $row) { + if (! is_array($row) || QuoteSheet::province($row[0] ?? '') !== $province) { + continue; + } + if (trim((string) ($row[1] ?? '')) === '同城') { + $sameCityRows[] = array_map([QuoteSheet::class, 'num'], $row); + continue; + } + foreach (preg_split('/[,,、\/\n]+/u', (string) ($row[1] ?? '')) as $name) { + if (self::city($name) === $city) { + $rows[] = array_map([QuoteSheet::class, 'num'], $row); + break; + } + } + } + if ($sameCityRows !== []) { + $originProvince = self::originProvince($pkg); + $originCity = self::city((string) ($pkg['sender_city'] ?? $pkg['origin_city'] ?? '')); + if ($originProvince === '' || ($originProvince === $province && $originCity === '')) { + return [null, null, '缺少明确的始发城市,无法区分同城报价']; + } + if ($originProvince === $province && $originCity === $city) { + return self::unique($sameCityRows, $province . $city . '(同城)', '同城报价缺失'); + } + } + + return self::unique($rows, $province . $city, '报价无此城市(' . $province . $city . ')'); + } + + private static function city(string $city): string + { + // 甘孜州/甘孜藏族自治州、阿坝州/阿坝藏族羌族自治州等使用同一行政地名。 + return (string) preg_replace('/(?:藏族羌族|蒙古族藏族|哈尼族彝族|傣族景颇族|苗族侗族|布依族苗族|土家族苗族|蒙古族|朝鲜族|哈萨克|藏族|彝族|傣族|回族|白族|壮族)?(?:自治州|地区|市|州|盟)$/u', '', trim($city)); + } + + private static function unique(array $rows, string $label, string $missing): array + { + if ($rows === []) { + return [null, $label, $missing]; + } + // 地区列之后的完整报价必须一致,重复行才允许合并。 + $prices = array_unique(array_map(static fn ($row) => serialize(array_slice($row, 2)), $rows)); + if (count($prices) > 1) { + return [null, $label, '报价地区匹配不唯一(' . $label . ')']; + } + + return [$rows[0], $label, null]; + } +} diff --git a/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php b/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php index 48826fa5..52d0c25b 100644 --- a/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php +++ b/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php @@ -32,7 +32,8 @@ final class ShippingFreightCalculator return $bad('计费方式未配置'); } $jdCity = ($cfg['mode'] ?? '') === 'JDCITY'; - if ($quote === null || ($quote['p'] === null && ! $jdCity)) { + $routeTable = in_array($cfg['mode'] ?? '', ['ROUTE_C2', 'ROUTE_TIERS', 'CITY_FIRST'], true); + if ($quote === null || ($quote['p'] === null && ! $jdCity && ! $routeTable)) { return $bad('发货日没有生效的报价'); } $name = (string) ($cfg['_name'] ?? ''); @@ -58,6 +59,11 @@ final class ShippingFreightCalculator $p = $quote['p'] ?? ['head' => [], 'rows' => [], 'top' => '']; $head = $p['head']; + foreach ($head as &$label) { + $key = is_numeric($label) ? (string) (float) $label : $label; + $label = $cfg['header_aliases'][$key] ?? $label; + } + unset($label); $ck = null; foreach (QuoteSheet::CITY_KEYS as $k) { if ($city !== '' && str_contains($city, $k) && array_key_exists($k, $p['rows'])) { @@ -66,6 +72,7 @@ final class ShippingFreightCalculator } } $row = $ck !== null ? $p['rows'][$ck] : ($p['rows'][$prov] ?? null); + $unlistedPrice = false; $d['region'] = $ck ?? $prov; $rowMissing = "报价无此省份({$prov})"; if ($jdCity) { @@ -73,6 +80,27 @@ final class ShippingFreightCalculator $d['region'] = $label ?? $prov; $rowMissing = $error ?? $rowMissing; } + if ($routeTable) { + [$row, $label, $error] = ($cfg['mode'] === 'CITY_FIRST') + ? FreightQuoteRoutes::cityRow((array) ($quote['vals'] ?? []), $pkg) + : FreightQuoteRoutes::provinceRow((array) ($quote['vals'] ?? []), $pkg); + $d['region'] = $label ?? $prov; + $rowMissing = $error ?? $rowMissing; + } + if (array_key_exists($prov, $cfg['excluded_destinations'] ?? [])) { + $excluded = $cfg['excluded_destinations'][$prov]; + $destination = implode(' ', [$city, $pkg['receiver_district'] ?? '', $pkg['receiver_address'] ?? '']); + if ($excluded === [] || array_filter($excluded, static fn ($place) => str_contains($destination, $place)) !== []) { + return $bad('报价注明该地区不发货'); + } + if ($city === '' && empty($pkg['receiver_district']) && empty($pkg['receiver_address'])) { + return $bad('缺少收货城市或区县,无法排除报价不发地区'); + } + } + if ($row === null && array_key_exists('unlisted_price', $cfg) && in_array($prov, QuoteSheet::PROVINCES, true)) { + $row = [$prov, (float) $cfg['unlisted_price']]; + $unlistedPrice = true; + } $on = self::pos($pkg['online_weight'] ?? null); $act = self::pos($pkg['actual_weight'] ?? null); @@ -101,6 +129,12 @@ final class ShippingFreightCalculator if ($row === null) { return $bad($rowMissing); } + if (isset($cfg['max_weight']) && $settle > $cfg['max_weight']) { + return $bad('超出报价限重(' . self::fmt($cfg['max_weight']) . 'kg)'); + } + if (isset($cfg['max_length']) && max($L, $W, $H) > $cfg['max_length']) { + return $bad('超出报价限长(' . self::fmt($cfg['max_length']) . 'cm)'); + } $w = $settle; $minw = null; if (! empty($cfg['minw']) && $w < $cfg['minw']) { @@ -502,10 +536,12 @@ final class ShippingFreightCalculator [$fee, $F] = $lin($md, $b, $w); } break; + case 'ROUTE_C2': case 'C2': $fk = (float) $cfg['first']; $a = $g((int) $cfg['fc']); - $b = $g((int) $cfg['rc']); + $rc = isset($cfg['rate_break']) && $w > $cfg['rate_break'][0] ? $cfg['rate_break'][1] : $cfg['rc']; + $b = $g((int) $rc); if (! $need($a, $b)) { return $bad('报价单元格无数值(' . self::show($a) . ',' . self::show($b) . ')'); } @@ -518,6 +554,32 @@ final class ShippingFreightCalculator $F = "首重{$f($a)}+({$f($w)}-{$f($fk)})kg×续重{$f($b)}"; } break; + case 'ROUTE_TIERS': + [$fk, $fc, $rc] = $w < 20 ? [10.0, 3, 4] : ($w < 30 ? [20.0, 5, 6] : [30.0, 7, 8]); + $a = $g($fc); + $b = $g($rc); + if (! $need($a, $b)) { + return $bad('报价单元格无数值'); + } + [$d['first_kg'], $d['first_price'], $d['rate']] = [$fk, $a, $b]; + $w = max($fk, $w); + $fee = $a + ($w - $fk) * $b; + $F = "首重{$f($fk)}kg价{$f($a)}+({$f($w)}-{$f($fk)})kg×续重{$f($b)}"; + break; + case 'CITY_FIRST': + $offset = $w <= $g(4) ? 0 : 5; + [$fk, $upper, $a, $b] = [$g(3 + $offset), $g(4 + $offset), $g(5 + $offset), $g(6 + $offset)]; + if (! $need($fk, $upper, $a, $b)) { + return $bad('报价单元格无数值'); + } + if ($w > $upper) { + return $bad('超出报价公斤段'); + } + [$d['first_kg'], $d['first_price'], $d['rate']] = [$fk, $a, $b]; + $w = max($fk, $w); + $fee = $a + ($w - $fk) * $b; + $F = "首重{$f($fk)}kg价{$f($a)}+({$f($w)}-{$f($fk)})kg×续重{$f($b)}"; + break; case 'LNSF': $r = $this->luoningSf($d, $quote, $prov, $city, $w); if (isset($r['error'])) { @@ -683,6 +745,9 @@ final class ShippingFreightCalculator $d['flat'] = $v; $fee = $v; $F = "一口价{$f($v)}/单(按包裹计,不乘件数)"; + if ($unlistedPrice) { + $F = '未列省份按确认价,' . $F; + } break; case 'YT2': if ($w <= 3) { @@ -840,7 +905,7 @@ final class ShippingFreightCalculator if (! empty($cfg['add'])) { $fee += $cfg['add']; $d['extra'] += $cfg['add']; - $F .= "+超长{$f($cfg['add'])}"; + $F .= '+' . ($cfg['add_label'] ?? '超长') . $f($cfg['add']); } if (! empty($cfg['ceil']) && $m !== 'MDJT') { $F .= '(重量向上取整)'; diff --git a/tests/Unit/EcommerceFinance/MissingCourierBillingRulesTest.php b/tests/Unit/EcommerceFinance/MissingCourierBillingRulesTest.php new file mode 100644 index 00000000..ddb20787 --- /dev/null +++ b/tests/Unit/EcommerceFinance/MissingCourierBillingRulesTest.php @@ -0,0 +1,142 @@ +calculate($package + [ + 'express_name1' => $name, 'receiver_state' => '江苏省', 'receiver_city' => '南京市', + 'warehouse_name' => '测试仓', 'actual_weight' => $weight, + ], ['id' => 'TEST-QUOTE', 'p' => QuoteSheet::parse($values), 'vals' => $values, 'rules' => ''], + FreightCarrierRules::for($name) + ['_name' => $name]); + } + + public function test_shangran_date_headers_keep_all_weight_bands_and_do_not_extrapolate(): void + { + $values = [['省份', '0-2kg', 44595, 44624, '4.01-5', '5.01-6', '6.01-7', '备注'], + ['江苏', 3, 4, 6, 8, 10, 12, '测试报价']]; + foreach (['尚然极兔', '尚然申通'] as $name) { + foreach ([[2, 3], [2.001, 4], [3, 4], [3.001, 6], [4, 6], [4.001, 8], [5, 8], [6, 10], [7, 12]] as [$w, $fee]) { + $r = $this->calculate($name, $values, $w); + $this->assertSame('OK', $r['status']); + $this->assertSame((float) $fee, $r['fee']); + } + $this->assertStringStartsWith('超出报价公斤段', $this->calculate($name, $values, 7.01)['status']); + $r = $this->calculate($name, $values, 2, ['length_cm' => 40, 'width_cm' => 40, 'height_cm' => 20]); + $this->assertSame(4.0, $r['settle_w']); + $this->assertSame(6.0, $r['fee']); + $this->assertNull($this->calculate($name, $values, 0)['fee']); + } + } + + public function test_senbaihe_charges_one_package_and_keeps_exclusions_unpriced(): void + { + $values = [['省份', '一口价(加收)', '备注'], ['内蒙古', 17, '测试报价'], ['青海', 19], ['云南', 21]]; + foreach (['森百禾韵达快运', '森百禾中通快运', '森百禾顺丰干配', '森百禾德邦快运', '森百禾百世快运'] as $name) { + $r = $this->calculate($name, $values, 0, ['receiver_state' => '内蒙古自治区', 'pkg_count' => '1/8']); + $this->assertSame('OK', $r['status']); + $this->assertSame(17.0, $r['fee']); + foreach ([['青海', '玉树藏族自治州'], ['云南', '大理白族自治州'], ['新疆', '乌鲁木齐'], ['安徽', '黄山市']] as [$p, $c]) { + $r = $this->calculate($name, $values, 3, ['receiver_state' => $p, 'receiver_city' => $c]); + $this->assertSame('报价注明该地区不发货', $r['status']); + $this->assertNull($r['fee']); + } + $r = $this->calculate($name, $values, 3); + $this->assertSame(0.0, $r['fee']); + $this->assertSame('OK', $r['status']); + $this->assertStringContainsString('未列省份按确认价', $r['formula']); + $this->assertNull($this->calculate($name, $values, 3, ['receiver_state' => ''])['fee']); + $r = $this->calculate($name, $values, 0, ['receiver_state' => '青海', 'receiver_city' => '']); + $this->assertStringStartsWith('缺少收货城市', $r['status']); + } + } + + public function test_wujin_applies_volume_and_one_fixed_surcharge(): void + { + $values = [['省份', '面单', '费率(元/kg)'], ['江苏', 4, 2]]; + $r = $this->calculate('常州武进', $values, 2, ['volume_cm3' => 30000]); + $this->assertSame(15.0, $r['fee']); + $this->assertSame(5.0, $r['settle_w']); + $this->assertSame(1.0, $r['extra']); + $this->assertStringContainsString('+每票加收1', $r['formula']); + $r = $this->calculate('常州武进', $values, 2, ['volume_cm3' => 30000, 'warehouse_name' => '常州简尚家居有限公司']); + $this->assertSame(9.0, $r['fee']); + } + + public function test_five_kg_first_weight_selects_rate_by_total_weight_without_rounding(): void + { + $values = [['省份', '首重5kg(元)', '续重0-20kg', '续重20kg以上'], ['江苏', 10, 2, 3]]; + foreach (['9999', '常州方圆'] as $name) { + foreach ([[1, 10], [5, 10], [5.25, 10.5], [20, 40], [20.5, 56.5], [50, 145]] as [$w, $fee]) { + $r = $this->calculate($name, $values, $w); + $this->assertSame('OK', $r['status']); + $this->assertSame((float) $fee, $r['fee']); + } + $this->assertSame('超出报价限重(50kg)', $this->calculate($name, $values, 50.01)['status']); + $this->assertSame('超出报价限长(160cm)', $this->calculate($name, $values, 8, ['length_cm' => 161])['status']); + $r = $this->calculate($name, $values, 2, ['volume_cm3' => 180000]); + $this->assertSame(40.0, $r['fee']); + } + } + + public function test_route_first_weight_uses_both_provinces_and_seventy_kg_boundary(): void + { + $values = [['始发省', '省份', '首重20KG', '费率21-70KG', '费率70KG以上', '备注'], + ['安徽', '江苏', 30, 2, 1, ''], ['浙江', '江苏', 50, 3, 2, '']]; + foreach ([[10, 30], [20, 30], [70, 130], [71, 81]] as [$w, $fee]) { + $r = $this->calculate('好省邮顺心捷达', $values, $w, ['warehouse_name' => '工厂-安徽测试仓']); + $this->assertSame((float) $fee, $r['fee']); + $this->assertSame('安徽→江苏', $r['region']); + } + $this->assertSame(80.0, $this->calculate('好省邮顺心捷达', $values, 30, ['sender_state' => '浙江省'])['fee']); + $this->assertNull($this->calculate('好省邮顺心捷达', $values, 30)['fee']); + $this->assertNull($this->calculate('好省邮顺心捷达', $values, 30, ['sender_state' => '广东'])['fee']); + $values[] = ['安徽', '江苏', 31, 2, 1, '']; + $this->assertStringStartsWith('报价地区匹配不唯一', $this->calculate('好省邮顺心捷达', $values, 30, ['sender_state' => '安徽'])['status']); + } + + public function test_baishi_tiers_switch_at_twenty_thirty_and_stop_at_one_hundred_thirty(): void + { + $values = [['出发省', '到达省', '组合', '0-20', '', '20-30', '', '30-130', '', '备注'], + ['', '', '', '首重10', '费率', '首重20', '费率', '首重30', '费率', ''], + ['浙江', '江苏', '浙江江苏', 20, 2, 50, 3, 90, 4, '']]; + foreach ([[1, 20], [10, 20], [19, 38], [20, 50], [29, 77], [30, 90], [130, 490]] as [$w, $fee]) { + $r = $this->calculate('好省邮百世', $values, $w, ['sender_state' => '浙江省', 'volume_cm3' => 9000000]); + $this->assertSame((float) $fee, $r['fee']); + $this->assertNull($r['coef']); + } + $this->assertSame('超出报价限重(130kg)', $this->calculate('好省邮百世', $values, 131, ['sender_state' => '浙江'])['status']); + } + + public function test_sf_city_groups_same_city_and_weight_tiers_are_unambiguous(): void + { + $values = [['目的省份', '目的城市', '轻抛系数', '首重(kg)', '最大重量(kg)', '首重运费', '费率', '轻抛系数', '首重(kg)', '最大重量(kg)', '首重运费', '费率', '备注'], + ['四川', '同城', 6000, 20, 60, 20, 1, 6000, 60, 9999, 60, 2, ''], + ['四川', '成都市/资阳市,乐山市', 6000, 20, 60, 30, 2, 6000, 60, 9999, 110, 3, ''], + ['四川', '甘孜州', 6000, 20, 60, 40, 3, 6000, 60, 9999, 160, 4, ''], + ['江苏省', '南京市,苏州市', 6000, 20, 60, 50, 4, 6000, 60, 9999, 210, 5, '']]; + foreach ([[10, 30], [20, 30], [60, 110], [61, 113]] as [$w, $fee]) { + $r = $this->calculate('好省邮顺丰', $values, $w, + ['receiver_state' => '四川省', 'receiver_city' => '成都市', 'sender_state' => '江苏']); + $this->assertSame((float) $fee, $r['fee']); + } + $r = $this->calculate('好省邮顺丰', $values, 21, + ['receiver_state' => '四川', 'receiver_city' => '成都', 'sender_state' => '四川', 'sender_city' => '成都市']); + $this->assertSame(21.0, $r['fee']); + $this->assertStringContainsString('同城', $r['region']); + $r = $this->calculate('好省邮顺丰', $values, 20, + ['receiver_state' => '四川', 'receiver_city' => '甘孜藏族自治州', 'sender_state' => '浙江']); + $this->assertSame(40.0, $r['fee']); + $r = $this->calculate('好省邮顺丰', $values, 20, ['receiver_state' => '四川', 'receiver_city' => '成都市']); + $this->assertStringStartsWith('缺少明确的始发城市', $r['status']); + $this->assertNull($this->calculate('好省邮顺丰', $values, 20, ['receiver_city' => '徐州市'])['fee']); + $this->assertNull($this->calculate('好省邮顺丰', $values, 20, ['receiver_city' => ''])['fee']); + } +}