|
| 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\EmployeeLoan; |
| 8 | +use App\Modules\HR\Models\LoanRepayment; |
| 9 | +use Illuminate\Http\RedirectResponse; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Illuminate\Validation\Rule; |
| 12 | +use Inertia\Inertia; |
| 13 | +use Inertia\Response; |
| 14 | + |
| 15 | +class EmployeeLoanController extends Controller |
| 16 | +{ |
| 17 | + public function index(Request $request): Response |
| 18 | + { |
| 19 | + $this->authorize('viewAny', EmployeeLoan::class); |
| 20 | + |
| 21 | + $loans = EmployeeLoan::with('employee') |
| 22 | + ->when($request->status, fn ($q) => $q->where('status', $request->status)) |
| 23 | + ->orderBy('created_at', 'desc') |
| 24 | + ->paginate(15) |
| 25 | + ->withQueryString(); |
| 26 | + |
| 27 | + return Inertia::render('HR/EmployeeLoans/Index', [ |
| 28 | + 'loans' => $loans, |
| 29 | + 'filters' => $request->only(['status']), |
| 30 | + ]); |
| 31 | + } |
| 32 | + |
| 33 | + public function create(): Response |
| 34 | + { |
| 35 | + $this->authorize('create', EmployeeLoan::class); |
| 36 | + |
| 37 | + return Inertia::render('HR/EmployeeLoans/Create', [ |
| 38 | + 'employees' => Employee::active()->orderBy('last_name')->get()->map(fn ($e) => [ |
| 39 | + 'id' => $e->id, |
| 40 | + 'full_name' => $e->full_name, |
| 41 | + ]), |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + public function store(Request $request): RedirectResponse |
| 46 | + { |
| 47 | + $this->authorize('create', EmployeeLoan::class); |
| 48 | + |
| 49 | + $validated = $request->validate([ |
| 50 | + 'employee_id' => ['required', Rule::exists('employees', 'id')], |
| 51 | + 'type' => ['required', Rule::in(['loan', 'advance'])], |
| 52 | + 'amount' => ['required', 'numeric', 'min:0.01'], |
| 53 | + 'interest_rate' => ['nullable', 'numeric', 'min:0', 'max:100'], |
| 54 | + 'purpose' => ['nullable', 'string', 'max:255'], |
| 55 | + 'notes' => ['nullable', 'string'], |
| 56 | + 'repayment_start_date' => ['nullable', 'date'], |
| 57 | + ]); |
| 58 | + |
| 59 | + $loan = EmployeeLoan::create([ |
| 60 | + ...$validated, |
| 61 | + 'tenant_id' => auth()->user()->tenant_id, |
| 62 | + 'outstanding_balance' => $validated['amount'], |
| 63 | + 'status' => 'pending', |
| 64 | + ]); |
| 65 | + |
| 66 | + return redirect()->route('hr.employee-loans.show', $loan) |
| 67 | + ->with('success', 'Loan created successfully.'); |
| 68 | + } |
| 69 | + |
| 70 | + public function show(EmployeeLoan $employeeLoan): Response |
| 71 | + { |
| 72 | + $this->authorize('view', $employeeLoan); |
| 73 | + |
| 74 | + $employeeLoan->load(['employee', 'repayments']); |
| 75 | + |
| 76 | + return Inertia::render('HR/EmployeeLoans/Show', [ |
| 77 | + 'loan' => array_merge($employeeLoan->toArray(), [ |
| 78 | + 'total_repaid' => $employeeLoan->total_repaid, |
| 79 | + 'is_fully_repaid' => $employeeLoan->is_fully_repaid, |
| 80 | + ]), |
| 81 | + 'can' => [ |
| 82 | + 'create' => auth()->user()->can('create', EmployeeLoan::class), |
| 83 | + 'delete' => auth()->user()->can('delete', $employeeLoan), |
| 84 | + ], |
| 85 | + ]); |
| 86 | + } |
| 87 | + |
| 88 | + public function destroy(EmployeeLoan $employeeLoan): RedirectResponse |
| 89 | + { |
| 90 | + $this->authorize('delete', $employeeLoan); |
| 91 | + |
| 92 | + if ($employeeLoan->status !== 'pending') { |
| 93 | + return back()->withErrors(['status' => 'Only pending loans can be deleted.']); |
| 94 | + } |
| 95 | + |
| 96 | + $employeeLoan->delete(); |
| 97 | + |
| 98 | + return redirect()->route('hr.employee-loans.index') |
| 99 | + ->with('success', 'Loan deleted.'); |
| 100 | + } |
| 101 | + |
| 102 | + public function approve(EmployeeLoan $employeeLoan): RedirectResponse |
| 103 | + { |
| 104 | + $this->authorize('create', EmployeeLoan::class); |
| 105 | + |
| 106 | + $employeeLoan->approve(auth()->user()); |
| 107 | + |
| 108 | + return back()->with('success', 'Loan approved.'); |
| 109 | + } |
| 110 | + |
| 111 | + public function cancel(EmployeeLoan $employeeLoan): RedirectResponse |
| 112 | + { |
| 113 | + $this->authorize('create', EmployeeLoan::class); |
| 114 | + |
| 115 | + $employeeLoan->cancel(); |
| 116 | + |
| 117 | + return back()->with('success', 'Loan cancelled.'); |
| 118 | + } |
| 119 | + |
| 120 | + public function addRepayment(Request $request, EmployeeLoan $employeeLoan): RedirectResponse |
| 121 | + { |
| 122 | + $this->authorize('create', EmployeeLoan::class); |
| 123 | + |
| 124 | + $validated = $request->validate([ |
| 125 | + 'amount' => ['required', 'numeric', 'min:0.01'], |
| 126 | + 'payment_date' => ['required', 'date'], |
| 127 | + 'notes' => ['nullable', 'string'], |
| 128 | + ]); |
| 129 | + |
| 130 | + $repayment = LoanRepayment::create([ |
| 131 | + ...$validated, |
| 132 | + 'tenant_id' => auth()->user()->tenant_id, |
| 133 | + 'employee_loan_id' => $employeeLoan->id, |
| 134 | + ]); |
| 135 | + |
| 136 | + $employeeLoan->decrement('outstanding_balance', $repayment->amount); |
| 137 | + |
| 138 | + if ($employeeLoan->fresh()->outstanding_balance <= 0) { |
| 139 | + $employeeLoan->update(['status' => 'completed', 'outstanding_balance' => 0]); |
| 140 | + } |
| 141 | + |
| 142 | + return back()->with('success', 'Repayment recorded.'); |
| 143 | + } |
| 144 | +} |
0 commit comments