TIFNJK_E41222887/app/Http/Controllers/Admin/EmployeeController.php

118 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreEmployeeRequest;
use App\Http\Requests\UpdateEmployeeRequest;
use App\Models\Department;
use App\Models\Employee;
use App\Models\Position;
use Illuminate\Http\Request;
use Inertia\Inertia;
class EmployeeController extends Controller
{
public function index(Request $request)
{
$query = Employee::with(['user', 'department', 'position']);
if ($request->search) {
$query->where('nip', 'like', '%' . $request->search . '%')
->orWhere('name', 'like', '%' . $request->search . '%');
}
if ($request->department_id) {
$query->where('department_id', $request->department_id);
}
return Inertia::render('admin/employees/index', [
'employees' => $query->latest()->get(),
'departments' => Department::all(),
'filters' => $request->only(['search', 'department_id']),
]);
}
public function create()
{
return Inertia::render('admin/employees/create', [
'departments' => Department::all(),
'positions' => Position::all(),
]);
}
public function store(StoreEmployeeRequest $request)
{
$validated = $request->validated();
// Simpan hanya data karyawan ke tabel employees.
// Pembuatan akun User dilakukan secara terpisah oleh admin
// melalui menu Manajemen Pengguna.
Employee::create([
'nip' => $validated['nip'],
'name' => $validated['name'],
'email' => $validated['email'] ?? null,
'gender' => $validated['gender'],
'place_of_birth' => $validated['place_of_birth'] ?? null,
'birth_date' => $validated['birth_date'],
'address' => $validated['address'] ?? null,
'phone_number' => $validated['phone_number'] ?? null,
'department_id' => $validated['department_id'],
'position_id' => $validated['position_id'],
'status' => $validated['status'],
'join_date' => $validated['join_date'],
]);
return redirect()->route('admin.employees.index')
->with('success', 'Data karyawan berhasil ditambahkan.');
}
public function edit(Employee $employee)
{
$employee->load(['user', 'department', 'position']);
return Inertia::render('admin/employees/edit', [
'employee' => $employee,
'departments' => Department::all(),
'positions' => Position::all(),
]);
}
public function update(UpdateEmployeeRequest $request, Employee $employee)
{
$validated = $request->validated();
// Sinkronisasi ke akun User jika karyawan sudah memiliki akun
if ($employee->user) {
$employee->user->update([
'name' => $validated['name'],
'email' => $validated['email'] ?? $employee->user->email,
]);
}
// Update data karyawan di tabel employees
$employee->update([
'name' => $validated['name'],
'email' => $validated['email'] ?? null,
'nip' => $validated['nip'],
'gender' => $validated['gender'],
'place_of_birth' => $validated['place_of_birth'] ?? null,
'birth_date' => $validated['birth_date'],
'address' => $validated['address'] ?? null,
'phone_number' => $validated['phone_number'] ?? null,
'department_id' => $validated['department_id'],
'position_id' => $validated['position_id'],
'status' => $validated['status'],
'join_date' => $validated['join_date'],
]);
return redirect()->route('admin.employees.index')
->with('success', 'Data karyawan berhasil diperbarui.');
}
public function destroy(Employee $employee)
{
$employee->user?->delete();
return back()->with('success', 'Karyawan berhasil dihapus.');
}
}