462 lines
18 KiB
PHP
462 lines
18 KiB
PHP
<?php
|
|
class Order extends MY_Controller
|
|
{
|
|
function __construct() {
|
|
parent::__construct();
|
|
$this->db = $this->load->database("onedev", true);
|
|
}
|
|
function ref_order($incomingRefID) {
|
|
$adminUserID = 3;
|
|
$this->sys_user["M_UserID"] = $adminUserID;
|
|
|
|
$sql = "select * from incoming_ref where incomingRefID = ?";
|
|
$qry = $this->db->query($sql, array($incomingRefID) );
|
|
$rows = array();
|
|
$branchID = 0;
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
if ( count($rows) > 0 ) $branchID = $rows[0]["incomingRefM_BranchID"];
|
|
}
|
|
|
|
$companyID = 0;
|
|
if ($branchID > 0 ) {
|
|
$sql = "select *
|
|
from m_branch where M_BranchID = ?";
|
|
$qry = $this->db->query($sql, array($branchID) );
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
$companyID = $rows[0]["M_BranchM_CompanyID"];
|
|
$mouID = $rows[0]["M_BranchM_MouID"];
|
|
$doctorID = $rows[0]["M_BranchM_DoctorID"];
|
|
$doctorAddressID = $rows[0]["M_BranchM_DoctorAddressID"];
|
|
$branchKelurahanID= $rows[0]["M_BranchM_KelurahanID"];
|
|
$branchAddresss = $rows[0]["M_BranchAddress"];
|
|
}
|
|
}
|
|
if ($companyID == 0 ) {
|
|
$this->sys_error_db("Company / MOU belum di set");
|
|
exit;
|
|
}
|
|
$sql = "select *
|
|
from incoming_ref_detail
|
|
where incomingRefDetailIncomingRefID = ?
|
|
order by incomingRefDetailT_OrderHeaderID";
|
|
$qry = $this->db->query($sql, array($incomingRefID) );
|
|
$orders = array();
|
|
$ax_test = array();
|
|
$a_promise = array();
|
|
$a_barcode = array();
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
$prev_oh_id = 0;
|
|
foreach($rows as $r) {
|
|
$oh_id = $r["incomingRefDetailT_OrderHeaderID"];
|
|
if ( ! isset($orders[$oh_id]) ) {
|
|
$orders[$oh_id]["patient"] = json_decode($r["incomingRefDetailPatient"],true);
|
|
$orders[$oh_id]["foNote"] = $r["incomingRefDetailFoNote"];
|
|
$orders[$oh_id]["verificationNote"] = $r["incomingRefDetailVefificationNote"];
|
|
$orders[$oh_id]["samplingNote"] = $r["incomingRefDetailSamplingNote"];
|
|
$orders[$oh_id]["LabNumber"] = $r["incomingRefDetailT_OrderHeaderLabNumber"];
|
|
$orders[$oh_id]["test"] = array();
|
|
}
|
|
$orders[$oh_id]["test"][] = array (
|
|
"T_TestID" => $r["incomingRefDetailT_TestID"],
|
|
"Promise" => $r["incomingRefDetailT_OrderPromiseDateTime"],
|
|
"T_OrderDetailID" => $r["incomingRefDetailT_OrderDetailID"],
|
|
"T_BarcodeLabBarcode" => $r["incomingRefDetailT_BarcodeLabBarcode"],
|
|
"Requirements" => json_decode($r["incomingRefDetailRequirements"],true)
|
|
);
|
|
$ax_test[] = $r["incomingRefDetailT_TestID"];
|
|
$a_promise[$oh_id][$r["incomingRefDetailT_TestID"]] = $r["incomingRefDetailT_OrderPromiseDateTime"];
|
|
$a_barcode[$oh_id][$r["incomingRefDetailT_TestID"]] = $r["incomingRefDetailT_BarcodeLabBarcode"];
|
|
}
|
|
}
|
|
$s_test = "0," . join(",",$ax_test);
|
|
$sql = "select * from ss_price_mou
|
|
where Ss_PriceMouM_MouID = ? and T_TestID in ( $s_test ) ";
|
|
$qry = $this->db->query($sql, array($mouID) );
|
|
$a_test = array();
|
|
if ($qry ) {
|
|
$rows = $qry->result_array();
|
|
foreach($rows as $r) {
|
|
$testID = $r["T_TestID"];
|
|
$a_test[$testID] = array(
|
|
"T_PriceAmount" => $r["T_PriceAmount"],
|
|
"T_PriceDiscRp" => $r["T_PriceDiscRp"],
|
|
"T_PriceDisc" => $r["T_PriceDisc"]
|
|
);
|
|
}
|
|
}
|
|
$message = "";
|
|
foreach($orders as $oh_id => $o){
|
|
$noreg = $o["patient"]["M_PatientNoReg"];
|
|
$lab_number= $o["LabNumber"];
|
|
//if noreg not exists create patient
|
|
$sql = "select count(*) tot from m_patient where M_PatientNoReg=? and M_PatientIsActive = 'Y'";
|
|
$qry = $this->db->query($sql, array($noreg));
|
|
$patientID = $orders[$oh_id]["patient"]["M_PatientID"];
|
|
if($qry) {
|
|
$rows = $qry->result_array();
|
|
if ($rows[0]["tot"] == 0 ) {
|
|
$patientID = $this->create_patient($orders[$oh_id]["patient"]);
|
|
}
|
|
}
|
|
$sql = "select fn_global_age_count(M_PatientDOB,now()) age
|
|
from m_patient where M_PatientID = ? ";
|
|
$qry = $this->db->query($sql, array($patientID) );
|
|
$patientAge = "";
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
if (count($rows) > 0 ) {
|
|
$patientAge = $rows[0]["age"];
|
|
}
|
|
}
|
|
//echo "1. $lab_number : $patientID \n";
|
|
//register order
|
|
$xheader = array(
|
|
'patient_id' => $patientID,
|
|
'age' => $patientAge,
|
|
'sender_doctor_id' => $doctorID,
|
|
'sender_address_id' => $doctorAddressID,
|
|
'company_id' => $companyID,
|
|
'mou_id' => $mouID,
|
|
'lang_id' => '1',
|
|
'lang_si' => 'N',
|
|
'doctor_note' => '',
|
|
'fo_note' => $orders[$oh_id]["foNote"],
|
|
'queue' => '',
|
|
'received_sample' => 'Y',
|
|
'lang_id_2' => '2',
|
|
'lang_si_2' => 'N',
|
|
);
|
|
$xdel = array(
|
|
array(
|
|
'address_id' => $branchID,
|
|
'delivery_id' => '10',
|
|
'delivery_type_id' => '2',
|
|
'senderdoctorid' => $doctorID,
|
|
'senderaddressid' => $doctorAddressID,
|
|
'note' => $branchAddresss,
|
|
'kelurahan' => $branchKelurahanID
|
|
));
|
|
$xdet = array();
|
|
$reqs = array();
|
|
foreach($orders[$oh_id]["test"] as $t ) {
|
|
$v = $a_test[$t["T_TestID" ]];
|
|
$xdet[] = array(
|
|
't_id' => $t['T_TestID'],
|
|
't_cito' => 'N',
|
|
't_price' => $v['T_PriceAmount'],
|
|
't_disc' => $v['T_PriceDisc'],
|
|
't_discrp' => $v['T_PriceDiscRp'],
|
|
't_req' => 'Y',
|
|
't_reqnote' => '',
|
|
't_ispacket' => "N",
|
|
't_packettype' => "PX",
|
|
't_packetid' => '0'
|
|
);
|
|
if (count($t["Requirements"]) > 0 ) {
|
|
$reqs = array_merge($reqs, $t["Requirements"]);
|
|
}
|
|
}
|
|
//requirements
|
|
$xreq = array(
|
|
'status' => 'Y',
|
|
'reqs' => '[]'
|
|
);
|
|
$header_json = json_encode($xheader);
|
|
$delivery_json = json_encode($xdel);
|
|
$detail_json = json_encode($xdet);
|
|
$req_json = json_encode($xreq);
|
|
$sql = "CALL `sp_fo_register_save_v5`(
|
|
0,
|
|
'{$header_json}',
|
|
'{$delivery_json}',
|
|
'{$detail_json}',
|
|
'{$req_json}',
|
|
'{$userid}'
|
|
);";
|
|
$r = $this->db->query($sql)->row();
|
|
$this->clean_mysqli_connection($this->db->conn_id);
|
|
if ($r->status != "OK" ) {
|
|
print_r($r);
|
|
exit;
|
|
}
|
|
$data = json_decode($r->data,true);
|
|
$new_oh_id = $data["id"];
|
|
$new_lab_number= $data["number"];
|
|
|
|
//echo "2. $new_oh_id : $lab_number => $new_lab_number \n";
|
|
|
|
//update incoming_ref
|
|
$sql = "update incoming_ref_detail set incomingRefDetailNewT_OrderHeaderID=?,
|
|
incomingRefDetailNewM_PatientID = ?
|
|
where incomingRefDetailIncomingRefID = ?
|
|
and incomingRefDetailT_OrderHeaderID=?";
|
|
$this->db->query($sql,array($new_oh_id, $patientID, $incomingRefID, $oh_id));
|
|
|
|
$sql = "update t_orderheaderaddon set T_OrderHeaderAddOnLabNumberOrigin=?
|
|
where T_OrderHeaderAddOnT_OrderHeaderID = ?";
|
|
$this->db->query($sql,array($lab_number,$new_oh_id));
|
|
|
|
//update promise
|
|
$a_promise_test = array();
|
|
$a_promise_date= array();
|
|
$a_promise_id= array();
|
|
foreach($a_promise[$oh_id] as $testID => $promise ) {
|
|
if(!isset($a_promise_date[$promise])) {
|
|
$sql = "insert into t_orderpromise(T_OrderPromiseT_OrderHeaderID, T_OrderPromiseDateTime)
|
|
values(?,?)";
|
|
$this->db->query($sql, array($new_oh_id,$promise));
|
|
$promise_id = $this->db->insert_id();
|
|
$a_promise_test[$testID] = $promise_id;
|
|
$a_promise_date["$promise"] = $promise_id;
|
|
} else {
|
|
$a_promise_test[$testID] = $a_promise_date["$promise"];
|
|
}
|
|
}
|
|
if ( count($a_promise_test) > 0 ) {
|
|
$s_test = join(",", array_keys($a_promise_test));
|
|
$sql = "update t_orderpromise,t_orderdetail
|
|
set T_OrderPromiseIsActive = 'N'
|
|
where T_OrderPromiseID = T_OrderDetailT_OrderPromiseID
|
|
and T_OrderDetailT_OrderHeaderID = ?
|
|
and T_OrderDetailT_TestID in ($s_test) ";
|
|
$this->db->query($sql, array($new_oh_id));
|
|
}
|
|
foreach($a_promise_test as $testID => $promiseID ) {
|
|
$sql = "update t_orderdetail
|
|
set T_OrderDetailT_OrderPromiseID= ?
|
|
where T_OrderDetailT_OrderHeaderID = ?
|
|
and T_OrderDetailT_TestID = ? ";
|
|
$this->db->query($sql, array($promiseID , $new_oh_id, $testID) );
|
|
}
|
|
//insert requirements
|
|
foreach($reqs as $r) {
|
|
$sql ="insert into t_orderreq(T_OrderReqT_OrderHeaderID,T_OrderReqNat_PositionID,
|
|
T_OrderReqStatus, T_OrderReqT_TestID, T_OrderReqs)
|
|
values(?,?, ?,?,?)";
|
|
$this->db->query($sql, array($new_oh_id, $r["T_OrderReqNat_PositionID"],
|
|
$r["T_OrderReqStatus"], $r["T_OrderReqT_TestID"], $r["T_OrderReqs"] ));
|
|
}
|
|
//update note
|
|
$sql = "update t_orderheader
|
|
set T_OrderHeaderFoNote = ? , T_OrderHeaderFoNoteM_UserID = ?,
|
|
T_OrderHeaderSamplingNote = ?, T_OrderHeaderSamplingNoteM_UserID = ?,
|
|
T_OrderHeaderVerificationNote = ?, T_OrderHeaderVerificationNoteM_UserID = ?
|
|
where T_OrderHeaderID = ?
|
|
";
|
|
$this->db->query($sql, array( $o["foNote"],$adminUserID,
|
|
$o["samplingNote"], $adminUserID, $o["verificationNote"], $adminUserID,
|
|
$new_oh_id));
|
|
$result = $this->fo_verify($new_oh_id,$adminUserID);
|
|
|
|
//update t_barcodelab
|
|
foreach($a_barcode[$oh_id] as $testID => $barcode) {
|
|
$sql = "update t_barcodelab,t_test
|
|
set T_BarcodeLabBarcodeOrigin =?
|
|
where T_BarcodeLabT_OrderHeaderID = ?
|
|
and T_TestID = ?
|
|
and T_TestIsActive = 'Y' and T_BarcodeLabT_SampleTypeID = T_TestT_SampleTypeID";
|
|
$this->db->query($sql, array($barcode, $new_oh_id, $testID));
|
|
}
|
|
// update nilai normal
|
|
$sql = "select distinct T_OrderDetailT_OrderHeaderID id,
|
|
T_TestT_SampleTypeID sid
|
|
from t_orderdetail
|
|
join t_test on T_OrderDetailT_OrderHeaderID = ?
|
|
and T_OrderDetailIsActive = 'Y' and
|
|
T_OrderDetailT_TestID = T_TestID
|
|
and T_TestIsActive = 'Y'
|
|
";
|
|
$qry = $this->db->query($sql, array($new_oh_id));
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
foreach($rows as $r) {
|
|
$sql = "call sp_sampling_set_normal(?,?)";
|
|
$this->db->query($sql,array($r["id"],$r["sid"]));
|
|
}
|
|
}
|
|
|
|
$message .= "$lab_number => $new_lab_number [ Fo Verify : $result ] \n";
|
|
}
|
|
echo $message;
|
|
}
|
|
function create_patient($p) {
|
|
$sql = "insert into m_patient";
|
|
$userID = 3;
|
|
//check title
|
|
$sql = "select M_TitleID from m_title where M_TitleName = ? and M_TitleIsActive = 'Y'";
|
|
$qry = $this->db->query($sql, array($p["title"]["M_TitleName"]));
|
|
$titleID = 0;
|
|
if ($qry) {
|
|
$rows = $qry->result_array();
|
|
if (count($rows) > 0 ) $titleID = $rows[0]["M_TitleID"];
|
|
}
|
|
if ($titleID == 0) {
|
|
$sql = "insert into m_title(M_TitleName, M_TitleM_SexID) values ( ? , ? )";
|
|
$qry = $this->db->query($sql, array($p["title"]["M_TitleName"], $p["title"]["M_TitleM_SexID"]));
|
|
if ($qry) {
|
|
$titleID = $this->db->insert_id();
|
|
}
|
|
}
|
|
if ($titleID == 0 ) {
|
|
$this->sys_error_db("Invalid Title");
|
|
exit;
|
|
}
|
|
$pdob = date('Y-m-d',strtotime($p['M_PatientDOB']));
|
|
$query ="INSERT INTO m_patient (
|
|
M_PatientM_TitleID,
|
|
M_PatientName,
|
|
M_PatientDOB,
|
|
M_PatientM_SexID,
|
|
M_PatientM_ReligionID,
|
|
M_PatientEmail,
|
|
M_PatientPOB,
|
|
M_PatientHP,
|
|
M_PatientPhone,
|
|
M_PatientM_IdTypeID,
|
|
M_PatientIDNumber,
|
|
M_PatientNote,
|
|
M_PatientUserID
|
|
)
|
|
VALUES(
|
|
'{$titleID}',
|
|
'{$p['M_PatientName']}',
|
|
'{$pdob}',
|
|
'{$p['M_PatientM_SexID']}',
|
|
'{$p['M_PatientM_ReligionID']}',
|
|
'{$p['M_PatientEmail']}',
|
|
'{$p['M_PatientPOB']}',
|
|
'{$p['M_PatientHP']}',
|
|
'{$p['M_PatientPhone']}',
|
|
'{$p['M_PatientM_IdTypeID']}',
|
|
'{$p['M_PatientIDNumber']}',
|
|
'{$p['M_PatientNote']}',
|
|
$userID
|
|
)";
|
|
$qry = $this->db->query($query);
|
|
$patientID = 0;
|
|
if ($qry ) {
|
|
$patientID = $this->db->insert_id();
|
|
}
|
|
|
|
//
|
|
$sql = "insert into m_patientaddress(M_PatientAddressM_PatientID, M_PatientAddressNote,
|
|
M_PatientAddressDescription,M_PatientAddressLocation,M_PatientAddressM_KelurahanID,
|
|
M_PatientAddressOldCityID, M_PatientAddressPostCodeID, M_PatientAddressFax,
|
|
M_PatientAddressEmail, M_PatientAddressPhone)
|
|
values(?,?, ?,?,?, ?,?,?, ?,?) ";
|
|
foreach($p["address"] as $adr) {
|
|
$prm = array($patientID , $adr["M_PatientAddressNote"],
|
|
$adr["M_PatientAddressDescription"],$adr["M_PatientAddressLocation"],$adr["M_PatientAddressM_KelurahanID"],
|
|
$adr["M_PatientAddressOldCityID"], $adr["M_PatientAddressPostCodeID"], $adr["M_PatientAddressFax"],
|
|
$adr["M_PatientAddressEmail"], $adr["M_PatientAddressPhone"] );
|
|
$this->db->query($sql,$prm);
|
|
}
|
|
|
|
return $patientID;
|
|
}
|
|
|
|
|
|
|
|
|
|
function fo_verify($orderID,$userID) {
|
|
$sql="INSERT INTO fo_verificationsvalue (
|
|
Fo_VerificationsValueT_OrderHeaderID,
|
|
Fo_VerificationsValueFo_VerificationsLabelID,
|
|
Fo_VerificationsValueCheck,
|
|
Fo_VerificationsValueNote,
|
|
Fo_VerificationsValueUserID,
|
|
Fo_VerificationsValueCreated )
|
|
SELECT {$orderID},
|
|
Fo_VerificationsLabelID,
|
|
'Y',
|
|
'', {$userID},
|
|
NOW()
|
|
FROM fo_verificationslabel
|
|
WHERE
|
|
Fo_VerificationsLabelIsActive = 'Y' ";
|
|
$this->db->query($sql);
|
|
|
|
$sql = "call sp_fo_barcode_generate({$orderID})";
|
|
$this->db->query($sql);
|
|
|
|
$fostatusid = 3;
|
|
$fologcode = 'FO.VERIFICATION.CONFIRM';
|
|
|
|
$sql = "insert into fo_status(
|
|
Fo_StatusDate,
|
|
Fo_StatusT_OrderHeaderID,
|
|
Fo_StatusM_StatusID,
|
|
Fo_StatusM_UserID,
|
|
Fo_StatusCreated,
|
|
Fo_StatusUpdated)
|
|
values( now(), ?, ?, ?, now(),now())";
|
|
|
|
$query = $this->db->query($sql,
|
|
array(
|
|
$orderID,
|
|
$fostatusid, $userID
|
|
)
|
|
);
|
|
|
|
if (!$query) {
|
|
return "Error Fo Status";
|
|
}
|
|
|
|
$sql = "SELECT * FROM fo_verificationsvalue WHERE Fo_VerificationsValueT_OrderHeaderID = {$orderID}";
|
|
|
|
$data_log = array();
|
|
$data_log['orderid'] = $orderID;
|
|
$data_log['values'] = $this->db->query($sql)->result_array();
|
|
$data_log['note'] = '';
|
|
|
|
$json_dt_log = json_encode($data_log);
|
|
$sql = "insert into one_log.log_fo(
|
|
Log_FoDate,
|
|
Log_FoCode,
|
|
Log_FoJson,
|
|
Log_FoUserID)
|
|
values( now(), ?, ?, ?)";
|
|
$query = $this->db->query($sql,
|
|
array(
|
|
$fologcode,
|
|
$json_dt_log,
|
|
$userID
|
|
)
|
|
);
|
|
|
|
if (!$query) {
|
|
return "Error Fo Log";
|
|
}
|
|
|
|
/* T_OrderSample */
|
|
$adminUserID = 3;
|
|
$sql = "update t_ordersample
|
|
set T_OrderSampleSampling='Y', T_OrderSampleSamplingDate = now(),
|
|
T_OrderSampleSamplingTime = now(),T_OrderSampleSamplingUserID = $adminUserID,
|
|
T_OrderSampleReceive='Y', T_OrderSampleReceiveDate = now() , T_OrderSampleReceiveTime = now(),
|
|
T_OrderSampleReceiveUserID = $adminUserID,
|
|
T_OrderSampleVerification='Y', T_OrderSampleVerificationDate=now(), T_OrderSampleVerificationTime=now(),
|
|
T_OrderSampleVerificationUserID = $adminUserID,
|
|
T_OrderSampleSendHandling = 'Y', T_OrderSampleSendHandlingDate =now(), T_OrderSampleSendHandlingTime=now(),
|
|
T_OrderSampleSendHandlingUserID = $adminUserID,
|
|
T_OrderSampleReceiveHandling = 'Y', T_OrderSampleReceiveHandlingDate=now(), T_OrderSampleReceiveHandlingTime=now(),
|
|
T_OrderSampleReceiveHandlingUserID=$adminUserID,
|
|
T_OrderSampleHandling='Y', T_OrderSampleHandlingDate=now(), T_OrderSampleHandlingTime=now(),
|
|
T_OrderSampleHandlingUserID=$adminUserID,
|
|
T_OrderSampleProcessing='Y', T_OrderSampleReadyToProcessDateTime=now(), T_OrderSampleProcessingDate=now(),
|
|
T_OrderSampleProcessingTime=now(), T_OrderSampleProcessingUserID=$adminUserID,
|
|
T_OrderSampleUserID=$adminUserID
|
|
where T_OrderSampleT_OrderHeaderID = ?";
|
|
|
|
|
|
$qry = $this->db->query($sql, array($orderID) );
|
|
if (!$qry) {
|
|
return "Error T_OrderSample : " . $this->db->last_query();
|
|
}
|
|
return "OK";
|
|
}
|
|
}
|