Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,82 @@
<?php
session_start();
require_once '../includes/db_hr.php'; // โหลดการเชื่อมต่อ DB ตัวที่ 2
require_once '../includes/api_helper.php';
if (!isset($_SESSION['user_id'])) {
jsonResponse(false, 'Unauthorized', [], 401);
}
if (!$pdo_hr) {
jsonResponse(false, 'ไม่สามารถเชื่อมต่อฐานข้อมูล hosoffice_2566 ได้ กรุณาตั้งค่าใน includes/db_hr.php');
}
$keyword = $_GET['q'] ?? '';
if (empty($keyword) || mb_strlen($keyword, 'UTF-8') < 2) {
jsonResponse(true, 'Success', []); // คืนค่าว่างถ้าคำค้นหาสั้นไป
}
try {
$sql = "SELECT a.HR_CID, a.HR_FNAME, a.HR_LNAME, a.HR_STARTWORK_DATE, a.HR_PHONE, a.SEX,
(a.HR_IMAGE IS NOT NULL AND LENGTH(a.HR_IMAGE) > 0) as HAS_IMAGE,
e.HR_PREFIX_NAME, b.HR_DEPARTMENT_SUB_SUB_NAME,
c.HR_LEVEL_NAME, d.HR_PERSON_TYPE_NAME, f.HR_POSITION_NAME
FROM hr_person a
LEFT OUTER JOIN hr_department_sub_sub b on a.HR_DEPARTMENT_SUB_SUB_ID = b.HR_DEPARTMENT_SUB_SUB_ID
LEFT OUTER JOIN hr_level c ON a.HR_LEVEL_ID = c.HR_LEVEL_ID
LEFT OUTER JOIN hr_person_type d ON a.HR_PERSON_TYPE_ID = d.HR_PERSON_TYPE_ID
LEFT OUTER JOIN hr_prefix e ON a.HR_PREFIX_ID=e.HR_PREFIX_ID
LEFT OUTER JOIN hr_position f ON a.HR_POSITION_ID=f.HR_POSITION_ID
WHERE (a.HR_FNAME LIKE ? OR a.HR_LNAME LIKE ? OR a.HR_CID LIKE ?) AND a.HR_STATUS_ID='01'
LIMIT 15";
$stmt = $pdo_hr->prepare($sql);
$searchTerm = "%$keyword%";
$stmt->execute([$searchTerm, $searchTerm, $searchTerm]);
$raw_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$results = [];
foreach ($raw_results as $person) {
$prefix = $person['HR_PREFIX_NAME'] ?? '';
$fname = $person['HR_FNAME'] ?? '';
$lname = $person['HR_LNAME'] ?? '';
$fullName = trim($prefix . $fname . ' ' . $lname);
$department = $person['HR_DEPARTMENT_SUB_SUB_NAME'] ?? 'ไม่ระบุ';
$position = trim(($person['HR_POSITION_NAME'] ?? '') . ' ' . ($person['HR_LEVEL_NAME'] ?? ''));
$person_type = $person['HR_PERSON_TYPE_NAME'] ?? 'ไม่ระบุ';
$work_duration = 'ไม่ระบุวันที่เริ่มงาน';
if (!empty($person['HR_STARTWORK_DATE'])) {
try {
$startDate = new DateTime($person['HR_STARTWORK_DATE']);
$currentDate = new DateTime();
if ($startDate <= $currentDate) {
$interval = $currentDate->diff($startDate);
$work_duration = $interval->format('%y ปี %m เดือน %d วัน');
}
} catch (Exception $e) { }
}
$results[] = [
'prefix' => $prefix,
'fname' => $fname,
'lname' => $lname,
'full_name' => $fullName,
'department' => $department,
'position' => $position,
'person_type' => $person_type,
'work_duration' => $work_duration,
'phone' => $person['HR_PHONE'] ?? '',
'sex' => $person['SEX'] ?? '',
'has_image' => (bool)$person['HAS_IMAGE'],
'cid' => $person['HR_CID']
];
}
jsonResponse(true, 'Success', $results);
} catch (PDOException $e) {
jsonResponse(false, 'Database Error: ' . $e->getMessage());
}
?>