feat: 增强代理结算和账单管理功能

- 在多个控制器中引入 SettlementPartyEnrichment 服务,以优化代理结算和账单的处理逻辑。
- 更新 AgentSettlementBillIndexController 和 AgentSettlementBillShowController,支持根据账单 ID 和关键字进行查询。
- 在 AgentSettlementPeriodCloseController 中添加对站点管理权限的验证,确保只有具备相应权限的管理员能够关闭账期。
- 在 AgentSettlementPeriodIndexController 中更新账期数据的返回格式,提升数据的完整性和可用性。
- 引入对相对占成比例的支持,增强代理资料的管理能力,确保数据一致性。
This commit is contained in:
2026-06-05 18:00:56 +08:00
parent a44679665d
commit 2d32f006c5
63 changed files with 4893 additions and 288 deletions

View File

@@ -84,16 +84,16 @@ test('parent agent can sync delegation grants for direct child', function (): vo
]);
grantSuperAdminRole($super);
$parent = $service->createChild($super, [
$parent = $service->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'deleg-parent',
'name' => 'Deleg Parent',
]);
$child = $service->createChild($super, [
]));
$child = $service->createChild($super, agentChildPayload([
'parent_id' => $parent->id,
'code' => 'deleg-child',
'name' => 'Deleg Child',
]);
]));
$parentAdmin = AdminUser::query()->create([
'username' => 'deleg_parent_admin',
@@ -139,11 +139,11 @@ test('delegation ceiling blocks role permissions beyond child grants', function
]);
grantSuperAdminRole($super);
$branch = $service->createChild($super, [
$branch = $service->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'ceil-branch',
'name' => 'Ceil Branch',
]);
]));
$viewActionId = (int) DB::table('admin_menu_actions')
->where('permission_code', 'agent.node.view')

View File

