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

250 lines
7.9 KiB
PHP

<?php
class Uploader 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 get_patient($patientID)
{
$sql = "select * from m_patient where M_PatientID = ? and M_PatientIsActive = 'Y'";
$qry = $this->db->query($sql, [$patientID]);
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] Patient ID $patientID not found.");
return false;
exit;
}
$patient = $rows[0];
$sql = "select * from m_patientaddress where M_PatientAddressM_PatientID=? and M_PatientAddressIsActive = 'Y'";
$qry = $this->db->query($sql, [$patientID]);
if (!$qry) {
$this->log("[ERR] " . $this->db->error()["message"] . "|" . $this->db->last_query());
exit;
};
$rows = $qry->result_array();
$patient["address"] = $rows;
return $patient;
}
function get_user_name($userID)
{
$sql = "select * from m_user where M_UserID = ?";
$qry = $this->db->query($sql, [$userID]);
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] User ID $userID not found.");
exit;
}
return $rows[0]["M_UserUsername"];
}
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 process_id($pid)
{
$max_process = 300;
$sql = "select
M_PatientNatLogID, M_PatientNatLogJson
from m_patient_nat_log
where M_PatientNatLogStatus <> 'Sent'
and M_PatientNatLogRetry < 10
and M_PatientNatLogID = ?
limit 0,$max_process";
if ($pid == 0) {
$sql = "select
M_PatientNatLogID, M_PatientNatLogJson
from m_patient_nat_log
where M_PatientNatLogStatus <> 'Sent'
and M_PatientNatLogRetry < 10
limit 0,$max_process";
}
$qry = $this->db->query($sql, [$pid]);
if (!$qry) {
$this->log("[ERR] select m_patient_nat_log " . $this->db->error()["message"] . "|" . $this->db->last_query());
exit;
}
$rows = $qry->result_array();
$logs = [];
list($branchID, $branchCode) = $this->get_branch();
$log_rows = [];
foreach ($rows as $r) {
$json = json_decode($r["M_PatientNatLogJson"], true);
if ($json) {
$patientID = $json["lokalM_PatientID"];
$l_patient = $this->get_patient($patientID);
if ($l_patient === false) continue;
$r_patient = $json["remote"];
$userID = $json["userID"];
$userName = $this->get_user_name($userID);
$log_rows[$patientID][] = $r["M_PatientNatLogID"];
$logs[$patientID] = [
"branchID" => $branchID,
"branchCode" => $branchCode,
"userName" => $userName,
"local" => $l_patient,
"remote" => $r_patient
];
}
}
$j_param = json_encode($logs);
print_r($logs);
echo "<pre>J Param :
$j_param</pre>";
}
function process($debug = "")
{
$max_process = 300;
$sql = "select
M_PatientNatLogID, M_PatientNatLogJson
from m_patient_nat_log
where M_PatientNatLogStatus <> 'Sent'
and M_PatientNatLogRetry < 10
limit 0,$max_process";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log("[ERR] select m_patient_nat_log " . $this->db->error()["message"] . "|" . $this->db->last_query());
exit;
}
$rows = $qry->result_array();
$logs = [];
list($branchID, $branchCode) = $this->get_branch();
$log_rows = [];
$not_found_patient = [];
foreach ($rows as $r) {
$json = json_decode($r["M_PatientNatLogJson"], true);
if ($json) {
$patientID = $json["lokalM_PatientID"];
$l_patient = $this->get_patient($patientID);
if ($l_patient === false) {
$not_found_patient[] = $r["M_PatientNatLogID"];
continue;
}
$r_patient = $json["remote"];
$userID = $json["userID"];
$userName = $this->get_user_name($userID);
$log_rows[$patientID][] = $r["M_PatientNatLogID"];
$logs[$patientID] = [
"branchID" => $branchID,
"branchCode" => $branchCode,
"userName" => $userName,
"local" => $l_patient,
"remote" => $r_patient
];
}
}
if (count($not_found_patient) > 0) {
$nfs = implode(",",$not_found_patient);
$sql = "update m_patient_nat_log set M_PatientNatLogStatus='NF' where M_PatientNatLogID in ( $nfs)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log("[ERR] update m_patient_nat_log " . $this->db->error()["message"] . "|" . $this->db->last_query());
} else {
$this->log("[INFO] update not found " . count($not_found_patient));
}
}
$j_param = json_encode($logs);
$size = round(strlen($j_param) / 1024, 2);
$z_param = gzcompress($j_param, 9);
$size_z = round(strlen($z_param) / 1024, 2);
$url = $this->NAT_PATIENT_API . "upload_confirm";
$this->log("Post to $url | size : $size k | comprezzed $size_z k");
$resp = $this->post($url, $z_param);
$j_resp = json_decode($resp, true);
print($resp);
if ($j_resp["status"] == "OK") {
if (count($j_resp["result"]["new"]) > 0) {
$logIds = [];
foreach ($j_resp["result"]["new"] as $r_patientID) {
$logIds = array_merge($logIds, $log_rows[$r_patientID]);
}
if (count($logIds) > 0) {
$s_logIds = implode(",", $logIds);
$sql = "update m_patient_nat_log set M_PatientNatLogStatus = 'Sent' where M_PatientNatLogID in ( $s_logIds )";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log("[ERR] update m_patient_nat_log " . $this->db->error()["message"] . "|" . $this->db->last_query());
exit;
}
}
}
if (count($j_resp["result"]["update"]) > 0) {
$logIds = [];
foreach ($j_resp["result"]["update"] as $r_patientID) {
$logIds = array_merge($logIds, $log_rows[$r_patientID]);
}
if (count($logIds) > 0) {
$s_logIds = implode(",", $logIds);
$sql = "update m_patient_nat_log set M_PatientNatLogStatus = 'Sent' where M_PatientNatLogID in ( $s_logIds )";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log("[ERR] update m_patient_nat_log " . $this->db->error()["message"] . "|" . $this->db->last_query());
exit;
}
}
}
}
}
function log($msg)
{
// $dt = Date("Y-m-d H:i:s");
// echo "$dt $msg \n";
echo $msg;
}
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;
}
}