Files
BE_IBL/application/controllers/mitra/Uploader.php
2026-04-15 15:23:57 +07:00

219 lines
7.0 KiB
PHP

<?php
class Uploader extends MY_Controller
{
function __construct()
{
parent::__construct();
}
//general uploader
//mitra
//mitra_mou
//ss_price_mou
//test
//t_sampletype
function get_branch()
{
$sql = "select M_BranchID,M_BranchCode
from m_branch where
M_BranchIsDefault = 'Y' and M_BranchIsActive ='Y'";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err($this->db_error("Get Default Branch"));
}
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->reply_err("No Default Branch");
}
return [$rows[0]["M_BranchID"], $rows[0]["M_BranchCode"]];
}
function index()
{
list($branchID, $branchCode) = $this->get_branch();
$xnow = date("Y-m-d h:i:s");
$last_upload = $this->get_last_upload();
$this->log(
"Start Upload MasterData Mitra | Last Uploaded $last_upload"
);
$sql = "select '$branchCode' M_BranchCode, mitra.* from mitra where mitraLastUpdated > ? and MitraIsActive = 'Y'";
$qry = $this->db->query($sql, [$last_upload]);
if (!$qry) {
$this->reply_err($this->db_error("Get Mitra"));
}
$mitra = $qry->result_array();
if (count($mitra) == 0) {
$this->log("Done no pending Upload");
}
$s_ids = "0";
foreach ($mitra as $m) {
$s_ids .= "," . $m["MitraID"];
}
$sql = "select '$branchCode' M_BranchCode,mitra_mou.* from mitra_mou where MitraMouMitraID in ($s_ids) and MitraMouIsActive = 'Y'";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err($this->db_error("Get Mitra Mou"));
}
$mitra_mou = $qry->result_array();
if (count($mitra_mou) == 0) {
$this->log("Done no pending Mitra Mou Upload");
}
$s_ids = "0";
foreach ($mitra_mou as $mm) {
$s_ids .= ", " . $mm["MitraMouM_MouID"];
}
$sql = "select distinct '$branchCode' M_BranchCode,m_mou.* from m_mou where M_MouID in ($s_ids)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err($this->db_error("Get Mou"));
}
$mou = $qry->result_array();
$sql = "select distinct '$branchCode' M_BranchCode,ss_price_mou.* from ss_price_mou where Ss_PriceMouM_MouID in ($s_ids)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err($this->db_error("Get SsPriceMou"));
}
$ss_price_mou = $qry->result_array();
$t_test = [];
foreach ($ss_price_mou as $sp) {
if (!in_array($sp["T_TestID"], $t_test)) {
$t_test[] = $sp["T_TestID"];
}
$ct = json_decode($sp["child_test"], true);
foreach ($ct as $ict) {
if (!in_array($ict["T_TestID"], $t_test)) {
$t_test[] = $ict["T_TestID"];
}
}
}
//Test
//SampleType
$test = [];
$sampletype = [];
if (count($t_test) > 0) {
$s_ids = implode(",", $t_test);
$sql = "select distinct * from t_test
where T_TestID in ($s_ids)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err($this->db_error("Get Test"));
}
$test = $qry->result_array();
$sql = "select distinct t_sampletype.*
from t_sampletype
join t_test on T_TestT_SampletypeID = T_SampleTypeID
and T_TestID in ($s_ids)";
$qry = $this->db->query($sql);
if (!$qry) {
$this->reply_err($this->db_error("Get SampleType"));
}
$sampletype = $qry->result_array();
}
foreach ($mitra as $idx => $m) {
$mitraID = $m["MitraID"];
$x_mou = array_filter($mitra_mou, function ($mu) use ($mitraID) {
return $mitraID == $mu["MitraMouMitraID"];
});
$x_mou = array_values($x_mou);
$mitra[$idx]["mou"] = $x_mou;
}
$a_payload = [
"mitra" => $mitra,
"mou" => $mou,
"test" => $test,
"ss_price_mou" => $ss_price_mou,
"sampletype" => $sampletype,
];
$md5 = md5(json_encode($a_payload));
$param = ["md5" => $md5, "payload" => $a_payload];
$zparam = gzcompress(json_encode($param));
$service =
"https://devregonline.pramita.co.id/one-api/mitra/r_uploader";
$resp = $this->post($service, $zparam);
print_r($resp);
}
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, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/octet",
"Content-Length: " . strlen($data),
]);
$result = curl_exec($ch);
if (curl_error($ch) != "") {
return "ERROR Accessing QrCode : " . curl_error($ch) . "\n";
}
curl_close($ch);
return $result;
}
function log($msg)
{
$xnow = date("Y-m-d H:i:s");
echo $xnow . " $msg\n";
}
function reset_last_upload()
{
$file = "/xtmp/mitra-last-uploaded.json";
unlink($file);
}
function update_last_upload($inp)
{
$file = "/xtmp/mitra-last-uploaded.json";
file_put_contents($file, json_encode($inp));
}
function get_last_upload($debug = "")
{
$file = "/xtmp/mitra-last-uploaded.json";
if (!file_exists($file)) {
$last_upload = date("Y-m-d h:i:s", strtotime("now - 130 day"));
if ($debug != "") {
echo "Last Upload : $last_upload\n";
}
return $last_upload;
}
$last_upload = json_decode(file_get_contents($file));
if ($last_upload == "") {
$last_upload = date("Y-m-d h:i:s", strtotime("now - 130 day"));
if ($debug != "") {
echo "Last Upload : $last_upload\n";
}
return $last_upload;
}
if ($debug != "") {
echo "Last Upload : $last_upload\n";
}
return $last_upload;
}
function db_error($prefix)
{
$msg =
$prefix .
"| " .
$this->db->error()["message"] .
"| " .
$this->db->last_query();
return $msg;
}
function reply($data = [])
{
echo json_encode([
"status" => "OK",
"data" => $data,
]);
exit();
}
function reply_err($message = "")
{
echo json_encode([
"status" => "ERR",
"message" => $message,
]);
exit();
}
}