db_onedev = $this->load->database("onedev", true); } function cek($incomingRefID, $id) { $adminUserID = 3; $id = str_replace("-",",",$id); $sql = "select * from incoming_ref where incomingRefID = ?"; $qry = $this->db_onedev->query($sql, array($incomingRefID) ); $rows = array(); $branchID = 0; if ($qry) { $rows = $qry->result_array(); if ( count($rows) > 0 ) $branchID = $rows[0]["incomingRefM_BranchID"]; } if ($branchID == 0 ) { echo json_encode(array( "status" => "ERR", "message" => "Branch belum di set")); } $companyID = 0; if ($branchID > 0 ) { $sql = "select * from m_branch where M_BranchID = ?"; $qry = $this->db_onedev->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 ) { echo json_encode(array( "status" => "ERR", "message" => "Branch / Company/MOU belum di set")); } //create order yg di confirm saja $sql = "select * from incoming_ref_detail where incomingRefDetailID in ( $id ) and incomingRefDetailStatus = 'Y' order by incomingRefDetailT_OrderHeaderID"; $qry = $this->db_onedev->query($sql ); $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"]; $incomingRefDetailFoNote = str_replace("'", " ", $r["incomingRefDetailFoNote"]); if ( ! isset($orders[$oh_id]) ) { $orders[$oh_id]["patient"] = json_decode($r["incomingRefDetailPatient"],true); $orders[$oh_id]["foNote"] = $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_onedev->query($sql, array($mouID) ); //test primer sama dengan T_TestID $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"] ); } } //test sekunder dari Nat_Test $a_test_v2 = array(); $sql = "select ss_price_mou.* from ss_price_mou where Ss_PriceMouM_MouID = ? and ss_price_mou.T_TestID in ( select s.T_TestID from t_test s join t_test p on s.T_TestNat_TestID = p.T_TestNat_TestID and p.T_TestID in ( $s_test ) ) "; $qry = $this->db_onedev->query($sql, array($mouID) ); //test primer sama dengan T_TestID if ($qry ) { $rows = $qry->result_array(); foreach($rows as $r) { $testID = $r["T_TestID"]; $a_test_v2[$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_onedev->query($sql, array($noreg)); $patientID = 0; if($qry) { $rows = $qry->result_array(); if ($rows[0]["tot"] == 0 ) { $patientID = $this->create_patient($orders[$oh_id]["patient"], $adminUserID); } else { $patientID = $orders[$oh_id]["patient"]["M_PatientID"]; } } if ($patientID == 0 ) { if ($message != "" ) $message .= ","; $message .= "[ $lab_number, gagal buat pasien $noreg ]"; continue; } $sql = "select fn_global_age_count(M_PatientDOB,now()) age from m_patient where M_PatientID = ? "; $qry = $this->db_onedev->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 ) { if ( isset($a_test[$t["T_TestID"]]) ) { $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' ); } elseif ( isset($a_test_v2["T_TestID"] ) ) { $v=$a_test_v2[$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' ); } else { $xdet[] = array( 't_id' => $t['T_TestID'], 't_cito' => 'N', 't_price' => 0, 't_disc' => 0, 't_discrp' => 0, '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 = addslashes(str_replace('\n', '\\\n', json_encode($xheader))); $delivery_json = addslashes(str_replace('\n', '\\\n', json_encode($xdel))); $detail_json = addslashes(str_replace('\n', '\\\n', json_encode($xdet))); $req_json = json_encode($xreq); $sql = "CALL `sp_fo_register_save_v5`( 0, '{$header_json}', '{$delivery_json}', '{$detail_json}', '{$req_json}', '{$adminUserID}' );"; //exit sebelum create order echo $sql . "\n"; // exit; $r = $this->db_onedev->query($sql)->row(); $this->clean_mysqli_connection($this->db_onedev->conn_id); if ($r->status != "OK" ) { if ($message != "" ) $message .= ","; $xmsg = print_r($r, true); $message .= "[ $lab_number, gagal sp_fo_register_save_v5 : $xmsg ]"; $g_date = date("Y-m-d H:i:s"); file_put_contents("/xtmp/gagal-order.log","$g_date $message\n", FILE_APPEND); continue; } $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_onedev->query($sql,array($new_oh_id, $patientID, $incomingRefID, $oh_id)); $sql = "update t_orderheaderaddon set T_OrderHeaderAddOnLabNumberOrigin=? where T_OrderHeaderAddOnT_OrderHeaderID = ?"; $this->db_onedev->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_onedev->query($sql, array($new_oh_id,$promise)); $promise_id = $this->db_onedev->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_onedev->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_onedev->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_onedev->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_onedev->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_onedev->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_onedev->query($sql, array($new_oh_id)); if ($qry) { $rows = $qry->result_array(); foreach($rows as $r) { $sql = "call sp_sampling_set_normal(?,?)"; $this->db_onedev->query($sql,array($r["id"],$r["sid"])); } } if ($message != "" ) $message .= ","; $message .= "[ $lab_number -> $new_lab_number Fo Verify : $result ]"; } try { //update child test $sql = "update incoming_ref_child, incoming_ref_detail set incomingRefChildNewT_OrderHeaderID = incomingRefDetailNewT_OrderHeaderID where incomingRefChildIncomingRefID = incomingRefDetailIncomingRefID and incomingRefChildT_OrderHeaderID = incomingRefDetailT_OrderHeaderID and incomingRefChildNewT_OrderHeaderID is null and incomingRefChildIncomingRefID = ? "; $this->db_onedev->query($sql,array($incomingRefID)); } catch(Exception $e) { echo "Err Update Child Test \n"; print_r($e); } try { //update barcode $sql = "update incoming_ref_detail, t_test, t_barcodelab set T_BarcodeLabBarcodeOrigin = incomingRefDetailT_BarcodeLabBarcode where incomingRefDetailT_TestID = T_TestID and incomingRefDetailNewT_OrderHeaderID = T_BarcodeLabT_OrderHeaderID and T_BarcodeLabT_SampleTypeID = json_extract(fn_worklist_get_info(incomingRefDetailNewT_OrderHeaderID,T_TestID), '$.sampleTypeID') and incomingRefDetailIncomingRefID = ? and T_TestT_SampleTypeID = 0 and T_BarcodeLabBarcodeOrigin is null"; $this->db_onedev->query($sql,array($incomingRefID)); } catch(Exception $e) { echo "Err Update Barcode \n"; print_r($e); } echo json_encode(array( "status" => "OK", "message" => $message )); } function create_patient($p, $userID) { $sql = "insert into m_patient"; //check title $sql = "select M_TitleID from m_title where M_TitleName = ? and M_TitleIsActive = 'Y'"; $qry = $this->db_onedev->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_onedev->query($sql, array($p["title"]["M_TitleName"], $p["title"]["M_TitleM_SexID"])); if ($qry) { $titleID = $this->db_onedev->insert_id(); } } if ($titleID == 0 ) { return 0; } $patient_name = str_replace("'", "\\'", $p['M_PatientName']); $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}', '{$patient_name}', '{$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_onedev->query($query); $patientID = 0; if ($qry ) { $patientID = $this->db_onedev->insert_id(); } echo $this->db_onedev->last_query(); // $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) { $address_description = str_replace("'", "\\'", $adr['M_PatientAddressDescription']); $prm = array($patientID , $adr["M_PatientAddressNote"], $address_description,$adr["M_PatientAddressLocation"],$adr["M_PatientAddressM_KelurahanID"], $adr["M_PatientAddressOldCityID"], $adr["M_PatientAddressPostCodeID"], $adr["M_PatientAddressFax"], $adr["M_PatientAddressEmail"], $adr["M_PatientAddressPhone"] ); $this->db_onedev->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_onedev->query($sql); $sql = "call sp_fo_barcode_generate({$orderID})"; $this->db_onedev->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_onedev->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_onedev->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_onedev->query($sql, array( $fologcode, $json_dt_log, $userID ) ); if (!$query) { return "Error Fo Log"; } /* T_OrderSample */ $adminUserID = $userID; $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_onedev->query($sql, array($orderID) ); if (!$qry) { return "Error T_OrderSample : " . $this->db_onedev->last_query(); } return "OK"; } }