407 lines
13 KiB
PHP
407 lines
13 KiB
PHP
<?php
|
|
class Patient extends MY_Controller
|
|
{
|
|
|
|
var $db_onedev;
|
|
public function index()
|
|
{
|
|
echo "Resultentry API";
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_onedev = $this->load->database("onedev", true);
|
|
$this->db_log = $this->load->database("log", true);
|
|
$this->load->helper(array('form', 'url'));
|
|
}
|
|
|
|
public function error_log($data, $userid = 999)
|
|
{
|
|
|
|
$sql = "INSERT INTO ais_error_log (
|
|
AisErrorLogFnName,
|
|
AisErrorLogMessage,
|
|
AisErrorLogQuery,
|
|
AisErrorLogJson,
|
|
AisErrorLogUserID,
|
|
AisErrorLogCreated
|
|
)
|
|
VALUES(
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
NOW()
|
|
)";
|
|
$qry = $this->db_log->query($sql, array($data['fn_name'], $data['message'], $data['query'], $data['json'], $userid));
|
|
//echo $this->db_log->last_query();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Generic POST request function
|
|
* @param string $url API endpoint URL
|
|
* @param array $data Request payload
|
|
* @param array $headers Custom headers (optional)
|
|
* @return array Response from API
|
|
*/
|
|
public function post_request($url, $data = array(), $headers = array())
|
|
{
|
|
// Default headers
|
|
$default_headers = array(
|
|
'Content-Type: application/json'
|
|
);
|
|
|
|
// Merge custom headers with default headers
|
|
$final_headers = array_merge($default_headers, $headers);
|
|
|
|
// Initialize cURL
|
|
$ch = curl_init();
|
|
|
|
// Set cURL options
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $final_headers);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
// Execute cURL request
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
|
|
// Close cURL
|
|
curl_close($ch);
|
|
|
|
// Prepare result
|
|
$result = array(
|
|
'success' => false,
|
|
'http_code' => $http_code,
|
|
'response' => null,
|
|
'error' => null
|
|
);
|
|
|
|
if ($error) {
|
|
$result['error'] = $error;
|
|
} else {
|
|
$result = json_decode($response, true);
|
|
if ($result['status'] == 200) {
|
|
$result['success'] = true;
|
|
} else {
|
|
$result['success'] = false;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function get_request($url, $headers = array())
|
|
{
|
|
// Retrieve configuration
|
|
$config = $this->get_config();
|
|
$token = $config['AisConfigAuthToken'] ?? 'default-token';
|
|
$header_token = $config['AisConfigHeaderToken'];
|
|
|
|
// Default headers based on the curl command
|
|
$default_headers = array(
|
|
'Header-Token: ' . $header_token,
|
|
'Authorization: Bearer ' . $token
|
|
);
|
|
|
|
// Merge custom headers with default headers
|
|
$final_headers = array_merge($default_headers, $headers);
|
|
|
|
// Initialize cURL
|
|
$ch = curl_init();
|
|
|
|
// Set cURL options
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_HTTPGET, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $final_headers);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
// Execute cURL request
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
|
|
// Close cURL
|
|
curl_close($ch);
|
|
|
|
// Prepare result
|
|
$result = array(
|
|
'success' => false,
|
|
'http_code' => $http_code,
|
|
'response' => null,
|
|
'error' => null
|
|
);
|
|
|
|
if ($error) {
|
|
$result['error'] = $error;
|
|
} else {
|
|
$decoded = json_decode($response, true);
|
|
$result['response'] = $decoded;
|
|
// Check if response is successful based on http code
|
|
if ($http_code === 200) {
|
|
$result['success'] = true;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
function get_config()
|
|
{
|
|
$sql = "SELECT * FROM ais_config LIMIT 1";
|
|
$qry = $this->db_onedev->query($sql);
|
|
if (!$qry) {
|
|
$this->error_log(array('fn_name' => 'get_config', 'message' => 'ais_config select', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
|
|
exit;
|
|
}
|
|
$dt_config = $qry->row_array();
|
|
return $dt_config;
|
|
}
|
|
|
|
/**
|
|
* POST request to auth API
|
|
* @param string $username Username for authentication
|
|
* @param string $password Password for authentication
|
|
* @return array Response from API
|
|
*/
|
|
public function post_auth()
|
|
{
|
|
$dt_config = $this->get_config();
|
|
$baseUrl = $dt_config['AisConfigBaseUrl'];
|
|
$url = $baseUrl . '/api/auth/auth.php';
|
|
$headers = array(
|
|
'Header-Token: ' . $dt_config['AisConfigHeaderToken']
|
|
);
|
|
|
|
$username = $dt_config['AisConfigUsername'];
|
|
$password = $dt_config['AisConfigPassword'];
|
|
|
|
$data = array(
|
|
'username' => $username,
|
|
'password' => $password
|
|
);
|
|
|
|
$result = $this->post_request($url, $data, $headers);
|
|
$sql = "INSERT INTO ais_login_log(
|
|
AisLoginLogUsername,
|
|
AisLoginLogPassword,
|
|
AisLoginLogHeaderToken,
|
|
AisLoginLogResult,
|
|
AisLoginLogCreated
|
|
)
|
|
VALUES(
|
|
?,
|
|
?,
|
|
?,
|
|
?,
|
|
NOW()
|
|
)";
|
|
$qry = $this->db_log->query($sql, array($username, $password, $dt_config['AisConfigHeaderToken'], json_encode($result)));
|
|
|
|
|
|
if (!$qry) {
|
|
$this->error_log(array('fn_name' => 'post_auth', 'message' => 'ais_login_log insert', 'query' => $this->db_log->last_query(), 'json' => json_encode($result)), 999);
|
|
exit;
|
|
}
|
|
//print_r($result);
|
|
//exit;
|
|
// Check if success
|
|
if (!$result['success']) {
|
|
$this->error_log(array('fn_name' => 'post_auth', 'message' => 'failed auth', 'query' => '', 'json' => json_encode($result)), 999);
|
|
$errors = array('status' => 'error', 'message' => 'Gagal Login');
|
|
echo json_encode($errors);
|
|
exit;
|
|
} else {
|
|
// Update token
|
|
$token = $result['data']['token'];
|
|
;
|
|
$sql = "UPDATE ais_config SET AisConfigAuthToken = ? WHERE AisConfigID = 1";
|
|
$qry = $this->db_onedev->query($sql, array($token));
|
|
//echo $this->db_onedev->last_query();
|
|
//exit;
|
|
if (!$qry) {
|
|
$this->error_log(array('fn_name' => 'post_auth', 'message' => 'ais_config update', 'query' => $this->db_onedev->last_query(), 'json' => ''), 999);
|
|
$errors = array('status' => 'error', 'message' => 'Gagal Update Token');
|
|
echo json_encode($errors);
|
|
exit;
|
|
}
|
|
|
|
//$success = array('status' => 'success', 'message' => 'Berhasil Login', 'token' => $token);
|
|
|
|
return $token;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST request to jenis layanan API
|
|
* @param array $data Array of jenis layanan data
|
|
* @return array Response from API
|
|
*/
|
|
|
|
function monitoring_patient()
|
|
{
|
|
$sql_base = "SELECT
|
|
oh.T_OrderHeaderID,
|
|
oh.T_OrderHeaderM_PatientID AS patient_id,
|
|
p.M_PatientName AS patient_name,
|
|
p.M_PatientNoReg AS patient_noreq,
|
|
p.M_PatientGender AS patient_gender,
|
|
p.M_PatientDOB AS patient_birthdate,
|
|
p.M_PatientHp AS patient_hp,
|
|
p.M_PatientEmail AS patient_email,
|
|
p.M_PatientIdentifierValue AS patient_identifier,
|
|
p.M_PatientNIP AS patient_nip,
|
|
p.M_PatientJob AS patient_job,
|
|
p.M_PatientPosisi AS patient_posisi,
|
|
p.M_PatientDivisi AS pasient_divisi,
|
|
am.Ais_MedrecStatus AS medrec_status,
|
|
|
|
-- STATUS AIS
|
|
IF(MAX(am.Ais_MedrecID) IS NULL, 'N', 'Y') AS medrec_status
|
|
|
|
FROM t_orderheader oh
|
|
JOIN m_patient p
|
|
ON p.M_PatientID = oh.T_OrderHeaderM_PatientID
|
|
LEFT JOIN ".$this->db_log->database.".ais_medrec am
|
|
ON am.Ais_MedrecPatientNoReg = p.M_PatientNoReg
|
|
";
|
|
|
|
// Pagination
|
|
$page = $this->input->get('page') ? intval($this->input->get('page')) : 1;
|
|
$limit = 10;
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
// Filter
|
|
$patient_name = $this->input->get('patient_name');
|
|
$patient_noreq = $this->input->get('patient_noreq');
|
|
$medrec_status = $this->input->get('medrec_status');
|
|
|
|
$params = [];
|
|
$where_clauses = [];
|
|
|
|
if ($medrec_status === 'Y') {
|
|
$where_clauses[] = "am.Ais_MedrecID IS NOT NULL";
|
|
} elseif ($medrec_status === 'N') {
|
|
$where_clauses[] = "am.Ais_MedrecID IS NULL";
|
|
}
|
|
|
|
if ($patient_name) {
|
|
$where_clauses[] = "p.M_PatientName LIKE ?";
|
|
$params[] = "%$patient_name%";
|
|
}
|
|
|
|
if ($patient_noreq) {
|
|
$where_clauses[] = "p.M_PatientNoReg LIKE ?";
|
|
$params[] = "%$patient_noreq%";
|
|
}
|
|
|
|
// WHERE builder
|
|
$where_sql = "";
|
|
if (!empty($where_clauses)) {
|
|
$where_sql = " WHERE " . implode(" AND ", $where_clauses);
|
|
}
|
|
|
|
// Count total rows
|
|
$count_sql = "SELECT COUNT(DISTINCT oh.T_OrderHeaderM_PatientID) AS total
|
|
FROM t_orderheader oh
|
|
JOIN m_patient p
|
|
ON p.M_PatientID = oh.T_OrderHeaderM_PatientID
|
|
LEFT JOIN ".$this->db_log->database.".ais_medrec am
|
|
ON am.Ais_MedrecPatientNoReg = p.M_PatientNoReg
|
|
$where_sql";
|
|
|
|
$qry_count = $this->db_onedev->query($count_sql, $params);
|
|
$total = $qry_count->row()->total ?? 0;
|
|
|
|
// Main query
|
|
$sql = $sql_base . $where_sql . "
|
|
GROUP BY oh.T_OrderHeaderM_PatientID
|
|
LIMIT $limit OFFSET $offset";
|
|
|
|
$qry = $this->db_onedev->query($sql, $params);
|
|
|
|
if (!$qry) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Gagal mengambil data pasien.']);
|
|
exit;
|
|
}
|
|
|
|
$data = $qry->result_array();
|
|
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'message' => 'Berhasil mengambil data pasien.',
|
|
'data' => $data,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'limit' => $limit,
|
|
'total_page' => ceil($total / $limit)
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
|
|
function get_json_response()
|
|
{
|
|
// Ambil patient_noreg dari query parameter
|
|
$patient_noreg = $this->input->get('patient_noreg');
|
|
|
|
if (empty($patient_noreg)) {
|
|
$this->output
|
|
->set_status_header(400)
|
|
->set_content_type('application/json')
|
|
->set_output(json_encode([
|
|
'status' => 'error',
|
|
'message' => 'patient_noreg parameter is required'
|
|
]));
|
|
return;
|
|
}
|
|
|
|
$sql = "SELECT
|
|
Ais_MedrecPatientNoReg AS patient_noreg,
|
|
Ais_MedrecStatus AS status,
|
|
Ais_MedrecJSON AS json,
|
|
Ais_MedrecResponse AS response,
|
|
Ais_MedrecCreated AS created,
|
|
Ais_MedrecUserID AS user_id
|
|
FROM ais_medrec
|
|
WHERE Ais_MedrecPatientNoReg = ?";
|
|
|
|
// Query menggunakan db_log
|
|
$qry = $this->db_log->query($sql, array($patient_noreg));
|
|
|
|
// Ambil multiple rows
|
|
$results = $qry->result_array();
|
|
|
|
if ($results) {
|
|
foreach ($results as &$result) {
|
|
$result['json'] = json_decode($result['json']);
|
|
$result['response'] = json_decode($result['response']);
|
|
}
|
|
unset($result);
|
|
|
|
echo json_encode(['status' => 'success', 'data' => $results]);
|
|
} else {
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'No data found for the given patient_noreq.'
|
|
]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
|
|
}
|