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); });