feat: enhance agent settlement features and improve data access controls

- Added new section in AGENTS.md detailing learned workspace facts for better understanding of settlement processes.
- Updated AgentNodeDestroyController to remove unnecessary checks for admin users.
- Enhanced AgentSettlement controllers to assert permissions for finance adjustments and bill operations.
- Improved query scopes in AgentSettlement services to ensure proper data access based on admin roles.
- Refactored methods in SettlementPartyEnrichment for better bill row enrichment and data handling.
- Introduced new methods in AdminAgentSettlementScope for managing agent node visibility and finance adjustments.
This commit is contained in:
2026-06-12 15:59:05 +08:00
parent e14b7b4569
commit 980f3c9593
47 changed files with 2403 additions and 187 deletions

View File

@@ -31,3 +31,162 @@ test('settlement bills index api resource is configured after migrations', funct
->assertOk()
->assertJsonPath('data.items', fn ($items) => is_array($items));
});
test('settlement bill show returns enriched party labels', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$childId = (int) DB::table('agent_nodes')->insertGetId([
'admin_site_id' => $siteId,
'parent_id' => $rootId,
'code' => 'bill_show_child',
'name' => 'Bill Show Child',
'depth' => 1,
'path' => '/'.$rootId.'/',
'status' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('agent_nodes')->where('id', $childId)->update([
'path' => '/'.$rootId.'/'.$childId.'/',
]);
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subWeek(),
'period_end' => now(),
'status' => 'closed',
'created_at' => now(),
'updated_at' => now(),
]);
$billId = (int) DB::table('settlement_bills')->insertGetId([
'settlement_period_id' => $periodId,
'bill_type' => 'agent',
'owner_type' => 'agent',
'owner_id' => $childId,
'counterparty_type' => 'agent',
'counterparty_id' => $rootId,
'net_amount' => 6400,
'unpaid_amount' => 6400,
'paid_amount' => 0,
'status' => 'confirmed',
'created_at' => now(),
'updated_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'bill_show_super',
'name' => 'Bill Show Super',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($admin);
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
$rootName = (string) DB::table('agent_nodes')->where('id', $rootId)->value('name');
$this->withHeader('Authorization', 'Bearer '.$token)
->getJson('/api/v1/admin/settlement-bills/'.$billId)
->assertOk()
->assertJsonPath('data.bill.owner_party_label', 'Bill Show Child')
->assertJsonPath('data.bill.superior_agent_label', $rootName);
$this->withHeader('Authorization', 'Bearer '.$token)
->getJson('/api/v1/admin/settlement-bills?bill_id='.$billId)
->assertOk()
->assertJsonPath('data.items.0.owner_party_label', 'Bill Show Child')
->assertJsonPath('data.items.0.superior_agent_label', $rootName);
});
test('settlement bill show returns downline share breakdown for parent agent bill', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$parentId = (int) DB::table('agent_nodes')->insertGetId([
'admin_site_id' => $siteId,
'parent_id' => $rootId,
'code' => 'downline_parent',
'name' => 'Downline Parent',
'depth' => 1,
'path' => '/'.$rootId.'/',
'status' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('agent_nodes')->where('id', $parentId)->update([
'path' => '/'.$rootId.'/'.$parentId.'/',
]);
$childId = (int) DB::table('agent_nodes')->insertGetId([
'admin_site_id' => $siteId,
'parent_id' => $parentId,
'code' => 'downline_child',
'name' => 'Downline Child',
'depth' => 2,
'path' => '/'.$rootId.'/'.$parentId.'/',
'status' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('agent_nodes')->where('id', $childId)->update([
'path' => '/'.$rootId.'/'.$parentId.'/'.$childId.'/',
]);
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subWeek(),
'period_end' => now(),
'status' => 'closed',
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('settlement_bills')->insert([
'settlement_period_id' => $periodId,
'bill_type' => 'agent',
'owner_type' => 'agent',
'owner_id' => $childId,
'counterparty_type' => 'agent',
'counterparty_id' => $parentId,
'net_amount' => 3916,
'unpaid_amount' => 3916,
'paid_amount' => 0,
'status' => 'confirmed',
'meta_json' => json_encode(['share_profit' => 484]),
'created_at' => now(),
'updated_at' => now(),
]);
$parentBillId = (int) DB::table('settlement_bills')->insertGetId([
'settlement_period_id' => $periodId,
'bill_type' => 'agent',
'owner_type' => 'agent',
'owner_id' => $parentId,
'counterparty_type' => 'agent',
'counterparty_id' => $rootId,
'gross_win_loss' => 4400,
'net_amount' => 3520,
'unpaid_amount' => 3520,
'paid_amount' => 0,
'status' => 'confirmed',
'meta_json' => json_encode(['share_profit' => 396]),
'created_at' => now(),
'updated_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'downline_share_super',
'name' => 'Downline Share Super',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($admin);
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
$this->withHeader('Authorization', 'Bearer '.$token)
->getJson('/api/v1/admin/settlement-bills/'.$parentBillId)
->assertOk()
->assertJsonPath('data.downline_shares.total', 484)
->assertJsonPath('data.downline_shares.items.0.owner_label', 'Downline Child')
->assertJsonPath('data.downline_shares.items.0.share_profit', 484);
});