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 = 12; $tot_count =0; $q = [ 'noreg' => '%', 'name' => '%', 'hp' => '%', 'dob' => '%', 'address' => '%' ]; if ($prm['noreg'] != '') $q['noreg'] = "%{$prm['noreg']}%"; if ($prm['search'] != '') { $e = explode('+', $prm['search']); if (isset($e[0])) $q['name'] = "%{$e[0]}%"; if (isset($e[1])) $q['hp'] = "%{$e[1]}%"; if (isset($e[2])) $q['dob'] = "%{$e[2]}%"; if (isset($e[3])) $q['address'] = "%{$e[3]}%"; } // QUERY TOTAL $sql = "select count(distinct m_patientid) total from m_patient join m_title on M_PatientM_TitleID = M_TitleID where M_PatientNoReg like ? and M_PatientName LIKE ? and ((M_PatientHP LIKE ? and M_PatientHP IS NOT NULL) OR M_PatientHP IS NULL) and ((M_PatientDOB LIKE ? and M_PatientDOB IS NOT NULL) OR M_PatientDOB IS NULL)"; $query = $this->db_smartone->query($sql, array($q['noreg'], $q['name'], $q['hp'], $q['dob'])); if ($query) { $tot_count = $query->result_array()[0]["total"]; } else { $this->sys_error_db("m_patient count",$this->db_smartone); exit; } $sql = "select M_PatientID, M_PatientNoReg, concat(M_TitleName,' ',M_PatientName) M_PatientName, M_PatientHP, M_PatientDOB, M_PatientNote, concat(M_PatientAddressDescription, '\n\n', m_kelurahanname, ', ', m_districtname, '\n', m_cityname, ', ', m_provincename) as M_PatientAddress, M_PatientNote, M_PatientPhoto from m_patient join m_title on M_PatientM_TitleID = M_TitleID left join m_patientaddress on M_PatientAddressM_PatientID = M_PatientID and M_PatientAddressIsActive = 'Y' left join m_kelurahan on m_patientaddressm_kelurahanid = m_kelurahanid left join m_district on m_kelurahanm_districtid = m_districtid left join m_city on m_districtm_cityid = m_cityid left join m_province on m_citym_provinceid = m_provinceid where M_PatientNoReg like ? and M_PatientName LIKE ? and ((M_PatientHP LIKE ? and M_PatientHP IS NOT NULL) OR M_PatientHP IS NULL) and ((M_PatientDOB LIKE ? and M_PatientDOB IS NOT NULL) OR M_PatientDOB IS NULL) group by m_patientid limit 0,{$max_rst}"; $query = $this->db_smartone->query($sql, array($q['noreg'], $q['name'], $q['hp'], $q['dob'])); if ($query) { $rows = $query->result_array(); $result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows)); $this->sys_ok($result); } else { $this->sys_error_db("m_patient rows",$this->db_smartone); exit; } } public function searchdoctor() { $prm = $this->sys_input; $max_rst = 12; $tot_count =0; $q = [ 'search' => '%' ]; if ($prm['search'] != '') { $q['search'] = "%{$prm['search']}%"; } // QUERY TOTAL $sql = "select count(*) total from m_doctor where M_DoctorIsActive = 'Y' and M_DoctorName like ?"; $query = $this->db_smartone->query($sql, array($q['search'])); if ($query) { $tot_count = $query->result_array()[0]["total"]; } else { $this->sys_error_db("m_patient count",$this->db_smartone); exit; } $sql = "select M_DoctorID as id, concat(IFNULL(M_DoctorPrefix, ''),' ',M_DoctorName, ' ', IFNULL(M_DoctorSufix, '')) as name, '' as address from m_doctor left join m_doctoraddress on M_DoctorAddressIsActive = 'Y' and M_DoctorAddressM_DoctorID = M_DoctorID where M_DoctorIsActive = 'Y' and concat(IFNULL(M_DoctorPrefix, ''),' ',M_DoctorName, ' ', IFNULL(M_DoctorSufix, '')) like ? group by M_DoctorID"; $query = $this->db_smartone->query($sql, array($q['search'])); if ($query) { $rows = $query->result_array(); foreach ($rows as $k => $v){ $sql = "SELECT * FROM m_doctoraddress WHERE M_DoctorAddressM_DoctorID = {$v['id']} AND M_DoctorAddressIsActive = 'Y'"; $rows[$k]['address'] = $this->db_onedev->query($sql)->result(); } $result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows)); $this->sys_ok($result); } else { $this->sys_error_db("m_doctor rows",$this->db_smartone); exit; } } function add_new() { $prm = $this->sys_input; $prm['M_PatientDOB'] = date('Y-m-d', strtotime($prm['M_PatientDOB'])); $ptn = [ 'M_PatientName' => $prm['M_PatientName'], 'M_PatientM_TitleID' => $prm['M_PatientM_TitleID'], '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_PatientHP' => $prm['M_PatientHP'], 'M_PatientPhone' => $prm['M_PatientPhone'], 'M_PatientEmail' => $prm['M_PatientEmail'], 'M_PatientM_IdTypeID' => $prm['M_PatientM_IdTypeID'], 'M_PatientIDNumber' => $prm['M_PatientIDNumber'], 'M_PatientNote' => $prm['M_PatientNote'] ]; $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')"); // save address $add = [ 'M_PatientAddressM_PatientID' => $id, 'M_PatientAddressDescription' => $prm['M_PatientAddressDescription'], 'M_PatientAddressM_KelurahanID' => $prm['M_PatientAddressM_KelurahanID'] ]; $this->db_smartone->insert('m_patientaddress', $add); // 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); } // public function search_() // { // $prm = $this->sys_input; // $noreg = $prm["noreg"]; // $search = $prm["search"]; // //prioritas pada noreg // if ($noreg != "") { // $noreg = "%$noreg%"; // $sql = "select count(*) total // from // m_patient join m_title on M_PatientM_TitleID = M_TitleID // where M_PatientNoReg like ?"; // $query = $this->db_smartone->query($sql, array($noreg)); // $tot_count =0; // if ($query) { // $tot_count = $query->result_array()[0]["total"]; // } else { // $this->sys_error_db("m_patient count",$this->db_smartone); // exit; // } // $sql = "select M_PatientID, M_PatientNoReg, // concat(M_TitleName,' ',M_PatientName) M_PatientName, // M_PatientHP, M_PatientDOB, M_PatientNote, M_PatientAddress // from // m_patient join m_title on M_PatientM_TitleID = M_TitleID // where M_PatientNoReg like ? // limit 0,10"; // $query = $this->db_smartone->query($sql, array($noreg)); // $rows = $query->result_array(); // $this->_add_address($rows); // $result = array("total" => $tot_count, "records" => $rows); // $this->sys_ok($result); // exit; // } // //parse query // $nama = $dob = $hp = $alamat = ""; // try { // list($nama, $hp, $dob,$alamat) = explode("+", $search); // } catch(Exception $e) { // } // $sql_where = ""; // $sql_param = array(); // if ($nama != "") { // if ($sql_where != "") { // $sql_where .=" and "; // } // $sql_where .= " M_PatientName like ? "; // $sql_param[] = "%$nama%"; // } // if ($dob != "") { // if ($sql_where != "") { // $sql_where .=" and "; // } // $sql_where .= " M_PatientDOB like ? "; // $sql_param[] = "%$dob%"; // } // if ($hp != "") { // if ($sql_where != "") { // $sql_where .=" and "; // } // $sql_where .= " M_PatientHp like ? "; // $sql_param[] = "%$hp%"; // } // if ($alamat != "") { // if ($sql_where != "") { // $sql_where .=" and "; // } // $sql_where .= " M_PatientAddress like ?"; // $sql_param[] = "%$alamat%"; // } // if ($sql_where != "") { // $sql_where = "where $sql_where"; // } // $sql = "select count(*) total // from // m_patient join m_title on M_PatientM_TitleID = M_TitleID // $sql_where"; // $query = $this->db_smartone->query($sql, $sql_param); // $tot_count =0; // if ($query) { // $tot_count = $query->result_array()[0]["total"]; // } else { // $this->sys_error_db("m_patient count", $this->db_smartone); // exit; // } // $sql = "select M_PatientID, M_PatientNoReg, // concat(M_TitleName,' ',M_PatientName) M_PatientName, // M_PatientHP, M_PatientDOB, M_PatientAddress,M_PatientNote // from // m_patient join m_title on M_PatientM_TitleID = M_TitleID // $sql_where // limit 0,10"; // $query = $this->db_smartone->query($sql, $sql_param); // $rows = $query->result_array(); // $this->_add_address($rows); // $result = array("total" => $tot_count, "records" => $rows, "sql"=> $this->db_smartone->last_query()); // $this->sys_ok($result); // exit; // } 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 getdefaultdoctor(){ if (! $this->isLogin) { $this->sys_error("Invalid Token"); exit; } $prm = $this->sys_input; $userid = $this->sys_user["M_UserID"]; $sql = "SELECT M_DoctorID as id, concat(IFNULL(M_DoctorPrefix, ''),' ',M_DoctorName, ' ', IFNULL(M_DoctorSufix, '')) as name, '' as address FROM m_doctor WHERE M_DoctorIsDefaultMcu = 'Y' AND M_DoctorIsActive = 'Y' LIMIT 1"; $rows = $this->db_onedev->query($sql)->result_array(); $sql = "SELECT * FROM m_doctoraddress WHERE M_DoctorAddressM_DoctorID = {$rows[0]['id']} AND M_DoctorAddressIsActive = 'Y'"; //echo $sql; $rows[0]['address'] = $this->db_onedev->query($sql)->result(); $result = array( "total" => 1 , "records" => $rows, ); $this->sys_ok($result); exit; } function getdatapatient() { if (! $this->isLogin) { $this->sys_error("Invalid Token"); exit; } //# ambil parameter input $xuserid = $this->sys_user['M_UserID']; $prm = $this->sys_input; $sql = "select M_PatientID, M_PatientNoReg, concat(M_TitleName,' ',M_PatientName) M_PatientName, M_PatientHP, M_PatientDOB, M_PatientNote, concat(M_PatientAddressDescription, '\n\n', m_kelurahanname, ', ', m_districtname, '\n', m_cityname, ', ', m_provincename) as M_PatientAddress, M_PatientNote, M_PatientPhoto from m_patient join m_title on M_PatientM_TitleID = M_TitleID left join m_patientaddress on M_PatientAddressM_PatientID = M_PatientID and M_PatientAddressIsActive = 'Y' AND M_PatientAddressNote = 'Utama' left join m_kelurahan on m_patientaddressm_kelurahanid = m_kelurahanid left join m_district on m_kelurahanm_districtid = m_districtid left join m_city on m_districtm_cityid = m_cityid left join m_province on m_citym_provinceid = m_provinceid join mcu_preregister_details ON Mcu_PreregisterDetailsM_PatientID = M_PatientID AND Mcu_PreregisterDetailsIsActive = 'Y' AND Mcu_PreregisterDetailsIsRegistered = 'N' AND ( Mcu_PreregisterDetailsFlagAction = 'R') AND Mcu_PreregisterDetailsByUserID = {$xuserid} where M_PatientIsActive = 'Y' LIMIT 1"; //echo $sql; $query = $this->db_smartone->query($sql); if ($query) { $rows = $query->row_array(); $result = array("records" => $rows); $this->sys_ok($result); } else { $this->sys_error_db("m_idtype rows",$this->db_smartone); exit; } } }