Files
BE_IBL/application/controllers/mockup/mitra/Uploader.php
2026-04-15 15:24:12 +07:00

584 lines
21 KiB
PHP

<?php
class Uploader extends MY_Controller
{
var $db_onedev;
public function __construct()
{
parent::__construct();
$this->resp = ["status" => "ERR", "message" => "Unknown error"];
$this->db_onedev = $this->load->database("onedev", true);
$this->debug = false;
$this->host = "http://localhost";
}
public function index()
{
echo "request upload";
}
function reply($encrypt = true)
{
echo json_encode($this->resp);
exit();
}
function list()
{
//
$sql = "select group_concat(distinct Mitra_UploadT_OrderHeaderID) xids
from mitra_upload
WHERE Mitra_UploadIsActive = 'Y' AND Mitra_UploadIsUploaded = 'N'
";
$qry = $this->db_onedev->query($sql);
/* if ($qry) {
$rows = $qry->result_array();
} else {
$this->sys_error_db("Total select");
exit;
}
$this->sys_ok($rows);
*/
if (!$qry) {
$this->resp["message"] =
"Err select orderheaderid | " .
$this->db_onedev->error()["message"] .
"|" .
$this->db_onedev->last_query();
$this->reply();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->resp["message"] = "No Pending Request";
$this->reply();
}
$order_ids = $rows[0]["xids"];
if ($order_ids == "") {
$order_ids = "-1";
}
$sql = "select
distinct T_OrderHeaderID, T_OrderHeaderLabNumber, T_OrderHeaderLabNumberExt,
Group_ResultDetailGroup_ResultID Group_ResultID,
M_DeliverySource,
ifnull(Result_ProcessToOfficeStatus,'N') Result_ProcessToOfficeStatus,
M_DeliveryID, M_DeliveryName, T_OrderDeliveryDestination,
T_OrderHeaderDate, fn_get_patient_atribute(T_OrderHeaderM_PatientID) patientAttribute,
fn_global_doctor_name(T_OrderHeaderSenderM_DoctorID) DoctorName,
IF(M_MouIsBill = 'Y','Y',ifnull(Last_StatusPaymentIsLunas,'N')) IsLunas,
T_OrderHeaderDate,
fn_global_doctor_name(T_OrderHeaderSenderM_DoctorID) DoctorName,
M_DoctorHP,
T_OrderHeaderAddOnReadyPrint,
T_OrderHeaderSenderM_DoctorID, T_OrderHeaderM_PatientID, T_OrderHeaderM_MouID,
T_OrderHeaderM_CompanyID, T_OrderDeliveryID,
T_OrderNumber
from t_orderheader
join t_orderdetail
on T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestIsResult = 'Y'
join group_resultdetail
on Group_ResultDetailT_TestID = T_OrderDetailT_TestID
and Group_ResultDetailIsActive = 'Y'
join group_result
on Group_ResultIsActive = 'Y'
and Group_ResultID = Group_ResultDetailGroup_ResultID
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID
join m_doctor on T_OrderHeaderSenderM_DoctorID = M_DoctorID
JOIN m_mou ON T_OrderHeaderM_MouID = M_MouID
join t_orderdelivery on T_OrderHeaderID = T_OrderDeliveryT_OrderHeaderID and T_OrderDeliveryIsActive = 'Y'
join m_delivery on T_OrderDeliveryM_DeliveryID = M_DeliveryID
left join result_processtooffice
on Result_ProcessToOfficeT_OrderHeaderID= T_OrderHeaderID
and Result_ProcessToOfficeT_OrderDeliveryID = T_OrderDeliveryID
AND
(
(
Result_ProcessToOfficeGroup_ResultID = T_OrderDetailT_TestID
AND
Group_ResultFlagPerTest = 'Y'
)
or
(
Result_ProcessToOfficeGroup_ResultID = Group_ResultID
AND
Group_ResultFlagPerTest = 'N'
)
)
and Result_ProcessToOfficeIsActive ='Y'
left join last_statuspayment
ON Last_StatusPaymentT_OrderHeaderID = T_OrderHeaderID AND Last_StatusPaymentIsActive = 'Y'
left join one_mitra.t_order ON T_OrderT_OrderHeaderID = T_OrderHeaderID
where T_OrderHeaderID in ( $order_ids )
group by T_OrderHeaderID, Group_ResultID, M_DeliverySource
order by IsLunas desc, T_OrderHeaderID
limit 0,1000";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
$this->resp["message"] =
"Err Populate List | " .
$this->db_onedev->error()["message"] .
"|" .
$this->db_onedev->last_query();
$this->reply();
}
$rows = $qry->result_array();
// $this->sys_ok($rows);
$arr_partial = [];
foreach ($rows as $r) {
$headerID = $r["T_OrderHeaderID"];
$Result_ProcessToOfficeStatus = $r["Result_ProcessToOfficeStatus"];
if ($Result_ProcessToOfficeStatus == "N") {
$arr_partial[] = $headerID;
}
}
$list_result = [];
$group_result = [];
$counter_list = 0;
foreach ($rows as $idx => $r) {
$headerID = $r["T_OrderHeaderID"];
if (in_array($headerID, $arr_partial)) {
if ($r["Result_ProcessToOfficeStatus"] == "S") {
$rows_partial_only[] = $r;
}
continue;
}
$deliveryID = $r["M_DeliveryID"];
$groupResultID = $r["Group_ResultID"];
if ($r["T_OrderDeliveryDestination"] == "" || strlen(trim($r["T_OrderDeliveryDestination"])) < 6 ) {
continue;
}
$rows[$idx]["template"] = "01";
$group_idx = $headerID . "-" . $r["M_DeliverySource"];
$j_pasien = json_decode($r["patientAttribute"], true);
$xname = $j_pasien["patient_fullname"];
$xname = $this->escape_fname($xname);
$x_reports = $this->get_pdf(
$r["T_OrderHeaderID"],
$r["T_OrderHeaderLabNumberExt"],
$xname,
$groupResultID,
$r["T_OrderHeaderDate"],
$r["T_OrderNumber"]
);
$reports = [];
foreach ($x_reports as $xr) {
$groupResultID = $xr["groupID"];
$wa_idx = "$headerID-$deliveryID-$groupResultID";
if (isset($tx_whatsapp[$wa_idx])) {
continue;
}
if ($xr["url"] == "") {
continue;
}
if ($xr["url"] == "-") {
continue;
}
$xr["Result_ProcessToOfficeStatus"] =
$r["Result_ProcessToOfficeStatus"];
$reports[] = $xr;
}
if (count($reports) == 0) {
unset($rows[$idx]);
continue;
}
$rows[$idx]["reports"] = $reports;
$list_result[] = $rows[$idx];
if (isset($group_result[$group_idx])) {
$group_result[$group_idx]["reports"][] = $xr;
} else {
$group_result[$group_idx] = $rows[$idx];
}
$counter_list++;
if ($counter_list > 5) {
break;
}
}
$list_result = [];
foreach ($group_result as $gr) {
$list_result[] = $gr;
}
$params = [];
//$this->get_branch();
foreach ($list_result as $result) {
$param = $this->populate_param($result, true);
//$param = $result;
// $this->uploadnew($result['reports'][0]['fname'],$result['T_OrderHeaderM_CompanyID']);
$params[] = $param;
}
// print_r(json_encode($params));
// exit();
$this->resp["status"] = "OK";
$this->resp["params"] = $params;
$this->reply();
}
public function uploadnew($filename,$companyid)
{
$tahun = date("Y");
$userid = $this->sys_user["M_UserID"];
$target_dir = "path/data_s3/".$companyid."/".$tahun."/";
$target_path = $target_dir . $filename;
$result = array("url" => "http://" . $_SERVER['SERVER_NAME'] . "/one-media/one-photo/" . $filename. "?d=" . date("YmdHis"));
$this->sys_ok($result);
exit;
}
function populate_param($r, $debug = false)
{
$pasien = json_decode($r["patientAttribute"], true);
$tgl_lab = date("Ymd", strtotime($r["T_OrderHeaderDate"]));
$dob = date("Ymd", strtotime($pasien["M_PatientDOB"]));
$files = [];
$groupID = [];
$companyid = $r["T_OrderHeaderM_CompanyID"];
$tahun = date("Y");
$target_dir = "/"."data-s3/".$companyid."/".$tahun."/";
if(!file_exists($target_dir)) {
mkdir($target_dir,0777,true);
}
foreach ($r["reports"] as $xr) {
$groupID[] = $xr["groupID"];
$target_path = $target_dir . $xr["fname"];
$content = file_get_contents($this->host . $xr["url"]);
//echo $content;
file_put_contents($target_path,$content);
$oID = $r["T_OrderHeaderID"];
$sql_update_request_upload = "UPDATE mitra_upload
set Mitra_UploadIsUploaded = 'Y' WHERE Mitra_UploadT_OrderHeaderID = $oID";
$query_update_request_upload = $this->db->query($sql_update_request_upload);
if ($this->debug || $debug) {
$file = [
"name" => $xr["fname"],
"url" => $this->host . $xr["url"]
];
} else {
if (strpos($xr["url"], "jpg")) {
$file = [
"name" => $xr["fname"]];
} else {
$file = [
"name" => $xr["fname"]];
}
}
if ($this->debug) {
$file["url"] = $this->host . $xr["url"];
}
$files[] = $file;
}
$param = [
"sapaan" => $pasien["M_TitleName"],
"nama" => substr(
$this->sanitize_string($pasien["M_PatientName"]),
0,
250
),
"tgl_lab" => $tgl_lab,
"tgl_lahir" => $dob,
"files" => $files,
"groupID" => $groupID,
"noreg" => $r["T_OrderHeaderLabNumberExt"],
];
return $param;
}
function get_pdf($id, $noreg, $pname, $groupResultID, $orderDate, $tnumber)
{
$orderDate = substr($orderDate, 0, 10);
$pname = str_replace(" ", "-", $pname);
$pname = str_replace(".", "_", $pname);
$pname = str_replace("\\'", "", $pname);
$pname = str_replace("(", "", $pname);
$pname = str_replace(")", "", $pname);
$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'
and Group_ResultID = ?
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_onedev->query($sql, [$id, $groupResultID]);
if ($query) {
$rows = $query->result_array();
$ts = "&ts=" . date("Ymdhis");
foreach ($rows as $r) {
$xname = $r["Group_ResultName"];
$name = $this->escape_fname($xname);
$gid = $r["Group_ResultID"];
$temail = $r["temail"];
$isnonlab = $r["Group_ResultFlagNonLab"];
$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=" .
$id .
$ts;
break;
case 2:
$report =
"/birt/frameset?__report=report/one/lab/rpt_hasil_papsmear_email.rptdesign&__format=pdf&username=admin&PID=" .
$id .
$ts;
break;
case 3:
$report =
"/birt/frameset?__report=report/one/lab/rpt_hasil_fna_email.rptdesign&__format=pdf&username=admin&PID=" .
$id .
$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=" .
$id .
$ts;
break;
case 13:
$report =
"/birt/frameset?__report=report/one/lab/rpt_test_mikro_email.rptdesign&__format=pdf&username=admin&PID=" .
$id .
$ts;
break;
case 14:
$report =
"/birt/frameset?__report=report/one/lab/rpt_hasil_cytologi_email.rptdesign&__format=pdf&username=admin&PID=" .
$id .
$ts;
break;
case 15:
$report = $emailnonlaburl;
break;
case 16:
$report = $emailnonlaburl;
break;
case 17:
$report = $emailnonlaburl;
break;
}
if ($report != "-") {
$result[] = [
"name" => $name,
"groupID" => $gid,
"fname" => $tnumber . "-LAB.pdf",
"url" => $this->encode_url_param($report),
];
} else {
$result[] = [
"name" => $name . "*" . $temail . "*",
"fname" => "File tidak ditemukan",
"url" => "",
];
}
}
}
if (
$groupResultID == 4 &&
count($result) > 0 &&
$result[0]["url"] != ""
) {
$files = $this->get_files($noreg, $orderDate);
foreach ($files as $f) {
$result[] = [
"name" => basename($f),
"groupID" => 4,
"fname" => basename($f),
"url" => $f,
];
}
}
return $result;
}
function sanitize_string($data)
{
$hasil = "";
$hasil = preg_replace("/[^0-9a-zA-Z ()|:.,?!+@_-]/", " ", $data);
$hasil = filter_var(
$hasil,
FILTER_SANITIZE_STRING,
FILTER_FLAG_STRIP_LOW
);
$hasil = filter_var(
$hasil,
FILTER_SANITIZE_STRING,
FILTER_FLAG_STRIP_HIGH
);
$hasil = str_replace("'", "", $hasil);
if (str_replace(" ", "", $hasil) == "") {
return "";
} else {
return $hasil;
}
}
function encode_url_param($data)
{
$path = parse_url($data, PHP_URL_PATH);
$query = parse_url($data, PHP_URL_QUERY);
parse_str($query, $args);
$result = "";
foreach ($args as $k => $v) {
if ($result != "") {
$result .= "&";
}
$result .= $k . "=" . urlencode($v);
}
$result = $path . "?$result";
return $result;
}
function get_files($noreg, $orderDate)
{
$orderDate = str_replace("-", "", $orderDate);
$prm_glob = "/data-pacs/$orderDate/$noreg*jpg";
$result = [];
foreach (glob($prm_glob) as $filename) {
$result[] = $filename;
}
return $result;
}
function escape_fname($xname)
{
$find = [" ", "&", '\r\n', '\n', "+", ","];
$xname = str_replace($find, "-", $xname);
//delete and replace rest of special chars
$find = ["/[^a-zA-Z0-9\-<>]/", "/[\-]+/", "/<[^>]*>/"];
$repl = ["", "-", ""];
$xname = preg_replace($find, $repl, $xname);
return $xname;
}
function get_patient($id)
{
$this->db->where("M_PatientID", $id);
$qry = $this->db->get("m_patient");
if (!$qry) {
echo "ERR Patient : " .
$this->db->error()["message"] .
"|" .
$this->db->last_query() .
"\n";
return;
}
$rows = $qry->result_array();
if (count($rows) > 0) {
return $rows[0];
}
return [];
}
function get_mou($id)
{
$this->db->where("M_MouID", $id);
$qry = $this->db->get("m_mou");
if (!$qry) {
echo "ERR MOU : " .
$this->db->error()["message"] .
"|" .
$this->db->last_query() .
"\n";
return;
}
$rows = $qry->result_array();
if (count($rows) > 0) {
return $rows[0];
}
return [];
}
function get_company($id)
{
$this->db->where("M_CompanyID", $id);
$qry = $this->db->get("m_company");
if (!$qry) {
echo "ERR Company : " .
$this->db->error()["message"] .
"|" .
$this->db->last_query() .
"\n";
return;
}
$rows = $qry->result_array();
if (count($rows) > 0) {
return $rows[0];
}
return [];
}
function get_order($id)
{
$this->db->where("T_OrderHeaderID", $id);
$qry = $this->db->get("t_orderheader");
if (!$qry) {
echo "ERR OrderHeader : " .
$this->db->error()["message"] .
"|" .
$this->db->last_query() .
"\n";
return;
}
$rows = $qry->result_array();
if (count($rows) > 0) {
return $rows[0];
}
return [];
}
}
?>