$id, 'p' => QuoteSheet::parse($vals), 'rules' => $rules, 'vals' => $vals]; } private function cfg(string $name): array { return FreightCarrierRules::for($name) + ['_name' => $name]; } private function pkg(array $over = []): array { return $over + [ 'express_name1' => '新北中通', 'receiver_state' => '江苏省', 'receiver_city' => '南京市', 'warehouse_name' => '简尚-崔桥工厂仓', 'online_weight' => 2.0, 'actual_weight' => null, 'weight_kg' => 1.0, 'length_cm' => 50, 'width_cm' => 40, 'height_cm' => 30, 'volume_cm3' => null, 'pkg_count' => '1/1', 'barcodes' => 'SJ0001N01V01', ]; } public function test_waybill_rate_uses_volume_weight_and_free_warehouses_do_not(): void { $calc = new ShippingFreightCalculator(); $q = $this->quote([['省份', '面单', '费率'], ['江苏', 3.5, 0.7], ['广东', 4, 1.1]]); // 50×40×30 ÷ 8000 = 7.5kg > 线上 2kg:按体积重 $r = $calc->calculate($this->pkg(), $q, $this->cfg('新北中通')); $this->assertSame('OK', $r['status']); $this->assertSame(7.5, $r['settle_w']); $this->assertSame(8.75, $r['fee']); $this->assertSame('面单3.5+费率0.7×7.5kg = 8.75', $r['formula']); // 免抛仓、仓库名含「配件」:不计泡,按线上重量 foreach (['常州简尚家居有限公司', '配件-山东沂海配件单仓'] as $wh) { $r = $calc->calculate($this->pkg(['warehouse_name' => $wh]), $q, $this->cfg('新北中通')); $this->assertTrue($r['free']); $this->assertSame(4.9, $r['fee']); } // 实称优先于线上重量 $r = $calc->calculate($this->pkg(['actual_weight' => 9.0, 'length_cm' => null]), $q, $this->cfg('新北中通')); $this->assertSame('实称重量', $r['base_src']); $this->assertSame(9.8, $r['fee']); } public function test_missing_weight_blocks_weight_based_quotes_but_allows_flat_per_package_price(): void { $calc = new ShippingFreightCalculator(); $pkg = $this->pkg([ 'actual_weight' => null, 'online_weight' => 0, 'weight_kg' => null, 'length_cm' => null, 'width_cm' => null, 'height_cm' => null, ]); $weightQuote = $this->quote([['省份', '面单', '费率'], ['江苏', 3.5, 0.7]]); $result = $calc->calculate($pkg, $weightQuote, $this->cfg('新北中通')); $this->assertSame('缺少有效计费重量', $result['status']); $this->assertNull($result['fee']); $this->assertNull($result['settle_w']); $pkg['volume_cm3'] = 60000; $result = $calc->calculate($pkg, $weightQuote, $this->cfg('新北中通')); $this->assertSame('OK', $result['status']); $this->assertSame(8.75, $result['fee']); $flatQuote = $this->quote([['省份', '一口价'], ['江苏', 5.2]]); $result = $calc->calculate($pkg, $flatQuote, $this->cfg('苏北极兔-滚轮')); $this->assertSame('OK', $result['status']); $this->assertSame(5.2, $result['fee']); } public function test_first_weight_with_extra_pieces_and_over_length(): void { $calc = new ShippingFreightCalculator(); $q = $this->quote([['省份', '首重3kg', '续重'], ['江苏', 7, 1.5]]); // 常州德邦:首重3kg,多件每件 3.5,单边超 160cm 加 7 $r = $calc->calculate($this->pkg(['express_name1' => '常州德邦', 'online_weight' => 5.0, 'length_cm' => 170, 'width_cm' => 10, 'height_cm' => 10, 'pkg_count' => '1/3']), $q, $this->cfg('常州德邦')); $this->assertSame('OK', $r['status']); // 5kg(体积 17000÷6000=2.83 < 5):7 + 2×1.5 + 3.5×2 + 7 $this->assertSame(24.0, $r['fee']); $this->assertSame(14.0, $r['extra']); // 免抛仓不收多件费 $r = $calc->calculate($this->pkg(['express_name1' => '常州德邦', 'warehouse_name' => '常州简尚家居有限公司', 'online_weight' => 5.0, 'length_cm' => 10, 'width_cm' => 10, 'height_cm' => 10, 'pkg_count' => '1/3']), $q, $this->cfg('常州德邦')); $this->assertSame(10.0, $r['fee']); } public function test_flat_bands_city_rows_and_per_kg_provinces(): void { $calc = new ShippingFreightCalculator(); // 重量段一口价(菏泽顺帆顺丰:不计泡) $q = $this->quote([['省份', '0-1kg', '1-2kg', '2-3kg'], ['浙江', 3, 4, 5]]); $r = $calc->calculate($this->pkg(['express_name1' => '菏泽顺帆顺丰', 'receiver_state' => '浙江省', 'online_weight' => 1.5]), $q, $this->cfg('菏泽顺帆顺丰')); $this->assertSame(4.0, $r['fee']); $r = $calc->calculate($this->pkg(['express_name1' => '菏泽顺帆顺丰', 'receiver_state' => '浙江省', 'online_weight' => 3.5]), $q, $this->cfg('菏泽顺帆顺丰')); $this->assertStringStartsWith('超出报价公斤段', $r['status']); // 城市单独报价行:深圳优先于广东 $q = $this->quote([['省份', '面单', '费率'], ['广东', 4, 1.1], ['深圳', 5.1, 1.7]]); $r = $calc->calculate($this->pkg(['receiver_state' => '广东省', 'receiver_city' => '深圳市', 'length_cm' => null]), $q, $this->cfg('新北中通')); $this->assertSame('深圳', $r['region']); $this->assertSame(8.5, $r['fee']); // 整省「X元/公斤」+ 报价说明里的面单(北京韵达) $q = $this->quote([['省份', '0-1kg', '面单', '6.01-50kg'], ['北京', 3, 2.5, 1.2], ['新疆', '15元/公斤', null, null]], 'QB-200', '面单费2元'); $r = $calc->calculate($this->pkg(['express_name1' => '北京韵达', 'receiver_state' => '新疆维吾尔自治区', 'online_weight' => 2.0, 'length_cm' => null]), $q, $this->cfg('北京韵达')); $this->assertSame(32.0, $r['fee']); $this->assertSame('面单2+该省按15元/公斤×2kg = 32.00', $r['formula']); } public function test_version_specific_coefficients_and_ddb_barcodes(): void { $calc = new ShippingFreightCalculator(); $grid = [['省份', '0-1kg', '面单', '6.01-50kg'], ['北京', 3, 2.5, 1.2]]; $pkg = $this->pkg(['express_name1' => '北京韵达', 'receiver_state' => '北京', 'online_weight' => 1.0, 'length_cm' => 100, 'width_cm' => 40, 'height_cm' => 20]); // QB-200 系数 8000:80000/8000=10kg;QB-198 按报价 10000:8kg $this->assertSame(10.0, $calc->calculate($pkg, $this->quote($grid, 'QB-200'), $this->cfg('北京韵达'))['settle_w']); $this->assertSame(8.0, $calc->calculate($pkg, $this->quote($grid, 'QB-198'), $this->cfg('北京韵达'))['settle_w']); // 洞洞板(DDB 开头条码)在景瓴小件-莒南按 9000 计泡 $q = $this->quote([['省份', '首重3kg', '续重'], ['江苏', 6, 2]]); $base = $this->pkg(['express_name1' => '景瓴十方顺丰小件-莒南', 'online_weight' => 1.0, 'length_cm' => 90, 'width_cm' => 50, 'height_cm' => 20]); $this->assertSame(15.0, $calc->calculate($base, $q, $this->cfg('景瓴十方顺丰小件-莒南'))['settle_w']); $this->assertSame(10.0, $calc->calculate(['barcodes' => 'DDB0012N01V01'] + $base, $q, $this->cfg('景瓴十方顺丰小件-莒南'))['settle_w']); } public function test_missing_quote_or_rules_explain_why(): void { $calc = new ShippingFreightCalculator(); $this->assertSame('发货日没有生效的报价', $calc->calculate($this->pkg(), null, $this->cfg('新北中通'))['status']); $this->assertSame('计费方式未配置', $calc->calculate($this->pkg(), $this->quote([['省份', '面单', '费率'], ['江苏', 3, 1]]), null)['status']); $r = $calc->calculate($this->pkg(['receiver_state' => '西藏自治区']), $this->quote([['省份', '面单', '费率'], ['江苏', 3, 1]]), $this->cfg('新北中通')); $this->assertSame('报价无此省份(西藏)', $r['status']); $this->assertNull($r['fee']); } public function test_barcode_quotes_pick_the_package_barcode_price(): void { $calc = new ShippingFreightCalculator(); $q = $this->quote([['条形码', '江苏浙江上海', '其余省份'], ['HPYD001N01V01', 6, 9], ['新疆西藏不发', null, null]]); $r = $calc->calculate($this->pkg(['express_name1' => '浩浦韵达', 'receiver_state' => '浙江省', 'barcodes' => 'HPYD001N01V01']), $q, $this->cfg('浩浦韵达')); $this->assertSame(6.0, $r['fee']); $r = $calc->calculate($this->pkg(['express_name1' => '浩浦韵达', 'receiver_state' => '四川省', 'barcodes' => 'HPYD001N01V01']), $q, $this->cfg('浩浦韵达')); $this->assertSame(9.0, $r['fee']); $r = $calc->calculate($this->pkg(['express_name1' => '浩浦韵达', 'receiver_state' => '新疆', 'barcodes' => 'HPYD001N01V01']), $q, $this->cfg('浩浦韵达')); $this->assertSame('新疆不发货', $r['status']); } public function test_jd_city_flow_quotes_use_first_15kg_and_progressive_rates(): void { $calc = new ShippingFreightCalculator(); $vals = [ ['重货标快/特快重货价格表-2026年'], ['始发区域', '始发省份', '始发城市', '目的城市', '目的省份', '目的区域', '城市-城市流向', "首重\n15kg", "费率1\n(15-60kg】", "费率1\n(60kg以上)", '备注'], ['华东', '江苏', '常州', '铜陵', '安徽', '华东', '常州铜陵', 10.8, 0.864, 0.576, '计泡值:6000'], ['华东', '江苏', '常州', '合肥', '安徽', '华东', '常州合肥', 10.8, 0.864, 0.576, null], ['华东', '江苏', '常州', '临夏州', '甘肃', '西北', '常州临夏州', 27.36, 1.488, 1.104, null], ['华东', '江苏', '常州', '兰州', '甘肃', '西北', '常州兰州', 26, 1.4, 1.0, null], ['华东', '江苏', '常州', '哈尔滨', '黑龙', '东北', '常州哈尔滨', 30.72, 1.584, 1.008, null], ]; $q = ['id' => 'QB-900', 'p' => QuoteSheet::parse($vals), 'rules' => '', 'vals' => $vals]; $this->assertNull($q['p']); $pkg = fn (array $over) => $this->pkg($over + ['express_name1' => '崔桥重货特惠', 'receiver_state' => '安徽省', 'receiver_city' => '铜陵市', 'online_weight' => null, 'weight_kg' => null, 'length_cm' => null, 'width_cm' => null, 'height_cm' => null]); $cfg = $this->cfg('崔桥重货特惠'); $this->assertSame(['mode' => 'JDCITY', 'coef' => 6000], array_intersect_key($cfg, ['mode' => 1, 'coef' => 1])); foreach ([[40.0, 32.4, '首重10.8+(40-15)kg×0.864 = 32.40'], [80.0, 61.2, '首重10.8+45kg×0.864+(80-60)kg×0.576 = 61.20'], [60.0, 49.68, '首重10.8+(60-15)kg×0.864 = 49.68'], [12.0, 10.8, '12kg≤15kg,首重10.8 = 10.80']] as [$kg, $fee, $formula]) { $r = $calc->calculate($pkg(['actual_weight' => $kg]), $q, $cfg); $this->assertSame('OK', $r['status'], (string) $kg); $this->assertSame($fee, $r['fee'], (string) $kg); $this->assertSame($formula, $r['formula']); $this->assertSame('安徽铜陵', $r['region']); } // 计泡 6000:100×60×40 ÷ 6000 = 40kg > 实重 20kg $r = $calc->calculate($pkg(['actual_weight' => 20.0, 'length_cm' => 100, 'width_cm' => 60, 'height_cm' => 40]), $q, $cfg); $this->assertSame(40.0, $r['settle_w']); $this->assertSame(32.4, $r['fee']); // 自治州全称、黑龙江省份简称都能匹配;城市未列出但全省同价按全省价;不同价则提示 $r = $calc->calculate($pkg(['actual_weight' => 40.0, 'receiver_state' => '甘肃省', 'receiver_city' => '临夏回族自治州']), $q, $cfg); $this->assertSame(['OK', 64.56, '甘肃临夏州'], [$r['status'], $r['fee'], $r['region']]); $r = $calc->calculate($pkg(['actual_weight' => 40.0, 'receiver_state' => '黑龙江省', 'receiver_city' => '哈尔滨市']), $q, $cfg); $this->assertSame(['OK', 70.32], [$r['status'], $r['fee']]); $r = $calc->calculate($pkg(['actual_weight' => 40.0, 'receiver_city' => '芜湖市']), $q, $cfg); $this->assertSame(['OK', 32.4, '安徽(全省同价)'], [$r['status'], $r['fee'], $r['region']]); $r = $calc->calculate($pkg(['actual_weight' => 40.0, 'receiver_state' => '甘肃省', 'receiver_city' => '天水市']), $q, $cfg); $this->assertSame('报价无此城市(甘肃天水市)', $r['status']); $r = $calc->calculate($pkg(['actual_weight' => 40.0, 'receiver_state' => '广东省', 'receiver_city' => '广州市']), $q, $cfg); $this->assertSame('报价无此省份(广东)', $r['status']); // 京东快运同一口径 foreach (['横林重货特惠', '横林京东特惠', '崔桥京东特惠', '睢宁重货特惠', '横林京东快运', '崔桥京东快运', '苏北京东快运', '曹县京东快运'] as $name) { $this->assertSame('JDCITY', FreightCarrierRules::for($name)['mode'], $name); } } public function test_new_quotes_added_on_2026_12_01(): void { $calc = new ShippingFreightCalculator(); $pkg = fn (string $name, string $state, float $w, array $over = []) => $this->pkg($over + ['express_name1' => $name, 'receiver_state' => $state, 'online_weight' => $w, 'weight_kg' => null, 'length_cm' => null, 'width_cm' => null, 'height_cm' => null]); // 淼缘韵达:和淼缘中通快运同版式;海南写成「面单5+10/kg」 $my = $this->quote([ ['省份', null, '0-1kg', '1.01-3kg', '3.01-5kg', '5.01-10kg', null, '10.01-30kg', null, '备注'], [null, null, '一口价', '一口价', '一口价', '面单费', '元/kg', '面单费', '元/kg'], ['湖北', null, 3, 5, 10.5, 4, 2, 5, 2], ['海南', null, '面单5+10/kg'], ]); $cfg = $this->cfg('淼缘韵达'); $this->assertSame(10.5, $calc->calculate($pkg('淼缘韵达', '湖北省', 4.5), $my, $cfg)['fee']); $this->assertSame(18.0, $calc->calculate($pkg('淼缘韵达', '湖北省', 7), $my, $cfg)['fee']); $r = $calc->calculate($pkg('淼缘韵达', '海南省', 3), $my, $cfg); $this->assertSame(['OK', 35.0], [$r['status'], $r['fee']]); // 雄部落韵达:1-10kg 整公斤段一口价,10kg 以上 首重+(重量-1)×费率;计泡 9000;北京每票+1 $xbl = $this->quote([ ['省份', '1KG', '2KG', '3KG', '4KG', '5KG', '6KG', '7KG', '8KG', '9KG', '10KG', '10.01KG以上费率', '首重/1KG', '备注'], ['广东', 3, 4, 4.8, 7, 8, 9, 10, 11, 12, 13, 1, 4, null], ['北京', 3, 4, 4.8, 9, 10.5, 12, 13.5, 15, 16.5, 18, 1.5, 4.5, null], ]); $cfg = $this->cfg('雄部落韵达'); $this->assertSame(4.8, $calc->calculate($pkg('雄部落韵达', '广东省', 2.3), $xbl, $cfg)['fee']); $this->assertSame(15.3, $calc->calculate($pkg('雄部落韵达', '广东省', 12.3), $xbl, $cfg)['fee']); $this->assertSame(5.0, $calc->calculate($pkg('雄部落韵达', '北京', 2), $xbl, $cfg)['fee']); $r = $calc->calculate($pkg('雄部落韵达', '广东省', 2, ['length_cm' => 60, 'width_cm' => 50, 'height_cm' => 30]), $xbl, $cfg); $this->assertSame([10.0, 13.0], [$r['settle_w'], $r['fee']]); // 60×50×30÷9000 = 10kg // 帝富尼顺丰:首重20kg + (重量-20)×费率,按第一档;计泡 12000 $dfn = $this->quote([ ['公斤段', '20kg+(5万-10万区间)', null, '20kg+(10万以上区间)', null, '备注'], ['省份', '首重', '费率', '首重', '费率'], ['江苏', 43, 2.01, 42, 1.91], ]); foreach (['帝富尼顺丰快运', '帝富尼顺丰干配'] as $name) { $this->assertSame(43.0, $calc->calculate($pkg($name, '江苏省', 15), $dfn, $this->cfg($name))['fee']); $this->assertSame(74.16, $calc->calculate($pkg($name, '江苏省', 35.5), $dfn, $this->cfg($name))['fee']); } // 莲强:首重10kg + (重量-10)×费率,不计泡 $lq = $this->quote([['省份', '首重10kg', '费率', '备注'], ['河北', 26.5, 2.2, null]]); foreach (['莲强顺丰干配', '莲强韵达'] as $name) { $this->assertSame(26.5, $calc->calculate($pkg($name, '河北省', 8, ['length_cm' => 100, 'width_cm' => 100, 'height_cm' => 100]), $lq, $this->cfg($name))['fee']); $this->assertSame(55.1, $calc->calculate($pkg($name, '河北省', 23), $lq, $this->cfg($name))['fee']); } } }