Files
BE_CPONE/application/controllers/tools/regonline/Heartbeat-prod.php
2026-04-27 10:26:26 +07:00

517 lines
17 KiB
PHP

<?php
class Heartbeat extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->url_heartbeat = "https://regonline.pramita.co.id/one-api/tools/regonline/r_heartbeat";
}
function get_sel_px()
{
$sql = "select M_MouID from m_mou where M_MouIsActive = 'Y'
and M_MouIsOnline = 'Y' and M_MouIsReleased = 'Y'";
$qry = $this->db->query($sql);
if (!$qry) {
echo date("Y-m-d H:i:s") . " Err GET SEL PX: " . $this->db->error()["message"] . "\n";
return "0";
}
$rows = $qry->result_array();
$mou_ids = "0";
foreach ($rows as $r) {
$mou_ids .= "," . $r["M_MouID"];
}
$sql = "select T_TestID
from
ss_price_mou
where Ss_PriceMouM_MouID in ( $mou_ids )
and is_packet = 'N'";
$qry = $this->db->query($sql);
if (!$qry) {
echo date("Y-m-d H:i:s") . " Err Ss PriceMou Test : " . $this->db->error()["message"] . "\n";
return "0";
}
$rows = $qry->result_array();
$result = "0";
foreach ($rows as $r) {
$result .= "," . $r["T_TestID"];
}
return $result;
}
function get_order($date)
{
if ($date == "") {
$date = date("Y-m-d 00:00:01");
}
list($branchID, $branchCode) = $this->getBranch();
$px_ids = $this->get_sel_px();
$sql = "select T_OrderHeaderID branch_OrderT_OrderHeaderID,
T_OrderHeaderLabNumber branch_OrderT_OrderHeaderLabNumber,
$branchID branch_OrderM_BranchID,
'$branchCode' branch_OrderM_BranchCode,
T_OrderHeaderDate branch_OrderT_OrderHeaderDate,
T_OrderDetailT_TestID branch_OrderT_TestID,
T_OrderDetailT_TestName branch_OrderT_TestName,
datediff(T_OrderPromiseDateTime, T_OrderHeaderDate) branch_OrderCitoInDay,
T_OrderDetailIsActive branch_OrderIsActive,
T_OrderDetailValidation branch_OrderValidation
from t_orderheader
join t_orderheaderaddon on T_OrderHeaderID = T_OrderHeaderAddOnT_OrderHeaderID
and T_OrderHeaderIsActive = 'Y'
and T_OrderHeaderAddOnFoVerification = 'Y'
and T_OrderHeaderAddOnLabNumberOrigin is null
join t_orderdetail on T_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderDetailLastUpdated >= ?
and date(T_OrderHeaderDate) >= date(?)
and T_OrderDetailT_TestID in ( $px_ids )
join t_orderpromise on T_OrderDetailT_OrderPromiseID = T_OrderPromiseID";
$qry = $this->db->query($sql, array($date, $date));
if (!$qry) {
echo date("Y-m-d H:i:s") . " Err : " . $this->db->error()["message"] . "\n";
return array("type" => "ORDER_BRANCH", "data" => []);
}
$rows = $qry->result_array();
return array("type" => "ORDER_BRANCH", "data" => $rows);
}
function now()
{
return Date("Y-m-d H:i:s");
}
function get_price($date = "")
{
// check the upload konfirmasion
$sql = "select count(*) total
from s_regonline_upload
where S_RegOnlineUploadIsSent = 'N' and S_RegOnlineUploadIsActive = 'Y'";
$qry = $this->db->query($sql);
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$rows = $qry->result_array();
$tot_reqonline_upload = 0;
if (count($rows) > 0) {
$tot_reqonline_upload = $rows[0]["total"];
}
if ($tot_reqonline_upload == 0) {
echo "{$this->now()} ERR : No Upload Confirmation \n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$sql = "update s_regonline_upload set S_RegOnlineUploadIsSent = 'Y',
S_RegOnlineUploadSentDate = now()
where S_RegOnlineUploadIsSent = 'N' ";
$qry = $this->db->query($sql);
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$sql = "select * from m_branch where M_BranchIsActive='Y' and M_BranchIsDefault='Y'";
$qry = $this->db->query($sql);
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "{$this->now()} ERR : No Default Branch\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$branchID = $rows[0]["M_BranchID"];
//Online Released MOU
$sql = "select * from m_mou where
M_MouIsReleased='Y' and
M_MouIsOnline='Y'
order by M_MouID desc";
$qry = $this->db->query($sql);
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "{$this->now()} ERR : Online Released MOU not found\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$mou = $rows[0];
$mouID = $mou["M_MouID"];
//SsPrice MOU
$sql = "select * from ss_price_mou where Ss_PriceMouM_MouID = ?";
$qry = $this->db->query($sql, array($mou["M_MouID"]));
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "{$this->now()} ERR : Ss Price MOU not found\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$ss_price_mou = $rows;
//Working
$sql = "select $branchID M_RegPacketWorkM_BranchID,
m_reg_packetwork.*, T_PacketName
from m_reg_packetwork
join t_packet on M_RegPacketWorkT_PacketID = T_PacketID
where M_RegPacketWorkIsActive = 'Y'
and M_RegPacketWorkM_MouID=?";
$qry = $this->db->query($sql, array($mouID));
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$rows = $qry->result_array();
$arr_working = array();
foreach ($rows as $r) {
$arr_working[ strtolower($r["T_PacketName"])] = true;
}
$sql = "select $branchID M_RegPacketWorkM_BranchID,
m_reg_packetwork.*
from m_reg_packetwork
join t_packet on M_RegPacketWorkT_PacketID = T_PacketID
and M_RegPacketWorkM_MouID=?
";
$qry = $this->db->query($sql, array($mouID));
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$reg_working = $qry->result_array();
//Sale
$sql = "select $branchID M_RegPacketSaleM_BranchID,
m_reg_packetsale.* , T_PacketName
from m_reg_packetsale
join t_packet on M_RegPacketSaleT_PacketID = T_PacketID
where M_RegPacketSaleIsActive = 'Y'
and M_RegPacketSaleM_MouID=?";
$qry = $this->db->query($sql, array($mouID));
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$rows = $qry->result_array();
$arr_sell = array();
$sale_ids = "0";
foreach ($rows as $r) {
$arr_sell[ strtolower($r["T_PacketName"]) ] = true;
$sale_ids .= "," . $r["M_RegPacketSaleID"];
}
$sql = "select $branchID M_RegPacketSaleOptionM_BranchID,
m_reg_packetsaleoption.*
from m_reg_packetsaleoption
where M_RegPacketSaleOptionM_RegPacketSaleID in ( $sale_ids )
";
$qry = $this->db->query($sql, array($mouID));
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$reg_sell_option = $qry->result_array();
$sql = "select $branchID M_RegPacketSaleM_BranchID,
m_reg_packetsale.*
from m_reg_packetsale
join t_packet on M_RegPacketSaleT_PacketID = T_PacketID
and M_RegPacketSaleM_MouID=?
";
$qry = $this->db->query($sql, array($mouID));
if (!$qry) {
echo "{$this->now()} ERR : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$reg_sell = $qry->result_array();
$s_packet = "-1";
foreach ($ss_price_mou as $idx => $r) {
$ss_price_mou[$idx]["Ss_PriceMouM_BranchID"] = $branchID;
$is_sell = "N";
$is_work = "N";
$packetName = strtolower($r["T_TestName"]);
$packetID = $r["packet_id"];
if ($packetID > 0) {
if (isset($arr_working[$packetName])) $is_work = "Y";
if (isset($arr_sell[$packetName])) $is_sell = "Y";
}
$ss_price_mou[$idx]["Ss_PriceMouIsSell"] = $is_sell;
$ss_price_mou[$idx]["Ss_PriceMouIsWork"] = $is_work;
$s_packet .= "," . $packetID;
}
$sql = "select $branchID T_PacketM_BranchID, t_packet.*
from t_packet where T_PacketID in ( $s_packet) ";
// and T_PacketLastUpdated >= ? ";
$qry = $this->db->query($sql, array($date));
if (!$qry) {
echo "{$this->now()} ERR Packet : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$packets = $qry->result_array();
$sql = "select $branchID T_PacketDetailM_BranchID, t_packetdetail.*
from t_packetdetail
where T_PacketDetailT_PacketID in ( $s_packet) and T_PacketDetailIsActive = 'Y'";
$qry = $this->db->query($sql, array($date));
if (!$qry) {
echo "{$this->now()} ERR Packet : {$this->db->error()['message']}\n";
return array("type" => "ERR_PRICE_PACKET", "data" => array());
}
$packet_details = $qry->result_array();
$data = array(
"ss_price_mou" => $ss_price_mou,
"packet" => $packets,
"working" => $reg_working,
"sell" => $reg_sell,
"sell_option" => $reg_sell_option,
"detail" => $packet_details
);
return array("type" => "PRICE_PACKET", "data" => $data);
}
function index()
{
$this->process();
}
function process($addhoc_date = "")
{
$sql = "select * from regonline.heartbeat";
$qry = $this->db->query($sql);
if (!$qry) {
echo date("Y-m-d H:i:s") . " Err : " . $this->db->error()["message"] . "\n";
exit;
}
$heartbeat = array();
$rows = $qry->result_array();
foreach ($rows as $r) {
$heartbeat[$r["heartbeatCode"]] = $r["heartbeatExecuted"];
if ($addhoc_date != "") {
$heartbeat[$r["heartbeatCode"]] = "$addhoc_date 00:00:01";
}
}
//Ping
list($branchID, $branchCode) = $this->getBranch();
$ping_param = array("type" => "PING", "data" => array("branchID" => $branchID, "branchCode" => $branchCode));
//SWAB Schedule
$swab_param = $this->upload_swab($heartbeat["SWAB"]);
//QUOTA PCR
$quota_param = $this->upload_kuota($heartbeat["QUOTA_PCR"]);
//ORDER BRANCH
$order_branch_param = $this->get_order($heartbeat["ORDER_BRANCH"]);
$price_packet_param = $this->get_price($heartbeat["PRICE_PACKET"]);
//do the heartbeat
echo date("Y-m-d H:i:s") . " Start HeartBeat to : {$this->url_heartbeat}\n";
$data = array(
$ping_param, $swab_param, $quota_param, $order_branch_param,
$price_packet_param
);
$md5 = md5(json_encode($data));
$z_param = gzdeflate(json_encode(array(
"md5" => $md5,
"data" => $data
)), 9);
$response = $this->post($this->url_heartbeat, $z_param);
$j_response = json_decode($response, true);
if (!$j_response) {
echo date("Y-m-d H:i:s") . " End HeartBeat : Error Json : $response\n";
exit;
}
if ($j_response["status"] == "ERR") {
echo date("Y-m-d H:i:s") . " End HeartBeat : Error : {$j_response['message']}\n";
exit;
}
foreach ($j_response["result"] as $type => $rst) {
$msg = "";
if (isset($rst["message"])) $msg = $rst["message"];
$status = $rst["status"];
$this->update_heartbeat($type, $status, $msg);
echo date("Y-m-d H:i:s") . " \t $type : $status : $msg \n";
}
}
function update_heartbeat($type, $status, $msg)
{
//jika tidak OK executed tidak di set
if ($status == "OK") {
$sql = "update regonline.heartbeat set heartbeatExecuted = now(), heartbeatStatus = ? , heartbeatMessage = ?
where heartbeatCode = ?";
$qry = $this->db->query($sql, array($status, $msg, $type));
} else {
$sql = "update regonline.heartbeat set heartbeatStatus = ? , heartbeatMessage = ?
where heartbeatCode = ?";
$qry = $this->db->query($sql, array($status, $msg, $type));
}
if (!$qry) {
echo date("Y-m-d H:i:s") . " \t Error $type : {$this->db->error()['message']}\n";
}
}
function upload_kuota($date)
{
list($branchID, $branchCode) = $this->getBranch();
if ($date == "") $date = "2020-01-01 00:00:00";
$sql = "select m_reg_kuota.*, $branchID M_RegKuotaM_BranchID, '$branchCode' M_RegKuotaM_BranchCode
from m_reg_kuota where M_RegKuotaLastUpdated > ?";
$qry = $this->db->query($sql, array($date));
if (!$qry) {
echo date("Y-m-d H:i:s") . " Error Kuota : " . $this->db->error()["message"] . "\n";
exit;
}
$kuota = $qry->result_array();
$sql = "select m_reg_kuota_log.*, $branchID M_RegKuotaLogM_BranchID, '$branchCode' M_RegKuotaLogM_BranchCode
from m_reg_kuota_log where M_RegKuotaLogLastUpdated > ?";
$qry = $this->db->query($sql, array($date));
if (!$qry) {
echo date("Y-m-d H:i:s") . " Error Kuota Log : " . $this->db->error()["message"] . "\n";
exit;
}
$log = $qry->result_array();
$param = array("type" => "QUOTA_PCR", "data" => array("kuota" => $kuota, "log" => $log));
return $param;
}
function upload_swab($date)
{
list($branchID, $branchCode) = $this->getBranch();
if ($date == "") $date = "2020-01-01 00:00:00";
$sql = "select m_reg_schedule.*, $branchID M_RegScheduleM_BranchID, '$branchCode' M_RegScheduleM_BranchCode
from m_reg_schedule where M_RegScheduleIsActive = 'Y'";
$qry = $this->db->query($sql, array($date));
if (!$qry) {
echo date("Y-m-d H:i:s") . " Error Schedule : " . $this->db->error()["message"] . "\n";
exit;
}
$schedule = $qry->result_array();
$sql = "select m_reg_scheduledetail.*, $branchID M_RegScheduleDetailM_BranchID, '$branchCode' M_RegScheduleDetailM_BranchCode
from m_reg_scheduledetail where M_RegScheduleDetailIsActive = 'Y'";
$qry = $this->db->query($sql, array($date));
if (!$qry) {
echo date("Y-m-d H:i:s") . " Error Schedule : " . $this->db->error()["message"] . "\n";
exit;
}
$detail = $qry->result_array();
$param = array("type" => "SWAB", "data" => array("schedule" => $schedule, "detail" => $detail));
return $param;
}
function get_mou()
{
$sql = "select S_SystemIPAddressRegional from conf_systems";
$qry = $this->db->query($sql);
if (!$qry) {
return "devone.aplikasi.web.id";
}
$rows = $qry->result_array();
if (count($rows) > 0) return $rows[0]["S_SystemIPAddressRegional"];
}
function do_insert($table, $r)
{
$qry = $this->db->insert($table, $r);
if (!$qry) echo $this->db->error()["message"] . " \n";
}
function do_update($table, $id, $r)
{
$this->db->set($r);
$this->db->where($id, $r[$id]);
$qry = $this->db->update($table);
if (!$qry) echo $this->db->error()["message"] . " \n";
}
function exists($table, $id, $val)
{
$sql = "select count(*) total from {$table} where {$id} = ? ";
$qry = $this->db->query($sql, array($val));
if (!$qry) {
echo date("Y-m-d H:i:s") . " Err : " . $this->db->error()["message"] . "\n";
exit;
}
$rows = $qry->result_array();
return $rows[0]["total"] > 0;
}
function update_mou($mou)
{
foreach ($mou as $m) {
$mouID = $m["selMouID"];
if ($this->exists("regonline.sel_mou", "selMouID", $mouID)) {
$this->do_update("regonline.sel_mou", "selMouID", $m);
} else {
$this->do_insert("regonline.sel_mou", $m);
}
}
}
function update_px($px)
{
foreach ($px as $r) {
$pxID = $r["selPxID"];
if ($this->exists("regonline.sel_px", "selPxID", $pxID)) {
$this->do_update("regonline.sel_px", "selPxID", $r);
} else {
$this->do_insert("regonline.sel_px", $r);
}
}
}
function getBranch()
{
$sql = "select * from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'";
$qry = $this->db->query($sql);
if (!$qry) {
echo date("Y-m-d H:i:s") . " Err Get Branch: " . $this->db->error()["message"] . "\n";
exit;
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo date("Y-m-d H:i:s") . " Err Get Branch: " . $this->db->error()["message"] . "\n";
exit;
}
return array($rows[0]["M_BranchID"], $rows[0]["M_BranchCode"]);
}
function getRegionalIP()
{
$sql = "select S_SystemIPAddressRegional from conf_systems";
$qry = $this->db->query($sql);
if (!$qry) {
return "devone.aplikasi.web.id";
}
$rows = $qry->result_array();
if (count($rows) > 0) return $rows[0]["S_SystemIPAddressRegional"];
return "devone.aplikasi.web.id";
}
function get($url, $timeout = 60, $c_timeout = 5)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $c_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$err_msg = curl_error($ch);
if ($err_msg != "") {
return json_encode(array("status" => "ERR", "message" => $err_msg));
}
return gzinflate($result);
}
function post($url, $data, $timeout = 60, $c_timeout = 5)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $c_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
));
$result = curl_exec($ch);
$err_msg = curl_error($ch);
if ($err_msg != "") {
return json_encode(array("status" => "ERR", "message" => $err_msg,
"url" => $url, "data" => json_decode($data,true)));
}
return gzinflate($result);
}
}