db->query($sql); if (!$qry) { $this->log("[ERR] " . $this->db->error()["message"] . "|" . $this->db->last_query()); exit; } $rows = $qry->result_array(); if (count($rows) == 0) { $this->log("[ERR] Default Branch not found."); exit; } return [$rows[0]["M_BranchID"], $rows[0]["M_BranchCode"]]; } function download() { list($branchID, $branchCode) = $this->get_branch(); $url = $this->NAT_PATIENT_API . "download/" . $branchCode; $resp = $this->post($url, ""); $j_resp = json_decode($resp, true); $this->db->trans_begin(); if ($j_resp["status"] == "OK") { if (count($j_resp["data"]) > 0) { //insert to download and confirm $ids = []; $sql = "insert into m_patient_nat_download(M_PatientNatDownloadData) values(?)"; foreach ($j_resp["data"] as $d) { $ids[] = $d["logDownloadID"]; $qry = $this->db->query($sql, [json_encode($d["logDownloadData"])]); if (!$qry) { $this->db->trans_rollback(); $this->log("Error " . $j_resp["message"]); exit; } } $param = ["branchCode" => $branchCode, "ids" => $ids]; $j_param = json_encode($param); $url = $this->NAT_PATIENT_API . "download_confirm/"; $z_param = gzcompress($j_param, 9); $resp = $this->post($url, $z_param); $j_resp = json_decode($resp, true); if ($j_resp["status"] == "OK") { $this->db->trans_commit(); $this->log("Success"); } else { $this->db->trans_rollback(); $this->log("Err " . $j_resp["message"]); } } else { $this->log("No National Patient Downloaded Data"); } } else { $this->log("Error " . $j_resp["message"]); } } function process() { list($branchID, $branchCode) = $this->get_branch(); $this->db->trans_begin(); $sql = "select * from m_patient_nat_download where M_PatientNatDownloadIsProcessed = 'N' limit 0,50"; $qry = $this->db->query($sql); if (!$qry) { $this->db->trans_rollback(); $this->log("Error select m_patient_nat_download | " . $this->db->last_query()); exit; } $rows = $qry->result_array(); foreach ($rows as $r) { $data = json_decode($r["M_PatientNatDownloadData"], true); if ($branchCode == $data["patient"]["M_BranchCode"]) { $sql = "update m_patient set M_PatientIDNumber = ? where M_PatientID = ?"; $qry = $this->db->query($sql, [$data["patient"]["Nat_PatientIDNumber"], $data["patient"]["M_PatientID"]]); if (!$qry) { $this->db->trans_rollback(); $this->log("Error update m_patient | " . $this->db->last_query()); exit; } } else { $this->log("Invalid branch : " . $data["patient"]["M_BranchCode"]); } } $this->db->trans_commit(); $this->log("Success"); } public function post($url, $data) { $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, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/text", "Content-Length: " . strlen($data), ]); $result = curl_exec($ch); if (curl_error($ch) != "") { echo json_encode([ "status" => "ERR", "message" => "Http Error : " . curl_error($ch), ]); curl_close($ch); exit(); } curl_close($ch); return $result; } }