Files
BE_IBL/application/controllers/klinik/cashierklinik/Patient.php
2026-06-12 13:50:52 +07:00

231 lines
6.8 KiB
PHP

<?php
class Patient extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Patient API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
$this->load->library('ibl_encryptor');
}
public function add_notes($orderid){
$sql = " SELECT PaymentOrderID as note_order_id,
PaymentID as note_id,
PaymentDate as note_date,
PaymentNumber as note_number,
GROUP_CONCAT(M_PaymentTypeName separator ' , ') as paymenttypes_name,
SUM(PaymentDetailAmount) as note_amount,
M_UserUsername as note_user,
PaymentDetailIsActive as note_active
FROM one_klinik.`payment`
JOIN one_klinik.`paymentdetail` ON PaymentDetailPaymentID = PaymentID
JOIN `m_paymenttype` ON PaymentDetailM_PaymentTypeID = M_PaymentTypeID
LEFT JOIN `m_user` ON PaymentDetailUserID = M_UserID
WHERE
PaymentOrderID = {$orderid}
GROUP BY PaymentID";
$query = $this->db_onedev->query($sql);
//echo $sql;
if ($query) {
$rows = $query->result_array();
return $rows;
} else {
$this->sys_error_db("get notes", $this->db_onedev);
exit;
}
}
public function search()
{
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$startdate = $prm['startdate'];
$enddate = $prm['enddate'] . " 23:59:59";
$search = $prm["search"];
$status = $prm["status"];
/*$company = $prm["company"];
$filter_company = '';
if($company != '0' || $company != 0)
$filter_company = " AND M_CompanyID = {$company}";
*/
$number_limit = 10;
$number_offset = ($prm['current_page'] - 1) * $number_limit;
$where = " ( DATE(orderDate) BETWEEN '{$startdate}' AND '{$enddate}' ) AND ";
if ($search != '') {
if (strlen($search) == 11) {
$where = "orderNumber = '{$search}' AND ";
} else {
$tokens = $this->ibl_encryptor->query_tokens($search);
if ($tokens) {
$bidx_conds = implode(' AND ', array_map(function($h) {
return "JSON_CONTAINS(M_PatientName_bidx, '\"$h\"')";
}, $tokens));
$where = "( orderNumber LIKE '%{$search}%' OR ({$bidx_conds}) ) AND ";
} else {
$where = "orderNumber LIKE '%{$search}%' AND ";
}
}
}
$sql = "SELECT count(*) as total
FROM one_klinik.`order`
JOIN m_patient ON orderM_PatientID = M_PatientID
JOIN m_title ON M_PatientM_TitleID = M_TitleID
JOIN m_sex ON M_PatientM_SexID = M_SexID
WHERE
$where
( ('{$status}' = 'N' AND orderIsLunas = 'N') OR ('{$status}' = 'Y' AND orderIsLunas = 'Y') )";
$query = $this->db_onedev->query($sql);
$tot_count = 0;
$tot_page = 0;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
$tot_page = ceil($tot_count / $number_limit);
} else {
$this->sys_error_db("patient count", $this->db_onedev);
exit;
}
$sql = "SELECT orderID,
orderDate,
orderNumber,
orderM_PatientID,
M_PatientNoReg,
orderKeluhan,
DATE_FORMAT(orderDate,'%d-%m-%Y %H:%i') as order_date,
M_PatientName_enc,
M_PatientName AS patient_name_masked,
M_PatientPrefix, M_PatientSuffix,
M_TitleName, M_TitleLangName,
orderTotal as totalbill,
0 as paid,
0 as unpaid,
orderIsLunas as flaglunas,
'' as notes,
100 as mindp_percent,
settingPriceDefault as mindp_amount,
0 as F_BillDetailID
FROM one_klinik.`order`
JOIN m_patient ON orderM_PatientID = M_PatientID
JOIN m_title ON M_PatientM_TitleID = M_TitleID
JOIN m_sex ON M_PatientM_SexID = M_SexID
JOIN one_klinik.`setting` ON settingIsActive = 'Y'
WHERE
$where
( ('{$status}' = 'N' AND orderIsLunas = 'N') OR ('{$status}' = 'Y' AND orderIsLunas = 'Y') )
GROUP BY orderID
ORDER BY orderID ASC
LIMIT $number_limit OFFSET $number_offset";
$query = $this->db_onedev->query($sql);
if (!$query) {
$this->sys_error_db("patient rows", $this->db_onedev);
exit;
}
$rows = $query->result_array();
$enc = $this->ibl_encryptor;
if ($rows) {
foreach ($rows as $k => $v) {
$p_name = $enc->decrypt($v['M_PatientName_enc'] ?? '') ?: $v['patient_name_masked'];
$title = $v['M_TitleName'] ? $v['M_TitleName'] . '. ' : '';
$title_e = $v['M_TitleLangName'] ? $v['M_TitleLangName'] . '. ': '';
$prefix = $v['M_PatientPrefix'] ? $v['M_PatientPrefix'] . ' ' : '';
$suffix = $v['M_PatientSuffix'] ? ' ' . $v['M_PatientSuffix'] : '';
$rows[$k]['M_PatientName'] = trim($title . $prefix . $p_name . $suffix);
$rows[$k]['M_PatientName_eng'] = trim($title_e . $prefix . $p_name . $suffix);
unset($rows[$k]['M_PatientName_enc'], $rows[$k]['patient_name_masked'],
$rows[$k]['M_PatientPrefix'], $rows[$k]['M_PatientSuffix'],
$rows[$k]['M_TitleLangName']);
$data_payment = $this->db_onedev->query(
"SELECT IFNULL(SUM(PaymentTotal),0) as total FROM one_klinik.payment
WHERE PaymentOrderID = ? AND PaymentIsActive = 'Y'",
[$v['orderID']]
)->row();
$rows[$k]['unpaid'] = $v['totalbill'] - $data_payment->total;
$rows[$k]['paid'] = $data_payment->total;
$rows[$k]['notes'] = $this->add_notes($v['orderID']);
}
}
$result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_onedev->last_query());
$this->sys_ok($result);
exit;
}
function searchcompany(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM m_company
WHERE
M_CompanyName like ?
AND M_CompanyIsActive = 'Y'";
$query = $this->db_onedev->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_onedev);
exit;
}
$rows = array('id'=>0,'name'=>'Semua');
$sql = "
SELECT M_CompanyID as id, M_CompanyName as name
FROM m_company
WHERE
M_CompanyName like ?
AND M_CompanyIsActive = 'Y'
ORDER BY M_CompanyName DESC
";
$query = $this->db_onedev->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
array_push($rows,array('id'=>0,'name'=>'Semua'));
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_company rows",$this->db_onedev);
exit;
}
}
}