diff --git a/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php b/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php index ab62ea30..503c8f29 100644 --- a/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php +++ b/modules/EcommerceFinance/Services/Freight/ShippingFreightCalculator.php @@ -176,6 +176,12 @@ final class ShippingFreightCalculator } } + if ($settle <= 0 && ! in_array($m, ['F', 'PERQTY'], true)) { + $d['settle_w'] = null; + + return $bad('缺少有效计费重量'); + } + switch ($m) { case '_PERKG': break; diff --git a/tests/Unit/EcommerceFinance/ShippingFreightCalculatorTest.php b/tests/Unit/EcommerceFinance/ShippingFreightCalculatorTest.php index b3df0b4e..288299a1 100644 --- a/tests/Unit/EcommerceFinance/ShippingFreightCalculatorTest.php +++ b/tests/Unit/EcommerceFinance/ShippingFreightCalculatorTest.php @@ -53,6 +53,31 @@ class ShippingFreightCalculatorTest extends TestCase $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();