CommissionController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Controllers\agent;
  3. use App\Http\Controllers\AgentApiController;
  4. use App\Models\Agent\Agent;
  5. use App\Models\Agent\AgentCommission;
  6. use App\Models\Agent\AgentCommissionRule;
  7. use App\Services\Agent\AgentCommissionService;
  8. use App\Services\Agent\AgentReportService;
  9. use App\Services\Agent\AgentService;
  10. use Illuminate\Http\Request;
  11. use App\Rules\DecimalNumber;
  12. class CommissionController extends AgentApiController
  13. {
  14. public function records(Request $request, AgentReportService $reports)
  15. {
  16. return $this->execute(function () use ($request, $reports) {
  17. $data = $request->validate([
  18. 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
  19. 'type' => ['nullable', 'in:flow,profit'], 'status' => ['nullable', 'in:pending,credited'],
  20. 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
  21. 'include_children' => ['nullable', 'boolean'],
  22. ]);
  23. $page = max(1, (int) ($data['page'] ?? 1));
  24. $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
  25. $agent = $this->currentAgent();
  26. $ids = !empty($data['include_children']) ? $reports->visibleAgentIds($agent) : [(int) $agent->id];
  27. $query = AgentCommission::query()->with('agent:id,username')->whereIn('agent_id', $ids);
  28. if (!empty($data['type'])) $query->where('type', $data['type']);
  29. if (!empty($data['status'])) $query->where('status', $data['status']);
  30. if (!empty($data['start_date'])) $query->where('settlement_date', '>=', $data['start_date']);
  31. if (!empty($data['end_date'])) $query->where('settlement_date', '<=', $data['end_date']);
  32. $total = (clone $query)->count();
  33. $list = $query->orderByDesc('settlement_date')->orderByDesc('id')->forPage($page, $limit)->get();
  34. return compact('total', 'page', 'limit', 'list');
  35. });
  36. }
  37. public function rules(Request $request, AgentReportService $reports)
  38. {
  39. return $this->execute(function () use ($request, $reports) {
  40. $data = $request->validate(['include_children' => ['nullable', 'boolean']]);
  41. $agent = $this->currentAgent();
  42. $ids = !empty($data['include_children']) ? $reports->visibleAgentIds($agent) : [(int) $agent->id];
  43. return AgentCommissionRule::query()->with('agent:id,username')->whereIn('agent_id', $ids)->orderByDesc('effective_from')->get();
  44. });
  45. }
  46. public function saveRule(Request $request, AgentService $agents, AgentCommissionService $commissions)
  47. {
  48. return $this->execute(function () use ($request, $agents, $commissions) {
  49. $root = $this->currentAgent();
  50. $data = $request->validate([
  51. 'agent_id' => ['required', 'integer', 'exists:agents,id'],
  52. 'flow_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  53. 'profit_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
  54. 'effective_from' => ['required', 'date_format:Y-m-d', 'date_equals:' . now()->toDateString()],
  55. ]);
  56. if ((int) $data['agent_id'] === (int) $root->id) throw new \RuntimeException('代理不能修改自己的佣金比例');
  57. $agents->assertVisible($root, (int) $data['agent_id']);
  58. return $commissions->saveRule(Agent::query()->findOrFail($data['agent_id']), (string) $data['flow_rate'], (string) $data['profit_rate'], $data['effective_from'], null);
  59. });
  60. }
  61. }