|
| 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\EmployeeGoal; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Inertia\Inertia; |
| 11 | +use Inertia\Response; |
| 12 | + |
| 13 | +class EmployeeGoalController extends Controller |
| 14 | +{ |
| 15 | + public function index(Request $request): Response |
| 16 | + { |
| 17 | + $this->authorize('viewAny', EmployeeGoal::class); |
| 18 | + |
| 19 | + $query = EmployeeGoal::with('employee') |
| 20 | + ->orderByDesc('created_at'); |
| 21 | + |
| 22 | + if ($request->filled('employee_id')) { |
| 23 | + $query->where('employee_id', $request->employee_id); |
| 24 | + } |
| 25 | + |
| 26 | + if ($request->filled('status')) { |
| 27 | + $query->where('status', $request->status); |
| 28 | + } |
| 29 | + |
| 30 | + $goals = $query->paginate(15); |
| 31 | + $filters = $request->only(['employee_id', 'status']); |
| 32 | + |
| 33 | + return Inertia::render('HR/EmployeeGoals/Index', compact('goals', 'filters')); |
| 34 | + } |
| 35 | + |
| 36 | + public function create(): Response |
| 37 | + { |
| 38 | + $this->authorize('create', EmployeeGoal::class); |
| 39 | + |
| 40 | + $employees = Employee::where('status', 'active') |
| 41 | + ->orderBy('first_name') |
| 42 | + ->get(['id', 'first_name', 'last_name']); |
| 43 | + |
| 44 | + return Inertia::render('HR/EmployeeGoals/Create', compact('employees')); |
| 45 | + } |
| 46 | + |
| 47 | + public function store(Request $request): RedirectResponse |
| 48 | + { |
| 49 | + $this->authorize('create', EmployeeGoal::class); |
| 50 | + |
| 51 | + $data = $request->validate([ |
| 52 | + 'employee_id' => ['required', 'exists:employees,id'], |
| 53 | + 'title' => ['required', 'string'], |
| 54 | + 'start_date' => ['required', 'date'], |
| 55 | + 'due_date' => ['required', 'date', 'after_or_equal:start_date'], |
| 56 | + 'priority' => ['nullable', 'in:low,medium,high'], |
| 57 | + ]); |
| 58 | + |
| 59 | + $data['tenant_id'] = app('tenant')->id; |
| 60 | + $data['created_by'] = auth()->id(); |
| 61 | + |
| 62 | + EmployeeGoal::create($data); |
| 63 | + |
| 64 | + return redirect()->route('hr.employee-goals.index'); |
| 65 | + } |
| 66 | + |
| 67 | + public function show(EmployeeGoal $employeeGoal): Response |
| 68 | + { |
| 69 | + $this->authorize('view', $employeeGoal); |
| 70 | + |
| 71 | + $employeeGoal->load('employee'); |
| 72 | + |
| 73 | + return Inertia::render('HR/EmployeeGoals/Show', compact('employeeGoal')); |
| 74 | + } |
| 75 | + |
| 76 | + public function edit(EmployeeGoal $employeeGoal): Response |
| 77 | + { |
| 78 | + $this->authorize('update', $employeeGoal); |
| 79 | + |
| 80 | + $employees = Employee::where('status', 'active') |
| 81 | + ->orderBy('first_name') |
| 82 | + ->get(['id', 'first_name', 'last_name']); |
| 83 | + |
| 84 | + $employeeGoal->load('employee'); |
| 85 | + |
| 86 | + return Inertia::render('HR/EmployeeGoals/Edit', compact('employeeGoal', 'employees')); |
| 87 | + } |
| 88 | + |
| 89 | + public function update(Request $request, EmployeeGoal $employeeGoal): RedirectResponse |
| 90 | + { |
| 91 | + $this->authorize('update', $employeeGoal); |
| 92 | + |
| 93 | + $data = $request->validate([ |
| 94 | + 'title' => ['required', 'string'], |
| 95 | + 'start_date' => ['required', 'date'], |
| 96 | + 'due_date' => ['required', 'date', 'after_or_equal:start_date'], |
| 97 | + 'priority' => ['nullable', 'in:low,medium,high'], |
| 98 | + ]); |
| 99 | + |
| 100 | + $employeeGoal->update($data); |
| 101 | + |
| 102 | + return redirect()->route('hr.employee-goals.index'); |
| 103 | + } |
| 104 | + |
| 105 | + public function destroy(EmployeeGoal $employeeGoal): RedirectResponse |
| 106 | + { |
| 107 | + $this->authorize('delete', $employeeGoal); |
| 108 | + |
| 109 | + $employeeGoal->delete(); |
| 110 | + |
| 111 | + return redirect()->route('hr.employee-goals.index'); |
| 112 | + } |
| 113 | + |
| 114 | + public function complete(EmployeeGoal $employeeGoal): RedirectResponse |
| 115 | + { |
| 116 | + $this->authorize('complete', $employeeGoal); |
| 117 | + |
| 118 | + $employeeGoal->complete(); |
| 119 | + |
| 120 | + return redirect()->route('hr.employee-goals.index'); |
| 121 | + } |
| 122 | + |
| 123 | + public function miss(EmployeeGoal $employeeGoal): RedirectResponse |
| 124 | + { |
| 125 | + $this->authorize('miss', $employeeGoal); |
| 126 | + |
| 127 | + $employeeGoal->miss(); |
| 128 | + |
| 129 | + return redirect()->route('hr.employee-goals.index'); |
| 130 | + } |
| 131 | + |
| 132 | + public function cancel(EmployeeGoal $employeeGoal): RedirectResponse |
| 133 | + { |
| 134 | + $this->authorize('cancel', $employeeGoal); |
| 135 | + |
| 136 | + $employeeGoal->cancel(); |
| 137 | + |
| 138 | + return redirect()->route('hr.employee-goals.index'); |
| 139 | + } |
| 140 | + |
| 141 | + public function updateProgress(Request $request, EmployeeGoal $employeeGoal): RedirectResponse |
| 142 | + { |
| 143 | + $this->authorize('update', $employeeGoal); |
| 144 | + |
| 145 | + $data = $request->validate([ |
| 146 | + 'progress' => ['required', 'integer', 'min:0', 'max:100'], |
| 147 | + ]); |
| 148 | + |
| 149 | + $employeeGoal->updateProgress($data['progress']); |
| 150 | + |
| 151 | + return redirect()->route('hr.employee-goals.index'); |
| 152 | + } |
| 153 | +} |
0 commit comments