|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\HR\Http\Controllers; |
| 4 | + |
| 5 | +use App\Modules\HR\Models\Employee; |
| 6 | +use App\Modules\HR\Models\EmployeeEmergencyContact; |
| 7 | +use Illuminate\Http\RedirectResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Inertia\Inertia; |
| 10 | +use Inertia\Response; |
| 11 | + |
| 12 | +class EmployeeEmergencyContactController |
| 13 | +{ |
| 14 | + public function index(Employee $employee): Response |
| 15 | + { |
| 16 | + $contacts = $employee->emergencyContacts()->orderByDesc('is_primary')->get(); |
| 17 | + return Inertia::render('HR/EmergencyContacts/Index', compact('employee', 'contacts')); |
| 18 | + } |
| 19 | + |
| 20 | + public function create(Employee $employee): Response |
| 21 | + { |
| 22 | + return Inertia::render('HR/EmergencyContacts/Create', compact('employee')); |
| 23 | + } |
| 24 | + |
| 25 | + public function store(Request $request, Employee $employee): RedirectResponse |
| 26 | + { |
| 27 | + $data = $request->validate([ |
| 28 | + 'name' => 'required|string|max:255', |
| 29 | + 'relationship' => 'required|string|max:100', |
| 30 | + 'phone_primary' => 'required|string|max:50', |
| 31 | + 'phone_secondary' => 'nullable|string|max:50', |
| 32 | + 'email' => 'nullable|email|max:255', |
| 33 | + 'address' => 'nullable|string', |
| 34 | + 'is_primary' => 'boolean', |
| 35 | + 'notes' => 'nullable|string', |
| 36 | + ]); |
| 37 | + |
| 38 | + $data['employee_id'] = $employee->id; |
| 39 | + |
| 40 | + $contact = EmployeeEmergencyContact::create($data); |
| 41 | + |
| 42 | + if (!empty($data['is_primary'])) { |
| 43 | + $contact->markAsPrimary(); |
| 44 | + } |
| 45 | + |
| 46 | + return redirect()->route('hr.employees.emergency-contacts.index', $employee); |
| 47 | + } |
| 48 | + |
| 49 | + public function show(EmployeeEmergencyContact $emergencyContact): Response |
| 50 | + { |
| 51 | + $emergencyContact->load('employee'); |
| 52 | + return Inertia::render('HR/EmergencyContacts/Show', [ |
| 53 | + 'employee' => $emergencyContact->employee, |
| 54 | + 'contact' => $emergencyContact, |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + public function edit(EmployeeEmergencyContact $emergencyContact): Response |
| 59 | + { |
| 60 | + return Inertia::render('HR/EmergencyContacts/Edit', [ |
| 61 | + 'contact' => $emergencyContact, |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + public function update(Request $request, EmployeeEmergencyContact $emergencyContact): RedirectResponse |
| 66 | + { |
| 67 | + $data = $request->validate([ |
| 68 | + 'name' => 'required|string|max:255', |
| 69 | + 'relationship' => 'required|string|max:100', |
| 70 | + 'phone_primary' => 'required|string|max:50', |
| 71 | + 'phone_secondary' => 'nullable|string|max:50', |
| 72 | + 'email' => 'nullable|email|max:255', |
| 73 | + 'address' => 'nullable|string', |
| 74 | + 'is_primary' => 'boolean', |
| 75 | + 'notes' => 'nullable|string', |
| 76 | + ]); |
| 77 | + |
| 78 | + $emergencyContact->update($data); |
| 79 | + |
| 80 | + if (!empty($data['is_primary'])) { |
| 81 | + $emergencyContact->markAsPrimary(); |
| 82 | + } |
| 83 | + |
| 84 | + return redirect()->route('hr.employees.emergency-contacts.index', $emergencyContact->employee_id); |
| 85 | + } |
| 86 | + |
| 87 | + public function destroy(EmployeeEmergencyContact $emergencyContact): RedirectResponse |
| 88 | + { |
| 89 | + $employeeId = $emergencyContact->employee_id; |
| 90 | + $emergencyContact->delete(); |
| 91 | + return redirect()->route('hr.employees.emergency-contacts.index', $employeeId); |
| 92 | + } |
| 93 | + |
| 94 | + public function markPrimary(Employee $employee, EmployeeEmergencyContact $emergencyContact): RedirectResponse |
| 95 | + { |
| 96 | + $emergencyContact->markAsPrimary(); |
| 97 | + return redirect()->route('hr.employees.emergency-contacts.index', $employee); |
| 98 | + } |
| 99 | +} |
0 commit comments