instalasi-sim-rs/app/Http/Controllers/EmployeeController.php

242 lines
8.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Employee;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Inertia\Inertia;
class EmployeeController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$employees = Employee::with(['user' => function($query) {
$query->select('id', 'name', 'email');
}])
->orderBy('created_at', 'desc')
->select([
'id',
'user_id',
'employee_id',
'gender',
'nik',
'birth_date',
'birth_place',
'address',
'phone_number',
'join_date',
'is_active',
'created_at',
])
->paginate(10)
->through(function ($employee) {
return [
'id' => $employee->id,
'employee_id' => $employee->employee_id,
'name' => $employee->user->name,
'email' => $employee->user->email,
'nik' => $employee->nik,
'birth_date' => $employee->birth_date,
'birth_place' => $employee->birth_place,
'address' => $employee->address,
'gender' => $employee->gender,
'phone_number' => $employee->phone_number,
'join_date' => $employee->join_date->format('Y-m-d'),
'is_active' => (bool) $employee->is_active,
'created_at' => $employee->created_at->format('Y-m-d H:i:s'),
];
});
return Inertia::render('employees/index', [
'employees' => $employees,
'status' => session('status'),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return Inertia::render('employees/form', [
'mode' => 'create',
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
// Validasi untuk User
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|string|min:8',
// Validasi untuk Employee
'gender' => 'required|in:laki-laki,perempuan',
'nik' => 'nullable|string|max:16',
'birth_date' => 'required|date',
'birth_place' => 'nullable|string|max:100',
'address' => 'nullable|string|max:255',
'phone_number' => 'nullable|string|max:15',
'specialization' => 'nullable|string|max:100',
'license_number' => 'nullable|string|max:50',
'join_date' => 'required|date',
]);
DB::transaction(function () use ($validated) {
// Create User
$user = User::create([
'id' => Str::uuid(),
'name' => $validated['name'],
'email' => $validated['email'],
'password' => Hash::make($validated['password']),
]);
// Create Employee
Employee::create([
'id' => Str::uuid(),
'user_id' => $user->id,
'gender' => $validated['gender'],
'nik' => $validated['nik'] ?? null,
'birth_date' => $validated['birth_date'],
'birth_place' => $validated['birth_place'] ?? null,
'address' => $validated['address'] ?? null,
'phone_number' => $validated['phone_number'] ?? null,
'specialization' => $validated['specialization'] ?? null,
'license_number' => $validated['license_number'] ?? null,
'join_date' => $validated['join_date'],
'resign_date' => $validated['resign_date'] ?? null,
'is_active' => true,
]);
});
return redirect()->route('employees.index')
->with('status', 'Berhasil membuat data pegawai');
}
/**
* Display the specified resource.
*/
public function show(Employee $employee)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Employee $employee)
{
return Inertia::render('employees/form', [
'mode' => 'edit',
'employee' => [
'id' => $employee->id,
'employee_id' => $employee->employee_id,
'name' => $employee->user->name,
'email' => $employee->user->email,
'gender' => $employee->gender,
'nik' => $employee->nik,
'birth_date' => $employee->birth_date,
'birth_place' => $employee->birth_place,
'specialization' => $employee->specialization,
'license_number' => $employee->license_number,
'address' => $employee->address,
'resign_date' => $employee->resign_date,
'phone_number' => $employee->phone_number,
'join_date' => $employee->join_date->format('Y-m-d'),
'is_active' => $employee->is_active,
],
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Employee $employee)
{
$validated = $request->validate([
// Validasi untuk User
'name' => 'required|string|max:255',
'email' => [
'required',
'email',
Rule::unique('users')->ignore($employee->user_id)
],
'password' => 'nullable|string|min:8', // Diubah menjadi nullable untuk update
// Validasi untuk Employee
'employee_id' => 'required|string|max:50',
'gender' => 'required|in:laki-laki,perempuan',
'nik' => 'nullable|string|max:16',
'birth_date' => 'required|date',
'birth_place' => 'nullable|string|max:100',
'address' => 'nullable|string|max:255',
'phone_number' => 'nullable|string|max:15',
'specialization' => 'nullable|string|max:100',
'license_number' => 'nullable|string|max:50',
'join_date' => 'required|date',
'resign_date' => 'nullable|date',
'is_active' => 'required|boolean',
]);
DB::transaction(function () use ($validated, $employee) {
// Update data user
$userData = [
'name' => $validated['name'],
'email' => $validated['email'],
];
// Hanya update password jika diisi
if (!empty($validated['password'])) {
$userData['password'] = Hash::make($validated['password']);
}
$employee->user->update($userData);
// Update data employee
$employee->update([
'employee_id' => $validated['employee_id'],
'gender' => $validated['gender'],
'nik' => $validated['nik'],
'birth_date' => $validated['birth_date'],
'birth_place' => $validated['birth_place'],
'address' => $validated['address'],
'phone_number' => $validated['phone_number'],
'specialization' => $validated['specialization'],
'license_number' => $validated['license_number'],
'join_date' => $validated['join_date'],
'resign_date' => $validated['resign_date'],
'is_active' => $validated['is_active'],
]);
});
return redirect()->route('employees.index')
->with('success', 'Data pegawai telah diperbaharui');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Employee $employee)
{
DB::transaction(function () use ($employee) {
$employee->user()->delete();
$employee->delete();
});
return redirect()->route('employees.index')
->with('status', 'Data pegawai berhasil dihapus');
}
}