feat: [pmis] 인원관리 실제 CRUD 구현
- PmisWorkforceController: 인원/직종 CRUD API - PmisConstructionWorker, PmisJobType 모델 추가 - 인원등록 탭: 실제 DB CRUD, 페이지네이션, 필터, 모달 - 직종 44개 시드 데이터 등록 - API 라우트 추가 (workers, job-types)
This commit is contained in:
133
app/Http/Controllers/Juil/PmisWorkforceController.php
Normal file
133
app/Http/Controllers/Juil/PmisWorkforceController.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Juil;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Juil\PmisConstructionWorker;
|
||||
use App\Models\Juil\PmisJobType;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PmisWorkforceController extends Controller
|
||||
{
|
||||
private function tenantId(): int
|
||||
{
|
||||
return (int) session('current_tenant_id', 1);
|
||||
}
|
||||
|
||||
// ── 인원 CRUD ──
|
||||
|
||||
public function workerList(Request $request): JsonResponse
|
||||
{
|
||||
$query = PmisConstructionWorker::tenant($this->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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user