CaiWuXiTong/tests/Feature/Business/ShippingFreightServiceTest.php

410 lines
27 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\Feature\Business;
use App\Models\Business\CourierQuoteChange;
use App\Models\Business\CourierQuoteVersion;
use App\Support\ReturnRouteFreightCalculator;
use App\Support\ReverseLogisticsQuoteSnapshot;
use Illuminate\Support\Facades\DB;
use Modules\EcommerceFinance\Repositories\ShippingOrderRepository;
use Modules\EcommerceFinance\Services\Freight\ShippingFreightService;
use Tests\Feature\FeatureTestCase;
class ShippingFreightServiceTest extends FeatureTestCase
{
public function test_return_carriers_use_reverse_quotes_and_persist_the_package_total(): void
{
$path = $this->bindReturnQuotes();
try {
$db = DB::connection('business_mirror');
$base = ['warehouse_name'=>'简尚-山东青岗集仓','ship_time'=>'2026-11-03 12:00:00',
'receiver_state'=>'山东省','receiver_city'=>'青岛市','receiver_address'=>'测试路',
'pkg_count'=>'1/2','qty'=>1,'barcode'=>'TEST-RETURN'];
$rows = [
$base+['source_id'=>101,'sku_source_id'=>1,'express_no'=>'TEST-SY','express_name1'=>'常州松宇货运',
'actual_weight'=>389,'volume_cm3'=>1000000,'m_volume_cm3'=>1000000,'length_cm'=>1000],
$base+['source_id'=>101,'sku_source_id'=>2,'express_no'=>'TEST-SY','express_name1'=>'常州松宇货运',
'actual_weight'=>null,'volume_cm3'=>1,'m_volume_cm3'=>896390,'length_cm'=>1000],
array_replace($base,['source_id'=>102,'sku_source_id'=>1,'express_no'=>'TEST-ZT','express_name1'=>'中铁客退',
'receiver_city'=>null,'receiver_name'=>'测试青岛厂','online_weight'=>389,'volume_cm3'=>1896390]),
];
foreach ($rows as $row) $db->table('jst_oms_shipping')->insert($row);
$result = app(ShippingFreightService::class)->recalculate('2026-11-01','2026-12-01',['常州松宇货运','中铁客退']);
$this->assertSame(['packages'=>2,'ok'=>2],$result);
$songyu=$db->table('jst_oms_shipping_freight')->where('source_id',101)->sole();
$this->assertEquals(258.60,$songyu->freight);
$this->assertEquals(50,$songyu->extra_fee);
$this->assertEquals(389,$songyu->base_weight);
$this->assertSame('实称重量',$songyu->base_source);
$this->assertStringStartsWith('RL-',$songyu->quote_version);
$this->assertStringContainsString('1.89639方×110',$songyu->formula);
$this->assertEquals(239.64,$db->table('jst_oms_shipping_freight')->where('source_id',102)->value('freight'));
$attach=new \ReflectionMethod(ShippingOrderRepository::class,'attachFreight');
$shown=$attach->invoke(app(ShippingOrderRepository::class),[['id'=>1,'source_id'=>101]],false);
$this->assertEquals(258.60,$shown[0]['qc_freight']);
$this->assertSame('OK',$shown[0]['qc_status']);
} finally { unlink($path); }
}
public function test_return_quotes_keep_missing_inputs_uncovered_routes_and_future_quotes_unpriced(): void
{
$path=$this->bindReturnQuotes();
try {
$service=app(ShippingFreightService::class);
$base=['express_name1'=>'常州松宇货运','warehouse_name'=>'简尚-山东青岗集仓','receiver_state'=>'山东省',
'receiver_city'=>'青岛市','actual_weight'=>389,'volume_cm3'=>1896390,'ship_time'=>'2026-11-01'];
foreach ([
[['volume_cm3'=>null],'缺少重量或体积,无法计算运费'],
[['receiver_state'=>'广东省','receiver_city'=>'广州市'],'报价未覆盖该路线'],
[['ship_time'=>'2026-07-31'],'发货日没有生效的逆向物流报价'],
] as [$changes,$reason]) {
$result=$service->calculatePackage(array_replace($base,$changes));
$this->assertSame($reason,$result['status']);
$this->assertNull($result['fee']);
}
} finally { unlink($path); }
}
public function test_purchase_returns_match_the_single_supplier_without_using_product_suppliers_on_normal_shipments(): void
{
$path = $this->bindReturnQuotes();
try {
$db = DB::connection('business_mirror');
$base = ['express_name1' => '中铁客退', 'warehouse_name' => '简尚-山东青岗集仓',
'ship_time' => '2026-11-03 12:00:00', 'receiver_state' => '山东省', 'receiver_city' => '青岛市',
'receiver_address' => '测试路', 'receiver_name' => '收件人', 'supplier_name' => '测试青岛厂',
'barcode' => 'RETURN-TEST', 'qty' => 1, 'online_weight' => 389, 'volume_cm3' => 948195];
foreach ([3000000301, 3000000302, 303] as $id) {
$db->table('jst_oms_shipping')->insert($base + ['source_id' => $id, 'sku_source_id' => 1]);
}
// 同一供应商多个商品行仍为唯一厂家;多厂家不能任选一家。
foreach ([3000000301 => '测试青岛厂', 3000000302 => '另一家工厂'] as $id => $supplier) {
$db->table('jst_oms_shipping')->insert(array_replace($base, ['source_id' => $id, 'sku_source_id' => 2,
'supplier_name' => $supplier, 'online_weight' => null]));
}
$result = app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', ['中铁客退']);
$this->assertSame(['packages' => 3, 'ok' => 1], $result);
$this->assertEquals(239.64, $db->table('jst_oms_shipping_freight')->where('source_id', 3000000301)->value('freight'));
foreach ([3000000302, 303] as $id) {
$row = $db->table('jst_oms_shipping_freight')->where('source_id', $id)->sole();
$this->assertSame('报价未覆盖该路线', $row->status);
$this->assertNull($row->freight);
}
} finally {
unlink($path);
}
}
public function test_purchase_returns_use_complete_detail_volume_instead_of_duplicated_merged_volume(): void
{
$path = $this->bindReturnQuotes();
try {
$db = DB::connection('business_mirror');
$base = ['express_name1' => '中铁客退', 'warehouse_name' => '简尚-山东青岗集仓',
'ship_time' => '2026-11-03 12:00:00', 'receiver_name' => '测试青岛厂',
'qty' => 1, 'online_weight' => 389, 'volume_cm3' => 1896390, 'm_volume_cm3' => 1000000000];
foreach ([3000000401, 3000000402, 3000000403, 404] as $id) {
$db->table('jst_oms_shipping')->insert(array_replace($base, ['source_id' => $id, 'sku_source_id' => 1,
'volume_cm3' => $id === 3000000402 ? null : $base['volume_cm3']]));
}
$db->table('jst_oms_shipping')->insert(array_replace($base, ['source_id' => 3000000403,
'sku_source_id' => 2, 'online_weight' => null, 'volume_cm3' => null]));
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', ['中铁客退']);
$this->assertEquals(239.64, $db->table('jst_oms_shipping_freight')->where('source_id', 3000000401)->value('freight'));
foreach ([3000000402, 3000000403] as $id) {
$row = $db->table('jst_oms_shipping_freight')->where('source_id', $id)->sole();
$this->assertSame('缺少重量或体积,无法计算运费', $row->status);
$this->assertNull($row->freight);
}
// 普通发货仍沿用合并体积,防止改动其他承运线路的口径。
$this->assertEquals(100000, $db->table('jst_oms_shipping_freight')->where('source_id', 404)->value('freight'));
} finally {
unlink($path);
}
}
public function test_shared_purchase_return_waybills_keep_complete_volume_and_all_suppliers(): void
{
$path = $this->bindReturnQuotes();
try {
$db = DB::connection('business_mirror');
$base = ['express_name1' => '中铁客退', 'warehouse_name' => '简尚-山东青岗集仓',
'ship_time' => '2026-11-03 12:00:00', 'receiver_name' => '收件人', 'supplier_name' => '测试青岛厂',
'qty' => 1, 'barcode' => 'RETURN-A', 'volume_cm3' => 948195, 'm_volume_cm3' => 1000000000];
foreach ([3000000501, 3000000511, 3000000521] as $sid) {
$db->table('jst_oms_shipping')->insert($base + ['source_id' => $sid, 'sku_source_id' => 1,
'express_no' => 'RETURN-MERGE-' . $sid, 'online_weight' => 389]);
$db->table('jst_oms_shipping')->insert(array_replace($base, ['source_id' => $sid + 1,
'sku_source_id' => 1, 'express_no' => 'RETURN-MERGE-' . $sid, 'barcode' => 'RETURN-B',
'online_weight' => null, 'supplier_name' => $sid === 3000000511 ? '另一家工厂' : '测试青岛厂',
'volume_cm3' => $sid === 3000000521 ? null : 948195]));
}
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01', ['中铁客退']);
$freight = fn (int $sid) => $db->table('jst_oms_shipping_freight')->where('source_id', $sid)->sole();
$this->assertEquals(239.64, $freight(3000000501)->freight);
$this->assertEquals(0, $freight(3000000502)->freight);
$this->assertSame('报价未覆盖该路线', $freight(3000000511)->status);
$this->assertNull($freight(3000000511)->freight);
$this->assertSame('缺少重量或体积,无法计算运费', $freight(3000000521)->status);
$this->assertNull($freight(3000000521)->freight);
} finally {
unlink($path);
}
}
private function bindReturnQuotes(): string
{
$path=tempnam(sys_get_temp_dir(),'route-service-');
$header=['出发仓','出发省份','到达地','到达省份','吨价(元/吨)','方价(元/方)','来源','备注','派送费门槛(方)','门槛含等于','派送费(元/票)'];
file_put_contents($path,json_encode(['sheets'=>[
['name'=>'常州松宇货运返货报价','values'=>[['执行日期:2026-08-01'],$header,['山东','山东','青岛/临沂','山东',500,110,'报价单','',10,'是',50]]],
['name'=>'中铁物流返货报价','values'=>[['执行日期:2000-01-01'],$header,['山东曹县','山东','测试青岛厂','山东',420,100,'报价单','',15,'否',50]]],
]],JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));
app()->instance(ReturnRouteFreightCalculator::class,new ReturnRouteFreightCalculator(new ReverseLogisticsQuoteSnapshot($path)));
return $path;
}
public function test_packages_are_priced_by_the_quote_in_force_on_the_ship_date_and_shown_on_shipping_rows(): void
{
$this->version('QB-901', '新北中通', '2026-10-01', '2026-11-10', [['省份', '面单', '费率'], ['江苏', 3.5, 0.7]]);
$this->version('QB-902', '新北中通', '2026-11-11', null, [['省份', '面单', '费率'], ['江苏', 3.0, 0.6]]);
$db = DB::connection('business_mirror');
$row = static fn (int $sid, int $sku, string $time, array $extra = []) => $extra + [
'source_id' => $sid, 'sku_source_id' => $sku, 'express_no' => 'ZT' . $sid, 'express_name1' => '新北中通',
'warehouse_name' => '简尚-崔桥工厂仓', 'receiver_state' => '江苏省', 'receiver_city' => '南京市', 'ship_time' => $time,
'pkg_count' => '1/1', 'barcode' => 'SJ' . $sid . '-' . $sku, 'qty' => 1, 'freight' => 5,
'online_weight' => null, 'length_cm' => null, 'width_cm' => null, 'height_cm' => null,
];
$db->table('jst_oms_shipping')->insert([
// 两件商品一个包裹:重量求和 2+1=3kg,长宽高取最大 50×40×30 → 体积重 7.5kg
$row(1, 1, '2026-11-05 10:00:00', ['online_weight' => 2, 'length_cm' => 50, 'width_cm' => 40, 'height_cm' => 30]),
$row(1, 2, '2026-11-05 10:00:00', ['online_weight' => 1, 'length_cm' => 10, 'width_cm' => 10, 'height_cm' => 10, 'freight' => null]),
// 11-11 起用新报价;免抛仓不计泡
$row(2, 1, '2026-11-12 09:00:00', ['online_weight' => 4, 'length_cm' => 50, 'width_cm' => 40, 'height_cm' => 30, 'warehouse_name' => '常州简尚家居有限公司']),
// 10 月历史数据使用当时生效的报价
$row(3, 1, '2026-10-01 00:00:00', ['online_weight' => 4]),
$row(5, 1, '2026-09-30 23:59:59', ['online_weight' => 4]),
// 核算起始日(9-01)之前的不核算
$row(6, 1, '2026-08-31 23:59:59', ['online_weight' => 4]),
// 报价管理里没有的快递
$row(4, 1, '2026-11-12 09:00:00', ['online_weight' => 4, 'express_name1' => '某某快递']),
]);
$res = app(ShippingFreightService::class)->recalculate('2026-08-01', '2026-12-01');
// 9-30 的包裹也核算了,但新北中通 9 月没有生效报价
$this->assertSame(['packages' => 5, 'ok' => 3], $res);
$r1 = $db->table('jst_oms_shipping_freight')->where('source_id', 1)->sole();
$this->assertSame('QB-901', $r1->quote_version);
$this->assertEquals(8.75, $r1->freight);
$this->assertEquals(7.5, $r1->settle_weight);
$this->assertEquals(5, $r1->jst_freight);
$r2 = $db->table('jst_oms_shipping_freight')->where('source_id', 2)->sole();
$this->assertSame('QB-902', $r2->quote_version);
$this->assertEquals(5.4, $r2->freight);
$r3 = $db->table('jst_oms_shipping_freight')->where('source_id', 3)->sole();
$this->assertSame('QB-901', $r3->quote_version);
$this->assertEquals(6.3, $r3->freight);
$this->assertSame('发货日没有生效的报价', $db->table('jst_oms_shipping_freight')->where('source_id', 5)->value('status'));
$this->assertSame(0, $db->table('jst_oms_shipping_freight')->where('source_id', 6)->count());
$this->assertSame('报价管理里没有「某某快递」的报价', $db->table('jst_oms_shipping_freight')->where('source_id', 4)->value('status'));
// 发货数据页面:合并口径每个包裹带核算列,明细口径只在包裹第一行
$attach = new \ReflectionMethod(ShippingOrderRepository::class, 'attachFreight');
$repo = app(ShippingOrderRepository::class);
$merged = $attach->invoke($repo, [['id' => 99, 'source_id' => 1]], false);
$this->assertEquals(8.75, $merged[0]['qc_freight']);
$this->assertEquals(3.75, $merged[0]['qc_diff']);
$this->assertSame('QB-901', $merged[0]['qc_quote_version']);
$this->assertSame('线上重量 3kg;体积重 7.5kg(÷8000)', $merged[0]['qc_basis']);
$ids = $db->table('jst_oms_shipping')->where('source_id', 1)->orderBy('id')->pluck('id')->all();
$detail = $attach->invoke($repo, [['id' => $ids[0], 'source_id' => 1], ['id' => $ids[1], 'source_id' => 1]], true);
$this->assertEquals(8.75, $detail[0]['qc_freight']);
$this->assertArrayNotHasKey('qc_freight', $detail[1]);
}
public function test_one_waybill_shared_by_several_orders_is_priced_once_and_leftover_rows_without_waybill_are_zero(): void
{
$this->version('QB-921', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
$db = DB::connection('business_mirror');
$row = static fn (int $sid, ?string $no, string $trade, ?string $barcode, ?float $online, int $sku = 1) => [
'source_id' => $sid, 'sku_source_id' => $sku, 'express_no' => $no, 'trade_no' => $trade, 'express_name1' => '新北中通',
'warehouse_name' => '简尚-崔桥工厂仓', 'receiver_state' => '江苏省', 'receiver_city' => '南京市', 'ship_time' => '2026-11-05 10:00:00',
'pkg_count' => '1/1', 'barcode' => $barcode, 'qty' => 1, 'online_weight' => $online,
];
$db->table('jst_oms_shipping')->insert([
// 合单:同一单号,另一个订单没有商品明细却也带着整包重量 -> 只在有商品的订单上算一次
$row(22, 'ZTX', 'T22', null, 4.0),
$row(21, 'ZTX', 'T21', 'A1', 4.0),
// 合单两个订单都有商品,出库单校正后重量只在主包裹:第二个订单并进来,体积/条码合在一起算
$row(31, 'ZTW', 'T31', 'W1', 5.0),
$row(32, 'ZTW', 'T32', 'W2', null),
// 没有快递单号的残留行:同一交易单号已有带单号的包裹
$row(23, null, 'T24', null, 2.0),
$row(24, 'ZTY', 'T24', 'B1', 3.0),
// 同一单号分批出库,各有自己的重量和商品:各算各的
$row(25, 'ZTZ', 'T25', 'C1', 2.0),
$row(26, 'ZTZ', 'T26', 'C2', 1.0),
// 自动发货仓这类没有单号、订单也没有其他包裹的:照常核算
$row(27, null, 'T27', 'D1', 1.0),
]);
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01');
$f = fn (int $sid) => $db->table('jst_oms_shipping_freight')->where('source_id', $sid)->sole();
$this->assertEquals(7.0, $f(21)->freight);
$this->assertEquals(0.0, $f(22)->freight);
$this->assertSame('OK', $f(22)->status);
$this->assertStringContainsString('合成一个包裹', $f(22)->formula);
$this->assertEquals(8.0, $f(31)->freight);
$this->assertEquals(0.0, $f(32)->freight);
$this->assertEquals(0.0, $f(23)->freight);
$this->assertStringContainsString('没有快递单号', $f(23)->formula);
$this->assertEquals(6.0, $f(24)->freight);
$this->assertEquals(5.0, $f(25)->freight);
$this->assertEquals(4.0, $f(26)->freight);
$this->assertEquals(4.0, $f(27)->freight);
// 同一单号的运费合计只算一次
$this->assertEquals(7.0, $db->table('jst_oms_shipping_freight')->where('express_no', 'ZTX')->sum('freight'));
// 无商品的合单行排在前面时,对账仍展示真正核算行的报价版本和公式。
$details = app(\App\Support\CourierBills\ShippingLookup::class)->details(['ZTX']);
$this->assertSame('QB-921', $details['ZTX']['qc_quote_version']);
$this->assertSame($f(21)->formula, $details['ZTX']['qc_formula']);
$this->assertEquals(7.0, $details['ZTX']['qc_freight']);
}
public function test_numeric_waybills_and_trade_numbers_are_queried_as_strings(): void
{
$this->version('QB-922', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
$db = DB::connection('business_mirror');
$base = ['sku_source_id' => 1, 'express_name1' => '新北中通', 'warehouse_name' => '简尚-崔桥工厂仓',
'receiver_state' => '江苏省', 'ship_time' => '2026-11-05 10:00:00', 'pkg_count' => '1/1', 'qty' => 1];
$db->table('jst_oms_shipping')->insert([
$base + ['source_id' => 41, 'express_no' => '123456789012', 'trade_no' => '9876543210', 'barcode' => 'A', 'online_weight' => 4.0],
$base + ['source_id' => 42, 'express_no' => '123456789012', 'trade_no' => '9876543211', 'barcode' => 'B', 'online_weight' => null],
$base + ['source_id' => 43, 'express_no' => null, 'trade_no' => '9876543210', 'barcode' => null, 'online_weight' => null],
]);
$bindings = [];
$db->listen(function ($query) use (&$bindings): void {
if (str_contains($query->sql, 'where "express_no" in') || str_contains($query->sql, 'where "trade_no" in')) {
$bindings[] = $query->bindings;
}
});
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01');
$this->assertNotEmpty($bindings);
foreach ($bindings as $values) {
foreach ($values as $value) {
$this->assertIsString($value);
}
}
$this->assertEquals(7.0, $db->table('jst_oms_shipping_freight')->where('source_id', 41)->value('freight'));
$this->assertEquals(0.0, $db->table('jst_oms_shipping_freight')->where('source_id', 42)->value('freight'));
$this->assertEquals(0.0, $db->table('jst_oms_shipping_freight')->where('source_id', 43)->value('freight'));
}
public function test_imported_measured_weights_take_priority_when_every_barcode_is_measured(): void
{
$this->version('QB-911', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
$db = DB::connection('business_mirror');
$row = static fn (int $sid, int $sku, string $barcode, array $extra = []) => $extra + [
'source_id' => $sid, 'sku_source_id' => $sku, 'express_no' => 'ZT' . $sid, 'express_name1' => '新北中通',
'warehouse_name' => '常州简尚家居有限公司', 'receiver_state' => '江苏省', 'receiver_city' => '南京市',
'ship_time' => '2026-11-05 10:00:00', 'pkg_count' => '1/1', 'barcode' => $barcode, 'qty' => 1,
'online_weight' => null, 'actual_weight' => null,
];
$db->table('jst_oms_shipping')->insert([
// 一个包裹两个条码都有称重:A 6kg×2 件 + B 3kg → 15kg;实称重量 9kg 不用
$row(21, 1, 'BC-A', ['qty' => 2, 'online_weight' => 8, 'actual_weight' => 9]),
$row(21, 2, 'bc-b'),
// 只有部分条码有称重:整单重量拆不出单件,不用称重,照旧按实称重量
$row(23, 1, 'BC-A', ['online_weight' => 10, 'actual_weight' => 11]),
$row(23, 2, 'BC-X'),
// 称重只登记给别的快递:照旧按实称重量
$row(22, 1, 'BC-C', ['online_weight' => 4, 'actual_weight' => 5]),
]);
DB::table('shipping_measured_weights')->insert([
['company_id' => company_id() ?? 1, 'carrier_name' => '新北中通', 'barcode' => 'BC-A', 'weight_kg' => 6, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
['company_id' => company_id() ?? 1, 'carrier_name' => '新北中通', 'barcode' => 'BC-B', 'weight_kg' => 3, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
['company_id' => company_id() ?? 1, 'carrier_name' => '武进韵达', 'barcode' => 'BC-C', 'weight_kg' => 1, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
]);
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01');
$r21 = $db->table('jst_oms_shipping_freight')->where('source_id', 21)->sole();
$this->assertSame('称重重量', $r21->base_source);
$this->assertEquals(15, $r21->base_weight);
$r23 = $db->table('jst_oms_shipping_freight')->where('source_id', 23)->sole();
$this->assertSame('实称重量', $r23->base_source);
$this->assertEquals(11, $r23->base_weight);
$r22 = $db->table('jst_oms_shipping_freight')->where('source_id', 22)->sole();
$this->assertSame('实称重量', $r22->base_source);
$this->assertEquals(5, $r22->base_weight);
}
public function test_measured_weights_from_another_company_are_not_used_without_a_current_company(): void
{
// 命令行(calc-freight)没有当前公司:按 jushuitan.freight_company_id 取本公司称重,不能取到别家公司同条码的称重
$this->version('QB-931', '新北中通', '2026-10-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
DB::connection('business_mirror')->table('jst_oms_shipping')->insert([
'source_id' => 31, 'sku_source_id' => 1, 'express_no' => 'ZT31', 'express_name1' => '新北中通',
'warehouse_name' => '常州简尚家居有限公司', 'receiver_state' => '江苏省', 'ship_time' => '2026-11-05 10:00:00',
'pkg_count' => '1/1', 'barcode' => 'BC-M', 'qty' => 1, 'online_weight' => 8, 'actual_weight' => 9,
]);
$other = (int) $this->company->id + 1000;
DB::table('shipping_measured_weights')->insert([
['company_id' => $this->company->id, 'carrier_name' => '新北中通', 'barcode' => 'BC-M', 'weight_kg' => 5, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
['company_id' => $other, 'carrier_name' => '新北中通', 'barcode' => 'BC-M', 'weight_kg' => 12, 'updated_by' => 1, 'created_at' => now(), 'updated_at' => now()],
]);
config(['jushuitan.freight_company_id' => $this->company->id]);
\App\Models\Common\Company::forgetCurrent();
$this->assertNull(company_id());
app(ShippingFreightService::class)->recalculate('2026-11-01', '2026-12-01');
$r = DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 31)->sole();
$this->assertSame('称重重量', $r->base_source);
$this->assertEquals(5, $r->base_weight);
}
public function test_quote_changes_recalculate_that_carrier_from_the_effective_date(): void
{
DB::connection('business_mirror')->table('jst_oms_shipping')->insert([
'source_id' => 11, 'sku_source_id' => 1, 'express_no' => 'ZT11', 'express_name1' => '新北中通', 'warehouse_name' => '简尚-崔桥工厂仓',
'receiver_state' => '江苏省', 'ship_time' => '2026-11-16 09:00:00', 'pkg_count' => '1/1', 'online_weight' => 2,
]);
$this->version('QB-911', '新北中通', '2026-11-01', null, [['省份', '面单', '费率'], ['江苏', 3.0, 1.0]]);
ShippingFreightService::flushPending();
$this->assertEquals(5.0, DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 11)->value('freight'));
// 补录 11-15 起的新报价:审核生效后自动按新报价重算
$this->version('QB-912', '新北中通', '2026-11-15', null, [['省份', '面单', '费率'], ['江苏', 4.0, 1.0]]);
ShippingFreightService::flushPending();
$row = DB::connection('business_mirror')->table('jst_oms_shipping_freight')->where('source_id', 11)->sole();
$this->assertSame('QB-912', $row->quote_version);
$this->assertEquals(6.0, $row->freight);
}
private function version(string $code, string $name, string $from, ?string $to, array $vals): CourierQuoteVersion
{
$change = CourierQuoteChange::create([
'company_id' => $this->company->id, 'number' => 'BG-T-' . $code, 'source_quote_id' => $code,
'source_sheet_name' => $name, 'carrier_name' => $name, 'quote_type' => '标准快递报价', 'change_type' => '面单调整',
'status' => CourierQuoteChange::STATUS_APPROVED, 'effective_date' => $from, 'is_retrospective' => false,
'recalculation_status' => CourierQuoteChange::RECALCULATION_NOT_REQUIRED, 'summary' => '测试报价',
'created_by' => $this->user->id, 'submitted_by' => $this->user->id, 'submitted_at' => now(),
'approved_by' => $this->user->id, 'approved_at' => now(),
]);
return CourierQuoteVersion::create([
'company_id' => $this->company->id, 'version_code' => $code, 'source_quote_id' => $code, 'source_change_id' => $change->id,
'sheet_name' => $name, 'company_name' => $name, 'carrier_name' => $name, 'quote_type' => '标准快递报价',
'effective_date' => $from, 'end_date' => $to, 'source_range' => 'A1:C2',
'values_json' => json_encode($vals, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
'formulas_json' => json_encode([[]], JSON_THROW_ON_ERROR), 'status' => 'approved',
'signature_status' => '历史签署资料已归档', 'created_by' => $this->user->id, 'approved_by' => $this->user->id,
]);
}
}