Files
2026-04-15 15:23:57 +07:00

819 lines
33 KiB
PHP

<?php
/*
template function {
$this->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 = 100;
$tot_count =0;
$number_limit = 10;
$number_offset = (!isset($prm['current_page'])?1:$prm['current_page'] - 1) * $number_limit ;
$q = [
'noreg' => "",
'name' => '',
'hp' => '',
'dob' => '',
'address' => '',
'nik' =>''
];
if ($prm['noreg'] != '')
$q['noreg'] = "AND M_PatientNoReg like '%{$prm['noreg']}%'";
if ($prm['search'] != '')
{
$e = explode('+', $prm['search']);
if (isset($e[0])){
$e[0] = str_replace("'", "\\'", $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]}%'";
if (isset($e[4]))
$q['nik'] = "AND M_PatientIDNumber = '{$e[4]}'";
}
$sql = "SELECT 'N' divider,M_PatientID, M_PatientNoReg,M_PatientEmail,M_PatientPrefix,M_PatientSuffix,M_PatientJob,
concat(M_TitleName,' ',IFNULL(M_PatientPrefix,''),' ',M_PatientName,' ',IFNULL(M_PatientSuffix,'')) M_PatientName,
IFNULL(M_IdTypeName,'-') as idtype_name,
M_PatientName M_PatientRealName, M_TitleID, M_TitleName, M_SexID, M_SexName,
M_PatientHP, M_PatientPOB, M_PatientDOB, DATE_FORMAT(M_PatientDOB,'%d-%m-%Y') as dob_ina,
'' M_PatientAddress,
M_PatientAddressDescription, M_PatientM_IdTypeID, M_PatientIDNumber,IFNULL(M_PatientNIK,'') as M_PatientNIK,
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_PatientAddressM_KelurahanID M_KelurahanID, 0 M_DistrictID, 0 M_CityID, 0 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']}
LEFT JOIN m_idtype ON M_IdTypeID = M_PatientM_IdTypeID AND M_IdTypeIsActive = 'Y'
left join m_religion on m_patientm_religionid = m_religionid
where M_PatientIsActive = 'Y'
{$q['noreg']}
{$q['name']}
{$q['hp']}
{$q['dob']}
{$q['nik']}
group by M_PatientID
limit $number_limit offset $number_offset";
//echo $sql;
$query = $this->db_smartone->query($sql);
if ($query) {
$rows = $query->result_array();
foreach ($rows as $k => $v)
{
$rows[$k]['M_PatientName'] = stripslashes($rows[$k]['M_PatientName']);
$rows[$k]['M_PatientAddressDescription'] = stripslashes($v['M_PatientAddressDescription']);
$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_smartone->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_smartone->query("SELECT fn_fo_patient_visit(?) info", [$v['M_PatientID']])->row();
$rows[$k]['info'] = json_decode($info->info);
}
$result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_patient rows",$this->db_smartone);
exit;
}
}
public function search_lama()
{
$prm = $this->sys_input;
$max_rst = 100;
$tot_count =0;
$number_limit = 10;
$number_offset = (!isset($prm['current_page'])?1:$prm['current_page'] - 1) * $number_limit ;
$q = [
'noreg' => "",
'name' => '',
'hp' => '',
'dob' => '',
'address' => ''
];
if ($prm['noreg'] != '')
$q['noreg'] = "AND p_o.M_PatientNoReg like '%{$prm['noreg']}%'";
if ($prm['search'] != '')
{
$e = explode('+', $prm['search']);
if (isset($e[0])){
$e[0] = str_replace("'", "\\'", $e[0]);
$q['name'] = "AND p_o.M_PatientName LIKE '%{$e[0]}%'";
}
if (isset($e[1]))
$q['hp'] = "AND ((p_o.M_PatientHP LIKE '%{$e[1]}%' and p_o.M_PatientHP IS NOT NULL) OR (p_o.M_PatientHP IS NULL AND '{$e[1]}' = ''))";
if (isset($e[2]))
$q['dob'] = "AND ((DATE_FORMAT(p_o.M_PatientDOB, '%d-%m-%Y') LIKE '%{$e[2]}%' and p_o.M_PatientDOB IS NOT NULL) OR (p_o.M_PatientDOB IS NULL AND '{$e[2]}' = ''))";
if (isset($e[3]))
$q['address'] = "AND M_PatientAddressDescription LIKE '%{$e[3]}%'";
}
$sql = "SELECT 'N' divider,p_o.M_PatientID, p_o.M_PatientNoReg,p_o.M_PatientEmail,p_o.M_PatientPrefix,p_o.M_PatientSuffix,p_o.M_PatientJob,
concat(M_TitleName,' ',IFNULL(p_o.M_PatientPrefix,''),' ',p_o.M_PatientName,' ',IFNULL(p_o.M_PatientSuffix,'')) M_PatientName,
p_o.M_PatientName M_PatientRealName, M_TitleID, M_TitleName, M_SexID, M_SexName,
p_o.M_PatientHP, p_o.M_PatientPOB, p_o.M_PatientDOB, DATE_FORMAT(p_o.M_PatientDOB,'%d-%m-%Y') as dob_ina,
'' M_PatientAddress,
M_PatientAddressDescription, p_o.M_PatientM_IdTypeID, p_o.M_PatientIDNumber,
IFNULL(p_o.M_PatientNote, '') M_PatientNote, p_o.M_PatientPhoto, IF(p_o.M_PatientPhone IS NULL OR p_o.M_PatientPhone = '', p_o.M_PatientHP, p_o.M_PatientPhone) hp,
M_PatientAddressM_KelurahanID M_KelurahanID, 0 M_DistrictID, 0 M_CityID, 0 M_ProvinceID, p_o.M_PatientM_ReligionID,
IFNULL(M_ReligionName, '-') M_ReligionName,
'' as hsaddress,
GROUP_CONCAT(CONCAT(HS_AddressLabel,' | ' , HS_AddressAddress)) as alamaths,
IFNULL(p_hs.M_PatientID,0) as patientidhs,
p_o.M_PatientM_TitleID,
p_o.M_PatientM_SexID,
0 as subtotal
FROM
m_patient p_o
join m_title on p_o.M_PatientM_TitleID = M_TitleID
join m_sex on p_o.M_PatientM_SexID = M_SexID
join m_patientaddress on M_PatientAddressM_PatientID = p_o.M_PatientID and M_PatientAddressIsActive = 'Y'
{$q['address']}
left join m_religion on m_patientm_religionid = m_religionid
left join one_hs.m_patient p_hs ON p_hs.M_PatientLocalM_PatientID = p_o.M_PatientID AND p_hs.M_PatientIsLocal = 'Y'
left join one_hs.hs_address ON HS_AddressIsLocal = 'Y' AND HS_AddressM_PatientID = p_hs.M_PatientID AND HS_AddressIsActive = 'Y'
where p_o.M_PatientIsActive = 'Y'
{$q['noreg']}
{$q['name']}
{$q['hp']}
{$q['dob']}
group by p_o.M_PatientID
limit $number_limit offset $number_offset";
// echo $sql;
$query = $this->db_smartone->query($sql);
if ($query) {
$rows = $query->result_array();
foreach ($rows as $k => $v)
{
$rows[$k]['p_o.M_PatientName'] = stripslashes($rows[$k]['p_o.M_PatientName']);
$rows[$k]['M_PatientAddressDescription'] = stripslashes($v['M_PatientAddressDescription']);
$patient_name = str_replace("'", "\\'", $prm['p_o.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_smartone->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_smartone->query("SELECT fn_fo_patient_visit(?) info", [$v['p_o.M_PatientID']])->row();
$rows[$k]['info'] = json_decode($info->info);
$rows[$k]['hsaddress'] = $this->list_hsaddress($v['patientidhs']);
}
$result = array("total" => $tot_page, "records" => $rows, "sql"=> $this->db_smartone->last_query());
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_patient rows",$this->db_smartone);
exit;
}
}
function list_hsaddress($patientid){
$sql = $this->db_smartone->query("SELECT M_BranchID as branchid FROM
m_branch
WHERE M_BranchIsActive = 'Y' AND M_BranchIsDefault = 'Y'")->row();
$branchid = $sql->branchid;
$sql = $this->db_smartone->query("SELECT HS_ConfigLat as branchlat FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$branchlat = $sql->branchlat;
$sql = $this->db_smartone->query("SELECT HS_ConfigLong as branchlong FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$branchlong = $sql->branchlong;
$query =" SELECT hs_address.*,IFNULL(HS_PriceAmount,0) as hsprice,hs_config.*
FROM one_hs.hs_address
LEFT JOIN one_hs.hs_price ON HS_PriceM_BranchID = $branchid AND
( ROUND(one_hs.distance_v2(HS_AddressLat,HS_AddressLng,$branchlat,$branchlong)/1000,1) > HS_PriceMinDistance AND ROUND(one_hs.distance_v2(HS_AddressLat,HS_AddressLng,$branchlat,$branchlong)/1000,1) <= HS_PriceMaxDistance ) AND
HS_PriceIsActive = 'Y'
JOIN one_hs.hs_config ON HS_ConfigIsActive = 'Y'
WHERE HS_AddressIsLocal = 'Y' AND HS_AddressM_PatientID = $patientid AND HS_AddressIsActive = 'Y'";
//echo $query;
$rows = $this->db_smartone->query($query)->result_array();
if(!$rows)
$rows = array();
return $rows;
}
public function search_hsaddress()
{
$prm = $this->sys_input;
$patientid = $prm['patientid'];
$sql = $this->db_smartone->query("SELECT M_BranchID as branchid FROM
m_branch
WHERE M_BranchIsActive = 'Y' AND M_BranchIsDefault = 'Y'")->row();
$branchid = $sql->branchid;
$sql = $this->db_smartone->query("SELECT HS_ConfigLat as branchlat FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$branchlat = $sql->branchlat;
$sql = $this->db_smartone->query("SELECT HS_ConfigLong as branchlong FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'")->row();
$branchlong = $sql->branchlong;
$sql = "SELECT hs_address.*,hs_config.*,IFNULL(HS_PriceAmount,0) as hsprice, IF(HS_AddressIsUtama = 'N','N','Y') as chex
FROM one_hs.hs_address
LEFT JOIN one_hs.hs_price ON HS_PriceM_BranchID = $branchid AND
( ROUND(one_hs.distance_v2(HS_AddressLat,HS_AddressLng,$branchlat,$branchlong)/1000,1) > HS_PriceMinDistance AND ROUND(one_hs.distance_v2(HS_AddressLat,HS_AddressLng,$branchlat,$branchlong)/1000,1) <= HS_PriceMaxDistance ) AND
HS_PriceIsActive = 'Y'
JOIN one_hs.hs_config ON HS_ConfigIsActive = 'Y'
WHERE HS_AddressIsLocal = 'Y' AND HS_AddressM_PatientID = $patientid AND HS_AddressIsActive = 'Y'";
//echo $sql;
$query = $this->db_smartone->query($sql);
if ($query) {
$rows = $query->result_array();
$result = array("total" => count($rows), "records" => $rows, "sql"=> $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()
{
$userid = $this->sys_user["M_UserID"];
$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'];
}
IF(empty($prm['M_PatientM_SexID']) || $prm['M_PatientM_SexID'] == '0'){
$this->sys_error_db("sex id null");
//exit;
}else{
$patient_name = str_replace("'", "\\'", $prm['M_PatientName']);
$ptn = [
'M_PatientName' => $patient_name,
'M_PatientM_TitleID' => $prm['M_PatientM_TitleID'],
'M_PatientPrefix' => $prm['M_PatientPrefix'],
'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_PatientJob' => $prm['M_PatientJob'],
'M_PatientM_IdTypeID' => $M_IdTypeID ,
'M_PatientIDNumber' => $prm['M_PatientIDNumber'],
'M_PatientNote' => $prm['M_PatientNote'],
'M_PatientUserID' => $userid
];
$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')");
$address_description = str_replace("'", "\\'", $prm['M_PatientAddressDescription']);
// save address
$add = [
'M_PatientAddressM_PatientID' => $id,
'M_PatientAddressDescription' => $address_description,
'M_PatientAddressUserID'=> $userid,
'M_PatientAddressM_KelurahanID' => $prm['M_PatientAddressM_KelurahanID']
];
$this->db_smartone->insert('m_patientaddress', $add);
//echo $this->db_smartone->last_query();
// 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 add_new_hs()
{
$userid = $this->sys_user["M_UserID"];
$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'];
}
$patient_name = str_replace("'", "\\'", $prm['M_PatientRealName']);
$address_description = str_replace("'", "\\'", $prm['M_PatientAddressDescription']);
$ptnhs = [
'M_PatientIsLocal' => 'Y',
'M_PatientLocalM_PatientID' => $prm['M_PatientID'],
'M_PatientName' => $patient_name,
'M_PatientM_TitleID' => $prm['M_PatientM_TitleID'],
'M_PatientM_SexID' => $prm['M_PatientM_SexID'],
'M_PatientDOB' => $prm['M_PatientDOB'],
'M_PatientPOB' => $prm['M_PatientPOB'],
'M_PatientHp' => $prm['M_PatientHP'],
'M_PatientEmail' => $prm['M_PatientEmail'],
'M_PatientJob' => $prm['M_PatientJob'],
'M_PatientM_IdTypeID' => $M_IdTypeID ,
'M_PatientIDNumber' => $prm['M_PatientIDNumber'],
'M_PatientUserID' => $userid
];
$this->db_smartone->insert('one_hs.m_patient', $ptnhs);
$err = $this->db_smartone->error();
if ( $err['message'] != "" )
{
$this->sys_error_db("m_patient hs rows", $this->db_smartone);
return;
}
$idhs = $this->db_smartone->insert_id();
$ptnhs = json_encode($ptnhs);
$addhs = [
'M_PatientAddressM_PatientID' => $idhs,
'M_PatientAddressDescription' => $address_description,
'M_PatientAddressUserID'=> $userid,
'M_PatientAddressM_KelurahanID' => $prm['M_KelurahanID']
];
$this->db_smartone->insert('one_hs.m_patientaddress', $addhs);
$addhs = json_encode($addhs);
// INSERT HS
// get
$r = $this->db_smartone->where('M_PatientID', $id)
->get('m_patient')
->row();
$rst = array("id" => $id,"idhs" => $idhs, 'noreg'=>$r->M_PatientNoReg);
$this->sys_ok($rst);
}
function edit()
{
$prm = $this->sys_input;
$userid = $this->sys_user["M_UserID"];
$prm['M_PatientDOB'] = date('Y-m-d', strtotime($prm['M_PatientDOB']));
$patient_name = str_replace("'", "\\'", $prm['M_PatientName']);
$this->db_smartone->set('M_PatientName', $patient_name)
->set('M_PatientM_TitleID', $prm['M_PatientM_TitleID'])
->set('M_PatientPrefix', $prm['M_PatientPrefix'])
->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_PatientJob', $prm['M_PatientJob'])
->set('M_PatientM_IdTypeID', $prm['M_PatientM_IdTypeID'])
->set('M_PatientIDNumber', $prm['M_PatientIDNumber'])
->set('M_PatientNote', $prm['M_PatientNote'])
->set('M_PatientUserID', $userid)
->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']}')");
$sql = "SELECT * FROM m_patientaddress
WHERE
M_PatientAddressM_PatientID = {$id} AND
M_PatientAddressIsactive = 'Y'
ORDER BY M_PatientAddressID ASC
LIMIT 1";
$id_address = $this->db_smartone->query($sql)->row()->M_PatientAddressID;
$address_description = str_replace("'", "\\'", $prm['M_PatientAddressDescription']);
$this->db_smartone->set('M_PatientAddressM_KelurahanID', $prm['M_PatientAddressM_KelurahanID'])
->set('M_PatientAddressDescription', $address_description )
->set('M_PatientAddressUserID', $userid )
->where('M_PatientAddressID', $id_address)
->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 save_hsaddress()
{
try {
//# cek token valid
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
//# ambil parameter input
$prm = $this->sys_input;
$patientid = $prm['patientid'];
$hslabel = $prm['label'];
$hsaddress = $prm['address'];
$hsdetailaddress = $prm['detail'];
$hsisutama = $prm['isutama'];
$lat = $prm['lat'];
$lng = $prm['long'];
$userid = $this->sys_user["M_UserID"];
$sql = "insert into one_hs.hs_address(
HS_AddressM_PatientID,
HS_AddressLabel,
HS_AddressAddress,
HS_AddressDetails,
HS_AddressLat,
HS_AddressLng,
HS_AddressIsUtama,
HS_AddressIsLocal,
HS_AddressUserID,
HS_AddressCreated,
HS_AddressLastUpdated
)
values( ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now())";
$query = $this->db_onedev->query($sql,
array(
$patientid,
$hslabel,
$hsaddress,
$hsdetailaddress,
$lat,
$lng,
$hsisutama,
'Y',
$userid
)
);
// echo $this->db_onedev->last_query();
if (!$query) {
$this->sys_error_db("hs address insert");
exit;
}
$last_id = $this->db_onedev->insert_id();
if ($hsisutama == "Y" ) {
$sql = "update one_hs.hs_address set HS_AddressIsUtama= 'N'
where HS_AddressM_PatientID = '{$patientid}'
AND HS_AddressID <> '{$last_id}'";
$this->db_onedev->query($sql,array($last_id));
}
$result = array ("total" => 1, "records" => array("xid" => 0));
$this->sys_ok($result);
} catch(Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
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;
}
}
public function gettimekuota()
{
$prm = $this->sys_input;
$date = $prm['sdate'];
$nameOfDay = date('D', strtotime($date));
$sqlday = $this->db_smartone->query("SELECT
CASE
WHEN '{$nameOfDay}' = 'Mon' THEN 1
WHEN '{$nameOfDay}' = 'Tue' THEN 2
WHEN '{$nameOfDay}' = 'Wed' THEN 3
WHEN '{$nameOfDay}' = 'Thu' THEN 4
WHEN '{$nameOfDay}' = 'Fri' THEN 5
WHEN '{$nameOfDay}' = 'Sat' THEN 6
WHEN '{$nameOfDay}' = 'Sun' THEN 7
ELSE
0
END as dayid")->row();
$dayid = $sqlday->dayid;
$sql = "SELECT M_RegTimeID,M_RegTimeName,CONCAT(M_RegTimeName,' | Kuota : ',HS_ScheduleLimit - IFNULL(one_hs.fn_get_kuota_schedule('{$date}',M_RegTimeName),0)) as descp, HS_ScheduleLimit, IFNULL(one_hs.fn_get_kuota_schedule('{$date}',M_RegTimeName),0) as transaksi,HS_ScheduleLimit - IFNULL(one_hs.fn_get_kuota_schedule('{$date}',M_RegTimeName),0) as kuota,
IF(CONCAT('{$date}',' ',M_RegTimeName) < DATE_FORMAT(now(), '%Y-%m-%d %H:%i') OR (HS_ScheduleLimit - IFNULL(one_hs.fn_get_kuota_schedule('{$date}',M_RegTimeName),0)) = 0,'true','false') as xdisable
FROM one_hs.hs_schedule
JOIN one_hs.hs_scheduledetail ON HS_ScheduleDetailHS_ScheduleID = HS_ScheduleID AND HS_ScheduleDetailIsActive = 'Y'
JOIN m_reg_time ON M_RegTimeID = HS_ScheduleDetailM_RegTimeID
WHERE HS_ScheduleM_RegDayID = $dayid
ORDER BY M_RegTimeID 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;
}
}
public function getconfig()
{
$prm = $this->sys_input;
$sql = "SELECT * FROM
one_hs.hs_config
WHERE HS_ConfigIsActive = 'Y'";
$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;
}
}
}