|
| 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\PerformanceReview; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Inertia\Inertia; |
| 11 | +use Inertia\Response; |
| 12 | + |
| 13 | +class PerformanceReviewController extends Controller |
| 14 | +{ |
| 15 | + public function index(): Response |
| 16 | + { |
| 17 | + $this->authorize('viewAny', PerformanceReview::class); |
| 18 | + $reviews = PerformanceReview::with(['employee', 'reviewer']) |
| 19 | + ->orderByDesc('period_end') |
| 20 | + ->paginate(25); |
| 21 | + return Inertia::render('HR/PerformanceReviews/Index', compact('reviews')); |
| 22 | + } |
| 23 | + |
| 24 | + public function create(Request $request): Response |
| 25 | + { |
| 26 | + $this->authorize('create', PerformanceReview::class); |
| 27 | + $employees = Employee::where('status', 'active')->orderBy('first_name')->get(['id', 'first_name', 'last_name']); |
| 28 | + $employeeId = $request->get('employee_id'); |
| 29 | + return Inertia::render('HR/PerformanceReviews/Create', compact('employees', 'employeeId')); |
| 30 | + } |
| 31 | + |
| 32 | + public function store(Request $request): RedirectResponse |
| 33 | + { |
| 34 | + $this->authorize('create', PerformanceReview::class); |
| 35 | + $data = $request->validate([ |
| 36 | + 'employee_id' => 'required|exists:employees,id', |
| 37 | + 'period_start' => 'required|date', |
| 38 | + 'period_end' => 'required|date|after_or_equal:period_start', |
| 39 | + 'comments' => 'nullable|string', |
| 40 | + 'goals' => 'nullable|array', |
| 41 | + 'goals.*.title' => 'required_with:goals|string', |
| 42 | + 'goals.*.description' => 'nullable|string', |
| 43 | + 'competencies' => 'nullable|array', |
| 44 | + 'competencies.*.name' => 'required_with:competencies|string', |
| 45 | + 'competencies.*.rating' => 'nullable|integer|min:1|max:5', |
| 46 | + 'competencies.*.notes' => 'nullable|string', |
| 47 | + ]); |
| 48 | + |
| 49 | + $review = PerformanceReview::create([ |
| 50 | + 'tenant_id' => auth()->user()->tenant_id, |
| 51 | + 'employee_id' => $data['employee_id'], |
| 52 | + 'reviewer_id' => auth()->id(), |
| 53 | + 'period_start' => $data['period_start'], |
| 54 | + 'period_end' => $data['period_end'], |
| 55 | + 'status' => 'draft', |
| 56 | + 'comments' => $data['comments'] ?? null, |
| 57 | + ]); |
| 58 | + |
| 59 | + foreach ($data['goals'] ?? [] as $goal) { |
| 60 | + $review->goals()->create([ |
| 61 | + 'title' => $goal['title'], |
| 62 | + 'description' => $goal['description'] ?? null, |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + foreach ($data['competencies'] ?? [] as $comp) { |
| 67 | + $review->competencies()->create([ |
| 68 | + 'name' => $comp['name'], |
| 69 | + 'rating' => $comp['rating'] ?? null, |
| 70 | + 'notes' => $comp['notes'] ?? null, |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + return redirect()->route('hr.performance-reviews.show', $review)->with('success', 'Review created.'); |
| 75 | + } |
| 76 | + |
| 77 | + public function show(PerformanceReview $performanceReview): Response |
| 78 | + { |
| 79 | + $this->authorize('view', $performanceReview); |
| 80 | + $performanceReview->load(['employee', 'reviewer', 'goals', 'competencies']); |
| 81 | + return Inertia::render('HR/PerformanceReviews/Show', compact('performanceReview')); |
| 82 | + } |
| 83 | + |
| 84 | + public function startReview(PerformanceReview $performanceReview): RedirectResponse |
| 85 | + { |
| 86 | + $this->authorize('update', $performanceReview); |
| 87 | + abort_unless($performanceReview->status === 'draft', 422, 'Only draft reviews can be started.'); |
| 88 | + $performanceReview->update(['status' => 'in_review']); |
| 89 | + return back()->with('success', 'Review started.'); |
| 90 | + } |
| 91 | + |
| 92 | + public function complete(PerformanceReview $performanceReview, Request $request): RedirectResponse |
| 93 | + { |
| 94 | + $this->authorize('update', $performanceReview); |
| 95 | + abort_unless($performanceReview->status === 'in_review', 422, 'Only in-review reviews can be completed.'); |
| 96 | + $request->validate(['overall_rating' => 'required|integer|min:1|max:5']); |
| 97 | + $performanceReview->update([ |
| 98 | + 'status' => 'completed', |
| 99 | + 'overall_rating' => $request->overall_rating, |
| 100 | + 'completed_at' => now(), |
| 101 | + ]); |
| 102 | + return back()->with('success', 'Review completed.'); |
| 103 | + } |
| 104 | + |
| 105 | + public function updateGoal(PerformanceReview $performanceReview, Request $request, int $goalId): RedirectResponse |
| 106 | + { |
| 107 | + $this->authorize('update', $performanceReview); |
| 108 | + $goal = $performanceReview->goals()->findOrFail($goalId); |
| 109 | + $request->validate([ |
| 110 | + 'achieved' => 'boolean', |
| 111 | + 'achievement_notes' => 'nullable|string', |
| 112 | + ]); |
| 113 | + $goal->update($request->only('achieved', 'achievement_notes')); |
| 114 | + return back()->with('success', 'Goal updated.'); |
| 115 | + } |
| 116 | + |
| 117 | + public function destroy(PerformanceReview $performanceReview): RedirectResponse |
| 118 | + { |
| 119 | + $this->authorize('delete', $performanceReview); |
| 120 | + abort_unless($performanceReview->status === 'draft', 422, 'Only draft reviews can be deleted.'); |
| 121 | + $performanceReview->delete(); |
| 122 | + return redirect()->route('hr.performance-reviews.index')->with('success', 'Review deleted.'); |
| 123 | + } |
| 124 | +} |
0 commit comments