diff --git a/api.php b/api.php index c2063f2..5aa19cd 100644 --- a/api.php +++ b/api.php @@ -891,6 +891,73 @@ LIMIT 1")->fetchColumn(); echo json_encode(['success' => true]); break; + case 'export_users_csv': + $data = json_decode(file_get_contents('php://input'), true); + $userIds = $data['user_ids'] ?? []; + $columns = $data['columns'] ?? []; // ['name' => '이름', 'emp_id' => '사번', ...] + + if (empty($columns)) { + echo json_encode(['success' => false, 'error' => '내보낼 컬럼을 선택해주세요.']); + break; + } + + // 전체 유저 정보 조회 (기본 쿼리 재사용) + $query = "WITH RECURSIVE dept_path(id, path) AS ( + SELECT id, name FROM departments WHERE parent_id IS NULL + UNION ALL + SELECT d.id, dp.path || ' > ' || d.name + FROM departments d JOIN dept_path dp ON d.parent_id = dp.id + ) + SELECT u.*, dp.path as dept_name, + (SELECT asset_tag FROM laptop_assets WHERE current_user_id = u.id ORDER BY id DESC LIMIT 1) as laptop_tag + FROM users u + LEFT JOIN dept_path dp ON u.department_id = dp.id"; + + if (!empty($userIds)) { + $placeholders = implode(',', array_fill(0, count($userIds), '?')); + $query .= " WHERE u.id IN ($placeholders)"; + $stmt = $db->prepare($query); + $stmt->execute($userIds); + } else { + $stmt = $db->query($query); + } + + $users = $stmt->fetchAll(); + + // CSV 스트림 생성 + header('Content-Type: text/csv; charset=utf-8'); + header('Content-Disposition: attachment; filename=직원_마스터_리스트_' . date('Ymd_His') . '.csv'); + + $output = fopen('php://output', 'w'); + + // Zero-Mojibake: BOM (Byte Order Mark) 삽입 + fwrite($output, "\xEF\xBB\xBF"); + + // 헤더 작성 + fputcsv($output, array_values($columns)); + + // 데이터 작성 + foreach ($users as $user) { + $row = []; + foreach (array_keys($columns) as $key) { + $val = $user[$key] ?? ''; + // 상태 값 한글 변환 + if ($key === 'status') { + $val = match ($val) { + 'active' => '재직', + 'retired' => '퇴사', + 'on_leave' => '휴직', + 'classification' => '분류', + default => $val + }; + } + $row[] = $val; + } + fputcsv($output, $row); + } + fclose($output); + exit; + case 'get_general_rentals': $query = "SELECT r.*, GROUP_CONCAT(i.item_name, ', ') as items FROM general_rentals r diff --git a/users.php b/users.php index 69b4aa9..fef8d4e 100644 --- a/users.php +++ b/users.php @@ -88,6 +88,14 @@ 불러오기 + @@ -152,13 +160,13 @@
- + + +
+
+ 내보낼 항목 선택 + +
+
+ +
+
+ +
+
+
+ + + +

+ 명의 선택된 직원 정보를 내보냅니다. + 전체 명의 직원 정보를 내보냅니다. +
UTF-8 BOM이 포함되어 엑셀에서 바로 열어도 한글이 깨지지 않습니다. +

+
+
+ +
+ + +