84 lines
2.7 KiB
PHP
84 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Position;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class PositionController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = Position::withCount('employees');
|
|
if ($request->search) {
|
|
$query->where('name', 'like', '%' . $request->search . '%');
|
|
}
|
|
return Inertia::render('admin/positions/index', [
|
|
'positions' => $query->latest()->get(),
|
|
'filters' => $request->only(['search']),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return Inertia::render('admin/positions/create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255|unique:positions,name',
|
|
'basic_salary' => 'nullable|integer|min:0',
|
|
], [
|
|
'name.required' => 'Nama jabatan wajib diisi.',
|
|
'name.max' => 'Nama jabatan maksimal 255 karakter.',
|
|
'name.unique' => 'Nama jabatan sudah terdaftar.',
|
|
'basic_salary.integer' => 'Gaji pokok harus berupa angka bulat.',
|
|
'basic_salary.min' => 'Gaji pokok tidak boleh negatif.',
|
|
]);
|
|
|
|
Position::create($validated);
|
|
|
|
return redirect()->to('/admin/positions')
|
|
->with('success', 'Jabatan berhasil ditambahkan.');
|
|
}
|
|
|
|
public function edit(Position $position)
|
|
{
|
|
return Inertia::render('admin/positions/edit', [
|
|
'position' => $position
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, Position $position)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255|unique:positions,name,' . $position->id,
|
|
'basic_salary' => 'nullable|integer|min:0',
|
|
], [
|
|
'name.required' => 'Nama jabatan wajib diisi.',
|
|
'name.max' => 'Nama jabatan maksimal 255 karakter.',
|
|
'name.unique' => 'Nama jabatan sudah terdaftar.',
|
|
'basic_salary.integer' => 'Gaji pokok harus berupa angka bulat.',
|
|
'basic_salary.min' => 'Gaji pokok tidak boleh negatif.',
|
|
]);
|
|
|
|
$position->update($validated);
|
|
|
|
return redirect()->to('/admin/positions')
|
|
->with('success', 'Jabatan berhasil diperbarui.');
|
|
}
|
|
|
|
public function destroy(Position $position)
|
|
{
|
|
if ($position->employees()->count() > 0) {
|
|
return back()->with('error', 'Gagal hapus! Masih ada karyawan dengan jabatan ini.');
|
|
}
|
|
|
|
$position->delete();
|
|
|
|
return back()->with('success', 'Jabatan berhasil dihapus.');
|
|
}
|
|
} |