user(); Gate::authorize('viewAny', Call::class); $query = Call::with('lead:id,first_name,last_name,phone', 'user:id,name', 'contact:id,name,role', 'contactPhone:id,phone,type,status'); AccessControl::scopeCalls($query, $user); if ($request->lead_id) { $query->where('lead_id', $request->lead_id); } if ($request->result) { $query->where('result', $request->result); } if ($request->date_from) { $query->whereDate('created_at', '>=', $request->date_from); } if ($request->date_to) { $query->whereDate('created_at', '<=', $request->date_to); } return response()->json($query->orderBy('created_at', 'desc')->paginate($request->per_page ?? 15)); } public function results(): JsonResponse { return response()->json( CallResult::where('is_active', true) ->orderBy('sort_order') ->get(['id', 'name', 'slug', 'color', 'requires_follow_up', 'is_positive', 'is_negative', 'is_final']) ); } public function store(Request $request): JsonResponse { $validated = $request->validate([ 'lead_id' => 'required|exists:leads,id', 'contact_phone_id' => 'nullable|exists:contact_phones,id', ]); $lead = Lead::findOrFail($validated['lead_id']); Gate::authorize('create', [Call::class, $lead]); $result = $this->callService->initiateCall($validated['lead_id'], auth()->id(), $validated['contact_phone_id'] ?? null); return response()->json($result, 201); } public function show(Call $call): JsonResponse { Gate::authorize('view', $call); if ($call->recording_url && Gate::allows('viewRecording', $call)) { ActivityLogger::log('recording_viewed', "Recording viewed for call {$call->id}", $call); } return response()->json($call->load('lead', 'user', 'contact', 'contactPhone')); } public function registerResult(Request $request): JsonResponse { $validated = $request->validate([ 'call_id' => 'required|exists:calls,id', 'result' => 'required|string|max:50', 'notes' => 'nullable|string', 'next_follow_up_at' => 'nullable|date', 'referral' => 'nullable|array', 'referral.name' => 'required_with:referral|string|max:255', 'referral.phone' => 'required_with:referral|string|max:30', 'referral.phone_type' => 'nullable|string|in:mobile,landline,extension', 'referral.role' => 'nullable|string|max:80', 'referral.relation_description' => 'nullable|string|max:255', 'referral.description' => 'nullable|string', 'referral.make_primary' => 'nullable|boolean', 'referral.next_follow_up_at' => 'nullable|date', ]); $call = Call::with('lead')->findOrFail($validated['call_id']); Gate::authorize('registerResult', $call); $callResult = CallResult::where('name', $validated['result'])->first(); if ($callResult?->requires_follow_up && empty($validated['next_follow_up_at']) && empty($validated['referral']['next_follow_up_at'])) { return response()->json(['message' => 'برای این نتیجه تماس، زمان پیگیری الزامی است'], 422); } $call = $this->callService->registerResult( $validated['call_id'], $validated['result'], $validated['notes'] ?? null, $validated['next_follow_up_at'] ?? null, $validated['referral'] ?? null ); return response()->json($call); } }