diff --git a/application/controllers/tools/Medical_checkup_report.php b/application/controllers/tools/Medical_checkup_report.php index 254442ed..9705c0d1 100644 --- a/application/controllers/tools/Medical_checkup_report.php +++ b/application/controllers/tools/Medical_checkup_report.php @@ -70,25 +70,29 @@ class MedicalCheckupReportPdf extends FPDF class Medical_checkup_report extends MY_Controller { + const QR_GAP_FROM_FOOTER_MM = 5.0; + const QR_SIZE_MM = 22.0; + public function __construct() { parent::__construct(); $this->db_onedev = $this->load->database('onedev', true); + $this->load->library('Generateqrreport'); } public function index() { $this->sys_ok([ - 'message' => 'Use /tools/medical_checkup_report/pdf?T_OrderHeaderID=&username=' + 'message' => 'Use /tools/medical_checkup_report/pdf?T_OrderHeaderGroupResultDetailsT_OrderDetailID=&username=' ]); } public function pdf() { try { - $orderHeaderId = intval($this->input->get('T_OrderHeaderID', true)); - if ($orderHeaderId <= 0) { - $this->sys_error('T_OrderHeaderID mandatory'); + $orderDetailId = intval($this->input->get('T_OrderHeaderGroupResultDetailsT_OrderDetailID', true)); + if ($orderDetailId <= 0) { + $this->sys_error('T_OrderHeaderGroupResultDetailsT_OrderDetailID mandatory'); return; } @@ -97,6 +101,14 @@ class Medical_checkup_report extends MY_Controller $printBy = 'ADMIN'; } + $reportContext = $this->get_report_context($orderDetailId); + if (!$reportContext) { + $this->sys_error('Mapping order detail / group result tidak ditemukan'); + return; + } + + $orderHeaderId = intval($reportContext['order_header_id']); + $header = $this->get_header_data($orderHeaderId); if (!$header) { $this->sys_error('Data order tidak ditemukan'); @@ -106,6 +118,11 @@ class Medical_checkup_report extends MY_Controller $rows = $this->get_result_rows($orderHeaderId); $saran = $this->get_saran($orderHeaderId); $branch = $this->get_default_branch(); + $verifyUrl = $this->get_verify_url( + $orderHeaderId, + intval($reportContext['group_result_id']), + intval($reportContext['test_id']) + ); require_once APPPATH . 'third_party/fpdf/fpdf.php'; @@ -185,10 +202,15 @@ class Medical_checkup_report extends MY_Controller $pdf->Ln(4); $this->render_result_rows($pdf, $rows, $saran); + $tempQrPath = $this->render_last_page_qr($pdf, $verifyUrl); header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="medical_checkup_report_' . $orderHeaderId . '.pdf"'); - echo $pdf->Output('S'); + $output = $pdf->Output('S'); + if ($tempQrPath !== '') { + @unlink($tempQrPath); + } + echo $output; } catch (Exception $exc) { $this->sys_error($exc->getMessage()); } @@ -293,6 +315,61 @@ class Medical_checkup_report extends MY_Controller return trim((string)$row['saran']); } + private function get_report_context($orderDetailId) + { + $sql = "SELECT + ogd.T_OrderHeaderGroupResultDetailsT_OrderHeaderID AS order_header_id, + ogr.T_OrderHeaderGroupResultGroup_ResultID AS group_result_id, + od.T_OrderDetailT_TestID AS test_id + FROM t_orderheader_group_result_details ogd + JOIN t_orderheader_group_result ogr + ON ogr.T_OrderHeaderGroupResultID = ogd.T_OrderHeaderGroupResultDetailsT_OrderHeaderGroupResultID + AND ogr.T_OrderHeaderGroupResultIsActive = 'Y' + JOIN t_orderdetail od + ON od.T_OrderDetailID = ogd.T_OrderHeaderGroupResultDetailsT_OrderDetailID + AND od.T_OrderDetailIsActive = 'Y' + WHERE ogd.T_OrderHeaderGroupResultDetailsT_OrderDetailID = ? + AND ogd.T_OrderHeaderGroupResultDetailsIsActive = 'Y' + LIMIT 1"; + + $qry = $this->db_onedev->query($sql, [$orderDetailId]); + if (!$qry) { + return false; + } + + $row = $qry->row_array(); + if (!$row) { + return false; + } + + return $row; + } + + private function get_verify_url($orderHeaderId, $groupResultId, $testId) + { + $sql = "SELECT IFNULL(QR_PrintOutVerifyURL, '') AS verify_url + FROM one_lab.qr_printout + WHERE QR_PrintOutT_OrderHeaderID = ? + AND QR_PrintOutGroup_ResultID = ? + AND QR_PrintOutT_TestID = ? + AND QR_PrintOutIsActive = 1 + AND IFNULL(QR_PrintOutVerifyURL, '') <> '' + ORDER BY QR_PrintOutID DESC + LIMIT 1"; + + $qry = $this->db_onedev->query($sql, [$orderHeaderId, $groupResultId, $testId]); + if (!$qry) { + return ''; + } + + $row = $qry->row_array(); + if (!$row) { + return ''; + } + + return trim((string)$row['verify_url']); + } + private function render_result_rows($pdf, $rows, $saran = '') { $left = 14; @@ -571,6 +648,45 @@ class Medical_checkup_report extends MY_Controller } } + private function render_last_page_qr($pdf, $verifyUrl) + { + $verifyUrl = trim((string)$verifyUrl); + if ($verifyUrl === '') { + return ''; + } + + $tempQrPath = $this->generate_temp_qr_file($verifyUrl); + if ($tempQrPath === '') { + return ''; + } + + $footerTopY = $pdf->GetPageHeight() - 18.0; + $qrBottomY = $footerTopY - self::QR_GAP_FROM_FOOTER_MM; + $qrTopY = $qrBottomY - self::QR_SIZE_MM; + if ($pdf->GetY() > $qrTopY) { + $pdf->AddPage(); + } + + $pdf->Image($tempQrPath, 14, $qrTopY, self::QR_SIZE_MM, self::QR_SIZE_MM, 'PNG'); + return $tempQrPath; + } + + private function generate_temp_qr_file($verifyUrl) + { + $tempQrPath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) + . DIRECTORY_SEPARATOR + . 'medical_checkup_report_qr_' + . md5($verifyUrl . microtime(true)) + . '.png'; + + $isSaved = $this->generateqrreport->generateQRImageFile($verifyUrl, $tempQrPath, 4, 'H'); + if (!$isSaved || !file_exists($tempQrPath)) { + return ''; + } + + return $tempQrPath; + } + private function safe_text($text) { $text = (string)$text;