|
| 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\EmployeeDocument; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Inertia\Inertia; |
| 11 | +use Inertia\Response; |
| 12 | + |
| 13 | +class EmployeeDocumentController extends Controller |
| 14 | +{ |
| 15 | + public function index(Request $request): Response |
| 16 | + { |
| 17 | + $this->authorize('viewAny', EmployeeDocument::class); |
| 18 | + |
| 19 | + $documents = EmployeeDocument::with('employee') |
| 20 | + ->when($request->employee_id, fn ($q) => $q->where('employee_id', $request->employee_id)) |
| 21 | + ->when($request->document_type, fn ($q) => $q->where('document_type', $request->document_type)) |
| 22 | + ->latest() |
| 23 | + ->paginate(20) |
| 24 | + ->withQueryString(); |
| 25 | + |
| 26 | + return Inertia::render('HR/EmployeeDocuments/Index', [ |
| 27 | + 'documents' => $documents, |
| 28 | + 'filters' => $request->only(['employee_id', 'document_type']), |
| 29 | + ]); |
| 30 | + } |
| 31 | + |
| 32 | + public function store(Request $request): RedirectResponse |
| 33 | + { |
| 34 | + $this->authorize('create', EmployeeDocument::class); |
| 35 | + |
| 36 | + $validated = $request->validate([ |
| 37 | + 'employee_id' => 'required|exists:employees,id', |
| 38 | + 'document_type' => 'required|string|max:50', |
| 39 | + 'document_name' => 'required|string|max:255', |
| 40 | + 'document_number' => 'nullable|string|max:100', |
| 41 | + 'file_url' => 'nullable|string|max:500', |
| 42 | + 'issued_date' => 'nullable|date', |
| 43 | + 'expiry_date' => 'nullable|date', |
| 44 | + 'notes' => 'nullable|string', |
| 45 | + ]); |
| 46 | + |
| 47 | + EmployeeDocument::create([ |
| 48 | + 'tenant_id' => auth()->user()->tenant_id, |
| 49 | + ...$validated, |
| 50 | + ]); |
| 51 | + |
| 52 | + return back()->with('success', 'Document added.'); |
| 53 | + } |
| 54 | + |
| 55 | + public function show(EmployeeDocument $employeeDocument): Response |
| 56 | + { |
| 57 | + $this->authorize('view', $employeeDocument); |
| 58 | + $employeeDocument->load(['employee', 'verifiedBy']); |
| 59 | + |
| 60 | + return Inertia::render('HR/EmployeeDocuments/Show', [ |
| 61 | + 'document' => $employeeDocument, |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + public function verify(EmployeeDocument $employeeDocument): RedirectResponse |
| 66 | + { |
| 67 | + $this->authorize('update', $employeeDocument); |
| 68 | + $employeeDocument->verify(auth()->id()); |
| 69 | + |
| 70 | + return back()->with('success', 'Document verified.'); |
| 71 | + } |
| 72 | + |
| 73 | + public function destroy(EmployeeDocument $employeeDocument): RedirectResponse |
| 74 | + { |
| 75 | + $this->authorize('delete', $employeeDocument); |
| 76 | + $employeeDocument->delete(); |
| 77 | + |
| 78 | + return back()->with('success', 'Document deleted.'); |
| 79 | + } |
| 80 | +} |
0 commit comments