114 lines
9.9 KiB
PHP
114 lines
9.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Support\CourierBills\{Category, CourierBillMatcher, CourierBillStore, StatementExporter, SummaryMapper, SummaryTemplate, WeightDifferenceSubsidy};
|
|
use Illuminate\Support\Facades\Storage;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class CourierSettlementRulesTest extends FeatureTestCase
|
|
{
|
|
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_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'));
|
|
}
|
|
}
|