AgentDashboardTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\agent\ProfileController;
  4. use App\Models\Agent\Agent;
  5. use App\Models\Agent\AgentSetting;
  6. use App\Services\Agent\AgentPeriodReportService;
  7. use App\Services\Agent\AgentReportService;
  8. use App\Services\PaymentOrderService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Database\Schema\Blueprint;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Schema;
  13. use PHPUnit\Framework\TestCase;
  14. use Tests\Support\AgentTestEnvironment;
  15. /** 代理首页 GET /agent/dashboard:只统计本代理名下会员,按日期汇总资金和投注。 */
  16. class AgentDashboardTest extends TestCase
  17. {
  18. use AgentTestEnvironment;
  19. public function test_dashboard_sums_only_own_members_within_the_date_range(): void
  20. {
  21. $agent = $this->agent();
  22. $other = $this->agent();
  23. foreach (['m1' => $agent, 'm2' => $agent, 'x1' => $other] as $memberId => $owner) {
  24. DB::table('users')->insert(['member_id' => $memberId, 'username' => $memberId, 'agent_id' => $owner->id, 'created_at' => now()]);
  25. DB::table('wallets')->insert(['member_id' => $memberId, 'available_balance' => 100]);
  26. }
  27. $in = '2026-09-07 10:00:00';
  28. $out = '2026-09-01 10:00:00';
  29. foreach ([['m1', 500, $in], ['m2', 300, $in], ['m1', 999, $out], ['x1', 777, $in]] as [$member, $amount, $at]) {
  30. DB::table('recharges')->insert(['member_id' => $member, 'status' => 1, 'amount' => $amount, 'created_at' => $at]);
  31. }
  32. DB::table('recharges')->insert(['member_id' => 'm1', 'status' => 0, 'amount' => 50, 'created_at' => $in]); // 未到账
  33. DB::table('payment_orders')->insert(['member_id' => 'm2', 'status' => 2, 'type' => '1', 'amount' => 200, 'created_at' => $in]);
  34. DB::table('withdraws')->insert(['member_id' => 'm1', 'status' => 1, 'amount' => 150, 'created_at' => $in]);
  35. foreach ([['人工充值', null, 80], ['人工扣款', null, -30], ['优惠活动', null, 20], ['返水', null, 10], ['人工充值', 0, 5]] as [$type, $related, $amount]) {
  36. DB::table('balance_logs')->insert(['member_id' => 'm1', 'change_type' => $type, 'related_id' => $related, 'amount' => $amount, 'created_at' => $in]);
  37. }
  38. $request = Request::create('/agent/dashboard', 'GET', ['start_date' => '2026-09-07', 'end_date' => '2026-09-07']);
  39. $request->attributes->set('agent', $agent);
  40. $this->app->instance('request', $request);
  41. $data = (new ProfileController())->dashboard($request, new AgentReportService())->getData(true);
  42. $this->assertSame(0, $data['code'], (string) $data['msg']);
  43. $summary = $data['data'];
  44. $this->assertSame(2, $summary['member_count']);
  45. $this->assertSame('200.0000', $summary['member_balance']);
  46. $this->assertSame('1000.0000', $summary['deposit_amount'], 'recharges + paid online orders in range');
  47. $this->assertSame('150.0000', $summary['withdraw_amount']);
  48. $this->assertSame('80.0000', $summary['manual_credit']);
  49. $this->assertSame('30.0000', $summary['manual_debit']);
  50. $this->assertSame('25.0000', $summary['bonus_amount'], '优惠活动 + 人工充值(赠送)');
  51. $this->assertSame('10.0000', $summary['rebate_amount']);
  52. $this->assertSame('815.0000', $summary['company_profit'], '1000 - 150 - 25 - 10');
  53. $this->assertSame('0.0000', $summary['commission_amount']);
  54. $this->assertSame(0, $summary['agent_count']);
  55. $this->assertSame('own', $summary['scope']);
  56. foreach (['bet_count', 'bet_amount', 'valid_bet_amount', 'win_loss'] as $field) $this->assertArrayHasKey($field, $summary);
  57. $request = Request::create('/agent/dashboard', 'GET', ['start_date' => '2026-09-08', 'end_date' => '2026-09-07']);
  58. $request->attributes->set('agent', $agent);
  59. $this->assertNotSame(0, (new ProfileController())->dashboard($request, new AgentReportService())->getData(true)['code']);
  60. }
  61. public function test_equity_agent_sees_whole_platform_excluding_demo_accounts(): void
  62. {
  63. if (!Schema::hasColumn('users', 'from')) Schema::table('users', fn (Blueprint $t) => $t->integer('from')->nullable());
  64. $joint = $this->agent();
  65. $equity = $this->agent(Agent::TYPE_EQUITY);
  66. foreach (['a1' => [$joint->id, 1], 'a2' => [null, 1], 'a3' => [null, null], 'demo' => [null, 2]] as $memberId => [$agentId, $from]) {
  67. DB::table('users')->insert(['member_id' => $memberId, 'username' => $memberId, 'agent_id' => $agentId, 'from' => $from, 'created_at' => now()]);
  68. DB::table('recharges')->insert(['member_id' => $memberId, 'status' => 1, 'amount' => 100, 'created_at' => '2026-09-07 10:00:00']);
  69. }
  70. $summary = (new AgentReportService())->dashboard($equity, '2026-09-07', '2026-09-07');
  71. $this->assertSame('platform', $summary['scope']);
  72. $this->assertSame(3, $summary['member_count'], 'all real members, no demo account, regardless of agent');
  73. $this->assertSame('300.0000', $summary['deposit_amount']);
  74. $this->assertSame('0.0000', $summary['commission_amount']);
  75. $own = (new AgentReportService())->dashboard($joint, '2026-09-07', '2026-09-07');
  76. $this->assertSame(['own', 1, '100.0000'], [$own['scope'], $own['member_count'], $own['deposit_amount']]);
  77. $rows = array_column((new AgentReportService())->reportRows(null, ['start_date' => '2026-09-07'])['list'], null, 'id');
  78. $this->assertSame(0, $rows[$equity->id]['member_count'], 'admin agent report still counts only the equity agent\'s own members');
  79. }
  80. public function test_yearly_dashboard_preserves_third_game_date_boundaries_and_finance_totals(): void
  81. {
  82. Schema::table('users', function (Blueprint $table) {
  83. $table->integer('from')->default(1);
  84. $table->integer('user_type')->default(0);
  85. });
  86. Schema::create('third_game_orders', function (Blueprint $table) {
  87. $table->id();
  88. $table->unsignedBigInteger('user_id')->nullable();
  89. $table->integer('status');
  90. $table->dateTime('last_update_time');
  91. foreach (['bet_amount', 'valid_amount', 'settled_amount'] as $field) {
  92. $table->decimal($field, 18, 4)->nullable();
  93. }
  94. });
  95. $joint = $this->agent();
  96. $equity = $this->agent(Agent::TYPE_EQUITY);
  97. $test = $this->agents->create(['username' => 'dashboard-test', 'password' => 'secret1', 'is_test' => 1]);
  98. $users = [];
  99. foreach ([['own', $joint->id, 1, 0], ['direct', null, 1, 0], ['guest', null, 2, 1], ['simulation', null, 1, 2], ['test', $test->id, 1, 0]] as [$member, $agentId, $from, $userType]) {
  100. $users[$member] = DB::table('users')->insertGetId([
  101. 'member_id' => $member, 'username' => $member, 'agent_id' => $agentId, 'from' => $from, 'user_type' => $userType,
  102. ]);
  103. }
  104. $from = '2025-09-08';
  105. $to = '2026-09-07';
  106. foreach ([
  107. [$users['own'], 1, "$from 00:00:00", 10, 8, -2],
  108. [$users['own'], 1, "$to 23:59:59", 20, null, 5],
  109. [$users['direct'], 1, "$to 12:00:00", 40, 35, 7],
  110. [$users['own'], 0, "$to 12:00:00", 999, 999, 999],
  111. [$users['own'], 1, '2025-09-07 23:59:59', 999, 999, 999],
  112. [$users['own'], 1, '2026-09-08 00:00:00', 999, 999, 999],
  113. [$users['guest'], 1, "$to 12:00:00", 999, 999, 999],
  114. [$users['simulation'], 1, "$to 12:00:00", 999, 999, 999],
  115. [$users['test'], 1, "$to 12:00:00", 999, 999, 999],
  116. [null, 1, "$to 12:00:00", 999, 999, 999],
  117. ] as [$userId, $status, $at, $bet, $valid, $win]) {
  118. DB::table('third_game_orders')->insert(['user_id' => $userId, 'status' => $status, 'last_update_time' => $at,
  119. 'bet_amount' => $bet, 'valid_amount' => $valid, 'settled_amount' => $win]);
  120. }
  121. foreach ([['人工充值', null, 40], ['订单补偿', null, 2], ['人工扣款', null, -3], ['注册赠送', null, 1], ['优惠活动', null, 2],
  122. ['即充即送', null, 3], ['充值返现', null, 4], ['老用户回归', null, 5], ['人工充值', 0, 6], ['人工充值', 123, 999],
  123. ['比比返', null, 1], ['笔笔返', null, 2], ['返水', null, 3], ['回水', null, 4], ['结算', null, 999],
  124. ] as [$type, $relatedId, $amount]) {
  125. DB::table('balance_logs')->insert(['member_id' => 'own', 'change_type' => $type, 'related_id' => $relatedId,
  126. 'amount' => $amount, 'created_at' => "$from 00:00:00"]);
  127. }
  128. $reports = new AgentReportService();
  129. $betFields = array_flip(['bet_count', 'bet_amount', 'valid_bet_amount', 'win_loss']);
  130. $fundFields = array_flip(['manual_credit', 'manual_debit', 'bonus_amount', 'rebate_amount']);
  131. $platform = $reports->dashboard($equity, $from, $to);
  132. $own = $reports->dashboard($joint, $from, $to);
  133. $this->assertSame([3, '70.0000', '43.0000', '10.0000'], array_values(array_intersect_key($platform, $betFields)));
  134. $this->assertSame([2, '30.0000', '8.0000', '3.0000'], array_values(array_intersect_key($own, $betFields)));
  135. $this->assertSame(['42.0000', '3.0000', '21.0000', '10.0000'], array_values(array_intersect_key($platform, $fundFields)));
  136. $this->assertSame(array_intersect_key($platform, $fundFields), array_intersect_key($own, $fundFields));
  137. }
  138. public function test_charts_group_registrations_and_deduplicate_successful_daily_deposits_across_channels(): void
  139. {
  140. $agent = $this->agent();
  141. $other = $this->agent();
  142. foreach ([['a', $agent, '2026-08-31'], ['b', $agent, '2026-09-02'], ['c', $other, '2026-09-02']] as [$id, $owner, $day]) {
  143. DB::table('users')->insert(['member_id' => $id, 'username' => $id, 'agent_id' => $owner->id, 'created_at' => $day . ' 00:00:00']);
  144. }
  145. DB::table('recharges')->insert(['member_id' => 'a', 'status' => 1, 'amount' => 100,
  146. 'exchange_rate' => '7.1250', 'created_at' => '2026-09-02 10:00:00']);
  147. DB::table('recharges')->insert(['member_id' => 'a', 'status' => 1, 'amount' => 20, 'created_at' => '2026-09-03 00:00:00']);
  148. foreach ([
  149. ['a', PaymentOrderService::TYPE_PAY, PaymentOrderService::STATUS_SUCCESS, 30],
  150. ['b', PaymentOrderService::TYPE_SELF_PAY, PaymentOrderService::STATUS_SUCCESS, 40],
  151. ['c', PaymentOrderService::TYPE_PAY, PaymentOrderService::STATUS_SUCCESS, 1000],
  152. ['a', PaymentOrderService::TYPE_PAYOUT, PaymentOrderService::STATUS_SUCCESS, 1000],
  153. ['a', PaymentOrderService::TYPE_PAY, PaymentOrderService::STATUS_STAY, 1000],
  154. ] as [$member, $type, $status, $amount]) {
  155. DB::table('payment_orders')->insert(['member_id' => $member, 'type' => $type, 'status' => $status,
  156. 'amount' => $amount, 'created_at' => '2026-09-02 23:59:59']);
  157. }
  158. $range = ['start_date' => '2026-08-31', 'end_date' => '2026-09-07'];
  159. $days = $this->charts($agent, $range);
  160. $this->assertSame('own', $days['scope']);
  161. $this->assertNull($days['equity']);
  162. $this->assertSame([1, 0, 1, 0, 0, 0, 0, 0], array_column($days['registrations'], 'new_members'));
  163. $this->assertSame([0, 0, 2, 1, 0, 0, 0, 0], array_column($days['daily_deposits'], 'deposit_members'));
  164. $this->assertSame(['0.0000', '0.0000', '782.5000', '20.0000', '0.0000', '0.0000', '0.0000', '0.0000'], array_column($days['daily_deposits'], 'deposit_amount'));
  165. $weeks = $this->charts($agent, $range + ['period' => 'week']);
  166. $this->assertSame(['2026-W36', '2026-W37'], array_column($weeks['registrations'], 'period_key'));
  167. $this->assertSame([2, 0], array_column($weeks['registrations'], 'new_members'));
  168. $this->assertSame($days['daily_deposits'], $weeks['daily_deposits']);
  169. $months = $this->charts($agent, $range + ['period' => 'month']);
  170. $this->assertSame(['2026-08', '2026-09'], array_column($months['registrations'], 'period_key'));
  171. $this->assertSame([1, 1], array_column($months['registrations'], 'new_members'));
  172. }
  173. public function test_shareholder_chart_shows_current_formal_holdings_and_real_platform_members(): void
  174. {
  175. Schema::table('users', function (Blueprint $table) {
  176. $table->integer('from')->default(1);
  177. $table->integer('user_type')->default(0);
  178. });
  179. $holder = $this->agent(Agent::TYPE_EQUITY);
  180. $other = $this->agent(Agent::TYPE_EQUITY);
  181. $test = $this->agents->create(['username' => 'test-holder', 'password' => 'secret1', 'type' => Agent::TYPE_EQUITY, 'is_test' => 1]);
  182. foreach ([[$holder, '12.3456'], [$other, '7.6544'], [$test, '9.5000']] as [$shareholder, $percent]) {
  183. Agent::query()->whereKey($shareholder->id)->update(['equity_percent' => $percent]);
  184. }
  185. AgentSetting::current()->update(['company_equity_percent' => '60.5000']);
  186. foreach ([['real', null, 1, 0], ['test', $test->id, 1, 0], ['guest', null, 2, 1], ['simulated', null, 1, 2]] as [$id, $agentId, $from, $type]) {
  187. DB::table('users')->insert(['member_id' => $id, 'username' => $id, 'agent_id' => $agentId,
  188. 'from' => $from, 'user_type' => $type, 'created_at' => now()]);
  189. DB::table('recharges')->insert(['member_id' => $id, 'status' => 1, 'amount' => 100, 'created_at' => now()]);
  190. }
  191. $charts = $this->charts($holder, ['start_date' => '2026-09-07']);
  192. $this->assertSame('platform', $charts['scope']);
  193. $this->assertSame([1], array_column($charts['registrations'], 'new_members'));
  194. $this->assertSame([1], array_column($charts['daily_deposits'], 'deposit_members'));
  195. $this->assertSame(['100.0000'], array_column($charts['daily_deposits'], 'deposit_amount'));
  196. $this->assertSame([
  197. 'own_percent' => '12.3456', 'other_percent' => '7.6544', 'company_percent' => '60.5000', 'remaining_percent' => '19.5000',
  198. ], $charts['equity']);
  199. }
  200. private function charts(Agent $agent, array $params): array
  201. {
  202. $request = Request::create('/agent/dashboard/charts', 'GET', $params);
  203. $request->attributes->set('agent', $agent);
  204. $this->app->instance('request', $request);
  205. $response = (new ProfileController())->dashboardCharts($request, new AgentPeriodReportService(), new AgentReportService())->getData(true);
  206. $this->assertSame(0, $response['code'], (string) $response['msg']);
  207. return $response['data'];
  208. }
  209. }