tenantId()) ->with('jobType:id,name') ->orderByDesc('id'); if ($request->filled('company')) { $query->where('company_name', 'like', '%' . $request->company . '%'); } if ($request->filled('trade')) { $query->where('trade_name', $request->trade); } if ($request->filled('job_type_id')) { $query->where('job_type_id', $request->job_type_id); } if ($request->filled('search')) { $s = $request->search; $query->where(function ($q) use ($s) { $q->where('name', 'like', "%{$s}%") ->orWhere('phone', 'like', "%{$s}%"); }); } $perPage = $request->integer('per_page', 15); $workers = $query->paginate($perPage); return response()->json($workers); } public function workerStore(Request $request): JsonResponse { $validated = $request->validate([ 'company_name' => 'required|string|max:200', 'trade_name' => 'required|string|max:100', 'job_type_id' => 'nullable|exists:pmis_job_types,id', 'name' => 'required|string|max:50', 'phone' => 'nullable|string|max:20', 'birth_date' => 'nullable|string|max:6', 'ssn_gender' => 'nullable|string|max:1', 'wage' => 'nullable|integer|min:0', 'blood_type' => 'nullable|string|max:5', 'remark' => 'nullable|string|max:500', ]); $validated['tenant_id'] = $this->tenantId(); $validated['wage'] = $validated['wage'] ?? 0; $worker = PmisConstructionWorker::create($validated); $worker->load('jobType:id,name'); return response()->json($worker, 201); } public function workerUpdate(Request $request, int $id): JsonResponse { $worker = PmisConstructionWorker::tenant($this->tenantId())->findOrFail($id); $validated = $request->validate([ 'company_name' => 'sometimes|required|string|max:200', 'trade_name' => 'sometimes|required|string|max:100', 'job_type_id' => 'nullable|exists:pmis_job_types,id', 'name' => 'sometimes|required|string|max:50', 'phone' => 'nullable|string|max:20', 'birth_date' => 'nullable|string|max:6', 'ssn_gender' => 'nullable|string|max:1', 'wage' => 'nullable|integer|min:0', 'blood_type' => 'nullable|string|max:5', 'remark' => 'nullable|string|max:500', ]); $worker->update($validated); $worker->load('jobType:id,name'); return response()->json($worker); } public function workerDestroy(int $id): JsonResponse { $worker = PmisConstructionWorker::tenant($this->tenantId())->findOrFail($id); $worker->delete(); return response()->json(['message' => '삭제되었습니다.']); } // ── 직종 CRUD ── public function jobTypeList(): JsonResponse { $types = PmisJobType::tenant($this->tenantId()) ->where('is_active', true) ->orderBy('sort_order') ->orderBy('name') ->get(['id', 'name', 'sort_order']); return response()->json($types); } public function jobTypeStore(Request $request): JsonResponse { $validated = $request->validate([ 'name' => 'required|string|max:100', ]); $maxSort = PmisJobType::tenant($this->tenantId())->max('sort_order') ?? 0; $type = PmisJobType::create([ 'tenant_id' => $this->tenantId(), 'name' => $validated['name'], 'sort_order' => $maxSort + 1, ]); return response()->json($type, 201); } }