487 lines
45 KiB
PHP
487 lines
45 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Business;
|
||
|
||
use App\Support\CourierBills\CourierBillStore;
|
||
use App\Support\CourierBills\CourierBillMatcher;
|
||
use App\Support\CourierBills\ShippingLookup;
|
||
use App\Support\CourierBills\SurchargeSettlementMatcher;
|
||
use App\Support\CourierBills\StatementExporter;
|
||
use App\Support\DropshipFreight\DropshipFreightStore;
|
||
use App\Support\DropshipFreight\DropshipExporter;
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Http\UploadedFile;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||
use Tests\Feature\FeatureTestCase;
|
||
|
||
class BillSurchargeMatchingTest extends FeatureTestCase
|
||
{
|
||
private array $temp=[];
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();Storage::fake('local');
|
||
self::createSettlementTable();
|
||
}
|
||
|
||
public static function createSettlementTable(): void
|
||
{
|
||
$s=Schema::connection('business_mirror');
|
||
if($s->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();}
|
||
|
||
private function quote(string $courier,string $type,string $mode,string $standard,string $condition,string $scope='代发仓'): int
|
||
{
|
||
return DB::table('business_courier_surcharge_quotes')->insertGetId(['company_id'=>(int)$this->company->id,
|
||
'courier'=>$courier,'scope'=>$scope,'reconciler'=>'测试','types'=>json_encode([$type]),'mode'=>$mode,
|
||
'standard'=>$standard,'condition'=>$condition,'effective_at'=>'2026-01-01','status'=>'active','created_at'=>now(),'updated_at'=>now()]);
|
||
}
|
||
|
||
private function workOrder(string $no,string $courier,string $type,string $amount,?string $approved,string $created='2026-11-03 10:00:00',?string $reason=null,string $status='WAIT_CONFIRM',?int $orderId=null): void
|
||
{
|
||
$schema=Schema::connection('business_mirror');
|
||
if(!$schema->hasTable('js_jst_work_order_info'))$schema->create('js_jst_work_order_info',function(Blueprint $t){
|
||
$t->string('_mirror_key')->primary();$t->integer('Id');$t->string('woNo');$t->string('title');$t->string('status');$t->dateTime('created');$t->text('businessFieldList');
|
||
});
|
||
$fields=[['fieldId'=>'lIdField','value'=>$no],['fieldId'=>'logisticsField','value'=>$courier],
|
||
['fieldId'=>\Modules\EcommerceFinance\Services\CustomerServiceSurchargeSynchronizer::FEE_TABLE,'childFieldList'=>[[
|
||
['fieldId'=>\Modules\EcommerceFinance\Services\CustomerServiceSurchargeSynchronizer::FEE_TYPE,'value'=>$type],
|
||
['fieldId'=>\Modules\EcommerceFinance\Services\CustomerServiceSurchargeSynchronizer::FEE_AMOUNT,'value'=>$amount],
|
||
['fieldId'=>\Modules\EcommerceFinance\Services\CustomerServiceSurchargeSynchronizer::FEE_APPROVAL,'value'=>$approved],
|
||
]]]];
|
||
if($reason!==null)$fields[]=['fieldId'=>'SELECT_CASCADEField1413856620250813172526','value'=>$reason];
|
||
if($orderId!==null)$fields[]=['fieldId'=>'o_id','value'=>$orderId];
|
||
$db=DB::connection('business_mirror');$id=$db->table('js_jst_work_order_info')->count()+1;
|
||
$row=['_mirror_key'=>'work-'.$id,'Id'=>$id,'woNo'=>'GD-'.$id,'title'=>'查件','status'=>$status,'created'=>$created,'businessFieldList'=>json_encode($fields)];
|
||
$db->table('js_jst_work_order_info')->insert($row);
|
||
app(\Modules\EcommerceFinance\Services\WorkOrderWaybillLookup::class)->storeBatch([$row]);
|
||
$db->table('work_order_waybill_lookup_states')->updateOrInsert(['id'=>1],['completed_at'=>now(),'updated_at'=>now()]);
|
||
}
|
||
|
||
public function test_import_calculates_outbound_freight_and_does_not_apply_other_courier_rules(): void
|
||
{
|
||
$store=app(DropshipFreightStore::class);$id=$store->saveFactory(null,['name'=>'测试厂家','warehouse'=>'测试仓','keywords'=>'测试']);
|
||
$this->quote('测试圆通','拦截费','取发出运费','等于发出运费','取发出运费');
|
||
$this->ship('YT11111111','测试圆通');$this->ship('ST11111111','测试申通');
|
||
$db=$store->db();$db->table('jst_oms_shipping')->where('express_no','YT11111111')->update(['item_id'=>'12345678901234567890','barcode'=>'SKU001','shop_name'=>'测试店','trade_no'=>'ORDER001']);
|
||
$path=$this->book(['账单'=>[['单号','运费'],['YT11111111',2],['ST11111111',3]],
|
||
'附加费'=>[['快递单号','附加费金额','附加费类型'],['YT11111111',999,'拦截退回'],['YT11111111',999,'拦截费'],['ST11111111',999,'拦截退回']]]);
|
||
$store->uploadFor('测试厂家11月.xlsx',$path,'2026-11',$id,'测试');
|
||
$rows=app(SurchargeSettlementMatcher::class)->query('dropship',$id,'2026-11')->orderBy('id')->get();
|
||
$this->assertEquals(10,$rows[0]->self_amount);$this->assertEquals(0,$rows[1]->self_amount);$this->assertNull($rows[2]->self_amount);
|
||
$this->assertSame('重复',$rows[1]->match_status);$this->assertSame('SKU001',$rows[0]->product_code);
|
||
$this->assertSame('12345678901234567890',$rows[0]->platform_product_id);$this->assertSame('ORDER001',$rows[0]->online_order_no);
|
||
$this->assertStringContainsString('取本票已核算发出运费',$rows[0]->match_note);
|
||
$this->assertStringContainsString('没有启用',$rows[2]->match_note);
|
||
}
|
||
|
||
public function test_buyer_intercept_uses_structured_reason_without_requiring_a_return_or_approved_fee(): void
|
||
{
|
||
$store=app(DropshipFreightStore::class);$id=$store->saveFactory(null,['name'=>'测试厂家','warehouse'=>'测试仓','keywords'=>'测试']);
|
||
$this->quote('测试申通','拦截费','取发出运费','等于发出运费','查件扣单并且买家原因');
|
||
$cases=[['BUYER0001','扣单/买家-不想要',10],['BUYER0002','扣单/买家-冲动消费',10],
|
||
['BUYER0003','扣单/快递-破损',0],['BUYER0004','扣单/超时未更新物流信息【有揽收物流更新异常】',0],
|
||
['BUYER0005',null,null],['BUYER0006','扣单/',null],['BUYER0007','扣单/买家-',null],['BUYER0008','催件/买家-不想要',0]];
|
||
$fees=[['快递单号','附加费金额','附加费类型']];
|
||
foreach($cases as [$no,$reason,$amount]){
|
||
$this->ship($no,'测试申通');
|
||
if($reason!==null)$this->workOrder($no,'测试申通','', '',null,'2026-11-03 10:00:00',$reason);
|
||
$fees[]=[$no,999,'拦截退回'];
|
||
}
|
||
// 同票重复别名只付一次;旧结算结果不能绕过买家原因核验。
|
||
$fees[]=['BUYER0001',999,'拦截退回运费'];
|
||
$this->fee('BUYER0003','拦截费',99,'测试申通');$this->fee('BUYER0005','拦截费',99,'测试申通');
|
||
$path=$this->book(['账单'=>[['单号','运费'],['BUYER0001',10]],'附加费'=>$fees]);
|
||
$store->uploadFor('测试买家原因11月.xlsx',$path,'2026-11',$id,'测试');
|
||
$rows=app(SurchargeSettlementMatcher::class)->query('dropship',$id,'2026-11')->orderBy('row_no')->get();
|
||
foreach($cases as $i=>[$no,$reason,$amount]){
|
||
$this->assertSame($amount===null?null:(float)$amount,$rows[$i]->self_amount===null?null:(float)$rows[$i]->self_amount,$no);
|
||
$this->assertSame($amount===null?'未匹配':'已匹配',$rows[$i]->match_status,$no);
|
||
}
|
||
$this->assertStringContainsString('扣单/买家-不想要',$rows[0]->match_note);
|
||
$this->assertStringContainsString('快递-破损',$rows[2]->match_note);
|
||
$this->assertSame('重复',$rows[8]->match_status);$this->assertEquals(20,$rows->sum('self_amount'));
|
||
}
|
||
|
||
public function test_buyer_reason_requires_valid_courier_status_dates_and_latest_deduction(): void
|
||
{
|
||
$this->quote('测试申通','拦截费','取发出运费','等于发出运费','查件扣单并且买家原因');
|
||
$cases=[
|
||
['VALID0001','测试申通','2026-09-01 00:00:00','WAIT_CONFIRM',10],
|
||
['VALID0002','测试申通','2026-08-31 23:59:59','WAIT_CONFIRM',null],
|
||
['VALID0003','测试申通','2026-12-01 00:00:00','WAIT_CONFIRM',null],
|
||
['VALID0004','其他申通','2026-11-03 10:00:00','WAIT_CONFIRM',null],
|
||
['VALID0005','测试申通','2026-11-03 10:00:00','CANCELLED',null],
|
||
['VALID0006','测试申通','2026-11-30 23:59:59','FINISHED',10],
|
||
];$input=[];
|
||
foreach($cases as [$no,$courier,$date,$status,$amount]){
|
||
$this->ship($no,'测试申通');$this->workOrder($no,$courier,'','',null,$date,'扣单/买家-不想要',$status);
|
||
$input[]=['waybill_no'=>$no,'item'=>'拦截费'];
|
||
}
|
||
$this->ship('LATEST001','测试申通');$input[]=['waybill_no'=>'LATEST001','item'=>'拦截费'];
|
||
$this->workOrder('LATEST001','测试申通','','',null,'2026-11-02 10:00:00','扣单/买家-不想要');
|
||
$this->workOrder('LATEST001','测试申通','','',null,'2026-11-03 10:00:00','扣单/快递-破损');
|
||
$this->workOrder('LATEST001','测试申通','','',null,'2026-11-04 10:00:00','扣单/买家-冲动消费','CANCELLED');
|
||
$this->workOrder('LATEST001','测试申通','','',null,'2026-11-05 10:00:00','催件/买家-不想要');
|
||
$calc=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
|
||
$result=$calc->calculate($input,'dropship','2026-11',['测试申通']);
|
||
foreach($cases as [$no,$courier,$date,$status,$amount]){
|
||
if($amount===null){$this->assertSame([],$result['rows'][$no]['拦截费']);$this->assertStringContainsString('无法核实',$result['notes'][$no]['拦截费']);}
|
||
else $this->assertEquals($amount,$result['rows'][$no]['拦截费'][0]->settlement_amount);
|
||
}
|
||
$this->assertEquals(0,$result['rows']['LATEST001']['拦截费'][0]->settlement_amount);
|
||
$this->assertStringContainsString('快递-破损',$result['rows']['LATEST001']['拦截费'][0]->evidence);
|
||
// 报价生效前的工单和未建完的单号索引都不能作为依据。
|
||
DB::table('business_courier_surcharge_quotes')->update(['effective_at'=>'2026-10-01']);
|
||
$this->assertStringContainsString('无法核实',$calc->calculate([$input[0]],'dropship','2026-11',['测试申通'])['notes']['VALID0001']['拦截费']);
|
||
DB::connection('business_mirror')->table('work_order_waybill_lookup_states')->update(['completed_at'=>null]);
|
||
$this->assertStringContainsString('无法核实',$calc->calculate([$input[5]],'dropship','2026-11',['测试申通'])['notes']['VALID0006']['拦截费']);
|
||
}
|
||
|
||
public function test_intercept_aliases_are_consistent_across_bill_customer_service_and_import(): void
|
||
{
|
||
$repository=app(\Modules\EcommerceFinance\Repositories\CourierSurchargeSettlementMirrorRepository::class);
|
||
$normalizer=new \ReflectionMethod($repository,'normalizeImportType');
|
||
foreach(['拦截','拦截退回','拦截退回运费','拦截费'] as $alias){
|
||
$this->assertSame('拦截费',SurchargeSettlementMatcher::type($alias));
|
||
$this->assertSame('拦截费',\Modules\EcommerceFinance\Services\CustomerServiceSurchargeSynchronizer::normalizeType($alias));
|
||
$this->assertSame('拦截费',$normalizer->invoke($repository,$alias));
|
||
}
|
||
$this->quote('测试申通','拦截费','聚工单客服核实','读取聚工单附加费明细','聚工单客服回复通过');
|
||
$this->ship('ALIAS0001','测试申通');$this->workOrder('ALIAS0001','测试申通','拦截退回运费','8.25','通过');
|
||
$result=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class)->calculate([['waybill_no'=>'ALIAS0001','item'=>'拦截退回']],'dropship','2026-11',['测试申通']);
|
||
$this->assertEquals(8.25,$result['rows']['ALIAS0001']['拦截费'][0]->settlement_amount);
|
||
}
|
||
|
||
public function test_buyer_confirmation_is_scoped_to_rule_month_waybill_and_unchanged_work_order_reason(): void
|
||
{
|
||
$rule=$this->quote('测试申通','拦截费','取发出运费','等于发出运费','查件扣单并且买家原因');
|
||
$this->ship('CONFIRM001','测试申通');$this->ship('CONFIRM002','测试申通');
|
||
$this->workOrder('CONFIRM001','测试申通','','',null,'2026-11-03 10:00:00','扣单/快递-破损');
|
||
$this->workOrder('CONFIRM002','测试申通','','',null,'2026-11-03 10:00:00','扣单/快递-破损');
|
||
$calc=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
|
||
$store=app(\Modules\EcommerceFinance\Services\SupplierPayableOverrideStore::class);
|
||
$key=$store->key($calc::BUYER_CONFIRMATIONS,['id'=>$calc::buyerConfirmationKey($rule,'2026-11','CONFIRM001','GD-1')]);
|
||
$store->save($calc::BUYER_CONFIRMATIONS,$key,['buyer_reason_confirmed'=>true,'original_reason'=>'扣单/快递-破损','evidence'=>'负责人确认此票属于买家原因'],(int)$this->company->id,null);
|
||
$otherKey=$store->key($calc::BUYER_CONFIRMATIONS,['id'=>$calc::buyerConfirmationKey($rule,'2026-11','CONFIRM002','GD-2')]);
|
||
$store->save($calc::BUYER_CONFIRMATIONS,$otherKey,['buyer_reason_confirmed'=>true,'original_reason'=>'扣单/快递-破损','evidence'=>'其他公司确认'],(int)$this->company->id+1,null);
|
||
$input=[['waybill_no'=>'CONFIRM001','item'=>'拦截费'],['waybill_no'=>'CONFIRM002','item'=>'拦截费']];
|
||
foreach([1,2] as $_){$result=$calc->calculate($input,'dropship','2026-11',['测试申通']);
|
||
$this->assertEquals(10,$result['rows']['CONFIRM001']['拦截费'][0]->settlement_amount);
|
||
$this->assertStringContainsString('人工确认买家原因',$result['rows']['CONFIRM001']['拦截费'][0]->evidence);
|
||
$this->assertEquals(0,$result['rows']['CONFIRM002']['拦截费'][0]->settlement_amount);}
|
||
$freightStore=app(DropshipFreightStore::class);$factory=$freightStore->saveFactory(null,['name'=>'确认测试厂','warehouse'=>'测试仓','keywords'=>'测试']);
|
||
$path=$this->book(['账单'=>[['单号','运费'],['CONFIRM001',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['CONFIRM001',99,'拦截退回']]]);
|
||
$freightStore->uploadFor('确认11月.xlsx',$path,'2026-11',$factory,'测试');
|
||
app(SurchargeSettlementMatcher::class)->dropship($factory,'2026-11');
|
||
$this->assertEquals(10,app(SurchargeSettlementMatcher::class)->query('dropship',$factory,'2026-11')->value('self_amount'));
|
||
$this->assertEquals(0,$calc->calculate($input,'dropship','2026-12',['测试申通'])['rows']['CONFIRM001']['拦截费'][0]->settlement_amount);
|
||
$newRule=$this->quote('测试申通','拦截费','取发出运费','等于发出运费','查件扣单并且买家原因');
|
||
$this->assertEquals(0,$calc->calculate($input,'dropship','2026-11',['测试申通'])['rows']['CONFIRM001']['拦截费'][0]->settlement_amount);
|
||
DB::table('business_courier_surcharge_quotes')->where('id',$newRule)->delete();
|
||
$this->workOrder('CONFIRM001','测试申通','','',null,'2026-11-04 10:00:00','扣单/快递-破损');
|
||
$this->assertEquals(0,$calc->calculate($input,'dropship','2026-11',['测试申通'])['rows']['CONFIRM001']['拦截费'][0]->settlement_amount);
|
||
$db=DB::connection('business_mirror');$db->table('js_jst_work_order_info')->where('woNo','GD-3')->delete();
|
||
$raw=$db->table('js_jst_work_order_info')->where('woNo','GD-1')->first();$fields=json_decode($raw->businessFieldList,true);
|
||
foreach($fields as &$field)if($field['fieldId']==='SELECT_CASCADEField1413856620250813172526')$field['value']='扣单/物流异常';unset($field);
|
||
$db->table('js_jst_work_order_info')->where('woNo','GD-1')->update(['businessFieldList'=>json_encode($fields)]);
|
||
$this->assertEquals(0,$calc->calculate($input,'dropship','2026-11',['测试申通'])['rows']['CONFIRM001']['拦截费'][0]->settlement_amount);
|
||
$this->assertCount(1,$store->listRows($calc::BUYER_CONFIRMATIONS,(int)$this->company->id));
|
||
}
|
||
|
||
public function test_fixed_price_import_counts_once_per_waybill_and_preserves_metadata(): void
|
||
{
|
||
$store=app(DropshipFreightStore::class);$factory=$store->saveFactory(null,['name'=>'固定单价测试','warehouse'=>'测试仓','keywords'=>'测试']);
|
||
$this->quote('测试固定','拦截费','固定单价','30元/单','按单收取');
|
||
$this->ship('FIXED0001','测试固定');$this->ship('FIXED0002','其他固定');
|
||
$db=$store->db();$db->table('jst_oms_shipping')->where('express_no','FIXED0001')->update(['barcode'=>'FIX-SKU','shop_name'=>'固定测试店','trade_no'=>'FIX-ORDER']);
|
||
$path=$this->book(['账单'=>[['单号','运费'],['FIXED0001',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['FIXED0001',999,'拦截费'],['FIXED0001',999,'拦截退回'],['FIXED0002',999,'拦截费']]]);
|
||
$store->uploadFor('固定测试11月.xlsx',$path,'2026-11',$factory,'测试');
|
||
foreach([1,2] as $_){app(SurchargeSettlementMatcher::class)->dropship($factory,'2026-11');$rows=app(SurchargeSettlementMatcher::class)->query('dropship',$factory,'2026-11')->orderBy('row_no')->get();
|
||
$this->assertEquals(30,$rows[0]->self_amount);$this->assertEquals(0,$rows[1]->self_amount);$this->assertNull($rows[2]->self_amount);
|
||
$this->assertSame('FIX-SKU',$rows[0]->product_code);$this->assertSame('固定测试店',$rows[0]->shop_name);$this->assertSame('FIX-ORDER',$rows[0]->online_order_no);}
|
||
}
|
||
|
||
public function test_fixed_buyer_return_condition_requires_buyer_reason_and_received_goods(): void
|
||
{
|
||
$this->quote('测试固定','拦截费','固定单价','30/单','查件扣单并且买家原因已退回的');
|
||
$db=DB::connection('business_mirror');$schema=Schema::connection('business_mirror');
|
||
$schema->create('ops_jst_return_refund_info',function(Blueprint $t){$t->integer('as_id');$t->integer('o_id')->index();});
|
||
$schema->create('ops_jst_return_refund_item',function(Blueprint $t){$t->integer('as_id')->index();$t->integer('r_qty');$t->dateTime('receive_date')->nullable();});
|
||
$cases=[['FIXRET001','扣单/买家-不想要','2026-09-01 00:00:00',1,30],['FIXRET002','扣单/买家-不想要',null,0,null],
|
||
['FIXRET003','扣单/快递-破损','2026-11-01',1,0],['FIXRET004','扣单/买家-不想要','2026-12-01',1,null],
|
||
['FIXRET005','扣单/买家-不想要','2026-08-31 23:59:59',1,null],['FIXRET006','扣单/买家-不想要','2026-11-30 23:59:59',0,null]];
|
||
$input=[];
|
||
foreach($cases as $i=>[$no,$reason,$received,$qty,$amount]){
|
||
$this->ship($no,'测试固定');$this->workOrder($no,'测试固定','','',null,'2026-11-03',$reason,'WAIT_CONFIRM',$i+1);
|
||
$db->table('ops_jst_return_refund_info')->insert(['as_id'=>$i+1,'o_id'=>$i+1]);
|
||
$db->table('ops_jst_return_refund_item')->insert(['as_id'=>$i+1,'r_qty'=>$qty,'receive_date'=>$received]);
|
||
$input[]=['waybill_no'=>$no,'item'=>'拦截费'];
|
||
}
|
||
$result=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class)->calculate($input,'dropship','2026-11',['测试固定']);
|
||
foreach($cases as [$no,$reason,$received,$qty,$amount]){
|
||
if($amount===null){$this->assertSame([],$result['rows'][$no]['拦截费']);$this->assertStringContainsString('退回进仓',$result['notes'][$no]['拦截费']);}
|
||
else $this->assertEquals($amount,$result['rows'][$no]['拦截费'][0]->settlement_amount);
|
||
}
|
||
$db->table('ops_jst_return_refund_item')->insert(['as_id'=>1,'r_qty'=>5,'receive_date'=>'2026-11-10']);
|
||
$repeated=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class)->calculate([$input[0]],'dropship','2026-11',['测试固定']);
|
||
$this->assertCount(1,$repeated['rows']['FIXRET001']['拦截费']);$this->assertEquals(30,$repeated['rows']['FIXRET001']['拦截费'][0]->settlement_amount);
|
||
$schema->drop('ops_jst_return_refund_item');
|
||
$missing=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class)->calculate([$input[0]],'dropship','2026-11',['测试固定']);
|
||
$this->assertStringContainsString('退回进仓',$missing['notes']['FIXRET001']['拦截费']);
|
||
}
|
||
|
||
public function test_fixed_customer_approval_uses_quote_price_and_enforces_type_courier_status(): void
|
||
{
|
||
$this->quote('测试固定','改地址','固定单价','30元/单','改地址客服回复通过');
|
||
$cases=[['FIXAPP001','测试固定','改地址费','通过','WAIT_CONFIRM',30],['FIXAPP002','测试固定','改地址',null,'WAIT_CONFIRM',null],
|
||
['FIXAPP003','其他固定','改地址','通过','WAIT_CONFIRM',null],['FIXAPP004','测试固定','改地址','通过','CANCELLED',null],
|
||
['FIXAPP005','测试固定','上楼费','通过','WAIT_CONFIRM',null]];$input=[];
|
||
foreach($cases as [$no,$courier,$type,$approval,$status,$amount]){
|
||
$this->ship($no,'测试固定');$this->workOrder($no,$courier,$type,'999',$approval,'2026-11-03',null,$status);$input[]=['waybill_no'=>$no,'item'=>'改地址'];
|
||
}
|
||
$result=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class)->calculate($input,'dropship','2026-11',['测试固定']);
|
||
foreach($cases as [$no,$courier,$type,$approval,$status,$amount]){
|
||
if($amount===null){$this->assertSame([],$result['rows'][$no]['改地址']);$this->assertStringContainsString('审核通过',$result['notes'][$no]['改地址']);}
|
||
else $this->assertEquals($amount,$result['rows'][$no]['改地址'][0]->settlement_amount);
|
||
}
|
||
}
|
||
|
||
public function test_fixed_rate_rejects_unknown_conditions_and_price_formats_without_old_settlement_fallback(): void
|
||
{
|
||
$rule=$this->quote('测试固定','拦截费','固定单价','30元/单','未知条件');$this->ship('FIXINVALID','测试固定');$this->fee('FIXINVALID','拦截费',999,'测试固定');
|
||
$calc=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);$input=[['waybill_no'=>'FIXINVALID','item'=>'拦截费']];
|
||
$this->assertStringContainsString('计费条件',$calc->calculate($input,'dropship','2026-11',['测试固定'])['notes']['FIXINVALID']['拦截费']);
|
||
DB::table('business_courier_surcharge_quotes')->where('id',$rule)->update(['condition'=>'按单收取']);
|
||
foreach(['30元/公斤','-30元/单','30.001元/单','30元/单另加10元'] as $bad){DB::table('business_courier_surcharge_quotes')->where('id',$rule)->update(['standard'=>$bad]);
|
||
$this->assertStringContainsString('格式不支持',$calc->calculate($input,'dropship','2026-11',['测试固定'])['notes']['FIXINVALID']['拦截费']);}
|
||
DB::table('business_courier_surcharge_quotes')->where('id',$rule)->update(['standard'=>'0.50元/票']);
|
||
$this->assertEquals(0.5,$calc->calculate($input,'dropship','2026-11',['测试固定'])['rows']['FIXINVALID']['拦截费'][0]->settlement_amount);
|
||
}
|
||
|
||
public function test_fixed_deduction_requires_only_the_registered_condition_and_keeps_old_results_from_bypassing_it(): void
|
||
{
|
||
$store=app(DropshipFreightStore::class);$factory=$store->saveFactory(null,['name'=>'固定扣单测试','warehouse'=>'测试仓','keywords'=>'测试']);
|
||
$this->quote('测试固定','拦截费','固定单价','1元/单','查件扣单');
|
||
$this->ship('DEDUCT001','测试固定');$this->ship('DEDUCT002','测试固定');
|
||
$this->workOrder('DEDUCT001','测试固定','','',null,'2026-11-03','扣单');
|
||
$this->workOrder('DEDUCT002','测试固定','','',null,'2026-11-03','扣单','CANCELLED');
|
||
$this->fee('DEDUCT002','拦截费',99,'测试固定');
|
||
$path=$this->book(['账单'=>[['单号','运费'],['DEDUCT001',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['DEDUCT001',30,'拦截费'],['DEDUCT002',99,'拦截费']]]);
|
||
$store->uploadFor('固定扣单11月.xlsx',$path,'2026-11',$factory,'测试');
|
||
$rows=app(SurchargeSettlementMatcher::class)->query('dropship',$factory,'2026-11')->orderBy('row_no')->get();
|
||
$this->assertEquals(1,$rows[0]->self_amount);$this->assertStringContainsString('GD-1',$rows[0]->match_note);
|
||
$this->assertNull($rows[1]->self_amount);$this->assertStringContainsString('未找到有效',$rows[1]->match_note);
|
||
}
|
||
|
||
public function test_customer_service_approval_weight_gate_and_missing_rule_are_explicit(): void
|
||
{
|
||
$store=app(CourierBillStore::class);$group=$store->saveGroup(null,['name'=>'测试德邦','express_names'=>['测试德邦']]);
|
||
$this->quote('测试德邦','上楼费','聚工单客服核实','读取聚工单附加费明细','重量60kg以上,上楼费聚工单客服回复通过','自有快递');
|
||
foreach(['DP11111111','DP22222222','DP33333333','DP44444444','DP55555555'] as $n)$this->ship($n,'测试德邦');
|
||
$db=$store->db();$db->table('jst_oms_shipping_freight')->update(['charge_weight'=>60]);
|
||
$sid=$db->table('jst_oms_shipping')->where('express_no','DP22222222')->value('source_id');$db->table('jst_oms_shipping_freight')->where('source_id',$sid)->update(['charge_weight'=>59.99]);
|
||
$this->workOrder('DP11111111','测试德邦','上楼费','37.50','通过');
|
||
$this->workOrder('DP22222222','测试德邦','上楼费','77','通过');
|
||
$this->workOrder('DP33333333','测试德邦','上楼费','77',null);
|
||
$this->workOrder('DP44444444','测试德邦','上楼费','77','通过','2026-12-01 00:00:00');
|
||
$path=$this->book(['账单'=>[['单号','运费'],['DP11111111',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],
|
||
['DP11111111',99,'上楼费'],['DP22222222',99,'上楼费'],['DP33333333',99,'上楼费'],['DP44444444',99,'上楼费'],['DP55555555',99,'改地址费']]]);
|
||
$fid=$store->upload([new UploadedFile($path,'测试德邦11月.xlsx',null,null,true)],'2026-11',[],'测试')[0];
|
||
$id=(int)$db->table('courier_bill_files')->where('id',$fid)->value('statement_id');$rows=app(SurchargeSettlementMatcher::class)->query('courier',$id)->orderBy('id')->get();
|
||
$this->assertEquals(37.50,$rows[0]->self_amount);$this->assertEquals(0,$rows[1]->self_amount);$this->assertNull($rows[2]->self_amount);$this->assertNull($rows[3]->self_amount);
|
||
$this->assertStringContainsString('未达到',$rows[1]->match_note);$this->assertStringContainsString('未审核通过',$rows[2]->match_note);
|
||
$this->assertStringContainsString('没有启用',$rows[4]->match_note);
|
||
}
|
||
|
||
public function test_quote_scope_effective_date_missing_freight_and_size_boundaries(): void
|
||
{
|
||
$this->ship('QA11111111','测试快递');$this->ship('QA22222222','测试快递');
|
||
$this->quote('测试快递','超长费','按尺寸/最长边','7元/单','单边超160cm','自有快递');
|
||
$db=DB::connection('business_mirror');$db->table('jst_oms_shipping')->update(['length_cm'=>160,'width_cm'=>20,'height_cm'=>20]);
|
||
$db->table('jst_oms_shipping')->where('express_no','QA22222222')->update(['length_cm'=>160.01]);
|
||
$input=[['waybill_no'=>'QA11111111','item'=>'超长费'],['waybill_no'=>'QA22222222','item'=>'超长费']];$calc=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
|
||
$result=$calc->calculate($input,'courier','2026-11',['测试快递']);$this->assertEquals(0,$result['rows']['QA11111111']['超长费'][0]->settlement_amount);$this->assertEquals(7,$result['rows']['QA22222222']['超长费'][0]->settlement_amount);
|
||
// 同一票在结算来源中有两张出库单,7元/票仍只能计7元。
|
||
$this->fee('QA22222222','超长费',7,'测试快递');
|
||
$source=(array)$db->table('courier_surcharge_settlement_rows')->where('waybill','QA22222222')->first();unset($source['id']);$source['source_key']='SECOND-OUTBOUND';$db->table('courier_surcharge_settlement_rows')->insert($source);
|
||
$store=app(CourierBillStore::class);$store->saveGroup(null,['name'=>'测试快递','express_names'=>['测试快递']]);
|
||
$path=$this->book(['账单'=>[['单号','运费'],['QA22222222',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['QA22222222',7,'超长费']]]);
|
||
$fid=$store->upload([new UploadedFile($path,'测试快递11月.xlsx',null,null,true)],'2026-11',[],'测试')[0];$sid=$db->table('courier_bill_files')->where('id',$fid)->value('statement_id');
|
||
$this->assertEquals(7,app(SurchargeSettlementMatcher::class)->query('courier',$sid)->value('self_amount'));
|
||
$this->assertSame([],$calc->calculate($input,'dropship','2026-11',['测试快递'])['rows']);
|
||
DB::table('business_courier_surcharge_quotes')->update(['effective_at'=>'2026-12-01']);$this->assertSame([],$calc->calculate($input,'courier','2026-11',['测试快递'])['rows']);
|
||
DB::table('business_courier_surcharge_quotes')->update(['effective_at'=>'2026-01-01','mode'=>'取发出运费','standard'=>'等于发出运费','condition'=>'取发出运费']);
|
||
$db->table('jst_oms_shipping_freight')->update(['status'=>'FAILED']);$this->assertStringContainsString('尚未核算完成',$calc->calculate($input,'courier','2026-11',['测试快递'])['notes']['QA11111111']['超长费']);
|
||
$db->table('jst_oms_shipping')->where('express_no','QA11111111')->update(['ship_time'=>'2026-12-01 00:00:00']);
|
||
$this->assertStringContainsString('晚于账单月份',$calc->calculate($input,'courier','2026-11',['测试快递'])['notes']['QA11111111']['超长费']);
|
||
$db->table('jst_oms_shipping')->where('express_no','QA11111111')->update(['ship_time'=>'2026-11-02 00:00:00']);
|
||
DB::table('business_courier_surcharge_quotes')->update(['deleted_at'=>now()]);$this->assertSame([],$calc->calculate($input,'courier','2026-11',['测试快递'])['rows']);
|
||
}
|
||
|
||
public function test_all_fees_use_three_calendar_months_including_the_bill_month(): void
|
||
{
|
||
$calc=app(\App\Support\CourierBills\BillSurchargeQuoteCalculator::class);
|
||
$this->assertSame(['from'=>'2026-09-01','until'=>'2026-12-01','months'=>3],$calc::window('2026-11'));
|
||
$this->assertSame(['from'=>'2026-11-01','until'=>'2027-02-01','months'=>3],$calc::window('2027-01'));
|
||
$this->quote('测试快递','拦截费','取发出运费','等于发出运费','取发出运费','自有快递');
|
||
$this->quote('测试快递','上楼费','聚工单客服核实','读取聚工单附加费明细','聚工单客服回复通过','自有快递');
|
||
$db=DB::connection('business_mirror');$input=[];
|
||
foreach(['2026-08-31 23:59:59','2026-09-01 00:00:00','2026-10-31 23:59:59','2026-11-30 23:59:59','2026-12-01 00:00:00'] as $i=>$date){
|
||
$no='WINDOW000'.$i;$this->ship($no,'测试快递');$db->table('jst_oms_shipping')->where('express_no',$no)->update(['ship_time'=>$date]);
|
||
$this->workOrder($no,'测试快递','上楼费','5','通过',$date);
|
||
$input[]=['waybill_no'=>$no,'item'=>'拦截费'];$input[]=['waybill_no'=>$no,'item'=>'上楼费'];
|
||
}
|
||
$result=$calc->calculate($input,'courier','2026-11',['测试快递']);
|
||
foreach([1,2,3] as $i){$this->assertEquals(10,$result['rows']['WINDOW000'.$i]['拦截费'][0]->settlement_amount);$this->assertEquals(5,$result['rows']['WINDOW000'.$i]['上楼费'][0]->settlement_amount);}
|
||
$this->assertArrayNotHasKey('WINDOW0000',$result['rows']);$this->assertArrayNotHasKey('WINDOW0004',$result['rows']);
|
||
// 已生成结算明细走同一日期范围,不能绕过下界。
|
||
$store=app(CourierBillStore::class);$store->saveGroup(null,['name'=>'来源快递','express_names'=>['来源快递']]);
|
||
foreach(['2026-08-31','2026-09-01','2026-11-30','2026-12-01'] as $i=>$date)$this->fee('SOURCE000'.$i,'上楼费',7,'来源快递',$date);
|
||
$path=$this->book(['账单'=>[['单号','运费'],['SOURCE0001',10]],'附加费'=>[['快递单号','附加费金额','附加费类型'],['SOURCE0000',7,'上楼费'],['SOURCE0001',7,'上楼费'],['SOURCE0002',7,'上楼费'],['SOURCE0003',7,'上楼费']]]);
|
||
$fid=$store->upload([new UploadedFile($path,'来源快递11月.xlsx',null,null,true)],'2026-11',[],'测试')[0];$sid=$db->table('courier_bill_files')->where('id',$fid)->value('statement_id');
|
||
$rows=app(SurchargeSettlementMatcher::class)->query('courier',$sid)->orderBy('row_no')->get();
|
||
$this->assertSame([null,7.0,7.0,null],$rows->map(fn($r)=>$r->self_amount===null?null:(float)$r->self_amount)->all());
|
||
}
|
||
}
|