Files
BE_IBL/application/controllers/mockup/fo/ibl_registration/Patient.php
2026-04-15 15:24:34 +07:00

482 lines
18 KiB
PHP

<?php
/*
template function {
$this->sys_debug();
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
*/
class Patient extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "Patient API";
}
public function __construct()
{
parent::__construct();
$this->db_smartone = $this->load->database("onedev", true);
}
function _add_address(&$pat) {
if (count($pat) == "0") {
return array();
}
foreach($pat as $idx => $p ) {
$pat[$idx]["address"] = array($p["M_PatientAddress"]);
}
$this->_add_history($pat);
}
function _add_history(&$pat) {
$pat_list = "-1";
foreach($pat as $idx => $p) {
$pat_list .= ", " . $p["M_PatientID"];
if (! isset($pat[$idx]["history"])) $pat[$idx]["history"] = array();
}
$sql = "select T_OrderHeaderM_PatientID,T_OrderHeaderLabNumber,T_OrderHeaderDate,
concat(T_OrderDetailT_TestName) T_TestName
from
t_orderheader
join t_orderdetail on
T_OrderHeaderID = T_OrderDetailID and
T_OrderHeaderIsActive = 'Y' and T_OrderDetailIsActive = 'Y'
and T_OrderHeaderM_PatientID in ( $pat_list )
join t_test on T_OrderDetailT_TestID = T_TestID
and T_TestIsPrice = 'Y'
order by T_OrderHeaderM_PatientID,T_OrderHeaderLabNumber";
$query = $this->db_smartone->query($sql);
if ($query) {
$rows= $query->result_array();
foreach($rows as $r) {
$patientID = $r["T_OrderHeaderM_PatientID"];
foreach($pat as $idx => $p) {
if($p["M_PatientID"] == $patientID) {
$pat[$idx]["history"][] = $r;
}
}
}
} else {
$this->sys_error_db("m_patient history",$this->db_smartone);
exit;
}
}
public function search()
{
$prm = $this->sys_input;
$max_rst = 100;
$tot_count =0;
$number_limit = 10;
$number_offset = (!isset($prm['current_page'])?1:$prm['current_page'] - 1) * $number_limit ;
$q = [
'noreg' => "",
'name' => '',
'hp' => '',
'dob' => '',
'address' => ''
];
$search_address = '';
if ($prm['noreg'] != '')
$q['noreg'] = "AND M_PatientNoReg like '%{$prm['noreg']}%'";
if ($prm['search'] != '')
{
$e = explode('+', $prm['search']);
if (isset($e[0])){
$e[0] = str_replace("'", "\\'", $e[0]);
$q['name'] = "AND M_PatientName LIKE '%{$e[0]}%'";
}
if (isset($e[1]))
$q['hp'] = "AND ((M_PatientHP LIKE '%{$e[1]}%' and M_PatientHP IS NOT NULL) OR (M_PatientHP IS NULL AND '{$e[1]}' = ''))";
if (isset($e[2]))
$q['dob'] = "AND ((DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') LIKE '%{$e[2]}%' and M_PatientDOB IS NOT NULL) OR (M_PatientDOB IS NULL AND '{$e[2]}' = ''))";
if (isset($e[3]))
$q['address'] = "AND M_PatientAddressDescription LIKE '%{$e[3]}%'";
}
if($q['address'] == ''){
$q['address'] = "AND M_PatientAddressNote = 'Utama'";
}
$sql = "SELECT 'N' divider,M_PatientID, M_PatientNoReg,M_PatientEmail,M_PatientPrefix,M_PatientSuffix,
concat(M_TitleName,' ',IFNULL(M_PatientPrefix,''),' ',M_PatientName,' ',IFNULL(M_PatientSuffix,'')) M_PatientName,
M_PatientName M_PatientRealName, M_TitleID, M_TitleName, M_SexID, M_SexName,
M_PatientHP, M_PatientPOB, M_PatientDOB, DATE_FORMAT(M_PatientDOB,'%d-%m-%Y') as dob_ina,
'' M_PatientAddress,
M_PatientAddressID,
M_PatientAddressDescription, M_PatientM_IdTypeID, M_PatientIDNumber,
M_PatientAddressRegionalCd,
M_PatientAddressLocation,
M_PatientAddressCity,
M_PatientAddressVillage,
M_PatientAddressDistrict,
M_PatientAddressState,
M_PatientAddressCountry,
M_PatientAddressCountryCode,
IFNULL(M_PatientNote, '') M_PatientNote, M_PatientPhoto, IF(M_PatientPhone IS NULL OR M_PatientPhone = '', M_PatientHP, M_PatientPhone) hp,
-- fn_fo_patient_visit(M_PatientID) info,
M_PatientAddressM_KelurahanID M_KelurahanID, 0 M_DistrictID, 0 M_CityID, 0 M_ProvinceID, M_PatientM_ReligionID,
IFNULL(M_ReligionName, '-') M_ReligionName,
IFNULL(Patient_SignatureUrl, '') image_signature,
M_PatientNote
FROM m_patient
join m_title on M_PatientM_TitleID = M_TitleID
join m_sex on M_PatientM_SexID = M_SexID
join m_patientaddress on M_PatientAddressM_PatientID = M_PatientID and M_PatientAddressIsActive = 'Y' {$q['address']}
left join m_religion on m_patientm_religionid = m_religionid
left join patient_signature on Patient_SignatureM_PatientID = M_PatientID and Patient_SignatureIsActive = 'Y'
where M_PatientIsActive = 'Y'
{$q['noreg']}
{$q['name']}
{$q['hp']}
{$q['dob']}
group by M_PatientID
limit $number_limit offset $number_offset";
//echo $sql;
$query = $this->db_smartone->query($sql);
if ($query) {
$rows = $query->result_array();
foreach ($rows as $k => $v)
{
$rows[$k]['M_PatientName'] = stripslashes($rows[$k]['M_PatientName']);
$rows[$k]['M_PatientAddress'] = stripslashes($rows[$k]['M_PatientAddressDescription']);
$info = $this->db_smartone->query("SELECT fn_fo_patient_visit(?) info", [$v['M_PatientID']])->row();
$rows[$k]['info'] = json_decode($info->info);
$references = [];
$sql = "SELECT M_ReferenceID, M_ReferenceName
FROM m_patient_reference
join m_reference on M_PatientReferenceM_ReferenceID = M_ReferenceID
WHERE M_PatientReferenceM_PatientID = ? AND M_PatientReferenceIsActive = 'Y'";
$query = $this->db_smartone->query($sql, [$v['M_PatientID']]);
if ($query) {
$references = $query->result_array();
}
$rows[$k]['references'] = $references;
}
$result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_patient rows",$this->db_smartone);
exit;
}
}
function add_new()
{
$userid = $this->sys_user["M_UserID"];
$prm = $this->sys_input;
$prm['M_PatientDOB'] = date('Y-m-d', strtotime($prm['M_PatientDOB']));
//sipe
$M_IdTypeID = 0;
if( $prm['M_PatientM_IdTypeID'] > 0 ) {
$M_IdTypeID = $prm['M_PatientM_IdTypeID'];
}
$patient_name = str_replace("'", "\\'", $prm['M_PatientName']);
$ptn = [
'M_PatientName' => $patient_name,
'M_PatientM_TitleID' => $prm['M_PatientM_TitleID'],
'M_PatientPrefix' => $prm['M_PatientPrefix'],
'M_PatientSuffix' => $prm['M_PatientSuffix'],
'M_PatientM_SexID' => $prm['M_PatientM_SexID'],
'M_PatientM_ReligionID' => $prm['M_PatientM_ReligionID'],
'M_PatientDOB' => $prm['M_PatientDOB'],
'M_PatientPOB' => $prm['M_PatientPOB'],
'M_PatientHP' => $prm['M_PatientHP'],
'M_PatientPhone' => $prm['M_PatientPhone'],
'M_PatientEmail' => $prm['M_PatientEmail'],
'M_PatientM_IdTypeID' => $M_IdTypeID ,
'M_PatientIDNumber' => $prm['M_PatientIDNumber'],
'M_PatientNote' => $prm['M_PatientNote'],
'M_PatientUserID' => $userid,
'M_PatientCreated' => date('Y-m-d H:i:s'),
'M_PatientCreatedUserID' => $userid
];
$this->db_smartone->insert('m_patient', $ptn);
$err = $this->db_smartone->error();
if ( $err['message'] != "" )
{
$this->sys_error_db("m_patient rows", $this->db_smartone);
return;
}
$id = $this->db_smartone->insert_id();
// LOG FO
$ptn = json_encode($ptn);
// $this->db_smartone->query("CALL one_log.log_me('FO', 'FO.PATIENT.ADD', '{$ptn}', '0')");
$address_description = str_replace("'", "\\'", $prm['M_PatientAddressDescription']);
// save address
$add = [
'M_PatientAddressM_PatientID' => $id,
'M_PatientAddressDescription' => $address_description,
'M_PatientAddressUserID'=> $userid,
'M_PatientAddressRegionalCd' => $prm['M_PatientAddressRegionalCd'],
'M_PatientAddressLocation' => $prm['M_PatientAddressLocation'],
'M_PatientAddressCity' => $prm['M_PatientAddressCity'],
'M_PatientAddressVillage' => $prm['M_PatientAddressVillage'],
'M_PatientAddressDistrict' => $prm['M_PatientAddressDistrict'],
'M_PatientAddressState' => $prm['M_PatientAddressState'],
'M_PatientAddressCountry' => $prm['M_PatientAddressCountry'],
'M_PatientAddressCountryCode' => $prm['M_PatientAddressCountryCode'],
'M_PatientAddressNote' => isset($prm['M_PatientAddressNote']) ? $prm['M_PatientAddressNote'] : 'Utama',
'M_PatientAddressCreated' => date('Y-m-d H:i:s'),
'M_PatientAddressCreatedUserID' => $userid
];
$this->db_smartone->insert('m_patientaddress', $add);
$err = $this->db_smartone->error();
if ( $err['message'] != "" )
{
$this->sys_error_db("m_patient address rows", $this->db_smartone);
return;
}
$references = isset($prm['references']) && !empty($prm['references']) ? $prm['references'] : [];
if(count($references) > 0){
foreach($references as $reference){
$this->db_smartone->insert('m_patient_reference', [
'M_PatientReferenceM_PatientID' => $id,
'M_PatientReferenceM_ReferenceID' => $reference['M_ReferenceID'],
'M_PatientReferenceCreated' => date('Y-m-d H:i:s'),
'M_PatientReferenceCreatedUserID' => $userid
]);
}
}
//echo $this->db_smartone->last_query();
// LOG FO
$add = json_encode($add);
//$this->db_smartone->query("CALL one_log.log_me('FO', 'FO.PATIENT.ADDRESS.ADD', '{$add}', '0')");
// get
$r = $this->db_smartone->where('M_PatientID', $id)
->get('m_patient')
->row();
$rst = array("id" => $id, 'noreg'=>$r->M_PatientNoReg);
$this->sys_ok($rst);
}
function edit()
{
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$prm['M_PatientDOB'] = date('Y-m-d', strtotime($prm['M_PatientDOB']));
$patient_name = str_replace("'", "\\'", $prm['M_PatientName']);
$this->db_smartone->set('M_PatientName', $patient_name)
->set('M_PatientM_TitleID', $prm['M_PatientM_TitleID'])
->set('M_PatientPrefix', $prm['M_PatientPrefix'])
->set('M_PatientSuffix', $prm['M_PatientSuffix'])
->set('M_PatientM_SexID', $prm['M_PatientM_SexID'])
->set('M_PatientM_ReligionID', $prm['M_PatientM_ReligionID'])
->set('M_PatientDOB', $prm['M_PatientDOB'])
->set('M_PatientPOB', $prm['M_PatientPOB'])
->set('M_PatientHP', $prm['M_PatientHP'])
->set('M_PatientPhone', $prm['M_PatientPhone'])
->set('M_PatientEmail', $prm['M_PatientEmail'])
->set('M_PatientM_IdTypeID', $prm['M_PatientM_IdTypeID'])
->set('M_PatientIDNumber', $prm['M_PatientIDNumber'])
->set('M_PatientNote', $prm['M_PatientNote'])
->set('M_PatientUserID', $userid)
->set('M_PatientLastUpdatedUserID', $userid)
->where('M_PatientID', $prm['id'])
->update('m_patient');
$err = $this->db_smartone->error();
if ( $err['message'] != "" )
{
$this->sys_error_db("m_patient rows", $this->db_smartone);
return;
}
$id = $prm['id'];
// LOG FO
unset($prm['token']);
$ptn = json_encode($prm);
$id_address = isset($prm['M_PatientAddressID']) && $prm['M_PatientAddressID'] > 0 ? $prm['M_PatientAddressID']:0;
$address_description = str_replace("'", "\\'", $prm['M_PatientAddressDescription']);
$this->db_smartone->set('M_PatientAddressRegionalCd', $prm['M_PatientAddressRegionalCd'])
->set('M_PatientAddressLocation', $prm['M_PatientAddressLocation'])
->set('M_PatientAddressCity', $prm['M_PatientAddressCity'])
->set('M_PatientAddressVillage', $prm['M_PatientAddressVillage'])
->set('M_PatientAddressDistrict', $prm['M_PatientAddressDistrict'])
->set('M_PatientAddressState', $prm['M_PatientAddressState'])
->set('M_PatientAddressCountry', $prm['M_PatientAddressCountry'])
->set('M_PatientAddressCountryCode', $prm['M_PatientAddressCountryCode'])
->set('M_PatientAddressDescription', $address_description )
->set('M_PatientAddressUserID', $userid )
->set('M_PatientAddressLastUpdatedUserID', $userid)
->where('M_PatientAddressID', $id_address)
->update('m_patientaddress');
$err = $this->db_smartone->error();
if ( $err['message'] != "" )
{
$this->sys_error_db("m_patientaddress rows", $this->db_smartone);
return;
}
// echo $this->db_smartone->last_query();
// LOG FO
//$this->db_smartone->query("CALL one_log.log_me('FO', 'FO.PATIENT.ADDRESS.EDIT', '{$add}', '0')");
// get
$r = $this->db_smartone->where('M_PatientID', $id)
->get('m_patient')
->row();
$rst = array("id" => $id, 'noreg'=>$r->M_PatientNoReg);
$this->sys_ok($rst);
}
public function search_idtype()
{
$prm = $this->sys_input;
$sql = "SELECT M_IdTypeID, M_IdTypeName
FROM m_idtype
WHERE M_IdTypeIsActive = 'Y'
ORDER BY M_IdTypeName ASC";
$query = $this->db_smartone->query($sql);
if ($query) {
$rows = $query->result_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_idtype rows",$this->db_smartone);
exit;
}
}
function searchregion(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = $prm['search'];
$sql = "SELECT
r.regional_cd,
r.regional_cd AS id,
r.regional_nm,
r.full_name AS text_nm,
r.pro_cd, IFNULL(pro.regional_nm,'') AS pro_nm,
r.kab_cd, IFNULL(kab.regional_nm,'') AS kab_nm,
r.kec_cd, IFNULL(kec.regional_nm,'') AS kec_nm,
r.kel_cd, IFNULL(kel.regional_nm,'') AS kel_nm,
r.status_cd, r.old_nm
FROM regional r
LEFT JOIN regional pro ON CONCAT(r.pro_cd, REPEAT('0', 8)) = pro.regional_cd
LEFT JOIN regional kab ON CONCAT(r.pro_cd, r.kab_cd, REPEAT('0', 6)) = kab.regional_cd
LEFT JOIN regional kec ON CONCAT(r.pro_cd, r.kab_cd, r.kec_cd, REPEAT('0', 3)) = kec.regional_cd
LEFT JOIN regional kel ON CONCAT(r.pro_cd, r.kab_cd, r.kec_cd, r.kel_cd) = kel.regional_cd
WHERE
r.full_name LIKE CONCAT('%','{$search}','%')
LIMIT 100
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->sys_error_db("search wilayah select error", $this->db_onedev);
exit;
}
$rows = $qry->result_array();
$result = array(
"records" => $rows,
"sql" => $this->db_onedev->last_query()
);
$this->sys_ok($result);
exit;
}
function search_countries(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = $prm['search'];
if(!$search || $search == ''){
$search = 'Indonesia';
}
$sql = "SELECT * FROM terminology WHERE attribute_path = 'Address.country' AND display LIKE '%$search%' ORDER BY display ASC LIMIT 20";
$query = $this->db_onedev->query($sql);
$rows = $query->result_array();
$result = array("records" => $rows);
$this->sys_ok($result);
exit;
}
function search_icd10()
{
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$userID = $this->sys_user['M_UserID'];
//print_r($prm['subgroup']);
$sql = "SELECT terminology.*, CONCAT(code,' | ', display) as display_name
FROM one_terminology.terminology
WHERE
attribute_path = 'icd10' AND ( code LIKE CONCAT('%',?,'%') OR MATCH (display) AGAINST (? IN NATURAL LANGUAGE MODE) OR CONCAT(code,' | ', display) LIKE CONCAT('%',?,'%'))
GROUP BY code";
$query = $this->db_onedev->query($sql, array($prm['search'], $prm['search'], $prm['search']));
if (!$query) {
$this->sys_error("Gagal cari End");
}
$result = $query->result_array();
$this->sys_ok($result);
exit;
}
}