121 lines
4.1 KiB
PHP
121 lines
4.1 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use App\Models\Patient;
|
|
|
|
/**
|
|
* Class HisConnector
|
|
* Hospital Information System (HIS / HOSxP) REST API Connector
|
|
*
|
|
* @package App\Services
|
|
*/
|
|
class HisConnector
|
|
{
|
|
private string $baseUrl;
|
|
private string $apiKey;
|
|
private int $timeout;
|
|
private bool $enabled;
|
|
|
|
public function __construct()
|
|
{
|
|
$config = require __DIR__ . '/../../config/integration.php';
|
|
$his = $config['his'];
|
|
$this->baseUrl = rtrim($his['api_base_url'], '/');
|
|
$this->apiKey = $his['api_key'];
|
|
$this->timeout = $his['timeout_seconds'] ?? 5;
|
|
$this->enabled = $his['enabled'] ?? true;
|
|
}
|
|
|
|
/**
|
|
* Fetch Patient Demographics from HIS by HN or CID
|
|
*/
|
|
public function getPatientFromHis(string $query, string $type = 'hn'): ?array
|
|
{
|
|
if (!$this->enabled) return null;
|
|
|
|
$endpoint = "{$this->baseUrl}/patients?" . ($type === 'hn' ? "hn={$query}" : "cid={$query}");
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $endpoint,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => $this->timeout,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Authorization: Bearer {$this->apiKey}",
|
|
"Accept: application/json",
|
|
],
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode === 200 && $response) {
|
|
$data = json_decode($response, true);
|
|
return $data['data'] ?? $data ?? null;
|
|
}
|
|
|
|
// หากเชื่อมต่อ HIS ไม่ได้ในสภาพแวดล้อม Demo ให้สร้าง Fallback Mock Data ส่งกลับทันที
|
|
return $this->getMockHisData($query, $type);
|
|
}
|
|
|
|
/**
|
|
* Send completed massage treatment result back to HIS EMR
|
|
*/
|
|
public function sendTreatmentResult(int $patientId, array $soapData): bool
|
|
{
|
|
if (!$this->enabled) return true;
|
|
|
|
$patientModel = new Patient();
|
|
$patient = $patientModel->find($patientId);
|
|
if (!$patient) return false;
|
|
|
|
$payload = [
|
|
'hn' => $patient['hn'],
|
|
'cid' => $patient['cid'],
|
|
'treatment_date' => date('Y-m-d'),
|
|
'dept_code' => 'THAI_MED',
|
|
'chief_complaint' => $soapData['subjective_symptoms'] ?? '',
|
|
'physical_exam' => $soapData['objective_signs'] ?? '',
|
|
'pre_pain_score' => $soapData['pre_pain_score'] ?? null,
|
|
'post_pain_score' => $soapData['post_pain_score'] ?? null,
|
|
'diag_text' => $soapData['assessment_diag'] ?? 'Thai traditional massage therapy',
|
|
];
|
|
|
|
$ch = curl_init("{$this->baseUrl}/encounters");
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($payload),
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => $this->timeout,
|
|
CURLOPT_HTTPHEADER => [
|
|
"Authorization: Bearer {$this->apiKey}",
|
|
"Content-Type: application/json",
|
|
],
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
]);
|
|
|
|
curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
return $httpCode >= 200 && $httpCode < 300;
|
|
}
|
|
|
|
private function getMockHisData(string $query, string $type): array
|
|
{
|
|
return [
|
|
'hn' => $type === 'hn' ? $query : ('HN-' . rand(100000, 999999)),
|
|
'cid' => $type === 'cid' ? $query : '3100000000000',
|
|
'first_name_th' => 'ข้อมูลผู้ป่วยจาก',
|
|
'last_name_th' => 'ระบบ HIS (Mock)',
|
|
'birth_date' => '1980-01-01',
|
|
'gender' => 'Male',
|
|
'underlying_diseases' => 'ความดันโลหิตสูง (จาก HIS HOSxP)',
|
|
'drug_allergies' => 'ไม่มีประวัติแพ้ยา',
|
|
'insurance_type' => 'สิทธิบัตรทอง / สปสช.',
|
|
];
|
|
}
|
|
}
|