@@ -0,0 +1,269 @@
<?php
use App\Models\AdminUser;
use App\Models\Player;
use App\Support\PlayerFundingMode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
test('credit ledger biz_type payment_record excludes credit rows', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$siteId = (int) $site->id;
$siteCode = (string) $site->code;
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subDay(),
'period_end' => now()->addDay(),
'status' => 'closed',
'created_at' => now(),
'updated_at' => now(),
]);
$player = Player::query()->create([
'site_code' => $siteCode,
'site_player_id' => 'native:ledger-filter',
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'ledger_filter_user',
'default_currency' => 'NPR',
'status' => 0,
]);
DB::table('credit_ledger')->insert([
'owner_type' => 'player',
'owner_id' => $player->id,
'amount' => -100,
'reason' => 'bet_hold',
'created_at' => now(),
'updated_at' => now(),
]);
$billId = (int) DB::table('settlement_bills')->insertGetId([
'settlement_period_id' => $periodId,
'bill_type' => 'player',
'owner_type' => 'player',
'owner_id' => $player->id,
'counterparty_type' => 'agent',
'counterparty_id' => 1,
'net_amount' => 100,
'unpaid_amount' => 0,
'paid_amount' => 100,
'status' => 'settled',
'created_at' => now(),
'updated_at' => now(),
]);
$paymentId = (int) DB::table('payment_records')->insertGetId([
'settlement_bill_id' => $billId,
'payer_type' => 'player',
'payer_id' => $player->id,
'payee_type' => 'agent',
'payee_id' => 1,
'amount' => 100,
'status' => 'confirmed',
'created_at' => now(),
'updated_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'ledger_filter_super',
'name' => 'Ledger Filter',
'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/credit-ledger?admin_site_id='.$siteId.'&settlement_period_id='.$periodId.'&reason=payment_record')
->assertOk()
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.entry_kind', 'payment')
->assertJsonPath('data.items.0.id', $paymentId);
$this->withHeader('Authorization', 'Bearer '.$token)
->getJson('/api/v1/admin/credit-ledger?admin_site_id='.$siteId.'&settlement_period_id='.$periodId.'&txn_no=CL')
->assertOk()
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.entry_kind', 'credit');
});
test('credit ledger index includes payment on agent bill', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$siteId = (int) $site->id;
$agentId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subDay(),
'period_end' => now()->addDay(),
'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' => $agentId,
'counterparty_type' => 'platform',
'counterparty_id' => 0,
'net_amount' => 3000,
'unpaid_amount' => 0,
'paid_amount' => 3000,
'status' => 'settled',
'created_at' => now(),
'updated_at' => now(),
]);
$paymentId = (int) DB::table('payment_records')->insertGetId([
'settlement_bill_id' => $billId,
'payer_type' => 'agent',
'payer_id' => $agentId,
'payee_type' => 'platform',
'payee_id' => 0,
'amount' => 3000,
'status' => 'confirmed',
'created_at' => now(),
'updated_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'ledger_agent_bill_super',
'name' => 'Agent Bill Ledger',
'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/credit-ledger?admin_site_id='.$siteId.'&settlement_period_id='.$periodId.'&reason=payment_record')
->assertOk()
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.entry_kind', 'payment')
->assertJsonPath('data.items.0.id', $paymentId)
->assertJsonPath('data.items.0.bill_type', 'agent');
});
test('credit ledger entry_kind share returns share ledger rows', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$siteId = (int) $site->id;
$siteCode = (string) $site->code;
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subDay(),
'period_end' => now()->addDay(),
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$player = Player::query()->create([
'site_code' => $siteCode,
'site_player_id' => 'native:share-ledger',
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'share_ledger_user',
'default_currency' => 'NPR',
'status' => 0,
'agent_node_id' => $rootId,
]);
$settledAt = now()->toDateTimeString();
$shareId = (int) DB::table('share_ledger')->insertGetId([
'ticket_item_id' => createShareLedgerTicketItem($player),
'player_id' => $player->id,
'agent_node_id' => $rootId,
'agent_path' => json_encode([$rootId]),
'share_snapshot' => json_encode([
'total_shares' => ['ROOT' => 1.0],
'chain_codes' => ['ROOT'],
]),
'game_win_loss' => 1200,
'basic_rebate' => 0,
'shared_net_win_loss' => 1200,
'allocations_json' => json_encode([]),
'settled_at' => $settledAt,
'created_at' => $settledAt,
'updated_at' => $settledAt,
]);
$admin = AdminUser::query()->create([
'username' => 'share_ledger_super',
'name' => 'Share Ledger',
'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/credit-ledger?admin_site_id='.$siteId.'&settlement_period_id='.$periodId.'&entry_kind=share')
->assertOk()
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.entry_kind', 'share')
->assertJsonPath('data.items.0.id', $shareId)
->assertJsonPath('data.items.0.biz_type', 'share_ledger')
->assertJsonPath('data.items.0.signed_amount', 1200);
});
function createShareLedgerTicketItem(Player $player): int
{
$draw = \App\Models\Draw::query()->create([
'draw_no' => 'DRAW-SHARE-LEDGER',
'business_date' => now()->toDateString(),
'sequence_no' => random_int(1, 9999),
'status' => \App\Lottery\DrawStatus::Open->value,
'current_result_version' => 0,
'settle_version' => 0,
'is_reopened' => false,
]);
$orderId = (int) DB::table('ticket_orders')->insertGetId([
'order_no' => 'ORD-SHARE-LEDGER',
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 1000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 1000,
'total_estimated_payout' => 0,
'status' => 'confirmed',
'submit_source' => 'h5',
'client_trace_id' => null,
'created_at' => now(),
'updated_at' => now(),
]);
return (int) DB::table('ticket_items')->insertGetId([
'ticket_no' => 'T-SHARE-LEDGER',
'order_id' => $orderId,
'player_id' => $player->id,
'draw_id' => $draw->id,
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 2,
'unit_bet_amount' => 1000,
'total_bet_amount' => 1000,
'rebate_rate_snapshot' => 0,
'commission_rate_snapshot' => 0,
'actual_deduct_amount' => 1000,
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'settled_lose',
'win_amount' => 0,
'jackpot_win_amount' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}

View File

@@ -68,6 +68,84 @@ test('admin credit ledger index returns credit player ledger rows', function ():
->assertJsonPath('data.items.0.available_actions', ['view_player']);
});
test('admin credit ledger simple display merges release and loss per ticket', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$siteId = (int) $site->id;
$siteCode = (string) $site->code;
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subDay(),
'period_end' => now()->addDay(),
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$player = Player::query()->create([
'site_code' => $siteCode,
'site_player_id' => 'native:simple-flow',
'auth_source' => PlayerAuthSource::LOTTERY_NATIVE,
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'simple_flow',
'default_currency' => 'NPR',
'status' => 0,
]);
$now = now();
DB::table('credit_ledger')->insert([
[
'owner_type' => 'player',
'owner_id' => $player->id,
'amount' => -1200,
'reason' => 'bet_hold',
'ref_type' => 'bet',
'ref_id' => null,
'created_at' => $now->copy()->subMinutes(2),
'updated_at' => $now->copy()->subMinutes(2),
],
[
'owner_type' => 'player',
'owner_id' => $player->id,
'amount' => 1200,
'reason' => 'bet_hold_release',
'ref_type' => 'ticket_item',
'ref_id' => 88,
'created_at' => $now,
'updated_at' => $now,
],
[
'owner_type' => 'player',
'owner_id' => $player->id,
'amount' => -1200,
'reason' => 'game_settlement_loss',
'ref_type' => 'ticket_item',
'ref_id' => 88,
'created_at' => $now,
'updated_at' => $now,
],
]);
$admin = AdminUser::query()->create([
'username' => 'simple_flow_super',
'name' => 'Simple Flow',
'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/credit-ledger?admin_site_id='.$siteId.'&settlement_period_id='.$periodId.'&bet_flow_display=simple')
->assertOk()
->assertJsonPath('data.ledger_source', 'credit_ledger')
->assertJsonPath('data.total', 1)
->assertJsonCount(1, 'data.items')
->assertJsonPath('data.items.0.biz_type', 'game_settlement')
->assertJsonPath('data.items.0.signed_amount', -1200);
});
test('settlement periods include pipeline credit and share counts', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$siteId = (int) $site->id;

View File

@@ -0,0 +1,172 @@
<?php
use App\Models\AgentProfile;
use App\Models\Draw;
use App\Models\Player;
use App\Models\TicketItem;
use App\Models\TicketOrder;
use App\Lottery\DrawStatus;
use App\Services\Agent\AgentNodeService;
use App\Services\AgentSettlement\AgentGameSettlementRecorder;
use App\Services\AgentSettlement\AgentSettlementPeriodCloseService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
test('period close aggregates share ledger written by game settlement recorder', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$super = \App\Models\AdminUser::query()->create([
'username' => 'pipe_super',
'name' => 'Super',
'email' => null,
'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$leaf = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'PIPE-C',
'name' => 'Pipe C',
'username' => 'pipe_c',
'total_share_rate' => 25,
'credit_limit' => 100_000,
'default_player_rebate' => 0.005,
]));
AgentProfile::query()->where('agent_node_id', $rootId)->update([
'total_share_rate' => 100,
'default_player_rebate' => 0.005,
]);
$player = Player::query()->create([
'site_code' => $siteCode,
'agent_node_id' => $leaf->id,
'site_player_id' => 'pipe-p1',
'username' => 'pipeplayer',
'nickname' => null,
'auth_source' => 'lottery_native',
'funding_mode' => 'credit',
'default_currency' => 'NPR',
'status' => 0,
]);
DB::table('player_credit_accounts')->insert([
'player_id' => $player->id,
'credit_limit' => 50_000,
'used_credit' => 0,
'frozen_credit' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
DB::table('player_rebate_profiles')->insert([
'player_id' => $player->id,
'game_type' => '*',
'rebate_rate' => 0.005,
'extra_rebate_rate' => 0,
'inherit_from_agent' => true,
'created_at' => now(),
'updated_at' => now(),
]);
$betMinor = 10_000;
$draw = Draw::query()->create([
'draw_no' => 'PIPE-DRAW-1',
'business_date' => now()->toDateString(),
'sequence_no' => 88,
'status' => DrawStatus::Open->value,
'current_result_version' => 0,
'settle_version' => 0,
'is_reopened' => false,
]);
$order = TicketOrder::query()->create([
'order_no' => 'ORD-PIPE-1',
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => $betMinor,
'total_rebate_amount' => 0,
'total_actual_deduct' => $betMinor,
'total_estimated_payout' => 0,
'status' => 'confirmed',
'submit_source' => 'h5',
'client_trace_id' => 'pipe-trace-1',
'created_at' => now(),
'updated_at' => now(),
]);
$item = TicketItem::query()->create([
'ticket_no' => 'T-PIPE-1',
'order_id' => $order->id,
'player_id' => $player->id,
'draw_id' => $draw->id,
'original_number' => '1234',
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 2,
'digit_slot' => null,
'bet_mode' => 'single',
'unit_bet_amount' => $betMinor,
'total_bet_amount' => $betMinor,
'rebate_rate_snapshot' => '0.0000',
'commission_rate_snapshot' => '0.0000',
'actual_deduct_amount' => $betMinor,
'odds_snapshot_json' => null,
'rule_snapshot_json' => null,
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'settled_lose',
'win_amount' => 0,
'jackpot_win_amount' => 0,
]);
$item->setRelation('player', $player);
$recorder = app(AgentGameSettlementRecorder::class);
expect($recorder->shouldRecord($item))->toBeTrue();
$recorder->recordForTicketItem($item, 0, 'settled_lose');
$item->refresh();
expect($item->agent_settled_at)->not->toBeNull()
->and($item->share_snapshot)->not->toBeNull();
$ledger = DB::table('share_ledger')->where('ticket_item_id', $item->id)->first();
expect($ledger)->not->toBeNull()
->and((int) $ledger->game_win_loss)->toBe($betMinor);
$settledAt = (string) $ledger->settled_at;
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->parse($settledAt)->subDay(),
'period_end' => now()->parse($settledAt)->addDay(),
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$close = app(AgentSettlementPeriodCloseService::class)->closePeriod($periodId);
expect($close['player_count'])->toBe(1)
->and($close['bill_ids'])->not->toBeEmpty();
$playerBill = DB::table('settlement_bills')
->where('settlement_period_id', $periodId)
->where('bill_type', 'player')
->where('owner_id', $player->id)
->first();
expect($playerBill)->not->toBeNull()
->and((int) $playerBill->gross_win_loss)->toBe($betMinor)
->and((int) $playerBill->rebate_amount)->toBeGreaterThan(0);
});

View File

@@ -0,0 +1,128 @@
<?php
use App\Models\AdminUser;
use App\Services\Agent\AgentNodeService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
test('creating child agent with relative_share_rate calculates total_share_rate correctly', 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');
$super = AdminUser::query()->create([
'username' => 'rel_super',
'name' => 'Rel',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
// 创建 A总占成 20%
$agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'REL_A',
'name' => 'Agent A',
'username' => 'rel_agent_a',
'total_share_rate' => 20,
'credit_limit' => 100000,
]));
// 用 relative_share_rate 创建 B输入 50即 A 的 50% = 10%
$agentB = app(AgentNodeService::class)->createChild($super, array_merge(
agentChildPayload([
'parent_id' => $agentA->id,
'code' => 'REL_B',
'name' => 'Agent B',
'username' => 'rel_agent_b',
'credit_limit' => 50000,
]),
['relative_share_rate' => 50]
));
// 验证 B 的实际总占成是 10%
$profileB = DB::table('agent_profiles')->where('agent_node_id', $agentB->id)->first();
expect($profileB)->not->toBeNull();
expect((float) $profileB->total_share_rate)->toBe(10.0);
});
test('relative_share_rate 100 gives same total as parent', 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');
$super = AdminUser::query()->create([
'username' => 'rel_super2',
'name' => 'Rel2',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'REL2_A',
'name' => 'Agent A',
'username' => 'rel2_agent_a',
'total_share_rate' => 30,
'credit_limit' => 100000,
]));
$agentB = app(AgentNodeService::class)->createChild($super, array_merge(
agentChildPayload([
'parent_id' => $agentA->id,
'code' => 'REL2_B',
'name' => 'Agent B',
'username' => 'rel2_agent_b',
'credit_limit' => 50000,
]),
['relative_share_rate' => 100]
));
$profileB = DB::table('agent_profiles')->where('agent_node_id', $agentB->id)->first();
expect((float) $profileB->total_share_rate)->toBe(30.0);
});
test('relative_share_rate 0 creates agent with zero share', 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');
$super = AdminUser::query()->create([
'username' => 'rel_super3',
'name' => 'Rel3',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'REL3_A',
'name' => 'Agent A',
'username' => 'rel3_agent_a',
'total_share_rate' => 40,
'credit_limit' => 100000,
]));
$agentB = app(AgentNodeService::class)->createChild($super, array_merge(
agentChildPayload([
'parent_id' => $agentA->id,
'code' => 'REL3_B',
'name' => 'Agent B',
'username' => 'rel3_agent_b',
'credit_limit' => 50000,
]),
['relative_share_rate' => 0]
));
$profileB = DB::table('agent_profiles')->where('agent_node_id', $agentB->id)->first();
expect((float) $profileB->total_share_rate)->toBe(0.0);
});

