Files
2026-04-27 10:26:26 +07:00

140 lines
4.4 KiB
PHP

<?php
class Downloader extends MY_Controller
{
// dev
//var $NAT_PATIENT_API = "http://10.9.8.249/one-api/nat_patient/r_api/";
//prod
var $NAT_PATIENT_API = "http://192.168.50.250/one-api/nat_patient/r_api/";
function __construct()
{
parent::__construct();
}
function log($msg)
{
//$dt = Date("Y-m-d H:i:s");
//echo "$dt $msg \n";
echo $msg;
}
/*
create table m_patient_nat_download (
M_PatientNatDownloadID int not null auto_increment primary key,
M_PatientNatDownloadDate datetime default current_timestamp(),
M_PatientNatDownloadData text,
M_PatientNatDownloadIsProcessed varchar(1) default 'N',
key(M_PatientNatDownloadIsProcessed)
);
*/
function get_branch()
{
$sql = "select M_BranchID,M_BranchCode from m_branch where M_BranchIsDefault = 'Y' and M_BranchIsActive = 'Y'";
$qry = $this->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;
}
}