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 = 20; $tot_count =0; $q = [ 'noreg' => "", 'name' => '', 'hp' => '', 'dob' => '', 'address' => '' ]; if ($prm['noreg'] != '') $q['noreg'] = "AND M_PatientNoReg like '%{$prm['noreg']}%'"; if ($prm['search'] != '') { $e = explode('+', $prm['search']); if (isset($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]}%'"; } // QUERY TOTAL // $sql = "select count(distinct m_patientid) total // from // m_patient join m_title on M_PatientM_TitleID = M_TitleID // join m_patientaddress on M_PatientAddressM_PatientID = M_PatientID and M_PatientAddressIsActive = 'Y' // {$q['address']} // where M_PatientIsActive = 'Y' // {$q['noreg']} // {$q['name']} // {$q['hp']} // {$q['dob']} // "; // $query = $this->db_smartone->query($sql); // if ($query) { // $tot_count = $query->result_array()[0]["total"]; // } // else { // $this->sys_error_db("m_patient count",$this->db_smartone); // exit; // } $tot_count = $max_rst; $sql = "SELECT M_PatientID, M_PatientNoReg, concat(M_TitleName,' ',M_PatientName) M_PatientName, M_PatientName M_PatientRealName, M_TitleID, M_TitleName, M_SexID, M_SexName, M_PatientHP, M_PatientPOB, M_PatientDOB, M_PatientNote, concat(M_PatientAddressDescription, '\n\n', m_kelurahanname, ', ', m_districtname, '\n', m_cityname, ', ', m_provincename) as M_PatientAddress, M_PatientAddressDescription, M_PatientM_IdTypeID, M_PatientIDNumber, 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_KelurahanID, M_DistrictID, M_CityID, M_ProvinceID, M_PatientM_ReligionID, IFNULL(M_ReligionName, '-') M_ReligionName 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']} -- and M_PatientAddressID = fn_first_patient_address(M_PatientID) 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 left join m_religion on m_patientm_religionid = m_religionid where M_PatientIsActive = 'Y' {$q['noreg']} {$q['name']} {$q['hp']} {$q['dob']} group by M_PatientID limit 0,{$max_rst}"; $query = $this->db_smartone->query($sql); if ($query) { $rows = $query->result_array(); foreach ($rows as $k => $v) { $info = $this->db_smartone->query("SELECT fn_fo_patient_visit(?) info", [$v['M_PatientID']])->row(); $rows[$k]['info'] = json_decode($info->info); } $result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows), "q" => $this->db_smartone->last_query()); $this->sys_ok($result); } else { $this->sys_error_db("m_patient rows",$this->db_smartone); exit; } } public function search_old() { $prm = $this->sys_input; $max_rst = 20; $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 join m_patientaddress on M_PatientAddressM_PatientID = M_PatientID and M_PatientAddressIsActive = 'Y' where M_PatientNoReg like ? and M_PatientName LIKE ? and ((M_PatientHP LIKE ? and M_PatientHP IS NOT NULL) OR M_PatientHP IS NULL) and ((DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') LIKE ? and M_PatientDOB IS NOT NULL) OR M_PatientDOB IS NULL) and M_PatientAddressDescription LIKE ?"; $query = $this->db_smartone->query($sql, array($q['noreg'], $q['name'], $q['hp'], $q['dob'], $q['address'])); 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_PatientName M_PatientRealName, M_TitleID, M_TitleName, M_SexID, M_SexName, M_PatientHP, M_PatientPOB, M_PatientDOB, M_PatientNote, concat(M_PatientAddressDescription, '\n\n', m_kelurahanname, ', ', m_districtname, '\n', m_cityname, ', ', m_provincename) as M_PatientAddress, M_PatientAddressDescription, M_PatientM_IdTypeID, M_PatientIDNumber, 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_KelurahanID, M_DistrictID, M_CityID, M_ProvinceID, M_PatientM_ReligionID, IFNULL(M_ReligionName, '-') M_ReligionName 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' and M_PatientAddressID = fn_first_patient_address(M_PatientID) 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 left join m_religion on m_patientm_religionid = m_religionid where M_PatientNoReg like ? and M_PatientName LIKE ? and ((M_PatientHP LIKE ? and M_PatientHP IS NOT NULL) OR M_PatientHP IS NULL) and ((DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') LIKE ? and M_PatientDOB IS NOT NULL) OR M_PatientDOB IS NULL) and M_PatientAddressDescription LIKE ? group by M_PatientID limit 0,{$max_rst}"; $query = $this->db_smartone->query($sql, array($q['noreg'], $q['name'], $q['hp'], $q['dob'], $q['address'])); if ($query) { $rows = $query->result_array(); foreach ($rows as $k => $v) $rows[$k]['info'] = json_decode($v['info']); $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; } } function add_new() { $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']; } $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_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'] ]; $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); } function edit() { $prm = $this->sys_input; $prm['M_PatientDOB'] = date('Y-m-d', strtotime($prm['M_PatientDOB'])); $this->db_smartone->set('M_PatientName', $prm['M_PatientName']) ->set('M_PatientM_TitleID', $prm['M_PatientM_TitleID']) ->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']) ->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); $this->db_smartone->query("CALL one_log.log_me('FO', 'FO.PATIENT.EDIT', '{$ptn}', '{$this->sys_user['M_UserID']}')"); // save address // $add = [ // 'M_PatientAddressM_PatientID' => $id, // 'M_PatientAddressDescription' => $prm['M_PatientAddressDescription'], // 'M_PatientAddressM_KelurahanID' => $prm['M_PatientAddressM_KelurahanID'] // ]; $this->db_smartone->set('M_PatientAddressM_KelurahanID', $prm['M_PatientAddressM_KelurahanID']) ->set('M_PatientAddressDescription', $prm['M_PatientAddressDescription']) ->where('M_PatientAddressM_PatientID', $id) ->where('M_PatientAddressNote', 'Utama') ->where('M_PatientAddressIsactive', 'Y') ->update('m_patientaddress'); // LOG FO $add = $this->db_smartone->last_query(); $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; } } }