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

309 lines
10 KiB
PHP

<?php
class Titip_extend extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->PAYMENT_TYPE_ID = 20;
$this->USER_ID = 1500;
}
function get_branch_ip($branchID)
{
$sql =
"select * from m_branch where M_BranchID = ? and M_BranchIsActive ='Y'";
$resp = $this->get_one_row($sql, [$branchID]);
if ($resp[0]) {
return $resp[1]["M_BranchIPAddress"];
} else {
$this->log("\tError Get Ip Address : " . $resp[1]);
}
return "";
}
function r_extend($debug=0) {
$sql = "select
m.M_MouID, m.M_MouNumber, m.M_MouName
from
f_bill_issue_pusat_extend
join f_bill_issue_pusat_extend_mou on F_BillIssuePusatExtendMouF_BillIssuePusatExtendID = F_BillIssuePusatExtendID
and F_BillIssuePusatExtendIsActive = 'Y'
and F_BillIssuePusatExtendLastUpdate + interval 3 day > now()
and F_BillIssuePusatExtendMouIsActive = 'Y'
join f_bill_issue_pusat_detail ON F_BillIssuePusatDetailF_BillIssuePusatID = F_BillIssuePusatExtendF_BillIssuePusatID
join f_bill_titip_detail b ON b.F_BillDetailF_BillID = F_BillIssuePusatDetailF_BillID
AND b.M_BranchID = F_BillIssuePusatDetailM_BranchID
join m_mou m ON m.M_MouID = b.M_MouID
GROUP BY m.M_MouID
";
$resp = $this->get_rows($sql);
if ($debug == 1) {
echo $this->db->last_query();
print_r($resp);
}
if(!$resp[0]) {
$this->reply_gz(["status" => "ERR", "message" => $resp[1]], $debug == 1) ;
exit();
}
$this->reply_gz(["status"=>"OK","data"=>$resp[1]], $debug == 1);
}
public function index()
{
$this->log("Starting Extend Titipan.");
list($regionalID, $branchCode, $branchID) = $this->get_branch();
if (!$regionalID) {
$this->log("Error No Default Branch");
exit();
}
$urls = $this->url_branches($regionalID);
foreach ($urls as $u) {
$url = $u["url"];
$name = $u["name"];
$this->log("Check Extend from $name : $url");
$resp = $this->get($url);
if ($resp["status"] == "ERR") {
$this->log("\t Error : " . $resp["message"]);
continue;
}
foreach ($resp["data"] as $d) {
$this->log(
"\t Titip Extend : " .
$d["M_MouNumber"] . " " . $d["M_MouName"] .
" | M_MouID : " . $d["M_MouID"]
);
$sql = "select M_MouIsActive from m_mou where M_MouID = ?";
$resp = $this->get_one_row($sql,[$d["M_MouID"]]);
if (!$resp[0]) {
$this->log("\t Error : " . $resp[1]);
continue;
}
if ($resp[1]["M_IsActive"] == "N") {
$this->log("\t Mou sudah di hapus.");
}
if ($resp[1]["M_IsActive"] == "H") {
$this->log("\t Mou sudah di extend.");
continue;
}
$sql ="update m_mou set M_MouIsActive = 'Y' where M_MouID = ?";
$qry = $this->db->query($sql,[$d["M_MouID"]]);
if (!$qry) {
$this->log("\t Error update m_mou : " . $this->db->error()["message"]. "|" . $this->db->last_query());
continue;
} else {
$this->log("\t Sukses extend Mou " . $d["M_MouNumber"] . " " . $d["M_MouName"]);
}
}
$this->log( "\tDone.");
}
}
function log($msg)
{
$sdate = date("Y-m-d H:i:s");
echo "$sdate $msg\n";
}
function insert_or_update($table, $dt, $keys, $keyID)
{
$s_where = "";
$param = [];
foreach ($keys as $k) {
if ($s_where != "") {
$s_where .= " and ";
}
$s_where .= " $k = ?";
$param[] = $dt[$k];
}
$sql = "select $keyID
from $table
where $s_where ";
$qry = $this->db->query($sql, $param);
if (!$qry) {
return [
"status" => "ERR",
"message" =>
$this->db->error()["message"] .
"|" .
$this->db->last_query(),
];
}
$rows = $qry->result_array();
$status = "Insert";
if (count($rows) > 0) {
$lastInsertID = $rows[0][$keyID];
foreach ($keys as $k) {
$this->db->where($k, $dt[$k]);
}
$qry = $this->db->update($table, $dt);
if (!$qry) {
return [
"status" => "ERR",
"message" =>
"ERR Update : " .
$this->db->error()["message"] .
"|" .
$this->db->last_query(),
];
}
$status = "Update";
} else {
$qry = $this->db->insert($table, $dt);
if (!$qry) {
return [
"status" => "ERR",
"message" =>
"ERR Insert : " .
$this->db->error()["message"] .
"|" .
$this->db->last_query(),
];
}
$lastInsertID = $this->db->insert_id();
}
return [
"status" => "OK",
"message" => $status,
"insertID" => $lastInsertID,
];
}
function reply_gz($param, $debug = false)
{
if ($debug) {
echo json_encode($param);
} else {
echo gzcompress(json_encode($param), 9);
}
}
function get_param()
{
$zdata = file_get_contents("php://input");
$data = gzuncompress($zdata);
$param = json_decode($data, true);
if (json_last_error() != JSON_ERROR_NONE) {
echo json_encode([
"status" => "ERR",
"message" => "Json Error : " . json_last_error_msg(),
"raw" => $data,
]);
exit();
}
return $param;
}
function get_branch()
{
$sql = "select M_BranchS_RegionalID,M_BranchCode,M_BranchID
from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'";
$qry = $this->db->query($sql);
if (!$qry) {
return [false, $this->db->error()["message"]];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
return ["ERR", "No Default Branch"];
}
return [
$rows[0]["M_BranchS_RegionalID"],
$rows[0]["M_BranchCode"],
$rows[0]["M_BranchID"],
];
}
function url_branches($regionalID)
{
$sql = "select M_BranchCode,M_BranchIpAddress,M_BranchName
from m_branch where M_BranchS_RegionalID = ? and M_BranchIsActive = 'Y'";
$qry = $this->db->query($sql, [$regionalID]);
if (!$qry) {
return [false, $this->db->error()["message"]];
}
$rows = $qry->result_array();
$url = [];
foreach ($rows as $r) {
$url[] = [
"url" =>
"http://" .
$r["M_BranchIpAddress"] .
"/one-api/keu/titip_extend/r_extend" ,
"name" => $r["M_BranchName"],
];
}
return $url;
}
public function post($url, $data)
{
$ch = curl_init($url);
$zdata = gzcompress($data, 9);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $zdata);
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($zdata),
]);
$zresult = 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);
$result = gzuncompress($zresult);
$jresult = json_decode($result, true);
if (json_last_error() != 0) {
return [
"status" => "ERR",
"message" => "Invalid JSON : " . $result,
];
}
return $jresult;
}
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);
$zresult = curl_exec($ch);
$err_msg = curl_error($ch);
if ($err_msg != "") {
return ["status" => "ERR", "message" => $err_msg];
}
$result = gzuncompress($zresult);
$jresult = json_decode($result, true);
if (json_last_error() != 0) {
return [
"status" => "ERR",
"message" => "Invalid JSON : " . $result,
];
}
return $jresult;
}
function get_rows($sql, $prm = false)
{
if ($prm) {
$qry = $this->db->query($sql, $prm);
} else {
$qry = $this->db->query($sql);
}
if (!$qry) {
$msg = "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
return [false, $msg];
}
$rows = $qry->result_array();
return [true, $rows];
}
function get_one_row($sql, $prm)
{
list($status, $msg) = $this->get_rows($sql, $prm);
if ($status !== true) {
return [$status, $msg];
}
if (count($msg) == 0) {
return [false, "Record not found. | " . $msg];
}
return [true, $msg[0]];
}
}