Files
2026-05-25 20:01:37 +07:00

320 lines
12 KiB
PHP

<?php
class Patient extends MY_Controller
{
var $db_regional;
var $load;
var $db;
public function __construct()
{
parent::__construct();
$this->db_regional = $this->load->database("regional", true);
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userID = $this->sys_user['M_UserID'];
$sql_cek_token = "SELECT M_UserActiveToken
from one_mitra.m_user
WHERE M_UserID = ?
AND M_UserActiveToken IS NOT NULL";
$qry_token = $this->db->query($sql_cek_token, [$userID]);
if (!$qry_token) {
$this->sys_error('Invalid token');
exit;
}
$rows_token = $qry_token->result_array();
if (count($rows_token) == 0) {
$this->sys_error('Invalid token');
exit;
}
}
function search()
{
try {
$prm = $this->sys_input;
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$keyword = '%%';
if (isset($prm['keyword'])) {
$keyword = '%' . $prm['keyword'] . '%';
}
$page = $prm['page'];
$rowPerPage = $prm['rpp'];
$companyID = $prm['company_id'];
// hitung start_offset
$start_offset = 0;
if (isset($prm['page'])) {
if (is_numeric((int)$prm['page']) && $prm['page'] > 0) {
$start_offset = ($page - 1) * intval($rowPerPage);
}
}
$sql_total = "SELECT
COUNT(M_PatientID) AS total
FROM one_mitra.m_patient
WHERE M_PatientIsActive = 'Y'
AND M_PatientM_CompanyID = ?
AND (M_PatientName LIKE ? OR
M_PatientNIK LIKE ? OR M_PatientHP LIKE ?)";
$query_total = $this->db->query($sql_total, [$companyID, $keyword, $keyword, $keyword]);
if (!$query_total) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$totals = $query_total->result_array()[0]['total'];
$sql = "SELECT
M_PatientID AS id,
M_PatientPrefix AS prefix,
M_PatientName AS name,
M_PatientSuffix AS suffix,
M_PatientDOB AS dob,
M_PatientNIK AS nik,
M_PatientNIP AS nip,
M_PatientTitleID AS title_id,
M_PatientM_SexID AS sex_id,
M_PatientHP AS hp,
M_PatientAddress AS address,
M_PatientNoRM AS noRM,
M_PatientJabatan AS jabatan,
M_PatientKedudukan AS kedudukan,
M_PatientLocation AS lokasi,
M_PatientJob AS pekerjaan,
M_PatientM_CompanyID,
one_mitra.fn_get_patient_status_del(M_PatientID) AS status_delete
FROM one_mitra.m_patient
WHERE M_PatientIsActive = 'Y'
AND M_PatientM_CompanyID = ?
AND (M_PatientName LIKE ? OR
M_PatientNIK LIKE ? OR M_PatientHP LIKE ?)
ORDER BY M_PatientName
LIMIT ? OFFSET ?
";
$query = $this->db->query($sql, [$companyID, $keyword, $keyword, $keyword, intval($rowPerPage), intval($start_offset)]);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$search = $query->result_array();
$result = [
"data" => $search,
"total" => $totals,
"total_page" => ceil($totals / $rowPerPage)
];
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function editpatient()
{
try {
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$patient = $prm['patient_data'];
$patientID = $prm['patient_id'];
$companyID = $this->sys_user["M_UserM_CompanyID"];
$patientDOB = date('Y-m-d', strtotime($patient['dob']));
$withoutNIK = $patient['without_nik'];
$nik = $patient['nik'];
$isNIK = 'N';
//JSON BEFORE
$sql = "SELECT * FROM one_mitra.m_patient
WHERE M_PatientID = ?";
$query = $this->db->query($sql, [$patientID]);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$JSONBefore = json_encode($query->result_array()[0]);
if ($withoutNIK == "N") {
$isNIK = 'Y';
}
if ($isNIK == 'Y') {
//sql cek kalau NIK sudah digunakan atau belum
$sql = "SELECT * FROM one_mitra.m_patient
WHERE M_PatientIsNIK = 'Y'
AND M_PatientNIK = ?
AND M_PatientM_CompanyID = ?
AND M_PatientID <> ?";
$query = $this->db->query($sql, [$nik, $companyID, $patientID]);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$cekNik = $query->result_array();
if (count($cekNik) > 0) {
$this->sys_error("NIK sudah digunakan oleh pasien lain");
exit;
}
}
//edit
$sql = "UPDATE one_mitra.m_patient
SET M_PatientPrefix = ?,
M_PatientName = ?,
M_PatientSuffix = ?,
M_PatientDOB = ?,
M_PatientNIK = ?,
M_PatientNIP = ? ,
M_PatientIsNIK = ?,
M_PatientTitleID = ?,
M_PatientM_SexID = ?,
M_PatientHP = ? ,
M_PatientNoRM = ?,
M_PatientJabatan = ?,
M_PatientKedudukan = ?,
M_PatientLocation = ?,
M_PatientJob = ?,
M_PatientAddress = ?
WHERE M_PatientID = ?
AND M_PatientIsActive = 'Y'";
$query = $this->db->query($sql, [
$patient['prefix'],
$patient['name'],
$patient['suffix'],
$patientDOB,
$nik,
$patient['nip'],
$isNIK,
$patient['saluation'],
$patient['gender'],
$patient['hp'],
$patient['noRM'],
$patient['jabatan'],
$patient['kedudukan'],
$patient['lokasi'],
$patient['pekerjaan'],
$patient['address'],
$patientID
]);
if (!$query) {
$message = $this->db->error();
$last_qry = $this->db->last_query();
$this->sys_error_db($message);
exit;
}
//JSON AFTER
$sql = "SELECT * FROM one_mitra.m_patient
WHERE M_PatientID = ?";
$query = $this->db->query($sql, [$patientID]);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$JSONAfter = json_encode($query->result_array()[0]);
//insert log
$sql = "INSERT INTO mitra_log.m_patient_log(
M_PatientLogM_PatientID,
M_PatientLogStatus,
M_PatientLogJSONBefore,
M_PatientLogJSONAfter,
M_patientLogUserID,
M_PatientLogCreated)VALUES(?,'EDIT',?,?,?, NOW())";
$query = $this->db->query($sql, [$patientID, $JSONBefore, $JSONAfter, $userid]);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$this->sys_ok("Berhasil Mengubah data");
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function deletePatient()
{
try {
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//cek bisa di delete atau tidak
$sql = "SELECT one_mitra.fn_get_patient_status_del(?) AS status_delete;";
$query = $this->db->query($sql, [$prm['patient_id']]);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$cek = $query->result_array()[0]['status_delete'];
if ($cek == 'Y') {
//delete
$sql = "UPDATE one_mitra.m_patient
SET M_PatientIsActive = 'N'
WHERE M_PatientID = ?";
$query = $this->db->query($sql, [$prm['patient_id']]);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
//JSON AFTER
$sql = "SELECT * FROM one_mitra.m_patient
WHERE M_PatientID = ?";
$query = $this->db->query(
$sql,
[$prm['patient_id']]
);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$JSONAfter = json_encode($query->result_array()[0]);
//insert log
$sql = "INSERT INTO mitra_log.m_patient_log(
M_PatientLogM_PatientID,
M_PatientLogStatus,
M_PatientLogJSONAfter,
M_patientLogUserID,
M_PatientLogCreated)VALUES(?,'DELETE',?,?, NOW())";
$query = $this->db->query(
$sql,
[$prm['patient_id'], $JSONAfter, $userid]
);
if (!$query) {
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$this->sys_ok("berhasil menghapus data");
} else {
//tidak bisa di delete
$this->sys_error("Gagal Menghapus data, order pasien sudah masuk kedalam surat jalan");
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}