Files
sam-manage/app/Services/HR/EmployeeService.php
김보곤 8d0dee2bb2 feat: [hr] 직급/직책 인라인 추가 기능 구현
- Position 생성 API 엔드포인트 추가 (POST /admin/hr/positions)
- 직급/직책 select 옆 "+" 버튼으로 모달 열기
- 모달에서 이름 입력 → API 저장 → 드롭다운에 자동 추가 및 선택
- 중복 key 방지 (기존 값이면 그대로 반환)
- create/edit 뷰 모두 적용
2026-02-26 17:07:12 +09:00

287 lines
9.3 KiB
PHP

<?php
namespace App\Services\HR;
use App\Models\HR\Employee;
use App\Models\HR\Position;
use App\Models\Tenants\Department;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
class EmployeeService
{
/**
* 사원 목록 조회 (페이지네이션)
*/
public function getEmployees(array $filters = [], int $perPage = 20): LengthAwarePaginator
{
$tenantId = session('selected_tenant_id');
$query = Employee::query()
->with(['user', 'department'])
->forTenant($tenantId);
// 검색 필터 (이름, 사번, 이메일)
if (! empty($filters['q'])) {
$search = $filters['q'];
$query->where(function ($q) use ($search) {
$q->where('display_name', 'like', "%{$search}%")
->orWhereHas('user', function ($uq) use ($search) {
$uq->where('name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%")
->orWhere('phone', 'like', "%{$search}%");
})
->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(json_extra, '$.employee_code')) LIKE ?", ["%{$search}%"]);
});
}
// 상태 필터
if (! empty($filters['status'])) {
$query->where('employee_status', $filters['status']);
}
// 부서 필터
if (! empty($filters['department_id'])) {
$query->where('department_id', $filters['department_id']);
}
// 정렬
$query->orderByRaw("FIELD(employee_status, 'active', 'leave', 'resigned')")
->orderBy('created_at', 'desc');
return $query->paginate($perPage);
}
/**
* 사원 상세 조회
*/
public function getEmployeeById(int $id): ?Employee
{
$tenantId = session('selected_tenant_id');
return Employee::query()
->with(['user', 'department', 'manager'])
->forTenant($tenantId)
->find($id);
}
/**
* 사원 통계
*/
public function getStats(): array
{
$tenantId = session('selected_tenant_id');
$baseQuery = Employee::query()->forTenant($tenantId);
return [
'total' => (clone $baseQuery)->count(),
'active' => (clone $baseQuery)->where('employee_status', 'active')->count(),
'leave' => (clone $baseQuery)->where('employee_status', 'leave')->count(),
'resigned' => (clone $baseQuery)->where('employee_status', 'resigned')->count(),
];
}
/**
* 사원 등록 (User + TenantUserProfile 동시 생성)
*/
public function createEmployee(array $data): Employee
{
$tenantId = session('selected_tenant_id');
return DB::transaction(function () use ($data, $tenantId) {
// User 생성
$user = User::create([
'user_id' => $data['email'] ?? $data['name'],
'name' => $data['name'],
'email' => $data['email'] ?? null,
'phone' => $data['phone'] ?? null,
'password' => bcrypt($data['password'] ?? 'sam1234!'),
'is_active' => true,
'must_change_password' => true,
'created_by' => auth()->id(),
]);
// user_tenants pivot 연동 (멀티테넌트 필수)
if ($tenantId) {
$user->tenants()->attach($tenantId, [
'is_active' => true,
'is_default' => true,
'joined_at' => now(),
]);
}
// json_extra 구성
$jsonExtra = [];
if (! empty($data['employee_code'])) {
$jsonExtra['employee_code'] = $data['employee_code'];
}
if (! empty($data['hire_date'])) {
$jsonExtra['hire_date'] = $data['hire_date'];
}
if (! empty($data['address'])) {
$jsonExtra['address'] = $data['address'];
}
if (! empty($data['emergency_contact'])) {
$jsonExtra['emergency_contact'] = $data['emergency_contact'];
}
// Employee(TenantUserProfile) 생성
$employee = Employee::create([
'tenant_id' => $tenantId,
'user_id' => $user->id,
'department_id' => $data['department_id'] ?? null,
'position_key' => $data['position_key'] ?? null,
'job_title_key' => $data['job_title_key'] ?? null,
'work_location_key' => $data['work_location_key'] ?? null,
'employment_type_key' => $data['employment_type_key'] ?? null,
'employee_status' => $data['employee_status'] ?? 'active',
'manager_user_id' => $data['manager_user_id'] ?? null,
'display_name' => $data['display_name'] ?? $data['name'],
'json_extra' => ! empty($jsonExtra) ? $jsonExtra : null,
]);
return $employee->load(['user', 'department']);
});
}
/**
* 사원 정보 수정
*/
public function updateEmployee(int $id, array $data): ?Employee
{
$employee = $this->getEmployeeById($id);
if (! $employee) {
return null;
}
// 기본 필드 업데이트
$updateData = array_filter([
'department_id' => $data['department_id'] ?? null,
'position_key' => $data['position_key'] ?? null,
'job_title_key' => $data['job_title_key'] ?? null,
'work_location_key' => $data['work_location_key'] ?? null,
'employment_type_key' => $data['employment_type_key'] ?? null,
'employee_status' => $data['employee_status'] ?? null,
'manager_user_id' => $data['manager_user_id'] ?? null,
'display_name' => $data['display_name'] ?? null,
], fn ($v) => $v !== null);
// json_extra 업데이트
$jsonExtraKeys = ['employee_code', 'hire_date', 'address', 'emergency_contact', 'salary', 'bank_account'];
$extra = $employee->json_extra ?? [];
foreach ($jsonExtraKeys as $key) {
if (array_key_exists($key, $data)) {
if ($data[$key] === null || $data[$key] === '') {
unset($extra[$key]);
} else {
$extra[$key] = $data[$key];
}
}
}
$updateData['json_extra'] = ! empty($extra) ? $extra : null;
$employee->update($updateData);
// User 기본정보 동기화
if ($employee->user) {
$userUpdate = [];
if (! empty($data['name'])) {
$userUpdate['name'] = $data['name'];
}
if (! empty($data['email'])) {
$userUpdate['email'] = $data['email'];
}
if (! empty($data['phone'])) {
$userUpdate['phone'] = $data['phone'];
}
if (! empty($userUpdate)) {
$employee->user->update($userUpdate);
}
}
return $employee->fresh(['user', 'department']);
}
/**
* 사원 삭제 (퇴직 처리)
*/
public function deleteEmployee(int $id): bool
{
$employee = $this->getEmployeeById($id);
if (! $employee) {
return false;
}
$employee->update(['employee_status' => 'resigned']);
return true;
}
/**
* 부서 목록 (드롭다운용)
*/
public function getDepartments(): \Illuminate\Database\Eloquent\Collection
{
$tenantId = session('selected_tenant_id');
return Department::query()
->where('is_active', true)
->when($tenantId, fn ($q) => $q->where('tenant_id', $tenantId))
->orderBy('sort_order')
->orderBy('name')
->get(['id', 'name', 'code']);
}
/**
* 직급/직책 목록 (드롭다운용)
*/
public function getPositions(string $type = 'rank'): \Illuminate\Database\Eloquent\Collection
{
return Position::query()
->forTenant()
->where('type', $type)
->where('is_active', true)
->ordered()
->get(['id', 'key', 'name']);
}
/**
* 직급/직책 추가
*/
public function createPosition(string $type, string $name): Position
{
$tenantId = session('selected_tenant_id');
// key 생성: 이름을 소문자+언더스코어로 변환, 한글은 그대로
$key = str_replace(' ', '_', mb_strtolower(trim($name)));
// 중복 체크 후 존재하면 기존 반환
$existing = Position::query()
->forTenant()
->where('type', $type)
->where('key', $key)
->first();
if ($existing) {
return $existing;
}
// 다음 sort_order
$maxSort = Position::query()
->forTenant()
->where('type', $type)
->max('sort_order') ?? 0;
return Position::create([
'tenant_id' => $tenantId,
'type' => $type,
'key' => $key,
'name' => trim($name),
'sort_order' => $maxSort + 1,
'is_active' => true,
]);
}
}