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); } 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 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' 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), "query" => $this->db_smartone->last_query()); $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'])); $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' => $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); } 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->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_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; } } }