resolveBusiness($request); if (! $business) { return $this->jsonError('کسب‌وکار یافت نشد.', 404); } $query = ServiceProvider::where('business_id', $business->id) ->with('linkedUser:id,name') ->orderBy('name'); if ($type = $request->get('provider_type')) { $query->where('provider_type', $type); } if ($cap = $request->get('capability')) { $query->where('capability', $cap); } if ($request->boolean('active_only')) { $query->where('is_active', true); } if ($search = $request->get('q')) { $query->where('name', 'like', "%{$search}%"); } $paginator = $query->paginate((int) $request->get('per_page', 15)); return $this->jsonSuccess([ 'items' => $paginator->items(), 'pagination' => [ 'total' => $paginator->total(), 'per_page' => $paginator->perPage(), 'current_page' => $paginator->currentPage(), 'last_page' => $paginator->lastPage(), ], ]); } public function store(Request $request) { $business = $this->resolveBusiness($request); if (! $business) { return $this->jsonError('کسب‌وکار یافت نشد.', 404); } $data = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'provider_type' => ['sometimes', 'string', 'in:internal,external'], 'capability' => ['sometimes', 'string', 'in:repair,commissioning,both'], 'specialties' => ['nullable', 'array'], 'phone' => ['nullable', 'string', 'max:50'], 'email' => ['nullable', 'email', 'max:255'], 'linked_user_id' => ['nullable', 'exists:users,id'], 'notes' => ['nullable', 'string'], 'is_active' => ['sometimes', 'boolean'], ]); $provider = ServiceProvider::create(array_merge($data, [ 'business_id' => $business->id, 'is_active' => $data['is_active'] ?? true, ])); return $this->jsonSuccess($provider, 'ارائه‌دهنده خدمات ایجاد شد.', 201); } public function update(Request $request, ServiceProvider $serviceProvider) { if (! $this->ensureBusinessOwnership($request, $serviceProvider->business_id)) { return $this->jsonError('دسترسی ندارید.', 403); } $data = $request->validate([ 'name' => ['sometimes', 'string', 'max:255'], 'provider_type' => ['sometimes', 'string', 'in:internal,external'], 'capability' => ['sometimes', 'string', 'in:repair,commissioning,both'], 'specialties' => ['nullable', 'array'], 'phone' => ['nullable', 'string', 'max:50'], 'email' => ['nullable', 'email', 'max:255'], 'linked_user_id' => ['nullable', 'exists:users,id'], 'notes' => ['nullable', 'string'], 'is_active' => ['sometimes', 'boolean'], ]); $serviceProvider->update($data); return $this->jsonSuccess($serviceProvider->fresh(), 'ارائه‌دهنده خدمات به‌روزرسانی شد.'); } public function destroy(Request $request, ServiceProvider $serviceProvider) { if (! $this->ensureBusinessOwnership($request, $serviceProvider->business_id)) { return $this->jsonError('دسترسی ندارید.', 403); } $serviceProvider->delete(); return $this->jsonSuccess(null, 'ارائه‌دهنده خدمات حذف شد.'); } }