Initial import
This commit is contained in:
377
application/controllers/cpone/Patient.php
Normal file
377
application/controllers/cpone/Patient.php
Normal file
@@ -0,0 +1,377 @@
|
||||
<?php
|
||||
class Patient extends MY_Controller
|
||||
{
|
||||
var $db;
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
// $this->db = $this->load->database("cpone", true);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
// $cek = $this->db->query("select database() as current_db")->result();
|
||||
// print_r($cek);
|
||||
echo "PATIENT GENERATE ADM API";
|
||||
}
|
||||
|
||||
public function generate_adm_patient()
|
||||
{
|
||||
try {
|
||||
// $this->db->trans_begin();
|
||||
$userid = $this->sys_user["M_UserID"];
|
||||
$prm = $this->sys_input;
|
||||
|
||||
//$date = $prm['date'];
|
||||
|
||||
$sql = "SELECT
|
||||
REG_DATE,
|
||||
REG_NO,
|
||||
PATIENT_ID,
|
||||
PATIENT_NAME,
|
||||
SEX_ID,
|
||||
DOB,
|
||||
AGE,
|
||||
NPK,
|
||||
JOB_TITLE,
|
||||
DEPT,
|
||||
DIVISION,
|
||||
LOCATION,
|
||||
AGREEMENT_NAME,
|
||||
OTHER_ID
|
||||
FROM x_adm_rekap_patient
|
||||
WHERE STAGE = '20'";
|
||||
$qry = $this->db->query($sql);
|
||||
if ($qry) {
|
||||
$rows = $qry->result_array();
|
||||
} else {
|
||||
$this->sys_error_db("select adm rekap patient error", $this->db);
|
||||
}
|
||||
|
||||
// print_r($rows);
|
||||
// exit;
|
||||
$count_m_patient = 1;
|
||||
$insert_query = [];
|
||||
foreach ($rows as $key => $value) {
|
||||
|
||||
// print_r($value['PATIENT_ID']);
|
||||
// exit;
|
||||
$timestamp = strtotime($value['DOB']);
|
||||
$pdob = date('Y-m-d', $timestamp);
|
||||
$p_name = addslashes($value['PATIENT_NAME']);
|
||||
$sql_check = "SELECT
|
||||
M_PatientID,
|
||||
M_PatientOldPID,
|
||||
M_PatientName
|
||||
FROM m_patient
|
||||
WHERE M_PatientIsActive = 'Y'
|
||||
AND
|
||||
(
|
||||
( M_PatientName = '{$p_name}' AND M_PatientDOB = '{$pdob}' AND '{$value['OTHER_ID']}' = '') OR
|
||||
('{$value['NPK']}' <> '' AND M_PatientNIP = '{$value['NPK']}' AND M_PatientDOB = M_PatientDOB = '{$pdob}' ) OR
|
||||
('{$value['OTHER_ID']}' <> '' AND M_PatientDOB = '{$pdob}' AND trim(M_PatientIdentifierValue) = '{$value['OTHER_ID']}')
|
||||
) ";
|
||||
$qry_check = $this->db->query($sql_check);
|
||||
|
||||
|
||||
if ($qry_check) {
|
||||
$rows_check = $qry_check->result_array();
|
||||
} else {
|
||||
/* $this->db->trans_rollback();
|
||||
$this->sys_error_db("select check patient error", $this->db);
|
||||
exit;*/
|
||||
}
|
||||
|
||||
// print_r(count($rows_check));
|
||||
// exit;
|
||||
|
||||
$title_id = 0;
|
||||
$gender = "";
|
||||
if (TRIM($value['SEX_ID']) == 'M') {
|
||||
$title_id = 1;
|
||||
$gender = "male";
|
||||
} else {
|
||||
$title_id = 3;
|
||||
$gender = "female";
|
||||
}
|
||||
|
||||
if (count($rows_check) == 0) {
|
||||
// print_r("insert");
|
||||
// exit;
|
||||
$p_name = addslashes($value['PATIENT_NAME']);
|
||||
$sql_insert = "INSERT INTO m_patient(
|
||||
M_PatientRegisteredByCorporateID,
|
||||
M_PatientOldPID,
|
||||
M_PatientNoReg,
|
||||
M_PatientM_TitleID,
|
||||
M_PatientName,
|
||||
M_PatientGender,
|
||||
M_PatientDOB,
|
||||
M_PatientNIP,
|
||||
M_PatientJob,
|
||||
M_PatientDivisi,
|
||||
M_PatientLocation,
|
||||
M_PatientDepartement,
|
||||
M_PatientCreated,
|
||||
M_PatientLastUpdated
|
||||
) VALUES(
|
||||
50,
|
||||
'{$value['PATIENT_ID']}',
|
||||
`fn_numbering_cpone`('P'),
|
||||
{$title_id},
|
||||
'{$p_name}',
|
||||
'{$gender}',
|
||||
'{$pdob}',
|
||||
'{$value['NPK']}',
|
||||
'{$value['JOB_TITLE']}',
|
||||
'{$value['DIVISION']}',
|
||||
'{$value['LOCATION']}',
|
||||
'{$value['DEPT']}',
|
||||
NOW(),
|
||||
NOW()
|
||||
)";
|
||||
$qry_insert = $this->db->query($sql_insert);
|
||||
$insert_query [] = $this->db->last_query();
|
||||
|
||||
/*if (!$qry_insert) {
|
||||
$this->db->trans_rollback();
|
||||
$this->sys_error_db("insert m_patient error", $this->db);
|
||||
exit;
|
||||
}*/
|
||||
$count_m_patient = $count_m_patient +1;
|
||||
} else {
|
||||
$sql_select = "SELECT X_PatientM_PatientID,
|
||||
X_PatientM_PatientOldPID
|
||||
FROM x_patient
|
||||
WHERE X_PatientM_PatientID = {$rows_check[0]['M_PatientID']}";
|
||||
$qry_select = $this->db->query($sql_select);
|
||||
if ($qry_select) {
|
||||
$rows = $qry_select->result_array();
|
||||
} else {
|
||||
$this->sys_error_db("select x_patient", $this->db);
|
||||
}
|
||||
|
||||
|
||||
if (count($rows) == 0) {
|
||||
$sql = "UPDATE m_patient SET M_PatientOldPID = '{$value['PATIENT_ID']}'
|
||||
WHERE M_PatientID = {$rows_check[0]['M_PatientID']} ";
|
||||
$this->db->query($sql);
|
||||
if ($rows_check[0]['M_PatientOldPID'] == "") {
|
||||
$sql = "INSERT INTO x_patient(
|
||||
X_PatientM_PatientID,
|
||||
X_PatientM_PatientOldPID,
|
||||
X_PatientCreated) VALUES(
|
||||
'{$rows_check[0]['M_PatientID']}',
|
||||
'{$value['PATIENT_ID']}',
|
||||
NOW()
|
||||
)";
|
||||
$qry = $this->db->query($sql);
|
||||
/*if (!$qry) {
|
||||
$this->db->trans_rollback();
|
||||
$this->sys_error_db("insert m_patient error", $this->db);
|
||||
exit;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//$this->db->trans_commit();
|
||||
$result = array(
|
||||
"message" => "Pasien berhasil ditambahkan sebanyak ".$count_m_patient,
|
||||
"affected_rows" => $this->db->affected_rows(),
|
||||
"sql_insert"=> JOIN(";",$insert_query)
|
||||
);
|
||||
|
||||
$this->sys_ok($result);
|
||||
} catch (Exception $exc) {
|
||||
$message = $exc->getMessage();
|
||||
$this->sys_error($message);
|
||||
}
|
||||
}
|
||||
|
||||
/* function patientloop()
|
||||
{
|
||||
$prm = $this->sys_input;
|
||||
$patientoldID = $prm['patientoldID'];
|
||||
$sql = "select *
|
||||
from x_adm_rekap_patient";
|
||||
|
||||
$qry = $this->db->query($sql);
|
||||
|
||||
$rows = $qry->result_array();
|
||||
|
||||
foreach ($rows as $key => $value) {
|
||||
$this->insert_patient($value['PATIENT_ID']);
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
"message" => "Selesai"
|
||||
]);
|
||||
} */
|
||||
|
||||
public function insert_patient()
|
||||
{
|
||||
try {
|
||||
|
||||
//$this->db->trans_begin();
|
||||
|
||||
$sql_adm = "SELECT REG_DATE,
|
||||
REG_NO,
|
||||
PATIENT_ID,
|
||||
PATIENT_NAME,
|
||||
AGE,
|
||||
top_patient.M_PatientID as m_patient_id
|
||||
FROM x_adm_rekap_patient
|
||||
JOIN m_patient as top_patient ON PATIENT_ID = top_patient.M_PatientOldPID
|
||||
WHERE
|
||||
STAGE = '20'";
|
||||
$qry_adm = $this->db->query($sql_adm);
|
||||
//echo $this->db->last_query();
|
||||
if (!$qry_adm) {
|
||||
//$this->db->trans_rollback();
|
||||
echo $this->db->last_query();
|
||||
$this->sys_error_db('select x_adm_rekap_patient error', $this->db);
|
||||
exit;
|
||||
}
|
||||
|
||||
$rows = $qry_adm->result_array();
|
||||
//print_r($rows);
|
||||
// exit;
|
||||
$count_order = 1;
|
||||
foreach ($rows as $key => $row_adm) {
|
||||
$sql_check_order = "SELECT
|
||||
T_OrderHeaderID,
|
||||
T_OrderHeaderDate,
|
||||
T_OrderHeaderLabNumber,
|
||||
T_OrderHeaderOldLabNumber,
|
||||
T_OrderHeaderM_PatientID
|
||||
FROM x_t_orderheader
|
||||
WHERE T_OrderHeaderIsActive = 'Y'
|
||||
AND T_OrderHeaderOldLabNumber = ?";
|
||||
$qry_check_order = $this->db->query($sql_check_order, array(
|
||||
$row_adm['REG_NO']
|
||||
));
|
||||
// echo $this->db->last_query();
|
||||
if ($qry_check_order) {
|
||||
$rows_check_order = $qry_check_order->result_array();
|
||||
} else {
|
||||
//$this->db->trans_rollback();
|
||||
echo $this->db->last_query();
|
||||
$this->sys_error_db("select check t_orderheader", $this->db);
|
||||
exit;
|
||||
}
|
||||
|
||||
$reg_date = $row_adm['REG_DATE'];
|
||||
$full_date = $reg_date . ' 12:00:00';
|
||||
|
||||
if (count($rows_check_order) == 0) {
|
||||
// print_r('belum ada');
|
||||
// exit;
|
||||
|
||||
$sql_insert_order = "INSERT INTO x_t_orderheader(
|
||||
T_OrderHeaderDate,
|
||||
T_OrderHeaderLabNumber,
|
||||
T_OrderHeaderOldLabNumber,
|
||||
T_OrderHeaderM_PatientID,
|
||||
T_OrderHeaderCorporateID,
|
||||
T_OrderHeaderMgm_McuID,
|
||||
T_OrderHeaderM_PatientAge,
|
||||
T_OrderHeaderCreated,
|
||||
T_OrderHeaderLastUpdated) VALUES(
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
now(),
|
||||
now())";
|
||||
$qry_insert_order = $this->db->query($sql_insert_order, array(
|
||||
$full_date,
|
||||
$row_adm['REG_NO'],
|
||||
$row_adm['REG_NO'],
|
||||
$row_adm['m_patient_id'],
|
||||
77,
|
||||
0,
|
||||
$row_adm['AGE']
|
||||
));
|
||||
//echo $this->db->last_query();
|
||||
if (!$qry_insert_order) {
|
||||
//$this->db->trans_rollback();
|
||||
echo $this->db->last_query();
|
||||
$this->sys_error_db("insert t_orderheader error", $this->db);
|
||||
exit;
|
||||
}
|
||||
$count_order = $count_order + 1;
|
||||
$insertid = $this->db->insert_id();
|
||||
|
||||
$sql_select_testid = "SELECT
|
||||
name,
|
||||
name_v,
|
||||
test_id
|
||||
FROM x_map_lab";
|
||||
$qry_select_testid = $this->db->query($sql_select_testid);
|
||||
//echo $this->db->last_query();
|
||||
if ($qry_select_testid) {
|
||||
$rows_test = $qry_select_testid->result_array();
|
||||
} else {
|
||||
//$this->db->trans_rollback;
|
||||
echo $this->db->last_query();
|
||||
$this->sys_error_db("select x_map_lab", $this->db);
|
||||
exit;
|
||||
}
|
||||
|
||||
// print_r($rows_test);
|
||||
// exit;
|
||||
foreach ($rows_test as $key => $value) {
|
||||
// print_r($value);
|
||||
// exit;
|
||||
$sql_insert_detail = "insert into x_t_orderdetail (
|
||||
T_OrderDetailT_OrderHeaderID,
|
||||
T_OrderDetailT_TestID,
|
||||
T_OrderDetailT_TestCode,
|
||||
T_OrderDetailT_TestSasCode,
|
||||
T_OrderDetailT_TestName,
|
||||
T_OrderDetailT_TestIsResult,
|
||||
T_OrderDetailT_TestIsPrice )
|
||||
SELECT
|
||||
?,
|
||||
T_TestID,
|
||||
T_TestCode,
|
||||
T_TestSasCode,
|
||||
T_TestName,
|
||||
T_TestIsResult,
|
||||
T_TestIsPrice
|
||||
FROM t_test
|
||||
WHERE T_TestID in (?)";
|
||||
$qry_insert_detail = $this->db->query($sql_insert_detail, array(
|
||||
$insertid,
|
||||
$value['test_id']
|
||||
));
|
||||
//echo $this->db->last_query();
|
||||
// exit;
|
||||
if (!$qry_insert_detail) {
|
||||
// $this->db->trans_rollback();
|
||||
echo $this->db->last_query();
|
||||
$this->sys_error_db('insert t_orderdetail error', $this->db);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//$this->db->trans_commit();
|
||||
$result = array(
|
||||
"message" => "Berhasil",
|
||||
"affected_rows" => $this->db->affected_rows()
|
||||
);
|
||||
|
||||
$this->sys_ok($result);
|
||||
} catch (Exception $exc) {
|
||||
$message = $exc->getMessage();
|
||||
$this->sys_error($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user