290 lines
10 KiB
PHP
290 lines
10 KiB
PHP
<?php
|
|
|
|
class Pre_eresult extends MY_Controller
|
|
{
|
|
var $SECRET_KEY = "76c564c67666102630f06ccdd88acb78";
|
|
var $intervalHour;
|
|
var $tmp_body;
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->intervalHour = 12;
|
|
}
|
|
|
|
function riwayat()
|
|
{
|
|
$this->auth_token();
|
|
$prm = $this->x_param();
|
|
$xID = $prm["x-id"];
|
|
$code = $prm["code"];
|
|
$noreg = $prm["noreg"];
|
|
if ($xID == "" || $code == "" || $noreg == "") {
|
|
echo json_encode(["status"=>"ERR","message"=>"x-id, code , and noreg is mandatory"]);
|
|
exit;
|
|
}
|
|
$riwayat = $prm["riwayat"];
|
|
$j_riwayat = json_encode($riwayat);
|
|
$sql = "select * from pre_eresult
|
|
where pre_eresultMcu_PreregisterDetailsID=?";
|
|
$qry = $this->db->query($sql, [$xID]);
|
|
$rows = $qry->result_array();
|
|
$action = "";
|
|
if (count($rows) == 0) {
|
|
$sql = "insert into pre_eresult(pre_eresultMcu_PreregisterDetailsID,
|
|
pre_eresultMcuOfflinePrepareCode,pre_eresultM_PatientNoreg,
|
|
pre_eresultRiwayat)
|
|
values (?,?,?,?)";
|
|
$qry = $this->db->query($sql, [$xID, $code, $noreg, $j_riwayat]);
|
|
$action = "insert";
|
|
} else {
|
|
$sql = "update pre_eresult set pre_eresultRiwayat = ?,
|
|
pre_eresultMcuOfflinePrepareCode=?,pre_eresultM_PatientNoreg=?
|
|
where pre_eresultMcu_PreregisterDetailsID=?";
|
|
$qry = $this->db->query($sql, [$j_riwayat, $code, $noreg, $xID]);
|
|
$action = "update";
|
|
}
|
|
if (!$qry) {
|
|
echo json_encode(["status" => "ERR", "message" => "Err $action pre_eresult"], JSON_PRETTY_PRINT);
|
|
print_r($this->db->error());
|
|
print_r($this->db->last_query());
|
|
exit;
|
|
}
|
|
echo json_encode(["status" => "OK", "message" => "Success. $action pre_eresult"], JSON_PRETTY_PRINT);
|
|
}
|
|
function translation()
|
|
{
|
|
$this->auth_token();
|
|
$sql = "SELECT Translate_WordFrom, Translate_WordTo
|
|
FROM translate_word
|
|
WHERE Translate_WordIsActive = 'Y'
|
|
AND Translate_WordNat_LangID = 2";
|
|
$query = $this->db->query($sql);
|
|
$result = $query->result_array();
|
|
echo json_encode(["status"=>"OK", "data"=>$result], JSON_PRETTY_PRINT);
|
|
}
|
|
public function template($code, $noreg)
|
|
{
|
|
$this->auth_token();
|
|
$sql = "select ifnull(M_TitleName,'') M_TitleName,p.*
|
|
from m_patient p
|
|
join m_title on M_PatientNoReg = ? and M_PatientIsActive = 'Y'
|
|
and M_PatientM_TitleID = M_TitleID";
|
|
$qry = $this->db->query($sql, [$noreg]);
|
|
if (!$qry) {
|
|
echo json_encode(["status" => "ERR", "message" => "Err get patient from offline preparate mcu"]);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo json_encode(["status" => "ERR", "message" => "Patient with Noreg $noreg not found"]);
|
|
exit;
|
|
} else if (count($rows) > 1) {
|
|
print_r($rows);
|
|
}
|
|
$patient = $rows[0];
|
|
$patientID = $patient["M_PatientID"];
|
|
//agreement
|
|
$sql = "SELECT Mcu_PreregisterDetailsAgreement, Mcu_PreregisterDetailsID
|
|
FROM mcu_offline_prepare m
|
|
JOIN mcu_preregister_patients pr
|
|
ON m.McuOfflinePrepareID = pr.Mcu_PreregisterDetailsMcuOfflinePrepareID
|
|
AND m.McuOfflinePrepareCode = ? and Mcu_PreregisterDetailsM_PatientID = ?
|
|
AND pr.Mcu_PreregisterDetailsIsActive = 'Y'";
|
|
$qry = $this->db->query($sql, [$code, $patientID]);
|
|
if (!$qry) {
|
|
echo json_encode(["status" => "ERR", "message" => "Err get Agreement No. not found for $code | noreg $noreg"]);
|
|
// print_r($this->db->last_query());
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo json_encode(["status" => "ERR", "message" => "Agreement No with Code $code | Noreg $noreg | PID: $patienID not found"]);
|
|
echo "\n" . $this->db->last_query();
|
|
exit;
|
|
}
|
|
$mcuNumber = $rows[0]["Mcu_PreregisterDetailsAgreement"];
|
|
$xID = $rows[0]["Mcu_PreregisterDetailsID"];
|
|
//mou
|
|
$sql = "select * from m_mou where M_MouIsActive='Y' and M_MouNumber = ? order by M_MouID desc limit 0,1";
|
|
$qry = $this->db->query($sql, [$mcuNumber]);
|
|
if (!$qry) {
|
|
echo json_encode(["status" => "ERR", "message" => "Err get agreement not found for $code | noreg $noreg"]);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo json_encode(["status" => "ERR", "message" => "Agreement with Code $code | Noreg $noreg not found"]);
|
|
exit;
|
|
}
|
|
$mou = $rows[0];
|
|
$companyID = $mou["M_MouM_CompanyID"];
|
|
//get company
|
|
$sql = "select * from m_company where M_CompanyID = ?";
|
|
$qry = $this->db->query($sql, [$companyID]);
|
|
if (!$qry) {
|
|
echo json_encode(["status" => "ERR", "message" => "Err get company not found for $code | noreg $noreg | company id $companyID"]);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo json_encode(["status" => "ERR", "message" => "Agreement with Code $code | Noreg $noreg | no active company not found"]);
|
|
exit;
|
|
}
|
|
$company = $rows[0];
|
|
//get riwayat
|
|
|
|
$sql = "select * from fisik_templateresult where Fisik_TemplateResultIsActive='Y' and Fisik_TemplateResultType= 'riwayat'";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo json_encode(["status" => "ERR", "message" => "Err get template riwayat not found for $code | noreg $noreg | company id $companyID"]);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo json_encode(["status" => "ERR", "message" => "Template riwayat not found"]);
|
|
exit;
|
|
}
|
|
$riwayat = json_decode($rows[0]["Fisik_TemplateResultText"]);
|
|
$result = [
|
|
"status" => "OK",
|
|
"x-id" => $xID,
|
|
"code" => $code,
|
|
"noreg" => $noreg,
|
|
"patient" => $patient,
|
|
"mou" => $mou,
|
|
"company" => $company,
|
|
"riwayat_template" => $riwayat
|
|
];
|
|
header('Content-Type: application/json');
|
|
echo json_encode($result, JSON_PRETTY_PRINT);
|
|
}
|
|
public function patient($code)
|
|
{
|
|
$this->auth_token();
|
|
|
|
$sql = "SELECT
|
|
|
|
p.M_PatientNoReg, t.M_TitleName, p.M_PatientPrefix, p.M_PatientName, p.M_PatientSuffix, p.M_PatientLocation, p.M_PatientKedudukan AS Department
|
|
FROM mcu_offline_prepare m
|
|
JOIN mcu_preregister_patients pr
|
|
ON m.McuOfflinePrepareID = pr.Mcu_PreregisterDetailsMcuOfflinePrepareID
|
|
JOIN m_patient p ON pr.Mcu_PreregisterDetailsM_PatientID = p.M_PatientID
|
|
JOIN m_title t ON p.M_PatientM_TitleID = t.M_TitleID
|
|
WHERE m.McuOfflinePrepareCode = ?
|
|
order by McuOfflinePrepareID desc";
|
|
$query = $this->db->query($sql, [$code]);
|
|
if (!$query) {
|
|
echo json_encode(["status" => "ERR", "message" => "Err get patient from offline preparate mcu"]);
|
|
exit;
|
|
}
|
|
$result = $query->result_array();
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode(["status" =>"OK", "data" =>$result], JSON_PRETTY_PRINT);
|
|
}
|
|
|
|
|
|
function x_param()
|
|
{
|
|
$body = file_get_contents("php://input");
|
|
$this->tmp_body = $body;
|
|
//print_r($body);
|
|
$result = json_decode($this->tmp_body, true);
|
|
if (json_last_error() != JSON_ERROR_NONE) {
|
|
echo json_encode(["status" => "ERR", "message" => "JSON ERROR [Auth003]" .
|
|
json_last_error_msg()]);
|
|
echo "|" . $this->tmp_body . "|";
|
|
exit;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
function auth_token()
|
|
{
|
|
$headers = getallheaders();
|
|
foreach ($headers as $k => $v) {
|
|
$headers[strtolower($k)] = $v;
|
|
}
|
|
if (!array_key_exists('authorization', $headers)) {
|
|
echo json_encode(["status" => "ERR", "message" => "No Authorization Bearer [Auth003]"]);
|
|
exit;
|
|
}
|
|
$token = $headers["authorization"];
|
|
$token = trim(str_replace("Bearer", "", $token));
|
|
$param = JWT::decode($token, $this->SECRET_KEY, true);
|
|
$param = json_decode(json_encode($param), true);
|
|
$xnow = date("Y-m-d H:i:s");
|
|
if ($param["expired"] < $xnow) {
|
|
echo json_encode(["status" => "ERR", "message" => "Access Token expired [Auth003]"]);
|
|
exit;
|
|
}
|
|
return $param["Code"];
|
|
}
|
|
|
|
function get_token()
|
|
{
|
|
//auth token
|
|
//get branchID from token
|
|
$param = $this->x_param();
|
|
$client = $param["client"];
|
|
$secret = $param["secret"];
|
|
$sql = "select api_KeyCode
|
|
from api_key
|
|
where api_KeyCode= ?
|
|
and api_KeySecretKey = ?
|
|
and api_KeyIsActive='Y'";
|
|
$qry = $this->db->query($sql, [$client, $secret]);
|
|
if (!$qry) {
|
|
echo json_encode(["status" => "ERR", "message" => "Auth Err, no client or secret key [Auth0001]"]);
|
|
exit;
|
|
}
|
|
// echo $this->db->last_query();
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo json_encode(["status" => "ERR", "message" => "Auth Err, invalid client or secret key [Auth0002]"]);
|
|
exit;
|
|
}
|
|
$expired = date("Y-m-d H:i:s", strtotime("now + {$this->intervalHour} hour"));
|
|
$auth_param = ["Code" => $rows[0]["api_KeyCode"], "expired" => $expired];
|
|
$token = JWT::encode($auth_param, $this->SECRET_KEY);
|
|
echo json_encode(["status" => "OK", "token" => $token, "expired" => $expired], JSON_PRETTY_PRINT);
|
|
}
|
|
function refresh_token()
|
|
{
|
|
$code = $this->auth_token();
|
|
$sql = "select api_KeyCode
|
|
from api_key
|
|
where api_KeyCode= ?
|
|
and api_KeyIsActive='Y'";
|
|
$qry = $this->db->query($sql, [$code]);
|
|
if (!$qry) {
|
|
echo $this->db->error()["message"];
|
|
echo json_encode(["status" => "ERR", "message" => "Auth Err, access Token invalid [Auth003]"]);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
echo json_encode(["status" => "ERR", "message" => "Auth Err, access Token invalid [Auth0004]"]);
|
|
exit;
|
|
}
|
|
$expired = date("Y-m-d H:i:s", strtotime("now + {$this->intervalHour} hour"));
|
|
$auth_param = ["code" => $rows[0]["api_KeyM_BranchCode"], "expired" => $expired];
|
|
$token = JWT::encode($auth_param, $this->SECRET_KEY);
|
|
echo json_encode(["status" => "OK", "token" => $token, "expired" => $expired]);
|
|
}
|
|
}
|
|
/*
|
|
create table pre_eresult(
|
|
pre_eresultID int not null auto_increment primary key,
|
|
pre_eresultMcu_PreregisterDetailsID int,
|
|
pre_eresultMcuOfflinePrepareCode varchar(100),
|
|
pre_eresultM_PatientNoreg varchar(30),
|
|
pre_eresultRiwayat text,
|
|
pre_eresultCreated datetime default current_timestamp(),
|
|
pre_eresultLastUpdated datetime default current_timestamp()
|
|
on update current_timestamp(),
|
|
key(pre_eresultMcu_PreregisterDetailsID),
|
|
key(pre_eresultMcuOfflinePrepareCode)
|
|
);
|
|
*/
|