diff --git a/application/controllers/klinik/Registrationv3.php b/application/controllers/klinik/Registrationv3.php index 5b3faf3d..d37cb895 100644 --- a/application/controllers/klinik/Registrationv3.php +++ b/application/controllers/klinik/Registrationv3.php @@ -874,182 +874,140 @@ class Registrationv3 extends MY_Controller public function search() { - $prm = $this->sys_input; - $max_rst = 100; - $tot_count = 0; - $number_limit = 10; + $number_limit = 10; $number_offset = (!isset($prm['current_page']) ? 1 : $prm['current_page'] - 1) * $number_limit; - $q = [ - 'noreg' => "", - 'name' => '', - 'hp' => '', - 'dob' => '', - 'address' => '', - 'nik' => '' - ]; + $where_noreg = ''; + $where_name = ''; + $where_hp = ''; + $where_dob = ''; + $where_nik = ''; + $where_snorm = ''; - $q_pid = ""; - - if ($prm['noreg'] != '') - $q['noreg'] = "AND M_PatientNoReg like '%{$prm['noreg']}%'"; - - if ($prm['snorm'] == '') { - if ($prm['search'] != '') { - $e = explode('+', $prm['search']); - // nama via trigram bidx - if (!empty($e[0]) && mb_strlen(trim($e[0])) >= 3) { - $toks = $this->ibl_encryptor->query_tokens($e[0]); - $conds = []; - foreach ($toks as $tok) { - $tok_esc = $this->db_onedev->escape_str($tok); - $conds[] = "JSON_CONTAINS(M_PatientName_bidx, '\"$tok_esc\"')"; - } - if ($conds) $q['name'] = 'AND (' . implode(' AND ', $conds) . ')'; - } - // HP via trigram bidx - if (!empty($e[1]) && mb_strlen(trim($e[1])) >= 3) { - $toks = $this->ibl_encryptor->query_tokens($e[1]); - $conds = []; - foreach ($toks as $tok) { - $tok_esc = $this->db_onedev->escape_str($tok); - $conds[] = "JSON_CONTAINS(M_PatientHP_bidx, '\"$tok_esc\"')"; - } - if ($conds) $q['hp'] = 'AND (' . implode(' AND ', $conds) . ')'; - } - // DOB via trigram bidx - if (!empty($e[2]) && mb_strlen(trim($e[2])) >= 3) { - $toks = $this->ibl_encryptor->query_tokens($e[2]); - $conds = []; - foreach ($toks as $tok) { - $tok_esc = $this->db_onedev->escape_str($tok); - $conds[] = "JSON_CONTAINS(M_PatientDOB_bidx, '\"$tok_esc\"')"; - } - if ($conds) $q['dob'] = 'AND (' . implode(' AND ', $conds) . ')'; - } - // Alamat dihapus dari search - // NIK via trigram bidx (e[3] — sebelumnya e[4]) - if (!empty($e[3]) && mb_strlen(trim($e[3])) >= 3) { - $toks = $this->ibl_encryptor->query_tokens($e[3]); - $conds = []; - foreach ($toks as $tok) { - $tok_esc = $this->db_onedev->escape_str($tok); - $conds[] = "JSON_CONTAINS(M_PatientNIK_bidx, '\"$tok_esc\"')"; - } - if ($conds) $q['nik'] = 'AND (' . implode(' AND ', $conds) . ')'; - } - } - } else { - $q_pid = "AND M_PatientNoReg = '{$prm['snorm']}'"; + if (!empty($prm['noreg'])) { + $noreg = $this->db_onedev->escape_like_str($prm['noreg']); + $where_noreg = "AND M_PatientNoReg LIKE '%{$noreg}%'"; } - if ($prm['snorm'] != '' || $prm['search'] != '') { + if (!empty($prm['snorm'])) { + $where_snorm = "AND M_PatientNoReg = '{$prm['snorm']}'"; + } elseif (!empty($prm['search'])) { + $e = explode('+', $prm['search']); - - - - $sql = "SELECT 'N' divider,M_PatientID, - M_PatientNoReg, - M_PatientPrefix, - M_PatientName_enc, M_PatientHP_enc, M_PatientEmail_enc, - M_PatientPOB_enc, M_PatientPhone_enc, M_PatientIDNumber_enc, M_PatientNIK_enc, - M_PatientDOB_enc, M_PatientDOB as dob_raw, - M_PatientSuffix, - M_PatientNote, - M_PatientNIK, - M_PatientJabatan, - M_PatientKedudukan, - M_PatientPJ, - M_PatientLocation, - M_PatientJob, - M_PatientM_SexID, - M_SexName, - M_PatientM_TitleID, - M_TitleName, - M_PatientM_ReligionID, - M_ReligionName, - M_PatientM_IdTypeID, - M_IdTypeName, - M_PatientIDNumber, - M_PatientPhoto, - IF(ISNULL(M_PatientSuspendID),'active','suspend' ) as status, - M_PatientAddressM_KelurahanID M_KelurahanID, - 0 M_DistrictID, 0 M_CityID, 0 M_ProvinceID - FROM m_patient - JOIN m_title ON M_PatientM_TitleID = M_TitleID - JOIN m_sex ON M_PatientM_SexID = M_SexID - JOIN m_branch ON M_BranchIsActive = 'Y' AND M_BranchIsDefault = 'Y' - JOIN m_patientaddress ON M_PatientAddressM_PatientID = M_PatientID AND M_PatientAddressIsActive = 'Y' - LEFT JOIN m_idtype ON M_IdTypeID = M_PatientM_IdTypeID AND M_IdTypeIsActive = 'Y' - LEFT JOIN m_religion ON m_patientm_religionid = m_religionid - LEFT JOIN m_patientsuspend ON M_PatientSuspendM_PatientID = M_PatientID AND M_PatientSuspendIsActive = 'Y' - WHERE - M_PatientSuspendID IS NULL AND M_PatientIsActive = 'Y' - {$q['address']} - {$q['noreg']} - {$q['name']} - {$q['hp']} - {$q['dob']} - {$q['nik']} - {$q_pid} - - GROUP BY M_PatientID - LIMIT $number_limit offset $number_offset"; - //echo $sql; - $query = $this->db_onedev->query($sql); - - if ($query) { - $rows = $query->result_array(); - $enc = $this->ibl_encryptor; - - foreach ($rows as $k => $v) { - $rows[$k]['M_PatientName'] = $enc->decrypt($v['M_PatientName_enc'] ?? '') ?? ''; - $rows[$k]['M_PatientHP'] = $enc->decrypt($v['M_PatientHP_enc'] ?? '') ?? ''; - $rows[$k]['M_PatientEmail'] = $enc->decrypt($v['M_PatientEmail_enc'] ?? '') ?? ''; - $rows[$k]['M_PatientPOB'] = $enc->decrypt($v['M_PatientPOB_enc'] ?? '') ?? ''; - $rows[$k]['M_PatientPhone'] = $enc->decrypt($v['M_PatientPhone_enc'] ?? '') ?? ''; - $rows[$k]['M_PatientIDNumber'] = $enc->decrypt($v['M_PatientIDNumber_enc']?? '') ?? ''; - $rows[$k]['M_PatientNIK'] = $enc->decrypt($v['M_PatientNIK_enc'] ?? '') ?? ''; - $rows[$k]['M_PatientDOB'] = $enc->decrypt($v['M_PatientDOB_enc'] ?? '') ?? date('d-m-Y', strtotime($v['dob_raw'] ?? 'now')); - $rows[$k]['M_PatientAddressDescription'] = $enc->decrypt($v['M_PatientAddressDescription_enc'] ?? '') ?? ''; - foreach (array_keys($rows[$k]) as $col) { - if (substr($col, -4) === '_enc') unset($rows[$k][$col]); - } - unset($rows[$k]['dob_raw']); - $patient_name = str_replace("'", "\\'", $prm['M_PatientName']); - $sql = "SELECT *, concat('{$rows[$k]['M_PatientAddressDescription']}', '\n\n', - m_kelurahanname, ', ', - m_districtname,'\n', - m_cityname, ', ', - m_provincename) as xaddress - FROM m_kelurahan - JOIN m_district ON M_KelurahanM_DistrictID = M_DistrictID - JOIN m_city ON M_DistrictM_CityID = M_CityID - JOIN m_province ON M_CityM_ProvinceID = M_ProvinceID - WHERE - M_KelurahanID = {$v['M_KelurahanID']} "; - //echo $sql; - $row_address = $this->db_onedev->query($sql)->row_array(); - $rows[$k]['M_PatientAddress'] = stripslashes($row_address['xaddress']); - $rows[$k]['M_DistrictID'] = $row_address['M_DistrictID']; - $rows[$k]['M_CityID'] = $row_address['M_CityID']; - $rows[$k]['M_ProvinceID'] = $row_address['M_ProvinceID']; - $info = $this->db_onedev->query("SELECT fn_fo_patient_visit(?) info", [$v['M_PatientID']])->row(); - $rows[$k]['info'] = json_decode($info->info); + if (!empty($e[0]) && mb_strlen(trim($e[0])) >= 3) { + $toks = $this->ibl_encryptor->query_tokens($e[0]); + $conds = []; + foreach ($toks as $tok) { + $tok_esc = $this->db_onedev->escape_str($tok); + $conds[] = "JSON_CONTAINS(M_PatientName_bidx, '\"$tok_esc\"')"; } - - $result = array("total" => $tot_page, "records" => $rows, "sql" => $this->db_onedev->last_query()); - $this->sys_ok($result); - } else { - $this->sys_error_db("m_patient rows", $this->db_onedev); - exit; + if ($conds) $where_name = 'AND (' . implode(' AND ', $conds) . ')'; + } + + if (!empty($e[1]) && mb_strlen(trim($e[1])) >= 3) { + $toks = $this->ibl_encryptor->query_tokens($e[1]); + $conds = []; + foreach ($toks as $tok) { + $tok_esc = $this->db_onedev->escape_str($tok); + $conds[] = "JSON_CONTAINS(M_PatientHP_bidx, '\"$tok_esc\"')"; + } + if ($conds) $where_hp = 'AND (' . implode(' AND ', $conds) . ')'; + } + + if (!empty($e[2]) && mb_strlen(trim($e[2])) >= 3) { + $toks = $this->ibl_encryptor->query_tokens($e[2]); + $conds = []; + foreach ($toks as $tok) { + $tok_esc = $this->db_onedev->escape_str($tok); + $conds[] = "JSON_CONTAINS(M_PatientDOB_bidx, '\"$tok_esc\"')"; + } + if ($conds) $where_dob = 'AND (' . implode(' AND ', $conds) . ')'; + } + + if (!empty($e[3]) && mb_strlen(trim($e[3])) >= 3) { + $toks = $this->ibl_encryptor->query_tokens($e[3]); + $conds = []; + foreach ($toks as $tok) { + $tok_esc = $this->db_onedev->escape_str($tok); + $conds[] = "JSON_CONTAINS(M_PatientNIK_bidx, '\"$tok_esc\"')"; + } + if ($conds) $where_nik = 'AND (' . implode(' AND ', $conds) . ')'; } - } else { - $result = array("total" => 0, "records" => [], "sql" => $this->db_onedev->last_query()); - $this->sys_ok($result); } + + if (empty($prm['snorm']) && empty($prm['search']) && empty($prm['noreg'])) { + $this->sys_ok(["total" => 0, "records" => []]); + return; + } + + $sql = "SELECT 'N' divider, M_PatientID, M_PatientNoReg, M_PatientPrefix, M_PatientSuffix, + concat(M_TitleName,' ',IFNULL(M_PatientPrefix,''),' ',M_PatientName,' ',IFNULL(M_PatientSuffix,'')) M_PatientNameRaw, + M_TitleID, M_TitleName, M_SexID, M_SexName, + M_PatientDOB, + '' M_PatientAddress, + M_PatientAddressID, + M_PatientAddressRegionalCd, M_PatientAddressLocation, M_PatientAddressCity, + M_PatientAddressVillage, M_PatientAddressDistrict, M_PatientAddressState, + M_PatientAddressCountry, M_PatientAddressCountryCode, + M_PatientAddressM_KelurahanID M_KelurahanID, 0 M_DistrictID, 0 M_CityID, 0 M_ProvinceID, + M_PatientM_ReligionID, IFNULL(M_ReligionName, '-') M_ReligionName, + IFNULL(M_PatientNote, '') M_PatientNote, M_PatientPhoto, + M_PatientM_IdTypeID, + M_PatientJabatan, M_PatientKedudukan, M_PatientPJ, M_PatientLocation, M_PatientJob, + IF(ISNULL(M_PatientSuspendID),'active','suspend') as status, + M_PatientName_enc, M_PatientHP_enc, M_PatientDOB_enc, + M_PatientEmail_enc, M_PatientPhone_enc, M_PatientPOB_enc, + M_PatientIDNumber_enc, M_PatientNIK_enc, M_PatientAddressDescription_enc + 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_religion ON M_PatientM_ReligionID = M_ReligionID + LEFT JOIN m_patientsuspend ON M_PatientSuspendM_PatientID = M_PatientID AND M_PatientSuspendIsActive = 'Y' + WHERE M_PatientIsActive = 'Y' AND M_PatientSuspendID IS NULL + {$where_noreg} + {$where_snorm} + {$where_name} + {$where_hp} + {$where_dob} + {$where_nik} + GROUP BY M_PatientID + LIMIT {$number_limit} OFFSET {$number_offset}"; + + $query = $this->db_onedev->query($sql); + + if (!$query) { + $this->sys_error_db("m_patient rows", $this->db_onedev); + return; + } + + $rows = $query->result_array(); + $enc = $this->ibl_encryptor; + + foreach ($rows as $k => $v) { + $rows[$k]['M_PatientName'] = $enc->decrypt($v['M_PatientName_enc']) ?? $v['M_PatientNameRaw']; + $rows[$k]['M_PatientHP'] = $enc->decrypt($v['M_PatientHP_enc']) ?? ''; + $rows[$k]['M_PatientEmail'] = $enc->decrypt($v['M_PatientEmail_enc']) ?? ''; + $rows[$k]['M_PatientPOB'] = $enc->decrypt($v['M_PatientPOB_enc']) ?? ''; + $rows[$k]['M_PatientPhone'] = $enc->decrypt($v['M_PatientPhone_enc']) ?? ''; + $rows[$k]['M_PatientIDNumber'] = $enc->decrypt($v['M_PatientIDNumber_enc']) ?? ''; + $rows[$k]['M_PatientNIK'] = $enc->decrypt($v['M_PatientNIK_enc']) ?? ''; + $rows[$k]['M_PatientDOB'] = $enc->decrypt($v['M_PatientDOB_enc']) ?? date('d-m-Y', strtotime($v['M_PatientDOB'])); + $rows[$k]['M_PatientAddressDescription'] = $enc->decrypt($v['M_PatientAddressDescription_enc']) ?? ''; + $rows[$k]['M_PatientAddress'] = $rows[$k]['M_PatientAddressDescription']; + + foreach (array_keys($rows[$k]) as $col) { + if (substr($col, -4) === '_enc') unset($rows[$k][$col]); + } + unset($rows[$k]['M_PatientNameRaw'], $rows[$k]['M_PatientDOB']); + + $info = $this->db_onedev->query("SELECT fn_fo_patient_visit(?) info", [$v['M_PatientID']])->row(); + $rows[$k]['info'] = json_decode($info->info); + } + + $this->sys_ok(["total" => 0, "records" => $rows]); } function calculateAge($tanggal_lahir)