90 lines
3.5 KiB
PHP
90 lines
3.5 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use App\Helpers\Response;
|
|
use App\Helpers\Validator;
|
|
use App\Middleware\JwtAuthMiddleware;
|
|
use App\Models\Patient;
|
|
|
|
/**
|
|
* Class PatientController
|
|
* Patient EMR & Registration REST API Controller
|
|
*
|
|
* @package App\Controllers
|
|
*/
|
|
class PatientController
|
|
{
|
|
private Patient $patientModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->patientModel = new Patient();
|
|
}
|
|
|
|
/**
|
|
* GET /api/v1/patients/search?q={keyword}
|
|
*/
|
|
public function search(): void
|
|
{
|
|
JwtAuthMiddleware::handle();
|
|
$keyword = trim($_GET['q'] ?? '');
|
|
if (empty($keyword)) {
|
|
Response::success('รายชื่อผู้รับบริการทั้งหมด (ล่าสุด)', $this->patientModel->all(50));
|
|
} else {
|
|
Response::success("ผลการค้นหาผู้รับบริการสำหรับ '{$keyword}'", $this->patientModel->search($keyword));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST /api/v1/patients - Create New Patient
|
|
*/
|
|
public function store(): void
|
|
{
|
|
JwtAuthMiddleware::handle();
|
|
$input = json_decode(file_get_contents('php://input'), true) ?? $_POST;
|
|
|
|
$validator = Validator::make($input, [
|
|
'cid' => 'required|cid_13',
|
|
'first_name_th' => 'required|min:2',
|
|
'last_name_th' => 'required|min:2',
|
|
'birth_date' => 'required|date',
|
|
'phone' => 'required|min:9',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
Response::error('ข้อมูลลงทะเบียนผู้ป่วยไม่ถูกต้อง', 422, $validator->getErrors());
|
|
}
|
|
|
|
$existing = $this->patientModel->findByCid(trim($input['cid']));
|
|
if ($existing) {
|
|
Response::error('เลขบัตรประชาชนนี้มีอยู่ในระบบแล้ว (HN: ' . $existing['hn'] . ')', 409);
|
|
}
|
|
|
|
$hn = 'HN-' . date('ym') . rand(1000, 9999);
|
|
$patientId = $this->patientModel->create([
|
|
'cid' => trim($input['cid']),
|
|
'hn' => $hn,
|
|
'first_name_th' => trim($input['first_name_th']),
|
|
'last_name_th' => trim($input['last_name_th']),
|
|
'first_name_en' => $input['first_name_en'] ?? null,
|
|
'last_name_en' => $input['last_name_en'] ?? null,
|
|
'birth_date' => $input['birth_date'],
|
|
'gender' => $input['gender'] ?? 'Male',
|
|
'blood_group' => $input['blood_group'] ?? null,
|
|
'address' => $input['address'] ?? null,
|
|
'phone' => trim($input['phone']),
|
|
'line_id' => $input['line_id'] ?? null,
|
|
'email' => $input['email'] ?? null,
|
|
'underlying_diseases' => $input['underlying_diseases'] ?? 'ไม่มี',
|
|
'massage_contraindications' => $input['massage_contraindications'] ?? 'ไม่มี',
|
|
'drug_allergies' => $input['drug_allergies'] ?? 'ไม่มี',
|
|
'emergency_contact_name' => $input['emergency_contact_name'] ?? null,
|
|
'emergency_contact_phone' => $input['emergency_contact_phone'] ?? null,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
]);
|
|
|
|
Response::success('ลงทะเบียนผู้รับบริการใหม่เรียบร้อยแล้ว', $this->patientModel->find($patientId), 201);
|
|
}
|
|
}
|