Files
REG_IBL/one-api/application/controllers/app_doctor/DownloadOrderPatient.php
2026-05-25 20:01:37 +07:00

372 lines
15 KiB
PHP

<?php
class DownloadOrderPatient extends MY_Controller
{
var $db;
function __construct()
{
parent::__construct();
$this->db_regional = $this->load->database("regional", true);
}
function index()
{
echo "Api: Download Patient";
}
function download()
{
try {
$prm = $this->get_param();
// $limit = 20;
$limit = 100;
if (isset($prm['limit'])) {
$limit = trim($prm['limit']);
}
$branchId = $prm['branchId'];
$branchCode = $prm['branchCode'];
// tidak order nasional
// $sql = "SELECT distinct OrderPatientID,
// downloadLogM_BranchID,
// downloadLogM_BranchCode,
// downloadLogIsDownloaded,
// downloadLogRetry
// FROM one_doctor.order_patient
// JOIN one_doctor.order_patient_details ON OrderPatientID = OrderPatientDetailsOrderPatientID
// AND OrderPatientDetailsIsActive = 'Y'
// JOIN m_mou on OrderPatientM_MouID = M_MouID
// AND M_MouIsActive = 'Y'
// LEFT JOIN one_doctor.download_log ON OrderPatientID = downloadLogOrderPatientID
// WHERE OrderPatientIsActive = 'Y'
// AND (ISNULL(downloadLogM_BranchID)
// OR (downloadLogM_BranchID = ? AND downloadLogIsDownloaded = 'N')
// OR (downloadLogM_BranchID <> ? ))
// AND (ISNULL(downloadLogRetry) OR downloadLogRetry < 10)
// AND OrderPatientID NOT IN (
// SELECT downloadLogOrderPatientID
// from one_doctor.download_log
// WHERE downloadLogM_BranchID = ?
// )
// GROUP BY OrderPatientID, downloadLogM_BranchID
// ORDER BY OrderPatientID
// LIMIT ?";
// pakai order nasional
$sql = "SELECT distinct OrderPatientID,
downloadLogM_BranchID,
downloadLogM_BranchCode,
downloadLogIsDownloaded,
downloadLogRetry
FROM one_doctor.order_patient
JOIN one_doctor.order_patient_details
ON OrderPatientID = OrderPatientDetailsOrderPatientID
AND OrderPatientDetailsIsActive = 'Y'
JOIN m_mou
ON (OrderPatientM_MouID = M_MouID AND M_MouIsActive = 'Y' OR OrderPatientM_MouID = 0)
LEFT JOIN one_doctor.download_log
ON OrderPatientID = downloadLogOrderPatientID
WHERE OrderPatientIsActive = 'Y'
AND (ISNULL(downloadLogM_BranchID)
OR (downloadLogM_BranchID = ? AND downloadLogIsDownloaded = 'N')
OR (downloadLogM_BranchID <> ? ))
AND (ISNULL(downloadLogRetry) OR downloadLogRetry < 10)
AND OrderPatientID NOT IN (
SELECT downloadLogOrderPatientID
from one_doctor.download_log
WHERE downloadLogM_BranchID = ?
)
GROUP BY OrderPatientID, downloadLogM_BranchID
ORDER BY OrderPatientID
LIMIT ?";
$qry = $this->db_regional->query($sql, [
intval($branchId),
intval($branchId),
intval($branchId),
intval($limit)
]);
if (!$qry) {
$response = ["status" => "ERR", "message" => "select order patient | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()];
$this->reply_gz($response);
exit;
}
$rows = $qry->result_array();
$arrOrderPatientId = array();
foreach ($rows as $key => $val) {
$orderPatientId = $val['OrderPatientID'];
if (in_array($orderPatientId, $arrOrderPatientId)) {
continue;
}
$arrOrderPatientId[] = $orderPatientId;
$this->insertDownloadLog($branchId, $branchCode, $orderPatientId);
}
if (count($arrOrderPatientId) > 0) {
$orderPatientIdAll = implode(',', $arrOrderPatientId);
// tnp order nasional
// $sqlData = "SELECT order_patient.* , M_MouM_CompanyID FROM one_doctor.order_patient
// JOIN m_mou ON OrderPatientM_MouID = M_MouID
// AND M_MouIsActive = 'Y'
// WHERE OrderPatientID IN ($orderPatientIdAll)
// AND OrderPatientIsActive = 'Y'";
// pake order nasional
$sqlData = "SELECT order_patient.* , IFNULL(M_MouM_CompanyID,0) as M_MouM_CompanyID
FROM one_doctor.order_patient
LEFT JOIN m_mou
ON (OrderPatientM_MouID = M_MouID AND M_MouIsActive = 'Y')
WHERE OrderPatientID IN ($orderPatientIdAll)
AND OrderPatientIsActive = 'Y'";
$qryData = $this->db_regional->query($sqlData);
if (!$qryData) {
$response = ["status" => "ERR", "message" => "select order patient | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()];
$this->reply_gz($response);
exit;
}
$rowsData = $qryData->result_array();
foreach ($rowsData as $key => $value) {
$orderPatientId = $value['OrderPatientID'];
$sqlGetDetail = "SELECT * FROM one_doctor.order_patient_details
WHERE OrderPatientDetailsOrderPatientID = ?
AND OrderPatientDetailsIsActive = 'Y' ";
$qryGetDetail = $this->db_regional->query($sqlGetDetail, [intval($orderPatientId)]);
if (!$qryGetDetail) {
$response = ["status" => "ERR", "message" => "select order patient detail | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()];
$this->reply_gz($response);
exit;
}
$rowsgetDetail = $qryGetDetail->result_array();
$rowsData[$key]['details'] = $rowsgetDetail;
// $sqlGetSaran = "SELECT *
// FROM one_doctor.order_saran
// WHERE OrderSaranOrderPatientID = ?
// AND OrderSaranIsActive = 'Y'";
// $qryGetSaran = $this->db->query($sqlGetSaran, [intval($orderPatientId)]);
// if (!$qryGetSaran) {
// $response = ["status" => "ERR", "message" => "select order saran | " .
// $this->db->error()["message"], "debug" => $this->db->last_query()];
// $this->reply_gz($response);
// exit;
// }
// $rowsgetSaran = $qryGetSaran->result_array();
// $rowsData[$key]['saran'] = $rowsgetSaran;
}
$result = array(
'status' => 'OK',
"data" => $rowsData,
);
$this->reply_gz($result);
// $this->reply($result);
} else {
$this->reply_gz("Tidak ada data terbaru");
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$result = array(
'status' => 'err',
"message" => $message,
);
$this->reply_gz($result);
}
}
function insertDownloadLog($branchId, $branchCode, $patientId)
{
$sql = "INSERT INTO one_doctor.download_log
(downloadLogM_BranchID,
downloadLogM_BranchCode,
downloadLogOrderPatientID,
downloadLogRetry)
VALUES
(?,?,?,0) ON DUPLICATE KEY UPDATE downloadLogRetry = downloadLogRetry + 1";
$qry = $this->db_regional->query($sql, [intval($branchId), $branchCode, intval($patientId)]);
if (!$qry) {
$this->sys_error_db(["status" => "ERR", "message" => "select order patient | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()]);
exit;
}
}
function updateLog()
{
try {
$prm = $this->get_param_z();
$branchId = $prm['branchId'];
// $branchCde = $prm['branchCode'];
$arrOrderPatientId = $prm['arrOrderPatientId'];
foreach ($arrOrderPatientId as $key => $value) {
$sqlUpdate = "UPDATE one_doctor.download_log SET
downloadLogIsDownloaded = 'Y'
WHERE downloadLogOrderPatientID = ?
AND downloadLogM_BranchID = ?";
$qry = $this->db_regional->query($sqlUpdate, [intval($value), intval($branchId)]);
if (!$qry) {
$this->sys_error_db(["status" => "ERR", "message" => "Insert Log | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()]);
exit;
}
}
$result = array(
"message" => 'Berhasil',
"sql" => $this->db_regional->last_query()
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function downloadSaran()
{
try {
$prm = $this->get_param();
$limit = 10;
if (isset($prm['limit'])) {
$limit = trim($prm['limit']);
}
$branchId = $prm['branchId'];
$branchCode = $prm['branchCode'];
$sql = "SELECT * FROM one_doctor.order_saran
LEFT JOIN one_doctor.download_saran_log ON OrderSaranID = downloadSaranLogOrderSaranID
AND downloadSaranLogM_BranchID = ?
WHERE OrderSaranIsActive = 'Y'
AND (
(downloadSaranLogIsDownloaded = 'N' AND downloadSaranLogRetry < 10)
OR ISNULL(downloadSaranLogM_BranchID)
)
LIMIT ?";
$qry = $this->db_regional->query($sql, [intval($branchId), intval($limit)]);
if (!$qry) {
$response = ["status" => "ERR", "message" => "select order saran | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()];
// $this->reply_gz($response);
$this->reply($response);
exit;
}
$rows = $qry->result_array();
$arrOrderSaran = array();
// print_r(json_encode($rows));
// exit;
foreach ($rows as $key => $val) {
$orderSaran = $val['OrderSaranID'];
if (in_array($orderSaran, $arrOrderSaran)) {
continue;
}
$arrOrderSaran[] = $orderSaran;
$this->insertDownloadSaranLog($branchId, $branchCode, $orderSaran);
}
if (count($arrOrderSaran) > 0) {
$orderSaranIdAll = implode(',', $arrOrderSaran);
$sqlData = "SELECT * FROM one_doctor.order_saran
WHERE OrderSaranIsActive ='Y'
AND OrderSaranID IN ($orderSaranIdAll)";
$qryData = $this->db_regional->query($sqlData);
if (!$qryData) {
$response = ["status" => "ERR", "message" => "select order saran | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()];
// $this->reply($response);
$this->reply_gz($response);
exit;
}
$rowsData = $qryData->result_array();
$result = array(
'status' => 'OK',
"data" => $rowsData,
);
$this->reply_gz($result);
// $this->reply($result);
} else {
$this->reply_gz("Tidak ada data terbaru");
// $this->reply("Tidak ada data terbaru");
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$result = array(
'status' => 'err',
"message" => $message,
);
$this->reply_gz($result);
}
}
function insertDownloadSaranLog($branchId, $branchCode, $saranId)
{
$sql = "INSERT INTO one_doctor.download_saran_log
(downloadSaranLogM_BranchID,
downloadSaranLogM_BranchCode,
downloadSaranLogOrderSaranID,
downloadSaranLogRetry)
VALUES (?, ?, ?,0) ON DUPLICATE KEY UPDATE downloadSaranLogRetry = downloadSaranLogRetry + 1";
$qry = $this->db_regional->query($sql, [intval($branchId), $branchCode, intval($saranId)]);
if (!$qry) {
$resp = ["status" => "ERR", "message" => "insert download saran log | " .
$this->db_regional->error()["message"], "debug" => $this->db_regional->last_query()];
$this->reply_gz($resp);
exit;
}
}
function updateSaranLog()
{
try {
// $prm = $this->get_param();
$prm = $this->get_param_z();
$branchId = $prm['branchId'];
// $branchCde = $prm['branchCode'];
$arrOrderSaranId = $prm['arrOrderSaranId'];
foreach ($arrOrderSaranId as $key => $value) {
$sqlUpdate = "UPDATE one_doctor.download_saran_log
SET downloadSaranLogIsDownloaded = 'Y'
WHERE downloadSaranLogOrderSaranID = ?
AND downloadSaranLogM_BranchID = ?";
$qry = $this->db_regional->query($sqlUpdate, [intval($value), intval($branchId)]);
if (!$qry) {
$this->sys_error_db(["status" => "ERR", "message" => "Insert Log | " .
$this->db_regional->error()["message"], "debug" => $this->db->last_query()]);
exit;
}
}
$result = array(
"message" => 'Berhasil',
"sql" => $this->db_regional->last_query()
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function reply($resp)
{
echo json_encode($resp);
}
function reply_gz($resp)
{
echo gzcompress(json_encode($resp));
}
function get_param()
{
$body = file_get_contents("php://input");
return json_decode($body, true);
}
function get_param_z()
{
$body_z = file_get_contents("php://input");
$body = gzuncompress($body_z);
return json_decode($body, true);
}
}