305 lines
12 KiB
Plaintext
305 lines
12 KiB
Plaintext
<?php
|
|
|
|
class Riwayatformv2 extends MY_Controller
|
|
{
|
|
var $db_onedev;
|
|
public function index()
|
|
{
|
|
echo "Riwayat Form API";
|
|
}
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_onedev = $this->load->database("onedev", true);
|
|
}
|
|
|
|
public function get_patient()
|
|
{
|
|
try {
|
|
$prm = $this->sys_input;
|
|
$form_code = isset($prm['form_code']) ? $this->sanitize_form_code($prm['form_code']) : '';
|
|
$uuid = isset($prm['id']) ? $this->sanitize_uuid($prm['id']) : '';
|
|
|
|
if($form_code == '' || $uuid == ''){
|
|
$this->sys_error("Form code atau ID tidak boleh kosong");
|
|
exit;
|
|
}
|
|
|
|
$sql = " SELECT FormRiwayatPasienID as xid,
|
|
M_PatientName as patient_name,
|
|
LOWER(M_SexNameLang) as gender,
|
|
DATE_FORMAT(M_PatientDOB, '%d-%m-%Y') as dob,
|
|
IFNULL(M_PatientPosisi, '-') as posisi,
|
|
IFNULL(M_PatientDivisi, '-') as divisi,
|
|
IFNULL(M_PatientLocation, '-') as location_mcu,
|
|
IFNULL(M_PatientDepartement, '-') as department,
|
|
M_CompanyName as corporate_name,
|
|
M_CompanyAddress as corporate_address,
|
|
IFNULL(T_SamplingSoFlag, 'N') as is_sampling,
|
|
T_OrderHeaderID as t_orderheader_id
|
|
FROM form_riwayat_pasien
|
|
JOIN t_orderheader ON FormRiwayatPasienT_OrderHeaderID = T_OrderHeaderID AND T_OrderHeaderIsActive = 'Y'
|
|
JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID AND M_PatientIsActive = 'Y'
|
|
JOIN m_patientaddress ON M_PatientAddressM_PatientID = M_PatientID AND M_PatientAddressIsActive = 'Y'
|
|
JOIN m_sex ON M_PatientM_SexID = M_SexID
|
|
JOIN m_company ON T_OrderHeaderM_CompanyID = M_CompanyID
|
|
LEFT JOIN t_samplingso ON T_SamplingSoT_OrderHeaderID = FormRiwayatPasienT_OrderHeaderID AND T_SamplingSoIsActive = 'Y' AND T_SamplingSoT_TestID = 2562
|
|
WHERE FormRiwayatPasienCode = ? AND
|
|
FormRiwayatPasienUUID = ? AND
|
|
FormRiwayatPasienIsActive = 'Y'
|
|
";
|
|
$sql_param = array($form_code, $uuid);
|
|
$query = $this->db_onedev->query($sql, $sql_param);
|
|
//echo $this->db_onedev->last_query();
|
|
|
|
if(!$query){
|
|
echo $this->db_onedev->last_query();
|
|
$this->sys_error_db("form_riwayat_pasien error get exist");
|
|
exit;
|
|
}
|
|
$exist_riwayat = $query->result_array();
|
|
|
|
if(count($exist_riwayat) > 0){
|
|
$sql = "";
|
|
$result = array ("total" => 1, "total_filter"=>1,"records" => $exist_riwayat[0]);
|
|
$this->sys_ok($result);
|
|
}else{
|
|
$this->sys_error("Data tidak ditemukan");
|
|
}
|
|
|
|
} catch(Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function get_riwayat()
|
|
{
|
|
try {
|
|
$prm = $this->sys_input;
|
|
$xid = isset($prm['id']) ? $this->sanitize_int_id($prm['id']) : 0;
|
|
$t_orderheader_id = isset($prm['t_orderheader_id']) ? $this->sanitize_int_id($prm['t_orderheader_id']) : 0;
|
|
if($xid == 0 || $xid == ''){
|
|
$this->sys_error("ID tidak boleh kosong");
|
|
exit;
|
|
}
|
|
if($t_orderheader_id == 0 || $t_orderheader_id == ''){
|
|
$this->sys_error("T_OrderHeaderID tidak boleh kosong");
|
|
exit;
|
|
}
|
|
|
|
$results = array();
|
|
$return_data = array();
|
|
|
|
|
|
|
|
$sql = " SELECT FisikTemplateID as fisik_template_id,
|
|
FisikTemplateCode as template_code,
|
|
FisikTemplateTableName as template_table_name,
|
|
FisikTemplateTitle as template_title,
|
|
FormRiwayatPasienTemplateDetails as details,
|
|
FormRiwayatPasienTemplateID as xid
|
|
FROM form_riwayat_pasien
|
|
JOIN form_riwayat_pasien_template ON FormRiwayatPasienTemplateFormRiwayatPasienID = FormRiwayatPasienID AND FormRiwayatPasienTemplateIsActive = 'Y'
|
|
JOIN fisik_template ON FormRiwayatPasienTemplateFisikTemplateID = FisikTemplateID AND FisikTemplateIsActive = 'Y' AND
|
|
(FisikTemplateType = 'Riwayat' OR FisikTemplateType = 'K3')
|
|
WHERE FormRiwayatPasienID = ? AND
|
|
FormRiwayatPasienIsActive = 'Y'
|
|
GROUP BY FisikTemplateID ";
|
|
|
|
$query = $this->db_onedev->query($sql, array($xid));
|
|
|
|
if(!$query){
|
|
$this->sys_error_db("form_riwayat_pasien error get exist");
|
|
exit;
|
|
}
|
|
|
|
$exist_riwayat = $query->result_array();
|
|
if(count($exist_riwayat) > 0){
|
|
foreach($exist_riwayat as $key => $item){
|
|
//$results[$key]['details'] = json_decode($item['details'], true);
|
|
//$results[$key]['xid'] = $item['xid'];
|
|
$return_data[] = json_decode($item['details'], true);
|
|
}
|
|
$result = array ("records" => $return_data);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
|
|
$sql = " SELECT FisikTemplateID as fisik_template_id,
|
|
FisikTemplateCode as template_code,
|
|
FisikTemplateTableName as template_table_name,
|
|
FisikTemplateTitle as template_title,
|
|
FisikTemplateJSON as details
|
|
FROM t_orderheader
|
|
JOIN t_orderdetail ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID AND T_OrderDetailIsActive = 'Y'
|
|
JOIN nat_test ON Nat_TestCode = T_OrderDetailT_TestCode AND Nat_TestIsActive = 'Y'
|
|
JOIN fisik_map_test_template ON Fisik_MapTestTemplateNat_TestID = Nat_TestID AND Fisik_MapTestTemplateIsActive = 'Y'
|
|
JOIN fisik_template_mapping ON Fisik_MapTestTemplateFisikTemplateMappingID = FisikTemplateMappingID AND FisikTemplateMappingIsActive = 'Y'
|
|
JOIN fisik_template_mapping_detail ON FisikTemplateMappingDetailFisikTemplateMappingID = FisikTemplateMappingID AND FisikTemplateMappingDetailIsActive = 'Y'
|
|
JOIN fisik_template ON FisikTemplateID = FisikTemplateMappingDetailFisikTemplateID AND FisikTemplateIsActive = 'Y' AND
|
|
(FisikTemplateType = 'Riwayat' OR FisikTemplateType = 'K3')
|
|
WHERE T_OrderHeaderID = ?
|
|
GROUP BY FisikTemplateID";
|
|
$sql_param = array($t_orderheader_id);
|
|
$query = $this->db_onedev->query($sql, $sql_param);
|
|
if(!$query){
|
|
echo $this->db_onedev->last_query();
|
|
$this->sys_error_db("form_riwayat_pasien error get exist");
|
|
exit;
|
|
}
|
|
|
|
$riwayat_template = $query->result_array();
|
|
|
|
foreach($riwayat_template as $key => $item){
|
|
$sql = " SELECT *
|
|
FROM form_riwayat_pasien_template
|
|
WHERE
|
|
FormRiwayatPasienTemplateFisikTemplateID = ? AND
|
|
FormRiwayatPasienTemplateFormRiwayatPasienID = ? AND
|
|
FormRiwayatPasienTemplateIsActive = 'Y'";
|
|
$sql_param = array($item['fisik_template_id'], $xid);
|
|
$query = $this->db_onedev->query($sql, $sql_param);
|
|
if(!$query){
|
|
$this->sys_error_db("form_riwayat_pasien error get exist");
|
|
exit;
|
|
}
|
|
$exist_riwayat = $query->result_array();
|
|
if(count($exist_riwayat) > 0){
|
|
//$results[$key]['details'] = json_decode($exist_riwayat[0]['details'], true);
|
|
$return_data[] = json_decode($exist_riwayat[0]['details'], true);
|
|
}else{
|
|
$details = json_decode($item['details'], true);
|
|
$sql = "INSERT INTO form_riwayat_pasien_template (
|
|
FormRiwayatPasienTemplateFormRiwayatPasienID,
|
|
FormRiwayatPasienTemplateFisikTemplateID,
|
|
FormRiwayatPasienTemplateDetails,
|
|
FormRiwayatPasienTemplateIsActive,
|
|
FormRiwayatPasienTemplateCreated,
|
|
FormRiwayatPasienTemplateCreatedUserID)
|
|
VALUES (?, ?, ?, 'Y',NOW(),45312)";
|
|
$sql_param = array($xid, $item['fisik_template_id'], json_encode($details));
|
|
$query = $this->db_onedev->query($sql, $sql_param);
|
|
if(!$query){
|
|
$this->sys_error_db("form_riwayat_pasien error save");
|
|
exit;
|
|
}
|
|
//$last_id = $this->db_onedev->insert_id();
|
|
//$results[$key]['xid'] = $last_id;
|
|
$return_data[] = $details;
|
|
}
|
|
}
|
|
|
|
$result = array ("records" => $return_data);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
|
|
} catch(Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
function clean_json_encode($data) {
|
|
return str_replace(
|
|
['\r', '\n'],
|
|
'',
|
|
json_encode($data, JSON_UNESCAPED_SLASHES)
|
|
);
|
|
}
|
|
|
|
private function sanitize_form_code($value) {
|
|
$value = trim((string)$value);
|
|
if ($value === '' || !preg_match('/^[A-Za-z0-9_-]{1,64}$/', $value)) {
|
|
$value = '';
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
private function sanitize_uuid($value) {
|
|
$value = trim((string)$value);
|
|
if ($value === '' || !preg_match('/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/', $value)) {
|
|
$value = '';
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
private function sanitize_int_id($value) {
|
|
if (!is_numeric($value)) {
|
|
$value = 0;
|
|
}
|
|
return (int)$value;
|
|
}
|
|
|
|
public function save_riwayat()
|
|
{
|
|
try {
|
|
$prm = $this->sys_input;
|
|
$xid = isset($prm['id']) ? $this->sanitize_uuid($prm['id']) : '';
|
|
$form_code = isset($prm['form_code']) ? $this->sanitize_form_code($prm['form_code']) : '';
|
|
$data_riwayat = isset($prm['data_riwayat']) && is_array($prm['data_riwayat']) ? $prm['data_riwayat'] : array();
|
|
|
|
if($xid == '' || $form_code == ''){
|
|
$this->sys_error("ID atau form code tidak boleh kosong");
|
|
exit;
|
|
}
|
|
|
|
if(count($data_riwayat) == 0){
|
|
$this->sys_error("Data riwayat tidak boleh kosong");
|
|
exit;
|
|
}
|
|
|
|
$sql = "SELECT * FROM form_riwayat_pasien WHERE FormRiwayatPasienCode = ? AND FormRiwayatPasienUUID = ? AND FormRiwayatPasienIsActive = 'Y'";
|
|
$query = $this->db_onedev->query($sql, array($form_code, $xid));
|
|
if(!$query){
|
|
$this->sys_error_db("error get exist or code form not match");
|
|
exit;
|
|
}
|
|
$exist_form_riwayat = $query->result_array();
|
|
$parent_id = $exist_form_riwayat[0]['FormRiwayatPasienID'];
|
|
|
|
$this->db_onedev->trans_begin();
|
|
|
|
foreach($data_riwayat as $key => $item){
|
|
$riwayat = json_encode($item, JSON_UNESCAPED_SLASHES);
|
|
$sql = "UPDATE form_riwayat_pasien_template
|
|
JOIN fisik_template ON FormRiwayatPasienTemplateFisikTemplateID = FisikTemplateID AND
|
|
FisikTemplateIsActive = 'Y' AND
|
|
FisikTemplateTitle = ?
|
|
SET
|
|
FormRiwayatPasienTemplateDetails = ?,
|
|
FormRiwayatPasienTemplateLastUpdated = NOW(),
|
|
FormRiwayatPasienTemplateLastUpdatedUserID = 45312
|
|
WHERE
|
|
FormRiwayatPasienTemplateFormRiwayatPasienID = ? AND
|
|
FormRiwayatPasienTemplateIsActive = 'Y'
|
|
|
|
";
|
|
$rst = $this->db_onedev->query($sql, array(
|
|
$item['title'],
|
|
$riwayat,
|
|
$parent_id
|
|
));
|
|
if(!$rst){
|
|
echo $this->db_onedev->last_query();
|
|
$this->db_onedev->trans_rollback();
|
|
$this->sys_error_db("form_riwayat_pasien error save");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$this->db_onedev->trans_commit();
|
|
$this->sys_ok("Data berhasil disimpan");
|
|
|
|
} catch(Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|