CaiWuXiTong/tests/Feature/Business/ShippingWeightPrecisionTest.php

111 lines
7.7 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 Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Services\Jushuitan\ShippingWeightPrecision;
use Modules\EcommerceFinance\Services\Jushuitan\ShippingReplicaSynchronizer;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
use App\Support\ReturnRouteFreightCalculator;
use App\Support\ReverseLogisticsQuoteSnapshot;
use Tests\Feature\FeatureTestCase;
class ShippingWeightPrecisionTest extends FeatureTestCase
{
public function test_raw_weight_recovers_precision_only_for_the_same_valid_outbound(): void
{
$schema = Schema::connection('business_mirror');
$schema->create(ShippingWeightPrecision::RAW_TABLE, function (Blueprint $table): void {
$table->integer('Id'); $table->integer('io_id'); $table->string('l_id');
$table->string('weight')->nullable(); $table->string('status');
});
try {
$db = DB::connection('business_mirror');
foreach ([['5.0E-4', 'A', 'Confirmed'], ['1.2345', 'B', 'Confirmed'], ['0', 'C', 'Confirmed'],
['0.0001', 'D', 'Cancelled'], ['0.0002', 'DIFFERENT', 'Confirmed'], ['9.1256', 'F', 'Confirmed']] as $i => [$w,$l,$s]) {
$db->table(ShippingWeightPrecision::RAW_TABLE)->insert(['Id'=>$i+1,'io_id'=>$i+1,'l_id'=>$l,'weight'=>$w,'status'=>$s]);
}
$rows = [];
foreach (['A','B','C','D','E','F'] as $i=>$l) $rows[]=['io_id'=>$i+1,'l_id'=>$l,'weight'=>$i===1?1.23:0];
$actual = app(ShippingWeightPrecision::class)->outboundRows($rows);
$this->assertSame([0.0005,1.2345,0,0,0,0],array_column($actual,'weight'));
$this->assertSame(0.0001,ShippingWeightPrecision::positive('1e-4'));
$this->assertNull(ShippingWeightPrecision::positive('not-a-weight'));
} finally { $schema->drop(ShippingWeightPrecision::RAW_TABLE); }
}
public function test_confirmed_weights_and_region_survive_replacement_and_do_not_leak_between_months(): void
{
$db=DB::connection('business_mirror');
$db->table(ShippingWeightPrecision::CORRECTIONS)->insert(['ship_month'=>'2026-11','express_no'=>'TEST10001','carrier'=>'测试快递',
'online_weight'=>0.0001,'receiver_state'=>'江苏','receiver_city'=>'苏州','source'=>'user-confirmed']);
$base=['source_id'=>1,'express_no'=>'@TEST10001','express_name1'=>'测试快递','ship_time'=>'2026-11-30','online_weight'=>null];
$rows=app(ShippingWeightPrecision::class)->shipmentRows([$base,$base,array_replace($base,['source_id'=>2,'ship_time'=>'2026-12-01'])]);
$this->assertSame(0.0001,$rows[0]['online_weight']);
$this->assertNull($rows[1]['online_weight']);
$this->assertSame('江苏',$rows[0]['receiver_state']);
$this->assertNull($rows[2]['online_weight']);
$this->assertArrayNotHasKey('receiver_state',$rows[2]);
}
public function test_alignment_keeps_confirmed_tiny_weight_on_the_primary_row(): void
{
$db=DB::connection('business_mirror');
$db->table(ShippingWeightPrecision::CORRECTIONS)->insert(['ship_month'=>'2026-11','express_no'=>'SMALL1001','carrier'=>'测试快递','online_weight'=>0.0001,'source'=>'user-confirmed']);
foreach ([1,2] as $sku) $db->table('jst_oms_shipping')->insert(['source_id'=>1001,'sku_source_id'=>$sku,'barcode'=>'SKU'.$sku,
'express_no'=>'SMALL1001','express_name1'=>'测试快递','ship_time'=>'2026-11-05 10:00:00','online_weight'=>null]);
$up=$this->createMock(UpstreamReadOnlyQuery::class);
$up->method('select')->willReturnCallback(static fn(string $sql)=>str_contains($sql,'FROM rpt.jst_salesOutboundInfo so WHERE')
? [['io_id'=>11,'l_id'=>'SMALL1001','io_date'=>'2026-11-05 10:00:00','status'=>'Confirmed','weight'=>0,'f_weight'=>null]] : []);
$factory=$this->createMock(UpstreamReadOnlyQueryFactory::class);
$factory->method('forSource')->willReturn($up);
(new ShippingReplicaSynchronizer($factory))->alignWithOutbound('2026-11-05','2026-11-06');
$weights=$db->table('jst_oms_shipping')->where('source_id',1001)->orderBy('id')->pluck('online_weight')->all();
$this->assertEquals(0.0001,$weights[0]);
$this->assertNull($weights[1]);
}
public function test_confirmed_return_route_does_not_change_other_shipments_or_actual_warehouse(): void
{
$path=tempnam(sys_get_temp_dir(),'route-confirmed-');
file_put_contents($path,json_encode(['sheets'=>[
['name'=>'中铁物流返货报价','values'=>[['执行日期:2000-01-01'],['出发仓','出发省份','到达地','到达省份','吨价(元/吨)','方价(元/方)','来源','备注'],['山东曹县','山东','测试临沂厂','山东',420,100,'报价单','']]],
]],JSON_UNESCAPED_UNICODE));
app()->instance(ReturnRouteFreightCalculator::class,new ReturnRouteFreightCalculator(new ReverseLogisticsQuoteSnapshot($path)));
$db=DB::connection('business_mirror');
foreach ([3000000601,3000000602] as $id) $db->table('jst_oms_shipping')->insert(['source_id'=>$id,'sku_source_id'=>1,
'express_no'=>'RAIL'.$id,'express_name1'=>'中铁客退','warehouse_name'=>'简尚-新苏北仓','ship_time'=>'2026-11-30','online_weight'=>1000,'volume_cm3'=>2000000]);
$db->table(ShippingWeightPrecision::CORRECTIONS)->insert(['ship_month'=>'2026-11','express_no'=>'RAIL3000000601','carrier'=>'中铁客退',
'return_origin'=>'山东曹县','return_destination'=>'测试临沂厂','source'=>'user-confirmed']);
try {
$service=app(\Modules\EcommerceFinance\Services\Freight\ShippingFreightService::class);
$result=$service->recalculateSourceIds([3000000601,3000000602]);
$this->assertSame(['packages'=>2,'ok'=>1],$result);
$row=$db->table('jst_oms_shipping_freight')->where('source_id',3000000601)->sole();
$this->assertStringContainsString('山东曹县→测试临沂厂',$row->formula);
$this->assertSame('简尚-新苏北仓',$db->table('jst_oms_shipping')->where('source_id',3000000601)->value('warehouse_name'));
$this->assertNull($db->table('jst_oms_shipping_freight')->where('source_id',3000000602)->value('freight'));
} finally { unlink($path); }
}
public function test_command_validates_scope_then_backs_up_and_recalculates_only_requested_packages(): void
{
$db=DB::connection('business_mirror');
foreach ([1001,1002] as $sid) $db->table('jst_oms_shipping')->insert(['source_id'=>$sid,'sku_source_id'=>1,
'express_no'=>'TEST'.$sid,'express_name1'=>'测试快递','ship_time'=>'2026-11-05','online_weight'=>null]);
$file=tempnam(sys_get_temp_dir(),'weight-correction-');
try {
file_put_contents($file,json_encode([['express_no'=>'TEST1001','online_weight'=>'0.0001']]));
$this->artisan('jushuitan:apply-shipping-corrections',['file'=>$file,'--month'=>'2026-11','--dry-run'=>true])->assertSuccessful();
$this->assertSame(0,$db->table(ShippingWeightPrecision::CORRECTIONS)->count());
$this->artisan('jushuitan:apply-shipping-corrections',['file'=>$file,'--month'=>'2026-11'])->assertSuccessful();
$this->assertEquals(0.0001,$db->table('jst_oms_shipping')->where('source_id',1001)->value('online_weight'));
$this->assertNull($db->table('jst_oms_shipping')->where('source_id',1002)->value('online_weight'));
$this->assertSame([1001],$db->table('jst_oms_shipping_freight')->pluck('source_id')->map(fn($id)=>(int)$id)->all());
} finally { unlink($file); }
}
}