hasTable('courier_surcharge_settlement_rows'))return; $s->create('courier_surcharge_settlement_rows',function(Blueprint $t):void{ $t->id();$t->integer('rule_id')->default(1);$t->integer('outbound_io_id')->nullable(); foreach(['courier_name','waybill','surcharge_type','settlement_status','platform_product_id','product_code','shop_name','online_order_no','source_kind','source_key'] as $key)$t->string($key)->nullable(); $t->decimal('settlement_amount',16,2);$t->dateTime('shipped_at')->nullable();$t->dateTime('pulled_at')->nullable();$t->dateTime('source_created_at')->nullable(); $t->index(['waybill']); }); } private function fee(string $waybill,string $type,float $amount,string $courier='常州中通快运',string $date='2026-10-02'): void { DB::connection('business_mirror')->table('courier_surcharge_settlement_rows')->insert([ 'waybill'=>$waybill,'surcharge_type'=>$type,'settlement_amount'=>$amount,'courier_name'=>$courier, 'settlement_status'=>'已计算','shipped_at'=>$date.' 12:00:00','source_key'=>'GD-'.$waybill.'-'.$type, 'source_kind'=>'work_order_customer_service','platform_product_id'=>'12345678901234567890', 'product_code'=>'SKU-TEST','shop_name'=>'测试店铺','online_order_no'=>'123456789012345678901']); } private function ship(string $no,string $courier,string $warehouse='测试仓'): void { $db=app(ShippingLookup::class)->connection(); $id=$db->table('jst_oms_shipping')->insertGetId(['express_no'=>$no,'express_name1'=>$courier,'warehouse_name'=>$warehouse,'ship_time'=>'2026-11-02 12:00:00']); $db->table('jst_oms_shipping')->where('id',$id)->update(['source_id'=>$id]); $db->table('jst_oms_shipping_freight')->insert(['source_id'=>$id,'freight'=>10,'status'=>'OK']); } private function book(array $sheets): string { $book=new Spreadsheet();$book->removeSheetByIndex(0); foreach($sheets as $name=>$rows){$sheet=$book->createSheet();$sheet->setTitle($name); foreach($rows as $r=>$row)foreach($row as $c=>$value)if($value!==null)$sheet->setCellValueExplicit([$c+1,$r+1],$value,is_string($value)?'s':'n');} $path=tempnam(sys_get_temp_dir(),'fee_bill_').'.xlsx';$this->temp[]=$path;(new Xlsx($book))->save($path);$book->disconnectWorksheets();return $path; } public function test_courier_import_matches_prior_month_fees_and_preserves_metadata_and_duplicates(): void { $store=app(CourierBillStore::class);$store->saveGroup(null,['name'=>'常州中通快运','express_names'=>['常州中通快运']]); foreach(['001234567890','001234567891','001234567892','001234567893'] as $no)$this->ship($no,'常州中通快运'); $this->fee('001234567890','上楼费',13.44);$this->fee('001234567891','改地址',5); $this->fee('001234567892','上楼费',99,'别家快递');$this->fee('001234567893','上楼费',88,'常州中通快运','2026-12-01'); $path=$this->book(['账单'=>[['单号','运费'],['001234567890',10],['001234567891',10],['001234567892',10],['001234567893',10]], '附加费'=>[['快递单号','附加费金额','附加费类型'],['001234567890',20,'上楼费'],['001234567890',20,'上楼费'],['001234567891',8,'改地址费'],['001234567892',99,'上楼费'],['001234567893',88,'上楼费']]]); $fileId=$store->upload([new UploadedFile($path,'常州中通快运11月.xlsx',null,null,true)],'2026-11',[],'测试')[0]; $db=$store->db();$id=(int)$db->table('courier_bill_files')->where('id',$fileId)->value('statement_id'); $matches=app(SurchargeSettlementMatcher::class)->query('courier',$id)->orderBy('id')->get(); $this->assertSame(['已匹配','重复','已匹配','未匹配','未匹配'],$matches->pluck('match_status')->all()); $this->assertEquals(18.44,round($matches->sum('self_amount'),2));$this->assertSame('12345678901234567890',$matches[0]->platform_product_id); $this->assertSame('SKU-TEST',$matches[0]->product_code);$this->assertSame('测试店铺',$matches[0]->shop_name); $this->assertSame('123456789012345678901',$matches[0]->online_order_no); $this->assertEquals(18.44,$db->table('courier_bill_summary_items')->where('statement_id',$id)->where('label','附加费')->value('self_amount')); $store->rematch($id);$this->assertSame(5,app(SurchargeSettlementMatcher::class)->query('courier',$id)->count()); $this->assertEquals(18.44,round((float)app(SurchargeSettlementMatcher::class)->query('courier',$id)->sum('self_amount'),2)); $out=tempnam(sys_get_temp_dir(),'fees_out');$this->temp[]=$out;$h=fopen($out,'wb');app(StatementExporter::class)->export($id,$h);fclose($h); $book=IOFactory::load($out);$sheet=$book->getSheetByName('附加费核对-'.$id);$this->assertNotNull($sheet); $cell=$sheet->getCell('K2')->getValue(); $this->assertSame('12345678901234567890',$cell instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText?$cell->getPlainText():$cell);$book->disconnectWorksheets(); $db->table('courier_bill_summary_items')->where('statement_id',$id)->where('label','11月运费')->update(['self_amount'=>123.45]); $db->table('courier_bill_summary_items')->where('statement_id',$id)->where('label','附加费')->update(['manual'=>true,'self_amount'=>17.12]); app(CourierBillMatcher::class)->refreshSurchargeSummary($id); $this->assertEquals(123.45,$db->table('courier_bill_summary_items')->where('statement_id',$id)->where('label','11月运费')->value('self_amount')); $this->assertEquals(17.12,$db->table('courier_bill_summary_items')->where('statement_id',$id)->where('label','附加费')->value('self_amount')); $this->user->attachPermission(\App\Models\Auth\Permission::where('name','read-admin-panel')->firstOrFail()); $this->loginAs()->get(route('business.pages.show',['section'=>'courier-payables-final','page'=>'courier-bill-reconciliation','cb_view'=>'statement','statement'=>$id])) ->assertOk()->assertSeeText('附加费核对明细')->assertSeeText('SKU-TEST')->assertSeeText('12345678901234567890'); } public function test_dropship_import_uses_system_fee_not_factory_quote_and_checks_warehouse(): void { $store=app(DropshipFreightStore::class);$id=$store->saveFactory(null,['name'=>'测试代发','warehouse'=>'工厂-测试代发仓','keywords'=>'代发快递']); $this->ship('YT0012345678','测试代发快递','工厂-测试代发仓');$this->fee('YT0012345678','改地址',4.25,'测试代发快递'); $path=$this->book(['账单'=>[['单号','运费'],['YT0012345678',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['YT0012345678',50,'改地址费']]]); $store->uploadFor('测试代发11月.xlsx',$path,'2026-11',$id,'测试'); $row=app(SurchargeSettlementMatcher::class)->query('dropship',$id,'2026-11')->first(); $this->assertEquals(50,$row->bill_amount);$this->assertEquals(4.25,$row->self_amount); $auto=app(\App\Support\DropshipFreight\DropshipFees::class)->auto('2026-11');$this->assertEquals(4.25,$auto[$id]['fees']['readdress']); $out=tempnam(sys_get_temp_dir(),'df_out');$this->temp[]=$out;$h=fopen($out,'wb');app(DropshipExporter::class)->export(collect($store->factories())->sole(),'2026-11',$h);fclose($h); $book=IOFactory::load($out);$this->assertNotNull($book->getSheetByName('附加费核对'));$summary=$book->getSheetByName('汇总')->toArray(null,false,false);$feeRow=current(array_filter($summary,fn($r)=>$r[0]==='厂家账单-附加费'));$this->assertEquals(4.25,$feeRow[2]);$book->disconnectWorksheets(); } public function test_other_month_cannot_pay_the_same_settlement_again_and_confirmed_is_frozen(): void { $db=DB::connection('business_mirror');$store=app(CourierBillStore::class);$group=$store->saveGroup(null,['name'=>'常州中通快运','express_names'=>['常州中通快运']]); $this->fee('HISTORY1234','上楼费',10); foreach(['2026-10','2026-11'] as $month){$sid=$db->table('courier_bill_statements')->insertGetId(['group_id'=>$group,'bill_month'=>$month,'status'=>'open','created_at'=>now(),'updated_at'=>now()]); $fid=$db->table('courier_bill_files')->insertGetId(['statement_id'=>$sid,'bill_month'=>$month,'group_id'=>$group,'original_name'=>'test','stored_path'=>'test','status'=>'parsed','created_at'=>now(),'updated_at'=>now()]); $db->table('courier_bill_surcharges')->insert(['file_id'=>$fid,'sheet'=>'上楼费','row_no'=>2,'waybill_no'=>'HISTORY1234','item'=>'上楼费','amount'=>50]); app(SurchargeSettlementMatcher::class)->courier($sid);$last=$sid;} $row=app(SurchargeSettlementMatcher::class)->query('courier',$last)->first();$this->assertSame('历史已计入',$row->match_status);$this->assertEquals(0,$row->self_amount); $db->table('courier_bill_statements')->where('id',$last)->update(['status'=>'confirmed']); $db->table('courier_surcharge_settlement_rows')->update(['settlement_amount'=>123]);app(SurchargeSettlementMatcher::class)->courier($last); $this->assertEquals(0,app(SurchargeSettlementMatcher::class)->query('courier',$last)->value('self_amount')); } public function test_monthly_surcharge_batch_defaults_to_previous_month_and_requires_a_scope(): void { $this->artisan('courier-surcharge:match-bills',['--month'=>'2026-11'])->assertFailed(); $this->artisan('courier-surcharge:match-bills',['--month'=>'2026-13','--all'=>true])->assertFailed(); $this->artisan('courier-surcharge:match-bills',['--month'=>'2026-11','--all'=>true])->assertSuccessful(); } public function test_daily_refresh_does_not_enable_legacy_bills_and_only_new_sources_are_counted(): void { $db=DB::connection('business_mirror');$store=app(CourierBillStore::class); $group=$store->saveGroup(null,['name'=>'常州中通快运','express_names'=>['常州中通快运']]); $this->fee('PARTIAL1234','上楼费',10); $ids=[]; foreach(['2026-10','2026-11'] as $month){ $sid=$db->table('courier_bill_statements')->insertGetId(['group_id'=>$group,'bill_month'=>$month,'status'=>'open','created_at'=>now(),'updated_at'=>now()]); $fid=$db->table('courier_bill_files')->insertGetId(['statement_id'=>$sid,'bill_month'=>$month,'group_id'=>$group,'original_name'=>'test','stored_path'=>'test','status'=>'parsed','created_at'=>now(),'updated_at'=>now()]); $db->table('courier_bill_surcharges')->insert(['file_id'=>$fid,'sheet'=>'上楼费','row_no'=>2,'waybill_no'=>'PARTIAL1234','item'=>'上楼费','amount'=>20]); $ids[]=$sid; } $matcher=app(SurchargeSettlementMatcher::class);$matcher->courier($ids[0]); $this->artisan('courier-surcharge:match-bills',['--month'=>'2026-11','--all'=>true,'--existing-only'=>true])->assertSuccessful(); $this->assertSame(0,$matcher->query('courier',$ids[1])->count()); $source=(array)$db->table('courier_surcharge_settlement_rows')->first();unset($source['id']); $source['source_key']='GD-NEW';$source['settlement_amount']=3.25;$source['shipped_at']='2026-11-05 12:00:00'; $db->table('courier_surcharge_settlement_rows')->insert($source); $matcher->courier($ids[1]);$row=$matcher->query('courier',$ids[1])->first(); $this->assertEquals(3.25,$row->self_amount);$this->assertStringContainsString('已扣除',$row->match_note); $db->table('courier_surcharge_settlement_rows')->where('source_key','GD-NEW')->update(['settlement_amount'=>4.25]); $this->artisan('courier-surcharge:match-bills',['--month'=>'2026-11','--all'=>true,'--existing-only'=>true])->assertSuccessful(); $this->assertEquals(4.25,$matcher->query('courier',$ids[1])->value('self_amount')); $matcher->courier($ids[0]);$this->assertEquals(10,$matcher->query('courier',$ids[0])->value('self_amount')); } public function test_dropship_rejects_a_matching_courier_from_another_factory_warehouse(): void { $store=app(DropshipFreightStore::class);$id=$store->saveFactory(null,['name'=>'本厂','warehouse'=>'本厂仓','keywords'=>'代发快递']); $this->ship('WRONG12345','测试代发快递','其他厂仓');$this->fee('WRONG12345','改地址',15,'测试代发快递'); $db=$store->db();$file=$db->table('dropship_bill_files')->insertGetId(['factory_id'=>$id,'bill_month'=>'2026-11','original_name'=>'test.xlsx','stored_path'=>'test.xlsx','status'=>'parsed','created_at'=>now(),'updated_at'=>now()]); $db->table('dropship_bill_surcharges')->insert(['factory_id'=>$id,'file_id'=>$file,'bill_month'=>'2026-11','sheet'=>'附加费','row_no'=>2,'waybill_no'=>'WRONG12345','item'=>'改地址','amount'=>30]); app(SurchargeSettlementMatcher::class)->dropship($id,'2026-11'); $row=app(SurchargeSettlementMatcher::class)->query('dropship',$id,'2026-11')->first(); $this->assertNull($row->self_amount);$this->assertSame('未匹配',$row->match_status); } protected function tearDown(): void {foreach($this->temp as $path)@unlink($path);parent::tearDown();} }