CaiWuXiTong/tests/Unit/EcommerceFinance/ShippingFreightCalculatorTest.php
limeng 1cf60dd7b0 feat(发货数据): 按报价管理的报价核算运费,结果显示在发货数据页面
从 2026-11-01 起,按发货日在报价管理里生效的报价版本(与报价管理「当前生效」同一判断:未作废、
已审核/签署生效、执行期间覆盖发货日,取执行日期最新)逐包裹核算运费,写入 jst_oms_shipping_freight。
- 结算重量:实称>线上>重量;按各快递计泡系数取 max(重量, 长×宽×高÷系数);免抛仓(含「配件」仓)不计泡、不收多件费;
  超长费计入,其他附加费不计;城市单独报价、整省「X元/公斤」、洞洞板 9000、个别版本系数等按 10 月逐单核对的口径;
- 计算引擎移植自 10 月离线核算脚本,10 月 29.5 万个包裹逐单比对结果一致(仅 x.xx5 边界按正确四舍五入进位);
- 发货数据同步完成后核算该窗口;报价版本新增/作废/签署归档后按该快递从执行日期起自动重算;
- 新命令 jushuitan:calc-freight --from --to [--carrier];
- 发货数据页面/导出新增「报价核算」列:核算运费、核算-聚水潭运费、报价版本、结算重量依据、核算结算重量、
  计费重量、首重、续重/费率、面单、一口价、多件/超长加收、核算公式、核算状态(合并口径每包裹一行,明细只在第一行)。

同时修复发货数据同步 11 月起频繁失败:副本库查发货视图超时后报 20047 DBPROCESS is dead,
原重试只识别 20003/dbnextrow,整次同步失败(11-17 只同步到 62%)。现把 20047 视为可重连重试,
发货视图每批出库单由 300 降到 100。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014nvGZkg27YMEdhry6eiuwf
2026-11-18 13:55:40 +08:00

133 lines
8.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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_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']);
}
}