|
| 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\ShiftAssignment; |
| 8 | +use App\Modules\HR\Models\ShiftTemplate; |
| 9 | +use Illuminate\Http\RedirectResponse; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Inertia\Inertia; |
| 12 | +use Inertia\Response; |
| 13 | + |
| 14 | +class ShiftAssignmentController extends Controller |
| 15 | +{ |
| 16 | + public function index(Request $request): Response |
| 17 | + { |
| 18 | + $this->authorize('viewAny', ShiftAssignment::class); |
| 19 | + |
| 20 | + $query = ShiftAssignment::with(['shiftTemplate', 'employee']) |
| 21 | + ->orderBy('assigned_date', 'desc'); |
| 22 | + |
| 23 | + if ($request->filled('employee_id')) { |
| 24 | + $query->where('employee_id', $request->employee_id); |
| 25 | + } |
| 26 | + |
| 27 | + if ($request->filled('date_from')) { |
| 28 | + $query->where('assigned_date', '>=', $request->date_from); |
| 29 | + } |
| 30 | + |
| 31 | + if ($request->filled('date_to')) { |
| 32 | + $query->where('assigned_date', '<=', $request->date_to); |
| 33 | + } |
| 34 | + |
| 35 | + $assignments = $query->paginate(20)->withQueryString(); |
| 36 | + |
| 37 | + $filters = $request->only(['employee_id', 'date_from', 'date_to']); |
| 38 | + |
| 39 | + return Inertia::render('HR/ShiftAssignments/Index', compact('assignments', 'filters')); |
| 40 | + } |
| 41 | + |
| 42 | + public function create(): Response |
| 43 | + { |
| 44 | + $this->authorize('create', ShiftAssignment::class); |
| 45 | + |
| 46 | + $shiftTemplates = ShiftTemplate::where('is_active', true)->orderBy('name')->get(); |
| 47 | + $employees = Employee::orderBy('first_name')->get(['id', 'first_name', 'last_name']); |
| 48 | + |
| 49 | + return Inertia::render('HR/ShiftAssignments/Create', compact('shiftTemplates', 'employees')); |
| 50 | + } |
| 51 | + |
| 52 | + public function store(Request $request): RedirectResponse |
| 53 | + { |
| 54 | + $this->authorize('create', ShiftAssignment::class); |
| 55 | + |
| 56 | + $data = $request->validate([ |
| 57 | + 'shift_template_id' => ['required', 'exists:shift_templates,id'], |
| 58 | + 'employee_id' => ['required', 'exists:employees,id'], |
| 59 | + 'assigned_date' => ['required', 'date'], |
| 60 | + 'notes' => ['nullable', 'string'], |
| 61 | + ]); |
| 62 | + |
| 63 | + $data['tenant_id'] = auth()->user()->tenant_id; |
| 64 | + $data['status'] = 'scheduled'; |
| 65 | + |
| 66 | + ShiftAssignment::create($data); |
| 67 | + |
| 68 | + return redirect()->route('hr.shift-assignments.index')->with('success', 'Shift assignment created.'); |
| 69 | + } |
| 70 | + |
| 71 | + public function destroy(ShiftAssignment $shiftAssignment): RedirectResponse |
| 72 | + { |
| 73 | + $this->authorize('delete', $shiftAssignment); |
| 74 | + |
| 75 | + $shiftAssignment->delete(); |
| 76 | + |
| 77 | + return redirect()->back()->with('success', 'Shift assignment deleted.'); |
| 78 | + } |
| 79 | + |
| 80 | + public function markStatus(Request $request, ShiftAssignment $shiftAssignment): RedirectResponse |
| 81 | + { |
| 82 | + $this->authorize('update', $shiftAssignment); |
| 83 | + |
| 84 | + $data = $request->validate([ |
| 85 | + 'status' => ['required', 'in:scheduled,completed,absent,swapped'], |
| 86 | + ]); |
| 87 | + |
| 88 | + $shiftAssignment->update($data); |
| 89 | + |
| 90 | + return redirect()->back()->with('success', 'Status updated.'); |
| 91 | + } |
| 92 | +} |
0 commit comments