Files
BE_CPONE/application/controllers/one-doctor/UploadConfirmsalah.php
2026-04-27 10:26:26 +07:00

378 lines
13 KiB
PHP

<?php
class UploadConfirm extends MY_Controller
{
var $db;
// tujuan
var $APP_DOCTOR_API = "http://devbandungraya.aplikasi.web.id/one-api/app_doctor/Receiveruploadlogconfirm/";
var $max_limit = 20;
public function index()
{
echo "DOKTER API";
}
public function __construct()
{
parent::__construct();
// db wilayah contoh surabaya
// $this->db = $this->load->database("regional", true);
// string database
$this->db_str_doctor = 'one_doctor';
}
// curl
public function post($url, $data)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/text",
"Content-Length: " . strlen($data),
]);
$result = curl_exec($ch);
if (curl_error($ch) != "") {
echo json_encode([
"status" => "ERR",
"message" => "Http Error : " . curl_error($ch),
]);
curl_close($ch);
exit();
}
curl_close($ch);
return $result;
}
function log($msg)
{
// $dt = Date("Y-m-d H:i:s");
// echo "$dt $msg \n";
echo $msg;
}
function get_param_z()
{
$body_z = file_get_contents("php://input");
$body = gzuncompress($body_z);
return json_decode($body, true);
}
function reply($resp)
{
echo json_encode($resp);
}
function reply_gz($resp)
{
//echo json_encode($resp);
echo gzcompress(json_encode($resp));
}
// api
function get_result()
{
$data = [];
// t_orderheader join f_payment join f_payment_orderheader
$sql_get_branch = "
select IFNULL(M_BranchID,'') as M_BranchID,
IFNULL(M_BranchCode,'') as M_BranchCode
from m_branch
WHERE M_BranchIsDefault='Y'
AND M_BranchIsActive ='Y'
";
$query_m_branch = $this->db->query($sql_get_branch, [
$id
]);
if(!$query_m_branch){
$this->db->trans_rollback();
$message = $this->db->error();
$this->sys_error($message);
exit;
}
$rows = $query_m_branch->result_array();
if(count($rows) == 0){
$this->db->trans_rollback();
$this->sys_error('Cabang Tidak Ketemu');
exit;
}
$branch_id = $rows[0]['M_BranchID'];
$branch_code = $rows[0]['M_BranchCode'];
$sql_isLunas = "SELECT T_OrderHeaderID, OrderFoOrderPatientID as order_patient_id,
M_PatientID, M_PatientName,
T_OrderHeaderDate, F_PaymentID, F_Payment_OrderHeaderIsLunas,
'' as M_BranchID,
'' as M_BranchCode
from t_orderheader
join $this->db_str_doctor.order_fo ON T_OrderHeaderID = OrderFoT_OrderHeaderID
AND OrderFoIsActive = 'Y'
join m_patient ON T_OrderHeaderM_PatientID = M_PatientID
AND M_PatientIsActive = 'Y'
join f_payment ON T_OrderHeaderID = F_PaymentT_OrderHeaderID
AND F_PaymentIsActive = 'Y'
join f_payment_orderheader ON F_PaymentID = F_Payment_OrderHeaderF_PaymentID
AND F_Payment_OrderHeaderIsLunas = 'Y'
where T_OrderHeaderIsActive = 'Y'
limit 0,$this->max_limit";
$query_isLunas = $this->db->query($sql_isLunas);
if(!$query_isLunas){
$this->db->trans_rollback();
// $this->sys_error_db("get ready print Y or request upload N failed", $this->db);
$this->sys_error_db(["status" => "ERR", "message" => "select data isLunas error | " .
$this->db->error()["message"], "debug" => $this->db->last_query()]);
exit;
}
$rows_result_isLunas = $query_isLunas->result_array();
$data = $rows_result_isLunas;
if(count($rows_result_isLunas) > 0)
{
foreach($rows_result_isLunas as $key => $val)
{
$data[] = array(
"order_patient_id" => $val['order_patient_id'],
"T_OrderHeaderDate" => $val['T_OrderHeaderDate'],
"T_OrderHeaderID" => $val['T_OrderHeaderID'],
"F_Payment_OrderHeaderIsLunas" => $val['F_Payment_OrderHeaderIsLunas'],
"M_BranchID" => $branch_id,
"M_BranchCode" => $branch_code
);
}
return json_encode(
array(
'status' => 'OK',
'data' => $data
)
);
}
else
{
return json_encode (
array(
'status' => 'OK',
'message' => 'Data Tidak Ada',
'data' => $data
)
);
}
}
function upload_log_confirm_gzcompress()
{
$this->db->trans_begin();
$j_param = $this->get_result();
$result = json_decode($j_param,true);
// print_r($j_param);
if(count($result['data']) > 0)
{
// check max retry
$sql_cek_max_retry = "select
uploadLogConfirmOrderPatientID, uploadLogConfirmJson
from $this->db_str_doctor.upload_log_confirm
where uploadLogConfirmIsSent = 'N'
and uploadLogConfirmRetry >= 10
limit 0,$this->max_limit";
$query_max_retry = $this->db->query($sql_cek_max_retry);
if(!$query_max_retry){
$this->db->trans_rollback();
$this->sys_error_db(["status" => "ERR", "message" => "select upload_log | " .
$this->db->error()["message"], "debug" => $this->db->last_query()]);
}
$data_json = [];
$rows_retry = $query_max_retry->result_array();
$data_order_patient_id_retry = [];
foreach($rows_retry as $key => $val)
{
$data_order_patient_id_retry[] = $val['uploadLogConfirmOrderPatientID'];
}
$upload_param = [];
foreach($result['data'] as $key)
{
// print_r(json_encode($result['data']));
// buka proteksi upload lebih dari 1x
if(in_array($key['order_patient_id'], $data_order_patient_id_retry)){
continue;
}
$data_json =
[
"order_patient_id" => $key['order_patient_id'],
"T_OrderHeaderDate" => $key['T_OrderHeaderDate'],
"T_OrderHeaderID" => $key['T_OrderHeaderID'],
"F_Payment_OrderHeaderIsLunas" => $key['F_Payment_OrderHeaderIsLunas'],
"M_BranchID" => $key['M_BranchID'],
"M_BranchCode" => $key['M_BranchCode']
];
$upload_param[] = $data_json;
$this->insert_upload_confirm_log($key['order_patient_id'],
$data_json
);
}
// send regional for update data
if(count($upload_param) == 0){
$this->db->trans_commit();
// $this->db->trans_rollback();
$this->reply([
"status" => "ERR",
'message' => "Data Tidak ada"
]);
exit;
}
$j_param = json_encode(array(
'data' => $upload_param
));
$size = round(strlen($j_param) / 1024, 2);
$z_param = gzcompress($j_param, 9);
$size_z = round(strlen($z_param) / 1024, 2);
$url = $this->APP_DOCTOR_API . "r_upload_log_confirm";
// $this->log("Post to $url | size : $size k | comprezzed $size_z k");
$resp = $this->post($url, $z_param);
$j_resp = json_decode($resp,true);
// $j_resp_x = $this->get_param_z();
// print_r($j_resp);
// print_r($resp);
// print_r($j_resp_x);
if ($j_resp["status"] == "OK") {
// update
$order_patient_id_str = implode(",",$j_resp['order_patient_id']);
if($order_patient_id_str == ""){
$order_patient_id_str = "0";
}
$sql_update = "UPDATE $this->db_str_doctor.upload_log_confirm
set uploadLogConfirmIsSent = 'Y'
WHERE uploadLogConfirmIsSent = 'N'
AND uploadLogConfirmOrderPatientID IN ($order_patient_id_str)";
$query_update = $this->db->query($sql_update);
// echo $this->db->last_query();
if(!$query_update)
{
$this->db->trans_rollback();
$this->sys_error_db(["status" => "ERR", "message" => "update upload_log_confirm | " .
$this->db->error()["message"], "debug" => $this->db->last_query()]);
}
$this->db->trans_commit();
// $this->db->trans_rollback();
$this->reply(["status" => "OK",'message'=>'Proses upload sudah berhasil']);
}
else
{
$this->db->trans_rollback();
// print_r($j_resp['status']);
$this->reply([
"status" => "ERR",
'message' => "Gagal update",
'debug' => print_r($resp, true)
]);
}
}
else
{
$this->db->trans_rollback();
// print_r($j_resp['status']);
$this->reply([
"status" => "OK",
'message' => "Tidak Ada Data",
'debug' => print_r($resp, true)
]);
}
}
function insert_upload_confirm_log($order_patient_id, $json_result)
{
$sql_check = "select *
from $this->db_str_doctor.upload_log_confirm
where uploadLogConfirmOrderPatientID = ?
AND uploadLogConfirmIsSent = 'N'";
$qry_check = $this->db->query($sql_check,[
$order_patient_id
]);
if (!$qry_check) {
// print_r($this->db->last_query());
$this->db->trans_rollback();
$this->sys_error_db(["status" => "ERR", "message" => "insert upload log | " .
$this->db->error()["message"], "debug" => $this->db->last_query()]);
exit;
}
$rows_check = $qry_check->result_array();
if(count($rows_check) == 0){
// insert table upload_log
$sql_insert_upload_log = "INSERT INTO $this->db_str_doctor.upload_log_confirm(
uploadLogConfirmOrderPatientID,
uploadLogConfirmJson,
uploadLogConfirmRetry
) VALUES (?, ?, 0)";
$query_insert = $this->db->query($sql_insert_upload_log, [
intval($order_patient_id),
json_encode($json_result)
]);
// echo $this->db->last_query();
if (!$query_insert) {
// print_r($this->db->last_query());
$this->db->trans_rollback();
$this->sys_error_db(["status" => "ERR", "message" => "insert upload log confirm | " .
$this->db->error()["message"], "debug" => $this->db->last_query()]);
exit;
}
}else{
// update tabel update_log
$sql_update_log = "UPDATE $this->db_str_doctor.upload_log_confirm
SET
uploadLogConfirmRetry = uploadLogConfirmRetry + 1
WHERE uploadLogConfirmID = ?";
$query_update = $this->db->query($sql_update_log, [
$rows_check[0]['uploadLogConfirmID']
]);
// echo $this->db->last_query();
if (!$query_update) {
// print_r($this->db->last_query());
$this->db->trans_rollback();
$this->sys_error_db(["status" => "ERR", "message" => "update upload log confirm | " .
$this->db->error()["message"], "debug" => $this->db->last_query()]);
exit;
}
}
}
}