158 lines
9.2 KiB
PHP
158 lines
9.2 KiB
PHP
<?php
|
||
|
||
namespace Tests\Unit\EcommerceFinance;
|
||
|
||
use Modules\EcommerceFinance\Services\Freight\FreightCarrierRules;
|
||
use Modules\EcommerceFinance\Services\Freight\QuoteSheet;
|
||
use Modules\EcommerceFinance\Services\Freight\ShippingFreightCalculator;
|
||
use PHPUnit\Framework\TestCase;
|
||
|
||
class ShippingFreightCalculatorTest extends TestCase
|
||
{
|
||
private function quote(array $vals, string $id = 'QB-001', string $rules = ''): array
|
||
{
|
||
return ['id' => $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']);
|
||
}
|
||
}
|