377 lines
15 KiB
PHP
377 lines
15 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Rpt_lab_result — FPDF-based lab result report
|
|
* Endpoint: GET /tools/rpt_lab_result/pdf?token=...&PT_OrderHeaderID=...
|
|
*
|
|
* Menggunakan sp_rpt_hasil_header untuk data pasien,
|
|
* query langsung untuk detail hasil pemeriksaan.
|
|
* Cache PDP di-populate sebelum call SP lalu dihapus setelah selesai.
|
|
*/
|
|
class Rpt_lab_result extends MY_Controller
|
|
{
|
|
public $db_onedev;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_onedev = $this->load->database('onedev', true);
|
|
$this->load->library('ibl_patient_decrypt');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->sys_ok(['message' => 'Use /tools/rpt_lab_result/pdf?PT_OrderHeaderID=<id>']);
|
|
}
|
|
|
|
public function pdf()
|
|
{
|
|
try {
|
|
$order_id = $this->_get_order_id();
|
|
if ($order_id <= 0) {
|
|
$this->sys_error('PT_OrderHeaderID wajib diisi');
|
|
return;
|
|
}
|
|
|
|
$username = trim($this->input->get('username', true) ?? $this->input->post('username', true) ?? '');
|
|
if ($username === '') {
|
|
$username = $this->sys_user['M_StaffName'] ?? $this->sys_user['M_UserUsername'] ?? 'ADMIN';
|
|
}
|
|
|
|
// Populate cache PDP
|
|
$cache_id = $this->ibl_patient_decrypt->populate_cache_by_order($order_id);
|
|
|
|
// Header data via SP
|
|
$header = $this->_fetch_header($order_id, $username);
|
|
if (!$header) {
|
|
$this->ibl_patient_decrypt->delete_cache($cache_id);
|
|
$this->sys_error('Data order tidak ditemukan');
|
|
return;
|
|
}
|
|
|
|
// Detail hasil pemeriksaan
|
|
$details = $this->_fetch_details($order_id, $username);
|
|
|
|
// Company info (address, phone IBL)
|
|
$company_info = $this->_fetch_company_info();
|
|
|
|
// Delete cache setelah data diambil
|
|
$this->ibl_patient_decrypt->delete_cache($cache_id);
|
|
|
|
// Generate PDF
|
|
$pdf_bytes = $this->_build_pdf($header, $details, $company_info, $username);
|
|
|
|
$filename = 'lab_result_' . $order_id . '_' . date('Ymd') . '.pdf';
|
|
header('Content-Type: application/pdf');
|
|
header('Content-Disposition: inline; filename="' . $filename . '"');
|
|
header('Content-Length: ' . strlen($pdf_bytes));
|
|
echo $pdf_bytes;
|
|
exit;
|
|
|
|
} catch (Exception $e) {
|
|
$this->sys_error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Data fetching
|
|
// -------------------------------------------------------------------------
|
|
|
|
public function data()
|
|
{
|
|
$order_id = $this->_get_order_id();
|
|
if ($order_id <= 0) { $this->sys_error('PT_OrderHeaderID wajib'); return; }
|
|
$username = $this->input->get('username', true) ?: 'ADMIN';
|
|
$details = $this->_fetch_details($order_id, $username);
|
|
$this->sys_ok(['count' => count($details), 'rows' => array_slice($details, 0, 5), 'last_query' => $this->db_onedev->last_query()]);
|
|
}
|
|
|
|
private function _fetch_header($order_id, $username)
|
|
{
|
|
$qry = $this->db_onedev->query('CALL sp_rpt_hasil_header(?, ?)', [$order_id, $username]);
|
|
if (!$qry) return null;
|
|
$row = $qry->row_array();
|
|
if (method_exists($this, 'clean_mysqli_connection')) {
|
|
$this->clean_mysqli_connection($this->db_onedev->conn_id);
|
|
} else {
|
|
$this->_clean_multi_result();
|
|
}
|
|
return $row ?: null;
|
|
}
|
|
|
|
private function _fetch_details($order_id, $username = '')
|
|
{
|
|
$qry = $this->db_onedev->query('CALL sp_rpt_hasil_lab(?, ?)', [$order_id, $username]);
|
|
if (!$qry) return [];
|
|
$rows = $qry->result_array();
|
|
if (method_exists($this, 'clean_mysqli_connection')) {
|
|
$this->clean_mysqli_connection($this->db_onedev->conn_id);
|
|
} else {
|
|
$this->_clean_multi_result();
|
|
}
|
|
return $rows;
|
|
}
|
|
|
|
private function _fetch_company_info()
|
|
{
|
|
$row = $this->db_onedev->query(
|
|
"SELECT S_SystemsCompanyAddress, S_SystemsCompanyCity, S_SystemsCompanyPhone, S_SystemsCompanyName FROM conf_systems LIMIT 1"
|
|
)->row_array();
|
|
return $row ?: [];
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// PDF builder
|
|
// -------------------------------------------------------------------------
|
|
|
|
private function _build_pdf(array $h, array $details, array $co, $username)
|
|
{
|
|
require_once APPPATH . 'third_party/fpdf/fpdf.php';
|
|
|
|
$pdf = new FPDF('P', 'mm', 'A4');
|
|
$pdf->SetMargins(10, 8, 10);
|
|
$pdf->SetAutoPageBreak(true, 10);
|
|
$pdf->AddPage();
|
|
|
|
$pw = $pdf->GetPageWidth(); // 210
|
|
$ml = 10;
|
|
$mr = 10;
|
|
$cw = $pw - $ml - $mr; // 190
|
|
|
|
// ── Page header ──────────────────────────────────────────────────────
|
|
$company_addr = $this->_s($co['S_SystemsCompanyAddress'] ?? 'Jl. LL. RE. Martadinata No. 135');
|
|
$company_city = $this->_s($co['S_SystemsCompanyCity'] ?? 'Bandung');
|
|
$company_phone = $this->_s($co['S_SystemsCompanyPhone'] ?? '(022)7271946');
|
|
$addr_line = $company_addr . ' ' . $company_city . ' Telp. ' . $company_phone;
|
|
$pj_name = $this->_s($h['M_DoctorName'] ?? '');
|
|
|
|
$pdf->SetFont('Arial', '', 8);
|
|
$pdf->SetXY($ml, 8);
|
|
$pdf->Cell($cw / 2, 5, $addr_line, 0, 0, 'L');
|
|
$pdf->SetX($ml + $cw / 2);
|
|
$pdf->Cell($cw / 2, 5, 'Penanggung Jawab : ' . $pj_name, 0, 1, 'R');
|
|
$pdf->SetX($ml);
|
|
$pdf->Cell($cw, 0.3, '', 'T', 1);
|
|
$pdf->Ln(1);
|
|
|
|
// ── Patient info (2 columns) ──────────────────────────────────────────
|
|
$lw = $cw * 0.50;
|
|
$rw = $cw * 0.50;
|
|
$lbl_w = 38;
|
|
$col_w = 4;
|
|
$val_w = $lw - $lbl_w - $col_w;
|
|
$lbl_rw = 38;
|
|
$val_rw = $rw - $lbl_rw - $col_w;
|
|
|
|
$pid = $this->_s($h['T_OrderHeaderLabNumber'] ?? '-');
|
|
$left_rows = [
|
|
['NO. REG', $this->_s($h['M_PatientNoReg'] ?? '-')],
|
|
['NAMA', $this->_s($h['M_PatientName'] ?? '-')],
|
|
['PENGIRIM', $this->_s($h['M_DoctorName2'] ?? '-')],
|
|
['KEL. PELANGGAN*', $this->_s($h['CorporateName'] ?? '-')],
|
|
['ALAMAT', $this->_s($h['M_PatientAddress'] ?? '-')],
|
|
];
|
|
$right_rows = [
|
|
['TANGGAL REG', $this->_s($h['T_OrderHeaderDate'] ?? '-')],
|
|
['PID', $pid],
|
|
['JENIS KELAMIN', $this->_s($h['Gender'] ?? '-')],
|
|
['TGL. LAHIR / USIA', $this->_s($h['Umur'] ?? '-')],
|
|
['NO. TLP. / HP', $this->_s($h['M_PatientHp'] ?? '-')],
|
|
];
|
|
|
|
$pdf->SetFont('Arial', '', 8);
|
|
$y_info_start = $pdf->GetY();
|
|
|
|
// Barcode (teks) di atas kolom kanan
|
|
$pdf->SetXY($ml + $lw, $y_info_start);
|
|
$pdf->SetFont('Arial', 'B', 9);
|
|
$pdf->Cell($rw, 6, '||| ' . $pid . ' |||', 0, 1, 'C');
|
|
$pdf->SetFont('Arial', '', 8);
|
|
|
|
$y_right_start = $pdf->GetY();
|
|
$y_left_cur = $y_info_start;
|
|
$y_right_cur = $y_right_start;
|
|
|
|
// Left column
|
|
foreach ($left_rows as $r) {
|
|
$y = $pdf->GetY();
|
|
$pdf->SetXY($ml, $y);
|
|
$pdf->SetFont('Arial', '', 8);
|
|
$pdf->Cell($lbl_w, 5, $r[0], 0, 0);
|
|
$pdf->Cell($col_w, 5, ':', 0, 0, 'C');
|
|
$pdf->SetXY($ml + $lbl_w + $col_w, $y);
|
|
$pdf->MultiCell($val_w, 5, $r[1], 0, 'L');
|
|
}
|
|
$y_left_cur = $pdf->GetY();
|
|
|
|
// Right column
|
|
$pdf->SetY($y_right_cur);
|
|
foreach ($right_rows as $r) {
|
|
$y = $pdf->GetY();
|
|
$pdf->SetXY($ml + $lw, $y);
|
|
$pdf->SetFont('Arial', '', 8);
|
|
$pdf->Cell($lbl_rw, 5, $r[0], 0, 0);
|
|
$pdf->Cell($col_w, 5, ':', 0, 0, 'C');
|
|
$pdf->SetXY($ml + $lw + $lbl_rw + $col_w, $y);
|
|
$pdf->MultiCell($val_rw, 5, $r[1], 0, 'L');
|
|
}
|
|
$y_right_cur = $pdf->GetY();
|
|
|
|
$pdf->SetY(max($y_left_cur, $y_right_cur) + 2);
|
|
$pdf->SetX($ml);
|
|
$pdf->Cell($cw, 0.3, '', 'T', 1);
|
|
$pdf->Ln(1);
|
|
|
|
// ── Table header ─────────────────────────────────────────────────────
|
|
$col = ['JENIS PEMERIKSAAN' => 67, 'HASIL' => 25, 'NILAI RUJUKAN' => 51, 'SATUAN' => 20, 'METODE' => 27];
|
|
$col_w_arr = array_values($col);
|
|
$row_h = 5;
|
|
|
|
$pdf->SetFont('Arial', 'B', 8);
|
|
$pdf->SetFillColor(240, 240, 240);
|
|
$pdf->SetX($ml);
|
|
foreach ($col as $label => $w) {
|
|
$pdf->Cell($w, $row_h + 1, $label, 'LRB', 0, 'C', true);
|
|
}
|
|
$pdf->Ln();
|
|
|
|
// ── Table rows ────────────────────────────────────────────────────────
|
|
// sp_rpt_hasil_lab columns:
|
|
// Nat_SubGroupName, Nat_SubSubGroupName, T_TestSasCode, T_TestName
|
|
// T_OrderDetailResult, T_OrderDetailNormalValueNote, T_OrderDetailNat_UnitName
|
|
// T_OrderdetailNat_MethodeName, T_OrderDetailResultFlag, qrreport
|
|
|
|
$prev_subgroup = null;
|
|
$prev_subsubgroup = null;
|
|
$qr_url = '';
|
|
|
|
foreach ($details as $d) {
|
|
$subgroup = strtoupper(trim($d['Nat_SubGroupName'] ?? ''));
|
|
$subsubgroup = trim($d['Nat_SubSubGroupName'] ?? '');
|
|
$sascode_len = strlen(trim($d['T_TestSasCode'] ?? ''));
|
|
$test_name = $this->_s(trim($d['T_TestName'] ?? ''));
|
|
$result = $this->_s(trim(strip_tags($d['T_OrderDetailResult'] ?? '')));
|
|
$normal = $this->_s(trim($d['T_OrderDetailNormalValueNote'] ?? $d['T_OrderDetailNormalValueDescription'] ?? ''));
|
|
$unit = $this->_s(trim($d['T_OrderDetailNat_UnitName'] ?? ''));
|
|
$method = $this->_s(trim($d['T_OrderdetailNat_MethodeName'] ?? $d['methodeName'] ?? ''));
|
|
$flag = trim($d['T_OrderDetailResultFlag'] ?? '');
|
|
$is_abnormal = ($flag !== '' && $flag !== 'N');
|
|
if (empty($qr_url) && !empty($d['qrreport'])) {
|
|
$qr_url = $d['qrreport'];
|
|
}
|
|
|
|
// Level 1: SubGroup (bold, full width)
|
|
if ($subgroup !== '' && $subgroup !== $prev_subgroup) {
|
|
$pdf->SetX($ml);
|
|
$pdf->SetFont('Arial', 'B', 8);
|
|
$pdf->Cell($cw, $row_h, $subgroup, 'LR', 1, 'L');
|
|
$prev_subgroup = $subgroup;
|
|
$prev_subsubgroup = null;
|
|
}
|
|
|
|
// Level 2: SubSubGroup (italic, full width)
|
|
if ($subsubgroup !== '' && $subsubgroup !== $prev_subsubgroup) {
|
|
$pdf->SetX($ml);
|
|
$pdf->SetFont('Arial', 'I', 8);
|
|
$pdf->Cell($cw, $row_h, ' ' . $this->_s($subsubgroup), 'LR', 1, 'L');
|
|
$prev_subsubgroup = $subsubgroup;
|
|
}
|
|
|
|
// Level 3: Test row — indented by SasCode length
|
|
$indent = 0;
|
|
$prefix = '';
|
|
if ($sascode_len >= 10 && $sascode_len < 12) { $indent = 3; }
|
|
elseif ($sascode_len >= 12 && $sascode_len < 14) { $indent = 6; $prefix = chr(183) . ' '; }
|
|
elseif ($sascode_len >= 14) { $indent = 9; $prefix = chr(183) . ' '; }
|
|
|
|
$display_name = $prefix . $test_name;
|
|
|
|
$pdf->SetFont('Arial', $is_abnormal ? 'B' : '', 8);
|
|
$x_start = $ml;
|
|
$y_start = $pdf->GetY();
|
|
|
|
// Hitung row height
|
|
$row_height = $row_h;
|
|
$texts = [$display_name, $result, $normal, $unit, $method];
|
|
foreach ($texts as $i => $txt) {
|
|
$avail = $col_w_arr[$i] - 2 - ($i === 0 ? $indent : 0);
|
|
if ($avail > 0 && $pdf->GetStringWidth($txt) > $avail) {
|
|
$lines = (int) ceil($pdf->GetStringWidth($txt) / $avail);
|
|
$row_height = max($row_height, $lines * $row_h);
|
|
}
|
|
}
|
|
|
|
// Render cells
|
|
$x_cur = $x_start;
|
|
foreach ($texts as $i => $txt) {
|
|
$w = $col_w_arr[$i];
|
|
$pad_l = ($i === 0) ? $indent : 0;
|
|
$align = ($i === 0) ? 'L' : 'C';
|
|
$pdf->SetXY($x_cur + $pad_l, $y_start);
|
|
$pdf->Cell($w - $pad_l, $row_height, $txt, 'LR', 0, $align);
|
|
$x_cur += $w;
|
|
}
|
|
$pdf->SetXY($ml, $y_start + $row_height);
|
|
$pdf->SetX($ml);
|
|
$pdf->Cell($cw, 0, '', 'T', 1);
|
|
}
|
|
|
|
// Tutup tabel
|
|
$pdf->SetX($ml);
|
|
$pdf->Cell($cw, 0, '', 'T', 1);
|
|
|
|
// ── QR code ───────────────────────────────────────────────────────────
|
|
if (!empty($qr_url)) {
|
|
$pdf->Ln(3);
|
|
$tmp = $this->_fetch_image_to_temp($qr_url);
|
|
if ($tmp) {
|
|
$pdf->SetX($ml);
|
|
$pdf->Image($tmp, $ml, $pdf->GetY(), 20, 20);
|
|
@unlink($tmp);
|
|
}
|
|
}
|
|
|
|
return $pdf->Output('S');
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Helpers
|
|
// -------------------------------------------------------------------------
|
|
|
|
private function _get_order_id()
|
|
{
|
|
foreach (['PT_OrderHeaderID', 'PID', 'order_id'] as $k) {
|
|
$v = $this->input->get($k, true) ?? $this->input->post($k, true) ?? ($this->sys_input[$k] ?? null);
|
|
if ($v !== null && $v !== '') return intval($v);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
private function _s($text)
|
|
{
|
|
return iconv('UTF-8', 'windows-1252//TRANSLIT', (string) $text);
|
|
}
|
|
|
|
private function _fetch_image_to_temp($url)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, CURLOPT_FOLLOWLOCATION => true]);
|
|
$data = curl_exec($ch);
|
|
curl_close($ch);
|
|
if (!$data) return null;
|
|
$tmp = tempnam(sys_get_temp_dir(), 'qr_') . '.png';
|
|
file_put_contents($tmp, $data);
|
|
return $tmp;
|
|
}
|
|
|
|
private function _clean_multi_result()
|
|
{
|
|
if (isset($this->db_onedev->conn_id) && $this->db_onedev->conn_id instanceof mysqli) {
|
|
while ($this->db_onedev->conn_id->more_results()) {
|
|
$this->db_onedev->conn_id->next_result();
|
|
}
|
|
}
|
|
}
|
|
}
|