View File

@@ -0,0 +1,238 @@
<?php
use App\Lottery\DrawStatus;
use App\Models\AdminUser;
use App\Models\Draw;
use App\Models\Player;
use App\Services\AgentSettlement\AgentSettlementReportQueryService;
use App\Services\AgentSettlement\SettlementPaymentService;
use App\Support\PlayerFundingMode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
test('record payment rejects bill that is not confirmed', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$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' => 'player',
'owner_type' => 'player',
'owner_id' => 1,
'counterparty_type' => 'agent',
'counterparty_id' => 1,
'net_amount' => 500,
'unpaid_amount' => 500,
'paid_amount' => 0,
'status' => 'pending_confirm',
'created_at' => now(),
'updated_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'pay_guard_super',
'name' => 'PayGuard',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($admin);
expect(fn () => app(SettlementPaymentService::class)->recordPayment($billId, 100, (int) $admin->id))
->toThrow(ValidationException::class);
expect(DB::table('payment_records')->where('settlement_bill_id', $billId)->exists())->toBeFalse();
});
test('settlement reports scope player win loss to agent subtree', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$service = app(\App\Services\Agent\AgentNodeService::class);
$super = AdminUser::query()->create([
'username' => 'report_scope_super',
'name' => 'Super',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$branch = $service->createChild($super, [
'parent_id' => $rootId,
'code' => 'report-branch',
'name' => 'Report Branch',
]);
$otherBranch = $service->createChild($super, [
'parent_id' => $rootId,
'code' => 'report-other',
'name' => 'Report Other',
]);
$periodStart = now()->subDay()->startOfDay()->toDateTimeString();
$periodEnd = now()->addDay()->endOfDay()->toDateTimeString();
$settledAt = now()->toDateTimeString();
foreach ([$branch, $otherBranch] as $node) {
$player = Player::query()->create([
'site_code' => $siteCode,
'site_player_id' => 'native:report-'.$node->code,
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'report_'.$node->code,
'default_currency' => 'NPR',
'status' => 0,
'agent_node_id' => $node->id,
]);
$ticketItemId = createReportScopeTicketItem($player, 'T-'.$node->code);
DB::table('share_ledger')->insert([
'ticket_item_id' => $ticketItemId,
'player_id' => $player->id,
'agent_node_id' => $node->id,
'agent_path' => json_encode([$node->id]),
'share_snapshot' => json_encode([
'total_shares' => [(string) $node->code => 1.0],
'chain_codes' => [(string) $node->code],
]),
'game_win_loss' => $node->id === $branch->id ? 1000 : 2000,
'basic_rebate' => 0,
'shared_net_win_loss' => $node->id === $branch->id ? 1000 : 2000,
'allocations_json' => json_encode([]),
'settled_at' => $settledAt,
'created_at' => $settledAt,
'updated_at' => $settledAt,
]);
}
$operator = AdminUser::query()->create([
'username' => 'report_scope_ops',
'name' => 'Ops',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantReportScopeAgentOperator($operator, $branch);
$reports = app(AgentSettlementReportQueryService::class);
$scoped = $reports->playerWinLoss($operator, 0, $periodStart, $periodEnd);
$all = $reports->playerWinLoss($super, 0, $periodStart, $periodEnd);
expect($scoped)->toHaveCount(1)
->and((int) $scoped[0]['game_win_loss'])->toBe(1000)
->and($all)->toHaveCount(2);
});
function createReportScopeTicketItem(Player $player, string $ticketNo): int
{
$draw = Draw::query()->create([
'draw_no' => 'DRAW-'.$ticketNo,
'business_date' => now()->toDateString(),
'sequence_no' => random_int(1, 9999),
'status' => DrawStatus::Open->value,
'current_result_version' => 0,
'settle_version' => 0,
'is_reopened' => false,
]);
$orderId = (int) DB::table('ticket_orders')->insertGetId([
'order_no' => 'ORD-'.$ticketNo,
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 10_000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 10_000,
'total_estimated_payout' => 0,
'status' => 'confirmed',
'submit_source' => 'h5',
'client_trace_id' => null,
'created_at' => now(),
'updated_at' => now(),
]);
return (int) DB::table('ticket_items')->insertGetId([
'ticket_no' => $ticketNo,
'order_id' => $orderId,
'player_id' => $player->id,
'draw_id' => $draw->id,
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 2,
'unit_bet_amount' => 10_000,
'total_bet_amount' => 10_000,
'rebate_rate_snapshot' => 0,
'commission_rate_snapshot' => 0,
'actual_deduct_amount' => 10_000,
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'settled_lose',
'win_amount' => 0,
'jackpot_win_amount' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
}
function grantReportScopeAgentOperator(AdminUser $admin, \App\Models\AgentNode $agent): void
{
$now = now();
$roleId = DB::table('admin_roles')->insertGetId([
'slug' => 'report_ops_'.$admin->id,
'code' => 'report_ops_'.$admin->id,
'name' => 'Report Ops',
'status' => 1,
'is_system' => false,
'sort_order' => 0,
'created_at' => $now,
'updated_at' => $now,
]);
$actionIds = DB::table('admin_menu_actions')
->whereIn('permission_code', ['agent.node.view'])
->pluck('id');
foreach ($actionIds as $actionId) {
DB::table('admin_role_menu_actions')->insert([
'role_id' => $roleId,
'menu_action_id' => (int) $actionId,
]);
}
DB::table('admin_user_site_roles')->insert([
'admin_user_id' => $admin->id,
'site_id' => (int) $agent->admin_site_id,
'role_id' => $roleId,
'granted_at' => $now,
]);
DB::table('admin_user_agents')->insert([
'admin_user_id' => $admin->id,
'agent_node_id' => (int) $agent->id,
'is_primary' => true,
'granted_at' => $now,
]);
DB::table('admin_user_agent_roles')->insert([
'admin_user_id' => $admin->id,
'agent_node_id' => (int) $agent->id,
'role_id' => $roleId,
'granted_at' => $now,
]);
}

View File

@@ -0,0 +1,399 @@
<?php
use App\Models\AdminUser;
use App\Models\AgentProfile;
use App\Models\Draw;
use App\Models\Player;
use App\Lottery\DrawStatus;
use App\Services\Agent\AgentNodeService;
use App\Services\AgentSettlement\AgentSettlementPeriodCloseService;
use App\Services\AgentSettlement\SettlementPaymentService;
use App\Support\Settlement\DesignDocExample12;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
function createSiteWithRoot(string $code): array
{
$siteId = (int) DB::table('admin_sites')->insertGetId([
'code' => $code,
'name' => $code,
'is_default' => false,
'created_at' => now(),
'updated_at' => now(),
]);
$rootId = (int) DB::table('agent_nodes')->insertGetId([
'admin_site_id' => $siteId,
'parent_id' => null,
'depth' => 0,
'path' => '/'.$code,
'code' => $code,
'name' => 'Root '.$code,
'status' => 1,
'created_at' => now(),
'updated_at' => now(),
]);
AgentProfile::query()->create([
'agent_node_id' => $rootId,
'total_share_rate' => 100,
'credit_limit' => 500_000,
'allocated_credit' => 0,
'used_credit' => 0,
'rebate_limit' => 0.01,
'default_player_rebate' => 0.005,
'settlement_cycle' => 'weekly',
]);
return ['site_id' => $siteId, 'site_code' => $code, 'root_id' => $rootId];
}
function createTicketItemForPlayer(Player $player, string $ticketNo): int
{
$draw = Draw::query()->create([
'draw_no' => 'DRAW-'.$ticketNo,
'business_date' => now()->toDateString(),
'sequence_no' => random_int(1, 9999),
'status' => DrawStatus::Open->value,
'current_result_version' => 0,
'settle_version' => 0,
'is_reopened' => false,
]);
$orderId = (int) DB::table('ticket_orders')->insertGetId([
'order_no' => 'ORD-'.$ticketNo,
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 10_000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 10_000,
'total_estimated_payout' => 0,
'status' => 'confirmed',
'submit_source' => 'h5',
'client_trace_id' => null,
'created_at' => now(),
'updated_at' => now(),
]);
return (int) DB::table('ticket_items')->insertGetId([
'ticket_no' => $ticketNo,
'order_id' => $orderId,
'player_id' => $player->id,
'draw_id' => $draw->id,
'original_number' => null,
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 2,
'digit_slot' => null,
'bet_mode' => null,
'unit_bet_amount' => 10_000,
'total_bet_amount' => 10_000,
'rebate_rate_snapshot' => 0,
'commission_rate_snapshot' => 0,
'actual_deduct_amount' => 10_000,
'odds_snapshot_json' => null,
'rule_snapshot_json' => null,
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'settled_lose',
'win_amount' => 0,
'jackpot_win_amount' => 0,
'settled_at' => null,
'created_at' => now(),
'updated_at' => now(),
]);
}
test('period close only tags share ledger rows for the closing site', function (): void {
$siteA = createSiteWithRoot('close-site-a');
$siteB = createSiteWithRoot('close-site-b');
$playerA = Player::query()->create([
'site_code' => $siteA['site_code'],
'agent_node_id' => $siteA['root_id'],
'site_player_id' => 'p-close-a',
'username' => 'close_a',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$playerB = Player::query()->create([
'site_code' => $siteB['site_code'],
'agent_node_id' => $siteB['root_id'],
'site_player_id' => 'p-close-b',
'username' => 'close_b',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$settledAt = now();
$snapshot = json_encode([
'total_shares' => ['close-site-a' => 100],
'chain_codes' => ['close-site-a'],
'agent_path' => [$siteA['root_id']],
]);
$snapshotB = json_encode([
'total_shares' => ['close-site-b' => 100],
'chain_codes' => ['close-site-b'],
'agent_path' => [$siteB['root_id']],
]);
$ticketAId = createTicketItemForPlayer($playerA, 'T-P0-A');
$ticketBId = createTicketItemForPlayer($playerB, 'T-P0-B');
$ledgerAId = (int) DB::table('share_ledger')->insertGetId([
'ticket_item_id' => $ticketAId,
'player_id' => $playerA->id,
'agent_node_id' => $siteA['root_id'],
'agent_path' => json_encode([$siteA['root_id']]),
'share_snapshot' => $snapshot,
'game_win_loss' => 1000,
'basic_rebate' => 0,
'shared_net_win_loss' => 1000,
'allocations_json' => json_encode([]),
'settled_at' => $settledAt,
'created_at' => $settledAt,
'updated_at' => $settledAt,
]);
$ledgerBId = (int) DB::table('share_ledger')->insertGetId([
'ticket_item_id' => $ticketBId,
'player_id' => $playerB->id,
'agent_node_id' => $siteB['root_id'],
'agent_path' => json_encode([$siteB['root_id']]),
'share_snapshot' => $snapshotB,
'game_win_loss' => 2000,
'basic_rebate' => 0,
'shared_net_win_loss' => 2000,
'allocations_json' => json_encode([]),
'settled_at' => $settledAt,
'created_at' => $settledAt,
'updated_at' => $settledAt,
]);
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteA['site_id'],
'period_start' => $settledAt->copy()->subDay(),
'period_end' => $settledAt->copy()->addDay(),
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
app(AgentSettlementPeriodCloseService::class)->closePeriod($periodId);
expect((int) DB::table('share_ledger')->where('id', $ledgerAId)->value('settlement_period_id'))
->toBe($periodId);
expect(DB::table('share_ledger')->where('id', $ledgerBId)->value('settlement_period_id'))
->toBeNull();
});
test('agent bill unpaid amount uses abs net so player-wins chain can be paid', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$super = AdminUser::query()->create([
'username' => 'p0_agent_pay',
'name' => 'P0',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$service = app(AgentNodeService::class);
$a = $service->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'P0A',
'name' => 'P0A',
'username' => 'p0_a',
'total_share_rate' => 60,
'credit_limit' => 500_000,
]));
$b = $service->createChild($super, agentChildPayload([
'parent_id' => $a->id,
'code' => 'P0B',
'name' => 'P0B',
'username' => 'p0_b',
'total_share_rate' => 40,
'credit_limit' => 200_000,
]));
$c = $service->createChild($super, agentChildPayload([
'parent_id' => $b->id,
'code' => 'P0C',
'name' => 'P0C',
'username' => 'p0_c',
'total_share_rate' => 25,
'credit_limit' => 100_000,
]));
$player = Player::query()->create([
'site_code' => $siteCode,
'agent_node_id' => $c->id,
'site_player_id' => 'p0-win-p1',
'username' => 'p0win',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$settledAt = now();
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => $settledAt->copy()->subDay(),
'period_end' => $settledAt->copy()->addDay(),
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$ticketItemId = createTicketItemForPlayer($player, 'T-P0-WIN');
// Player wins: negative game_win_loss produces negative agent edge settlements.
DB::table('share_ledger')->insert([
'ticket_item_id' => $ticketItemId,
'player_id' => $player->id,
'agent_node_id' => $c->id,
'agent_path' => json_encode([$a->id, $b->id, $c->id]),
'share_snapshot' => json_encode([
'total_shares' => ['P0C' => 25, 'P0B' => 40, 'P0A' => 60],
'actual_shares' => ['P0C' => 25, 'P0B' => 15, 'P0A' => 20, 'platform' => 40],
'chain_codes' => ['P0C', 'P0B', 'P0A'],
'agent_path' => [$a->id, $b->id, $c->id],
]),
'game_win_loss' => -DesignDocExample12::GAME_WIN_LOSS,
'basic_rebate' => DesignDocExample12::BASIC_REBATE,
'shared_net_win_loss' => -DesignDocExample12::SHARED_NET_WIN_LOSS,
'allocations_json' => json_encode([]),
'settled_at' => $settledAt,
'created_at' => $settledAt,
'updated_at' => $settledAt,
]);
app(AgentSettlementPeriodCloseService::class)->closePeriod($periodId);
$agentBill = DB::table('settlement_bills')
->where('settlement_period_id', $periodId)
->where('bill_type', 'agent')
->where('meta_json', 'like', '%P0C_to_P0B%')
->first();
expect($agentBill)->not->toBeNull();
expect((int) $agentBill->net_amount)->toBeLessThan(0);
expect((int) $agentBill->unpaid_amount)->toBe(abs((int) $agentBill->net_amount));
DB::table('settlement_bills')->where('id', $agentBill->id)->update([
'status' => 'confirmed',
'updated_at' => now(),
]);
app(SettlementPaymentService::class)->recordPayment(
(int) $agentBill->id,
(int) $agentBill->unpaid_amount,
(int) $super->id,
['method' => 'cash'],
);
$record = DB::table('payment_records')->where('settlement_bill_id', $agentBill->id)->first();
expect($record)->not->toBeNull();
expect((string) $record->payer_type)->toBe('agent');
expect((int) $record->payer_id)->toBe((int) $agentBill->counterparty_id);
expect((string) $record->payee_type)->toBe('agent');
expect((int) $record->payee_id)->toBe((int) $agentBill->owner_id);
$refreshed = DB::table('settlement_bills')->where('id', $agentBill->id)->first();
expect((int) $refreshed->unpaid_amount)->toBe(0);
expect((string) $refreshed->status)->toBe('settled');
});
test('period close fails when share ledger row is missing snapshot', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$player = Player::query()->create([
'site_code' => $siteCode,
'agent_node_id' => $rootId,
'site_player_id' => 'p0-missing-snap',
'username' => 'p0snap',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$settledAt = now();
$ticketItemId = createTicketItemForPlayer($player, 'T-P0-NOSNAP');
DB::table('share_ledger')->insert([
'ticket_item_id' => $ticketItemId,
'player_id' => $player->id,
'agent_node_id' => $rootId,
'agent_path' => json_encode([$rootId]),
'share_snapshot' => null,
'game_win_loss' => 500,
'basic_rebate' => 0,
'shared_net_win_loss' => 500,
'allocations_json' => json_encode([]),
'settled_at' => $settledAt,
'created_at' => $settledAt,
'updated_at' => $settledAt,
]);
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => $settledAt->copy()->subDay(),
'period_end' => $settledAt->copy()->addDay(),
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$caught = null;
try {
app(AgentSettlementPeriodCloseService::class)->closePeriod($periodId);
} catch (\Illuminate\Validation\ValidationException $e) {
$caught = $e;
}
expect($caught)->toBeInstanceOf(\Illuminate\Validation\ValidationException::class);
expect($caught?->errors()['period'][0] ?? null)->toBe('share_snapshot_missing');
expect((string) DB::table('settlement_periods')->where('id', $periodId)->value('status'))
->toBe('open');
expect(DB::table('settlement_bills')->where('settlement_period_id', $periodId)->count())
->toBe(0);
});
test('period close succeeds with no share ledger rows in window', function (): void {
['site_id' => $siteId] = createSiteWithRoot('empty-close');
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => '2026-06-01 00:00:00',
'period_end' => '2026-06-07 23:59:59',
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$result = app(AgentSettlementPeriodCloseService::class)->closePeriod($periodId);
expect($result['bill_ids'])->toBe([]);
expect($result['player_count'])->toBe(0);
expect((string) DB::table('settlement_periods')->where('id', $periodId)->value('status'))
->toBe('closed');
expect(DB::table('settlement_bills')->where('settlement_period_id', $periodId)->count())
->toBe(0);
});

View File

@@ -0,0 +1,197 @@
<?php
use App\Lottery\DrawStatus;
use App\Models\AdminUser;
use App\Models\Draw;
use App\Models\Player;
use App\Services\AgentSettlement\UnsettledTicketPeriodWarning;
use App\Support\PlayerFundingMode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
test('bound agent cannot open or close site settlement period', 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');
$service = app(\App\Services\Agent\AgentNodeService::class);
$super = AdminUser::query()->create([
'username' => 'period_scope_super',
'name' => 'Super',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$branch = $service->createChild($super, [
'parent_id' => $rootId,
'code' => 'period-scope-branch',
'name' => 'Period Scope Branch',
]);
$operator = AdminUser::query()->create([
'username' => 'period_scope_ops',
'name' => 'Ops',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantPeriodScopeAgentOperator($operator, $branch);
$token = $operator->createToken('test', ['*'], now()->addDay())->plainTextToken;
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/settlement-periods', [
'admin_site_id' => $siteId,
'period_start' => '2026-06-01 00:00:00',
'period_end' => '2026-06-30 23:59:59',
])
->assertForbidden();
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subWeek(),
'period_end' => now()->addWeek(),
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/settlement-periods/'.$periodId.'/close')
->assertForbidden();
});
test('unsettled ticket warning uses settled_at when game result is posted', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$player = Player::query()->create([
'site_code' => $siteCode,
'site_player_id' => 'native:unsettled-scope',
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'unsettled_scope',
'default_currency' => 'NPR',
'status' => 0,
'agent_node_id' => $rootId,
]);
$periodStart = now()->subDays(3)->toDateString();
$periodEnd = now()->addDay()->toDateString();
$settledAt = now()->subDay();
$draw = Draw::query()->create([
'draw_no' => 'DRAW-UNSETTLED-SCOPE',
'business_date' => now()->toDateString(),
'sequence_no' => random_int(1, 9999),
'status' => DrawStatus::Open->value,
'current_result_version' => 0,
'settle_version' => 0,
'is_reopened' => false,
]);
$orderId = (int) DB::table('ticket_orders')->insertGetId([
'order_no' => 'ORD-UNSETTLED-SCOPE',
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 1000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 1000,
'total_estimated_payout' => 0,
'status' => 'confirmed',
'submit_source' => 'h5',
'client_trace_id' => null,
'created_at' => now()->subMonth(),
'updated_at' => now(),
]);
DB::table('ticket_items')->insert([
'ticket_no' => 'T-UNSETTLED-SCOPE',
'order_id' => $orderId,
'player_id' => $player->id,
'draw_id' => $draw->id,
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 2,
'unit_bet_amount' => 1000,
'total_bet_amount' => 1000,
'rebate_rate_snapshot' => 0,
'commission_rate_snapshot' => 0,
'actual_deduct_amount' => 1000,
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'pending_payout',
'win_amount' => 5000,
'jackpot_win_amount' => 0,
'created_at' => now()->subMonth(),
'updated_at' => now(),
'settled_at' => $settledAt,
'agent_settled_at' => null,
]);
$warning = app(UnsettledTicketPeriodWarning::class);
$inPeriod = $warning->countForSite($siteId, $periodStart, $periodEnd);
$outPeriod = $warning->countForSite(
$siteId,
now()->subMonth()->toDateString(),
now()->subMonth()->addDay()->toDateString(),
);
expect($inPeriod['count'])->toBe(1)
->and($outPeriod['count'])->toBe(0);
});
function grantPeriodScopeAgentOperator(AdminUser $admin, \App\Models\AgentNode $agent): void
{
$now = now();
$roleId = DB::table('admin_roles')->insertGetId([
'slug' => 'period_scope_ops_'.$admin->id,
'code' => 'period_scope_ops_'.$admin->id,
'name' => 'Period Scope Ops',
'status' => 1,
'is_system' => false,
'sort_order' => 0,
'created_at' => $now,
'updated_at' => $now,
]);
$manageActionIds = DB::table('admin_menu_actions')
->whereIn('permission_code', ['prd.settlement.agent.manage', 'settlement.agent.manage'])
->pluck('id');
foreach ($manageActionIds as $actionId) {
DB::table('admin_role_menu_actions')->insert([
'role_id' => $roleId,
'menu_action_id' => (int) $actionId,
]);
}
DB::table('admin_user_site_roles')->insert([
'admin_user_id' => $admin->id,
'site_id' => (int) $agent->admin_site_id,
'role_id' => $roleId,
'granted_at' => $now,
]);
DB::table('admin_user_agents')->insert([
'admin_user_id' => $admin->id,
'agent_node_id' => (int) $agent->id,
'is_primary' => true,
'granted_at' => $now,
]);
DB::table('admin_user_agent_roles')->insert([
'admin_user_id' => $admin->id,
'agent_node_id' => (int) $agent->id,
'role_id' => $roleId,
'granted_at' => $now,
]);
}

View File

@@ -0,0 +1,74 @@
<?php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
test('cannot open duplicate settlement period for same range', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$super = \App\Models\AdminUser::query()->create([
'username' => 'period_dup_super',
'name' => 'Super',
'email' => null,
'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$token = $super->createToken('test', ['*'], now()->addDay())->plainTextToken;
$body = [
'admin_site_id' => $siteId,
'period_start' => '2026-06-01 00:00:00',
'period_end' => '2026-06-30 23:59:59',
];
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/settlement-periods', $body)
->assertCreated();
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/settlement-periods', $body)
->assertStatus(422)
->assertJsonPath(
'data.errors.period_start.0',
trans('validation.business.period_already_open'),
);
});
test('cannot open second settlement period while another is open on same site', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$super = \App\Models\AdminUser::query()->create([
'username' => 'period_one_open_super',
'name' => 'Super',
'email' => null,
'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$token = $super->createToken('test', ['*'], now()->addDay())->plainTextToken;
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/settlement-periods', [
'admin_site_id' => $siteId,
'period_start' => '2026-06-01 00:00:00',
'period_end' => '2026-06-07 23:59:59',
])
->assertCreated();
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/settlement-periods', [
'admin_site_id' => $siteId,
'period_start' => '2026-06-08 00:00:00',
'period_end' => '2026-06-14 23:59:59',
])
->assertStatus(422)
->assertJsonPath(
'data.errors.period_start.0',
trans('validation.business.period_site_has_open'),
);
});

View File

@@ -1,13 +1,20 @@
<?php
use App\Models\AdminUser;
use App\Models\Player;
use App\Services\AgentSettlement\AgentSettlementPeriodPipelineService;
use App\Services\AgentSettlement\AgentSettlementPeriodSummaryService;
use App\Support\PlayerFundingMode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
test('settlement periods index includes bill summary per period', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$periodId = (int) DB::table('settlement_periods')->insertGetId([
@@ -74,3 +81,285 @@ test('settlement periods index includes bill summary per period', function (): v
expect($summaries[$periodId]['player_bills'])->toBe(1);
expect($summaries[$periodId]['agent_bills'])->toBe(1);
});
test('pipeline counts respect agent subtree when admin is bound', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$service = app(\App\Services\Agent\AgentNodeService::class);
$super = AdminUser::query()->create([
'username' => 'pipe_scope_super',
'name' => 'Super',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$branch = $service->createChild($super, [
'parent_id' => $rootId,
'code' => 'pipe-branch',
'name' => 'Pipeline Branch',
]);
$otherBranch = $service->createChild($super, [
'parent_id' => $rootId,
'code' => 'pipe-other',
'name' => 'Pipeline Other',
]);
$periodStart = now()->subDay()->toDateString();
$periodEnd = now()->addDay()->toDateString();
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => $periodStart,
'period_end' => $periodEnd,
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$period = (object) [
'id' => $periodId,
'period_start' => $periodStart,
'period_end' => $periodEnd,
'admin_site_id' => $siteId,
];
foreach ([$branch, $otherBranch] as $node) {
$player = Player::query()->create([
'site_code' => $siteCode,
'site_player_id' => 'native:pipe-'.$node->code,
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'pipe_'.$node->code,
'default_currency' => 'NPR',
'status' => 0,
'agent_node_id' => $node->id,
]);
DB::table('credit_ledger')->insert([
'owner_type' => 'player',
'owner_id' => $player->id,
'amount' => -50,
'reason' => 'bet_hold',
'created_at' => now(),
'updated_at' => now(),
]);
}
$operator = AdminUser::query()->create([
'username' => 'pipe_scope_ops',
'name' => 'Ops',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantPipelineAgentOperator($operator, $branch);
$pipeline = app(AgentSettlementPeriodPipelineService::class);
$scoped = $pipeline->countsForPeriods(collect([$period]), $operator);
$all = $pipeline->countsForPeriods(collect([$period]), null);
expect($scoped[$period->id]['credit_ledger_count'])->toBe(1)
->and($all[$period->id]['credit_ledger_count'])->toBe(2);
});
test('pipeline game win loss total uses raw platform pnl or agent share profit for viewer', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$siteCode = (string) DB::table('admin_sites')->where('id', $siteId)->value('code');
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$service = app(\App\Services\Agent\AgentNodeService::class);
$super = AdminUser::query()->create([
'username' => 'pipe_profit_super',
'name' => 'Super',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$branch = $service->createChild($super, [
'parent_id' => $rootId,
'code' => 'pipe-profit-branch',
'name' => 'Profit Branch',
]);
$otherBranch = $service->createChild($super, [
'parent_id' => $rootId,
'code' => 'pipe-profit-other',
'name' => 'Profit Other',
]);
$periodStart = now()->subDay()->toDateString();
$periodEnd = now()->addDay()->toDateString();
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => $periodStart,
'period_end' => $periodEnd,
'status' => 'open',
'created_at' => now(),
'updated_at' => now(),
]);
$period = (object) [
'id' => $periodId,
'period_start' => $periodStart,
'period_end' => $periodEnd,
'admin_site_id' => $siteId,
];
$settledAt = now()->toDateTimeString();
foreach ([
[$branch, 300, 700, 1_000, 0],
[$otherBranch, 900, 100, -200, 0],
] as [$node, $agentProfit, $platformProfit, $gameWinLoss, $basicRebate]) {
$player = Player::query()->create([
'site_code' => $siteCode,
'site_player_id' => 'native:profit-'.$node->code,
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'profit_'.$node->code,
'default_currency' => 'NPR',
'status' => 0,
'agent_node_id' => $node->id,
]);
$ticketItemId = createPipelineProfitTicketItem($player, 'T-'.$node->code);
DB::table('share_ledger')->insert([
'ticket_item_id' => $ticketItemId,
'player_id' => $player->id,
'agent_node_id' => $node->id,
'agent_path' => json_encode([$node->id]),
'share_snapshot' => json_encode([
'total_shares' => [(string) $node->code => 30.0],
'chain_codes' => [(string) $node->code],
]),
'game_win_loss' => $gameWinLoss,
'basic_rebate' => $basicRebate,
'shared_net_win_loss' => $gameWinLoss - $basicRebate,
'allocations_json' => json_encode([
(string) $node->code => $agentProfit,
'platform' => $platformProfit,
]),
'settled_at' => $settledAt,
'created_at' => $settledAt,
'updated_at' => $settledAt,
]);
}
$operator = AdminUser::query()->create([
'username' => 'pipe_profit_ops',
'name' => 'Ops',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantPipelineAgentOperator($operator, $branch);
$pipeline = app(AgentSettlementPeriodPipelineService::class);
$platformView = $pipeline->countsForPeriods(collect([$period]), $super);
$agentView = $pipeline->countsForPeriods(collect([$period]), $operator);
expect($platformView[$period->id]['win_loss_scope'])->toBe('platform')
->and($platformView[$period->id]['game_win_loss_total'])->toBe(800)
->and($agentView[$period->id]['win_loss_scope'])->toBe('agent')
->and($agentView[$period->id]['game_win_loss_total'])->toBe(300);
});
function createPipelineProfitTicketItem(Player $player, string $ticketNo): int
{
$draw = \App\Models\Draw::query()->create([
'draw_no' => 'DRAW-'.$ticketNo,
'business_date' => now()->toDateString(),
'sequence_no' => random_int(1, 9999),
'status' => \App\Lottery\DrawStatus::Open->value,
'current_result_version' => 0,
'settle_version' => 0,
'is_reopened' => false,
]);
$orderId = (int) DB::table('ticket_orders')->insertGetId([
'order_no' => 'ORD-'.$ticketNo,
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 10_000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 10_000,
'total_estimated_payout' => 0,
'status' => 'confirmed',
'submit_source' => 'h5',
'client_trace_id' => null,
'created_at' => now(),
'updated_at' => now(),
]);
return (int) DB::table('ticket_items')->insertGetId([
'ticket_no' => $ticketNo,
'order_id' => $orderId,
'player_id' => $player->id,
'draw_id' => $draw->id,
'original_number' => null,
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 2,
'digit_slot' => null,
'bet_mode' => null,
'unit_bet_amount' => 10_000,
'total_bet_amount' => 10_000,
'rebate_rate_snapshot' => 0,
'commission_rate_snapshot' => 0,
'actual_deduct_amount' => 10_000,
'odds_snapshot_json' => null,
'rule_snapshot_json' => null,
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'settled_lose',
'win_amount' => 0,
'jackpot_win_amount' => 0,
]);
}
function grantPipelineAgentOperator(AdminUser $admin, \App\Models\AgentNode $agent): void
{
$now = now();
$roleId = DB::table('admin_roles')->insertGetId([
'slug' => 'pipe_ops_'.$admin->id,
'code' => 'pipe_ops_'.$admin->id,
'name' => 'Pipeline Ops',
'status' => 1,
'is_system' => false,
'sort_order' => 0,
'created_at' => $now,
'updated_at' => $now,
]);
$actionIds = DB::table('admin_menu_actions')
->whereIn('permission_code', ['agent.node.view'])
->pluck('id');
foreach ($actionIds as $actionId) {
DB::table('admin_role_menu_actions')->insert([
'role_id' => $roleId,
'menu_action_id' => (int) $actionId,
]);
}
DB::table('admin_user_site_roles')->insert([
'admin_user_id' => $admin->id,
'site_id' => (int) $agent->admin_site_id,
'role_id' => $roleId,
'granted_at' => $now,
]);
DB::table('admin_user_agents')->insert([
'admin_user_id' => $admin->id,
'agent_node_id' => (int) $agent->id,
'is_primary' => true,
'granted_at' => $now,
]);
DB::table('admin_user_agent_roles')->insert([
'admin_user_id' => $admin->id,
'agent_node_id' => (int) $agent->id,
'role_id' => $roleId,
'granted_at' => $now,
]);
}

View File

@@ -21,6 +21,32 @@ beforeEach(function (): void {
$this->seed(LotterySettingsSeeder::class);
});
test('native player can login without site code using default site', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$rootId = (int) DB::table('agent_nodes')->where('depth', 0)->value('id');
$player = Player::query()->create([
'site_code' => (string) $site->code,
'agent_node_id' => $rootId,
'site_player_id' => 'native:test-no-site',
'auth_source' => PlayerAuthSource::LOTTERY_NATIVE,
'funding_mode' => PlayerFundingMode::CREDIT,
'username' => 'agentplayer0',
'password_hash' => Hash::make('secret-pass'),
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$login = $this->postJson('/api/v1/player/auth/login', [
'username' => 'agentplayer0',
'password' => 'secret-pass',
]);
$login->assertOk()
->assertJsonPath('data.player.id', $player->id);
});
test('native player can login and access me', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$rootId = (int) DB::table('agent_nodes')->where('depth', 0)->value('id');

View File

@@ -0,0 +1,100 @@
<?php
use App\Models\AdminUser;
use App\Services\AgentSettlement\SettlementPaymentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
test('payment record uses counterparty as payer when player wins', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$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' => 'player',
'owner_type' => 'player',
'owner_id' => 1,
'counterparty_type' => 'agent',
'counterparty_id' => 1,
'net_amount' => -500,
'unpaid_amount' => 500,
'paid_amount' => 0,
'status' => 'confirmed',
'created_at' => now(),
'updated_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'pay_dir_super',
'name' => 'PayDir',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($admin);
app(SettlementPaymentService::class)->recordPayment($billId, 500, (int) $admin->id, [
'method' => 'cash',
]);
$record = DB::table('payment_records')->where('settlement_bill_id', $billId)->first();
expect($record)->not->toBeNull();
expect((string) $record->payer_type)->toBe('agent');
expect((int) $record->payer_id)->toBe(1);
expect((string) $record->payee_type)->toBe('player');
expect((int) $record->payee_id)->toBe(1);
});
test('payment record uses owner as payer when player loses', function (): void {
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
$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' => 'player',
'owner_type' => 'player',
'owner_id' => 1,
'counterparty_type' => 'agent',
'counterparty_id' => 1,
'net_amount' => 800,
'unpaid_amount' => 800,
'paid_amount' => 0,
'status' => 'confirmed',
'created_at' => now(),
'updated_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'pay_dir_super2',
'name' => 'PayDir2',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($admin);
app(SettlementPaymentService::class)->recordPayment($billId, 800, (int) $admin->id);
$record = DB::table('payment_records')->where('settlement_bill_id', $billId)->first();
expect((string) $record->payer_type)->toBe('player');
expect((int) $record->payer_id)->toBe(1);
expect((string) $record->payee_type)->toBe('agent');
expect((int) $record->payee_id)->toBe(1);
});

View File

@@ -0,0 +1,281 @@
<?php
use App\Models\AdminUser;
use App\Models\AgentNode;
use App\Models\Player;
use App\Services\Agent\AgentNodeService;
use App\Services\Player\PlayerCreditService;
use App\Support\AgentOverdueGuard;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});
test('severe overdue agent line (7+ days) freezes betting for all players in line', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$extra = json_decode((string) ($site->extra_json ?? '{}'), true);
if (! is_array($extra)) {
$extra = [];
}
$extra['credit_line_mode'] = true;
DB::table('admin_sites')->where('id', $site->id)->update([
'extra_json' => json_encode($extra),
'updated_at' => now(),
]);
$siteId = (int) $site->id;
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$super = AdminUser::query()->create([
'username' => 'severe_super',
'name' => 'Severe',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
// 创建三级代理链: root -> A -> B
$agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'SEV_A',
'name' => 'Agent A',
'username' => 'sev_agent_a',
'total_share_rate' => 60,
'credit_limit' => 100000,
'can_create_player' => true,
]));
$agentB = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $agentA->id,
'code' => 'SEV_B',
'name' => 'Agent B',
'username' => 'sev_agent_b',
'total_share_rate' => 40,
'credit_limit' => 50000,
'can_create_player' => true,
]));
// 创建玩家归属 B
$player = Player::query()->create([
'site_code' => (string) $site->code,
'agent_node_id' => $agentB->id,
'site_player_id' => 'sev-p1',
'auth_source' => 'lottery_native',
'funding_mode' => 'credit',
'username' => 'sev_player',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
DB::table('player_credit_accounts')->insert([
'player_id' => $player->id,
'credit_limit' => 10000,
'used_credit' => 0,
'frozen_credit' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
// 创建 A 的严重逾期账单8天前
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subWeeks(2),
'period_end' => now()->subWeeks(1),
'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' => $agentA->id,
'counterparty_type' => 'agent',
'counterparty_id' => $rootId,
'gross_win_loss' => 0,
'rebate_amount' => 0,
'adjustment_amount' => 0,
'net_amount' => 1000,
'paid_amount' => 0,
'unpaid_amount' => 1000,
'status' => 'overdue',
'updated_at' => now()->subDays(8), // 8天前逾期
'created_at' => now()->subDays(8),
]);
// 验证严重逾期检查
expect(AgentOverdueGuard::agentHasSevereOverdueBills($agentA->id, 7))->toBeTrue();
expect(AgentOverdueGuard::agentLineHasSevereOverdueBills($agentB->id, 7))->toBeTrue();
// 玩家下注应该被拒绝
expect(fn () => app(PlayerCreditService::class)->assertMayPlaceBet($player, 100))
->toThrow(\Illuminate\Validation\ValidationException::class);
});
test('normal overdue (less than 7 days) does not freeze line betting', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$extra = json_decode((string) ($site->extra_json ?? '{}'), true);
if (! is_array($extra)) {
$extra = [];
}
$extra['credit_line_mode'] = true;
DB::table('admin_sites')->where('id', $site->id)->update([
'extra_json' => json_encode($extra),
'updated_at' => now(),
]);
$siteId = (int) $site->id;
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$super = AdminUser::query()->create([
'username' => 'normal_super',
'name' => 'Normal',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'NORM_A',
'name' => 'Agent A',
'username' => 'norm_agent_a',
'total_share_rate' => 60,
'credit_limit' => 100000,
'can_create_player' => true,
]));
$agentB = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $agentA->id,
'code' => 'NORM_B',
'name' => 'Agent B',
'username' => 'norm_agent_b',
'total_share_rate' => 40,
'credit_limit' => 50000,
'can_create_player' => true,
]));
$player = Player::query()->create([
'site_code' => (string) $site->code,
'agent_node_id' => $agentB->id,
'site_player_id' => 'norm-p1',
'auth_source' => 'lottery_native',
'funding_mode' => 'credit',
'username' => 'norm_player',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
DB::table('player_credit_accounts')->insert([
'player_id' => $player->id,
'credit_limit' => 10000,
'used_credit' => 0,
'frozen_credit' => 0,
'created_at' => now(),
'updated_at' => now(),
]);
// 创建 A 的普通逾期账单3天前
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subWeeks(2),
'period_end' => now()->subWeeks(1),
'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' => $agentA->id,
'counterparty_type' => 'agent',
'counterparty_id' => $rootId,
'gross_win_loss' => 0,
'rebate_amount' => 0,
'adjustment_amount' => 0,
'net_amount' => 1000,
'paid_amount' => 0,
'unpaid_amount' => 1000,
'status' => 'overdue',
'updated_at' => now()->subDays(3), // 3天前逾期
'created_at' => now()->subDays(3),
]);
// 验证普通逾期检查
expect(AgentOverdueGuard::agentHasSevereOverdueBills($agentA->id, 7))->toBeFalse();
expect(AgentOverdueGuard::agentLineHasSevereOverdueBills($agentB->id, 7))->toBeFalse();
// 玩家下注应该成功(普通逾期不冻结整条线)
expect(fn () => app(PlayerCreditService::class)->assertMayPlaceBet($player, 100))
->not->toThrow(\Illuminate\Validation\ValidationException::class);
});
test('severe overdue check respects configurable days threshold', function (): void {
$site = DB::table('admin_sites')->where('is_default', true)->first();
$siteId = (int) $site->id;
$rootId = (int) DB::table('agent_nodes')->where('admin_site_id', $siteId)->where('depth', 0)->value('id');
$super = AdminUser::query()->create([
'username' => 'config_super',
'name' => 'Config',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($super);
$agentA = app(AgentNodeService::class)->createChild($super, agentChildPayload([
'parent_id' => $rootId,
'code' => 'CFG_A',
'name' => 'Agent A',
'username' => 'cfg_agent_a',
'total_share_rate' => 60,
'credit_limit' => 100000,
]));
// 创建 5 天前的逾期账单
$periodId = (int) DB::table('settlement_periods')->insertGetId([
'admin_site_id' => $siteId,
'period_start' => now()->subWeeks(2),
'period_end' => now()->subWeeks(1),
'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' => $agentA->id,
'counterparty_type' => 'agent',
'counterparty_id' => $rootId,
'gross_win_loss' => 0,
'rebate_amount' => 0,
'adjustment_amount' => 0,
'net_amount' => 1000,
'paid_amount' => 0,
'unpaid_amount' => 1000,
'status' => 'overdue',
'updated_at' => now()->subDays(5),
'created_at' => now()->subDays(5),
]);
// 7天阈值5天不算严重逾期
expect(AgentOverdueGuard::agentHasSevereOverdueBills($agentA->id, 7))->toBeFalse();
// 3天阈值5天算严重逾期
expect(AgentOverdueGuard::agentHasSevereOverdueBills($agentA->id, 3))->toBeTrue();
});