| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace App\Http\Controllers\agent;
- use App\Http\Controllers\AgentApiController;
- use App\Models\Agent\Agent;
- use App\Models\Agent\AgentCommission;
- use App\Models\Agent\AgentCommissionRule;
- use App\Services\Agent\AgentCommissionService;
- use App\Services\Agent\AgentReportService;
- use App\Services\Agent\AgentService;
- use Illuminate\Http\Request;
- use App\Rules\DecimalNumber;
- class CommissionController extends AgentApiController
- {
- public function records(Request $request, AgentReportService $reports)
- {
- return $this->execute(function () use ($request, $reports) {
- $data = $request->validate([
- 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1', 'max:100'],
- 'type' => ['nullable', 'in:flow,profit'], 'status' => ['nullable', 'in:pending,credited'],
- 'start_date' => ['nullable', 'date_format:Y-m-d'], 'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
- 'include_children' => ['nullable', 'boolean'],
- ]);
- $page = max(1, (int) ($data['page'] ?? 1));
- $limit = min(100, max(1, (int) ($data['limit'] ?? 20)));
- $agent = $this->currentAgent();
- $ids = !empty($data['include_children']) ? $reports->visibleAgentIds($agent) : [(int) $agent->id];
- $query = AgentCommission::query()->with('agent:id,username')->whereIn('agent_id', $ids);
- if (!empty($data['type'])) $query->where('type', $data['type']);
- if (!empty($data['status'])) $query->where('status', $data['status']);
- if (!empty($data['start_date'])) $query->where('settlement_date', '>=', $data['start_date']);
- if (!empty($data['end_date'])) $query->where('settlement_date', '<=', $data['end_date']);
- $total = (clone $query)->count();
- $list = $query->orderByDesc('settlement_date')->orderByDesc('id')->forPage($page, $limit)->get();
- return compact('total', 'page', 'limit', 'list');
- });
- }
- public function rules(Request $request, AgentReportService $reports)
- {
- return $this->execute(function () use ($request, $reports) {
- $data = $request->validate(['include_children' => ['nullable', 'boolean']]);
- $agent = $this->currentAgent();
- $ids = !empty($data['include_children']) ? $reports->visibleAgentIds($agent) : [(int) $agent->id];
- return AgentCommissionRule::query()->with('agent:id,username')->whereIn('agent_id', $ids)->orderByDesc('effective_from')->get();
- });
- }
- public function saveRule(Request $request, AgentService $agents, AgentCommissionService $commissions)
- {
- return $this->execute(function () use ($request, $agents, $commissions) {
- $root = $this->currentAgent();
- $data = $request->validate([
- 'agent_id' => ['required', 'integer', 'exists:agents,id'],
- 'flow_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'profit_rate' => ['required', 'numeric', 'between:0,100', new DecimalNumber(3, 4)],
- 'effective_from' => ['required', 'date_format:Y-m-d', 'date_equals:' . now()->toDateString()],
- ]);
- if ((int) $data['agent_id'] === (int) $root->id) throw new \RuntimeException('代理不能修改自己的佣金比例');
- $agents->assertVisible($root, (int) $data['agent_id']);
- return $commissions->saveRule(Agent::query()->findOrFail($data['agent_id']), (string) $data['flow_rate'], (string) $data['profit_rate'], $data['effective_from'], null);
- });
- }
- }
|