user->attachPermission(\App\Models\Auth\Permission::where('name', 'read-admin-panel')->firstOrFail()); $this->loginAs()->withExceptionHandling(); $db = CourierBillMatcher::db(); $store = app(CourierBillStore::class); $group = $store->saveGroup(null, ['name' => '武进极兔', 'express_names' => ['武进极兔']]); $shipping = app(\App\Support\CourierBills\ShippingLookup::class)->connection(); $cases = [['WTLOW0001', 8, 2, 10, 5, 2, 0, 0], ['WTHIGH0001', 12, 7, 10, 5, 5, 2, 2], ['WTEQUAL0001', 10, 4, 10, 5, 5, -1, 0], ['WTMISSING0001', 8, null, 10, 5, null, null, 0], ['WTZERO0001', 0, 0, 10, 5, 0, 0, 0], ['WTDECIMAL0001', 10.665, 6.85, 11.78, 9.2, 6.85, 0, 0]]; $csv = "单号,运费,重量\n"; foreach ($cases as $i => [$waybill, $bill, $weight, $calc, $charge]) { $shipping->table('jst_oms_shipping')->insert(['source_id' => $i + 1, 'express_no' => $waybill, 'express_name1' => '武进极兔', 'ship_time' => '2026-11-03 09:00:00']); $shipping->table('jst_oms_shipping_freight')->insert(['source_id' => $i + 1, 'status' => 'OK', 'freight' => $calc, 'charge_weight' => $charge]); $csv .= "$waybill,$bill,$weight\n"; } $file = $store->upload([\Illuminate\Http\UploadedFile::fake()->createWithContent('武进极兔.csv', $csv)], '2026-11', [], '测试')[0]; $id = (int) $db->table('courier_bill_files')->where('id', $file)->value('statement_id'); $before = $db->table('courier_bill_lines')->where('statement_id', $id)->orderBy('id')->get()->toJson(); $path = tempnam(sys_get_temp_dir(), 'settlement-weight'); $out = fopen($path, 'wb'); try { app(StatementExporter::class)->export($id, $out); } finally { fclose($out); } try { $book = IOFactory::load($path); $grid = $book->getSheetByName('对账')->toArray(null, false, false); $headers = array_shift($grid); $rows = collect($grid)->keyBy(fn ($row) => $row[array_search('快递单号', $headers, true)]); foreach ($cases as [$waybill, $bill, $weight, $calc, $charge, $expectedWeight, $expectedWeightDiff, $expectedFreightDiff]) { $row = $rows[$waybill]; $expectedWeight === null ? $this->assertNull($row[array_search('结算重量', $headers, true)]) : $this->assertEquals($expectedWeight, $row[array_search('结算重量', $headers, true)]); $expectedWeightDiff === null ? $this->assertNull($row[array_search('重量差', $headers, true)]) : $this->assertEquals($expectedWeightDiff, $row[array_search('重量差', $headers, true)]); $this->assertEquals($expectedFreightDiff, $row[array_search('运费差', $headers, true)]); $this->assertEquals($charge, $row[array_search('计费重量', $headers, true)]); $this->assertEquals($calc, $row[array_search('核算运费', $headers, true)]); } $book->disconnectWorksheets(); } finally { @unlink($path); } foreach (['courier-payables', 'courier-payables-final'] as $section) { $this->get(route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', 'cb_view' => 'statement', 'statement' => $id, 'month' => '2026-11']))->assertOk()->assertSeeText('结算重量'); $query = ['cb_view' => 'details', 'month' => '2026-11', 'group_id' => $group, 'resolution' => 'all']; $this->get(route('business.pages.show', ['section' => $section, 'page' => 'courier-bill-reconciliation', ...$query])) ->assertOk()->assertSeeText('结算重量')->assertSeeText('重量差'); $response = $this->get(route("business.$section.courier-bills.export-details", $query))->assertOk(); file_put_contents($path, $response->streamedContent()); try { $book = IOFactory::load($path);$grid = $book->getActiveSheet()->toArray(null, false, false);$headers = array_shift($grid); $low = collect($grid)->first(fn ($row) => $row[array_search('快递单号', $headers, true)] === 'WTLOW0001'); $this->assertEquals(2, $low[array_search('结算重量', $headers, true)]); $this->assertEquals(0, $low[array_search('重量差', $headers, true)]); $this->assertEquals(0, $low[array_search('运费差', $headers, true)]); $book->disconnectWorksheets(); } finally { @unlink($path); } } $this->assertSame($before, $db->table('courier_bill_lines')->where('statement_id', $id)->orderBy('id')->get()->toJson()); foreach (['中铁物流', '武进安能'] as $name) { $line = (object) ['bill_amount'=>8, 'bill_weight'=>2, 'calc_freight'=>10, 'charge_weight'=>5, 'self_amount'=>CourierBillMatcher::selfAmount(Category::DIFF,10,8,null,$name), 'category'=>Category::DIFF, 'resolution'=>null, 'express_name'=>$name]; $this->assertSame(['settlement_weight'=>5.0,'weight_diff'=>-3.0,'freight_diff'=>-2.0], \App\Support\CourierBills\SettlementComparison::courier($line)); } $line->resolution = 'agree'; $line->self_amount = 8; $this->assertSame(['settlement_weight'=>2.0,'weight_diff'=>0.0,'freight_diff'=>0.0], \App\Support\CourierBills\SettlementComparison::courier($line)); $line->resolution = 'exclude'; $line->self_amount = 0; $this->assertSame(['settlement_weight'=>0.0,'weight_diff'=>2.0,'freight_diff'=>8.0], \App\Support\CourierBills\SettlementComparison::courier($line)); } public function test_freight_settlement_caps_at_billed_amount_but_keeps_missing_values_and_manual_decisions(): void { foreach ([Category::MATCH, Category::DIFF, Category::UNCALCULATED, Category::PRIOR] as $category) { $this->assertEquals(8, CourierBillMatcher::selfAmount($category, 10, 8, null)); $this->assertEquals(10, CourierBillMatcher::selfAmount($category, 10, 12, null)); $this->assertEquals(0, CourierBillMatcher::selfAmount($category, 10, 0, null)); $this->assertNull(CourierBillMatcher::selfAmount($category, null, 8, null)); $this->assertEquals(10, CourierBillMatcher::selfAmount($category, 10, null, null)); } $this->assertEquals(12, CourierBillMatcher::selfAmount(Category::DIFF, 10, 12, 'agree')); foreach (['exclude', 'defer'] as $resolution) $this->assertEquals(0, CourierBillMatcher::selfAmount(Category::DIFF, 10, 8, $resolution)); $this->assertEquals(0, CourierBillMatcher::selfAmount(Category::DUPLICATE, 10, 8, null)); $this->assertEquals(8, CourierBillMatcher::selfAmount(Category::DIFF, 10, 8.004, null)); $this->assertEquals(8.01, CourierBillMatcher::selfAmount(Category::DIFF, 10, 8.006, null)); } public function test_actual_freight_exceptions_are_exact_names_and_preserve_manual_decisions(): void { foreach (['中铁物流', '武进安能'] as $name) { foreach ([Category::MATCH, Category::DIFF, Category::PRIOR] as $category) { foreach ([0, 8, 12, null] as $bill) $this->assertSame(10.0, CourierBillMatcher::selfAmount($category, 10, $bill, null, $name)); } $this->assertNull(CourierBillMatcher::selfAmount(Category::UNCALCULATED, null, 8, null, $name)); $this->assertSame(8.0, CourierBillMatcher::selfAmount(Category::DIFF, 10, 8, 'agree', $name)); foreach (['exclude', 'defer'] as $resolution) $this->assertSame(0.0, CourierBillMatcher::selfAmount(Category::DIFF, 10, 8, $resolution, $name)); $this->assertSame(0.0, CourierBillMatcher::selfAmount(Category::DUPLICATE, 10, 8, null, $name)); } foreach (['中铁客退', '中铁物流其他', '武进安能其他'] as $name) $this->assertEquals(8, CourierBillMatcher::selfAmount(Category::DIFF, 10, 8, null, $name)); } public function test_actual_freight_rule_applies_to_matching_manual_reset_and_scoped_historical_refresh(): void { Storage::fake('local');$db=CourierBillMatcher::db();$store=app(CourierBillStore::class); $store->saveGroup(null,['name'=>'中铁物流','express_names'=>['中铁物流','武进安能','中铁客退']]); $shipping=app(\App\Support\CourierBills\ShippingLookup::class)->connection(); foreach (['中铁物流','武进安能','中铁客退'] as $i=>$name) { $shipping->table('jst_oms_shipping')->insert(['source_id'=>$i+1,'express_no'=>'ACTUAL'.($i+1),'express_name1'=>$name,'ship_time'=>'2026-11-03 09:00:00']); $shipping->table('jst_oms_shipping_freight')->insert(['source_id'=>$i+1,'status'=>'OK','freight'=>10,'charge_weight'=>2]); } $path=tempnam(sys_get_temp_dir(),'actual').'.csv';file_put_contents($path,"单号,运费\nACTUAL1,8\nACTUAL2,8\nACTUAL3,8\n"); try {$file=$store->upload([new \Illuminate\Http\UploadedFile($path,'中铁物流.csv',null,null,true)],'2026-11',[],'测试')[0];} finally {@unlink($path);} $id=(int)$db->table('courier_bill_files')->where('id',$file)->value('statement_id'); $this->assertEquals([10,10,8],$db->table('courier_bill_lines')->where('statement_id',$id)->orderBy('row_no')->pluck('self_amount')->all()); $line=(int)$db->table('courier_bill_lines')->where('waybill_no','ACTUAL1')->value('id'); app(CourierBillMatcher::class)->resolve($id,[$line],'agree','测试'); $this->assertEquals(8,$db->table('courier_bill_lines')->where('id',$line)->value('self_amount')); app(CourierBillMatcher::class)->resolve($id,[$line],null,'测试'); $this->assertEquals(10,$db->table('courier_bill_lines')->where('id',$line)->value('self_amount')); $db->table('courier_bill_lines')->where('statement_id',$id)->update(['self_amount'=>8]); $db->table('bill_surcharge_matches')->insert(['scope'=>'courier','owner_id'=>$id,'file_id'=>$file,'sheet'=>'附加费','row_no'=>5,'bill_month'=>'2026-11','waybill_no'=>'ACTUAL1','item'=>'上楼费','summary_label'=>'附加费','bill_amount'=>5,'self_amount'=>9,'match_status'=>'已匹配','match_note'=>'保持核价']); $options=['--statement'=>[$id],'--express'=>['中铁物流','武进安能']]; $this->artisan('courier-bills:refresh-settlement',$options)->assertSuccessful(); $this->assertEquals(24,$db->table('courier_bill_lines')->sum('self_amount')); $this->artisan('courier-bills:refresh-settlement',$options+['--apply'=>true])->assertSuccessful(); $this->assertEquals([10,10,8],$db->table('courier_bill_lines')->orderBy('row_no')->pluck('self_amount')->all()); $this->assertEquals(9,$db->table('bill_surcharge_matches')->value('self_amount')); $out=tmpfile();app(StatementExporter::class)->export($id,$out);rewind($out);$path=tempnam(sys_get_temp_dir(),'actual-export');file_put_contents($path,stream_get_contents($out));fclose($out); try { $book=IOFactory::load($path);$grid=$book->getSheetByName('对账')->toArray(null,false,false);$headers=$grid[0]; $calcCol=array_search('核算运费',$headers,true);$settleCol=array_search('结算运费',$headers,true); $this->assertSame('结算重量',$headers[$calcCol+1]); $this->assertSame($calcCol+2,$settleCol);$this->assertEquals(10,$grid[1][$settleCol]); $this->assertSame(1,count(array_filter($headers,fn($h)=>$h==='结算运费')));$book->disconnectWorksheets(); } finally {@unlink($path);} } public function test_wujin_subsidy_is_directional_summed_once_and_exported_separately(): void { Storage::fake('local'); $db = CourierBillMatcher::db(); $group = app(CourierBillStore::class)->saveGroup(null, ['name' => '武进韵达', 'express_names' => ['武进韵达']]); $id = $db->table('courier_bill_statements')->insertGetId(['group_id' => $group, 'bill_month' => '2026-11', 'status' => 'open']); $cases = [ ['ELIGIBLE', '武进韵达', 12, 10, 120, 100, Category::DIFF, null], ['BOUNDARY', '武进韵达', 11, 10, 120, 100, Category::DIFF, null], ['REVERSED', '武进韵达', 8, 10, 120, 100, Category::DIFF, null], ['LOWBILL', '武进韵达', 12, 10, 80, 100, Category::DIFF, null], ['OTHER', '武进韵达快运', 12, 10, 120, 100, Category::DIFF, null], ['DUPLICATE', '武进韵达', 12, 10, 120, 100, Category::DUPLICATE, null], ['EXCLUDED', '武进韵达', 12, 10, 120, 100, Category::DIFF, 'exclude'], ['AGREED', '武进韵达', 12, 10, 120, 100, Category::DIFF, 'agree'], ['CENTS1', '武进韵达', 12, 10, 100.01, 100, Category::DIFF, null], ['CENTS2', '武进韵达', 12, 10, 100.01, 100, Category::DIFF, null], ]; foreach ($cases as $i => [$no,$courier,$billWeight,$chargeWeight,$bill,$calc,$category,$resolution]) { $db->table('courier_bill_lines')->insert(['statement_id'=>$id,'file_id'=>0,'sheet'=>'账单','row_no'=>$i+1,'raw'=>'[]', 'waybill_no'=>$no,'express_name'=>$courier,'bill_weight'=>$billWeight,'charge_weight'=>$chargeWeight, 'bill_amount'=>$bill,'calc_freight'=>$calc,'category'=>$category,'resolution'=>$resolution, 'self_amount'=>CourierBillMatcher::selfAmount($category,$calc,$bill,$resolution)]); } $subsidy = app(WeightDifferenceSubsidy::class)->summary($id); $this->assertSame(3, $subsidy['count']); $this->assertEquals(20.02, $subsidy['difference']); $this->assertEquals(10.01, $subsidy['amount']); app(CourierBillMatcher::class)->refreshSummary($id); $item = $db->table('courier_bill_summary_items')->where('statement_id',$id)->where('label',WeightDifferenceSubsidy::LABEL)->sole(); $this->assertEquals(10.01,$item->self_amount); $this->assertNull($item->bill_amount); $mapped = SummaryMapper::map([['label'=>$item->label,'bill'=>null,'self'=>(float)$item->self_amount]]); $this->assertEquals(10.01,$mapped['J']); $this->assertEquals(0,$mapped['AS']); $this->assertSame([],SummaryTemplate::fromLabels([WeightDifferenceSubsidy::LABEL],1)); $this->assertSame([],SummaryTemplate::apply([WeightDifferenceSubsidy::LABEL],[],12)); $out = tmpfile();app(StatementExporter::class)->export($id,$out);rewind($out); $path = tempnam(sys_get_temp_dir(),'subsidy');file_put_contents($path,stream_get_contents($out));fclose($out); try {$book=IOFactory::load($path);$sheet=$book->getSheetByName('重量差补款-'.$id);$this->assertNotNull($sheet);$this->assertEquals(10.01,$sheet->getCell('G6')->getValue());$book->disconnectWorksheets();} finally {@unlink($path);} $db->table('courier_bill_lines')->where('statement_id',$id)->update(['resolution'=>'exclude']); app(CourierBillMatcher::class)->refreshSummary($id); $this->assertSame(0,$db->table('courier_bill_summary_items')->where('statement_id',$id)->where('label',WeightDifferenceSubsidy::LABEL)->count()); } public function test_refresh_streams_existing_lines_and_does_not_touch_confirmed_statements(): void { Storage::fake('local');$db=CourierBillMatcher::db(); $group=app(CourierBillStore::class)->saveGroup(null,['name'=>'测试快递','express_names'=>['测试快递']]); $ids=[];foreach(['open','confirmed']as$status){$ids[$status]=$db->table('courier_bill_statements')->insertGetId(['group_id'=>$group,'bill_month'=>$status==='open'?'2026-11':'2026-10','status'=>$status,'line_count'=>1001]);} $rows=[];for($i=1;$i<=1001;$i++)$rows[]=['statement_id'=>$ids['open'],'file_id'=>0,'sheet'=>'账单','row_no'=>$i,'raw'=>'[]','waybill_no'=>'CAP'.$i,'express_name'=>'测试快递','bill_amount'=>8,'calc_freight'=>10,'self_amount'=>10,'category'=>Category::DIFF]; foreach(array_chunk($rows,100)as$chunk)$db->table('courier_bill_lines')->insert($chunk); $confirmed=$rows[0];$confirmed['statement_id']=$ids['confirmed'];$db->table('courier_bill_lines')->insert($confirmed); $this->artisan('courier-bills:refresh-settlement',['--all-open'=>true])->assertSuccessful(); $this->assertEquals(10010,$db->table('courier_bill_lines')->where('statement_id',$ids['open'])->sum('self_amount')); $this->artisan('courier-bills:refresh-settlement',['--all-open'=>true,'--apply'=>true])->assertSuccessful(); $this->assertEquals(8008,$db->table('courier_bill_lines')->where('statement_id',$ids['open'])->sum('self_amount')); $this->assertEquals(10,$db->table('courier_bill_lines')->where('statement_id',$ids['confirmed'])->value('self_amount')); $this->assertNotEmpty(Storage::disk('local')->allFiles('private/courier-settlement')); $this->artisan('courier-bills:refresh-settlement',['--all-open'=>true,'--apply'=>true])->assertSuccessful(); $this->assertEquals(8008,$db->table('courier_bill_lines')->where('statement_id',$ids['open'])->sum('self_amount')); } public function test_refresh_caps_existing_surcharges_without_repricing_sources(): void { Storage::fake('local');$db=CourierBillMatcher::db(); $group=app(CourierBillStore::class)->saveGroup(null,['name'=>'测试快递','express_names'=>['测试快递']]); $id=$db->table('courier_bill_statements')->insertGetId(['group_id'=>$group,'bill_month'=>'2026-11','status'=>'open','line_count'=>1]); $db->table('courier_bill_lines')->insert(['statement_id'=>$id,'file_id'=>0,'sheet'=>'账单','row_no'=>1,'raw'=>'[]','waybill_no'=>'CAP1','bill_amount'=>8,'calc_freight'=>10,'self_amount'=>10,'category'=>Category::DIFF]); $db->table('courier_bill_summary_items')->insert(['statement_id'=>$id,'label'=>'11月运费','bill_amount'=>8,'self_amount'=>10,'manual'=>false,'sort'=>1]); $db->table('courier_bill_summary_items')->insert(['statement_id'=>$id,'label'=>'配件补发扣款','bill_amount'=>null,'self_amount'=>-12,'manual'=>false,'sort'=>2]); $db->table('courier_bill_summary_items')->insert(['statement_id'=>$id,'label'=>'补10月记账','bill_amount'=>null,'self_amount'=>15,'manual'=>false,'sort'=>3]); $db->table('courier_bill_summary_items')->insert(['statement_id'=>$id,'label'=>'手工调整','bill_amount'=>5,'self_amount'=>3,'manual'=>true,'sort'=>4]); $db->table('courier_bill_files')->insert(['id'=>100,'statement_id'=>$id,'bill_month'=>'2026-11','original_name'=>'测试.xlsx','stored_path'=>'test.xlsx','status'=>'parsed']); $db->table('courier_bill_lines')->where('statement_id',$id)->update(['file_id'=>100]); $db->table('courier_bill_surcharges')->insert(['file_id'=>100,'sheet'=>'附加费','row_no'=>2,'waybill_no'=>'CAP1','item'=>'上楼费','amount'=>20]); $db->table('bill_surcharge_matches')->insert(['scope'=>'courier','owner_id'=>$id,'file_id'=>0,'sheet'=>'附加费','row_no'=>2,'bill_month'=>'2026-11','waybill_no'=>'CAP1','item'=>'上楼费','summary_label'=>'附加费','bill_amount'=>20,'self_amount'=>30,'match_status'=>'已匹配','match_note'=>'已核实费用']); $this->artisan('courier-bills:refresh-settlement',['--all-open'=>true,'--apply'=>true])->assertSuccessful(); $this->assertEquals(20,$db->table('bill_surcharge_matches')->value('self_amount')); $this->assertEquals(20,$db->table('courier_bill_summary_items')->where('label','附加费')->value('self_amount')); $this->assertEquals(8,$db->table('courier_bill_lines')->value('self_amount')); $this->assertEquals(-12,$db->table('courier_bill_summary_items')->where('label','配件补发扣款')->value('self_amount')); $this->assertEquals(15,$db->table('courier_bill_summary_items')->where('label','补10月记账')->value('self_amount')); $this->assertEquals(3,$db->table('courier_bill_summary_items')->where('label','手工调整')->value('self_amount')); } }