debug = false; $this->ONLINE_USER_ID = 1500; $this->SENDER_DOCTOR_ID = 0; $this->SENDER_ADDRESS_ID = 0; $this->PJ_DOCTOR_ID = 0; $this->KASIR_ONLINE_USER = 1500; $this->url = "https://devonline.pramita.co.id/one-api/tools/regonline/r_download/"; } public function now() { return Date("Y-m-d H:i:s"); } public function exists_tx($txID) { $sql = "select count(*) total from t_onlinetx where T_OnlineTxOrgID=?"; $qry = $this->db->query($sql, array($txID)); if (!$qry) { echo "{$this->now()} ERR Exist Tx : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); return $rows[0]["total"] > 0; } public function get_tx_id($orgID) { $result = 0; $sql = "select T_OnlineTxID from t_onlinetx where T_OnlineTxOrgID=?"; $qry = $this->db->query($sql, array($orgID)); if (!$qry) { echo "{$this->now()} ERR Exist Tx : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) > 0) { return $rows[0]["T_OnlineTxID"]; } return $result; } public function last_tx() { $sql = "select max(T_OnlineTxOrgID) maxID from t_onlinetx where T_OnlineTxIsActive = 'Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Max Date : {$this->db->error()['message']}\n"; return "2020-01-01 00:01:01"; } $rows = $qry->result_array(); if (count($rows) == 0) { return "2020-01-01 00:01:01"; } if ($rows[0]["maxID"] == "") { return 0; } return $rows[0]["maxID"]; } public function get_patient_by_id($idType, $idNo, $noreg) { $sql = "select * from m_patient where ( ( M_PatientM_IdTypeID=? and M_PatientIDNumber=? ) or ( M_PatientNoreg = ? and M_PatientNoreg <> '' ) ) and M_PatientIsActive='Y'"; $qry = $this->db->query($sql, array($idType, $idNo, $noreg)); if (!$qry) { echo "{$this->now()} ERR get pasien : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { return array(0, 0); } $patientID = $rows[0]["M_PatientID"]; $sql = "select M_PatientAddressID from m_patientaddress where M_PatientAddressM_PatientID = ? and M_PatientAddressIsActive = 'Y' limit 0,1"; $qry = $this->db->query($sql, array($patientID)); if (!$qry) { echo "{$this->now()} ERR get pasien address : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { return array($patientID, 0); } return array($patientID, $rows[0]["M_PatientAddressID"]); } public function get_company_id($mouID) { $sql = "select * from m_mou where M_MouID=?"; $qry = $this->db->query($sql, array($mouID)); if (!$qry) { echo "{$this->now()} ERR get Company : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { return 0; } return $rows[0]["M_MouM_CompanyID"]; } public function get_age($dob, $order_date) { $sql = "select fn_global_age_count(?,?) xage"; $qry = $this->db->query($sql, array($dob, $order_date)); if (!$qry) { echo "{$this->now()} ERR get Age : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { return ""; } return $rows[0]["xage"]; } public function get_branch_default() { $sql = "select * from m_branch where M_BranchIsDefault = 'Y' and M_BranchIsActive = 'Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} ERR Get Default Branch: {$this->db->error()['message']}\n"; exit; } $branchKelurahanID = $rows[0]["M_BranchM_KelurahanID"]; $this->SENDER_DOCTOR_ID = $rows[0]["M_BranchM_DoctorID"]; $this->SENDER_ADDRESS_ID = $rows[0]["M_BranchM_DoctorAddressID"]; $sql = "select * from m_doctorpj where M_DoctorPjIsActive = 'Y' and M_DoctorPjIsDefaultPJ='Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} ERR Get Default PJ: {$this->db->error()['message']}\n"; exit; } $this->PJ_DOCTOR_ID = $rows[0]["M_DoctorPjM_DoctorID"]; return array($branchKelurahanID); } public function populate_test($mouID, $details) { $a_test = array(); foreach ($details as $d) { $resultOptionID = $d["T_OrderDetailT_ResultOptionID"]; $packetID = $d["T_OrderDetailT_PacketID"]; $isCito = "N"; if ($resultOptionID == 1 || $resultOptionID == 2) { $isCito = "Y"; } $sql = "select * from ss_price_mou where Ss_PriceMouM_MouID = ? and packet_id = ? and is_packet = 'Y' and T_PriceIsCito=?"; $qry = $this->db->query($sql, array($mouID, $packetID, $isCito)); if (!$qry) { echo "{$this->now()} ERR Populate Test : {$this->db->error()['message']} | {$this->db->last_query()}\n"; exit; } $rows = $qry->result_array(); foreach ($rows as $px) { if ($px["px_type"] == "PR") { $child_test = json_decode($px["child_test"], true); foreach ($child_test as $xt) { $a_test[] = array( 't_id' => $xt['T_TestID'], 't_cito' => $xt["T_PriceIsCito"], 't_price' => $xt['T_PriceAmount'], 't_disc' => $xt['T_PriceDisc'], 't_discrp' => $xt['T_PriceDiscRp'], 't_req' => 'Y', 't_reqnote' => '', 't_ispacket' => $xt['is_packet'], 't_packettype' => $xt['px_type'], 't_packetid' => $xt['packet_id'] ); } } else { $a_test[] = array( 't_id' => $px['T_TestID'], 't_cito' => $px["T_PriceIsCito"], 't_price' => $px['T_PriceAmount'], 't_disc' => $px['T_PriceDisc'], 't_discrp' => $px['T_PriceDiscRp'], 't_req' => 'Y', 't_reqnote' => '', 't_ispacket' => $px['is_packet'], 't_packettype' => $px['px_type'], 't_packetid' => $px['packet_id'] ); } } } return $a_test; } // cari janji hasil public function get_promise_by_px($pxID, $headerDate) { $sql = "select date( case when M_ScheduleFlagAtTime = 'Y' then concat( date( ? + interval M_ScheduleMonth * 4 day + interval 7 * M_ScheduleWeek day + interval M_ScheduleDay day) , ' ', M_ScheduleAtTime , ':00') else ? + interval M_ScheduleMonth * 4 day + interval 7 * M_ScheduleWeek day + interval M_ScheduleDay day + interval M_ScheduleHour Hour + interval M_ScheduleMinute minute end ) promiseDate, max( case when M_ScheduleFlagAtTime = 'Y' then concat( date( ? + interval M_ScheduleMonth * 4 day + interval 7 * M_ScheduleWeek day + interval M_ScheduleDay day) , ' ', M_ScheduleAtTime , ':00') else ? + interval M_ScheduleMonth * 4 day + interval 7 * M_ScheduleWeek day + interval M_ScheduleDay day + interval M_ScheduleHour Hour + interval M_ScheduleMinute minute end ) promiseDateTime from t_test join m_schedulegrouptest on T_TestNat_TestID = M_ScheduleGroupTestNat_TestID and M_ScheduleGroupTestIsActive = 'Y' join m_schedule on M_ScheduleM_ScheduleGroupID = M_ScheduleGroupTestM_ScheduleGroupID and M_ScheduleIsActive = 'Y' and M_ScheduleDayOfWeek = dayofweek(?) and date_format(?, '%H:%i') >= M_ScheduleStartHourMinute and date_format(?, '%H:%i') <= M_ScheduleEndHourMinute and T_TestID in ( $pxID ) group by promiseDate"; $qry = $this->db->query($sql, array( $headerDate, $headerDate, $headerDate, $headerDate, $headerDate, $headerDate, $headerDate )); if (!$qry) { //echo "{$this->now()} ERR Promise Reguler : {$this->db->error()['message']} | {$this->db->last_query()}\n"; return array('', ''); } $rows = $qry->result_array(); if (count($rows) == 0) { //echo "{$this->now()} ERR Promise Reguler : Not Found \n"; return array('', ''); } //ambil yg pertama $promiseDate = $rows[0]["promiseDate"]; $promiseDateTime = $rows[0]["promiseDateTime"]; return array($promiseDate, $promiseDateTime); } public function fix_promise_reguler($headerID) { $test_ids = "0"; $sql = "select T_OrderHeaderDate, T_OrderDetailT_TestID , T_TestSasCode from t_orderdetail join t_orderheader on T_OrderDetailT_OrderHeaderID = T_OrderHeaderID and T_OrderHeaderID = ? and T_OrderDetailIsActive = 'Y' join t_test on T_OrderDetailT_TestID = T_TestID order by T_TestSasCode "; $qry = $this->db->query($sql, array($headerID)); if (!$qry) { //echo "{$this->now()} ERR Promise Reguler : {$this->db->error()['message']} | {$this->db->last_query()}\n"; return array(false, "{$this->now()} ERR Promise Reguler : {$this->db->error()['message']} | {$this->db->last_query()}\n"); } $rows = $qry->result_array(); $arr_promise = array(); foreach ($rows as $r) { $headerDate = $r["T_OrderHeaderDate"]; $testID = $r["T_OrderDetailT_TestID"]; $sasCode = $r["T_TestSasCode"]; list($date, $dateTime) = $this->get_promise_by_px($testID, $headerDate); if ($date == "") { continue; } if (!isset($arr_promise[$date])) { $arr_promise[$date]["px"] = array($testID); $arr_promise[$date]["sasCode"] = array($sasCode); $arr_promise[$date]["time"] = $dateTime; } else { $arr_promise[$date]["px"][] = $testID; $arr_promise[$date]["sasCode"][] = $sasCode; if ($dateTime > $arr_promise[$date]["time"]) { $arr_promise[$date]["time"] = $dateTime; } } } if (count($arr_promise) == 0) { //echo "{$this->now()} ERR Promise Reguler : Belum Ada Janji Hasil: $headerID | $sasCode : TestID $testID \n"; //echo "|" . $this->db->last_query() . "\n"; return array(false, "{$this->now()} ERR Promise Reguler : Belum Ada Janji Hasil: $headerID | $sasCode : TestID $testID \n"); } //reset promise $sql = "update t_orderpromise set T_OrderPromiseIsActive = 'X' where T_OrderPromiseT_OrderHeaderID=?"; $qry = $this->db->query($sql, array($headerID)); if (!$qry) { //echo "{$this->now()} ERR Promise Reguler : {$this->db->error()['message']} | {$this->db->last_query()}\n"; return array(false, "{$this->now()} ERR Promise Reguler : {$this->db->error()['message']} | {$this->db->last_query()}\n"); } foreach ($arr_promise as $p) { $promiseDateTime = $p["time"]; $arr = array( "T_OrderPromiseT_OrderHeaderID" => $headerID, "T_OrderPromiseDateTime" => $promiseDateTime ); $qry = $this->db->insert("t_orderpromise", $arr); if (!$qry) { echo "{$this->now()} ERR Create New Promise : {$this->db->error()['message']} | {$this->db->last_query()}\n"; return array(false, "{$this->now()} ERR Create New Promise : {$this->db->error()['message']} | {$this->db->last_query()}\n"); } $promiseID = $this->db->insert_id(); $w_sasCode = " true "; foreach ($p["sasCode"] as $sc) { if ($w_sasCode != "") { $w_sasCode .= " OR "; } $w_sasCode .= " T_TestSasCode like '" . $sc . "%' "; } $sql = "update t_orderdetail join t_test on T_OrderDetailT_OrderHeaderID = ? and T_OrderDetailIsActive = 'Y' and T_OrderDetailT_TestID = T_TestID and ( $w_sasCode ) set T_OrderDetailT_OrderPromiseID = ?"; $qry = $this->db->query($sql, array($headerID, $promiseID)); if (!$qry) { //echo "{$this->now()} ERR Promise Set PromiseID : {$this->db->error()['message']} | {$this->db->last_query()}\n"; return array(false, "{$this->now()} ERR Promise Set PromiseID : {$this->db->error()['message']} | {$this->db->last_query()}\n"); } } return array(true, ""); } public function fix_promise_cito($headerID, $citoDay) { //TODO: Fix Skip cito $sql = "update t_orderpromise join t_orderheader on T_OrderPromiseT_OrderHeaderID = T_OrderHeaderID and T_OrderHeaderID = ? set T_OrderPromiseDateTime = concat(date(T_OrderHeaderDate + interval $citoDay day), ' 18:00:00')"; $qry = $this->db->query($sql, array($headerID)); if (!$qry) { echo "{$this->now()} ERR fix_promise_reguler_cito: {$this->db->error()['message']} | {$this->db->last_query()}\n"; exit; } } public function fix_promise_empty($headerID) { $sql = "insert into t_orderpromise(T_OrderPromiseT_OrderHeaderID, T_OrderPromiseDateTime) values(?, concat(date(now() + interval 3 day), ' 18:01:01'))"; $qry = $this->db->query($sql, array($headerID)); if (!$qry) { return array( false, "{$this->now()} ERR fix_promise_empty: {$this->db->error()['message']} | {$this->db->last_query()}\n" ); } $promiseID = $this->db->insert_id(); $sql = "update t_orderdetail set T_OrderDetailT_OrderPromiseID = ? where T_OrderDetailT_OrderHeaderID = ?"; $qry = $this->db->query($sql, array($promiseID, $headerID)); if (!$qry) { return array( false, "{$this->now()} ERR fix_promise_empty: {$this->db->error()['message']} | {$this->db->last_query()}\n" ); } return array(true, ""); } public function get_promise_info($headerID) { $sql = "select distinct T_OrderPromiseDateTime from t_orderpromise join t_orderdetail on T_OrderDetailT_OrderHeaderID = ? and T_OrderDetailIsActive = 'Y' and T_OrderDetailT_OrderPromiseID = T_OrderPromiseID order by T_OrderPromiseDateTime"; $qry = $this->db->query($sql, array($headerID)); if (!$qry) { return array("status" => "ERR", "message" => "ERR Find Promise :{$this->db->error()['message']}"); } $rows = $qry->result_array(); if (!$qry) { return array("status" => "ERR", "message" => "ERR Find Promise No Promise"); } $message = ""; foreach ($rows as $r) { if ($message != "") { $message .= ", "; } $message .= $this->get_hari_id($r["T_OrderPromiseDateTime"]); } return array("status" => "OK", "message" => "Perkiraan Hasil Selesai : $message"); } public function status_order() { echo "{$this->now()} Start Order Create Status\n"; list($branchKelurahanID) = $this->get_branch_default(); $db_msg = ""; $this->db->trans_begin(); //1. Get Branch $sql = "select M_BranchName, M_BranchAddress from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} ERR Update Status - Branch : {$this->db->error()['message']}\n"; exit; } $branchName = $rows[0]["M_BranchName"]; $branchAddress = $rows[0]["M_BranchAddress"]; //1. Order Creation $sql = "select T_OnlineTxOrgID, T_OnlineOrderT_OrderID, T_OnlineOrderID, T_OrderHeaderLabNumberExt, T_OrderHeaderDate, T_OrderHeaderID from t_onlineorder join t_onlinetx on T_OnlineOrderT_OnlineTxID = T_OnlineTxID join t_orderheader on T_OrderHeaderID = T_OnlineOrderT_OrderHeaderID where T_OnlineOrderUploaded is null"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); $counter = 0; $status = array(); $headerDate = date("Y-m-d H:i:s"); foreach ($rows as $r) { $msg = "Nomor Registrasi\t: " . $r["T_OrderHeaderLabNumberExt"] . "\n"; $msg .= "Lab. Pramita $branchName\n$branchAddress\n"; $msg .= "Jadwal\t: " . $this->get_hari_id($r["T_OrderHeaderDate"]); $resp = $this->get_promise_by_px($r["T_OrderHeaderID"], $r["T_OrderHeaderDate"]); if ($resp["status"] != "OK") { $db_msg .= "|Err Promise Info : {$resp['message']}"; } else { $msg .= "\n" . $resp["message"]; } $status[] = array( "T_TxMessageT_TransactionID" => $r["T_OnlineTxOrgID"], "T_TxMessageT_OrderID" => $r["T_OnlineOrderT_OrderID"], "T_TxMessageNote" => $msg, "T_TxMessageCode" => "ORDER" ); $counter++; $headerDate = $r["T_OrderHeaderDate"]; $sql = "update t_onlineorder set T_OnlineOrderUploaded=? where T_OnlineOrderID = ?"; $qry = $this->db->query($sql, array($headerDate, $r["T_OnlineOrderID"])); if (!$qry) { echo "{$this->now()} ERR Update Uploaded: {$this->db->error()['message']}\n"; exit; } } if (count($status) > 0) { //upload $url = $this->url . "status"; $param = array("status" => $status); $z_param = gzdeflate(json_encode($param), 9); $resp = $this->post($url, $z_param); if ($resp["status"] == "OK") { $this->db->trans_commit(); echo "{$this->now()} Upload Status Creation : $counter\n"; } else { $this->db->trans_rollback(); echo "{$this->now()} ERR {$resp['message']}\n"; } } else { echo "{$this->now()} Upload Status Creation : $counter | 0 Status \n"; } } public function status_validasi() { echo "{$this->now()} Start Validasi Status\n"; $this->db->trans_begin(); $sql = "select T_OrderHeaderID, T_OrderHeaderLabNumberExt, T_OnlineOrderID, T_OnlineOrderT_OrderID, T_OnlineTxOrgID, max(T_OrderDetailValDate) maxDate from t_onlineorder join t_onlinetx on T_OnlineOrderT_OnlineTxID = T_OnlineTxID and T_OnlineOrderIsActive = 'Y' and T_OnlineTxIsActive = 'Y' join t_orderheader on T_OnlineOrderT_OrderHeaderID = T_OrderHeaderID and T_OnlineOrderUploaded is not null join pb_upload on pbUploadCode = 'VALIDASI' join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID and T_OrderHeaderAddOnIsActive = 'Y' and T_OrderHeaderAddOnValidationDone <> 'N' and T_OrderHeaderAddOnLastUpdated > pbUploadExecuted join t_orderdetail on T_OrderDetailT_OrderHeaderID = T_OrderHeaderID and T_OrderDetailIsActive = 'Y' and T_OrderDetailValidation = 'Y' group by T_OrderHeaderID"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Status Validasi : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} No Status Validation\n"; exit; } $sql = "update pb_upload set pbUploadExecuted=now() where pbUploadCode = 'VALIDASI'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Update Uploaded: {$this->db->error()['message']}\n"; exit; } $status = array(); $counter = 0; foreach ($rows as $r) { $headerID = $r["T_OrderHeaderID"]; $maxDate = $r["maxDate"]; $staffName = $this->get_validation_staff($headerID, $maxDate); $msg = "Nomor Registrasi : " . $r["T_OrderHeaderLabNumberExt"] . "\n"; $msg .= "Sudah di validasi oleh $staffName pada " . $this->get_hari_id($maxDate); $status[] = array( "T_TxMessageT_TransactionID" => $r["T_OnlineTxOrgID"], "T_TxMessageT_OrderID" => $r["T_OnlineOrderT_OrderID"], "T_TxMessageNote" => $msg, "T_TxMessageCode" => "VALIDATION" ); $counter++; } if (count($status) > 0) { //upload $url = $this->url . "status"; $param = array("status" => $status); $z_param = gzdeflate(json_encode($param), 9); $resp = $this->post($url, $z_param); if ($resp["status"] == "OK") { $this->db->trans_commit(); echo "{$this->now()} Upload Status Validation : $counter\n"; } else { $this->db->trans_rollback(); echo "{$this->now()} ERR {$resp['message']}\n"; } } else { echo "{$this->now()} Upload Status Validasi : $counter\n"; } } public function get_validation_staff($headerID, $maxDate) { $sql = "select M_StaffName from t_orderdetail join m_user on T_OrderDetailT_OrderHeaderID = ? and T_OrderDetailIsActive = 'Y' and T_OrderDetailValidation = 'Y' and T_OrderDetailValDate = ? and T_OrderDetailValUserID = M_UserID join m_staff on M_UserM_StaffID = M_StaffID"; $qry = $this->db->query($sql, array($headerID, $maxDate)); if (!$qry) { echo "{$this->now()} ERR Status Validasi : {$this->db->error()['message']}\n"; return ""; } $rows = $qry->result_array(); if (count($rows) == 0) { return ""; } return $rows[0]["M_StaffName"]; } public function status_report() { echo "{$this->now()} Start Report Status\n"; $this->db->trans_begin(); $sql = "select M_BranchCode from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} ERR Update Status - Branch : {$this->db->error()['message']}\n"; exit; } $branchCode = $rows[0]["M_BranchCode"]; $sql = "select T_OrderHeaderLabNumberExt, T_OrderHeaderID, T_OnlineOrderID, T_OnlineOrderT_OrderID, T_OnlineTxOrgID, max(Result_ProcessToOfficeLastUpdated) maxDate , max(T_OrderHeaderAddonReadyPrintDate) maxPrintDate, T_OrderHeaderM_PatientID from t_onlineorder join t_onlinetx on T_OnlineOrderT_OnlineTxID = T_OnlineTxID and T_OnlineOrderIsActive = 'Y' and T_OnlineTxIsActive = 'Y' and T_OnlineOrderUploaded is not null join t_orderheader on T_OnlineOrderT_OrderHeaderID = T_OrderHeaderID join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID join pb_upload on pbUploadCode = 'RESULT' join result_processtooffice on Result_ProcessToOfficeT_OrderHeaderID = T_OrderHeaderID and Result_ProcessToOfficeIsActive = 'Y' and ( Result_ProcessToOfficeLastUpdated > pbUploadExecuted or T_OrderHeaderAddonReadyPrintDate > pbUploadExecuted ) group by T_OrderHeaderID "; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Status Report : {$this->db->error()['message']} | {$this->db->last_query()}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} No Status Report\n"; exit; } $s_patientID = "0"; foreach ($rows as $r) { $s_patientID .= "," . $r["T_OrderHeaderM_PatientID"]; } //get patient $sql = " select M_PatientName, M_PatientDOB, M_PatientNoreg, M_PatientM_SexID, M_PatientJob, M_PatientM_IdTypeID, M_PatientIDNumber, T_OnlineOrderT_OrderID from m_patient join t_onlineorder on M_PatientID in ($s_patientID) and T_OnlineOrderM_PatientID = M_PatientID and T_OnlineOrderIsActive = 'Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Report : Patient : {$this->db->error()['message']} | {$this->db->last_query()}\n"; exit; } $patients = $qry->result_array(); //update pb_upload $sql = "update pb_upload set pbUploadExecuted=now() where pbUploadCode = 'RESULT'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Update Uploaded: {$this->db->error()['message']}\n"; exit; } $status = array(); $counter = 0; foreach ($rows as $r) { $headerID = $r["T_OrderHeaderID"]; $reports = $this->get_report($headerID); $arr_reports = array(); $idx = 1; foreach ($reports as $xr) { $rpt_url = "http://localhost/" . $xr["url_rpt"]; $fname = $r["T_OnlineTxOrgID"] . "_" . $r["T_OnlineOrderT_OrderID"] . "_" . $xr["Group_ResultName"] . "_" . $r["T_OrderHeaderLabNumberExt"] . ".pdf"; $arr_reports[] = array( "name" => $xr["Group_ResultName"], "branchCode" => $branchCode, "fname" => $fname, "content" => base64_encode(file_get_contents($rpt_url)) ); $idx++; } $msg = "Nomor Registrasi : " . $r["T_OrderHeaderLabNumberExt"] . "\n"; $msg .= "Sudah selesai."; $status[] = array( "T_TxMessageT_TransactionID" => $r["T_OnlineTxOrgID"], "T_TxMessageT_OrderID" => $r["T_OnlineOrderT_OrderID"], "T_TxMessageNote" => $msg, "reports" => $arr_reports, "T_TxMessageCode" => "RESULT" ); $counter++; } if (count($status) > 0) { $url = $this->url . "status"; $param = array("status" => $status, "patients" => $patients); $z_param = gzdeflate(json_encode($param), 9); $resp = $this->post($url, $z_param); if ($resp["status"] == "OK") { $this->db->trans_commit(); echo "{$this->now()} Upload Status Report : $counter\n{$resp['message']}\n"; foreach ($status["report"] as $r) { echo "{$this->now()} => $fname \n"; } } else { $this->db->trans_rollback(); echo "{$this->now()} ERR {$resp['message']}\n"; } } else { echo "{$this->now()} Upload Status Report : $counter\n"; } } public function get_report($headerID) { $sql = "SELECT distinct Group_ResultName, CASE WHEN Group_ResultID = 1 THEN CONCAT('/birt/frameset?__report=report/onelab/lab/rpt_test_email.rptdesign&__format=pdf&username=','regonline','&PID=',T_OrderHeaderID,'&ts=',UNIX_TIMESTAMP()) WHEN Group_ResultID = 2 THEN CONCAT('/birt/frameset?__report=report/onelab/lab/rpt_hasil_papsmear_email.rptdesign&__format=pdf&username=','regonline','&PID=',T_OrderHeaderID,'&ts=',UNIX_TIMESTAMP()) WHEN Group_ResultID = 3 THEN CONCAT('/birt/frameset?__report=report/onelab/lab/rpt_hasil_fna_email.rptdesign&__format=pdf&username=','regonline','&PID=',T_OrderHeaderID,'&ts=',UNIX_TIMESTAMP()) WHEN Group_ResultID = 12 THEN CONCAT('/birt/frameset?__report=report/onelab/lab/rpt_hasil_lcprep_email.rptdesign&__format=pdf&username=','regonline','&PID=',T_OrderHeaderID,'&ts=',UNIX_TIMESTAMP()) WHEN Group_ResultID = 13 THEN CONCAT('/birt/frameset?__report=report/onelab/lab/rpt_test_mikro_email.rptdesign&__format=pdf&username=','regonline','&PID=',T_OrderHeaderID,'&ts=',UNIX_TIMESTAMP()) WHEN Group_ResultID = 14 THEN CONCAT('/birt/frameset?__report=report/onelab/lab/rpt_hasil_cytologi_email.rptdesign&__format=pdf&username=','regonline','&PID=',T_OrderHeaderID,'&ts=',UNIX_TIMESTAMP()) END as url_rpt FROM t_orderheader JOIN t_orderdetail ON T_OrderHeaderID = ? AND T_OrderDetailT_OrderHeaderID = T_OrderHeaderID AND T_OrderDetailIsActive = 'Y' JOIN t_test ON T_OrderDetailT_TestID = T_TestID JOIN group_resultdetail ON Group_ResultDetailT_TestID = T_TestID JOIN group_result ON Group_ResultDetailGroup_ResultID = Group_ResultID AND Group_ResultFlagNonLab = 'N' "; $qry = $this->db->query($sql, array($headerID)); if (!$qry) { echo "{$this->now()} ERR Get Report : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} No Status Report\n"; exit; } return $rows; } public function status_patient() { $sql = "select T_OnlineOrderT_OrderID, M_PatientID, M_PatientName M_PatientDOB, M_PatientM_SexID, M_PatientJob, M_PatientM_IdTypeID, M_PatientIDNumber, M_PatientUserID from t_onlineorder join pb_upload on pbUploadCode = 'PATIENT' join m_patient on T_OnlineOrderM_PatientID = M_PatientID and M_PatientIsActive = 'Y' and M_PatientLastUpdated > pbUploadExecuted "; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Status Report : {$this->db->error()['message']} | {$this->db->last_query()}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} No Status Report\n"; exit; } //update pb_upload $sql = "update pb_upload set pbUploadExecuted=now() where pbUploadCode = 'PASIEN'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR Update Uploaded: {$this->db->error()['message']}\n"; exit; } $status = array(); $counter = 0; } public function expiry() { echo "{$this->now()} Start Expiry Old Transaction\n"; list($branchKelurahanID) = $this->get_branch_default(); //upload $url = $this->url . "expiry"; $resp = $this->post($url, ""); if ($resp["status"] == "OK") { echo "{$this->now()} Done Expiry : " . $resp["message"]; } else { echo "{$this->now()} ERR {$resp['message']}"; } } public function get_hari_id($orderDate) { $bulan = array( 1 => 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember' ); $tanggal = date("Y-m-d", strtotime($orderDate)); $split = explode('-', $tanggal); $hari = $split[2] . ' ' . $bulan[(int)$split[1]] . ' ' . $split[0]; $dow = date("l", strtotime($orderDate)); switch ($dow) { case 'Sunday': $dow = 'Minggu'; break; case 'Monday': $dow = 'Senin'; break; case 'Tuesday': $dow = 'Selasa'; break; case 'Wednesday': $dow = 'Rabu'; break; case 'Thursday': $dow = 'Kamis'; break; case 'Friday': $dow = 'Jumat'; break; case 'Saturday': $dow = 'Sabtu'; break; } $result = ""; if ($dow != "") { $result .= $dow . ", "; } $result .= $hari . " jam " . date("H:i", strtotime($orderDate)); return $result; } public function do_pelunasan( $headerID, $bankAccountID, $paymentTypeID, $paymentDate, $paymentNote ) { $sql = "select T_OrderHeaderTotal from t_orderheader where T_OrderHeaderID = ?"; $qry = $this->db->query($sql, array($headerID)); if (!$qry) { echo "{$this->now()} ERR Get Total Order: {$this->db->error()['message']} | {$this->db->last_query()}\n"; return false; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} ERR Get Total Order: Not Found\n"; return false; } $paymentTotal = $rows[0]["T_OrderHeaderTotal"]; $arr = array( "F_PaymentT_OrderHeaderID" => $headerID, "F_PaymentDate" => $paymentDate, "F_PaymentNote" => $paymentNote, "F_PaymentTotal" => $paymentTotal, "F_PaymentM_UserID" => $this->ONLINE_USER_ID ); $qry = $this->db->insert("f_payment", $arr); if (!$qry) { echo "{$this->now()} ERR Get Total Order: {$this->db->error()['message']} | {$this->db->last_query()}\n"; return false; } $paymentID = $this->db->insert_id(); $detailNote = ""; $arr = array( "F_PaymentDetailF_PaymentID" => $paymentID, "F_PaymentDetailM_PaymentTypeID" => $paymentTypeID, "F_PaymentDetailEDCNat_BankID" => 0, "F_PaymentDetailCardNat_BankID" => 0, "F_PaymentDetailM_BankAccountID" => $bankAccountID, "F_PaymentDetailNote" => $paymentNote, "F_PaymentDetailAmount" => $paymentTotal, "F_PaymentDetailActual" => $paymentTotal, "F_PaymentDetailChange" => 0 ); $qry = $this->db->insert("f_paymentdetail", $arr); if (!$qry) { echo "{$this->now()} ERR Pelunasan: {$this->db->error()['message']} | {$this->db->last_query()}\n"; return false; } return true; } public function get_bank($merchantCode) { $sql = "select * from pg_bank where pgBankMerchantCode=? and pgBankIsActive = 'Y'"; $qry = $this->db->query($sql, array($merchantCode)); if (!$qry) { $msg = "ERR Get Bank [{$merchantCode}]: {$this->db->error()['message']}\n"; return array("status" => "ERR", "message" => $msg); } $rows = $qry->result_array(); if (count($rows) == 0) { return array("status" => "ERR", "message" => "No default Bank for $merchantCode"); } return array( "status" => "OK", "bank" => $rows[0] ); } public function strip_unicode($inp) { $result = mb_convert_encoding($inp, 'US-ASCII', 'UTF-8'); return $result; } public function add_pasien($pasien) { $age = date_diff($pasien["M_PatientDOB"], date("Y-m-d")) / 365; if ($pasien["M_PatientM_SexID"] == 1) { //Laki-laki $titleID = 2; if ($age > 25) { $titleID = 1; } } else { $titleID = 5; if ($age > 25) { $titleID = 3; } } //TODO : tambah M_PatientHP dan di strip unicode $pasien["M_PatientHp"] = $this->strip_unicode($pasien["M_PatientHp"]); $pasien["M_PatientName"] = $this->strip_unicode($pasien["M_PatientName"]); $arr = array( "M_PatientM_TitleID" => $titleID, "M_PatientName" => $pasien["M_PatientName"], "M_PatientDOB" => $pasien["M_PatientDOB"], "M_PatientM_SexID" => $pasien["M_PatientM_SexID"], "M_PatientJob" => $pasien["M_PatientJob"], "M_PatientM_IdTypeID" => $pasien["M_PatientM_IdTypeID"], "M_PatientIDNumber" => $pasien["M_PatientIDNumber"], "M_PatientHp" => $pasien["M_PatientHp"], "M_PatientUserID" => $this->ONLINE_USER_ID ); $qry = $this->db->insert("m_patient", $arr); if (!$qry) { return array(0, 0, "Add Patient " . $this->db->error()["message"]); } $M_PatientID = $this->db->insert_id(); $M_PatientAddressID = 0; foreach ($pasien["address"] as $d) { $arr = array( "M_PatientAddressM_PatientID" => $M_PatientID, "M_PatientAddressNote" => $d["M_PatientAddressNote"], "M_PatientAddressDescription" => $d["M_PatientAddressDescription"], "M_PatientAddressM_KelurahanID" => $d["M_PatientAddressM_KelurahanID"], "M_PatientAddressUserID" => $this->ONLINE_USER_ID ); $qry = $this->db->insert("m_patientaddress", $arr); if (!$qry) { return array($M_PatientID, 0, $this->db->error()["message"]); } $M_PatientAddressID = $this->db->insert_id(); } return array($M_PatientID, $M_PatientAddressID, ""); } public function get_cito($resultOptionID) { if ($resultOptionID == 1) { $sql = "select * from nat_cito where Nat_CitoDuration < 24 * 60 and Nat_CitoIsActive = 'Y' order by Nat_CitoDuration desc limit 0,1"; } else { $sql = "select * from nat_cito where Nat_CitoDuration < 48 * 60 and Nat_CitoIsActive = 'Y' order by Nat_CitoDuration desc limit 0,1"; } $qry = $this->db->query($sql); if (!$qry) { $msg = "ERR Get Cito [{$resultOptionID}]: {$this->db->error()['message']}\n"; return array("status" => "ERR", "message" => $msg); } $rows = $qry->result_array(); if (count($rows) == 0) { return array("status" => "ERR", "message" => "No Cito ID for $resultOptionID"); } return array( "status" => "OK", "result" => $rows[0]["Nat_CitoID"] ); } public function serah_kasir() { echo "{$this->now()} Start Serahkan ke kasir\n"; $this->db->trans_begin(); $arr = array( "F_PaymentKasirNumber", "F_PaymentKasirDate", "F_PaymentKasirUserID" ); $arrd = array( "F_PaymentKasirDetailF_PaymentID", "F_PaymentKasirDetailF_PaymentKasirID" ); $sql = "select F_PaymentID from f_payment join t_onlineorder on F_PaymentT_OrderHeaderID = T_OnlineOrderT_OrderHeaderID and T_OnlineOrderIsActive = 'Y' and F_PaymentIsActive = 'Y' and F_PaymentID not in ( select F_PaymentKasirDetailF_PaymentID from f_payment_kasir_detail where F_PaymentKasirDetailIsActive = 'Y' )"; $qry = $this->db->query($sql); if (!$qry) { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $payment_rows = $qry->result_array(); if (count($payment_rows) == 0) { echo "{$this->now()} No Transaction\n"; exit; } //get serah no $sql = "select fn_numbering('F2C') serah_no"; $qry = $this->db->query($sql); if (!$qry) { $this->db->trans_rollback(); echo "{$this->now()} ERR Get F2C Numbering : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} No F2C Numbering \n"; exit; } $f2c_no = $rows[0]["serah_no"]; //create F_PaymentKasir $arr_kasir = array( "F_PaymentKasirNumber" => $f2c_no, "F_PaymentKasirDate" => date("Y-m-d H:i:s"), "F_PaymentKasirCreated" => date("Y-m-d H:i:s"), "F_PaymentKasirCreated" => date("Y-m-d H:i:s"), "F_PaymentKasirUserID" => $this->KASIR_ONLINE_USER ); $qry = $this->db->insert("f_payment_kasir", $arr_kasir); if (!$qry) { $this->db->trans_rollback(); echo "{$this->now()} ERR Insert F Payment Kasir : {$this->db->error()['message']}\n"; exit; } $fPaymentKasirID = $this->db->insert_id(); //create F_PaymentKasirDetail foreach ($payment_rows as $p) { $arrd = array( "F_PaymentKasirDetailF_PaymentID" => $p["F_PaymentID"], "F_PaymentKasirDetailF_PaymentKasirID" => $fPaymentKasirID ); $qry = $this->db->insert("f_payment_kasir_detail", $arrd); if (!$qry) { $this->db->trans_rollback(); echo "{$this->now()} ERR Insert F Payment Kasir Detail : {$this->db->error()['message']}\n"; exit; } } if ($this->db->trans_status === false) { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; } else { $this->db->trans_commit(); echo "{$this->now()} Created Penyerahan Kasir $f2c_no\n"; } } public function generate_order() { echo "{$this->now()} Start Order Creation\n"; list($branchKelurahanID) = $this->get_branch_default(); $this->db->trans_begin(); $sql = "select t_onlinetx.* from t_onlinetx where T_OnlineTxIsActive = 'Y' and T_OnlineTxIsProcess = 'N' and T_OnlineTxIsLunas = 'Y'"; $db_msg = ""; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR T_OnlineTX : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); $counter = 0; $tx_ids = "0"; $pending_order = ""; foreach ($rows as $r) { $tx = json_decode(gzinflate($r["T_OnlineTxJsonGz"]), true); $firstOrder = "Y"; $havingTodayOrder = false; foreach ($tx["order"] as $order) { if ($order["T_OrderDate"] == date("Y-m-d")) { $havingTodayOrder = true; } else { if ($pending_order != "") { $pending_order .= ", "; } $pending_order .= $order["T_OrderQrCode"] . "|" . $order["T_OrderDate"]; } } if ($havingTodayOrder) { $counter++; $tx_ids .= "," . $r["T_OnlineTxID"]; foreach ($tx["order"] as $order) { //1. check pasien $pasien = $order["patient"]; list($M_PatientID, $M_PatientAddressID) = $this->get_patient_by_id( $pasien["M_PatientM_IdTypeID"], $pasien["M_PatientIDNumber"], $pasien["M_PatientNoreg"] ); if ($M_PatientID == 0) { list($M_PatientID, $M_PatientAddressID, $msg) = $this->add_pasien($pasien); if ($msg != "") { $db_msg .= "|$msg"; } } if ($M_PatientID == 0) { continue; } $mouID = $tx["T_TransactionM_MouID"]; $merchantCode = "-0-"; $paymentDate = ""; $pgRefNo = ""; foreach ($tx["duitku"] as $d) { if ($d["duitkuCbPaymentResultCode"] == "00") { $merchantCode = $d["duitkuCbMerchantCode"]; $paymentDate = $d["duitkuCbCreated"]; $pgRefNo = $d["duitkuCbReference"]; } } /* echo "Riau:\n"; print_r($tx["duitku"]); */ //2. order header $fo_note = "Order Online " . $order["T_OrderQrCode"]; if ($pgRefNo != "") { $fo_note .= ", PaymentRef# $pgRefNo"; } if ($order["Test_PurposeName"] != "") { $fo_note .= ", " . $order["Test_PurposeName"]; } if ($this->SENDER_DOCTOR_ID == 0 || $this->SENDER_ADDRESS_ID == 0) { $db_msg = "Error Dr Sender not set."; } if ($this->PJ_DOCTOR_ID == 0) { $db_msg = "Error Dr PJ not set."; } $xheader = array( 'date' => $order['T_OrderDate'] . ' ' . $order['T_OrderTime'], 'patient_id' => $M_PatientID, 'age' => $this->get_age($pasien["M_PatientDOB"], $order["T_OrderDate"]), 'sender_doctor_id' => $this->SENDER_DOCTOR_ID, 'sender_address_id' => $this->SENDER_ADDRESS_ID, 'pj_doctor_id' => $this->PJ_DOCTOR_ID, 'company_id' => $this->get_company_id($tx["T_TransactionM_MouID"]), 'mou_id' => $tx["T_TransactionM_MouID"], 'lang_id' => '1', 'lang_si' => 'N', 'doctor_note' => '', 'fo_note' => $fo_note, 'queue' => '', 'received_sample' => 'N', 'lang_id_2' => '0', 'lang_si_2' => 'N', ); //3. delivery $xdelivery = array(); foreach ($order["delivery"] as $d) { $xdel = array( 'address_id' => $M_PatientAddressID, 'delivery_id' => $d['T_OrderDeliveriesM_DeliveryID'], 'delivery_type_id' => $d["T_OrderDeliveriesM_DeliveryTypeID"], 'senderdoctorid' => $this->SENDER_DOCTOR_ID, 'senderaddressid' => $this->SENDER_ADDRESS_ID, 'note' => $d["T_OrderDeliveriesDestination"], 'kelurahan' => intval($branchKelurahanID) ); $xdelivery[] = $xdel; } //4. order $xdetails = $this->populate_test($mouID, $order["detail"]); if (count($xdetails) == 0) { $db_msg .= "| Err Populate Detail : No Test "; $db_msg .= "|" . $this->db->last_query() . "\n"; continue; } $xreq = array( 'status' => 'Y', 'reqs' => '[]' ); $header_json = json_encode($xheader); $delivery_json = json_encode($xdelivery); $detail_json = json_encode($xdetails); $req_json = json_encode($xreq); $sql = "CALL `sp_fo_register_save_online`( 0, '{$header_json}', '{$delivery_json}', '{$detail_json}', '{$req_json}', '{$this->ONLINE_USER_ID}' );"; $qry = $this->db->query($sql); if (!$qry) { $db_msg .= "| sp_fo_register_save_online : " . $this->db->error()["message"]; $db_msg .= "|" . $this->db->last_query(); continue; } $rows = $qry->result_array(); $this->clean_mysqli_connection($this->db->conn_id); $orderID = 0; if (count($rows) > 0) { if ($rows[0]["status"] == "ERR") { $db_msg .= "| Order Creation : " . $rows[0]["message"]; } else { $r_data = json_decode($rows[0]["data"], true); $orderID = $r_data["id"]; $orderNo = $r_data["number"]; echo "{$this->now()} Order {$orderNo} from {$r['T_OnlineTxNumbering']}\n"; } } if ($orderID == 0) { $db_msg .= "|Order Creation Failed No Order"; } //5. update t_onlineorder $arr = array( "T_OnlineOrderT_OnlineTxID" => $r["T_OnlineTxID"], "T_OnlineOrderT_OrderID" => $order["T_OrderID"], "T_OnlineOrderM_PatientID" => $M_PatientID, "T_OnlineOrderIsFirst" => $firstOrder, "T_OnlineOrderT_OrderHeaderID" => $orderID, "T_OnlineOrderEHAC" => $order["T_OrderEHAC"], "T_OnlineOrderT_OrderQrCode" => $order["T_OrderQrCode"], "T_OnlineOrderIsKlinisi" => $order["T_OrderIsKlinisi"], "T_OnlineOrderBookingDate" => $order["T_OrderDate"] . " " . $order["T_OrderTime"] ); $qry = $this->db->insert("t_onlineorder", $arr); if (!$qry) { $db_msg .= "| sp_fo_register_save_online : " . $this->db->error()["message"]; } if ($db_msg != "") { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; exit; } //6. janji hasil $allow_janji_hasil_error = true; $resultOptionID = 0; foreach ($order["detail"] as $d) { if ($resultOptionID == 0) { $resultOptionID = $d["T_OrderDetailT_ResultOptionID"]; } if ($d["T_OrderDetailT_ResultOptionID"] < $resultOptionID) { $resultOptionID = $d["T_OrderDetailT_ResultOptionID"]; } } list($promise_status, $message) = $this->fix_promise_reguler($orderID); if (!$promise_status) { if (!$allow_janji_hasil_error) { $db_msg .= "\n|[Janji Hasil] " . $message; } list($fix_status, $msg) = $this->fix_promise_empty($orderID); if (!$fix_status) { if (!$allow_janji_hasil_error) { $db_msg .= "\nFix Empty: " . $msg; } } } $arr_subcategory = array_map(function($r) { return $r["T_SubCategoryID"]; }, $order["detail"]); $all_is_antigen = false; foreach($arr_subcategory as $subcategory) { if ($subcategory != 2) { $all_is_antigen = false; } else { $all_is_antigen = true; } } if (! $all_is_antigen) { if ($resultOptionID == 1) { $this->fix_promise_cito($orderID, 0); } elseif ($resultOptionID == 2) { $this->fix_promise_cito($orderID, 1); } } if ($resultOptionID == 1 || $resultOptionID == 2) { $stat = $this->get_cito($resultOptionID); if ($stat["status"] == "OK") { $natCitoID = $stat["result"]; } else { $db_msg .= "|ERR Cito {$stat['message']}"; $natCitoID = 0; } $arr_cito = array( "T_OrderHeaderIsCito" => "Y", "T_OrderHeaderNat_CitoID" => $natCitoID ); $this->db->where("T_OrderHeaderID", $orderID); $qry = $this->db->update("t_orderheader", $arr_cito); if (!$qry) { $db_msg .= "| ERR Update T_OrderHeader Cito : " . $this->db->error()["message"] . "| {$this->db->last_query()}"; } } //8. payment $rst = $this->get_bank($merchantCode); if ($rst["status"] != "OK") { $db_msg .= "|" . $rst["message"]; } $bankAccountID = $rst["bank"]["pgBankM_BankAccountID"]; $paymentTypeID = $rst["bank"]["pgBankM_PaymentTypeID"]; $paymentNote = "PG Ref No# " . $pgRefNo; $rst_payment = $this->do_pelunasan( $orderID, $bankAccountID, $paymentTypeID, $paymentDate, $paymentNote ); if (!$rst_payment) { $db_msg .= "|Error Payment $orderNo"; } $firstOrder = "N"; } } } $sql = "update t_onlinetx set T_OnlineTxIsProcess='Y' where T_OnlineTxID in ($tx_ids)"; $qry = $this->db->query($sql); if (!$qry) { $db_msg .= "| Update T_OnlineTx : " . $this->db->error()["message"]; } if ($db_msg != "") { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; exit; } if ($this->db->trans_status === false) { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; } else { $this->db->trans_commit(); echo "{$this->now()} Created $counter Order\n"; if ($pending_order != "") { echo "{$this->now()} Pending Order $pending_order\n"; } } } public function fix_ehac($date) { echo "{$this->now()} Start Order Creation\n"; list($branchKelurahanID) = $this->get_branch_default(); $this->db->trans_begin(); $sql = "select t_onlinetx.* from t_onlinetx where T_OnlineTxIsActive = 'Y' and T_OnlineTxIsProcess = 'Y' and date(T_OnlineTxDate) = date(?) "; $db_msg = ""; $qry = $this->db->query($sql, array($date)); if (!$qry) { echo "{$this->now()} ERR T_OnlineTX : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); $counter = 0; $tx_ids = "0"; $pending_order = ""; foreach ($rows as $r) { $tx = json_decode(gzinflate($r["T_OnlineTxJsonGz"]), true); echo "TXS: " . $r["T_OnlineTxID"] . "\n"; $orders = $tx["order"]; foreach ($orders as $order) { if ($order["T_OrderEHAC"] > 0) { echo "Ehac found :\n"; $orderID = $order["T_OrderID"]; $txID = $tx["T_TransactionID"]; $ehacAmount = $order["T_OrderEHAC"]; $sql = "update t_onlineorder join t_onlinetx on T_OnlineOrderT_OnlineTxID = T_OnlineTxID and T_OnlineTxOrgID = ? and T_OnlineOrderT_OrderID = ? and T_OnlineOrderEHAC = 0 set T_OnlineOrderEHAC = ?"; $qry = $this->db->query($sql, array($txID, $orderID, $ehacAmount)); if (!$qry) { echo "Err Updte Ehac : " . $this->db->error()["message"] . "|" . $this->db->last_query(); echo "\n"; } else { echo "Updated Ehac " . $this->db->affected_rows() . "\n"; } } } } if ($this->db->trans_status === false) { $this->db->trans_rollback(); echo "{$this->now()} ERR Commit \n"; } else { $this->db->trans_commit(); echo "{$this->now()} Done \n"; } } public function tx_ehac($tx, $txID) { $order = $tx["order"]; $sum_ehac = 0; foreach ($order as $o) { $sum_ehac += $o["T_OrderEHAC"]; } $arr = array( "T_OnlineTxID" => $txID, "T_OnlineTxEhacFee" => $sum_ehac ); $qry = $this->db->where("T_OnlineTxID", $txID); $qry = $this->db->update("t_onlinetx", $arr); if (!$qry) { return array(false, $this->db->error()["message"]); } return array(true, ""); } public function index() { $sql = "select * from m_branch where M_BranchIsActive='Y' and M_BranchIsDefault='Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} ERR : No Default Branch\n"; exit; } $branchID = $rows[0]["M_BranchID"]; $date = date("Y-m-d H:i:s"); $maxID = $this->last_tx(); echo "{$this->now()} Start Download Transaction [last tx $maxID] \n"; $url = $this->url; $param = array("branchID" => $branchID, "date" => $date, "maxID" => $maxID); $z_param = gzdeflate(json_encode($param), 9); $resp = $this->post($url, $z_param); if ($resp["status"] == "OK") { $this->db->trans_begin(); $db_msg = ""; $tx_counter = 0; $order_counter = 0; foreach ($resp["txs"] as $tx) { $ref = ""; foreach ($tx["duitku"] as $d) { if ($ref != "") { $ref .= "|"; } $ref .= $d["duitkuCbReference"] . "^" . $d["duitkuCbPaymentResultCode"]; } $tx_order = $tx["order"]; $px_name = ""; $px_qrcode = ""; $px_date = ""; foreach ($tx_order as $txo) { if ($px_date != "") { $px_date .= ","; } $x_date = $txo["T_OrderDate"] . " " . $txo["T_OrderTime"]; if (strtotime($x_date)) { $px_date .= date("d-m-Y H:i", strtotime($x_date)); } else { $px_date .= $txo["T_OrderDate"] . " " . $txo["T_OrderTime"]; } if ($px_qrcode != "") { $px_qrcode .= ","; } $px_qrcode .= $txo["T_OrderQrCode"]; if ($px_name != "") { $px_name .= ","; } $px_name .= $txo["patient"]["M_PatientName"]; } $dt = array( "T_OnlineTxOrgID" => $tx["T_TransactionID"], "T_OnlineTxDate" => $tx["T_TransactionDate"], "T_OnlineTxNumbering" => $tx["T_TransactionNumbering"], "T_OnlineTxTotal" => $tx["T_TransactionTotal"], "T_OnlineTxFee" => $tx["T_TransactionFee"], "T_OnlineTxPgReference" => $ref, "T_OnlineTxJsonGz" => gzdeflate(json_encode($tx), 9), "T_OnlineTxBookingQrCode" => $px_qrcode, "T_OnlineTxBookingDate" => $px_date, "T_OnlineTxBookingName" => $px_name, "T_OnlineTxConfig" => json_encode($tx["config"]) ); if ($this->exists_tx($tx["T_TransactionID"])) { $this->db->where("T_OnlineTxOrgID", $tx["T_TransactionID"]); $qry = $this->db->update("t_onlinetx", $dt); $tOnlineTxID = $this->get_tx_id($tx["T_TransactionID"]); } else { $qry = $this->db->insert("t_onlinetx", $dt); $tOnlineTxID = $this->db->insert_id(); } if (!$qry) { $db_msg .= "|Insert T_OnlineTx " . $this->db->error()['message']; $db_msg .= $this->db->last_query(); } list($statusEhac, $db_msg) = $this->tx_ehac($tx, $tOnlineTxID); $tx_counter++; } if ($db_msg != "") { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; exit; } if ($this->db->trans_status === false) { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; } else { $this->db->trans_commit(); echo "{$this->now()} Downloaded $tx_counter Transaction\n"; } } else { echo "{$this->now()} ERR : {$resp['message']}\n"; } } public function non_lunas() { $sql = "select * from m_branch where M_BranchIsActive='Y' and M_BranchIsDefault='Y'"; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); if (count($rows) == 0) { echo "{$this->now()} ERR : No Default Branch\n"; exit; } $branchID = $rows[0]["M_BranchID"]; $date = date("Y-m-d H:i:s"); $maxID = $this->last_tx(); echo "{$this->now()} Start Download Transaction [last tx $maxID] \n"; $url = $this->url . "non_lunas"; $param = array("branchID" => $branchID); $z_param = gzdeflate(json_encode($param), 9); $resp = $this->post($url, $z_param); if ($resp["status"] == "OK") { $db_msg = ""; $this->db->trans_begin(); $tx_counter = 0; $order_counter = 0; foreach ($resp["txs"] as $tx) { $ref = ""; foreach ($tx["duitku"] as $d) { if ($ref != "") { $ref .= "|"; } $ref .= $d["duitkuCbReference"] . "^" . $d["duitkuCbPaymentResultCode"]; } $dt = array( "T_OnlineTxOrgID" => $tx["T_TransactionID"], "T_OnlineTxDate" => $tx["T_TransactionDate"], "T_OnlineTxNumbering" => $tx["T_TransactionNumbering"], "T_OnlineTxTotal" => $tx["T_TransactionTotal"], "T_OnlineTxFee" => $tx["T_TransactionFee"], "T_OnlineTxPgReference" => $ref, "T_OnlineTxIsLunas" => "N", "T_OnlineTxJsonGz" => gzdeflate(json_encode($tx), 9) ); if ($this->exists_tx($tx["T_TransactionID"])) { $this->db->where("T_OnlineTxOrgID", $tx["T_TransactionID"]); $qry = $this->db->update("t_onlinetx", $dt); } else { $qry = $this->db->insert("t_onlinetx", $dt); } if (!$qry) { $db_msg .= "|" . $this->db->error()['message']; } $tx_counter++; } if ($db_msg != "") { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; exit; } if ($this->db->trans_status === false) { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; } else { $this->db->trans_commit(); echo "{$this->now()} Downloaded $tx_counter Transaction\n"; } } else { echo "{$this->now()} ERR : {$resp['message']}\n"; } } public function update_delivery($orderID, $deliveries) { $db_msg = ""; $sql = "update t_orderdelivery set T_OrderDeliveryIsActive = 'N' where T_OrderDeliveryT_OrderHeaderID = ?"; $qry = $this->db->query($sql, array($orderID)); if (!$qry) { return "ERR Update Delivery | " . $this->db->error()["message"] . "|" . $this->db->last_query(); } foreach ($deliveries as $d) { $arr = array( "T_OrderDeliveryT_OrderHeaderID" => $orderID, "T_OrderDeliveryM_DeliveryID" => $d["T_OrderDeliveriesM_DeliveryID"], "T_OrderDeliveryM_DeliveryTypeID" => $d["T_OrderDeliveriesM_DeliveryTypeID"], "T_OrderDeliveryDestination" => $d["T_OrderDeliveriesDestination"] ); $qry = $this->db->insert("t_orderdelivery", $arr); if (!$qry) { $db_msg .= "ERR Update Delivery | " . $this->db->error()["message"] . "|" . $this->db->last_query() . "\n"; } } return $db_msg; } public function generate_order_non_lunas() { echo "{$this->now()} Start Order Creation\n"; list($branchKelurahanID) = $this->get_branch_default(); $this->db->trans_begin(); $sql = "select * from t_onlinetx where T_OnlineTxIsActive = 'Y' and T_OnlineTxIsProcess = 'N' and T_OnlineTxIsLunas = 'N'"; $db_msg = ""; $qry = $this->db->query($sql); if (!$qry) { echo "{$this->now()} ERR : {$this->db->error()['message']}\n"; exit; } $rows = $qry->result_array(); $counter = 0; $tx_ids = "0"; foreach ($rows as $r) { $counter++; $tx = json_decode(gzinflate($r["T_OnlineTxJsonGz"]), true); $firstOrder = "Y"; $tx_ids .= "," . $r["T_OnlineTxID"]; foreach ($tx["order"] as $order) { //1. check pasien $pasien = $order["patient"]; list($M_PatientID, $M_PatientAddressID) = $this->get_patient_by_id( $pasien["M_PatientM_IdTypeID"], $pasien["M_PatientIDNumber"], $pasien["M_PatientNoreg"] ); if ($M_PatientID == 0) { list($M_PatientID, $M_PatientAddressID, $msg) = $this->add_pasien($pasien); if ($msg != "") { $db_msg .= "|$msg"; } } if ($M_PatientID == 0) { continue; } $mouID = $tx["T_TransactionM_MouID"]; $merchantCode = "-0-"; $paymentDate = ""; $pgRefNo = ""; foreach ($tx["duitku"] as $d) { if ($d["duitkuCbPaymentResultCode"] == "00") { $merchantCode = $d["duitkuCbMerchantCode"]; $paymentDate = $d["duitkuCbCreated"]; $pgRefNo = $d["duitkuCbReference"]; } } //2. order header $fo_note = "Order Online " . $order["T_OrderQrCode"]; if ($pgRefNo != "") { $fo_note .= ", PaymentRef# $pgRefNo"; } if ($order["Test_PurposeName"] != "") { $fo_note .= ", " . $order["Test_PurposeName"]; } if ($this->SENDER_DOCTOR_ID == 0 || $this->SENDER_ADDRESS_ID == 0) { $db_msg = "Error Dr Sender not set."; } if ($this->PJ_DOCTOR_ID == 0) { $db_msg = "Error Dr PJ not set."; } $xheader = array( 'date' => $order['T_OrderDate'] . ' ' . $order['T_OrderTime'], 'patient_id' => $M_PatientID, 'age' => $this->get_age($pasien["M_PatientDOB"], $order["T_OrderDate"]), 'sender_doctor_id' => $this->SENDER_DOCTOR_ID, 'sender_address_id' => $this->SENDER_ADDRESS_ID, 'pj_doctor_id' => $this->PJ_DOCTOR_ID, 'company_id' => $this->get_company_id($tx["T_TransactionM_MouID"]), 'mou_id' => $tx["T_TransactionM_MouID"], 'lang_id' => '1', 'lang_si' => 'N', 'doctor_note' => '', 'fo_note' => $fo_note, 'queue' => '', 'received_sample' => 'N', 'lang_id_2' => '2', 'lang_si_2' => 'N', ); //3. delivery $xdelivery = array(); foreach ($order["delivery"] as $d) { $xdel = array( 'address_id' => $d['T_OrderDeliveriesID'], 'delivery_id' => $d['T_OrderDeliveriesM_DeliveryID'], 'delivery_type_id' => $d["T_OrderDeliveriesM_DeliveryTypeID"], 'senderdoctorid' => $this->SENDER_DOCTOR_ID, 'senderaddressid' => $this->SENDER_ADDRESS_ID, 'note' => $d["T_OrderDeliveriesDestination"], 'kelurahan' => $branchKelurahanID ); $xdelivery[] = $xdel; } //4. order $xdetails = $this->populate_test($mouID, $order["detail"]); if (count($xdetails) == 0) { $db_msg .= "| Err Populate Detail : No Test "; $db_msg .= "|" . $this->db->last_query() . "\n"; continue; } $xreq = array( 'status' => 'Y', 'reqs' => '[]' ); $header_json = json_encode($xheader); $delivery_json = json_encode($xdelivery); $detail_json = json_encode($xdetails); $req_json = json_encode($xreq); $sql = "CALL `sp_fo_register_save_online`( 0, '{$header_json}', '{$delivery_json}', '{$detail_json}', '{$req_json}', '{$this->ONLINE_USER_ID}' );"; $qry = $this->db->query($sql); if (!$qry) { $db_msg .= "| sp_fo_register_save_online : " . $this->db->error()["message"]; $db_msg .= "|" . $this->db->last_query(); continue; } $rows = $qry->result_array(); $this->clean_mysqli_connection($this->db->conn_id); $orderID = 0; if (count($rows) > 0) { if ($rows[0]["status"] == "ERR") { $db_msg .= "| Order Creation : " . $rows[0]["message"]; } else { $r_data = json_decode($rows[0]["data"], true); $orderID = $r_data["id"]; $orderNo = $r_data["number"]; echo "{$this->now()} Order {$orderNo} from {$r['T_OnlineTxNumbering']}\n"; } } if ($orderID == 0) { $db_msg .= "|Order Creation Failed"; } //5. update t_onlineorder $arr = array( "T_OnlineOrderT_OnlineTxID" => $r["T_OnlineTxID"], "T_OnlineOrderT_OrderID" => $order["T_OrderID"], "T_OnlineOrderEHAC" => $order["T_OrderEHAC"], "T_OnlineOrderM_PatientID" => $M_PatientID, "T_OnlineOrderIsFirst" => $firstOrder, "T_OnlineOrderT_OrderHeaderID" => $orderID, "T_OnlineOrderT_OrderQrCode" => $order["T_OrderQrCode"], "T_OnlineOrderBookingDate" => $order["T_OrderDate"] . " " . $order["T_OrderTime"] ); $qry = $this->db->insert("t_onlineorder", $arr); if (!$qry) { $db_msg .= "| sp_fo_register_save_online : " . $this->db->error()["message"]; } if ($db_msg != "") { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; exit; } //6. janji hasil $resultOptionID = 0; foreach ($order["detail"] as $d) { if ($resultOptionID == 0) { $resultOptionID = $d["T_OrderDetailT_ResultOptionID"]; } if ($d["T_OrderDetailT_ResultOptionID"] < $resultOptionID) { $resultOptionID = $d["T_OrderDetailT_ResultOptionID"]; } } $this->fix_promise_reguler($orderID); if ($resultOptionID == 1) { $this->fix_promise_cito($orderID, 0); } elseif ($resultOptionID == 2) { $this->fix_promise_cito($orderID, 1); } if ($resultOptionID == 1 || $resultOptionID == 2) { $stat = $this->get_cito($resultOptionID); if ($stat["status"] == "OK") { $natCitoID = $stat["result"]; } else { $db_msg .= "|ERR Cito {$stat['message']}"; $natCitoID = 0; } $arr_cito = array( "T_OrderHeaderIsCito" => "Y", "T_OrderHeaderNat_CitoID" => $natCitoID ); $this->db->where("T_OrderHeaderID", $orderID); $qry = $this->db->update("t_orderheader", $arr_cito); if (!$qry) { $db_msg .= "| ERR Update T_OrderHeader Cito : " . $this->db->error()["message"] . "| {$this->db->last_query()}"; } } //delivery $db_msg .= $this->update_delivery($orderID, $order["delivery"]); $firstOrder = "N"; } } $sql = "update t_onlinetx set T_OnlineTxIsProcess='Y' where T_OnlineTxID in ($tx_ids)"; $qry = $this->db->query($sql); if (!$qry) { $db_msg .= "| Update T_OnlineTx : " . $this->db->error()["message"]; } if ($db_msg != "") { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; exit; } if ($this->db->trans_status === false) { $this->db->trans_rollback(); echo "{$this->now()} ERR : {$db_msg}\n"; } else { $this->db->trans_commit(); echo "{$this->now()} Created $counter Order\n"; } } public function post($url, $data) { echo "{$this->now()} DEBUG : $url\n"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_TIMEOUT, 120); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data) )); $z_result = curl_exec($ch); if (curl_errno($ch) > 0) { return array( "status" => "ERR", "message" => curl_error($ch) ); } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode != 200) { return array( "status" => "ERR", "message" => "Http Response : $httpCode | $z_result" ); } $result = gzinflate($z_result); $j_result = json_decode($result, true); if (!$j_result) { return array( "status" => "ERR", "message" => "JSON invalid: $z_result" ); } return $j_result; } } // asumsi 1 order 1 janji hasil