Files
BE_IBL/application/controllers/Merge_rpt.php
2026-04-15 15:24:53 +07:00

532 lines
22 KiB
PHP

<?php
class Merge_rpt extends MY_Controller
{
function corss()
{
global $_SERVER;
if (isset($_SERVER["HTTP_ORIGIN"])) {
header("Access-Control-Allow-Origin: " . $_SERVER["HTTP_ORIGIN"]);
} else {
header("Access-Control-Allow-Origin: */*");
}
header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS");
header(
"Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (
isset($_SERVER["REQUEST_METHOD"]) &&
$_SERVER["REQUEST_METHOD"] == "OPTIONS"
) {
http_response_code(200);
echo json_encode("OK");
exit();
}
}
function get_report($orderID, $username = "admin")
{
$rpt_lab =
"/birt/frameset?__report=report/one/lab/rpt_test_email.rptdesign";
$rpt_lab_en =
"/birt/frameset?__report=report/one/lab/rpt_test_eng_email.rptdesign";
$rpt_xray =
"/birt/run?__report=report/one/lab/rpt_hasil_so_xray_email.rptdesign";
$rpt_xray_en =
"/birt/run?__report=report/one/lab/rpt_hasil_so_xray_eng_email.rptdesign";
$rpt_xray_v2 =
"/birt/run?__report=report/one/lab/rpt_hasil_so_xray_2_email.rptdesign";
$rpt_xray_v2_en =
"/birt/run?__report=report/one/lab/rpt_hasil_so_xray_2_eng_email.rptdesign";
$rpt_elmd =
"/birt/run?__report=report/one/lab/rpt_hasil_so_elmd_email.rptdesign";
$rpt_elmd_en =
"/birt/run?__report=report/one/lab/rpt_hasil_so_elmd_eng_email.rptdesign";
$rpt_elmd_v2 =
"/birt/run?__report=report/one/lab/rpt_hasil_so_elmd_2_email.rptdesign";
$rpt_elmd_v2_en =
"/birt/run?__report=report/one/lab/rpt_hasil_so_elmd_2_eng_email.rptdesign";
$rpt_fisik_riwayat =
"/birt/run?__report=report/one/mcu/mcu_riwayat.rptdesign";
$rpt_fisik = "/birt/run?__report=report/one/mcu/mcu_fisik.rptdesign";
$rpt_mcu_resume =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_cp.rptdesign";
$rpt_mcu_resume_v2 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_p.rptdesign";
$rpt_mcu_resume_v3 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_cpp.rptdesign";
$rpt_mcu_resume_v4 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_pp.rptdesign";
$rpt_mcu_resume_v5 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_cp1.rptdesign";
$rpt_mcu_resume_v6 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_p1.rptdesign";
$rpt_mcu_resume_v7 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_cpp1.rptdesign";
$rpt_mcu_resume_v8 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_pp1.rptdesign";
$rpt_mcu_resume_v9 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_cpp_tp.rptdesign";
$rpt_mcu_resume_v10 =
"/birt/frameset?__report=report/one/mcu/rpt_mcu_resume_cpp_tp_1.rptdesign";
$this->corss();
$sql = "select T_OrderHeaderLabNumber,
T_OrderHeaderAddOnSecondM_LangID,
T_OrderHeaderM_LangID,
fn_get_patient_atribute(T_OrderHeaderM_PatientID) patient
from t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = ?
and T_OrderHeaderID = T_OrderHeaderAddonT_OrderHeaderID";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["message"],
]);
exit();
}
$reports = [];
$rows = $qry->result_array();
if (count($rows) == 0) {
echo json_encode([
"status" => "ERR",
"message" => "Order Not Found",
]);
exit();
}
$labNo = $rows[0]["T_OrderHeaderLabNumber"];
$jpat = json_decode($rows[0]["patient"], true);
$pname = str_replace(" ", "_", $jpat["patient_fullname"]);
$ts = "&ts=" . date("Ymdhis");
$reports[] = [
"name" => "Hasil Lab",
"url" => "{$rpt_lab}&__format=pdf&username={$username}&PID={$orderID}&ts={$ts}",
];
$haveSecondLang = false;
if (
$rows[0]["T_OrderHeaderAddOnSecondM_LangID"] > 0 &&
$rows[0]["T_OrderHeaderAddOnSecondM_LangID"] !=
$rows["T_OrderHeaderM_LangID"]
) {
$haveSecondLang = true;
$reports[] = [
"name" => "Hasil Lab (Inggris)",
"url" => "{$rpt_lab_en}&__format=pdf&username={$username}&PID={$orderID}&ts={$ts}",
];
}
//Xray
$reports[] = [
"name" => "Rontgen",
"url" => "{$rpt_xray}&__format=pdf&username={$username}&PID={$orderID}&PLang=1&ts={$ts}",
];
$reports[] = [
"name" => "Rontgen (v2)",
"url" => "{$rpt_xray_v2}&__format=pdf&username={$username}&PID={$orderID}&PLang=1&ts={$ts}",
];
if (false && $haveSecondLang) {
$reports[] = [
"name" => "Rontgen (Inggris)",
"url" => "{$rpt_xray_en}&__format=pdf&username={$username}&PID={$orderID}&PLang=1&ts={$ts}",
];
$reports[] = [
"name" => "Rontgen (Inggris v2)",
"url" => "{$rpt_xray_v2_en}&__format=pdf&username={$username}&PID={$orderID}&PLang=1&ts={$ts}",
];
}
$sql = "select * from so_resultentry where So_ResultEntryT_OrderHeaderID=? and So_ResultEntrySo_TemplateName='ECG'
and So_ResultEntryIsActive='Y'";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["messaga"],
]);
exit();
}
$rows = $qry->result_array();
foreach ($rows as $r) {
$soID = $r["So_ResultEntryID"];
$langID = $r["So_ResultEntryM_LangID"];
if ($langID == 1) {
$name = "Elektromedik";
$reports[] = [
"name" => $name,
"url" => "{$rpt_elmd}&__format=pdf&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "$name (v2)",
"url" => "{$rpt_elmd_v2}&__format=pdf&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
} else {
continue;
$name = "Elektromedik (Inggris)";
$reports[] = [
"name" => $name,
"url" => "{$rpt_elmd_en}&__format=pdf&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "Elektromedik (Inggris v2)",
"url" => "{$rpt_elmd_v2_en}&__format=pdf&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
}
}
//FISIK UMUM
$sql = "select * from so_resultentry where So_ResultEntryT_OrderHeaderID=? and So_ResultEntrySo_TemplateName='FISIK UMUM'
and So_ResultEntryIsActive='Y'";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["messaga"],
]);
exit();
}
$rows = $qry->result_array();
foreach ($rows as $r) {
$soID = $r["So_ResultEntryID"];
$langID = $r["So_ResultEntryM_LangID"];
if ($langID == 1) {
$reports[] = [
"name" => "Fisik (riwayat)",
"url" => "{$rpt_fisik_riwayat}&__format=pdf&PType=riwayat&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "Fisik",
"url" => "{$rpt_fisik}&__format=pdf&PType=fisik&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
} else {
continue;
$name = "Elektromedik (Inggris)";
$reports[] = [
"name" => $name,
"url" => "{$rpt_elmd_en}&__format=pdf&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "Elektromedik (Inggris v2)",
"url" => "{$rpt_elmd_v2_en}&__format=pdf&username={$username}&PID={$soID}&PLang={$langID}&ts={$ts}",
];
}
}
//FISIK UMUM
$sql =
"select * from mcu_resume where Mcu_ResumeT_OrderHeaderID=? and Mcu_ResumeIsActive = 'Y'";
$qry = $this->db->query($sql, [$orderID]);
if (!$qry) {
echo json_encode([
"status" => "ERR",
"message" => $this->db->error()["messaga"],
]);
exit();
}
$rows = $qry->result_array();
foreach ($rows as $r) {
$mcuResumeID = $r["Mcu_ResumeID"];
$langID = $r["Mcu_ResumeM_LangID"];
if ($langID == 1) {
$reports[] = [
"name" => "MCU Resume",
"url" => "{$rpt_mcu_resume}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v2)",
"url" => "{$rpt_mcu_resume_v2}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v3)",
"url" => "{$rpt_mcu_resume_v3}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v4)",
"url" => "{$rpt_mcu_resume_v4}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v5)",
"url" => "{$rpt_mcu_resume_v5}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v6)",
"url" => "{$rpt_mcu_resume_v6}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v7)",
"url" => "{$rpt_mcu_resume_v7}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v8)",
"url" => "{$rpt_mcu_resume_v8}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume (v9)",
"url" => "{$rpt_mcu_resume_v9}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
$reports[] = [
"name" => "MCU Resume v10",
"url" => "{$rpt_mcu_resume_v10}&__format=pdf&username={$username}&PID={$mcuResumeID}&PLang={$langID}&ts={$ts}",
];
} else {
continue;
}
}
echo json_encode([
"status" => "OK",
"data" => $reports,
"labNo" => $labNo,
"name" => $pname,
]);
}
function download_pdf()
{
$this->corss();
$orderHeaderID = $this->sys_input["headerId"];
$output_file_name = $orderHeaderID . "-" . "merge" . ".pdf";
$urls = $this->sys_input["urls"];
$target = tempnam("/xtmp", uniqid("target", true)) . ".pdf";
$merge_cmd = "/usr/bin/pdfunite";
$base_url = "http://localhost/";
$fnames_del = [];
foreach ($urls as $url) {
$fname = tempnam("/xtmp", uniqid("src", true)) . ".pdf";
$rpt_data = file_get_contents($base_url . $url);
file_put_contents($fname, $rpt_data);
$merge_cmd .= " $fname ";
$fnames_del[] = $fname;
}
$merge_cmd .= " $target";
$output = [];
exec($merge_cmd, $output);
header("Content-type: application/pdf");
header("Content-Disposition: inline");
/*
header(
'Content-Disposition: inline; filename="' .
$output_file_name .
'"'
);
*/
echo file_get_contents($target);
foreach ($fnames_del as $fdel) {
unlink($fdel);
}
if (file_exists($target)) {
unlink($target);
}
}
function get_pdf($orderID)
{
$sql = "select distinct
Group_ResultID,Group_ResultName,
Group_ResultFlagNonLab,IFNULL(T_EmailNonLabUrl,'-') EmailNonLabUrl,
IF(T_EmailNonLabUrl IS NULL AND Group_ResultFlagNonLab = 'Y',' [Belum Pilih Format Hasil]','') temail
from
t_orderdetail
join group_resultdetail
on Group_ResultDetailT_TestID = T_OrderDetailT_TestID
and T_OrderDetailIsActive = 'Y' and Group_ResultDetailIsActive = 'Y'
and T_OrderDetailT_OrderHeaderID = ?
join group_result
on Group_ResultDetailGroup_ResultID = Group_ResultID
and Group_ResultIsActive = 'Y'
LEFT JOIN t_email_nonlab ON T_EmailNonLabT_OrderHeaderID = T_OrderDetailT_OrderHeaderID AND
T_EmailNonLabType LIKE CONCAT('%',REPLACE(Group_ResultName, 'Elektromedik', 'electromedis'),'%')";
$query = $this->db->query($sql, [$orderID]);
if ($query) {
$rows = $query->result_array();
$ts = "&ts=" . date("Ymdhis");
foreach ($rows as $r) {
$xname = $r["Group_ResultName"];
$gid = $r["Group_ResultID"];
$emailnonlaburl = str_replace(" ", "", $r["EmailNonLabUrl"]);
if (strpos($emailnonlaburl, "fisik") > 0) {
continue;
}
$report = "";
switch ($gid) {
case 1:
$report =
"/birt/frameset?__report=report/one/lab/rpt_test_email.rptdesign&__format=pdf&username=admin&PID=" .
$orderID .
$ts;
break;
case 2:
$report =
"/birt/frameset?__report=report/one/lab/rpt_hasil_papsmear_email.rptdesign&__format=pdf&username=admin&PID=" .
$orderID .
$ts;
break;
case 3:
$report =
"/birt/frameset?__report=report/one/lab/rpt_hasil_fna_email.rptdesign&__format=pdf&username=admin&PID=" .
$orderID .
$ts;
break;
case 4:
$report = $emailnonlaburl;
break;
case 5:
$report = $emailnonlaburl;
break;
case 6:
$report = $emailnonlaburl;
break;
case 7:
$report = $emailnonlaburl;
break;
case 8:
$report = $emailnonlaburl;
break;
case 9:
$report = $emailnonlaburl;
break;
case 10:
$report = $emailnonlaburl;
break;
case 11:
$report = $emailnonlaburl;
break;
case 12:
$report =
"/birt/frameset?__report=report/one/lab/rpt_hasil_lcprep_email.rptdesign&__format=pdf&username=admin&PID=" .
$orderID .
$ts;
break;
case 13:
$report =
"/birt/frameset?__report=report/one/lab/rpt_test_mikro_email.rptdesign&__format=pdf&username=admin&PID=" .
$orderID .
$ts;
break;
case 14:
$report =
"/birt/frameset?__report=report/one/lab/rpt_hasil_cytologi_email.rptdesign&__format=pdf&username=admin&PID=" .
$orderID .
$ts;
break;
case 15:
$report = $emailnonlaburl;
break;
case 16:
$report = $emailnonlaburl;
break;
case 17:
$report = $emailnonlaburl;
break;
case 19:
$report = $emailnonlaburl;
break;
}
if ($report != "-") {
$result[] = [
"name" => $xname,
"groupID" => $gid,
"url" => $report,
];
}
}
}
echo json_encode(["status" => "OK", "data" => $result]);
}
function merge($orderHeaderID, $lang = 1, $rpt = "lab")
{
$sql = "select T_OrderHeaderLabNumber,T_OrderHeaderLabNumberExt,M_PatientName
from t_orderheader
join m_patient on T_OrderHeaderID = ?
and T_OrderHeaderM_PatientID = M_PatientID";
$qry = $this->db->query($sql, [$orderHeaderID]);
$output_file_name = $orderHeaderID . "-" . $rpt . ".pdf";
if ($qry) {
$rows = $qry->result_array();
if (count($rows) > 0) {
$output_file_name =
$rows[0]["T_OrderHeaderLabNumberExt"] . "-" . $rpt . ".pdf";
}
}
$fnames = [];
$target = tempnam("/xtmp", uniqid("target", true)) . ".pdf";
$merge_cmd = "/usr/bin/pdfunite";
$base_url = "http://localhost/";
$fnames_del = [];
if (strpos($rpt, "lab") !== false) {
$fname = tempnam("/xtmp", uniqid("src", true)) . ".pdf";
$url =
$base_url .
"/birt/frameset?__report=report/one/lab/rpt_test_email.rptdesign&__format=pdf&username=Administrator&PID=" .
$orderHeaderID .
"&tm=" .
date("Ymdhnis");
$rpt_data = file_get_contents($url);
file_put_contents($fname, $rpt_data);
$merge_cmd .= " $fname ";
$fnames_del[] = $fname;
}
if (strpos($rpt, "xray") !== false) {
$fname = tempnam("/xtmp", uniqid("src", true)) . ".pdf";
$url =
$base_url .
"/birt/run?__report=report/one/lab/rpt_hasil_so_xray_email.rptdesign&__format=pdf&username=Administrator&PID=" .
$orderHeaderID .
"&PLang=" .
$lang .
"&tm=" .
date("Ymdhnis");
$rpt_data = file_get_contents($url);
file_put_contents($fname, $rpt_data);
$merge_cmd .= " $fname ";
$fnames_del[] = $fname;
}
if (strpos($rpt, "ecg") !== false) {
$ecgID = 0;
$sql = "select * from so_resultentry
where So_ResultEntryT_OrderHeaderID=?
and So_ResultEntryIsActive = 'Y'
and So_ResultEntrySo_TemplateName = 'ECG'";
$qry = $this->db->query($sql, [$orderHeaderID]);
if ($qry) {
$rows = $qry->result_array();
if (count($rows) > 0) {
$ecgID = $rows[0]["So_ResultEntryID"];
}
}
if ($ecgID > 0) {
$fname = tempnam("/xtmp", uniqid("src", true)) . ".pdf";
$url =
$base_url .
"/birt/run?__report=report/one/lab/rpt_hasil_so_elmd_email.rptdesign&__format=pdf&username=Administrator&PID=" .
$ecgID .
"&PLang=" .
$lang .
"&tm=" .
date("Ymdhnis");
$rpt_data = file_get_contents($url);
file_put_contents($fname, $rpt_data);
$merge_cmd .= " $fname ";
$fnames_del[] = $fname;
}
}
$merge_cmd .= " $target";
$output = [];
exec($merge_cmd, $output);
header("Content-type: application/pdf");
header(
'Content-Disposition: attachment; filename="' .
$output_file_name .
'"'
);
echo file_get_contents($target);
foreach ($fnames_del as $fdel) {
unlink($fdel);
}
if (file_exists($target)) {
unlink($target);
}
exit();
}
}
?>