dstpabuaran.com/app/Services/Admin/HR/EmployeeService.php
Yoga Pangestu da30f12fc2 feat: add employee management features including create, edit, and list functionalities
- 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.
2026-07-29 22:33:55 +07:00

105 lines
3.1 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(): Collection
{
return User::whereHas('employee')
->with(['userProfile', 'employee'])
->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', 'Minimal8@');
$user->update(['password' => $defaultPassword]);
return $user;
}
}