129 lines
9.3 KiB
PHP
129 lines
9.3 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Business;
|
||
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Modules\EcommerceFinance\Services\CustomerServiceSurchargeSynchronizer as Sync;
|
||
use Modules\EcommerceFinance\Repositories\CourierSurchargeSettlementMirrorRepository;
|
||
use RuntimeException;
|
||
use Tests\TestCase;
|
||
|
||
class CustomerServiceSurchargeTest extends TestCase
|
||
{
|
||
private const COURIER = '测试中通快运';
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
$s = Schema::connection('business_mirror');
|
||
$s->create(Sync::TABLE, function (Blueprint $t): void {
|
||
$t->id();$t->integer('rule_id');$t->string('surcharge_type');
|
||
foreach (['courier_name','waybill','platform_product_id','product_code','shop_name','online_order_no','inspection_type','inspection_reason','return_waybill_no','settlement_status','evidence'] as $k) $t->string($k)->nullable();
|
||
foreach (['outbound_io_id','length','width','height','volume','settlement_weight','settlement_amount'] as $k) $t->decimal($k,16,2)->nullable();
|
||
$t->dateTime('shipped_at')->nullable();$t->dateTime('pulled_at');$t->timestamps();
|
||
});
|
||
config(['system_database.mode'=>'shared']);
|
||
(require base_path('database/migrations/2026_12_04_120000_add_customer_service_surcharge_mirror.php'))->up();
|
||
$s->create(Sync::SOURCE, function (Blueprint $t): void {
|
||
$t->integer('Id')->primary();$t->string('woNo');$t->string('woTypeId');$t->string('title');$t->string('status');$t->dateTime('created');$t->text('businessFieldList');
|
||
$t->index(['woTypeId','created','Id']);
|
||
});
|
||
$s->create('business_mirror_table_states', function (Blueprint $t): void {
|
||
$t->string('dataset_key')->primary();foreach(['source_key','source_object','target_table','status','run_mode','generation','last_error_code'] as $k)$t->string($k)->nullable();
|
||
$t->text('cursor')->nullable();foreach(['last_attempted_at','initial_completed_at','last_succeeded_at','last_full_succeeded_at','window_started_at','window_ends_at'] as $k)$t->dateTime($k)->nullable();
|
||
$t->integer('last_upserted_rows')->default(0);$t->integer('mirror_row_count')->default(0);$t->timestamps();
|
||
});
|
||
DB::connection('business_mirror')->table('business_mirror_table_states')->insert(['dataset_key'=>'existing-work-orders','target_table'=>Sync::SOURCE,'status'=>'succeeded','initial_completed_at'=>now()]);
|
||
DB::table('business_courier_surcharge_quotes')->insert(['id'=>101,'company_id'=>1,'courier'=>self::COURIER,'scope'=>'自有快递','reconciler'=>'测试','types'=>json_encode(['上楼费','改地址']),'mode'=>'聚工单客服核实','standard'=>'读取聚工单附加费明细','condition'=>'聚工单客服回复通过','effective_at'=>'2026-06-01','status'=>'active','created_at'=>now(),'updated_at'=>now()]);
|
||
}
|
||
|
||
private function record(int $id, array $fees, string $created='2026-11-01 10:00:00', string $courier=self::COURIER, string $status='WAIT_CONFIRM'): array
|
||
{
|
||
$fields=[];
|
||
foreach(['logisticsField'=>$courier,'lIdField'=>'001234567890123456','orderSendTime'=>'2026-10-30 12:00:00','SELECT_CASCADEField1413856620250813172526'=>'查件/核实上楼费'] as $k=>$v)$fields[]=['fieldId'=>$k,'value'=>$v];
|
||
$fields[]=['fieldId'=>Sync::FEE_TABLE,'childFieldList'=>array_map(fn($f)=>[['fieldId'=>Sync::FEE_TYPE,'value'=>$f[0]],['fieldId'=>Sync::FEE_AMOUNT,'value'=>$f[1]],['fieldId'=>Sync::FEE_APPROVAL,'value'=>$f[2]]],$fees)];
|
||
return ['Id'=>$id,'woNo'=>'GD-TEST-'.$id,'woTypeId'=>Sync::WORK_TYPE,'title'=>'','status'=>$status,'created'=>$created,'businessFieldList'=>json_encode($fields,JSON_UNESCAPED_UNICODE)];
|
||
}
|
||
|
||
public function test_only_row_approval_counts_and_repeated_sync_replaces_without_multiplying(): void
|
||
{
|
||
$db=DB::connection('business_mirror');
|
||
$db->table(Sync::SOURCE)->insert([$this->record(1,[['上楼费','3.50','通过'],['上楼费','50',''],['上楼费','2.25','通过'],['改地址费','4.25','通过']]),$this->record(2,[['上楼费','999','通过']],'2026-05-31 23:59:59'),$this->record(3,[['上楼费','99','通过']],'2026-11-02 00:00:00'),$this->record(4,[['上楼费','777','通过']],'2026-11-01 10:00:00','别家快递'),$this->record(5,[['上楼费','888','通过']],'2026-11-01 10:00:00',self::COURIER,'CANCELLED')]);
|
||
$sync=new Sync();$result=$sync->sync(1,self::COURIER,'2026-06-01','2026-11-01',null,2);
|
||
$this->assertSame(2,$result['rows']);$this->assertSame('10.00',$result['amount']);
|
||
$sync->sync(1,self::COURIER,'2026-06-01','2026-11-01',null,2);
|
||
$this->assertSame(2,$db->table(Sync::TABLE)->count());
|
||
$this->assertEquals(5.75,$db->table(Sync::TABLE)->where('surcharge_type','上楼费')->value('settlement_amount'));
|
||
$this->assertSame('001234567890123456',$db->table(Sync::TABLE)->value('waybill'));
|
||
$db->table(Sync::SOURCE)->where('Id',1)->update(['businessFieldList'=>$this->record(1,[['上楼费','3.50','不通过']])['businessFieldList']]);
|
||
$this->assertSame(0,$sync->sync(1,self::COURIER,'2026-06-01','2026-11-01')['rows']);
|
||
$this->assertSame('fresh',app(CourierSurchargeSettlementMirrorRepository::class)->state(['courier_name'=>self::COURIER])['code']);
|
||
}
|
||
|
||
public function test_failed_batch_preserves_live_rows_and_resumes_checkpoint(): void
|
||
{
|
||
$db=DB::connection('business_mirror');$sync=new FailingSurchargeSync();
|
||
$db->table(Sync::SOURCE)->insert([$this->record(1,[['上楼费','5','通过']]),$this->record(2,[['上楼费','6','通过']])]);
|
||
$sync->sync(1,self::COURIER,'2026-06-01','2026-11-01');
|
||
$sync->fail=true;
|
||
try {$sync->sync(1,self::COURIER,'2026-06-01','2026-11-01',null,1);$this->fail('failure expected');}catch(RuntimeException $e){$this->assertSame('test read failure',$e->getMessage());}
|
||
$this->assertEquals(11,$db->table(Sync::TABLE)->sum('settlement_amount'));
|
||
$sync->fail=false;$this->assertSame('11.00',$sync->sync(1,self::COURIER,'2026-06-01','2026-11-01',null,1)['amount']);
|
||
$this->assertSame(2,$db->table(Sync::TABLE)->count());
|
||
}
|
||
|
||
public function test_rule_constraints_and_invalid_approved_amount_do_not_publish(): void
|
||
{
|
||
DB::table('business_courier_surcharge_quotes')->where('id',101)->update(['condition'=>'重量40kg以上,上楼费聚工单客服回复通过']);
|
||
try {(new Sync())->sync(1,self::COURIER);$this->fail('constraint expected');}catch(RuntimeException $e){$this->assertStringContainsString('附加条件',$e->getMessage());}
|
||
DB::table('business_courier_surcharge_quotes')->where('id',101)->update(['condition'=>'聚工单客服回复通过']);
|
||
DB::connection('business_mirror')->table(Sync::SOURCE)->insert($this->record(1,[['上楼费','not money','通过']]));
|
||
$this->expectException(RuntimeException::class);(new Sync())->sync(1,self::COURIER);
|
||
}
|
||
|
||
public function test_import_sums_distinct_approved_work_orders_for_the_same_waybill(): void
|
||
{
|
||
DB::connection('business_mirror')->table(Sync::SOURCE)->insert([$this->record(1,[['上楼费','5','通过']]),$this->record(2,[['上楼费','6','通过']])]);
|
||
(new Sync())->sync(1,self::COURIER);
|
||
$repo=app(CourierSurchargeSettlementMirrorRepository::class);
|
||
$reflection=new \ReflectionMethod($repo,'matchImportRow');$reflection->setAccessible(true);
|
||
$row=$reflection->invoke($repo,DB::connection('business_mirror')->table(Sync::TABLE)->get()->all(),'上楼费',self::COURIER);
|
||
$this->assertEquals(11,$row->settlement_amount);
|
||
$this->assertStringContainsString('GD-TEST-1',$row->evidence);$this->assertStringContainsString('GD-TEST-2',$row->evidence);
|
||
}
|
||
|
||
public function test_count_change_does_not_publish_partial_results(): void
|
||
{
|
||
$db=DB::connection('business_mirror');$db->table(Sync::SOURCE)->insert($this->record(1,[['上楼费','5','通过']]));
|
||
(new Sync())->sync(1,self::COURIER);
|
||
$inserted=false;
|
||
try {
|
||
(new Sync())->sync(1,self::COURIER,null,null,function () use (&$inserted,$db): void {
|
||
if (!$inserted) {$db->table(Sync::SOURCE)->where('Id',1)->delete();$inserted=true;}
|
||
},1);
|
||
$this->fail('count mismatch expected');
|
||
} catch(RuntimeException $e) {$this->assertStringContainsString('数量发生变化',$e->getMessage());}
|
||
$this->assertEquals(5,$db->table(Sync::TABLE)->sum('settlement_amount'));
|
||
$this->assertNull($db->table('business_mirror_table_states')->where('dataset_key',Sync::dataset(1,self::COURIER))->value('cursor'));
|
||
}
|
||
|
||
public function test_disabled_rules_and_other_companies_are_not_used(): void
|
||
{
|
||
$db=DB::connection('business_mirror');$db->table(Sync::SOURCE)->insert($this->record(1,[['上楼费','5','通过']]));
|
||
DB::table('business_courier_surcharge_quotes')->where('id',101)->update(['company_id'=>2]);
|
||
$this->expectException(RuntimeException::class);(new Sync())->sync(1,self::COURIER);
|
||
}
|
||
}
|
||
|
||
class FailingSurchargeSync extends Sync
|
||
{
|
||
public bool $fail=false;
|
||
protected function batch(string $from,string $until,?string $lastDate,int $lastId,int $limit): array
|
||
{
|
||
if($this->fail && $lastId>0)throw new RuntimeException('test read failure');
|
||
return parent::batch($from,$until,$lastDate,$lastId,$limit);
|
||
}
|
||
}
|