356 lines
12 KiB
PHP
356 lines
12 KiB
PHP
<?php
|
|
class Uploadqrreport extends MY_Controller
|
|
{
|
|
var $db;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
echo "API for upload QR report";
|
|
}
|
|
|
|
function reply_gz($resp)
|
|
{
|
|
echo gzcompress($resp);
|
|
exit();
|
|
}
|
|
|
|
public function get_data()
|
|
{
|
|
try {
|
|
$prm = $this->sys_input;
|
|
$limit = 10;
|
|
if (isset($prm['limit'])) {
|
|
$limit = trim($prm["limit"]);
|
|
$limit = $prm['limit'];
|
|
}
|
|
$sql = "SELECT QR_PrintOutID,
|
|
QR_PrintOutT_OrderHeaderID,
|
|
QR_PrintOutGroup_ResultID,
|
|
QR_PrintOutT_TestID,
|
|
QR_PrintOutGroup_ResultName,
|
|
QR_PrintOutUUID,
|
|
QR_PrintOutVerifyURL,
|
|
QR_PrintOutReportURL,
|
|
QR_PrintOutTempFilePath,
|
|
QR_PrintOutUploadStatus,
|
|
QR_PrintOutRetryCount,
|
|
QR_PrintOutLastRetryAt,
|
|
T_OrderHeaderDate,
|
|
T_OrderHeaderLabNumber
|
|
FROM qr_printout
|
|
JOIN t_orderheader ON QR_PrintOutT_OrderHeaderID = T_OrderHeaderID AND T_OrderHeaderIsActive = 'Y'
|
|
WHERE QR_PrintOutIsActive = 1
|
|
AND (QR_PrintOutUploadStatus = 'pending' OR QR_PrintOutUploadStatus = 'failed')
|
|
AND (QR_PrintOutRetryCount <= 3 OR QR_PrintOutRetryCount = 0)
|
|
AND QR_PrintOutReportURL IS NOT NULL
|
|
ORDER BY QR_PrintOutCreatedAt ASC
|
|
limit $limit";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
$this->sys_error_db("Failed to fetch QR printout data: " . $this->db->error()['message']);
|
|
exit;
|
|
}
|
|
|
|
$data = $qry->result_array();
|
|
|
|
$result = array(
|
|
'status' => 'OK',
|
|
'data' => $data
|
|
);
|
|
echo json_encode($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function get_json_upload($printoutID, $orderHeaderID)
|
|
{
|
|
try {
|
|
$sql = "SELECT QR_PrintOutID,
|
|
QR_PrintOutUUID,
|
|
QR_PrintOutVerifyURL,
|
|
QR_PrintOutReportURL
|
|
FROM qr_printout
|
|
WHERE QR_PrintOutID = ?";
|
|
$qry = $this->db->query($sql, array($printoutID));
|
|
if (!$qry) {
|
|
$this->sys_error_db("Failed to fetch json QR printout data for ID $printoutID: " . $this->db->error()['message']);
|
|
exit;
|
|
}
|
|
|
|
$data = $qry->row_array();
|
|
|
|
$report_url = $data['QR_PrintOutReportURL'];
|
|
|
|
// download isi file (binary PDF)
|
|
$pdfContent = $this->download_file($report_url);
|
|
|
|
if (!$pdfContent) {
|
|
$this->sys_error("Gagal download file dari URL");
|
|
exit;
|
|
}
|
|
|
|
if (strpos($pdfContent, '%PDF') === false) {
|
|
$this->sys_error("File bukan PDF! Kemungkinan URL BIRT error / butuh login");
|
|
exit;
|
|
}
|
|
|
|
$base64_file = base64_encode($pdfContent);
|
|
|
|
// ambil endpoint URL dari DB
|
|
$sql_url = "SELECT QR_ReportEndpointID,
|
|
QR_ReportEndpointUrl,
|
|
QR_ReportEndpointIsActive
|
|
FROM qr_report_endpoint
|
|
WHERE QR_ReportEndpointIsActive = 'Y' LIMIT 1";
|
|
$qry_url = $this->db->query($sql_url);
|
|
$endpoint_url = "";
|
|
if ($qry_url) {
|
|
$data_url = $qry_url->result_array();
|
|
if (count($data_url) > 0) {
|
|
$endpoint_url = $data_url[0]['QR_ReportEndpointUrl'];
|
|
}
|
|
} else {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Error Query Endpoint URL", $this->db);
|
|
exit;
|
|
}
|
|
|
|
// $url = "http://10.9.10.38:8899/xgw/upload";
|
|
$url = $endpoint_url . "upload";
|
|
// $token = "aW5pIHRva2VuIGJ1YXQgdXBsb2FkIGlibCBxciByZXBvcnQ=";
|
|
$token = "secure-token-libCBxciByZXBvcnQ=";
|
|
|
|
if (empty($data)) {
|
|
echo json_encode([
|
|
"status" => "ERR",
|
|
"message" => "No QR printout data found for ID $printoutID"
|
|
]);
|
|
exit;
|
|
} else {
|
|
$result = json_encode([
|
|
"qrcode" => $data['QR_PrintOutUUID'],
|
|
"url" => $data['QR_PrintOutReportURL'],
|
|
"type" => "pdf",
|
|
"name" => $data['QR_PrintOutUUID'] . '.pdf',
|
|
"base64_file" => $base64_file
|
|
], JSON_UNESCAPED_SLASHES);
|
|
|
|
$resp = $this->post($url, $result, $token);
|
|
// print_r('Response: ' . $resp);
|
|
// exit;
|
|
|
|
$r_res = json_decode($resp, true);
|
|
|
|
|
|
if (isset($r_res['status']) && $r_res['status'] == 'OK') {
|
|
$this->log_insert_qr($printoutID, $orderHeaderID, $result, $resp);
|
|
|
|
echo json_encode([
|
|
"status" => "OK",
|
|
"data" => $r_res
|
|
]);
|
|
} else {
|
|
$this->log_insert_qr($printoutID, $orderHeaderID, $result, $resp);
|
|
|
|
echo json_encode([
|
|
"status" => "ERR",
|
|
"message" => "Failed to upload QR report for printout ID $printoutID",
|
|
"error" => $r_res
|
|
]);
|
|
}
|
|
}
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function update_status()
|
|
{
|
|
try {
|
|
$prm = $this->sys_input;
|
|
$id = 0;
|
|
if (isset($prm['id'])) {
|
|
$id = trim($prm["id"]);
|
|
$id = $prm['id'];
|
|
}
|
|
|
|
$status = "A";
|
|
if (isset($prm['status'])) {
|
|
$status = trim($prm["status"]);
|
|
$status = $prm['status'];
|
|
}
|
|
|
|
if ($id == 0) {
|
|
$this->sys_error("ID mandatory !");
|
|
exit;
|
|
}
|
|
|
|
if ($status == "A") {
|
|
$this->sys_error("status mandatory !");
|
|
exit;
|
|
}
|
|
|
|
$sql = "SELECT QR_PrintOutRetryCount
|
|
FROM qr_printout
|
|
WHERE QR_PrintOutID = ?";
|
|
$query = $this->db->query($sql, [$id]);
|
|
$last_qry = $this->db->last_query();
|
|
if (!$query) {
|
|
$this->sys_error_db("Failed to fetch QR printout data for ID $id: " . $this->db->error()['message']);
|
|
exit;
|
|
}
|
|
|
|
$retry = intval($query->result_array()[0]['QR_PrintOutRetryCount']) + 1;
|
|
if (count($query->result_array()) > 0) {
|
|
if ($status == "E") {
|
|
|
|
if ($retry > 3) {
|
|
// gagal permanen
|
|
$sql_update = "UPDATE qr_printout SET
|
|
QR_PrintOutUploadStatus = 'failed_permanent',
|
|
QR_PrintOutRetryCount = ?,
|
|
QR_PrintOutLastRetryAt = NOW()
|
|
WHERE QR_PrintOutID = ?";
|
|
} else {
|
|
// masih boleh retry
|
|
$sql_update = "UPDATE qr_printout SET
|
|
QR_PrintOutUploadStatus = 'failed',
|
|
QR_PrintOutRetryCount = ?,
|
|
QR_PrintOutLastRetryAt = NOW()
|
|
WHERE QR_PrintOutID = ?";
|
|
}
|
|
|
|
$qry_update = $this->db->query($sql_update, [$retry, $id]);
|
|
if (!$qry_update) {
|
|
$this->sys_error_db("Failed to update QR printout status for ID $id: " . $this->db->error()['message']);
|
|
exit;
|
|
}
|
|
} else if ($status == "S") {
|
|
$sql_update = "UPDATE qr_printout SET QR_PrintOutUploadStatus = 'uploaded',
|
|
QR_PrintOutUploadedAt = NOW()
|
|
WHERE QR_PrintOutID = ?";
|
|
$qry_update = $this->db->query($sql_update, [$id]);
|
|
if (!$qry_update) {
|
|
$this->sys_error_db("Failed to update QR printout status for ID $id: " . $this->db->error()['message']);
|
|
exit;
|
|
}
|
|
}
|
|
} else {
|
|
$this->sys_error("No QR printout data found for ID $id");
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok("OK");
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function log_insert_qr($printoutID, $orderHeaderID, $json, $response)
|
|
{
|
|
try {
|
|
$sql = "INSERT INTO one_lab_log.log_qr_printout(
|
|
Log_QR_PrintOutQR_PrintOutID,
|
|
Log_QR_PrintOutT_OrderHeaderID,
|
|
Log_QR_PrintOutJSON,
|
|
Log_QR_PrintOutResponse,
|
|
Log_QR_PrintOutDateTime) VALUES(?,?,?,?,NOW())";
|
|
$qry = $this->db->query($sql, array(
|
|
$printoutID,
|
|
$orderHeaderID,
|
|
$json,
|
|
$response
|
|
));
|
|
if (!$qry) {
|
|
$this->sys_error_db("Failed to insert log for QR printout ID $printoutID: " . $this->db->error()['message']);
|
|
exit;
|
|
}
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function get_report_url()
|
|
{
|
|
try {
|
|
$sql_url = "SELECT QR_ReportEndpointID,
|
|
QR_ReportEndpointUrl,
|
|
QR_ReportEndpointIsActive
|
|
FROM qr_report_endpoint
|
|
WHERE QR_ReportEndpointIsActive = 'Y' LIMIT 1";
|
|
$qry_url = $this->db->query($sql_url);
|
|
$endpoint_url = "";
|
|
if ($qry_url) {
|
|
$data_url = $qry_url->result_array();
|
|
if (count($data_url) > 0) {
|
|
$endpoint_url = $data_url[0]['QR_ReportEndpointUrl'];
|
|
}
|
|
} else {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Error Query Endpoint URL", $this->db);
|
|
exit;
|
|
}
|
|
$result = array(
|
|
"report_url" => $endpoint_url . "rpt/"
|
|
);
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function post($url, $data, $token)
|
|
{
|
|
$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, 10);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json",
|
|
"Content-Length: " . strlen($data),
|
|
"Authorization: Bearer $token",
|
|
]);
|
|
$result = curl_exec($ch);
|
|
|
|
if (curl_error($ch) != "") {
|
|
return "ERROR API [$url] : " . curl_error($ch) . "\n";
|
|
}
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
|
|
public function download_file($url)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
// kalau HTTPS tapi self-signed
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
if (curl_error($ch)) {
|
|
echo "CURL ERROR: " . curl_error($ch);
|
|
curl_close($ch);
|
|
return false;
|
|
}
|
|
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
}
|