AgentDashboardTest.php 5.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\agent\ProfileController;
  4. use App\Models\Agent\Agent;
  5. use App\Services\Agent\AgentReportService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Database\Schema\Blueprint;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Schema;
  10. use PHPUnit\Framework\TestCase;
  11. use Tests\Support\AgentTestEnvironment;
  12. /** 代理首页 GET /agent/dashboard:只统计本代理名下会员,按日期汇总资金和投注。 */
  13. class AgentDashboardTest extends TestCase
  14. {
  15. use AgentTestEnvironment;
  16. public function test_dashboard_sums_only_own_members_within_the_date_range(): void
  17. {
  18. $agent = $this->agent();
  19. $other = $this->agent();
  20. foreach (['m1' => $agent, 'm2' => $agent, 'x1' => $other] as $memberId => $owner) {
  21. DB::table('users')->insert(['member_id' => $memberId, 'username' => $memberId, 'agent_id' => $owner->id, 'created_at' => now()]);
  22. DB::table('wallets')->insert(['member_id' => $memberId, 'available_balance' => 100]);
  23. }
  24. $in = '2026-09-07 10:00:00';
  25. $out = '2026-09-01 10:00:00';
  26. foreach ([['m1', 500, $in], ['m2', 300, $in], ['m1', 999, $out], ['x1', 777, $in]] as [$member, $amount, $at]) {
  27. DB::table('recharges')->insert(['member_id' => $member, 'status' => 1, 'amount' => $amount, 'created_at' => $at]);
  28. }
  29. DB::table('recharges')->insert(['member_id' => 'm1', 'status' => 0, 'amount' => 50, 'created_at' => $in]); // 未到账
  30. DB::table('payment_orders')->insert(['member_id' => 'm2', 'status' => 2, 'type' => '1', 'amount' => 200, 'created_at' => $in]);
  31. DB::table('withdraws')->insert(['member_id' => 'm1', 'status' => 1, 'amount' => 150, 'created_at' => $in]);
  32. foreach ([['人工充值', null, 80], ['人工扣款', null, -30], ['优惠活动', null, 20], ['返水', null, 10], ['人工充值', 0, 5]] as [$type, $related, $amount]) {
  33. DB::table('balance_logs')->insert(['member_id' => 'm1', 'change_type' => $type, 'related_id' => $related, 'amount' => $amount, 'created_at' => $in]);
  34. }
  35. $request = Request::create('/agent/dashboard', 'GET', ['start_date' => '2026-09-07', 'end_date' => '2026-09-07']);
  36. $request->attributes->set('agent', $agent);
  37. $this->app->instance('request', $request);
  38. $data = (new ProfileController())->dashboard($request, new AgentReportService())->getData(true);
  39. $this->assertSame(0, $data['code'], (string) $data['msg']);
  40. $summary = $data['data'];
  41. $this->assertSame(2, $summary['member_count']);
  42. $this->assertSame('200.0000', $summary['member_balance']);
  43. $this->assertSame('1000.0000', $summary['deposit_amount'], 'recharges + paid online orders in range');
  44. $this->assertSame('150.0000', $summary['withdraw_amount']);
  45. $this->assertSame('80.0000', $summary['manual_credit']);
  46. $this->assertSame('30.0000', $summary['manual_debit']);
  47. $this->assertSame('25.0000', $summary['bonus_amount'], '优惠活动 + 人工充值(赠送)');
  48. $this->assertSame('10.0000', $summary['rebate_amount']);
  49. $this->assertSame('815.0000', $summary['company_profit'], '1000 - 150 - 25 - 10');
  50. $this->assertSame('0.0000', $summary['commission_amount']);
  51. $this->assertSame(0, $summary['agent_count']);
  52. $this->assertSame('own', $summary['scope']);
  53. foreach (['bet_count', 'bet_amount', 'valid_bet_amount', 'win_loss'] as $field) $this->assertArrayHasKey($field, $summary);
  54. $request = Request::create('/agent/dashboard', 'GET', ['start_date' => '2026-09-08', 'end_date' => '2026-09-07']);
  55. $request->attributes->set('agent', $agent);
  56. $this->assertNotSame(0, (new ProfileController())->dashboard($request, new AgentReportService())->getData(true)['code']);
  57. }
  58. public function test_equity_agent_sees_whole_platform_excluding_demo_accounts(): void
  59. {
  60. if (!Schema::hasColumn('users', 'from')) Schema::table('users', fn (Blueprint $t) => $t->integer('from')->nullable());
  61. $joint = $this->agent();
  62. $equity = $this->agent(Agent::TYPE_EQUITY);
  63. foreach (['a1' => [$joint->id, 1], 'a2' => [null, 1], 'a3' => [null, null], 'demo' => [null, 2]] as $memberId => [$agentId, $from]) {
  64. DB::table('users')->insert(['member_id' => $memberId, 'username' => $memberId, 'agent_id' => $agentId, 'from' => $from, 'created_at' => now()]);
  65. DB::table('recharges')->insert(['member_id' => $memberId, 'status' => 1, 'amount' => 100, 'created_at' => '2026-09-07 10:00:00']);
  66. }
  67. $summary = (new AgentReportService())->dashboard($equity, '2026-09-07', '2026-09-07');
  68. $this->assertSame('platform', $summary['scope']);
  69. $this->assertSame(3, $summary['member_count'], 'all real members, no demo account, regardless of agent');
  70. $this->assertSame('300.0000', $summary['deposit_amount']);
  71. $this->assertSame('0.0000', $summary['commission_amount']);
  72. $own = (new AgentReportService())->dashboard($joint, '2026-09-07', '2026-09-07');
  73. $this->assertSame(['own', 1, '100.0000'], [$own['scope'], $own['member_count'], $own['deposit_amount']]);
  74. $rows = array_column((new AgentReportService())->reportRows(null, ['start_date' => '2026-09-07'])['list'], null, 'id');
  75. $this->assertSame(0, $rows[$equity->id]['member_count'], 'admin agent report still counts only the equity agent\'s own members');
  76. }
  77. }