dstpabuaran.com/app/Services/Admin/HR/EmployeeService.php
Yoga Pangestu bc810c93d2 feat: enhance Employee management with filtering options and improved data handling
- Updated EmployeeController to accept request filters for employment status, activity status, and gender.
- Modified EmployeeService to support filtering in the getAll method.
- Enhanced Employee index page with a filter toolbar for better user experience.
- Fixed route parameter naming for employee-related routes.
- Added comprehensive tests for employee index functionality and filtering capabilities.
2026-07-30 11:26:11 +07:00

112 lines
3.8 KiB
PHP

<?php
namespace App\Services\Admin\HR;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
class EmployeeService
{
public function getAll(array $filters = []): Collection
{
return User::select('id', 'email', 'username', 'is_active')
->whereHas('employee')
->with([
'userProfile' => fn ($q) => $q->select('id', 'user_id', 'full_name', 'phone_number', 'gender'),
'employee' => fn ($q) => $q->select('id', 'user_id', 'join_date', 'employment_status', 'base_salary'),
])
->when($filters['employment_status'] ?? null, fn ($q, $status) => $q->whereHas('employee', fn ($eq) => $eq->where('employment_status', $status)))
->when(isset($filters['is_active']) && $filters['is_active'] !== '', fn ($q) => $q->where('is_active', filter_var($filters['is_active'], FILTER_VALIDATE_BOOLEAN)))
->when($filters['gender'] ?? null, fn ($q, $gender) => $q->whereHas('userProfile', fn ($uq) => $uq->where('gender', $gender)))
->latest()
->get();
}
public function getById(int $id): User
{
return User::with(['userProfile', 'employee'])->findOrFail($id);
}
public function create(array $data): User
{
return DB::transaction(function () use ($data) {
$user = User::create([
'email' => $data['email'],
'username' => $data['username'],
'password' => config('auth.password_default'),
'is_active' => true,
]);
$user->userProfile()->create([
'full_name' => $data['full_name'],
'phone_number' => $data['phone_number'] ?? null,
'gender' => $data['gender'] ?? null,
'birth_date' => $data['birth_date'] ?? null,
'address' => $data['address'] ?? null,
]);
$user->employee()->create([
'join_date' => $data['join_date'],
'resign_date' => $data['resign_date'] ?? null,
'employment_status' => $data['employment_status'],
'base_salary' => $data['base_salary'],
]);
return $user;
});
}
public function update(User $user, array $data): User
{
DB::transaction(function () use ($user, $data) {
$user->update([
'email' => $data['email'],
'username' => $data['username'],
]);
$user->userProfile()->updateOrCreate([], [
'full_name' => $data['full_name'],
'phone_number' => $data['phone_number'] ?? null,
'gender' => $data['gender'] ?? null,
'birth_date' => $data['birth_date'] ?? null,
'address' => $data['address'] ?? null,
]);
$user->employee()->updateOrCreate([], [
'join_date' => $data['join_date'],
'resign_date' => $data['resign_date'] ?? null,
'employment_status' => $data['employment_status'],
'base_salary' => $data['base_salary'],
]);
});
return $user->fresh(['userProfile', 'employee']);
}
public function delete(User $user): bool
{
return DB::transaction(function () use ($user) {
$user->employee()->delete();
$user->userProfile()->delete();
return $user->delete();
});
}
public function toggleActive(User $user): User
{
$user->update(['is_active' => ! $user->is_active]);
return $user;
}
public function resetPassword(User $user): User
{
$defaultPassword = config('auth.password_default');
$user->update(['password' => $defaultPassword]);
return $user;
}
}