Files
2026-04-15 15:24:12 +07:00

342 lines
11 KiB
PHP

<?php
class Patient extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Patient API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
$this->load->library('ImageManipulator');
}
function regenerateOldPhoto($home_dir, $id)
{
$r = $this->db_onedev->select('m_patientphoto, m_patientphotocounter', false)
->where('m_patientid', $id)
->get('m_patient')
->row();
if ($r->m_patientphoto != null && $r->m_patientphotocounter > 0) {
$full_path = substr_replace($home_dir ,"", -1) . $r->m_patientphoto;
$path_parts = pathinfo($full_path);
$rename = $path_parts['dirname'] . '/' . $path_parts['filename'] . '-' . $r->m_patientphotocounter . '.' . $path_parts['extension'];
rename($full_path, $rename);
// echo $path_parts['dirname'], "\n";
// echo $path_parts['extension'], "\n";
// echo $path_parts['filename'], "\n";
return "/" . str_replace($home_dir, "", $rename);
}
return false;
}
public function upload()
{
$inp = $this->sys_input;
$home_dir = "/home/one/project/one/";
$target_dir = $home_dir . "one-media/one-photo/patient/" . date("Y") . "/";
$y = $this->regenerateOldPhoto($home_dir, $inp['id']);
// get patient mr
$p = $this->db_onedev->select("M_PatientNoReg")
->where("M_PatientID", $inp['id'])
->get('m_patient')
->row();
if (!file_exists($target_dir)) {
mkdir($target_dir, 0755, true);
}
$target_path = $target_dir . $p->M_PatientNoReg . ".jpg";
$this->base64_to_jpeg($inp['data'], $target_path);
// CROP Image
$im = new ImageManipulator($target_path);
$w = $im->getWidth();
$h = $im->getHeight();
$mw = ceil(3 * $h / 4);
if ($w <= $mw)
{
$x1 = 0;
$y1 = 0;
$x2 = $w;
$y2 = $h;
}
else
{
$x1 = floor(($w - $mw) / 2);
$y1 = 0;
$x2 = ceil($w - (($w - $mw) / 2));
$y2 = $h;
}
$im->crop($x1, $y1, $x2, $y2); // takes care of out of boundary conditions automatically
$im->save($target_path);
$x = $this->generateThumbnail($target_path, 75, 100);
// Save to DB
$this->db_onedev->set("M_PatientPhoto", "/" . str_replace($home_dir, "", $target_path))
->set("M_PatientPhotoThumb", "/" . str_replace($home_dir, "", $x))
->set('M_PatientPhotoCounter', '`M_PatientPhotoCounter` + 1', false)
->where('M_PatientID', $inp['id'])
->update('m_patient');
// LOGGING
$code = $y ? "PHOTO.PATIENT.EDIT" : "PHOTO.PATIENT.ADD";
$one_log = $this->load->database('onelog', true);
$one_log->set('Log_PhotoCode', $code)
->set('Log_PhotoM_PatientID', $inp['id'])
->set('Log_PhotoUrl', $y ? $y : "/" . str_replace($home_dir, "", $target_path))
->insert('log_photo');
$this->sys_ok(["rename"=>$y, "patient_id"=>$inp['id'], "patient_mr"=>$p->M_PatientNoReg, "photo_url"=>"http://" . $_SERVER['SERVER_NAME'] . "/" . str_replace($home_dir, "", $target_path) . "?d=" . date("YmdHis")]);
}
function generateThumbnail($img, $width, $height, $quality = 90)
{
if (is_file($img)) {
$imagick = new Imagick(realpath($img));
$imagick->setImageFormat('jpeg');
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$imagick->setImageCompressionQuality($quality);
$imagick->thumbnailImage($width, $height, false, false);
$filename_no_ext = reset(explode('.', $img));
if (file_put_contents($filename_no_ext . '_thumb' . '.jpg', $imagick) === false) {
throw new Exception("Could not put contents.");
}
return $filename_no_ext . '_thumb' . '.jpg';
}
else {
throw new Exception("No valid image provided with {$img}.");
}
}
function base64_to_jpeg($base64_string, $output_file) {
// open the output file for writing
$ifp = fopen( $output_file, 'wb' );
// split the string on commas
// $data[ 0 ] == "data:image/png;base64"
// $data[ 1 ] == <actual base64 string>
$data = explode( ',', $base64_string );
// we could add validation here with ensuring count( $data ) > 1
fwrite( $ifp, base64_decode( $data[ 1 ] ) );
// clean up the file resource
fclose( $ifp );
return $output_file;
}
function exist_in_array($arrs,$arr,$key){
$rtn = -1;
foreach($arrs as $k => $v){
if($v[$key] == $arr[$key]){
$rtn = $k;
break;
}
}
return $rtn;
}
function start_mcu(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$xuserid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$sql = "
SELECT *,
DATE_FORMAT(McuOfflinePrepareStartDate,'%d-%m-%Y') as start_date,
DATE_FORMAT(McuOfflinePrepareEndDate,'%d-%m-%Y') as end_date,
'' as notes
FROM mcu_offline_prepare
JOIN m_company ON McuOfflinePrepareM_CompanyID = M_CompanyID
JOIN m_mou ON McuOfflinePrepareM_MouDefaultID = M_MouID
JOIN m_doctor ON McuOfflinePrepareM_DoctorDefaultID = M_DoctorID
JOIN m_doctoraddress ON McuOfflinePrepareM_DoctorAddressDefaultID = M_DoctorAddressID
WHERE
McuOfflinePrepareCode = '{$prm['code']}' AND McuOfflinePrepareIsActive = 'Y'
LIMIT 1
";
//echo $sql;
$query = $this->db_onedev->query($sql)->row_array();
if (!$query) {
$this->sys_error_db("f_paymentdetail delete");
exit;
}
else{
$my_string = preg_replace(array('/\n/', '/\r/'), '#FHM', $query['M_MouNote']);
$query['notes'] = explode('#FHM', $my_string);
}
$result = array(
"total" => 1 ,
"records" => $query
);
$this->sys_ok($result);
exit;
}
function patient_list(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$mous = array();
//# ambil parameter input
$xuserid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$tot_count =0;
$number_limit = 10;
$number_offset = (!isset($prm['current_page'])?1:intval($prm['current_page']) - 1) * $number_limit ;
$sql_where = '';
if($prm['search'] != ''){
$search = $prm['search'];
$sql_where = " AND ( Mcu_PreregisterDetailsPatientName LIKE CONCAT('%','{$search}','%') OR Mcu_PreregisterDetailsKTP = '{$search}' )";
}
if(isset($prm['status']) && $prm['status'] != 'A'){
$sql_where .= " AND Mcu_PreregisterDetailsFlagAction = '{$prm['status']}'";
}
$sql = "
SELECT *,
CONCAT(IFNULL(M_TitleName,''),' ',IFNULL(M_PatientPrefix,''),M_PatientName,IFNULL(M_PatientSuffix,'')) as patient_fullname,
M_SexName,
'' as tests,
'' as x_tests,
'' as notes,
if(M_MouNote = NULL OR M_MouNote = '','X','N') as show_note
FROM mcu_preregister_patients
JOIN m_patient ON Mcu_PreregisterDetailsM_PatientID = M_PatientID
JOIN m_sex ON M_PatientM_SexID = M_SexID
JOIN m_mou ON Mcu_PreregisterDetailsAgreement = M_MouNumber AND M_MouIsActive = 'Y'
JOIN m_company ON M_MouM_CompanyID = M_CompanyID
LEFT JOIN m_title ON M_PatientM_TitleID = M_TitleID
WHERE
Mcu_PreregisterDetailsMcuOfflinePrepareID = {$prm['header_id']} AND Mcu_PreregisterDetailsIsActive = 'Y' $sql_where
limit $number_limit offset $number_offset
";
//echo $sql;
$query = $this->db_onedev->query($sql)->result_array();
if (!$query) {
//$this->sys_error_db("f_paymentdetail ttt");
$query = [];
//exit;
}
else{
$sql = "SELECT *
FROM m_mou
WHERE
M_MouM_CompanyID = {$query[0]['M_CompanyID']} AND M_MouIsActive = 'Y'";
$mous = $this->db_onedev->query($sql)->result_array();
foreach($query as $k => $v){
$my_string = preg_replace(array('/\n/', '/\r/'), '#FHM', $v['M_MouNote']);
$query[$k]['notes'] = explode('#FHM', $my_string);
$tests_code = $v['Mcu_PreregisterDetailsTests'];
$exp_test_code = explode(',',$tests_code);
$test_arr = array();
$test_name_arr = array();
foreach($exp_test_code as $ki => $vi){
$sql = "
SELECT *
FROM (
SELECT ss_price_mou.*, t_test.T_TestName as test_name
FROM ss_price_mou
JOIN t_test ON ss_price_mou.T_TestID = t_test.T_TestID AND T_TestSasCode = '{$vi}' AND T_TestIsActive = 'Y'
JOIN nat_test ON t_test.T_TestNat_TestID = nat_test.Nat_TestID AND nat_test.Nat_TestIsActive = 'Y'
WHERE
is_packet = 'N' AND Ss_PriceMouM_MouID = {$v['M_MouID']}
UNION
SELECT ss_price_mou.*, T_PacketName as test_name
FROM ss_price_mou
JOIN t_packet ON T_TestID = T_PacketID AND T_PacketM_MouID = Ss_PriceMouM_MouID AND T_PacketSasCode = '{$vi}' AND T_PacketIsActive = 'Y'
WHERE
is_packet = 'Y' AND Ss_PriceMouM_MouID = {$v['M_MouID']}
) x
LIMIT 1
";
// echo $sql;
$row_x = $this->db_onedev->query($sql)->row_array();
array_push($test_arr,$row_x);
array_push($test_name_arr,$row_x['test_name']);
}
$query[$k]['tests'] = $test_arr;
$query[$k]['x_tests'] = join(", ",$test_name_arr);
}
}
$result = array(
"total" => 1 ,
"records" => $query,
"mous" => $mous
);
$this->sys_ok($result);
exit;
}
function search_test(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$xuserid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$tot_count =0;
$number_limit = 10;
$number_offset = (!isset($prm['current_page'])?1:intval($prm['current_page']) - 1) * $number_limit ;
$search = $prm['search'];
$sql = "
SELECT * FROM (
SELECT ss.*, ts.T_PacketName as test_name
FROM ss_price_mou ss
JOIN t_packet ts ON ss.T_TestID = ts.T_PacketID
WHERE
ss.Ss_PriceMouM_MouID = {$prm['mouid']} AND ss.is_packet = 'Y' AND ts.T_PacketName LIKE CONCAT('%','{$search}','%')
UNION
SELECT ss.*, ts.T_TestName as test_name
FROM ss_price_mou ss
JOIN t_test ts ON ss.T_TestID = ts.T_TestID
JOIN nat_test nt ON ts.T_TestNat_TestID = nt.Nat_TestID AND nt.Nat_TestIsActive = 'Y'
WHERE
ss.Ss_PriceMouM_MouID = {$prm['mouid']} AND ss.is_packet = 'N' AND ts.T_TestName LIKE CONCAT('%','{$search}','%')
) x limit $number_limit offset $number_offset";
$data = $this->db_onedev->query($sql)->result_array();
/*if($data){
}*/
$result = array(
"total" => 1 ,
"records" => $data
);
$this->sys_ok($result);
exit;
}
}