|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\HR\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\HR\Models\EmployeePositionChange; |
| 7 | +use Illuminate\Http\RedirectResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Inertia\Inertia; |
| 10 | +use Inertia\Response; |
| 11 | + |
| 12 | +class EmployeePositionChangeController extends Controller |
| 13 | +{ |
| 14 | + public function index(Request $request): Response |
| 15 | + { |
| 16 | + $this->authorize('viewAny', EmployeePositionChange::class); |
| 17 | + |
| 18 | + $changes = EmployeePositionChange::with(['employee', 'fromDepartment', 'toDepartment']) |
| 19 | + ->when($request->employee_id, fn ($q) => $q->where('employee_id', $request->employee_id)) |
| 20 | + ->orderBy('effective_date', 'desc') |
| 21 | + ->paginate(20) |
| 22 | + ->withQueryString(); |
| 23 | + |
| 24 | + return Inertia::render('HR/PositionChanges/Index', [ |
| 25 | + 'changes' => $changes, |
| 26 | + 'filters' => $request->only(['employee_id']), |
| 27 | + ]); |
| 28 | + } |
| 29 | + |
| 30 | + public function store(Request $request): RedirectResponse |
| 31 | + { |
| 32 | + $this->authorize('create', EmployeePositionChange::class); |
| 33 | + |
| 34 | + $validated = $request->validate([ |
| 35 | + 'employee_id' => 'required|exists:employees,id', |
| 36 | + 'change_type' => 'required|string|in:promotion,demotion,transfer,salary_change,title_change,department_change', |
| 37 | + 'effective_date' => 'required|date', |
| 38 | + 'from_title' => 'nullable|string|max:255', |
| 39 | + 'to_title' => 'nullable|string|max:255', |
| 40 | + 'from_department_id' => 'nullable|exists:departments,id', |
| 41 | + 'to_department_id' => 'nullable|exists:departments,id', |
| 42 | + 'from_salary' => 'nullable|numeric|min:0', |
| 43 | + 'to_salary' => 'nullable|numeric|min:0', |
| 44 | + 'reason' => 'nullable|string', |
| 45 | + 'notes' => 'nullable|string', |
| 46 | + ]); |
| 47 | + |
| 48 | + $change = EmployeePositionChange::create([ |
| 49 | + 'tenant_id' => auth()->user()->tenant_id, |
| 50 | + ...$validated, |
| 51 | + ]); |
| 52 | + |
| 53 | + return redirect()->route('hr.position-changes.show', $change); |
| 54 | + } |
| 55 | + |
| 56 | + public function show(EmployeePositionChange $positionChange): Response |
| 57 | + { |
| 58 | + $this->authorize('view', $positionChange); |
| 59 | + |
| 60 | + $positionChange->load(['employee', 'fromDepartment', 'toDepartment', 'approvedBy']); |
| 61 | + |
| 62 | + return Inertia::render('HR/PositionChanges/Show', [ |
| 63 | + 'change' => $positionChange, |
| 64 | + ]); |
| 65 | + } |
| 66 | + |
| 67 | + public function approve(EmployeePositionChange $positionChange): RedirectResponse |
| 68 | + { |
| 69 | + $this->authorize('update', $positionChange); |
| 70 | + |
| 71 | + $positionChange->approve(auth()->id()); |
| 72 | + |
| 73 | + return redirect()->back()->with('success', 'Position change approved successfully.'); |
| 74 | + } |
| 75 | + |
| 76 | + public function destroy(EmployeePositionChange $positionChange): RedirectResponse |
| 77 | + { |
| 78 | + $this->authorize('delete', $positionChange); |
| 79 | + |
| 80 | + $positionChange->delete(); |
| 81 | + |
| 82 | + return redirect()->route('hr.position-changes.index'); |
| 83 | + } |
| 84 | +} |
0 commit comments