|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\HR\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\HR\Models\Employee; |
| 7 | +use App\Modules\HR\Models\EmployeeTrainingRecord; |
| 8 | +use App\Modules\HR\Models\TrainingCourse; |
| 9 | +use Illuminate\Http\RedirectResponse; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Inertia\Inertia; |
| 12 | +use Inertia\Response; |
| 13 | + |
| 14 | +class EmployeeTrainingRecordController extends Controller |
| 15 | +{ |
| 16 | + public function index(): Response |
| 17 | + { |
| 18 | + $this->authorize('viewAny', EmployeeTrainingRecord::class); |
| 19 | + |
| 20 | + $records = EmployeeTrainingRecord::with(['employee', 'trainingCourse']) |
| 21 | + ->orderByDesc('completed_date') |
| 22 | + ->paginate(25); |
| 23 | + |
| 24 | + return Inertia::render('HR/TrainingRecords/Index', compact('records')); |
| 25 | + } |
| 26 | + |
| 27 | + public function create(Request $request): Response |
| 28 | + { |
| 29 | + $this->authorize('create', EmployeeTrainingRecord::class); |
| 30 | + |
| 31 | + $employees = Employee::where('status', 'active') |
| 32 | + ->orderBy('first_name') |
| 33 | + ->get(['id', 'first_name', 'last_name']); |
| 34 | + |
| 35 | + $courses = TrainingCourse::where('is_active', true) |
| 36 | + ->orderBy('title') |
| 37 | + ->get(['id', 'title']); |
| 38 | + |
| 39 | + $employeeId = $request->get('employee_id'); |
| 40 | + $courseId = $request->get('training_course_id'); |
| 41 | + |
| 42 | + return Inertia::render('HR/TrainingRecords/Create', compact('employees', 'courses', 'employeeId', 'courseId')); |
| 43 | + } |
| 44 | + |
| 45 | + public function store(Request $request): RedirectResponse |
| 46 | + { |
| 47 | + $this->authorize('create', EmployeeTrainingRecord::class); |
| 48 | + |
| 49 | + $data = $request->validate([ |
| 50 | + 'employee_id' => 'required|exists:employees,id', |
| 51 | + 'training_course_id' => 'nullable|exists:training_courses,id', |
| 52 | + 'course_title' => 'required|string|max:255', |
| 53 | + 'completed_date' => 'required|date', |
| 54 | + 'expiry_date' => 'nullable|date|after_or_equal:completed_date', |
| 55 | + 'score' => 'nullable|numeric|min:0', |
| 56 | + 'passed' => 'boolean', |
| 57 | + 'certificate_number' => 'nullable|string|max:255', |
| 58 | + 'notes' => 'nullable|string', |
| 59 | + ]); |
| 60 | + |
| 61 | + // Snapshot course_title from selected course if not provided or if course is selected |
| 62 | + if (!empty($data['training_course_id'])) { |
| 63 | + $course = TrainingCourse::find($data['training_course_id']); |
| 64 | + if ($course) { |
| 65 | + $data['course_title'] = $course->title; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $record = EmployeeTrainingRecord::create([ |
| 70 | + 'tenant_id' => auth()->user()->tenant_id, |
| 71 | + ...$data, |
| 72 | + ]); |
| 73 | + |
| 74 | + return redirect()->route('hr.training-records.show', $record) |
| 75 | + ->with('success', 'Training record created.'); |
| 76 | + } |
| 77 | + |
| 78 | + public function show(EmployeeTrainingRecord $trainingRecord): Response |
| 79 | + { |
| 80 | + $this->authorize('view', $trainingRecord); |
| 81 | + |
| 82 | + $trainingRecord->load(['employee', 'trainingCourse']); |
| 83 | + |
| 84 | + return Inertia::render('HR/TrainingRecords/Show', compact('trainingRecord')); |
| 85 | + } |
| 86 | + |
| 87 | + public function destroy(EmployeeTrainingRecord $trainingRecord): RedirectResponse |
| 88 | + { |
| 89 | + $this->authorize('delete', $trainingRecord); |
| 90 | + |
| 91 | + $trainingRecord->delete(); |
| 92 | + |
| 93 | + return redirect()->route('hr.training-records.index') |
| 94 | + ->with('success', 'Training record deleted.'); |
| 95 | + } |
| 96 | +} |
0 commit comments