- Implemented employee columns for data table with sorting and action buttons. - Created employee creation form with validation and default password information. - Developed employee editing form pre-filled with existing data. - Added employee listing page with delete and reset password functionalities. - Introduced leave request management with columns for status and actions. - Created leave request form for adding and editing requests with date pickers. - Implemented confirmation dialogs for delete, approve, and reject actions.
120 lines
4.3 KiB
PHP
120 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\HR;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\HR\EmployeeRequest;
|
|
use App\Models\User;
|
|
use App\Services\Admin\HR\EmployeeService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class EmployeeController extends Controller
|
|
{
|
|
public function __construct(
|
|
private EmployeeService $service
|
|
) {}
|
|
|
|
public function index(): Response
|
|
{
|
|
return Inertia::render('admin/hr/employee/index', [
|
|
'employees' => $this->service->getAll(),
|
|
]);
|
|
}
|
|
|
|
public function create(): Response
|
|
{
|
|
return Inertia::render('admin/hr/employee/create');
|
|
}
|
|
|
|
public function store(EmployeeRequest $request): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->create($request->validated());
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Pegawai berhasil ditambahkan.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.hr.employees.index');
|
|
}
|
|
|
|
public function edit(User $employee): Response
|
|
{
|
|
return Inertia::render('admin/hr/employee/edit', [
|
|
'employee' => $this->service->getById($employee->id),
|
|
]);
|
|
}
|
|
|
|
public function update(EmployeeRequest $request, User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->update($employee, $request->validated());
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Pegawai berhasil diperbarui.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.hr.employees.index');
|
|
}
|
|
|
|
public function destroy(User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->delete($employee);
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Pegawai berhasil dihapus.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.hr.employees.index');
|
|
}
|
|
|
|
public function toggleActive(User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->toggleActive($employee);
|
|
|
|
$status = $employee->fresh()->is_active ? 'diaktifkan' : 'dinonaktifkan';
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => "Pegawai berhasil {$status}."]);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.hr.employees.index');
|
|
}
|
|
|
|
public function resetPassword(User $employee): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->resetPassword($employee);
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Kata sandi pegawai berhasil direset ke kata sandi default.']);
|
|
} catch (ValidationException $e) {
|
|
$firstError = collect($e->errors())->flatten()->first();
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $firstError ?? 'Terjadi kesalahan.']);
|
|
} catch (\Exception $e) {
|
|
Inertia::flash('toast', ['type' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
return to_route('admin.hr.employees.index');
|
|
}
|
|
}
|