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

151 lines
4.6 KiB
PHP

<?php
class Download_sync_branch extends MY_Controller
{
function __contruct()
{
parent::__contruct();
}
function get_branch()
{
$sql =
"select M_BranchCode from m_branch where M_BranchIsDefault='Y' and M_BranchIsActive ='Y'";
$qry = $this->db->query($sql);
if (!$qry) {
$this->log(
"Error Get Default Branch | " .
$this->db->error()["message"] .
"\n" .
$this->db->last_query()
);
exit();
}
$rows = $qry->result_array();
if(count($rows) == 0) {
$this->log(
"Error No Default Branch "
);
exit();
}
return $rows[0]["M_BranchCode"];
}
// curl -o data.gz http://bandungraya.aplikasi.web.id/one-api/pramitalabku/sync
function index()
{
//download nasional data
// php /home/one/project/one/one-api/index.php pramitalabku download_sync
$this->log("Start Download from Nasional");
$url = "http://bandungraya.aplikasi.web.id/one-api/pramitalabku/sync_branch";
$branchCode = $this->get_branch();
$jdata = $this->post(
$url,
json_encode(["branchCode" => $branchCode ])
);
$resp = json_decode($jdata, true);
if ($resp["status"] != "OK") {
$this->log($resp["message"]);
exit();
}
foreach ($resp["data"] as $table => $rows) {
switch ($table) {
case "t_subcategory":
$keys = ["T_SubCategoryID"];
break;
case "t_subcategory_test":
$keys = ["T_SubcategoryTestID"];
break;
default:
$this->log("Table $table not configured yet!");
exit();
}
$tot_insert = 0;
$tot_update = 0;
foreach ($rows as $r) {
$state = $this->insert_or_update($table, $r, $keys);
if ($state == "U") {
$tot_update++;
} else {
$tot_insert++;
}
}
$tot_rows = $tot_insert + $tot_update;
$this->log(
"$table , $tot_insert inserted / $tot_update updated total : $tot_rows"
);
}
}
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, [
"Content-Type: application/json",
"Content-Length: " . strlen($data),
]);
$result = curl_exec($ch);
$err_msg = curl_error($ch);
if ($err_msg != "") {
$this->log("Err Post " . $err_msg);
exit();
}
return gzuncompress($result);
}
function insert_or_update($table, $data, $key)
{
$s_where = "";
foreach ($key as $k) {
if ($s_where != "") {
$_where .= " and ";
}
$s_where .= " $k = '" . $data[$k] . "' ";
}
$this->db->trans_begin();
$sql = "select " . $key[0] . " from $table " . " where $s_where ";
$qry = $this->db->query($sql, $key);
if (!$qry) {
$this->log(
"Error Insert/Update $table Check | " .
$this->db->error()["message"] .
"\n" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$rows = $qry->result_array();
if (count($rows) > 0) {
foreach ($key as $k) {
$this->db->where($k, $data[$k]);
}
$qry = $this->db->update($table, $data);
$state = "U";
} else {
$qry = $this->db->insert($table, $data);
$state = "I";
}
if (!$qry) {
$this->log(
"Error Insert/Update $table | " .
$this->db->error()["message"] .
"\n" .
$this->db->last_query()
);
$this->db->trans_rollback();
exit();
}
$this->db->trans_commit();
return $state;
}
function log($message)
{
echo date("Y-m-d H:i:s ") . $message . "\n";
}
}
?>